340 likes | 522 Views
CS5205: Foundation in Programming Languages Semantics. Static Semantics Dynamic Semantics Operational Semantics Big-step Small-Step Denotational Semantics Abstract Machines. Introduction. Important to give precise meanings to programs.
E N D
CS5205: Foundation in Programming Languages Semantics • Static Semantics • Dynamic Semantics • Operational Semantics • Big-step • Small-Step • Denotational Semantics • Abstract Machines Semantics
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 abstractionlevels provides detailed run-time properties Semantics
An Example Language Expression Syntax e ::= Var x | Num i | Plus e1 e2 | Times e1 e2 | Bool b | Neg e | Or e1 e2 | And e1 e2| If e1 e2 e3 | Let x e1 e2 | Lam v e | Apply e1 e2 | Ref e | Assign e1 e2 | ! e | Pair e1 e2 | Fst e | Snd e | While e1 e2 | e1 ; e2 Three categories (i) pure expressions, (ii) commands, (iii) expressions with side-effetcs Semantics
Static Semantics – Scope Rules Checking that all variables are correctly bound. judgment for scope rule v 2 ` expr ` v set of free variables expression to check An Example Language Possible Values Val ::= ( ) | i | b | l | (Lam x . e) | (Val1,Val2) Semantics
Variable Scope Rules x 2 ` Num i ` Bool b ` Var x ` e1 ` en + {x}` e …. ` Lam x e ` op e1 .. en + {x}` e1 + {x}` e2 ` Letrec x e1 e2 Semantics
+ {x:t}` e1 : t t 2(x) + {x:t}` e2 : t2 ` Var x ` Letrec x:t e1 e2 : t2 Type Judgment expression ` e : t type environment type Possible Types t ::= Void | Int | Bool | Ref t | t1! t2 | (t1,t2) Semantics
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'). 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). Semantics
Thus, better to use: <E1,1,e1 > ! <E2,2,e2 > variable environment command store Dynamic Semantics Previously, we use for a pure language: One-step: e1! e2 Many-steps: e1!* e2 In case of side-effects (e.g memory store), we will need a configuration. Semantics
Big Step Semantics: E ` <1,e1 > ) <2,val > Good for optimization proofs Structured Operational Semantics Small Step Semantics: <E1,1,e1 > ! <E2,2,e2 > Good for progress + preservation proofs Semantics
OPERATIONS Look-up : E(x) Extension : E + {x:v} Update : E {x:v} Removal : E - {x} Environment E : Var ! Value Semantics
OPERATIONS : Look-up : (l) Extension : + {l : v} Update : {l : v} Removal : - {l} Store : Loc ! Value Semantics
Big Step Semantics E ` <,Num i > ) <,i > E ` <, e1 > ) <1, v1 > E ` <1, e2 > ) <2, v2 > E ` <,Plus e1 e2 > ) <2, v1+v2 > Semantics
E ` <, e1 > ) <1, (Lam x e) > E ` <1, e2 > ) <2, v2 > E ` <, e1 > ) <1, v1 > E ` <, Apply e1 e2 > ! <2, [xv2] e> E+{x:v1} ` <1, e2 > ) <2, v2 > E ` < , Letrec x e1 e2> ) < 2, v2> Big Step Semantics Semantics
fresh lE ` <, e> ) <1, v > E ` <, Ref e > ) <1+{l:v}, l> E ` <, e1 > ) <1, l > E ` <1, e2 > ) <2, v2 > E ` < , e1 := e2> ) < 2{l:v2}, ( )> E ` <, e> ) <1, l > E ` <, ! e > ) <1, 1(l)> Big Step Semantics Semantics
<E, , e1 > ! <E’, ’, e’1 > <E, , e2 > ! <E’, ’, e’2 > <E, ,Plus e1 e2 > ! <E’, ’, Plus e’1 e2 > <E, ,Plus v1 e2 > ! <E’, ’, Plus v1 e’2 > Small Step Semantics <E, ,Num i > ! <E, ,i > <E, ,Plus v1 v2 > ! <E, ’, v1 + v2 > Semantics
<E, , e1 > ! <E’, ’, e’1 > <E, , e2 > ! <E’, ’, e’2 > <E, , Apply e1 e2 > ! <E’, ’, Apply e’1 e2 > <E, , Apply v1 e2 > ! <E’, ’, Apply v1 e’2 > Small Step Semantics <E, , Apply (Lam x e) v2 > ! <E, , [x v2] e > Semantics
<E, , e> ! <E, ’, e’ > fresh l <E, , Ref e > ! <E’, ’, Ref e’> <E, , Ref v > ! <E, +{l:v}, l > Small Step Semantics Semantics
Small Step Semantics <E, , e1 > ! <E’, , e1’> <E, , e1 := e2 > ! <E’, ’, e1’ := e2 > <E, , e2 > ! <E’, , e2’> <E, , v1 := e2 > ! <E’, ’, v1 := e2’ > <E, , l := v > ! <E’, {l v}, ( ) > Semantics
<E, , e1 > ! <E’, ’, e1’> <E, , Letrec x e1 e2 > ! <E’, ’, Letrec x e1’ e2> <E, , e > ! <E’, ’, e’> <E, , Ret x e > ! <E’, ’, Ret x e’> Small Step Semantics <E,, Letrec x v1 e2 > ! < E+{x:v1}, ’, Ret x e2> intermediate expression <E, , Ret x v > ! <E-{x}, , v> Semantics
Homework Finish the rest of the (i) big-step operational semantics (ii) small-step operational semantics for our simple language. 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" Semantics
Denotation Pure Expression [[ Expr ]] :: State ! Value [[ Num i ]] st = i [[ Var v ]] st = st(v) [[ e1 + e2 ]] st = [[ e1 ]] st + [[ e2 ]] st Semantics
Denotation Command with Side-Effects [[ Command ]] :: State ! State [[ skip ]] st = st [[ s1 ; s2 ]] st = [[ s2 ]] ([[ s1 ]] st) [[ v := e ]] st = st ( v -> [[ e ]] st ) Semantics
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 e1 e2 ]] st = let (st1,v1) = [[ e1 ]] st in let (st2,v2) = [[ e2 ]] st1+(v:v1) in (st2-{v},v2) Semantics
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 Semantics
Abstract Machine • Configuration : <Stack, Env, Heap, Control> • Stack : [ Value ] • Env : Var ! Value • Heap : Loc ! Value • Control : [ Expr ] Initial Configuration : < [ ], { }, { }, [e] > Final Configuration: < [v], [], H, [] >, if e is an expression < [], [], H, [] >, if e is a command Semantics
Transition System <S, E, H, (Num i) C> ) <i S, E, H, C> <S, E, H, (Var x) C> ) <(E x) S, E, H, C> <S, E, H, (Plus e1 e2) C> ) <S, H, e1 e2 (Plus) C> <v2 v1 S, E, H, (Plus) C> ) <(+ v1 v2) S, E, H, C> Semantics
Transition System <S, E, H, (Let x e1 e2) C> ) <S, E, H, e1 (Let x e2) C> <v S, E, H, (Let x e2) C> ) <S, E+(x:v), H, e2 (Pop x) C> <S, E, H, (Pop x) C> ) <S, E-{x}, H C> Semantics
Transition System <S, E, H, (Lam x e) C> ) <(Lam x e) S, E, H, C> <S, E, H, (Apply e1 e2) C> ) <S, E+(x:v), H, e1 e2 (Apply) C> <v (Lam x e) S, E, H, (Apply) C> ) <S, E+(x:v), H, e (Pop x) C> Semantics
Transition System <S, E, H, (Ref e) C> ) <S, E, H, e (Ref) C> <v S, E, H, (Ref) C> ) <S, E, H+{l:v}, C> where not (l2 dom(H)) < v l S, E, H, (:=) C> ) <S, E, H {l:v}, C> <S, E, H, (e1 := e2) C> ) <S, E, H, e1 e2 (:=) C> Semantics
Transition System <S, E, H, (Pair e1 e2) C> ) <S, E, H, e1 e2 (Pair) C> < v2 v1 S, E, H, (Pair) C> ) <(v1,v2) S, E, H, C> <(v1,v2) S, E, H, (Fst) C> ) <v1 S, E, H, C> <(v1,v2) S, E, H, (Snd, C> ) <v2 S, E, H, C> Semantics
Transition System <S, E, H, (If e1 e2 e3) C> ) <S, E, H, e1 (If e2 e3) C> <true S, E, H, (If e2 e3) C> ) <S, E, H, e2 C> <false S, E, H, (If e2 e3) C> ) <S, E, H, e3 C> Semantics
Transition System <S, E, H, (e1;e2) C> ) <S, E, H, e1 e2 C> <S, E, H, Skip C> ) <S, E, H, C> <S, E, H, (While e1 e2) C> ) <S, E, H, e1 (Loop-If e1 e2) C> <true S, E, H, (Loop-If e1 e2) C> ) <S, E, H, e2 e1 (Loop-If e1 e2) C> <false S, E, H, (Loop-If e1 e2) C> ) <S, E, H, C> Semantics
Java Abstract Machine • Reference : • Tobias Nipkow and David von Oheimb • JavaLight is Type-Safe – Definitely, POPL 1998. • 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. Semantics