Type variables

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Type variables

Lawrence Bottorff-2
I put this into ghci

makeTriple :: a -> b -> c -> (a,b,c)
makeTriple x y z = (x,y,z)

then as expected

> :t makeTriple
: makeTriple :: a -> b -> c -> (a, b, c)

but then on a whim I try this

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

I retry

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

What is this telling me? I'm not experienced enough to decode this.

LB

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Type variables

Ut Primum
Hi,

the first time you asked for the type of the function MakeTriple, which as you expected takes 3 arguments of generic types a, b and c and returns something of type (a,b,c). 

The second time you asked for the type of MakeTriple applied to three specific arguments. This time the result is no more the type of a function, but is just the type of the result, which is exactly the same as the type of
(123,"Hi",234.0)

Maybe now your question is why that expression has the type you obtain.

First of all, what is at the left of the symbol
=>
are class constraints. This means that they tell you that now c is not "any type" (as it was in the type of MakeTriple), but is "any type in the class Fractional". So it could be for example of type Float or Double, which are two different types but both in the class Fractional. 
Similarly Num a means that a can be "any type in the class Num" (so it can be Int, Integer, Float etc.).
The same class constraint you obtain if you ask for the type of 12.

Then at the right of the => symbol, you have the type of your expression, where a and c are not completely generic types, because of the contraints expressed before.

happy new year,
Ut


Il ven 1 gen 2021, 06:15 Lawrence Bottorff <[hidden email]> ha scritto:
I put this into ghci

makeTriple :: a -> b -> c -> (a,b,c)
makeTriple x y z = (x,y,z)

then as expected

> :t makeTriple
: makeTriple :: a -> b -> c -> (a, b, c)

but then on a whim I try this

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

I retry

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

What is this telling me? I'm not experienced enough to decode this.

LB
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Type variables

Francesco Ariis
In reply to this post by Lawrence Bottorff-2
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)

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners