Send Beginners mailing list submissions to
        [email protected]

To subscribe or unsubscribe via the World Wide Web, visit
        http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        [email protected]

You can reach the person managing the list at
        [email protected]

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1.  suggestions for re-implementing mapM (Vale Cofer-Shabica)
   2. Re:  suggestions for re-implementing mapM (David McBride)
   3.  Multi-parameter type classes and ambiguous type  variables...
      (Stuart Hungerford)
   4. Re:  Multi-parameter type classes and ambiguous type
      variables... (Sumit Sahrawat, Maths & Computing, IIT (BHU))


----------------------------------------------------------------------

Message: 1
Date: Fri, 20 Mar 2015 15:52:46 -0400
From: Vale Cofer-Shabica <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: [Haskell-beginners] suggestions for re-implementing mapM
Message-ID:
        <CAAzfV4R1hevE6PfWD+f-YyuVpa7VJRT=jgh_cgypsn6luyq...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

Hello all,

I've been reading through "Tackling the Awkward Squad" [1] and am
implementing the definitions "left as exercises" as I go. For section 2.2,
I define:

    putLine :: [Char] -> IO ()
    putLine :: mapM_ putChar

where

    mapM_ :: Monad m => (a -> m b) -> a -> m ()
    mapM_ f [] = return ()
    mapM_ f (x:xs) = (f x) >> (mapM_ f xs)

which works without difficulty. For the sake of learning, I decided to
implement mapM as well. My definition (below) works, but seems really
in-elegant. I checked the prelude source and found mapM defined in terms of
sequence, which has some do-notation I'm not so clear on. I'm trying to
avoid using do notation in my implementation because it still feels like
magic. I'll work on de-sugaring do notation again once I have a solid
handle on (>>=), (>>), and return. Any suggestions for cleaning this up
would be much appreciated!

    mapM :: Monad m => (a -> m b) -> [a] -> m [b]
    mapM f [] = return []
    mapM f (x:xs) = consMM (f x) (mapM f xs)

    consMM :: Monad m => m a -> m [a] -> m [a]
    consMM mx mxs = mx >>= ((flip consM)  mxs) where
        consM x mxs = mxs>>=(\xs -> return (x:xs))

Thank you,
vale

[1] Suggested by apfelmus in a recent email to the list:
http://research.microsoft.com/en-us/um/people/simonpj/papers/marktoberdorf/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20150320/7c073c24/attachment-0001.html>

------------------------------

Message: 2
Date: Fri, 20 Mar 2015 16:07:56 -0400
From: David McBride <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] suggestions for re-implementing mapM
Message-ID:
        <can+tr43pv+p-ek-terabebxbsagpts1s_ooysy1lvo-z8pv...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

Sometimes it is easier to write it with do notation and then rewrite it
back in normal terms:

mapM :: Monad m => (a -> m b) -> [a] -> m [b]
mapM _ [] = return []
mapM f (x:xs) = do
  x' <- f x
  xs' <- mapM f xs
  return $ x':xs'

You can rewrite the second part step by step as:

mapM f (x:xs) = f x >>= \x' -> mapM f xs >>= \xs' -> return (x' : xs')

Also the base package does not use do notation.  It defines it much more
elegantly:

mapM            :: Monad m => (a -> m b) -> [a] -> m [b]mapM f as
 =  sequence (map f as)




On Fri, Mar 20, 2015 at 3:52 PM, Vale Cofer-Shabica <
[email protected]> wrote:

