[Haskell-beginners] Why is :t 42 ::Num a => a -> a ?

Sylvain Henry sylvain at haskus.fr
Sat Sep 24 14:05:37 UTC 2016


Hi,

 > :t 42
42 :: Num t => t

The parser has to make some choices to distinguish between negate and 
subtraction. In ambiguous cases, subtraction is chosen:

 > abs -42

<interactive>:2:1: error:
     • Non type-variable argument in the constraint: Num (a -> a)
       (Use FlexibleContexts to permit this)
     • When checking the inferred type
         it :: forall a. (Num (a -> a), Num a) => a -> a
 > abs (-42)
42
 > let abs=10
 > abs-42
-32
 > abs -42
-32
 > abs - 42
-32
 > abs (-42)

<interactive>:15:1: error:
     • Non type-variable argument in the constraint: Num (t -> t1)
       (Use FlexibleContexts to permit this)
     • When checking the inferred type
         it :: forall t t1. (Num (t -> t1), Num t) => t1


It leads to some confusion:

 > fmap (+ 42) [1,2]
[43,44]
 > fmap (- 42) [1,2]

<interactive>:12:1: error:
     • Non type-variable argument in the constraint: Num (a -> b)
       (Use FlexibleContexts to permit this)
     • When checking the inferred type
         it :: forall a b. (Num (a -> b), Num a) => [b]
 > fmap (flip (-) 42) [1,2]
[-41,-40]

Cheers
Sylvain


On 24/09/2016 15:54, Olumide wrote:
> Is 42 a function? And if it is why does 42 + 24 work, if both are 
> functions. I know there's something I'm getting wrong here.
>
> Also, is the expression abs -42 an error. I initially thought that abs 
> to the operator (-) first but there is no space between - and 42. Or 
> is there some associativity issue going on here.
>
> - Olumide
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners



More information about the Beginners mailing list