[Syntax] [Big Steps] [Small Steps] Operational Semantics Part I Jim Royer CIS 352 February 12, 2019 1 / 22
[Syntax] [Big Steps] [Small Steps] References Andrew Pitts’ Lecture Notes on Semantics of Programming Languages http://www.inf.ed.ac.uk/teaching/courses/lsi/sempl.pdf . We’ll be following the Pitts’ notes for a while and mostly using his notation. Matthew Hennessy’s Semantics of programming languages: https: //www.scss.tcd.ie/Matthew.Hennessy/splexternal2015/LectureNotes/Notes14%20copy.pdf is very readable and very good. There are many of other good references in Hennessy’s reading list: https://www.scss.tcd.ie/Matthew.Hennessy/splexternal2015/reading.php 2 / 22
[Syntax] [Big Steps] [Small Steps] Aexp , A little language for arithmetic expressions Syntactic categories n ∈ Num Numerals a ∈ Aexp Arithmetic expressions Grammar Conventions a :: = n Metavariables: n , a , b , w , x , etc. | ( a 1 + a 2 ) We write � 35 � for the numeral 35. | ( a 1 − a 2 ) Examples | ( a 1 ∗ a 2 ) � 2 � n :: = . . . ( � 2 � + � 5 � ) ((( � 2 � + � 5 � ) ∗ � 13 � ) − � 9 � ) 3 / 22
[Syntax] [Big Steps] [Small Steps] Syntax Concrete syntax ≈ phonemes, characters, words, tokens — the raw stuff of language Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor inci- didunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, . . . Grammar ≈ collection of formation rules to organize parts into a whole. E.g., words into noun phrases, verb phrases, . . . , sentences key words, tokens, . . . into expressions, statements, . . . , programs Abstract syntax ≈ a structure (e.g., labeled tree or data structure) showing how a “phrase” breaks down into pieces according to a specific rule. 4 / 22
[Syntax] [Big Steps] [Small Steps] Aexp ’s abstract syntax Grammar In Haskell As a Parse Tree data AExp = Num Integer a :: = n - | Plus AExp AExp | ( a 1 + a 2 ) | Minus AExp AExp * 9 | Times AExp AExp | ( a 1 − a 2 ) ((( � 2 � + � 5 � ) ∗ � 13 � ) − � 9 � ) | ( a 1 ∗ a 2 ) + 13 Minus (Times (Plus (Num 2) (Num 5)) n :: = . . . 2 5 (Num 13)) (Num 9) 5 / 22
[Syntax] [Big Steps] [Small Steps] What do Aexp expression mean? Big-step rules Notes a :: = n | ( a 1 + a 2 ) | ( a 1 − a 2 ) | ( a 1 ∗ a 2 ) a ⇓ v ≡ expression a evaluates to value v . ⇓ is an evaluation relation. PLUS: a 1 ⇓ v 1 a 2 ⇓ v 2 ( v = v 1 + v 2 ) Upstairs assertions are called ( a 1 + a 2 ) ⇓ v premises. Downstairs assertions are called MINUS: a 1 ⇓ v 1 a 2 ⇓ v 2 ( v = v 1 − v 2 ) conclusions. ( a 1 − a 2 ) ⇓ v Parenthetical equations on the side MULT: a 1 ⇓ v 1 a 2 ⇓ v 2 are called side conditions. ( v = v 1 ∗ v 2 ) N : numerals → Z . ( a 1 ∗ a 2 ) ⇓ v I.e., N [[ � − 43 � ]] = − 43. The NUM BSS rule is an example of NUM: n ⇓ v ( N [[ n ]] = v ) an axiom. 6 / 22
[Syntax] [Big Steps] [Small Steps] Digression: Rules, 1 General Format for Rules · · · Name: premise 1 premise k ( side condition ) conclusion Example 1. Modus Ponens: p = ⇒ q p q Transitivity: x ≡ y y ≡ z x ≡ z PLUS: a 1 ⇓ v 1 a 2 ⇓ v 2 ( v = v 1 + v 2 ) ( a 1 + a 2 ) ⇓ v 7 / 22
[Syntax] [Big Steps] [Small Steps] Digression: Rules, 2 General Format for Rules · · · Name: premise 1 premise k ( side condition ) conclusion Definition 2. A rule with no premises is an axiom . Definition 3. A rule is sound if and only if the conclusion is true whenever the premises (and side-condition—if any) are true. Question So an axiom is sound when . . . ? 8 / 22
[Syntax] [Big Steps] [Small Steps] Digression: Rules, 3 General Format for Rules · · · Name: premise 1 premise k ( side condition ) conclusion Proofs from gluing together rule applications Num: � 2 � ⇓ 2 Num: � 5 � ⇓ 5 ( 2 + 5 = 7 ) Plus: Num: � 13 � ⇓ 13 ( � 2 � + � 5 � ) ⇓ 7 ( 7 ∗ 13 = 91 ) Times: (( � 2 � + � 5 � ) ∗ � 13 � ) ⇓ 91 9 / 22
[Syntax] [Big Steps] [Small Steps] Rules can also be the basis of a computation . . . (( � 2 � + � 5 � ) ∗ � 13 � ) ⇓ ?? 10 / 22
[Syntax] [Big Steps] [Small Steps] Rules can also be the basis of a computation . . . (( � 2 � + � 5 � ) ∗ � 13 � ) ⇓ ?? �� . . . ( � 2 � + � 5 � ) ⇓ ?? � 13 � ⇓ 13 (( � 2 � + � 5 � ) ∗ � 13 � ) ⇓ ?? 10 / 22
[Syntax] [Big Steps] [Small Steps] Rules can also be the basis of a computation . . . (( � 2 � + � 5 � ) ∗ � 13 � ) ⇓ ?? �� . . . ( � 2 � + � 5 � ) ⇓ ?? � 13 � ⇓ 13 (( � 2 � + � 5 � ) ∗ � 13 � ) ⇓ ?? �� � 2 � ⇓ 2 � 5 � ⇓ 5 ( � 2 � + � 5 � ) ⇓ ?? � 13 � ⇓ 13 (( � 2 � + � 5 � ) ∗ � 13 � ) ⇓ ?? 10 / 22
[Syntax] [Big Steps] [Small Steps] Rules can also be the basis of a computation . . . (( � 2 � + � 5 � ) ∗ � 13 � ) ⇓ ?? �� �� � 2 � ⇓ 2 � 5 � ⇓ 5 ( � 2 � + � 5 � ) ⇓ 7 � 13 � ⇓ 13 . . . (( � 2 � + � 5 � ) ∗ � 13 � ) ⇓ ?? ( � 2 � + � 5 � ) ⇓ ?? � 13 � ⇓ 13 (( � 2 � + � 5 � ) ∗ � 13 � ) ⇓ ?? �� � 2 � ⇓ 2 � 5 � ⇓ 5 ( � 2 � + � 5 � ) ⇓ ?? � 13 � ⇓ 13 (( � 2 � + � 5 � ) ∗ � 13 � ) ⇓ ?? 10 / 22
[Syntax] [Big Steps] [Small Steps] Rules can also be the basis of a computation . . . (( � 2 � + � 5 � ) ∗ � 13 � ) ⇓ ?? �� �� � 2 � ⇓ 2 � 5 � ⇓ 5 ( � 2 � + � 5 � ) ⇓ 7 � 13 � ⇓ 13 . . . (( � 2 � + � 5 � ) ∗ � 13 � ) ⇓ ?? ( � 2 � + � 5 � ) ⇓ ?? � 13 � ⇓ 13 (( � 2 � + � 5 � ) ∗ � 13 � ) ⇓ ?? �� �� � 2 � ⇓ 2 � 5 � ⇓ 5 ( � 2 � + � 5 � ) ⇓ 7 � 13 � ⇓ 13 � 2 � ⇓ 2 � 5 � ⇓ 5 (( � 2 � + � 5 � ) ∗ � 13 � ) ⇓ 91 ( � 2 � + � 5 � ) ⇓ ?? � 13 � ⇓ 13 (( � 2 � + � 5 � ) ∗ � 13 � ) ⇓ ?? 10 / 22
[Syntax] [Big Steps] [Small Steps] The big-step semantics in Haskell A Haskell version of the abstract syntax data Aexp = Num Integer | Add Aexp Aexp | Sub Aexp Aexp | Mult Aexp Aexp The big-step semantics as an evaluator function aBig (Add a1 a2) = (aBig a1) + (aBig a2) aBig (Sub a1 a2) = (aBig a1) - (aBig a2) aBig (Mult a1 a2) = (aBig a1) * (aBig a2) aBig (Num n) = n 11 / 22
[Syntax] [Big Steps] [Small Steps] Do these rules make sense? Theorem 4. Suppose e ∈ Aexp . Then there is a unique integer v such that e ⇓ v. Proof (by rule induction). C ASE : NUM . This is immediate. C ASE : PLUS . By IH, there are unique v 1 and v 2 such that a 1 ⇓ v 1 and a 2 ⇓ v 2 . By arithmetic, there is a unique v such that v = v 1 + v 2 . Hence, there is a unique v such that a 1 + a 2 ⇓ v . C ASES : MINUS and MULT . These follow mutatis mutandis . PLUS BSS : a 1 ⇓ v 1 a 2 ⇓ v 2 ( v = v 1 + v 2 ) NUM BSS : n ⇓ v ( N [[ n ]] = v ) . . . ( a 1 + a 2 ) ⇓ v 12 / 22
[Syntax] [Big Steps] [Small Steps] What do Aexp expression mean? Small-step rules a :: = n | ( a 1 + a 2 ) | ( a 1 − a 2 ) | ( a 1 ∗ a 2 ) | v Notes a 1 → a ′ 1 These are rewrite rules. PLUS-1 SSS : ( a 1 + a 2 ) → ( a ′ 1 + a 2 ) We now allow values in expressions. a → a ′ is a transition. a 2 → a ′ 2 PLUS-2 SSS : ( a 1 + a 2 ) → ( a 1 + a ′ a → a ′ ≡ expression a evaluates (or 2 ) rewrites) to a ′ in one-step. PLUS-3 SSS : ( v 1 + v 2 ) → v ( v = v 1 + v 2 ) v is a terminal expression. . The rules for − and ∗ follow the . . same pattern as the + -rules. NUM SSS : n → v ( N [[ n ]] = v ) 13 / 22
[Syntax] [Big Steps] [Small Steps] Class exercise Show: ((( � 3 � ∗ � 2 � ) + ( � 8 � − � 3 � )) ∗ ( � 5 � − � 2 � )) (( 6 + ( � 8 � − � 3 � )) ∗ ( � 5 � − � 2 � )) → ((( � 3 � ∗ � 2 � ) + 5 ) ∗ ( � 5 � − � 2 � )) ((( � 3 � ∗ � 2 � ) + ( � 8 � − � 3 � )) ∗ 3 ) 14 / 22
[Syntax] [Big Steps] [Small Steps] Some full small-step derivations of transitions MINUS 3 ( 8 − 3 ) → 5 The derivations show that the steps in PLUS 2 the transition sequence below are legal ( 6 + ( 8 − 3 )) → ( 6 + 5 ) MULT 1 (i.e., follow from the rules). (( 6 + ( 8 − 3 )) ∗ ( 5 − 2 )) → (( 6 + 5 ) ∗ ( 5 − 2 )) (( 6 + ( 8 − 3 )) ∗ ( 5 − 2 )) PLUS 3 ( 6 + 5 ) → 11 → MULT 1 (( 6 + 5 ) ∗ ( 5 − 2 )) → 11 ∗ ( 5 − 2 ) (( 6 + 5 ) ∗ ( 5 − 2 )) → 11 ∗ ( 5 − 2 ) MINUS 3 ( 5 − 2 ) → 3 MULT 2 → ( 11 ∗ ( 5 − 2 )) → 11 ∗ 3 11 ∗ 3 → MULT 3 33 ( 11 ∗ 3 ) → 33 15 / 22
[Syntax] [Big Steps] [Small Steps] There is a lattice of transitions (((3*2)+(8-3))*(5-2)) ((6+(8-3))*(5-2)) (((3*2)+5)*(5-2)) (((3*2)+(8-3))*3) ((6+5)*(5-2)) ((6+(8-3))*3) (((3*2)+5)*3) (11*(5-2)) ((6+5)*3) (11*3) 33 16 / 22
Recommend
More recommend