So far, we have seen intrinsically-typed expressions and intrinsically-typed statements. Before we look at intrinsically-typed programs we will look at how we can intrinsically type memory.
Many imperative languages enable values to be placed on the heap and mutated. We have purposefully left detailing how we can type such programs until now to concentrate on intrinsically-typed terms.
Encoding ‘memory’ or references within Ola/Olaf takes inspiration from TAPLs Lambda Ref. Here we will describe the typing only.
First we need to extend our languages types with types for references &t.
We do so by adding an extra data constructor to Ty:
data Ty = INT | BOOL | REF Ty
Our next steps are to describe terms for allocation of memory, fetching of memory, and mutating memory. Lambda Ref is an expression language so we will provide expressions for these operations.
As a gentle reminder, the type/judgement form for intrinsically-typed expressions is:
data Expr : (ctxt : SnocList Ty)
-> (type : Ty)
-> Type
where
We begin with fetching memory. The typing rule for dereferencing memory is:
l |- a : &t
---- [ Deferencing ]
l |- &e : a
Fetch : (expr : Expr ctxt (REF type))
-> Expr ctxt type
Here is the rule for allocation:
l |- e : t
---- [ Allocation ]
l |- alloc e : &e
and its encoding in Idris:
Alloc : (expr : Expr ctxt type)
-> Expr ctxt (REF type)
Finally, here is the rule for mutation:
l |- mref : &t
l |- val : t
---- [ Allocation ]
l |- mref := val : unit
and its encoding in Idris:
Mutate : (ref : Expr ctxt (REF type))
-> (val : Expr ctxt type)
-> Expr ctxt UNIT
Remember, the syntax for Ola/Olaf is abstract syntax and there are two valid ways we can integrate memory expressions:
- we can add all these expressions to
Expr;varstatements can then disappear and are instances ofLetthatAllocthe value being bound; - we only add
FetchtoExpr, and makeAllocandMutatestatements;
Try integrating memory operations into Expr and Stmt using both approaches.
What do you notice about both approaches in terms of number of expressions and statements? Pay particular notice to the terms for allocating memory and mutating allocated memory.
The decisions we make here impact what the core language is for Ola/Olaf and what terms can be constructed using other terms.