Skip to content
Exercises · Assignment 3

FCPLCD Assignment 3Download PDF

In the last lectures you’ve seen the defined of a language by its syntax and semantics. In this exercise we are going to see this in practice by implementing a simple language and establishing some useful properties of it.

Defining the Syntax

We will call our new language FCPLang. It’s going to contain natural numbers, booleans, a conjunction operator and conditionals. The following is the concrete syntax of our language:

Here are some FCPLang terms:

FCPLang
true
if true then 1 else false
if (if 1 then true else false) then 2 else 3
true and (if true then 1 else 2)
EXERCISE 3.1
Abstract Syntax

Define the abstract syntax of FCPLang that corresponds to the concrete syntax presented above. That is, define a data type FCPTerm that represents the terms of FCPLang. Name its constructors boolT, natT, andT and iteT, as the code below does.

Defining Semantics as a Relation

In the lecture you have seen different ways to formalize the semantics of a language. We start with the simplest one here: small-step semantics, implemented as a relation.

Single-Step

For single-step evaluation (), you have already seen the following rules:

Since we extended the language you know from the lecture by the and operator, we also need new evaluation rules. Think about which forms the term could have and how they should be evaluated.

We can now formalize this relation as an inductively defined proposition in Rocq, which we call Eval1. Since is a relation between two terms, Eval1 is parameterized over two FCPLang terms, and each of its constructors corresponds to one of the rules for . We have formalized the first rule for you:

Rocq
Inductive Eval_1 : FCPTerm -> FCPTerm -> Prop :=
| E_If_True : forall t1 t2,
Eval_1 (iteT (boolT true) t1 t2) t1
(* ... *).
EXERCISE 3.2
Single-Step Evaluation

There are different sets of rules for and, but you can get away with just three rules. Complete the definition of Eval1 by adding the remaining constructors, i.e. the remaining rules for conditionals and your rules for and. Subsequently, add the notation →1 for Eval1.

Multi-Step

EXERCISE 3.3
Multi-Step Evaluation

Define a relation Evaln that forms the reflexive and transitive closure of Eval1. As before, use an inductive type for this definition. Subsequently, add the notation →n for Evaln, and prove the following transitivity theorem by induction over the proof of :

Normal Forms

RECAP 3.4

In the lecture, the concept of normal forms was introduced. Before we start coding, recall the definitions for yourself.

  1. When is a term in normal form?
  2. When is an evaluation relation deterministic?
EXERCISE 3.5
Defining Normal Forms

Define a predicate isNF t that holds if a given term t : FCPTerm is in normal form.

Uniqueness and Termination

In the lecture you have seen some useful properties a language can have. Among them was the theorem that each term has at most one normal form.

EXERCISE 3.6
Uniqueness of Normal Forms

Along the way, you will want to use the inversion tactic, as shown in the lecture. Prove that normal forms are unique:

As an intermediate step, prove that single-step evaluation is deterministic:

EXTRA 3.7
Termination

Caution: This one is hard! Prove that evaluation terminates, i.e. that every term has a normal form:

Values

We talked about normal forms as the result, or endpoint, of an evaluation. But usually we want the result of an evaluation to be a value (hence the name).

EXTRA 3.8
Values

Define a predicate isVal t that holds if a given term t is a value. You may either use a Definition or an Inductive. Next evaluate (or even prove) whether this predicate is different from isNF?