Send Beginners mailing list submissions to
        [email protected]

To subscribe or unsubscribe via the World Wide Web, visit
        http://www.haskell.org/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. Re:  fmap versus bind (Patrick LeBoutillier)
   2. Re:  fmap versus bind (Felipe Almeida Lessa)
   3. Re:  fmap versus bind (Daniel Fischer)
   4. Re:  fmap versus bind (Sean Perry)
   5. Re:  fmap versus bind (Patrick LeBoutillier)
   6.  Return a Foldable instance (Federico Mastellone)
   7. Re:  Return a Foldable instance (Brandon S Allbery KF8NH)
   8. Re:  Return a Foldable instance (Antoine Latter)


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

Message: 1
Date: Tue, 3 May 2011 11:32:14 -0400
From: Patrick LeBoutillier <[email protected]>
Subject: Re: [Haskell-beginners] fmap versus bind
To: Sean Perry <[email protected]>
Cc: beginners <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

Hi all,

On Mon, May 2, 2011 at 9:56 PM, Sean Perry <[email protected]> wrote:
> i came across hlint today and ran it on my experiments. It pointed out places 
> where I was overly cautious with parens or used a $ and forget to remove it 
> later. But one suggestion it made I was curious about.
>
> Why does it recommend using fmap over >>=? Idiomatically does fmap make more 
> sense when the Monad is more like a collection?
>
> For instance I had "(applyOp op x y) >>= (flip (:) ds)". This seems more 
> clear than "fmap ((:) ds) (applyOp op x y)".


I'm trying to understand this example and I can't get the types to line up.
Can you provide the "real" types for ds and (applyOp op x y)?

This is what I'm working out:

     fmap ((:) ds) (applyOp op x y)
== fmap (ds :) (applyOp op x y)
== fmap (\dss -> ds : dss) (applyOp op x y)

      (applyOp op x y) >>= (flip (:) ds)
== (applyOp op x y) >>= (: ds)
== (applyOp op x y) >>= (\d -> d : ds)


To my untrained eyes is doesn't even look like the code is doing the
same thing...
What am I missing here (or more probably where is my mistake)?


Patrick

> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>



-- 
=====================
Patrick LeBoutillier
Rosem?re, Qu?bec, Canada



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

Message: 2
Date: Tue, 3 May 2011 12:50:57 -0300
From: Felipe Almeida Lessa <[email protected]>
Subject: Re: [Haskell-beginners] fmap versus bind
To: Patrick LeBoutillier <[email protected]>
Cc: beginners <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8

On Tue, May 3, 2011 at 12:32 PM, Patrick LeBoutillier
<[email protected]> wrote:
> I'm trying to understand this example and I can't get the types to line up.
> Can you provide the "real" types for ds and (applyOp op x y)?
>
> This is what I'm working out:
>
> ? ? fmap ((:) ds) (applyOp op x y)
> == fmap (ds :) (applyOp op x y)
> == fmap (\dss -> ds : dss) (applyOp op x y)
>
> ? ? ?(applyOp op x y) >>= (flip (:) ds)
> == (applyOp op x y) >>= (: ds)
> == (applyOp op x y) >>= (\d -> d : ds)
>
> To my untrained eyes is doesn't even look like the code is doing the
> same thing...
> What am I missing here (or more probably where is my mistake)?

I think that OP forgot a return, i.e., he meant 'applyOp op x y >>=
return . flip (:) ds'.

Regardless, there's a trick that you may use whenever you want to know
the types of something.  Just make a function where everything you
don't know is an argument, and ask GHCi to give you its type.
Supposing it's Haskell 98, GHCi will always give you the most general
type.  With this example:

Prelude> :t \ds applyOp op x y -> applyOp op x y >>= flip (:) ds
\ds applyOp op x y -> applyOp op x y >>= flip (:) ds
  :: [a] -> (t -> t1 -> t2 -> [a]) -> t -> t1 -> t2 -> [a]

So we see that

  ds :: [a]
  applyOp :: t -> u -> v -> [a]
  op :: t
  x :: u
  y :: v
  applyOp op x y >>= flip (:) ds :: [a]

works.  In other words, it's an expression in the list monad.  Whether
it is useful or not, I don't know =).  Adding the 'return':

Prelude> :t \ds applyOp op x y -> applyOp op x y >>= return . flip (:) ds
\ds applyOp op x y -> applyOp op x y >>= return . flip (:) ds
  :: (Monad m) =>
     [a] -> (t -> t1 -> t2 -> m a) -> t -> t1 -> t2 -> m [a]

