user634615
user634615

Reputation: 677

Type signature of functions in Haskell

I have defined three functions in haskell which multiply two numbers

k = \x y -> x * y

foo y = \x -> x * y

bar x = \x -> x * x

But I am getting different signatures for all the three functions.

λ> :t k
k :: Integer -> Integer -> Integer
λ> :t foo
foo :: Num a => a -> a -> a
λ> :t bar
bar :: Num a => t -> a -> a

Can somebody explain why it is so? I can see t in type signature of bar. is it different from normal usage of a, b, or a1, a2 etc

Upvotes: 3

Views: 101

Answers (1)

Random Dev
Random Dev

Reputation: 52290

First of: all the signatures basically come from (*) :: Num a => a -> a -> a and your usage of it. And yes k and foo should be the same but bar is indeed a bit different as you wrote it here.

  • foo should be obvious (I hope) (it's just that * is polymorphic and the type says just that)
  • for bar it's because the x on the left side is not used on the right side so you get the extra parameter t - it's the same as bar y = \x -> x * x
  • for k see the Monomorphism restriction - basically Haskell will pull a default-type here to work around some slight problems that could arise. It's a bit strange that you get it here because by default newer GHCi version should not show this behavior (see the link)

btw: don't wonder to much about the names of the types ;)

The lesson learned should probably be to just write down your signatures (at least on top-level functions) ^^ - it would have solved all your problems

Upvotes: 6

Related Questions