Documentation menu
Language

Functions and definitions

Functions, implicit arguments, definitions, annotations and holes, and the definitions that work at every universe level.

Find a section4 sections

Functions

\(x : A) -> body          a function
\x -> body                the same, when the type of x can be inferred
\(x : A) (y : B) -> body  a function of two arguments
\(x y : A) -> body        two arguments of one type
A -> B                    the type of the functions from A to B
forall (x : A) -> B       the same, when B mentions x
f x y                     an application
Type
let add = \(x : Natural) (y : Natural) -> x + y
let same = \(x y : Natural) -> x + y
let inferred = \x -> x + 1
let zeros = \(n : Natural) -> std:Vec.replicate n 0
in  { add, same, inferred, zeros }
Your result
Complete example · with imports

Type

{ add : Natural -> Natural -> Natural
, inferred : Natural -> Natural
, same : Natural -> Natural -> Natural
, zeros : forall (n : Natural) -> std:Vec.Vec Natural n
}

The type of zeros is a dependent function type: its result, a vector of n zeros, has a type that mentions the argument n.

A function that is an argument or an operand is written between parentheses, as in std:List.map (\(n : Natural) -> n + 1) xs. So is a let, an if, a forall or another construct that extends as far as it can.

Implicit arguments

An argument between braces is implicit: the type checker finds it from the other arguments. @ gives it explicitly:

Normal form
let id : forall {a : Type} -> a -> a = \x -> x
in  { inferred = id 1, given = id @Text "one" }
Your result
Complete example · with imports

Normal form

{ given = "one", inferred = 1 }

A function binds an implicit argument with \{a} -> …. When a function is checked against a type with implicit arguments, they are bound automatically, as in id above.

@ skips the implicit arguments that are universe levels, and gives the next one that is not. An argument that is itself a level goes to the next level argument instead:

Type
let id : forall {l : Level} {a : Type l} -> a -> a = \x -> x
in  { natural = id @Natural, types = id @1 }
Your result
Complete example · with imports

Type

{ natural : Natural -> Natural, types : forall {a : Type 1} -> a -> a }

So std:Monad.io @1 is the monad of IO on Type 1.

Definitions

let x = e                 x has the type of e
let x : A = e             e is checked against A
e : A                     an annotation: e, which has type A
_                         a hole: a term that the type checker finds

Each definition sees the ones before it:

Type
let a = 1
let b : Natural = a + 1
let xs : List _ = [ a, b ]
in  { sum = a + b, xs, count = (std:List.length xs : Natural) }
Your result
Complete example · with imports

Type

{ count : Natural, sum : Natural, xs : List Natural }

A definition does not see itself, so there is no recursion by name: no let rec and no general fix. Recursion goes through the eliminators of the data types, such as Natural/elim and List/elim, and the functions of the standard library that are built on them. Totality explains why, and how IO/loop describes an interaction that runs for an unbounded time.

Definitions at every level

A definition at the root of a file, without a type annotation, works at every universe level that nothing in it fixes. std:Map.Map takes types of any universe, and so does ByName:

Type
let ByName = std:Map.Map Text
in  { numbers = ByName Natural, types = ByName Type }
Your result
Complete example · with imports

Type

{ numbers : Type, types : Type 1 }

Each use of the definition chooses these levels, as an application chooses the implicit arguments of a function. The levels that nothing chooses are 0: main below is an action of type IO unit, which can be run, and which a record or a list can hold.

Program
let main = std:IO.forEach [ "a", "b" ] std:IO.putStrLn
in  main
Your result
Complete example · with imports

Output

a
b

These levels are not parameters of the definition, which @ would give. To give one, make it a parameter:

Type
let ByName = \{lv : Level} -> std:Map.Map @0 @lv Text
in  ByName @1 Type
Your result
Complete example · with imports

Type

Type 1