[Haskell-beginners] Type variables

Francesco Ariis fa-ml at ariis.it
Fri Jan 1 10:20:02 UTC 2021


Il 31 dicembre 2020 alle 23:04 Lawrence Bottorff ha scritto:
> > :t (123,"Hi",234.0)
> (123,"Hi",234.0) :: (Fractional c, Num a) => (a, [Char], c)

When you do not write a signature yourself GHC/GHCi tries to infer it.
Alas — for certain types — GHC tries to default it

    -- prova.hs
    a = (123, "Hi", 234.0)
    λ> :load prova.hs
    [1 of 1] Compiling Main             ( prova.hs, interpreted )
    Ok, one module loaded.
    λ> :t a
    a :: (Integer, [Char], Double)

while GHCi goes for a more general type

    λ> b = (123, "Hi", 234.0)
    λ> :t b
    (123,"Hi",234.0) :: (Fractional c, Num a) => (a, [Char], c)

A couple of considerations:
- those are typeclasses — tl;dr they are interfaces providing a specific type
  of polymorphism. Ignore them for now if your text hasn’t explained them
  yet.
- When GHCi gives a type signature with typeclasses but you would rather see
  concrete types, use `+d`

    λ> :t +d b
    b :: (Integer, [Char], Double)



More information about the Beginners mailing list