proving monad laws
  Home FAQ Contact Sign in
comp.lang.haskell only
 
Advanced search
POPULAR GROUPS

more...

comp.lang.haskell Profile…
 Up
proving monad laws         


Author: andrea
Date: Mar 31, 2008 16:01

Given the three monad laws

(return x) >>= f == f x
m >>= return == m
(m >>= f) >>= g == m >>= (\x -> f x >>= g)

If I create a new monad, should I try to see if they are actually true??
how could you prove that for any f or m?

I was trying with the simple Maybe monad with some numerical examples,
but with no luck.

*Main Monad> let t = Just 10
*Main Monad> (return t) >>= (+1)

:1:15:
No instance for (Num (Maybe Integer))
arising from a use of `+' at :1:15-18
Possible fix: add an instance declaration for (Num (Maybe Integer))
In the second argument of `(>>=)', namely `(+ 1)'
In the expression: (return t) >>= (+ 1)
In the definition of `it': it = (return t) >>= (+ 1)

Shouldn't be the function be of type (Integer -> Integer)? Am I missing
something (as usual)?
7 Comments
Re: proving monad laws         


Author: Gareth McCaughan
Date: Mar 31, 2008 16:50

"andrea" wrote:
> Given the three monad laws
>
>
> (return x) >>= f == f x
> m >>= return == m
> (m >>= f) >>= g == m >>= (\x -> f x >>= g)
>
> If I create a new monad, should I try to see if they are actually true??
> how could you prove that for any f or m?
>
> I was trying with the simple Maybe monad with some numerical examples,
> but with no luck.
>
> *Main Monad> let t = Just 10
> *Main Monad> (return t) >>= (+1)

So you're trying to apply

(return x) >>= f == f x

in the case where
Show full article (1.23Kb)
no comments
Re: proving monad laws         


Author: Vesa Karvonen
Date: Apr 1, 2008 01:23

andrea gmail.com> wrote:
> Given the three monad laws
> (return x) >>= f == f x
> m >>= return == m
> (m >>= f) >>= g == m >>= (\x -> f x >>= g)
> If I create a new monad, should I try to see if they are actually true??

Well, yes. To program without understanding is voodoo.
> how could you prove that for any f or m?

One way is to do a little calculation, using the definitions of the
combinators (return and >>=) and the rules of the language. You start
with the left hand side of a law and transform it step-by-step until you
reach the right hand side of the law.

As it happens, I've been recently tinkering with a monadic iterator
combinator library in Standard ML:

http://mlton.org/cgi-bin/viewsvn.cgi/*checkout*/mltonlib/trunk/com/ssh/extended-basis/unstable/public/control/iter.sig

An iterator, or enumerator, is a higher-order function that applies a
given function to elements drawn from a sequence of some kind:

type 'a t = ('a unit) -> unit

(Note: In Haskell one possible type for the iterator monad would be
Show full article (3.23Kb)
no comments
Re: proving monad laws         


Author: andrea
Date: Apr 1, 2008 01:23

On 1 Apr, 01:50, Gareth McCaughan pobox.com> wrote:
> "andrea" wrote:
>> Given the three monad laws
>
>> (return x) >>= f == f x
>> m >>= return == m
>> (m >>= f) >>= g == m >>= (\x -> f x >>= g)
>
>> If I create a new monad, should I try to see if they are actually true??
>> how could you prove that for any f or m?
>
>> I was trying with the simple Maybe monad with some numerical examples,
>> but with no luck.
>
>> *Main Monad> let t = Just 10
>> *Main Monad> (return t) >>= (+1)
>
> So you're trying to apply
>
>     (return x) >>= f  ==  f x ...
Show full article (1.58Kb)
no comments
Re: proving monad laws         


Author: Gareth McCaughan
Date: Apr 1, 2008 02:03

"andrea" wrote:

[me:]
>> If instead you take x == 10 then you should get the right types.
>> (In the Maybe monad, return == Just.)

[Andrea:]
> return x == Just x
> of course works, but it's not the monad law I think...

Why do you think that, and what do you think *is* the
monad law?

See, e.g.,
http://en.wikipedia.org/wiki/Monads_in_functional_programming
http://www.haskell.org/all_about_monads/html/maybemonad.html
http://www.randomhacks.net/articles/2007/03/12/monads-in-15-minutes
which are the top three Google results for "maybe monad"
and all mention that the Maybe monad's return is Just.
> I know that the types were wrong, but I just tried to apply the law,
> how can it be done then?

I'm afraid I don't understand the question.
Show full article (0.81Kb)
no comments
Re: proving monad laws         


Author: andrea
Date: Apr 1, 2008 04:45

On 1 Apr, 11:03, Gareth McCaughan pobox.com> wrote:
> "andrea" wrote:
>
> [me:]
>
>>> If instead you take x == 10 then you should get the right types.
>>> (In the Maybe monad, return == Just.)
>
> [Andrea:]
>
>> return x == Just x
>> of course works, but it's not the monad law I think...
>
> Why do you think that, and what do you think *is* the
> monad law?

Ok this
*Transform List> (return 10 >>= Just) == Just 10
True

Looks more like the "proof" of the law, doesn't it?
no comments
Re: proving monad laws         


Author: Vesa Karvonen
Date: Apr 1, 2008 05:29

andrea gmail.com> wrote:
> On 1 Apr, 11:03, Gareth McCaughan pobox.com> wrote:
>> "andrea" wrote:
>>
>> [me:]
>>
>>>> If instead you take x == 10 then you should get the right types.
>>>> (In the Maybe monad, return == Just.)
>>
>> [Andrea:]
>>
>>> return x == Just x
>>> of course works, but it's not the monad law I think...
>>
>> Why do you think that, and what do you think *is* the
>> monad law?
> Ok this
> *Transform List> (return 10 >>= Just) == Just 10
> True
Show full article (1.42Kb)
no comments
Re: proving monad laws         


Author: andrea
Date: Apr 5, 2008 12:26

On 1 Apr, 14:29, Vesa Karvonen wrote:
> andrea gmail.com> wrote:
>> On 1 Apr, 11:03, Gareth McCaughan pobox.com> wrote:
>>> "andrea" wrote:
>
>>> [me:]
>
>>>>> If instead you take x == 10 then you should get the right types.
>>>>> (In the Maybe monad, return == Just.)
>
>>> [Andrea:]
>
>>>> return x == Just x
>>>> of course works, but it's not the monad law I think...
>
>>> Why do you think that, and what do you think *is* the
>>> monad law?
>> Ok this
>> *Transform List> (return 10 >>= Just) == Just 10
>> True ...
Show full article (1.66Kb)
no comments

RELATED THREADS
SubjectArticles qty Group
Re: Gun Laws save 2,500 lives in Australia / No gun laws kills 15,000 lives in USA PER ANNUMrec.audio.opinion ·