operational semantics this course why semantics y 1
play

Operational Semantics This course Why semantics? y := 1; precise - PowerPoint PPT Presentation

Operational Semantics This course Why semantics? y := 1; precise specification of software and while ( x = 1) do ( y := x y ; x := x 1) hardware First we assign 1 to y , then we test whether facilitate reasoning about systems:


  1. Operational Semantics This course Why semantics? y := 1; • precise specification of software and while ¬ ( x = 1) do ( y := x ∗ y ; x := x − 1) hardware First we assign 1 to y , then we test whether • facilitate reasoning about systems: x is 1 or not. If it is then we stop and testing may reveal errors but not their otherwise we update y to be the product of absense x and the previous value of y and then we • form the basis for prototype imple- decrement x by one. Now we test whether mentations, e.g. interpreters and com- the new value of x is 1 or not · · · pilers Two kinds of operational semantics: Why functional programming? • Natural Semantics • based on mathematical notation, not on the von Neuman architecture • Structural Operational Semantics • excelent for prototype definitions I.1 I.2

  2. Denotational Semantics Axiomatic Semantics y := 1; while ¬ ( x = 1) do ( y := x ∗ y ; x := x − 1) y := 1; The program computes a partial function while ¬ ( x = 1) do ( y := x ∗ y ; x := x − 1) from states to states: the final state will If x = n holds before the program is exe- be equal to the initial state except that the cuted then y = n ! will hold when the exe- value of x will be 1 and the value of y will cution terminates (if it terminates) be equal to the factorial of the value of x in the initial state. Two kinds of axiomatic semantics: • Partial Correctness Two kinds of denotational semantics: • Total Correctness • Direct Style Semantics • Continuation Style Semantics I.3 I.4

  3. Which approach? Selection criteria • constructs of the language Programming Language – imperative ↓ – functional – concurrent/parallel Semantics – object oriented • natural semantics – non-deterministic • structural operational semantics – · · · • what is the semantics used for • direct style denotational semantics – understanding the language • continuation style denotational se- mantics – verification of programs – prototyping • partial correctness axiomatic seman- tics – compiler construction – program analysis • total correctness axiomatic semantics – · · · I.5 I.6

  4. Approach While language Theoretical Development S ::= x := a | skip | S 1 ; S 2 concrete LL(1)/LALR(1) grammar | if b then S 1 else S 2 syntax concrete syntax trees | while b do S | repeat S until b ↓ struct. op. natural abstract syntactic categories semantics semantics syntax abstract syntax trees ↓ direct style cont. style den. sem. den. sem. semantics semantic categories semantic definitions partial total correctness correctness I.7 I.8

  5. Miranda Syntactic Categories for While language • numerals n ∈ Num Programming languages • variables • imperative (procedural) x ∈ Var • applicative (functional) • arithmetic expressions a ∈ Aexp – lazy languages a ::= n | x | a 1 + a 2 Miranda | a 1 ∗ a 2 | a 1 − a 2 – eager languages Standard ML • booleans expressions Lisp?, Scheme? b ∈ Bexp b ::= true | false | a 1 = a 2 • declarative (logical) | a 1 ≤ a 2 | ¬ b | b 1 ∧ b 2 • object-oriented • statements • concurrent / parallel S ∈ Stm S ::= x := a | skip | S 1 ; S 2 • · · · | if b then S 1 else S 2 | while b do S I.9 II.1

  6. Semantic Categories Meanings of the syntactic categories Natural numbers N = { 0 , 1 , 2 , · · ·} Numerals N : Num → N Truth values T = { tt, ff } Variables s ∈ State = Var → N States Arithmetic expressions • State = Var → N • State ′ = (Var × N) ∗ A : Aexp → (State → N) • State ′′ = Var ∗ × N ∗ Boolean expressions Lookup in a state: s x B : Bexp → (State → T) Update a state: s ′ = s [ y �→ v ] Statements S : Stm → (State ֒ → State)  s x if x � = y  s ′ x = if x = y v  II.2 II.3

  7. A : Aexp → State → N B : Bexp → State → T B [ true ] s = tt B [ false ] s = ff  tt if A [ a 1 ] s = A [ a 2 ] s   B [ a 1 = a 2 ] s = ff if A [ a 1 ] s � = A [ a 2 ] s   A [ n ] s N [ n ] =  tt if A [ a 1 ] s ≤ A [ a 2 ] s A [ x ] s =  s x  B [ a 1 ≤ a 2 ] s = ff if A [ a 1 ] s �≤ A [ a 2 ] s   A [ a 1 + a 2 ] s A [ a 1 ] s + A [ a 2 ] s =  tt if B [ b ] s = ff A [ a 1 ∗ a 2 ] s A [ a 1 ] s ∗ A [ a 2 ] s =   B [ ¬ b ] s = if B [ b ] s = tt ff  A [ a 1 − a 2 ] s A [ a 1 ] s − A [ a 2 ] s =   tt if B [ b 1 ] s = tt     and B [ b 2 ] s = tt     B [ b 1 ∧ b 2 ] s = if B [ b 1 ] s = ff ff       or B [ b 2 ] s = ff   II.4 II.5

  8. Compositional Definitions Statements Syntactic Category S ::= x := a | skip | S 1 ; S 2 • The syntactic categories are specified | if b then S 1 else S 2 by an abstract syntax giving a unique | while b do S decomposition of each element into its constituents. Meaning of the syntactic category: • The semantics is defined by composi- S : Stm → (State ֒ → State) tional definitions of functions. Two operational semantics Since the decomposition of the ele- • Natural Semantics ments is unique this means that the semantics is well-defined. • Structural Operational Semantics specified by transition systems II.6 II.7

  9. Transition System Natural semantics Idea: describe how the overall result of the computation is obtained Transition system: (Γ , T, → ) • Γ = { ( S, s ) | S ∈ While, s ∈ State } (Γ , T, > ) ∪ State • Γ : a set of configurations • T = State • T : a set of terminal configurations • → ⊆ { ( S, s ) | S ∈ While, s ∈ State } T ⊆ Γ × State • > : a transition relation Typical transition: > ⊆ Γ × Γ ( S, s ) → s ′ where S is the program s is the initial state s ′ is the final state II.8 II.9

  10. Natural Semantics ( x := a, s ) → s [ x �→ A [ a ] s ] ( skip , s ) → s ( S 1 , s ) → s ′ , ( S 2 , s ′ ) → s ′′ ( S 1 ; S 2 , s ) → s ′′ ( S 1 , s ) → s ′ ( if b then S 1 else S 2 , s ) → s ′ if B [ b ] s = tt ( S 2 , s ) → s ′ ( if b then S 1 else S 2 , s ) → s ′ if B [ b ] s = ff ( S, s ) → s ′ , ( while b do S, s ′ ) → s ′′ ( while b do S, s ) → s ′′ if B [ b ] s = tt ( while b do S, s ) → s if B [ b ] s = ff II.10

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend