MinML with Exceptions, Continuations and Evaluation Contexts ------------------------------------------------------------- types t ::= int | bool | t -> t | t cont expressions e ::= x | n | b | if e then e else e | fun f(x:t) : t = e | e e | try e with e | fail | callcc (\x.e) | throw e to e | cont(E) values v ::= n | b | fun(x:t) : t = e | cont(E) evaluation contexts (for call-by-value, left-to-right evaluation) E ::= [] | if E then e else e | E e | v E | try E with e | throw E to e | throw v to E top level evaluation e --> e e -->i e' --------------- E[e] --> E[e'] (try E' with e) not in E --------------------------- E[fail] --> fail ---------------------------------- E[throw v to cont (E')] --> E'[v] -------------------------------------- E[callcc (\x.e)] --> E[e[cont(E)/x]] beta reduction/ instruction rules e -->i e ----------------------------------- if true then e1 else e2 -->i e1 ----------------------------------- if false then e1 else e2 -->i e2 ------------------------------------------------------------- (fun f(x:t1) : t2 = e) v -->i e[fun f(x:t1) : t2 = e/f] [v/x] ---------------------- try v with e2 -->i v (try E' with e2') not in E ------------------------------ try E[fail] with e2 -->i e2 Typing Rules -------------- Before typing continuations we must decide what the type of the final result of the program is. Let's call that final result type "ans." It doesn't matter what ans is, we just have to be consistent with our choice when we pick it. The standard rules for MinML integers, booleans, functions are included in the type system. --------------- G |- fail : t G |- e1 : t G |- e2 : t ---------------------------- G |- try e1 with e2 : t G, x:t cont |- e : t ------------------------ G |- callcc (\x.e) : t G |- e1 : t G |- e2 : t cont ---------------------------------- G |- throw e1 to e2 : t' G, x:t |- E[x] : ans ------------------------ G |- cont (E) : t cont