[Haskell-cafe] Re: Help me refactor this type!

apfelmus apfelmus at quantentunnel.de
Fri Apr 25 07:22:47 EDT 2008


Ryan Ingram wrote:
> More FRP stuff: a new type for Future that came to me in an 
> inspiration this morning.  But it's ugly and I need someone with 
> better theoretical underpinnings than me to tell me what I've really 
> built :)
> 
> data Future t a = Known t a | Unknown (t -> IO (STM (Maybe (Future t a))))

This is a composition of (applicative) functors. Not sure whether this
helps from the theoretical side, but it can be used to considerably
shorten your code:

   type Tower t b = t -> IO (STM (Maybe b))

   fmap4 :: (a -> b) -> Tower a -> Tower b
   fmap4 f = fmap . fmap . fmap . fmap $ f

   delayF :: Ord t => t -> Future t a -> Future t a
   delayF t0 (Known t a) = Known (max t0 t) a
   delayF t0 (Unknown f) = Unknown $ fmap4 (delayF0 t0) f

   instance (Ord t, Bounded t) => Monad (Future t) where
       return = Known minBound
       Known t a >>= g = delayF t (g a)
       Unknown f >>= g = Unknown $ fmap4 (>>= g) f


Regards,
apfelmus



More information about the Haskell-Cafe mailing list