CS 541 Lecture -*- Outline -*- * examples of denotational semantics ** arithmetic language ----------------------------- DENOTATIONAL SEMANTICS OF ARITHMETIC EXPRESSIONS Abstract Syntax: Expr ::= num N | Op Expr Expr Op ::= add | sub | mult | divide N ::= Digit | N Digit Digit ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 Semantic Domains: Integer (see Watt's appendix D.1.3) Semantic Functions: valuation: Expr -> Integer opVal: Op -> (Integer -> Integer -> Integer) numVal: N -> Integer digitVal: Digit -> Integer valuation[[num n]] = digitVal[[n]] valuation[[f e1 e2]] = (opVal[[f]]) (valuation[[e1]]) (valuation[[e2]]) opVal[[add]] x y = sum(x,y) opVal[[sub]] x y = difference(x,y) opVal[[mult]] x y = product(x,y) opVal[[divide]] x y = if y = 0 then fail else truncated-quotient(x,y) digitVal[[0]] = 0 digitVal[[1]] = 1 digitVal[[2]] = 2 digitVal[[3]] = 3 digitVal[[4]] = 4 digitVal[[5]] = 5 digitVal[[6]] = 6 digitVal[[7]] = 7 digitVal[[8]] = 8 digitVal[[9]] = 9 numVal[[d]] = digitVal[[d]] numVal[[n d]] = sum(product(10, digitVal[[n]]), digitVal[[d]]) ------------------------------- ** simply typed lambda calculus (core of SML) --------------------------- DENOTATIONAL SEMANTICS OF SIMPLY TYPED LAMBDA CALCULUS (CALL BY VALUE VERSION as in SML) Abstract Syntax: Expr ::= Identifier | (Expr Expr) | (fn Identifier : Type => Expr) Type ::= int | bool | (Type -> Type) Semantic Domains: Integer = {0,1,-1,2,-2, ...} (see Watt's appendix D.1.3) TruthValue = {true, false} (see Watt's appendix D.1.2) Value = integer Integer + truth TruthValue + func (Value -> Value) Bindable = Value Environ = Identifier -> (bound Bindable + unbound) (see Watt's appendix D.1.13) --------------------------- The recursion in the definition of Value should worry you a bit, because in Set theory, the equation V = V -> V can't be solved, as the cardinality of V -> V is larger than the cardinality of V. It was this problem that led Dana Scott to the notions of continuity and domains in denotational semantics --------------------------- Semantic Functions: type-meaning: Type -> Powerset(Value) valuation: Expr -> Environ -> Value type-meaning[[int]] = {integer(i) | i in Integer} type-meaning[[bool]] = {truth(tv) | tv in TruthValue} type-meaning[[(t1 -> t2)]] = {func(f) | dom(f) = type-meaning[[t1]], range(f) = type-meaning[[t2]]} valuation[[id]] env = find(env,id) valuation[[(e1 e2)]] env = let func(v1) = valuation[[e1]] env v2 = valuation[[e2]] env in v1(v2) valuation[[(fn id:t => e)]] env = func(\x. if x in type-meaning[[t]] then valuation[[e]] (overlay(bind(id,x),env)) else fail) ------------------------------- Q: What if you try to do this in SML? Does it come out the same? you're going to have some difficulty with the meaning of arrow types, as you'll have to represent the information in some other way (the sets are too big in practice for their use) this is the notion of abstract specification, vs. implementation. Q: how does this differ from operational semantics of the \-calculus? *** call by name Q: how can this be made into a call-by-name language? ------------------------- CALL BY NAME VERSION (CHANGES) Semantic Domains: Value = integer Integer + truth TruthValue + func (Value -> Value) Bindable = Unit -> Value Semantic Functions: valuation[[id]] env = (find(env,id))() valuation[[(e1 e2)]] env = let func(v1) = valuation[[e1]] env v2 = \u.(valuation[[e2]] env) in (v1 v2) valuation[[(fn id:t => e)]] env = func(\x. (valuation[[e]] (overlay (bind(id, (\u. if x() in type-meaning[[t]] then x() else fail)), env)))) ----------------------- Q: what is the value of ((fn x:int => (fn y:int => x)) (loop-forever 3)) ? *** call by text Q: How would you do call-by-text? ------------------------- CALL BY TEXT VERSION (CHANGES) Semantic Domains: Value = integer Integer + truth TruthValue + func (Value -> Value) Bindable = exp Expr + inValue Value Semantic Functions: valuation[[id]] env = (case find(env,id) of inValue(v) => v | exp(e) => valuation[[e]] env) valuation[[(e1 e2)]] env = let func(v1) = valuation[[e1]] env in (v1 [[e2]]) valuation[[(fn id:t => e)]] env = func(\x. (valuation[[e]] (overlay(bind(id,exp(x)), env)))) ---------------------------- Q: what is valuatation[[((fn x:int => ((fn p:(int -> int) => (p x)) (fn y:int => ((fn x:int => y) two)))) one)]] (overlay(bind(one, inValue(integer(1)), bind(two, inValue(integer(2)))))) ? ** fl, a functional language (omitted in 1994) see the file fl-semantics.sml or fl-semantics2.sml This is a functional language, whose semantics was originally done by Gifford at MIT The style is in the tradition of Schmidt's book.