Start of topic | Skip to actions

Staged Intepreter (cont.)

Some discussions before main topic

Jun asked if we are able to write untyped functions in a typed setting.

Walid: It is not easy to change the use of a language. There is somethign called "predicate subtype", which might does similar thing. But it will introduce undecidability and disable static checking. For example, it is not easy to treat a sized list as an unsized list and write functions on it. Maybe possible for small programms, but it will looks pretty ugly.

If the data type of environment is a list?

Walid: Not really. Usually, the environment behaves more like a tuple instead of a list.

This is because we can have difference date types in an enviroment. Remember that for a list, all elements in a list must have the same type, but using tuple, you can get different data types.

Why we don't use list combined with a type value defined using different tags to represent different types?

Walid: There is nothing wrong with using tags, and it is actually pretty convienient to do so. The downside is that the performance is not good because a lot of time is used on comparing the tags. Indexed type can allow us to eliminate tags statically. I.e, there is no tags, everything is known exactly as what it is. More details we can refer to his publications talking about "tagless interpreter".

Staged interpreter

we have untyped intepreter

eval e env = match e with 
   | Int i -> VInt i
   | Var Z -> first env
   | Var (S n) -> eval (Var n) (second env)  //env is tuple so it can have different types 
   | Lam e -> VFun (fun v -> eval e (EPair (v, env))
   | App e1 e2 -> | (unVFun (eval e1 env)) (eval e2 env)

you could use array to get a save on environment lookup, but environment disappear.

Following the the difference when we write list and tupe:

x: alpha y alpha list
Cons(x, y) : alpha list

x: alpha, y:beta
Pair(x, y) : ( alpha, beta) pair

A X B X C X D ~= A X ( BX (CX D X 1)))

Concoqtion encoding of typed term

type (env, t) term =
 | TInt of let env' in int : (env', Int) term
 | TVarZ of let t', env' in unit: ((env', t')pair, t') term
 | TVarS of let t, t'', env' in (env', t') term : ((env', t'') pair, t') term
 | TApp of let env', t1, t2 in (env' , Arrow t1 t2) term * (env', t1) term : (env', t2) term
 | TLam of let env', t', t'' in ((env' t') pair, t'') term: (env', Arrow t' t'') term

About lifting: (Sorry I didn't get the point here, and following is what Walid wrote on the board to explain it.)

lam x. lam y. x x
x x  #st # sz
     s(#0) s(#0)
     s(#0 #0)

So now look at the "eval" function, we want the index to allow us do more:

eval (e:(env, t) term) (env:(eval_env env) OcamlType) = 
     match e with   (*env*)
env'           | Int i -> VInt i
env', t'       | Var Z -> first env
env' t', t''   | Var (S n) -> eval (Var n) (second env)  
env' t' t''    | Lam e -> VFun (fun v -> eval e (EPair (v, env))
env', t', t''  | App e1 e2 -> | (unVFun (eval e1 env)) (eval e2 env)

Things appear before the "|"s are what extra information we have now.


End of topic
Skip to actions | Back to top
Creative Commons LicenseThis work is licensed under a Creative Commons Attribution 2.5 License. Please follow our citation guidelines.