CS5205 Semantics 1
CS5205: Foundation in Programming Languages Semantics
• Static Semantics
• Dynamic Semantics
• Operational Semantics• Big-step• Small-Step
• Denotational Semantics
• Abstract Machines
CS5205 Semantics 2
IntroductionIntroduction
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-timee.g. variable scope and typing rules;
Dynamic Semantics : differs across languages with different abstraction levelsprovides detailed run-time properties
CS5205 Semantics 3
An Example LanguageAn 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
CS5205 Semantics 4
An Example LanguageAn Example Language
Possible Values
Val ::= ( ) | i | b | l | (Lam x . e) | (Val1,Val2)
` v
v 2
Static Semantics – Scope RulesStatic Semantics – Scope Rules
Checking that all variables are correctly bound.
` expr
set of free variables
expression to check
judgment for scope rule
CS5205 Semantics 5
Variable Scope RulesVariable Scope Rules
` Var x
x 2 ` Num i ` Bool b
` op e1 .. en
` e1 ` en….
` Lam x e
{x}` e
` Letrec x e1 e2
{x}` e1
{x}` e2
CS5205 Semantics 6
Type JudgmentType Judgment
` e : t
type environment type
expression
Possible Types
t ::= Void | Int | Bool | Ref t | t1 ! t2 | (t1,t2)
` Letrec x:t e1 e2 : t2
{x:t}` e1 : t{x:t}` e2 : t2
` Var x
t 2 (x)
CS5205 Semantics 7
Transition SystemTransition 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 SystemTerminal 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').
CS5205 Semantics 8
Dynamic SemanticsDynamic 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.
Thus, better to use: <E1,1,e1 > ! <E2,2,e2 >
store commandvariable
environment
CS5205 Semantics 9
Structured Operational SemanticsStructured Operational Semantics
Small Step Semantics: <E1,1,e1 > ! <E2,2,e2 >
Good for progress + preservation proofs
Big Step Semantics: E ` <1,e1 > ) <2,val >
Good for optimization proofs
CS5205 Semantics 10
EnvironmentEnvironment
E : Var ! Value
Extension : E + {x:v}
Update : E {x:v}
Removal : E - {x}
Look-up : E(x)
OPERATIONS
CS5205 Semantics 11
StoreStore
Loc ! Value
Extension : + {l : v}
Update : {l : v}
Removal : - {l}
Look-up : (l)
OPERATIONS :
CS5205 Semantics 12
Big Step SemanticsBig Step Semantics
E ` <,Num i > ) <,i >
E ` <,Plus e1 e2 > ) <2, v1+v2 >
E ` <, e1 > ) <1, v1 >
E ` <, e2 > ) <2, v2 >
CS5205 Semantics 13
Big Step SemanticsBig Step Semantics
E ` <, Apply e1 e2 > ! <2, [xv2] e >
E ` <, e1 > ) <1, (Lam x e) >
E ` <, e2 > ) <2, v2 >
E ` < , Letrec x e1 e2> ) < 2, v2 >
E ` <, e1 > ) <1, v1 > E+{x:v1} ` <, e2 > ) <2, v2 >
CS5205 Semantics 14
Big Step SemanticsBig Step Semantics
E ` <, Ref e > ) <1+{l :v}, l >
fresh l E ` <, e > ) <1, v >
E ` < , e1 := e2> ) < 2{l:v2}, ( ) >
E ` <, e1 > ) <1, l > E ` <, e2 > ) <2, v2 >
E ` <, ! e > ) <1, 1(l) >
E ` <, e > ) <1, l >
CS5205 Semantics 15
Small Step SemanticsSmall Step Semantics
<E, ,Num i > ! <E, ,i >
<E, ,Plus e1 e2 > ! <E’, ’, Plus e’1 e2 >
<E, , e1 > ! <E’, ’, e’1 >
<E, ,Plus v1 e2 > ! <E’, ’, Plus v1 e’2 >
<E, , e2 > ! <E’, ’, e’2 >
<E, ,Plus v1 v2 > ! <E, ’, v1 + v2 >
CS5205 Semantics 16
Small Step SemanticsSmall Step Semantics
<E, , Apply e1 e2 > ! <E’, ’, Apply e’1 e2 >
<E, , e1 > ! <E’, ’, e’1 >
<E, , Apply v1 e2 > ! <E’, ’, Apply v1 e’2 >
<E, , e2 > ! <E’, ’, e’2 >
<E, , Apply (Lam x e) v2 > ! <E, , [x v2] e >
CS5205 Semantics 17
Small Step SemanticsSmall Step Semantics
<E, , Ref e > ! <E’, ’, Ref e’ >
<E, , e > ! <E, ’, e’ >
<E, , Ref v > ! <E, +{l:v}, l >
fresh l
CS5205 Semantics 18
Small Step SemanticsSmall Step Semantics
<E, , l := v > ! <E’, {l v} , ( ) >
<E, , e1 := e2 > ! <E’, ’, e1’ := e2 >
<E, , e1 > ! <E’, , e1’>
<E, , v1 := e2 > ! <E’, ’, v1 := e2’ >
<E, , e2 > ! <E’, , e2’>
CS5205 Semantics 19
Small Step SemanticsSmall Step Semantics
<E, , Letrec x e1 e2 > ! <E’, ’, Letrec x e1’ e2 >
<E, , e1 > ! <E’, ’, e1’>
<E,, Letrec x v1 e2 > ! < E+{x:v1}, ’, Ret x e2>
<E, , Ret x e > ! <E’, ’, Ret x e’ >
<E, , e > ! <E’, ’, e’>
<E, , Ret x v > ! <E-{x}, , v >
intermediateexpression
CS5205 Semantics 20
HomeworkHomework
Finish the rest of the
(i) big-step operational semantics(ii) small-step operational semantics
for our simple language.
CS5205 Semantics 21
Denotational SemanticsDenotational 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"
CS5205 Semantics 22
DenotationDenotation
Pure Expression
[[ Expr ]] :: State ! Value
[[ Num i ]] st = i
[[ Var v ]] st = st(v)
[[ e1 + e2 ]] st = [[ e1 ]] st + [[ e2 ]] st
CS5205 Semantics 23
DenotationDenotation
Command with Side-Effects
[[ Command ]] :: State ! State
[[ skip ]] st = st
[[ s1 ; s2 ]] st = [[ s2 ]] ([[ s1 ]] st)
[[ v := e ]] st = st ( v -> [[ e ]] st )
CS5205 Semantics 24
DenotationDenotation
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 inlet (st2,v2) = [[ e2 ]] st1+(v:v1) in (st2-{v},v2)
CS5205 Semantics 25
Abstract MachineAbstract 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
CS5205 Semantics 26
Abstract MachineAbstract Machine
ConfigurationConfiguration : <Stack, Env, Heap, Control>
Stack : [ Value ]Env : Var ! ValueHeap : Loc ! ValueControl : [ Expr ]
Initial Configuration : < [ ], { }, { }, [e] >
Final Configuration:< [v], [], H, [] >, if e is an expression
< [], [], H, [] >, if e is a command
CS5205 Semantics 27
Transition SystemTransition 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>
CS5205 Semantics 28
Transition SystemTransition 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>
CS5205 Semantics 29
Transition SystemTransition 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>
CS5205 Semantics 30
Transition SystemTransition 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 (l 2 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>
CS5205 Semantics 31
Transition SystemTransition 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>
CS5205 Semantics 32
Transition SystemTransition 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>
CS5205 Semantics 33
Transition SystemTransition 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>
CS5205 Semantics 34
Java Abstract MachineJava 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.
Top Related