1 / 34

CS5205: Foundation in Programming Languages Semantics

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.

nizana
Download Presentation

CS5205: Foundation in Programming Languages Semantics

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS5205: Foundation in Programming Languages Semantics • Static Semantics • Dynamic Semantics • Operational Semantics • Big-step • Small-Step • Denotational Semantics • Abstract Machines Semantics

  2. 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

  3. 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

  4. 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

  5. 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

  6.  + {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

  7. 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

  8. 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

  9. 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

  10. OPERATIONS Look-up : E(x) Extension : E + {x:v} Update : E {x:v} Removal : E - {x} Environment E : Var ! Value Semantics

  11. OPERATIONS : Look-up : (l) Extension :  + {l : v} Update :  {l : v} Removal :  - {l} Store  : Loc ! Value Semantics

  12. Big Step Semantics E ` <,Num i > ) <,i > E ` <, e1 > ) <1, v1 > E ` <1, e2 > ) <2, v2 > E ` <,Plus e1 e2 > ) <2, v1+v2 > Semantics

  13. E ` <, e1 > ) <1, (Lam x e) > E ` <1, e2 > ) <2, v2 > E ` <, e1 > ) <1, v1 > E ` <, Apply e1 e2 > ! <2, [xv2] e> E+{x:v1} ` <1, e2 > ) <2, v2 > E ` < , Letrec x e1 e2> ) < 2, v2> Big Step Semantics Semantics

  14. 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

  15. <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

  16. <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

  17. <E, , e> ! <E, ’, e’ > fresh l <E, , Ref e > ! <E’, ’, Ref e’> <E, , Ref v > ! <E, +{l:v}, l > Small Step Semantics Semantics

  18. 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

  19. <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

  20. Homework Finish the rest of the (i) big-step operational semantics (ii) small-step operational semantics for our simple language. Semantics

  21. 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

  22. Denotation Pure Expression [[ Expr ]] :: State ! Value [[ Num i ]] st = i [[ Var v ]] st = st(v) [[ e1 + e2 ]] st = [[ e1 ]] st + [[ e2 ]] st Semantics

  23. 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

  24. 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

  25. 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

  26. 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

  27. 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

  28. 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

  29. 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

  30. 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

  31. 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

  32. 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

  33. 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

  34. 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

More Related