Skip to content
Tutorials Center
My learning

Topics

Lesson 5 of 57 Β· Types and values

Basic Types and Type Signatures

Learn Haskell's basic types Int, Integer, Double, Bool, Char and String, how to read type signatures like a -> b -> c, and how type inference fills gaps.

Types are the heart of Haskell. Every value has one, the compiler checks that they fit together, and that is how it catches mistakes before your program ever runs. This lesson introduces the basic types, shows how to read a type signature, and explains how type inference lets you leave many of them out.

The Haskell blocks are real and read-only. The runnable JavaScript stand-ins model a few behaviours, such as the difference between Int and Integer, but JavaScript has no static types, so they cannot show the type checker working.

The common types

Numbers

Int Fixed-size whole number (64-bit on most machines).
Integer Whole number of any size.
Double Floating-point number.

Pick Integer when you need correctness, Int when you need speed.

1 / 2
HASKELL
age       :: Int
age       = 36

bigNumber :: Integer
bigNumber = 2 ^ 100            -- 1267650600228229401496703205376

pi'       :: Double
pi'       = 3.14159

isReady   :: Bool
isReady   = True

initial   :: Char
initial   = 'H'                -- single quotes for Char

greeting  :: String
greeting  = "Hello"            -- double quotes for String

Notes on each:

  • Int is a machine integer. It is fast but wraps around when it overflows: with 64 bits, maxBound :: Int is 9223372036854775807, and adding one to it gives the most negative number. The exact size is platform-dependent, but 64 bits is by far the most common.
  • Integer grows as needed, so 2 ^ 100 is exact. It is somewhat slower.
  • Double is a 64-bit floating-point number. Float also exists and has less precision.
  • Bool has exactly two values, True and False, capitalized. Combine them with &&, || and not.
  • Char uses single quotes: 'a', '\n'.
  • String is a type synonym for [Char]: a list of characters. That is why list functions like length and ++ work on strings. Strings, Chars and Text goes deeper.

Numbers are not converted automatically. To turn an Int into a Double, use fromIntegral:

HASKELL
average :: Int -> Int -> Double
average a b = fromIntegral (a + b) / 2

Two division operators exist: / for fractional numbers (7 / 2 = 3.5) and div for whole numbers (7 div 2 = 3). Using / on Int values is a type error.

Reading type signatures

A signature has the form name :: type. Read :: aloud as "has type".

HASKELL
not      :: Bool -> Bool
length   :: [a] -> Int
add      :: Int -> Int -> Int

Arrows separate the argument types from the result, and the last type is the result. So add :: Int -> Int -> Int takes two Ints and returns an Int. Arrows associate to the right, so this means Int -> (Int -> Int); currying explains why that matters.

Other type forms:

Type Meaning Example value
[Int] List of Int [1, 2, 3]
(Int, String) Pair (tuple) (1, "one")
() Unit ()
Maybe Int Maybe an Int Just 3 or Nothing
Int -> Bool Function from Int to Bool even

A lowercase name in a type, like a in length :: [a] -> Int, is a type variable: it stands for any type. length works on a list of anything. Such functions are called polymorphic, and parametric polymorphism is covered later.

Type inference

You do not have to write signatures. GHC works out the types from how names are used:

HASKELL
double x = x * 2         -- inferred: Num a => a -> a
shout s = s ++ "!"       -- inferred: [Char] -> [Char]

Then why write them? Because a signature is documentation, it gives better error messages, and it lets the compiler tell you when your code does not match your intent. The convention is to write a signature for every top-level definition and let local names be inferred.

If you do get it wrong, the compiler complains with a type error:

HASKELL
total :: Int
total = 5 + "apples"

GHC rejects this with a message along the lines of No instance for (Num String) arising from the literal... or a Couldn't match type error. The wording varies between versions, but the message always points to the offending expression. Reading these errors is a skill that common mistakes covers.

You can ask GHCi what it inferred with :t:

HASKELL
ghci> :t 'x'
'x' :: Char
ghci> :t (&&)
(&&) :: Bool -> Bool -> Bool
ghci> :t 3
3 :: Num a => a

The last line shows that a plain number literal is polymorphic: it can be an Int, an Integer or a Double depending on use. That Num a => part is a constraint, the subject of the next lesson.

Stand-ins in JavaScript

JavaScript numbers are all 64-bit floats, and it has a separate BigInt. We can use BigInt to imitate Integer and BigInt.asIntN(64, ...) to imitate Int wrapping. This models the behaviour, not the type checking.

JS Int wraps, Integer grows

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The second model is a tiny hand-made "type checker" that stores the declared type with each value and refuses mismatches. Real GHC does this analysis on source code before running anything.

JS A hand-made type check

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Check your understanding

Quick check Β· 5 questions

of 5 answered You scored / 5 Β· perfect!

  1. 1. Which type holds whole numbers of any size?

    Integer is arbitrary-precision, while Int is a fixed-size machine integer that wraps on overflow.
  2. 2. What is the result type of add :: Int -> Int -> Int?

    The final type after the last arrow is the result; the earlier ones are the arguments.
  3. 3. What is String in Haskell?

    A String is just a list of Char, which is why list functions work on it.
  4. 4. In length :: [a] -> Int, what is a?

    Lowercase names in types are type variables. length works for a list of any element type.
  5. 5. What do you use to convert an Int to a Double?

    Haskell does no implicit numeric conversions; fromIntegral converts an integral value to any numeric type.

Summary

  • The basic types are Int, Integer, Double, Bool, Char and String (which is [Char]).
  • Int is fixed-size and wraps; Integer is unbounded.
  • A signature a -> b -> c takes a and b and returns c; lowercase names are type variables.
  • GHC infers types, but top-level definitions should still carry signatures.
  • Convert numbers explicitly with fromIntegral; use / for fractions and div for whole numbers.

Next: Type Classes: Eq, Ord, Show, Read, Num.