¿Qué está pasando con los tipos en esta sesión de ghci?
Estoy aprendiendo Haskell, y estaba jugando en ghci cuando me encontré con algo muy desconcertante.
Primero, crea una función simple de agregar:
Prelude> let add x y = x + y
Tenga en cuenta que funciona con ints y flotadores:
Prelude> add 3 4
7
Prelude> add 2.5 1.3
3.8
Ahora crea una función de aplicación. Es idéntico a$
(pero no infijo). Funciona como un no-op en agregar:
Prelude> let apply f x = f x
Prelude> apply add 3 4
7
Prelude> apply add 2.5 1.3
3.8
Ok ahora hazadd'
que es lo mismo queadd'
pero 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.
Aquí están los 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>
Por queadd'
tiene un tipo diferente queapply add
?
¿Es esta una rareza ghci, o es verdad en Haskell en general? (¿Y cómo puedo saber la diferencia?)