[Haskell-beginners] let in GHCI

Henry Lockyer henry.lockyer at ntlworld.com
Sun Dec 11 19:55:44 CET 2011


Thanks a lot David  - that looks like it. 
Searched 'monomorphism' and found some material in the Haskell wiki to read. 
Wasn't galloping senility at least, 
in this case..
Cheers/ Henry

On 11 Dec 2011, at 18:36, David McBride wrote:

> I don't understand it perse, but I've run into it often enough to say
> that the problem is the monomorphism restriction.  When you say let
> mySort = sort, instead of having the type
> 
> mySort :: Ord a => [a] -> [a]
> 
> which is what you'd expect, instead there is some bizarre rule in the
> haskell spec that says it has to choose () for a in this case so if
> you look at the type after you define it in ghci, you'll see the type
> is actually
> 
> mySort :: [()] -> [()]
> 
> which is completely useless for anything.  If you go :set
> -XNoMonomorphismRestriction in ghci, you'll get the correct type and
> it will work the way you intend.  There is apparently some obscure
> case where this is desirable and that is the only reason it has not
> been removed by default.
> 
> On Sun, Dec 11, 2011 at 1:14 PM, Henry Lockyer
> <henry.lockyer at ntlworld.com> wrote:
>> Hello Haskellers
>> 
>> Why is it that in GHCI I can do, for example,
>> 
>>> replicate 6 5
>> [5,5,5,5,5,5]
>>> let my6 = replicate 6
>>> my6 5
>> [5,5,5,5,5,5]
>> 
>> but if I do
>> 
>>> sort "bav"
>> "abv"
>> 
>> this is ok, but
>> 
>>> let mySort = sort
>>> mySort "bav"
>> 
>> <interactive>:1:8:
>>    Couldn't match expected type `()' with actual type `Char'
>>    Expected type: [()]
>>      Actual type: [Char]
>>    In the first argument of `mySort', namely `"bav"'
>>    In the expression: mySort "bav"
>> 
>> and/or
>> 
>>> mySort [6,5,9]
>> 
>> <interactive>:1:13:
>>    No instance for (Num ())
>>      arising from the literal `9'
>>    Possible fix: add an instance declaration for (Num ())
>>    In the expression: 9
>>    In the first argument of `mySort', namely `[6, 5, 9]'
>>    In the expression: mySort [6, 5, 9]
>> 
>> This is eluding me at the moment..!  ;-)
>> 
>> / Henry
>> 
>> 
>> 
>> 
>> _______________________________________________
>> Beginners mailing list
>> Beginners at haskell.org
>> http://www.haskell.org/mailman/listinfo/beginners




More information about the Beginners mailing list