[Haskell] Why is newChan in the IO Monad?

Jan-Willem Maessen - Sun Labs East Janwillem.Maessen at Sun.COM
Mon Apr 26 17:06:54 EDT 2004

If you want to pass in a splittable supply, I recommend saving 
yourself the trouble of building the plumbing.  Instead, try the 
Supply library which I wrote a few years back:


You can create a splittable supply of channels using
 >      chanSupply <- ioSupply newChan

Nonetheless, the problem of generating polymorphic channels is still 
(as Alastair suggests) a bit of an exercise.  You will need to create 
a polymorphic wrapper for the result of "newChan".

-Jan-Willem Maessen
jmaessen at alum.mit.edu

Alastair Reid wrote:
> 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 
> _______________________________________________
> Haskell mailing list
> Haskell at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell

More information about the Haskell mailing list