Posts

Showing posts from January, 2011

A tiny bit of denotational semantics

All languages and assignments in this blog post are (C) 2011 Ralf Lämmel. This is actually an assignment for my paradigms/semantics class . Consider the following super-trivial interpreter written in Haskell and in the direct style of denotational semantics: data Exp = Const Int | Add Exp Exp eval :: Exp -> Int eval (Const i) = i eval (Add x y) = eval x + eval y It's time to test: > eval (Add (Const 20) (Const 22)) 42 This incredible language handles integer constants and addition. Now assume that we are adding an "exit" form of expression. The intended semantics of a term "Exit e " is to define the final result of expression evaluation as the value of e . Hence, if an exit expression occurs inside an addition, then the addition is effectively cancelled. Thus: data Exp = Const Int | Add Exp Exp | Exit Exp For instance: Add (Const 20) (Const 22) should evaluate to 42. Add (Exit (Const 88)) (Const 42) should evaluate to 88. A particularly clumsy