Send Beginners mailing list submissions to
        beginners@haskell.org

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
        beginners-requ...@haskell.org

You can reach the person managing the list at
        beginners-ow...@haskell.org

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


Today's Topics:

   1. Re:  Question about List Type Constraint and Null (aditya siram)
   2. Re:  Question about List Type Constraint and Null
      (Patrick LeBoutillier)
   3. Re:  Question about List Type Constraint and Null (J?rgen Doser)
   4. Re:  Question about List Type Constraint and Null (J?rgen Doser)
   5. Re:  Question about List Type Constraint and Null (aditya siram)
   6. Re:  Question about List Type Constraint and Null (Daniel Fischer)
   7. Re:  Question about List Type Constraint and Null (Daniel Fischer)
   8. Re:  Question about List Type Constraint and Null (aditya siram)


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

Message: 1
Date: Sat, 22 Jan 2011 11:06:19 -0600
From: aditya siram <aditya.si...@gmail.com>
Subject: Re: [Haskell-beginners] Question about List Type Constraint
        and Null
To: Magnus Therning <mag...@therning.org>
Cc: beginners@haskell.org
Message-ID:
        <aanlktikepqwmvn_a84qyxagv6-qu+f3pgmazebm-s...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1

Ok I've figured out why I can't compile it. But now I'm more confused
than ever. The problem was that I have a -XNoMonomorphismRestriction
flag on Ghci. Removing this allowed me to compile.

But why?

-deech

On Sat, Jan 22, 2011 at 2:07 AM, Magnus Therning <mag...@therning.org> wrote:
> On 22/01/11 06:03, aditya siram wrote:
>> Hi all,
>> The following function gives me an "Ambiguous type variable `a' in the
>> constraint: `Read a' arising from a use of `res'" error:
>> ? test :: Read a => String -> Maybe [(a,String)]
>> ? test s = if null res then
>> ? ? ? ? ? ? ?Nothing
>> ? ? ? ? ? ?else
>> ? ? ? ? ? ? ?Just $ fst $ head res
>> ? ? ? ? ? ?where
>> ? ? ? ? ? ? ?res = reads s
>
> This code doesn't give me that error, in fact it gives me no error at all.
>
> /M
>
> --
> Magnus Therning ? ? ? ? ? ? ? ? ? ? ?OpenPGP: 0xAB4DFBA4
> email: mag...@therning.org ? jabber: mag...@therning.org
> twitter: magthe ? ? ? ? ? ? ? http://therning.org/magnus
>
>
> _______________________________________________
> Beginners mailing list
> Beginners@haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
>



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

Message: 2
Date: Sat, 22 Jan 2011 13:26:56 -0500
From: Patrick LeBoutillier <patrick.leboutill...@gmail.com>
Subject: Re: [Haskell-beginners] Question about List Type Constraint
        and Null
To: aditya siram <aditya.si...@gmail.com>
Cc: beginners <beginners@haskell.org>
Message-ID:
        <AANLkTim+gK2z9g4VaRB38D4hiU1pb5dXAS=omdxke...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1

deech,

Shouldn't the type of test be "test :: Read a => String -> Maybe a" ?

Also, specifying the expected return type will make the error go away in ghci:

  *Main> test "42"

  <interactive>:1:0:
      Ambiguous type variable `a' in the constraint:
        `Read a' arising from a use of `test' at <interactive>:1:0-8
      Probable fix: add a type signature that fixes these type variable(s)
  *Main> test "42" :: Maybe Int
  Just 42

Or else ghci has no context to determine what you are trying to
"read". Is this how you were trying out your code?


Patrick


On Sat, Jan 22, 2011 at 1:03 AM, aditya siram <aditya.si...@gmail.com> wrote:
> Hi all,
> The following function gives me an "Ambiguous type variable `a' in the
> constraint: `Read a' arising from a use of `res'" error:
> ?test :: Read a => String -> Maybe [(a,String)]
> ?test s = if null res then
> ? ? ? ? ? ? Nothing
> ? ? ? ? ? else
> ? ? ? ? ? ? Just $ fst $ head res
> ? ? ? ? ? where
> ? ? ? ? ? ? res = reads s
>
> The reason as 'jmcarthur' so patiently explained on IRC is that 'res'
> is used twice and the type constraint 'a' is different for each use,
> hence the ambiguity. I get that.
>
> But I have a further question why should 'null ...' care about the
> type of its list argument? Isn't it polymorphic? ?So it shouldn't make
> a difference that the 'a' inside res is ambiguous because we know for
> sure that it always returns a list of some kind.
>
> Thanks,
> -deech
>
> _______________________________________________
> Beginners mailing list
> Beginners@haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>



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



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

