开发者

Putting existentials into monad

I have following problem. I have value of type (forall r. MyType r) and I need ParsecT s u m (forall r. MyType r). Is it possible to do it wi开发者_运维知识库thout suppling additional data structures?


In general, if you have a true existential type, it's not allowed to "escape" into broader scope. The type (forall r. MyType r) means "this is a MyType r for any r you can think of", much like Nothing is of type forall a. Maybe a; it doesn't mean that there exists some unknown r such that you have a MyType of it.

If you actually want a polymorphic term, that's another matter entirely, but I'll assume you really did mean existential.

As I mentioned in the comments on the question, existential types represent a value with a specific but unknown type. Because you don't know the type, all you can do with such a value is apply to it a function which is polymorphic in its argument. There's no way to talk about an existential type directly, so the brief answer to your question is: No.

In order to manipulate an existential type, you can either hide them inside a continuation with a type like forall t. (forall r. MyType r -> t) -> t, or hide them in a data structure like data ExistMyType = forall r. ExistMyType (MyType r). In fact, these are pretty much the same thing, since the continuation amounts to a Church encoding of the data type.

But honestly, I suspect that what you really should do is reconsider your approach. Existential types are awkward, a bit confusing, and usually not the best solution in Haskell. In particular I'm dubious that anything resembling the type you wrote would actually be useful. I could be wrong, though.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