[Haskell-cafe] Type trickery
andrewcoppin at btinternet.com
Wed Mar 16 14:52:11 CET 2011
>> Hmm, yes. That will work, but I wonder if there's some way of doing this
>> that doesn't limit the scope of the container to one single span of
> You can write helper functions which take containers as argument by
> parameterizing these helper functions over s:
> takesTwoContainers :: Container s1 -> Container s2 -> ...
> takesTwoContainers c1 c2 = ... -- c1 and c2 can be used here
> This function could be called like this:
> withContainer (\c1 ->
> withContainer (\c2 ->
> takesTwoContainers c1 c2)) -- c1 and c2 can be used here
> In this example, the scope of the containers is not limited to a single
> span of code.
What you can't do is write functions such as
foo :: Container x -> (Cursor x, Cursor x)
Perhaps this property is just too tricky to check in the type system.
It's quite possible to check it at run-time; I'd just prefer to check at
compile-time, if it's not too difficult to implement.
I thought of a further complication: What happens if you concatinate two
containers? Then cursors to each of the original containers all become
valid for the new, concatenated container. I suppose you could issue a
new phantom type variable for the concatenated version, but the whole
idea of mutable containers is to, you know, mutate them.
Given the above, perhaps it's just not possible to check this kind of
thing statically at all.
More information about the Haskell-Cafe