If you have been following the exercises you should have an almost complete encoding of Ola’s syntax and typing rules, and their evaluation. Some extra exercises follow:
Method Type Arity
When completing checkArgs,
we needed to take into account that there may be more types than arguments and vice-versa.
The reason is because we used List.
If,
however,
we tracked the arity of arguments and types we can ensure that the list of arguments and types must be the same.
Vectors in Idris are defined as:
data Vect : Nat -> Type -> Type where
Nil : Vect Z a
(::) : (head : a)
-> (tail : Vect n a)
-> Vect (S n) a
To track the arity of method types we will need to realise SnocVect,
reverse vectors.
Define SnocVect to keep track the size of a backwards vector.
Add Type Annotations and Products/Pairs
Extend your mechanisation to include the following data structures and operations:
Type Annotations
Type annotations, ensuring that an expression has a specific type
e := (the t e) ...
g |- e : t
---- [ Annotations ]
g |- (the t e) : t
Product Types
Pairs with two values, together with primitives to access the first and second element;
t := (t,t) ...
e := (e,e) | fst e | snd e ...
g |- l : t_{l}
g |- r : t_{r}
---- [ New Pair ]
g |- (l,r) : (t_{l}, t_{r})
g |- e : (t_{l},t_{r})
---- [ First ]
g |- fst e : t_{l}
g |- e : (t_{l},t_{r})
---- [ Second ]
g |- snd e : t_{r}
Sum Types
Extend your mechanisation further to support sums:
t := <t,t> ...
e := Left e | Right e ...
s := match e { Left xref => s; Right xref => s }
g |- e : t_{l}
---- [ Left ]
g |- e : <t_{l},t_{r}>
g |- e : t_{r}
---- [ Right ]
g |- e : <t_{l},t_{r}>
g |- e : <t_{l},t_{r}>
g, (lref, t_{l}) |- l : t
g, (rref, t_{r}) |- r : t
---- [ Match ]
g |- match e { Left lref => l; Right rref => r }
Method Prototypes
Extend Ola/Olaf with support for method prototypes, enabling methods to be specified before their bodies.