[Haskell-beginners] Type variables

Ut Primum utprimum at gmail.com
Fri Jan 1 09:25:03 UTC 2021


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 <borgauf at gmail.com> 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
> Beginners at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20210101/1f8d10c7/attachment.html>


More information about the Beginners mailing list