About monad

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

About monad

Trung Quang Nguyen
Hi all,

I saw this


   1. instance Monad Maybe where
   2.     return x = Just x
   3.     Nothing >>= f = Nothing
   4.     Just x >>= f  = f x
   5.     fail _ = Nothing


I am wondering about the implementation of function (>>=). Why don't
it be *Just
x >>= f = Just (f x)*?

Any body knows about this?

--Trung
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20121220/263cf930/attachment.htm>

Reply | Threaded
Open this post in threaded view
|

About monad

Edward Z. Yang
Excerpts from Trung Quang Nguyen's message of Thu Dec 20 22:07:02 +0800 2012:

> Hi all,
>
> I saw this
>
>
>    1. instance Monad Maybe where
>    2.     return x = Just x
>    3.     Nothing >>= f = Nothing
>    4.     Just x >>= f  = f x
>    5.     fail _ = Nothing
>
>
> I am wondering about the implementation of function (>>=). Why don't
> it be *Just
> x >>= f = Just (f x)*?

Try it; it won't typecheck.

Cheers,
Edward


Reply | Threaded
Open this post in threaded view
|

About monad

Thomas Davie
In reply to this post by Trung Quang Nguyen

On 20 Dec 2012, at 14:07, Trung Quang Nguyen <trungnq97 at gmail.com> wrote:

> Hi all,
>
> I saw this
>
> instance Monad Maybe where  
>     return x = Just x  
>     Nothing >>= f = Nothing  
>     Just x >>= f  = f x  
>     fail _ = Nothing  
>
> I am wondering about the implementation of function (>>=). Why don't it be Just x >>= f = Just (f x)?
>
> Any body knows about this?

The reason is in the type of bind:

(>>=) :: Monad m => m a -> (a -> m b) -> m b

The function f takes a non-in-a-monad value, and gives you an in-a-monad value.

Bob

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20121220/de0b1ecd/attachment.htm>

Reply | Threaded
Open this post in threaded view
|

About monad

Trung Quang Nguyen
Oh yes, I understand now.

Just x >>= f  = f x
the output of f is actually (Monad value) like in this example

(Just 3) >>= (\x -> Just $ x^2)

At the first sight, I thought about (Monad (f x)), but it's wrong because
it will be (Monad (Monad value)) when f return.

Thanks a lot!

--Trung




2012/12/20 Tom Davie <tom.davie at gmail.com>

>
> On 20 Dec 2012, at 14:07, Trung Quang Nguyen <trungnq97 at gmail.com> wrote:
>
> Hi all,
>
> I saw this
>
>
>    1. instance Monad Maybe where
>    2.     return x = Just x
>    3.     Nothing >>= f = Nothing
>    4.     Just x >>= f  = f x
>    5.     fail _ = Nothing
>
>
> I am wondering about the implementation of function (>>=). Why don't it be
> *Just x >>= f = Just (f x)*?
>
> Any body knows about this?
>
>
> The reason is in the type of bind:
>
> (>>=) :: Monad m => m a -> (a -> m b) -> m b
>
> The function f takes a non-in-a-monad value, and gives you an in-a-monad
> value.
>
> Bob
>
>


--
*Trung Nguyen*
Mobile: +45 50 11 10 63
LinkedIn: http://www.linkedin.com/pub/trung-nguyen/36/a44/187
View my blog at http://www.onextrabit.com/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20121220/fbbf70e2/attachment-0001.htm>

Reply | Threaded
Open this post in threaded view
|

About monad

Trung Quang Nguyen
In reply to this post by Trung Quang Nguyen
Hi David,

Yours is more precise. f should output (ValueConstructor value) instead of
(Monad value) :)

--Trung




2012/12/20 David McBride <toad3k at gmail.com>

> The way I like to reason about these things
> Prelude> :t (>>=)
>
> (>>=) :: Monad m => m a -> (a -> m b) -> m b
>
> Since the Monad m in this definition is Maybe, I'll just substitute it in
> like so:
> Maybe a -> (a -> Maybe b) -> Maybe b
>
> Then you notice that f is not (a -> b), it is (a -> Maybe b).
>
> On Thu, Dec 20, 2012 at 9:07 AM, Trung Quang Nguyen <trungnq97 at gmail.com>wrote:
>
>> Hi all,
>>
>> I saw this
>>
>>
>>    1. instance Monad Maybe where
>>    2.     return x = Just x
>>    3.     Nothing >>= f = Nothing
>>    4.     Just x >>= f  = f x
>>    5.     fail _ = Nothing
>>
>>
>> I am wondering about the implementation of function (>>=). Why don't it
>> be *Just x >>= f = Just (f x)*?
>>
>> Any body knows about this?
>>
>> --Trung
>>
>>
>> _______________________________________________
>> Beginners mailing list
>> Beginners at haskell.org
>> http://www.haskell.org/mailman/listinfo/beginners
>>
>>
>


--
*Trung Nguyen*
Mobile: +45 50 11 10 63
LinkedIn: http://www.linkedin.com/pub/trung-nguyen/36/a44/187
View my blog at http://www.onextrabit.com/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20121220/7dedd86a/attachment.htm>

Reply | Threaded
Open this post in threaded view
|

About monad

Miguel Negrao
In reply to this post by Trung Quang Nguyen

A 20/12/2012, ?s 14:07, Trung Quang Nguyen escreveu:

> Hi all,
>
> I saw this
>
> ? instance Monad Maybe where  
> ?     return x = Just x  
> ?     Nothing >>= f = Nothing  
> ?     Just x >>= f  = f x  
> ?     fail _ = Nothing  
>
> I am wondering about the implementation of function (>>=). Why don't it be Just x >>= f = Just (f x)?
>
> Any body knows about this?

That would be the implementation of fmap for Maybe:

instance  Functor Maybe  where
    fmap _ Nothing       = Nothing
    fmap f (Just a)      = Just (f a)

so, different behavior.

best,
Miguel

Reply | Threaded
Open this post in threaded view
|

About monad

Trung Quang Nguyen
*fmap*<http://hackage.haskell.org/packages/archive/base/latest/doc/html/Prelude.html#v:fmap>
::
Functor f => (a -> b) -> f a -> f
b<http://hackage.haskell.org/packages/archive/base/latest/doc/html/Prelude.html#v:fmap>
fmap f (Just a)      = Just (f a)

We wrap Just around (f a) because f return a value with type b instead
(Just b).

But in
(*>>=*)<http://hackage.haskell.org/packages/archive/base/latest/doc/html/Prelude.html#v:-62--62--61->
::
Monad m => m a -> (a -> m b) -> m
b<http://hackage.haskell.org/packages/archive/base/latest/doc/html/Prelude.html#v:-62--62--61->
Just x >>= f  = f x

We don't need to wrap Just around (f a) because f return (Just b).

--Trung



2012/12/20 Miguel Negrao <miguel.negrao-lists at friendlyvirus.org>

>
> A 20/12/2012, ?s 14:07, Trung Quang Nguyen escreveu:
>
> > Hi all,
> >
> > I saw this
> >
> >       ? instance Monad Maybe where
> >       ?     return x = Just x
> >       ?     Nothing >>= f = Nothing
> >       ?     Just x >>= f  = f x
> >       ?     fail _ = Nothing
> >
> > I am wondering about the implementation of function (>>=). Why don't it
> be Just x >>= f = Just (f x)?
> >
> > Any body knows about this?
>
> That would be the implementation of fmap for Maybe:
>
> instance  Functor Maybe  where
>     fmap _ Nothing       = Nothing
>     fmap f (Just a)      = Just (f a)
>
> so, different behavior.
>
> best,
> Miguel
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>



--
*Trung Nguyen*
Mobile: +45 50 11 10 63
LinkedIn: http://www.linkedin.com/pub/trung-nguyen/36/a44/187
View my blog at http://www.onextrabit.com/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20121220/e857be6c/attachment.htm>