Message: 3
Date: Sat, 22 Jan 2011 19:45:40 +0100
From: J?rgen Doser <jurgen.do...@gmail.com>
Subject: Re: [Haskell-beginners] Question about List Type Constraint
        and Null
To: beginners@haskell.org
Message-ID: <1295721940.4103.10.ca...@imedia.irun.org>
Content-Type: text/plain; charset="UTF-8"

El s?b, 22-01-2011 a las 11:06 -0600, aditya siram escribi?:
> Ok I've figured out why I can't compile it. But now I'm more confused
> than ever. The problem was that I have a -XNoMonomorphismRestriction
> flag on Ghci. Removing this allowed me to compile.

This is indeed a bit tricky, it seems. res has a type-class polymorphic
type Read a => ... (the ... is complicated by the fact that you have
given a strange signature to test, see below)

With the monomorphism restriction on, ghc takes the type-signature for
test, inferes a type for res in the line Just $ fst $ head res, and uses
this type for the null res test. Interestingly, it doesn't infer a
monomorphic type for res, but insists that the instantiation for the
type-class parameter is the same in both occurences.

With no monomorphism restriction, ghc doesn't do this inference, because
the type for res in null res may be different to the type of res in Just
$ fst $ head res. (i.e., the instantion for the type-class parameter may
be different)

Finally, are you sure that the given type signature is what you want?
Don't you want

test ::  Read a => String -> Maybe a


        J?rgen





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

Message: 4
Date: Sat, 22 Jan 2011 19:50:55 +0100
From: J?rgen Doser <jurgen.do...@gmail.com>
Subject: Re: [Haskell-beginners] Question about List Type Constraint
        and Null
To: beginners@haskell.org
Message-ID: <1295722255.4103.13.ca...@imedia.irun.org>
Content-Type: text/plain; charset="UTF-8"

El s?b, 22-01-2011 a las 00:03 -0600, aditya siram escribi?:
>
> But I have a further question why should 'null ...' care about the
> type of its list argument? Isn't it polymorphic?  So it shouldn't make
> a difference that the 'a' inside res is ambiguous because we know for
> sure that it always returns a list of some kind.

The point is that for different types for res, the list has a different
number of elements. So without saying what the type of res is, you
cannot know if the list is empty or not.

        J?rgen




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

Message: 5
Date: Sat, 22 Jan 2011 13:01:19 -0600
From: aditya siram <aditya.si...@gmail.com>
Subject: Re: [Haskell-beginners] Question about List Type Constraint
        and Null
To: J?rgen Doser <jurgen.do...@gmail.com>
Cc: beginners@haskell.org
Message-ID:
        <aanlktin0xnh49yo3uotfwpyq7uotac5mwcnsg1q6a...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1

> Finally, are you sure that the given type signature is what you want?
> Don't you want
>
> test :: ?Read a => String -> Maybe a

Yes that was a mistake, but the weirder thing is that fixing the type
sig does nothing to change the behavior described before: it still
fails to compile with -XNoMonomorphismRestriction and compiles fine if
it's removed.

It's going to take me a while understand absorb your explanation.

Thanks!
-deech


>
>
> ? ? ? ?J?rgen
>
>
>
> _______________________________________________
> Beginners mailing list
> Beginners@haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>



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

Message: 6
Date: Sat, 22 Jan 2011 20:22:47 +0100
From: Daniel Fischer <daniel.is.fisc...@googlemail.com>
Subject: Re: [Haskell-beginners] Question about List Type Constraint
        and Null
To: beginners@haskell.org, aditya siram <aditya.si...@gmail.com>
Message-ID: <201101222022.47503.daniel.is.fisc...@googlemail.com>
Content-Type: text/plain;  charset="utf-8"

