[Haskell-beginners] Question about List Type Constraint and Null

Patrick LeBoutillier patrick.leboutillier at gmail.com
Sat Jan 22 19:26:56 CET 2011


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.siram at 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 at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>



-- 
=====================
Patrick LeBoutillier
Rosemère, Québec, Canada



More information about the Beginners mailing list