[Haskell] Mixing monadic and non-monadic functions

Einar Karttunen ekarttun at cs.helsinki.fi
Thu Sep 15 16:28:55 EDT 2005


On 15.09 23:40, Bulat Ziganshin wrote:
> of course
> 
> class Ref c a where
>   new :: a -> IO (c a)
>   get :: c a -> IO a
>   set :: c a -> a -> IO ()

Maybe even:

class Ref m t where
  new :: a -> m (t a)
  get :: t a -> m a
  set :: t a -> a -> m ()

Or if you want to support things like FastMutInts

class Ref m t v where
  new :: v -> m t
  get :: t -> v -> m a
  set :: t -> v -> m ()

That would even support an evil IOArray instance:

instance Ref IO (IOArray Int v, Int) v where
  new iv            = newArray_ (0,iv)
  get (arr,idx)     = readArray arr idx
  set (arr,idx) val = writeArray arr idx val

- Einar Karttunen


More information about the Haskell mailing list