So now we get:

  Monad m =>
  ds :: [a]
  applyOp :: t -> u -> v -> m a
  op :: t
  x :: u
  y :: v
  applyOp op x y >>= return . flip (:) ds :: m [a]

Seems much more useful!  So 'applyOp op x y :: m a' and the result of
everything is 'm [a]'.

Note that if there is a type mismatch (even with the general
arguments!), GHCi will tell you right away.  For example,

Prelude> :t \ds applyOp op x y -> applyOp op x y >>= fmap . flip (:) ds

<interactive>:1:48:
    Couldn't match expected type `a -> b' against inferred type `[a1]'
    In the second argument of `(.)', namely `flip (:) ds'
    In the second argument of `(>>=)', namely `fmap . flip (:) ds'
    In the expression: applyOp op x y >>= fmap . flip (:) ds

This means that no matter what ds, applyOp, op, x and y you choose,
'applyOp op x y >>= fmap . flip (:) ds' will always be ill-typed.

Cheers,

-- 
Felipe.



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

Message: 3
Date: Tue, 3 May 2011 18:00:40 +0200
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] fmap versus bind
To: [email protected]
Message-ID: <[email protected]>
Content-Type: Text/Plain;  charset="iso-8859-1"

On Tuesday 03 May 2011 17:32:14, Patrick LeBoutillier wrote:
> Hi all,
> 
> On Mon, May 2, 2011 at 9:56 PM, Sean Perry <[email protected]> wrote:
> > i came across hlint today and ran it on my experiments. It pointed out
> > places where I was overly cautious with parens or used a $ and forget
> > to remove it later. But one suggestion it made I was curious about.
> > 
> > Why does it recommend using fmap over >>=? Idiomatically does fmap
> > make more sense when the Monad is more like a collection?
> > 
> > For instance I had "(applyOp op x y) >>= (flip (:) ds)". This seems
> > more clear than "fmap ((:) ds) (applyOp op x y)".
> 
> I'm trying to understand this example and I can't get the types to line

Right, they don't match.

As first argument of (>>=), we must have

applyOp op x y :: m a, where m is some monad.

flip (:) ds = (: ds) = \x -> x:ds :: a -> [a], where ds :: [a].

So m = [],

applyOp op x y :: [a]

applyOp op x y >>= flip (:) ds :: [a]

On the other hand, assuming the flip has just been forgotten while typing,

fmap :: (a -> b) -> [a] -> [b] (using the specific Functor of this 
example), the fmapped function has type (a -> [a]), so

fmap (flip (:) ds) (applyOp op x y) :: [[a]]

There's a concat missing here, or a return in the (>>=) piece to make the 
types fit.

If the omission of flip in the fmap is not accidental, there's more amiss.


> up. Can you provide the "real" types for ds and (applyOp op x y)?
> 
> This is what I'm working out:
> 
>      fmap ((:) ds) (applyOp op x y)
> == fmap (ds :) (applyOp op x y)
> == fmap (\dss -> ds : dss) (applyOp op x y)
> 
>       (applyOp op x y) >>= (flip (:) ds)
> == (applyOp op x y) >>= (: ds)
> == (applyOp op x y) >>= (\d -> d : ds)
> 
> 
> To my untrained eyes is doesn't even look like the code is doing the
> same thing...
> What am I missing here (or more probably where is my mistake)?
> 
> 
> Patrick



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

Message: 4
Date: Tue, 03 May 2011 13:09:11 PDT
From: Sean Perry <[email protected]>
Subject: Re: [Haskell-beginners] fmap versus bind
To: beginners <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset="utf-8"


On Tue May  3  8:50 , Felipe Almeida Lessa  sent:

>On Tue, May 3, 2011 at 12:32 PM, Patrick LeBoutillier
>[email protected]> wrote:
>> I'm trying to understand this example and I can't get the types to line up.
>> Can you provide the "real" types for ds and (applyOp op x y)?
>>
>> This is what I'm working out:
>>
>> ? ? fmap ((:) ds) (applyOp op x y)
>> == fmap (ds :) (applyOp op x y)
>> == fmap (\dss -> ds : dss) (applyOp op x y)
>>
>> ? ? ?(applyOp op x y) >>= (flip (:) ds)
>> == (applyOp op x y) >>= (: ds)
>> == (applyOp op x y) >>= (\d -> d : ds)
>>
>> To my untrained eyes is doesn't even look like the code is doing the
>> same thing...
>> What am I missing here (or more probably where is my mistake)?
>
>I think that OP forgot a return, i.e., he meant 'applyOp op x y >>=
>return . flip (:) ds'.
>

