semantics
play

Semantics S E T O N T F A R D Gabriele Keller Where we are - PowerPoint PPT Presentation

Concepts of Program Design Semantics S E T O N T F A R D Gabriele Keller Where we are So far - Judgements and inference rules - Rule induction - Inference rules - Grammars specified using inference rules - Judgements and relations -


  1. Concepts of Program Design Semantics S E T O N T F A R D Gabriele Keller

  2. Where we are • So far - Judgements and inference rules - Rule induction - Inference rules - Grammars specified using inference rules - Judgements and relations - First- and higher-order abstract syntax - Substitution • Today - Static semantics - Dynamic semantics

  3. Static Semantics • What is static semantics? - properties of a program apparent without executing the program - can be checked by a compiler (or external tool such as lint ) • Example of static properties - does the program contain undefined occurrences of variables? - is the program type correct? - does it contain dead code, usage of uninitialised variables? • Arithmetic example language - there is only one type (Int), so not much to check - but we can check scoping (are all variables defined?)

  4. Scoping • Inference rules to check scoping - judgement e ok : e contains no free variables - how can we define this using inference rules? - key idea: we use an environment to keep track of all bound variables ‣ for now, the environment is just a set of variable names - composite judgement: ‣ { x 1 , x 2 ,..., x n } ⊢ e ok ‣ assuming the variables x 1 to x n are bound, e ok holds { y } ⊢ (Let y ( x . Plus x y )) ok {} ⊢ (Plus 1 3) ok { y,z } ⊢ (Let y ( x . Plus x y )) ok

  5. Scoping • Inference rules: Γ ⊢ (Num i) ok Γ ⊢ t 1 ok Γ ⊢ t 2 ok Γ ⊢ t 1 ok Γ ⊢ t 2 ok Γ ⊢ (Plus t 1 t 2 ) ok Γ ⊢ (Times t 1 t 2 ) ok x ∈ Γ Γ ⊢ t 1 ok Γ ∪ { x } ⊢ t 2 ok Γ ⊢ x ok Γ ⊢ (Let t 1 x .t 2 ) ok

  6. Scoping • Inference rules: Γ ⊢ (Num i) ok Γ ⊢ t 1 ok Γ ⊢ t 2 ok Γ ⊢ t 1 ok Γ ⊢ t 2 ok Γ ⊢ (Plus t 1 t 2 ) ok Γ ⊢ (Times t 1 t 2 ) ok x ∈ Γ Γ ⊢ t 1 ok Γ ∪ { x } ⊢ t 2 ok Γ ⊢ x ok Γ ⊢ (Let t 1 x .t 2 ) ok • Example: Let 5 (x.(Plus x x)) Let 5 (x.(Plus x y))

  7. Dynamic Semantics • What is dynamic semantics? - specifies the program execution process - may include side e ff ects and computed values - there are various kinds of dynamic semantics ‣ denotational ‣ operational ‣ axiomatic • Denotational Semantics: - Idea: syntactic expressions are mapped to mathematical objects, e.g., ‣ mapping to lambda-calculus ‣ fix-point semantics over complete partial orders (CPOs)

  8. Semantics • Axiomatic Semantics ★ Idea: statements over programs in the form of axioms describing logic program properties - Hoare’s calculus Hoare triple {P} s 1 {Q} {Q} s 2 {R} {P} prgrm {Q} {P} s 1 ;s 2 {R} {P[x:=E]} x:= E {P} P: precondition rule for assignment sequence of statements Q: postcondition - Dijkstra’s Weakest Precondition (WP) calculus wp (prgm, Q) = P wp (s 1 ;s 2 , R) = wp(s 1 ,wp(s 2 ,R)) wp (x:=E, R) = R[x:=E] sequence of statements rule for assignment What is the weakest precondition P such that after executing prgm, Q holds?

  9. Semantics • Operational Semantics - Idea: defines semantics in terms of an abstract machine - There are two main forms: ‣ small step semantics or structural operational semantics (SOS): step by step execution of a program ‣ big step, natural or evaluation semantics: specifies result of execution of complete programs/subprograms - we will be looking at both, small step as well as big step semantics

  10. Structural Operational Semantics Definition: Transition Systems A transition system specifies the step-by-step evaluation of a program and consists of ‣ a set of states S on an abstract computing device ‣ a set of initial states I ⊆ S ‣ a set of final state F ⊆ S , and ‣ a relation ↦ :: S × S describing the e ff ect of a single evaluation step on state s Definition An execution sequence s 0 , s 1 , ..., s n ‣ is maximal if there is no s n+1 such that s n ↦ s n+1 ‣ is complete if s n ∈ F

  11. Transition Systems Back to our arithmetic expression example • States: ‣ the set of all well-formed arithmetic expressions S = { e | ∃ Γ.Γ ⊢ e ok } • Initial States: ‣ the set of all closed, well formed arithmetic expressions: I = { e | {} ⊢ e ok } • Final States: ‣ values F = {( Num i ) | i Int } • Operations of the abstract machines:

  12. Evaluation Strategy • We need to fix an evaluation strategy • Example: addition (Plus (Num n) (Num m)) ↦ ( Num (n+m)) e 1 ↦ e 1 ’ (Plus e 1 e 2 ) ↦ ( Plus e 1 ’ e 2 ) e 2 ↦ e 2 ’ (Plus (Num n) e 2 ) ↦ ( Plus (Num n) e 2 ’) multiplication can be defined similarly

  13. Evaluation Strategy • Evaluating let -expressions let x = e 1 in e 2 Eager or strict evaluation: ‣ evaluate the right-hand side of binding e 1 to value v ‣ substitute the value v for the bound variable x , and ‣ evaluate the body e 2 [ x:=v ] Lazy evaluation ‣ substitute expression e 1 for the bound variable x , and ‣ evaluate the body e 2 [ x:=e 1 ]

  14. Evaluation Strategy ‣ 0 Eager or strict evaluation: ( Let(Num n) ( x.e 2 )) ↦ e 2 [x := Num n] e 1 ↦ e 1 ’ (Let e 1 ( x.e 2 )) ↦ ( Let e 1 ’ ( x.e 2 )) Lazy evaluation (Let e 1 ( x.e 2 )) ↦ e 2 [x := e 1 ]

  15. Reflexive, transitive closure of the transition relation • s ↦ * s ’ : state s evaluates to state s’ in zero or more steps • ↦ * is the reflexive, transitive closure of ↦ : s ↦ * s s 1 ↦ s 2 s 2 ↦ * s 3 s 1 ↦ * s 3 • complete evaluation ‣ s ↦ ! s’ : s fully evaluates to state s’ in zero or more steps ‣ formally, if s ↦ * s ’, and s’ ∈ F , then s ↦ ! s ’

  16. Transition System • Stuck States: - every complete execution sequence is maximal, but - not every maximal sequence is complete. Why? ‣ there may be states for which no follow up state exists, but which are not in F ‣ we call such a state a stuck state ‣ stuck states correspond to (non-handled) run-time errors in a program

  17. Evaluation or Big Step Semantics • Evaluation relation also called big step or natural semantics. Consists of ‣ a set of evaluable expressions E ‣ a set of values V (often, but not necessarily, a subset of E ), and ‣ an “evaluates to” relation ⇓ ∈ E × V

  18. Evaluation Semantics • Arithmetic expression example - Set of evaluable expressions: E = { e | {} ⊢ e ok } - Set of values: V = { Num i | i Int } (Num n) ⇓ ( Num n) e 1 ⇓ ( Num n 1 ) e 2 ⇓ ( Num n 2 ) (Plus e 1 e 2 ) ⇓ ( Num(n 1 +n 2 ) ) e 1 ⇓ Num n 1 e 2 ⇓ Num n 2 ( Times e 1 e 2 ) ⇓ ( Num(n 1 *n 2 ) ) e 1 ⇓ ( Num n 1 ) e 2 [ x := Num n 1 ] ⇓ ( Num n 2 ) e 2 [ x := e 1 ] ⇓ ( Num n 2 ) ( Let e 1 ( x.e 2 )) ⇓ ( Num n 2 ) ( Let e 1 ( x.e 2 )) ⇓ ( Num n 2 )

Recommend


More recommend