module Main where
data Toy b next =
Output b next
| Bell next
| Done
data FixE f e = Fix (f (FixE f e)) | Throw e
-- The working monadic function
catch :: (Functor f) => FixE f e1 -> (e1 -> FixE f e2) -> FixE f e2
catch (Fix x) f = Fix (fmap (`catch` f) x)
catch (Throw e) f = f e
-- Type error
applicate_fixe :: (Functor f) => FixE f (e1 -> e2) -> FixE f e1 -> FixE f e2
applicate_fixe a b = a `catch` (`fmap` b)
-- Type error
applicate_fixe' :: (Functor f) => FixE f (e1 -> e2) -> FixE f e1 -> FixE f e2
applicate_fixe' (Throw f) b = fmap f b
applicate_fixe' (Fix f) b = Fix (fmap (`applicate_fixe` b) f)
main :: IO()
main = print "Hello."
C:\!Various_Exercises\Haskell_Exercises\Free_Monad_Stuff\test.hs: 15, 33
Could not deduce (Functor (FixE f)) arising from a use of `fmap'
from the context (Functor f)
bound by the type signature for
applicate_fixe :: Functor f =>
FixE f (e1 -> e2) -> FixE f e1 -> FixE f e2
at test.hs:14:19-76
In the second argument of `catch', namely `(`fmap` b)'
In the expression: a `catch` (`fmap` b)
In an equation for `applicate_fixe':
applicate_fixe a b = a `catch` (`fmap` b)
C:\!Various_Exercises\Haskell_Exercises\Free_Monad_Stuff\test.hs: 18, 31
Could not deduce (Functor (FixE f)) arising from a use of `fmap'
from the context (Functor f)
bound by the type signature for
applicate_fixe' :: Functor f =>
FixE f (e1 -> e2) -> FixE f e1 -> FixE f e2
at test.hs:17:20-77
In the expression: fmap f b
In an equation for applicate_fixe':
applicate_fixe' (Throw f) b = fmap f b
我将结束本教程,试图找出 Free Monad,作为练习,我也在尝试执行 Applicative 函数。老实说,我不确定这些错误应该是什么意思。
此外,我不确定 的类型签名data FixE f e = Fix (f (FixE f e)) | Throw e
应该是什么。起初我认为f (FixE f e)
应该是一个元组,但它似乎是一个参数,因此该(FixE f e)
部分实际上是第一个的类型参数f
。但如果是这种情况,f
内部不应该FixE f e
也需要一个类型参数吗?
编辑:
applicate_fixe :: (Functor f) => FixE f (e1 -> e2) -> FixE f e1 -> FixE f e2
applicate_fixe (Fix f) b = Fix (fmap (`applicate_fixe` b) f) -- Works as the f argument in fmap is a functor
applicate_fixe (Throw f) (Fix b) = fmap f b -- The b is of type f (FixE f e1) so it is clearly a functor and yet the type system rejects it.
在任何事情之前,我不明白最后一部分。还有到底应该定义什么函子的实例?f
在上面的定义中应该已经有了这个约束。
Edit2:也许你的意思是 FixE 应该有一个 Functor 实例。
instance Functor f => Functor (FixE f) where
fmap f (Fix x) = fmap f x -- Type error
fmap f (Throw e) = Throw (f e)
这是我最好的镜头,但它抱怨第一行中的 f 型太僵硬了。