Term definition

A term definition has the formf p_1 p_2 … p_n = ewherefis the name of the term being defined. The parametersp_1throughp_nare the names of parameters, if any (if the term is a function), separated by spaces. The right-hand side of the=sign is anyUnison expression.

The names of the parameters as well as the name of the term are bound as local variables in the expression on the right-hand side (also known as thebodyof the function). When the function is called, the parameter names are bound to any arguments passed in the call. Seefunction applicationfor details on the call semantics of functions.

If the term takes no arguments, the term has the value of the fully evaluated expression on the right-hand side and is not a function.

The expression comprising the right-hand side can refer to the name given to the definition in the left-hand side. In that case, it’s a recursive definition. For example:

sumUpTo : Nat -> Nat
sumUpTo n =
  use Nat + - <
  if n < 2 then n else n + sumUpTo (n - 1)

The above defines a functionsumUpTothat recursively sums all the natural numbers less than some numbern.As an example,sumUpTo 3is1 + 2 + 3,which is6.

Note: The expressionn Nat.- 1on line 4 above subtracts1from the natural numbern.Since the natural numbers are not closed under subtraction (''n Nat.subtractInt 1'' is anInt),we use the operation-which has the convention that0 Nat.- n Nat.== 0for all natural numbersn.Unison's type system saves us from having to deal with negative numbers here.