If you have been following the exercises you should have an almost complete encoding of Ola’s syntax and typing rules. If you have performed the extra exercises from lecture 1 then you can now look at updating your implementation to evaluate those new terms. Concretely.
Evaluate Type Annotations and Products/Pairs
Extend your mechanisation to include the following data structures and operations:
Type Annotations
Type annotations, reduce by evaluating the expression and forgetting the type itself.
g |- e ~>* v
---- [ Annotations ]
g |- (the t e) ~>* v
Product Types
Pairs with two values, together with primitives to access the first and second element.
Their constructor is a value and evaluating pairs require evaluation of both the first and second element.
g |- l ~>* v_{l}
g |- r ~>* v_{r}
---- [ Pair ]
g |- (l,r) ~>* (v_{l}, v_{r})
First and second, drop the second and first elements.
g |- e ~>* (v_{l}, v_{r})
---- [ First ]
g |- fst e ~>* v_{l}
g |- e ~>* (v_{l}, v_{r})
---- [ First ]
g |- snd e ~>* v_{r}
Sum Types
Extend your mechanisation further to support sums, like pairs both the Left and Right terms are values.
For both left and right terms we evaluate them so that we have their values.
g |- e ~>* Left e
---- [ Left Simplify ]
g |- e ~>* Left e
g |- e ~>* v
---- [ Left Value]
g |- Left e ~>* Left v
For match, we evaluate the scrutinee till and value and then evaluate the corresponding branch.
g |- e ~>* v
---- [ Match Scrutinee ]
g |- match e { Left lref => l; Right rref => r }
~>*
match v { Left lref => l; Right rref => r }
g,(lref,v_{l}) |- l ~>* v
---- [ Match Left ]
g |- match Left v_{l} { Left lref => l; Right rref => r }
~>*
v
g,(rref,v_{r}) |- r ~>* v
---- [ Match Left ]
g |- match Right v_{r} { Left lref => l; Right rref => r }
~>*
v