O que está acontecendo com os tipos nesta sessão ghci?
Estou aprendendo Haskell e estava brincando com ghci quando me deparei com algo muito intrigante.
Primeiro, crie uma função de adição simples:
Prelude> let add x y = x + y
Note que funciona com ints e floats:
Prelude> add 3 4
7
Prelude> add 2.5 1.3
3.8
Agora crie uma função de aplicação. É idêntico a$
(mas não infixo). Funciona como um no-op em add:
Prelude> let apply f x = f x
Prelude> apply add 3 4
7
Prelude> apply add 2.5 1.3
3.8
Ok, agora façaadd'
que é o mesmo queadd'
mas usandoapply
:
Prelude> let add' = apply add
Prelude> add' 3 4
7
Prelude> add' 2.5 1.3
<interactive>:1:9:
No instance for (Fractional Integer)
arising from the literal `1.3' at <interactive>:1:9-11
Possible fix: add an instance declaration for (Fractional Integer)
In the second argument of `add'', namely `1.3'
In the expression: add' 2.5 1.3
In the definition of `it': it = add' 2.5 1.3
Wat
Aqui estão os tipos:
Prelude> :t add
add :: (Num a) => a -> a -> a
Prelude> :t apply add
apply add :: (Num t) => t -> t -> t
Prelude> :t add'
add' :: Integer -> Integer -> Integer
Prelude>
Porqueadd'
tem um tipo diferente do queapply add
?
Isso é uma estranheza ghci, ou isso é verdade em Haskell em geral? (E como eu posso dizer a diferença?)