I am struggling with existential types in my program. I think I'm trying to do something very reasonable however I cannot get past the typechecker :(
I have a datatype that sort of mimics a Monad
data M o = R o | forall o1. B (o1 -> M o) (M o1)
Now I create a Context for it, similar to that described in Haskell Wiki article on Zipper, however I use a function instead of a data structure for simplicity -
type C o1 o2 = M o1 -> M o2
Now when I try to write a function that splits a data value into its context and subvalue, the typechecker complains -
ctx :: M o -> (M o1 -> M o, M o1)
ctx (B f m) = (B f, m) -- Doesn't typecheck
Error is -
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)
However, I can work around it like so -
ctx (B f m) = let (c,m') = ctx m in ((B f) . c, m') -- OK
Why does this second definition typecheck but not the first one?
Also, if I try to convert ctx
to a complete function by checking for R, I again get a typecheck error -
ctx (R o) = (id, R o) -- Doesn't typecheck
Error -
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)
How can I work around this error?
Any help is appreciated!