I am writing prefix names for some of Haskell's infix operators (specifically, +, -, *, /, :, <, <=, >, >=, ==, /=). I wrote the definitions to a file and loaded them into GHCi. At first I wrote each function with two parameters, like
geq x y = x >= y
which worked for all the operators listed. Then I tried doing the same thing by just assigning the functions themselves, like
geq = (>=)
which worked for the arithmetic operators and concatenation, but gave the following error for the comparison operators:
test.hs:1:1: error:
• Ambiguous type variable ‘a0’ arising from a use of ‘>=’
prevents the constraint ‘(Ord a0)’ from being solved.
Probable fix: use a type annotation to specify what ‘a0’ should be.
These potential instances exist:
instance Ord Ordering -- Defined in ‘GHC.Classes’
instance Ord Integer
-- Defined in ‘integer-gmp-1.0.0.1:GHC.Integer.Type’
instance Ord a => Ord (Maybe a) -- Defined in ‘GHC.Base’
...plus 22 others
...plus five instances involving out-of-scope types
(use -fprint-potential-instances to see them all)
• When instantiating ‘geq’, initially inferred to have
this overly-general type:
forall a. Ord a => a -> a -> Bool
NB: This instantiation can be caused by the monomorphism restriction.
Failed, modules loaded: none.
Adding a type annotation to the function
geq :: Ord a => a -> a -> Bool
(which I took from the GHCi by :t (>=)) solves the problem.
Interestingly, GHCi seems OK with the rejected definition if I do it in the interactive prompt, like
let geq = (>=)
But what about the comparison operators makes Haskell fail to infer the type of geq = (>=), which seems like it wouldn't even need any inference? What algorithm does Haskell use for type inference, and why is GHCi OK with that definition if that isn't valid Haskell without a type annotation?