You are correct, I did forget the return. Sorry for the confusion.

This particular bit of code was pulled out of the middle of a RPN calculator. So
'ds' is a list of doubles. applyOp takes 2 numbers and emits a 'm double' which
we then want to extract and add to the top of the list. The 'm' is there because
I do my calculation in the Either monad to support unknown operators, invalid
numbers, etc. A left value in the bind call stops the execution.

To my still novice eyes 'foo >>= handleResult' is more readable than 'fmap
handleResult foo'. When I look at the fmap call it looks like handlResult is the
more important computation.




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

Message: 5
Date: Tue, 3 May 2011 16:30:48 -0400
From: Patrick LeBoutillier <[email protected]>
Subject: Re: [Haskell-beginners] fmap versus bind
To: Felipe Almeida Lessa <[email protected]>
Cc: beginners <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

Felipe,

>
> Regardless, there's a trick that you may use whenever you want to know
> the types of something. ?Just make a function where everything you
> don't know is an argument, and ask GHCi to give you its type.

That's a great trick.

Thanks everybody for your help.


Patrick

> Supposing it's Haskell 98, GHCi will always give you the most general
> type. ?With this example:
>
> Prelude> :t \ds applyOp op x y -> applyOp op x y >>= flip (:) ds
> \ds applyOp op x y -> applyOp op x y >>= flip (:) ds
> ?:: [a] -> (t -> t1 -> t2 -> [a]) -> t -> t1 -> t2 -> [a]
>
> So we see that
>
> ?ds :: [a]
> ?applyOp :: t -> u -> v -> [a]
> ?op :: t
> ?x :: u
> ?y :: v
> ?applyOp op x y >>= flip (:) ds :: [a]
>
> works. ?In other words, it's an expression in the list monad. ?Whether
> it is useful or not, I don't know =). ?Adding the 'return':
>
> Prelude> :t \ds applyOp op x y -> applyOp op x y >>= return . flip (:) ds
> \ds applyOp op x y -> applyOp op x y >>= return . flip (:) ds
> ?:: (Monad m) =>
> ? ? [a] -> (t -> t1 -> t2 -> m a) -> t -> t1 -> t2 -> m [a]
>
> So now we get:
>
> ?Monad m =>
> ?ds :: [a]
> ?applyOp :: t -> u -> v -> m a
> ?op :: t
> ?x :: u
> ?y :: v
> ?applyOp op x y >>= return . flip (:) ds :: m [a]
>
> Seems much more useful! ?So 'applyOp op x y :: m a' and the result of
> everything is 'm [a]'.
>
> Note that if there is a type mismatch (even with the general
> arguments!), GHCi will tell you right away. ?For example,
>
> Prelude> :t \ds applyOp op x y -> applyOp op x y >>= fmap . flip (:) ds
>
> <interactive>:1:48:
> ? ?Couldn't match expected type `a -> b' against inferred type `[a1]'
> ? ?In the second argument of `(.)', namely `flip (:) ds'
> ? ?In the second argument of `(>>=)', namely `fmap . flip (:) ds'
> ? ?In the expression: applyOp op x y >>= fmap . flip (:) ds
>
> This means that no matter what ds, applyOp, op, x and y you choose,
> 'applyOp op x y >>= fmap . flip (:) ds' will always be ill-typed.
>
> Cheers,
>
> --
> Felipe.
>



-- 
=====================
Patrick LeBoutillier
Rosem?re, Qu?bec, Canada



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

Message: 6
Date: Tue, 3 May 2011 22:31:39 -0300
From: Federico Mastellone <[email protected]>
Subject: [Haskell-beginners] Return a Foldable instance
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

Hi,

I want to make a function that returns a Foldable instance, briefly,
something like this:

import Data.Foldable

test :: Foldable f => f Int
test = [1,2,3,4]

But I get this error:

    Couldn't match expected type `f' against inferred type `[]'
        `f' is a rigid type variable bound by
            the type signature for `test' at Test.hs:3:17
    In the expression: [1, 2, 3, 4]
    In the definition of `test': test = [1, 2, 3, ....]

How can I make a function like the one above?

