Tuple

Tuples can be represented by the n-ary type constructor Tuple. The 1-element tuple Tuple a is not representable. As an example, the type of a pair of values is Tuple a b, where a and b are the types of the first and the second value, respectively. The type of each component of a tuple can be distinct.

We can extract the components of a tuple using pattern matching:

val sumIntPair  = \((m, n) : Tuple Int Int) -> m + n
val a = sumIntPair (3, 5)
//    = 8

The 0-element tuple is representable, and Unit is just an alias to this type.

The standard library contains two functions that work with pairs (tuples of two elements): fst and snd:

fst : Tuple a b -> a

The first projection.

Examples

val a = fst (0, "s")
//    = 0

snd : Tuple a b -> b

The second projection.

Examples

val a = snd (0, "s")
//    = "s"

Triples

val sumTriple = \((a, b, c) : Tuple Int Int Int) -> a + b + c
val x = sumTriple (1,2,3)
//    = 1 + 2 + 3 = 6
val tripleToPair = \((a, b, c) : Tuple Int Int String) -> (a + b, c)
val x = tripleToPair (1, 2, "Hello")
//    = (3, "Hello")

Tuples are a convenient way to group pieces of data in a longer computation without a need for explicitly naming the type grouping the data.

val f = \(x : Int) -> x + x
val g = \(y : Int) -> y * y
val xs = [1, 2, 3]
val ys = [4, 5, 6]
val combined = List::map (\(x,y) -> (f x, g y)) (List::zip xs ys)
//           = [(2, 16), (4, 25), (6, 36)]