[Haskell] Why is newChan in the IO Monad?
Alastair Reid
alastair at reid-consulting-uk.ltd.uk
Mon Apr 26 14:35:56 EDT 2004
On Friday 23 April 2004 20:05, S. Alexander Jacobson wrote:
> Yes, that makes sense, but I'm ok with passing in
> an identity. I'd like a function like this:
>
> newChanSafe::Identity -> Chan a
> type Identity = Double -- or whatever
As Nick observes, using this function would require you to pass around a
supply of unique Identitys. If we assume you're going to have to do this,
why not simplify things and pass around a list of newChans:
type Identity = Chan Int
withChan :: (Identity -> a) -> [Identity] -> (a,[Identity])
...
You can use unsafeInterleaveIO to create a lazy list of channels.
Better yet, you can use unsafeInterleaveIO to create a lazy
tree of channels so that splitting the supply is efficient.
One minor detail left as an exercise: my definition of Identity is monomorphic
but you probably want them to be polymorphic. This is a bit tricky to fix
and will require a monad (or equivalent) to ensure that you don't allocate
the same chan twice and then use it at different types. (Probably requires
unsafeCast too.)
--
Alastair Reid
More information about the Haskell
mailing list