[Haskell] Mixing monadic and non-monadic functions

John Meacham john at repetae.net
Thu Sep 8 19:42:22 EDT 2005


On Thu, Sep 08, 2005 at 01:30:51PM -0700, Frederik Eaton wrote:
> On Thu, Sep 08, 2005 at 09:30:34AM -0700, Scherrer, Chad wrote:
> > One of Mark Jones's articles suggests something like
> > 
> > class Plus a b c | a b -> c where
> >   (+) :: a -> b -> c
> > 
> > Would
> > 
> > instance (Plus a b c, Monad m) => Plus (m a) (m b) (m c) where
> >   mx + my = do x <- mx
> >                y <- my
> >                return (x + y)
> > 
> > do what you're looking for?
> 
> Hi Chad,
> 
> I'm not sure exactly what you have in mind. Obviously I want something
> that applies to all functions, with any number of arguments, and not
> just (+). Furthermore, it should handle cases like 1+[2,3] where only
> one value is monadic. Keean Schupke's suggestion sounds more likely to
> be useful, but I'm still reading it. In any case, a minimum of
> syntactic overhead is desired.

I think what he means is that if we had a somewhat better design of the
prelude type classes, we would be able to do this in haskell now for
most interesting operations. as we could write an instance like

instance (Monad m,Num a) => Num (m a) where
..
..

of course, we can't do this because Num has Ord and Show as superclasses
when it really doesn't need to. (we would have to create a separate
class for 'pattern matchable nums' if we got rid of those, but that is
no problem other than being non-haskell-98 compatable). Solving this
'class inflexibility' problem in general is something I have given some
thought too. I will let everyone know if I figure something out...

        John 

-- 
John Meacham - ⑆repetae.net⑆john⑈ 


More information about the Haskell mailing list