Haskell ¿por qué "Num x" requiere "Mostrar x"?
Recientemente eché un vistazo a Haskell, usandoLYAH.
Estaba jugando con clases de tipos y escribí esta función de prueba rápida:
foo :: (Num x) => x -> String
foo x = show x ++ "!"
Pero eso produce este error:
test.hs:2:9:
Could not deduce (Show x) arising from a use of `show'
from the context (Num x)
bound by the type signature for foo :: Num x => x -> String
at test.hs:1:8-29
Possible fix:
add (Show x) to the context of
the type signature for foo :: Num x => x -> String
Pero según LYAH:
Para unirte a Num, un tipo ya debe ser amigo de Show y Eq.
Así que si todo enNum
es un subconjunto deShow
yEq
, ¿por qué necesito cambiar la firma de tipo afoo :: (Num x, Show x) => x -> String
para que esto funcione? ¿No debería ser posible inferir que unaNum
también es capaz de mostrar?