CS5205Semantics1 CS5205: Foundation in Programming Languages Semantics Static Semantics Dynamic Semantics Operational Semantics Big-step Small-Step Denotational Semantics Abstract Machines
CS5205Semantics2 Introduction Introduction Important to give precise meanings to programs. Semantics provide a formal definition for the constructs of a programming language. Static Semantics : abstract properties ascertained at compile-time e.g. variable scope and typing rules; Dynamic Semantics : differs across languages with different abstraction levels provides detailed run-time properties
CS5205Semantics3 An Example Language An Example Language Expression Syntax e ::= Var x | Num i | Plus e 1 e 2 | Times e 1 e 2 | Bool b | Neg e | Or e 1 e 2 | And e 1 e 2 | If e 1 e 2 e 3 | Let x e 1 e 2 | Lam v e | Apply e 1 e 2 | Ref e | Assign e 1 e 2 | ! e | Pair e 1 e 2 | Fst e | Snd e | While e 1 e 2 | e 1 ; e 2 Three categories (i) pure expressions, (ii) commands, (iii) expressions with side-effetcs
CS5205Semantics4 An Example Language An Example Language Possible Values Val ::= ( ) | i | b | l | (Lam x. e) | (Val 1,Val 2 ) ` v v 2 Static Semantics – Scope Rules Static Semantics – Scope Rules Checking that all variables are correctly bound. ` expr set of free variables expression to check judgment for scope rule
CS5205Semantics5 Variable Scope Rules Variable Scope Rules ` Var x x 2 ` Num i ` Bool b ` op e 1.. e n ` e 1 ` e n …. ` Lam x e {x} ` e ` Letrec x e 1 e 2 {x} ` e 1 {x} ` e 2
CS5205Semantics6 Type Judgment Type Judgment ` e : t type environment type expression Possible Types t ::= Void | Int | Bool | Ref t | t 1 ! t 2 | (t 1,t 2 ) ` Letrec x:t e 1 e 2 : t 2 {x:t} ` e 1 : t {x:t} ` e 2 : t 2 ` Var x t 2 (x)
CS5205Semantics7 Transition System Transition System A transition is a structure ( , ! ) where is a set of elements (called configuration) and ! :: x is a binary relation (called a transition relation). Terminal Transition System Terminal Transition System A terminal transition is a structure ( , !, T ) where ( , ! ) is a transition system, while T µ is a set of final configurations that satisfies: 8 y 2 T. : ( 9 y' 2 . y ! y').
CS5205Semantics8 Dynamic Semantics Dynamic Semantics Previously, we use for a pure language: One-step: e 1 ! e 2 Many-steps: e 1 ! * e 2 In case of side-effects (e.g memory store), we will need a configuration. Thus, better to use: ! store command variable environment
CS5205Semantics9 Structured Operational Semantics Structured Operational Semantics Small Step Semantics: ! Good for progress + preservation proofs Big Step Semantics: E ` ) Good for optimization proofs
CS5205Semantics10 Environment Environment E : Var ! Value Extension : E + {x:v} Update : E {x:v} Removal : E - {x} Look-up : E(x) OPERATIONS
CS5205Semantics11 Store Store Loc ! Value Extension : + {l : v} Update : {l : v} Removal : - {l} Look-up : (l) OPERATIONS :
CS5205Semantics12 Big Step Semantics Big Step Semantics E ` )
CS5205Semantics13 Big Step Semantics Big Step Semantics E ` ! E ` ) E+{x:v 1 } ` )
CS5205Semantics14 Big Step Semantics Big Step Semantics E ` ) fresh l E ` ) E ` )
CS5205Semantics15 Small Step Semantics Small Step Semantics !
CS5205Semantics16 Small Step Semantics Small Step Semantics !
CS5205Semantics17 Small Step Semantics Small Step Semantics ! fresh l
CS5205Semantics18 Small Step Semantics Small Step Semantics !
CS5205Semantics19 Small Step Semantics Small Step Semantics ! intermediate expression
CS5205Semantics20 Homework Homework Finish the rest of the (i) big-step operational semantics (ii) small-step operational semantics for our simple language.
CS5205Semantics21 Denotational Semantics Denotational Semantics describes meaning of program as a mathematical object typically: denotation of a command "maps states to states“ denotation of a pure expression "maps states to value“ denotation of an expression with effects "maps states to states and value"
CS5205Semantics22 Denotation Denotation Pure Expression [[ Expr ]] :: State ! Value [[ Num i ]] st = i [[ Var v ]] st = st(v) [[ e 1 + e 2 ]] st = [[ e 1 ]] st + [[ e 2 ]] st
CS5205Semantics23 Denotation Denotation Command with Side-Effects [[ Command ]] :: State ! State [[ skip ]] st = st [[ s 1 ; s 2 ]] st = [[ s 2 ]] ([[ s 1 ]] st) [[ v := e ]] st = st ( v -> [[ e ]] st )
CS5205Semantics24 Denotation Denotation Expression with side-effect [[ Expr ]] :: State ! (State, Value) [[ Ref e ]] st = let (st1,v) = [[ e ]] in let l = freshLoc in (st1 + [l->new v], l) [[ Let v e 1 e 2 ]] st = let (st 1,v 1 ) = [[ e 1 ]] st in let (st 2,v 2 ) = [[ e 2 ]] st 1 +(v:v 1 ) in (st 2 -{v},v 2 )
CS5205Semantics25 Abstract Machine Abstract Machine - lower-level semantics where abstract configuration is closer to actual machine - made popular via Java abstract machine where source code is first compiled to byte code format - Two key uses of abstract machine: (i) portable architecture with an interpreter (ii) allow type-checking of byte code
CS5205Semantics26 Abstract Machine Abstract Machine Configuration Configuration : Stack : [ Value ] Env : Var ! Value Heap : Loc ! Value Control : [ Expr ] Initial Configuration : Final Configuration:, if e is an expression,if e is a command
CS5205Semantics27 Transition System Transition System )
CS5205Semantics28 Transition System Transition System ) ) )
CS5205Semantics29 Transition System Transition System ) ) )
CS5205Semantics30 Transition System Transition System ) ) where not (l 2 dom(H)) )
CS5205Semantics31 Transition System Transition System )
CS5205Semantics32 Transition System Transition System )
CS5205Semantics33 Transition System Transition System ) ) ) )
CS5205Semantics34 Java Abstract Machine Java Abstract Machine Reference : Tobias Nipkow and David von Oheimb Java Light is Type-Safe – Definitely, POPL Can be viewed as a bytecode language. - Being used as a portable implementation for Java. - Important to check for type-safety (subject of the paper). - Useful to prove correctness of compilation and optimization.