On Saturday 22 January 2011 19:45:40, J?rgen Doser wrote:
> El s?b, 22-01-2011 a las 11:06 -0600, aditya siram escribi?:
> > Ok I've figured out why I can't compile it. But now I'm more confused
> > than ever. The problem was that I have a -XNoMonomorphismRestriction
> > flag on Ghci. Removing this allowed me to compile.
>
> This is indeed a bit tricky, it seems. res has a type-class polymorphic
> type Read a => ... (the ... is complicated by the fact that you have
> given a strange signature to test, see below)
>
> With the monomorphism restriction on, ghc takes the type-signature for
> test, inferes a type for res in the line Just $ fst $ head res, and uses
> this type for the null res test. Interestingly, it doesn't infer a
> monomorphic type for res, but insists that the instantiation for the
> type-class parameter is the same in both occurences.
>
> With no monomorphism restriction, ghc doesn't do this inference, because
> the type for res in null res may be different to the type of res in Just
> $ fst $ head res. (i.e., the instantion for the type-class parameter may
> be different)

You can however make ghc consider res a monomorphic value even with the MR 
turned off by
a) turning on MonoLocalBinds
b) turning on ScopedTypeVariables, bringing a into scope (via forall a) and 
giving res a type signature involving that a

or you can avoid all these problems by using the nicer code

test s = case reads s of
            [] -> Nothing
            ((x,_):_) -> Just x

>
> Finally, are you sure that the given type signature is what you want?
> Don't you want
>
> test ::  Read a => String -> Maybe a
>
>
>       J?rgen



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

Message: 7
Date: Sat, 22 Jan 2011 20:49:53 +0100
From: Daniel Fischer <daniel.is.fisc...@googlemail.com>
Subject: Re: [Haskell-beginners] Question about List Type Constraint
        and Null
To: beginners@haskell.org
Message-ID: <201101222049.53958.daniel.is.fisc...@googlemail.com>
Content-Type: text/plain;  charset="iso-8859-1"

On Saturday 22 January 2011 20:01:19, aditya siram wrote:
> Yes that was a mistake, but the weirder thing is that fixing the type
> sig does nothing to change the behavior described before: it still
> fails to compile with -XNoMonomorphismRestriction and compiles fine if
> it's removed.
>
> It's going to take me a while understand absorb your explanation.

Perhaps the following will help understanding it:

module ReadStuff where

test :: String -> Maybe Int
test s = if null res then
           Nothing
         else
           Just $ fst $ head res
         where
           res = reads s

Prelude> :l ReadStuff
[1 of 1] Compiling ReadStuff        ( ReadStuff.hs, interpreted )

ReadStuff.hs:4:18:
    Ambiguous type variable `a' in the constraint:
      (Read a) arising from a use of `res'
    Probable fix: add a type signature that fixes these type variable(s)
    In the first argument of `null', namely `res'
    In the expression: null res
    In the expression:
      if null res then Nothing else Just $ fst $ head res
Failed, modules loaded: none.


The inferred type for res is

res :: Read a => [(a,String)]

with an implicit (forall a.).
The value of `null res' for a given String obviously depends on the type at 
which res is used.
To make test work, the type of res to use there has to be determined 
somehow.

The natural way is to force res in line 4 to have the same type as res in 
line 7, where the type is determined by the signature of test (you could 
also write `null (res :: [(Bool,String)])' in line 4, but that would lead 
to
*** Exception: Prelude.head: empty list
for some inputs).

Turning the monomorphism restriction off says explicitly 'let res be a 
polymorphic value', so you get the ambiguous type variable in line 4.

The monomorphism restriction says 'don't generalise the type of res 
[there's no type signature on res, so it's in a restricted binding group]'.
So you get `res :: Read b => [(b,String)]' for some so far unknown, but 
monomorphic b. The use of res in line 7 determines b (here Int, in general 
the same type that instantiates the `a' in test's signature).



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

Message: 8
Date: Sat, 22 Jan 2011 16:11:07 -0600
From: aditya siram <aditya.si...@gmail.com>
Subject: Re: [Haskell-beginners] Question about List Type Constraint
        and Null
To: Daniel Fischer <daniel.is.fisc...@googlemail.com>
Cc: beginners@haskell.org
Message-ID:
        <AANLkTi=KjmB8RVYmC86oVPB9mqTh7JKkc=vpc_qyf...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1

> The value of `null res' for a given String obviously depends on the type at
> which res is used.
Why does it matter what the type of res is as long as it is some kind
of list? Doesn't 'reads' always gives [(a1,String)]? So why does it
matter what type 'a1' is?

Thanks for the clear explanation of the monomorphism restriction.
-deech



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

_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://www.haskell.org/mailman/listinfo/beginners


End of Beginners Digest, Vol 31, Issue 22
*****************************************

Reply via email to