Michael Saelee
Jan 25, 2019
Note, all types have capitalized names!
Note: :t
can be used to ask for the type of any expression
A function is a mapping (->) from one type (the domain) to another type (the range).
A function of multiple arguments can be implemented in one of two ways:
Note: (->) associates right-to-left, so, equivalent to:
I.e., foo is a function which takes an Int and returns a function which takes a Bool …
Function application (space) associates left-to-right, so:
is equivalent to:
Inspect the types of id
, const
, fst
, and snd
. Discuss.
In the type a -> b
, a
and b
are type variables. Since they are unqualified, they can be replaced with any type! A function whose type declaration contains a type variable is a polymorphic function.
Since an unqualified type variable says nothing about its actual type, you can’t do much with it (why?)
But this means the type of a polymorphic function is usually very helpful in determining what it does!
e.g., what do id
, const
, fst
, snd
do?
Inspect the types of ==
, +
, show
, read
Above, Num a
is a class constraint, implying that the actual type of type variable a
must belong to the type class Num
.
A type that contains a class constraint is called overloaded.
A class is a collection of types that support a set of overloaded functions called methods. Each type belonging to a given class is called an instance of that class.
:i
gives us information on both types and classes.
Though Haskell can infer types for us, it is good practice to always explitly declare types for all our functions.