What monad am I in?

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

What monad am I in?

Henry Laxen
Dear Group,

When I fire up ghci and define:

increment x = return (x+1)

I can say:
Main> increment 1

and ghci dutifully replies 2. Also as expected, the type signature of
increment is:  (Num a, Monad m) => a -> m a

However, if I say:

Main> let a = increment 1

I get:

<interactive>:1:8:
    Ambiguous type variable `m' in the constraint:
      `Monad m' arising from a use of `increment' at <interactive>:1:8-18
    Probable fix: add a type signature that fixes these type variable(s)


Have I, like Monsier Jourdain, been running in the IO monad all my
life, and didn't even know it?

Thanks,
Henry Laxen

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: What monad am I in?

Bugzilla from jonathanccast@fastmail.fm
On Tue, 2008-09-02 at 20:25 +0000, Henry Laxen wrote:

> Dear Group,
>
> When I fire up ghci and define:
>
> increment x = return (x+1)
>
> I can say:
> Main> increment 1
>
> and ghci dutifully replies 2. Also as expected, the type signature of
> increment is:  (Num a, Monad m) => a -> m a
>
> However, if I say:
>
> Main> let a = increment 1
>
> I get:
>
> <interactive>:1:8:
>     Ambiguous type variable `m' in the constraint:
>       `Monad m' arising from a use of `increment' at <interactive>:1:8-18
>     Probable fix: add a type signature that fixes these type variable(s)
>
>
> Have I, like Monsier Jourdain, been running in the IO monad all my
> life, and didn't even know it?

Yes.  This is a peculiarity of GHCi (and ghc -e) --- IO actions at
top-level are executed by default, while non-IO values are simply
printed out.

jcc


_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: What monad am I in?

Marc Weber
In reply to this post by Henry Laxen
> Have I, like Monsier Jourdain, been running in the IO monad all my
> life, and didn't even know it?
Sure, just try
readFile "doesnotexist" within ghci :-)
That's an IO action.
on the other side
ghci > (3+7)
10
is no IO action. So I think ghci has two default behaviours differing.
Either its a monad, than use IO, else evaluate the result. In both cases
show it. The ghc manual sould tell you all about this (too lazy to look
it up)


But the ghci error message is another one:
Try this:
 :set -XNoMonomorphismRestriction
 let increment x = return (x+1)
 let a = increment 1

the line let a = requires to find out about the type of m (Maybe any
Monad such as Maybe, list etc) without XNoMonomorphismRestriction.
With XNoMonomorphismRestriction you can tell ghc that you don't care yet
about this and it should try to resolve m later. That's why ghci shows
this:

ghci> :t a
a :: (Num t, Monad m) => m t

The second way to get rid of the ghci error message is telling ghci
which monad you want:
increment 1 -- this works because it's run within IO
print $ increment 1 -- won't, because ghc does'n know about the m type
print $ fromJust $ increment 1 -- works again, because you tell ghc that m is Maybe here
print $ (increment 1 :: [Int]) -- works as well, using list monad
...

Sincerly
Marc Weber
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: What monad am I in?

Ryan Ingram
In reply to this post by Henry Laxen
ghci has some crazy defaulting rules for expressions at the top level.

In particular, it tries to unify those expressions with a few
different types, including IO.

On the other hand, the let-expression is typed like regular Haskell
and you run into the monomorphism restriction.

  -- ryan

On Tue, Sep 2, 2008 at 1:25 PM, Henry Laxen <[hidden email]> wrote:

> Dear Group,
>
> When I fire up ghci and define:
>
> increment x = return (x+1)
>
> I can say:
> Main> increment 1
>
> and ghci dutifully replies 2. Also as expected, the type signature of
> increment is:  (Num a, Monad m) => a -> m a
>
> However, if I say:
>
> Main> let a = increment 1
>
> I get:
>
> <interactive>:1:8:
>    Ambiguous type variable `m' in the constraint:
>      `Monad m' arising from a use of `increment' at <interactive>:1:8-18
>    Probable fix: add a type signature that fixes these type variable(s)
>
>
> Have I, like Monsier Jourdain, been running in the IO monad all my
> life, and didn't even know it?
>
> Thanks,
> Henry Laxen
>
> _______________________________________________
> Haskell-Cafe mailing list
> [hidden email]
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: What monad am I in?

pweaver


On Tue, Sep 2, 2008 at 5:33 PM, Ryan Ingram <[hidden email]> wrote:
ghci has some crazy defaulting rules for expressions at the top level.

In particular, it tries to unify those expressions with a few
different types, including IO.

On the other hand, the let-expression is typed like regular Haskell
and you run into the monomorphism restriction.

Right.  Just to make it clear for the original poster, this monomorphism restriction is not about GHCi specifically, just GHC in general.  With the -fno-monomorphism-restriction, you will not get this error.



 -- ryan

On Tue, Sep 2, 2008 at 1:25 PM, Henry Laxen <[hidden email]> wrote:
> Dear Group,
>
> When I fire up ghci and define:
>
> increment x = return (x+1)
>
> I can say:
> Main> increment 1
>
> and ghci dutifully replies 2. Also as expected, the type signature of
> increment is:  (Num a, Monad m) => a -> m a
>
> However, if I say:
>
> Main> let a = increment 1
>
> I get:
>
> <interactive>:1:8:
>    Ambiguous type variable `m' in the constraint:
>      `Monad m' arising from a use of `increment' at <interactive>:1:8-18
>    Probable fix: add a type signature that fixes these type variable(s)
>
>
> Have I, like Monsier Jourdain, been running in the IO monad all my
> life, and didn't even know it?
>
> Thanks,
> Henry Laxen
>
> _______________________________________________
> Haskell-Cafe mailing list
> [hidden email]
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe


_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: What monad am I in?

Yitzchak Gale
In reply to this post by Marc Weber
Henry Laxen wrote:
>> Have I, like Monsier Jourdain, been running in the IO monad all my
>> life, and didn't even know it?

Marc Weber wrote:
> Sure...
> But the ghci error message is another one:
> Try this:
> :set -XNoMonomorphismRestriction

And I highly recommend putting that line in your .ghci file.

There is controversy about whether MR is helpful in general.
It is on by default, so I just leave it that way, and it seems
to be fine.

But at the GHCi prompt, MR is definitely a nuisance.
Get rid of it there by using :set in your .ghci file.

Regards,
Yitz
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe