[Haskell-cafe] Type trickery

Bas van Dijk v.dijk.bas at gmail.com
Wed Mar 16 12:45:20 CET 2011


On 16 March 2011 11:31, Andrew Coppin <andrewcoppin at btinternet.com> wrote:
> The well-known ST monad uses an ingenious hack to make it impossible for
> distinct ST computations to interact with each other.
>
> Is there a way to do something similar so that I can create "cursors" that
> reference a (mutable) container, and then write a function that takes two
> cursor arguments which are statically guaranteed to refer to the same
> container?

You could define a function:

withContainer ∷ (∀ s. Container s → α) → α

which creates a container, parameterizes it with an 's' that is only
scoped over the continuation and applies the continuation to the
created container.

If you then define a function to create a cursor from it like:

cursor ∷ Container s → Cursor s

it is statically guaranteed that two Cursors parameterized with the
same 's' refer to the same container:

foo ∷ Cursor s → Cursor s → ...

because the only way to create a Container s is through withContainer.

Regards,

Bas



More information about the Haskell-Cafe mailing list