Hello!
I am reading the book “Types and Programming Languages” by B.Pierce and I’m trying to implement the various models in Julia. The code I am trying to translate is the following (in Ocaml):
type term =
TmTrue of info
| TmFalse of info
| TmIf of info * term * term * term
| TmZero of info
| TmSucc of info * term
| TmPred of info * term
| TmIsZero of info * term
let rec isnumericval t = match t with
TmZero(_) -> true
| TmSucc(_,t1) -> isnumericval t1
| _ -> false
let rec isval t = match t with
TmTrue(_) -> true
| TmFalse(_) -> true
| t when isnumericval t -> true
| _ -> false
let rec eval1 t = match t with
TmIf(_,TmTrue(_),t2,t3) ->
t2
| TmIf(_,TmFalse(_),t2,t3) ->
t3
| TmIf(fi,t1,t2,t3) ->
let t1' = eval1 t1 in
TmIf(fi, t1', t2, t3)
| TmSucc(fi,t1) ->
let t1' = eval1 t1 in
TmSucc(fi, t1')
| TmPred(_,TmZero(_)) ->
TmZero(dummyinfo)
| TmPred(_,TmSucc(_,nv1)) when (isnumericval nv1) ->
nv1
| TmPred(fi,t1) ->
let t1' = eval1 t1 in
TmPred(fi, t1')
| TmIsZero(_,TmZero(_)) ->
TmTrue(dummyinfo)
| TmIsZero(_,TmSucc(_,nv1)) when (isnumericval nv1) ->
TmFalse(dummyinfo)
| TmIsZero(fi,t1) ->
let t1' = eval1 t1 in
TmIsZero(fi, t1')
| _ ->
raise NoRuleApplies
My attempt in Julia is the following
abstract type Info end
abstract type Term <:Info end
abstract type TmTrue <:Info end
abstract type TmFalse <:Info end
abstract type TmZero<:Info end
struct TmSucc
t::Term
end
struct TmPred
t::Term
end
struct TmIf
fi::Info
tmcond::Term
tmthen::Term
tmelse::Term
end
struct NoRuleApplies <:Exception end
function is_numerical(t::TmZero)
true
end
function is_numerical(x::TmSucc)
is_numerical(x.t)
end
function is_numerical(x)
false
end
function is_val(x::TmTrue)
true
end
function is_val(x::TmFalse)
true
end
function is_val(x)
is_numerical(x)
end
I’m stuck at the definition of eval1.
Thanks