Disclosure: I'm a Scala Programmer learning Haskell.
I'd like to understand type variable behaviour a bit better.
Contrary to Scala it seems we can declare variable of generic type (i.e. type variable). That is generic are not limited to function. However I can't understand the following deprecencies and would like some explanation for this.
Why is it that the following code does work on function:
g :: a -> a -- a can take the concrete type Num a => a or something else like Bool
g x = x
g 10
--g 10 :: Num a => a
g True
--g True :: Bool
However the following strange thing happen with variable as opposed to function
e :: a
e = 1
-- No instance for (Num a) arising from the literal ‘1’
-- Possible fix:
-- add (Num a) to the context of
-- the type signature for:
-- e1 :: forall a. a
-- In the expression: 1
-- In an equation for ‘e1’: e1 = 1
Same thing of course if I type the following in the REPL
e :: a; e = 1
Q1 ) From that it would seem we have a difference of Behaviour of generic type (i.e. type variable) between variable and function ? Why is that ? or what is it i am not seeing or getting yet ?
Now the strange part
If in the Repl however I do
e :: a; e = undefined
and then
e = 1
then i get
:t e
-- e1 :: Num p => p
So using undefined in the REPL "at least", I get back the Behaviour of Generic type on function.
Q2) What actually is going on here ? This all thing doe not sound super coherent to me. Can anyone explain please ?