For the context of the next few chapters, we will be considering a small, first-order, functional programming language working on integers and Booleans, called SFUN.

Evaluation strategies

We can classify functional languages according to the evaluation strategy they implement:

  • Call-by-value: argument expressions are evaluated before applying function definitions
  • Call-by-name: function definitions are applied before evaluating argument expressions

Syntax of SFUN

Let be a set of variables and left be a set of function names each with a fixed arity .

The terms of the language SFUN are defined by the grammar:

  • Where represents an integer value, .
  • Where represents a Boolean value, .

The notation is used to denote the variables that occur in the term .

  • A closed term is a term such that , that is, a term that contains no variables.

A program in SFUN is a set of recursive equations:

  • Where are terms.
  • Where .
  • Such that there is only one equation for each function name .

Equations may be recursive, hence the terms may contain occurrences of .

-- Example SFUN programs
max(x, y) = if x >= y then x else y
fact(x)   = if x <= 0 then 1 else x * fact(x - 1)
 
square(x)             = x * x
quadratic(x, a, b, c) = a * square(x) + b * c + c
 
mod(x, y)  = if x - y < 0 then x else mod(x - y, y)
even(x)    = mod(x, 2) = 0
collatz(x) = if x = 1 then 1 else if even(x) then x / 2 else 3 * x + 1

Operational Semantics of SFUN

In this section, the semantics of SFUN are provided in structural operation semantics style.

We assume that programs are well-typed.

  • We define the evaluation relation (a big-step semantics) for terms in the context of the program using a transition system where configurations are just terms.
  • The values of the system are integer and Boolean values.
  • The evaluation relation relates closed SFUN terms to values in the context of , and is denoted by .
  • The evaluation strategy we model is call-by-value.

The evaluation relation is defined inductively as:

Example: call-by-value evaluation

Let be the program:

infinity = infinity + 1
fortytwo(x) = 42
square(x) = x * x

The term has the value , that is, .

The term does not have a value, because the evaluation of the argument gives no value. A derivation for cannot be constructed because it recurses infinitely on the rule . The term has the value , .

Example: call-by-value derivation for the evaluation of the term .

Unicity of Normal Forms

The evaluation strategy defined by the axioms and rules is deterministic, that is: Theorem: for any closed term , and implies . Proof: by rule induction.

Details of proof of determinism

We distinguish cases according to the rule that applies to . For any term, there is only one rule that can be applied.

Base cases (axioms):

  • If is an integer then using the axiom ()
  • If is a Boolean then using the axiom ()

Therefore, there is only one value in both cases. There are no more base cases, because is closed and thus cannot contain variables.

Inductive cases (rules):

  • Assume is the term .
  • Then, using the rule , if and only if , and .
  • By the induction hypothesis, there is at most one value for each term and .
  • Therefore is uniquely determined.

The cases corresponding to the other rules are similar.