I'm creating different implementations of a multimap, using a Set and
a [], and instead of providing functions getValuesList and
getValuesSet that return a [] and a Set respectively I want to provide
a more generic one, getValues that returns a Foldable instance.

Thanks!!!

-- 
Federico Mastellone
Computer Science Engineer - ITBA
".. there are two ways of constructing a software design: One way is
to make it so simple that there are obviously no deficiencies, and the
other way is to make it so complicated that there are no obvious
deficiencies. The first method is far more difficult."

Tony Hoare, 1980 ACM Turing Award Lecture.



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

Message: 7
Date: Tue, 03 May 2011 22:43:21 -0400
From: Brandon S Allbery KF8NH <[email protected]>
Subject: Re: [Haskell-beginners] Return a Foldable instance
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 5/3/11 21:31 , Federico Mastellone wrote:
> I want to make a function that returns a Foldable instance, briefly,
> something like this:
> 
> import Data.Foldable
> 
> test :: Foldable f => f Int
> test = [1,2,3,4]
> 
> But I get this error:
> 
>     Couldn't match expected type `f' against inferred type `[]'
>         `f' is a rigid type variable bound by
>             the type signature for `test' at Test.hs:3:17
>     In the expression: [1, 2, 3, 4]
>     In the definition of `test': test = [1, 2, 3, ....]

Right; you're not returning a call-point-specified Foldable, you're
returning a list always.  I suspect you really want Traversable, not
Foldable, so you can map elements from list to arbitrary Traversable; the
reverse is then Data.Foldable.toList.  (All Traversables are Foldables.)

http://www.haskell.org/ghc/docs/latest/html/libraries/base-4.3.1.0/Data-Traversable.html

- -- 
brandon s. allbery     [linux,solaris,freebsd,perl]    [email protected]
system administrator  [openafs,heimdal,too many hats]                kf8nh
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (Darwin)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk3AvUkACgkQIn7hlCsL25UVCACcCVHdZL8u2JwD0WZ0P4V6+u3+
XcQAoLI/mqsekYCA2SmlyAfMNpn5Kjem
=I0YQ
-----END PGP SIGNATURE-----



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

Message: 8
Date: Tue, 3 May 2011 22:27:41 -0500
From: Antoine Latter <[email protected]>
Subject: Re: [Haskell-beginners] Return a Foldable instance
To: Federico Mastellone <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8

On Tue, May 3, 2011 at 8:31 PM, Federico Mastellone <[email protected]> wrote:
> Hi,
>
> I want to make a function that returns a Foldable instance, briefly,
> something like this:
>
> import Data.Foldable
>
> test :: Foldable f => f Int
> test = [1,2,3,4]
>

One point that helped me figure things out:

The signature:

> test :: Foldable f => f Int

does NOT mean "the value test satisfies the 'Foldable' contract'

It means "the value test is of ANY 'Foldable' type". As in, it is of
whatever type the caller picks, so long as it is 'Foldable'. And a
list is not 'whatever type the caller picks.'

Type classes are used in Haskell to write polymorphic functions, not
for implementation hiding:

> myNumericalCalculation :: Fractional n => n -> n -> Bool -> n

The caller can pick if they want to use limited precision Floats or
Doubles, or if they want to pay the price for infinite precision Ratio
types, or some type that I hadn't even thought of when I wrote the
functions.

Antoine

> But I get this error:
>
> ? ?Couldn't match expected type `f' against inferred type `[]'
> ? ? ? ?`f' is a rigid type variable bound by
> ? ? ? ? ? ?the type signature for `test' at Test.hs:3:17
> ? ?In the expression: [1, 2, 3, 4]
> ? ?In the definition of `test': test = [1, 2, 3, ....]
>
> How can I make a function like the one above?
>
> I'm creating different implementations of a multimap, using a Set and
> a [], and instead of providing functions getValuesList and
> getValuesSet that return a [] and a Set respectively I want to provide
> a more generic one, getValues that returns a Foldable instance.
>
> Thanks!!!
>
> --
> Federico Mastellone
> Computer Science Engineer - ITBA
> ".. there are two ways of constructing a software design: One way is
> to make it so simple that there are obviously no deficiencies, and the
> other way is to make it so complicated that there are no obvious
> deficiencies. The first method is far more difficult."
>
> Tony Hoare, 1980 ACM Turing Award Lecture.
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>



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

_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners


End of Beginners Digest, Vol 35, Issue 4
****************************************

Reply via email to