1

I was playing around in Haskell and I wrote the following code

d::Integer->Integer
d 4=7
d=(*2)

Try it online!

In my mind this should return 7 when the input is 4 and twice the input otherwise. However a bit to my surprise this doesn't even compile, it gives the error in GHC:

Equations for ‘d’ have different numbers of arguments

Now in some ways the compiler is correct, the two definitions of d do have different numbers of arguments, however they do both take one argument (The first requires it be both Eq and Num, while the second only requires Num) and produce a member of the Num type class.

 ⚙ > ~/projects > ghci
GHCi, version 8.2.1: http://www.haskell.org/ghc/  :? for help
Prelude> f 4=7
Prelude> :t f
f :: (Eq a, Num a, Num p) => a -> p
Prelude> g=(*2)
Prelude> :t g
g :: Num a => a -> a
Prelude>

In principle I see no reason why the compiler shouldn't allow this.

Why does the compiler give an error here? What is the benefit/design philosophy of doing so?


This is not a debugging question, I know the issue and I know how to fix it, here is a working version of the code as proof.

d::Integer->Integer
d 4=7
d x=x*2

Try it online!

Wheat Wizard
  • 3,982
  • 14
  • 34

0 Answers0