我在我的程序中苦于存在类型。我想我试图做一些非常合理的,但是我不能让过去的typechecker :(Haskell中的存在类型错误
我有一个数据类型,那种模仿的单子
data M o = R o | forall o1. B (o1 -> M o) (M o1)
现在,我为它创建一个背景下,类似要在Haskell Wiki article on Zipper描述,但是我用的功能,而不是一个数据结构为简单起见 -
type C o1 o2 = M o1 -> M o2
现在,当我尝试写一个分割数据值到它的上下文和子值函数,因此typechecker抱怨 -
ctx :: M o -> (M o1 -> M o, M o1)
ctx (B f m) = (B f, m) -- Doesn't typecheck
错误是 -
Couldn't match type `o2' with `o1'
`o2' is a rigid type variable bound by
a pattern with constructor
B :: forall o o1. (o1 -> M o) -> M o1 -> M o,
in an equation for `ctx'
at delme1.hs:6:6
`o1' is a rigid type variable bound by
the type signature for ctx :: M o -> (M o1 -> M o, M o1)
at delme1.hs:6:1
Expected type: M o2
Actual type: M o1
In the expression: m
In the expression: (B f, m)
不过,我可以解决它像这样 -
ctx (B f m) = let (c,m') = ctx m in ((B f) . c, m') -- OK
为什么这第二个定义类型检测,但不是第一位?
另外,如果我尝试通过检查R键ctx
转换成完整的功能,我又得到一个类型检测错误 -
ctx (R o) = (id, R o) -- Doesn't typecheck
错误 -
Couldn't match type `o' with `o1'
`o' is a rigid type variable bound by
the type signature for ctx :: M o -> (M o1 -> M o, M o1)
at delme1.hs:7:1
`o1' is a rigid type variable bound by
the type signature for ctx :: M o -> (M o1 -> M o, M o1)
at delme1.hs:7:1
In the first argument of `R', namely `o'
In the expression: R o
In the expression: (id, R o)
我怎样才能解决这个错误?
任何帮助表示赞赏!
谢谢!使用一个存在的类型,而不是一个元组可观的工作,我在这个过程中学到了很多! –