[Haskell-cafe] Rigid types fun

Tillmann Rendel rendel at informatik.uni-marburg.de
Fri Nov 5 11:01:15 EDT 2010


Hi,

Mitar wrote:
> I would like to do that to remove repeating code like:
>
> from<- newChan
> for<- newChan
> let nerve = Nerve (Axon from) (AxonAny for)
>
> which I have to write again and again just to make types work out. Why
> I cannot move that into the function?

One option is to write a little library of functions which create axons 
and nerves:

   newAxon :: Impulse i => IO (Axon (Chan i) i AxonConductive)
   newAxon = do
     chan <- newChan
     return (Axon chan)

   newAxonAny :: IO (Axon (Chan i) AnyImpulse AxonConductive)
   newAxonAny = do
     chan <- newChan
     return (AxonAny chan)

   newNoAxon :: Monad m => m (Axon (Chan i) i AxonNotConductive)
   newNoAxon = do
     return NoAxon

   newNerve :: Monad m => m (Axon a a' b) -> m (Axon c c' d)
                 -> m (Nerve a a' b c c' d)
   newNerve newFrom newFor = do
     from <- newFrom
     for <- newFor
     return (Nerve from for)

Note that newNerve does not take Axons, but rather monadic actions which 
create Axons. Now, you can use something like

   nerve <- newNerve newAxon newAxonAny

to create a concrete nerve.

   Tillmann

PS. It might be an interesting exercise to use either liftM and liftM2 
(from Control.Monad) or the (<$>) and (<*>) operators from 
Control.Applicative to implement these functions without do notation.

PPS. Crosspost removed. I don't want to post to mailing lists which I do 
not read.


More information about the Haskell-Cafe mailing list