> Hello all,
>
> I've been reading through "Tackling the Awkward Squad" [1] and am
> implementing the definitions "left as exercises" as I go. For section 2.2,
> I define:
>
>     putLine :: [Char] -> IO ()
>     putLine :: mapM_ putChar
>
> where
>
>     mapM_ :: Monad m => (a -> m b) -> a -> m ()
>     mapM_ f [] = return ()
>     mapM_ f (x:xs) = (f x) >> (mapM_ f xs)
>
> which works without difficulty. For the sake of learning, I decided to
> implement mapM as well. My definition (below) works, but seems really
> in-elegant. I checked the prelude source and found mapM defined in terms of
> sequence, which has some do-notation I'm not so clear on. I'm trying to
> avoid using do notation in my implementation because it still feels like
> magic. I'll work on de-sugaring do notation again once I have a solid
> handle on (>>=), (>>), and return. Any suggestions for cleaning this up
> would be much appreciated!
>
>     mapM :: Monad m => (a -> m b) -> [a] -> m [b]
>     mapM f [] = return []
>     mapM f (x:xs) = consMM (f x) (mapM f xs)
>
>     consMM :: Monad m => m a -> m [a] -> m [a]
>     consMM mx mxs = mx >>= ((flip consM)  mxs) where
>         consM x mxs = mxs>>=(\xs -> return (x:xs))
>
> Thank you,
> vale
>
> [1] Suggested by apfelmus in a recent email to the list:
> http://research.microsoft.com/en-us/um/people/simonpj/papers/marktoberdorf/
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20150320/a095d696/attachment-0001.html>

------------------------------

Message: 3
Date: Sat, 21 Mar 2015 08:39:37 +1100
From: Stuart Hungerford <[email protected]>
To: [email protected]
Subject: [Haskell-beginners] Multi-parameter type classes and
        ambiguous type  variables...
Message-ID:
        <cag+kmrf3zufgnwfkdadrqafp-5qkmvukbnzf0nadywq4wxh...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8

Hi,

As a learning exercise I'm modelling some algebraic structures as
Haskell typeclasses. Some of these are multi-parameter type classes.
Here's a very simplified version of the type class relationships:

class MM a where
  one :: a

class AM a where
  zero :: a

class (AM a, MM a) => SR a

class (AM a) => AG a where
  inv :: a -> a

class (SR a) => R a where
  neg :: a -> a

class (R r, AG g) => M r g where
  sca :: r -> g -> g

check :: (Eq g, M r g) => g -> Bool
check x = sca one x == x

The problem I have is that GHC is finding the "r" type variable in the
"check" function ambiguous. Given my still limited Haskell knowledge
I'm not surprised this is happening. What I would like to know is how
experienced Haskellers handle this situation in practice: is there an
idiomatic way of disambiguating "r" or is it a sign of poor type class
design?

Thanks,

Stu


------------------------------

Message: 4
Date: Sat, 21 Mar 2015 13:07:52 +0530
From: "Sumit Sahrawat, Maths & Computing, IIT (BHU)"
        <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>,
        Haskell-cafe Cafe <[email protected]>
Subject: Re: [Haskell-beginners] Multi-parameter type classes and
        ambiguous type variables...
Message-ID:
        <cajbew8p2dnfg-juccv8fw9qvgg6dbc_pm2rkn_ryhcg8khj...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

This might be better answered at the haskell-cafe. Sending to cafe.

On 21 March 2015 at 03:09, Stuart Hungerford <[email protected]>
wrote:

> Hi,
>
> As a learning exercise I'm modelling some algebraic structures as
> Haskell typeclasses. Some of these are multi-parameter type classes.
> Here's a very simplified version of the type class relationships:
>
> class MM a where
>   one :: a
>
> class AM a where
>   zero :: a
>
> class (AM a, MM a) => SR a
>
> class (AM a) => AG a where
>   inv :: a -> a
>
> class (SR a) => R a where
>   neg :: a -> a
>
> class (R r, AG g) => M r g where
>   sca :: r -> g -> g
>
> check :: (Eq g, M r g) => g -> Bool
> check x = sca one x == x
>
> The problem I have is that GHC is finding the "r" type variable in the
> "check" function ambiguous. Given my still limited Haskell knowledge
> I'm not surprised this is happening. What I would like to know is how
> experienced Haskellers handle this situation in practice: is there an
> idiomatic way of disambiguating "r" or is it a sign of poor type class
> design?
>
> Thanks,
>
> Stu
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>



-- 
Regards

Sumit Sahrawat
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20150321/89031a5c/attachment-0001.html>

------------------------------

Subject: Digest Footer

_______________________________________________
Beginners mailing list
[email protected]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


------------------------------

End of Beginners Digest, Vol 81, Issue 51
*****************************************

Reply via email to