C LAUS B RABRAND S EMANTICS (Q1,’06) S EP 7, 2006 C LAUS B RABRAND © 2005-2006, University of Aarhus [ ] [

Slides:



Advertisements
Similar presentations
1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson.
Advertisements

 Computer Science 1MD3 Introduction to Programming Winter 2014.
ICE1341 Programming Languages Spring 2005 Lecture #6 Lecture #6 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
8. Introduction to Denotational Semantics. © O. Nierstrasz PS — Denotational Semantics 8.2 Roadmap Overview:  Syntax and Semantics  Semantics of Expressions.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
Program Representations. Representing programs Goals.
Semantics Q S EMANTICS (Q1,’07) Week 1 Jacob Andersen PhD student
CSE 425: Semantic Analysis Semantic Analysis Allows rigorous specification of a program’s meaning –Lets (parts of) programming languages be proven correct.
ISBN Chapter 3 Describing Syntax and Semantics.
1 Semantic Description of Programming languages. 2 Static versus Dynamic Semantics n Static Semantics represents legal forms of programs that cannot be.
CS 355 – Programming Languages
Comp 205: Comparative Programming Languages Semantics of Imperative Programming Languages denotational semantics operational semantics logical semantics.
Lecture 02 – Structural Operational Semantics (SOS) Eran Yahav 1.
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Computation Model Defining practical programming languages Carlos Varela RPI.
C LAUS B RABRAND S EMANTICS (Q1,’05) S EP 8, 2005 C LAUS B RABRAND © 2005, University of Aarhus [ ] [
9/27/2006Prof. Hilfinger, Lecture 141 Syntax-Directed Translation Lecture 14 (adapted from slides by R. Bodik)
1 Operational Semantics Mooly Sagiv Tel Aviv University Textbook: Semantics with Applications.
Representing programs Goals. Representing programs Primary goals –analysis is easy and effective just a few cases to handle directly link related things.
C LAUS B RABRAND S EMANTICS (Q1,’05) S EP 8, 2005 C LAUS B RABRAND © 2005, University of Aarhus [ ] [
1 Semantics Q S EMANTICS (Q1,’07) Week 2 Jacob Andersen PhD student
1 Semantics Q S EMANTICS (Q1,’07) Week 3 Jacob Andersen PhD student
Programming Language Semantics Mooly SagivEran Yahav Schrirber 317Open space html://
C LAUS B RABRAND © S EMANTICS (Q1,’06) A UG 31, 2006 C LAUS B RABRAND © 2005–2006, University of Aarhus [ ] [
C LAUS B RABRAND S EMANTICS (Q1,’06) S EP 14, 2006 C LAUS B RABRAND © , University of Aarhus [ ] [
1 Semantics Q S EMANTICS (Q1,’07) Week 4 Jacob Andersen PhD student
Semantics with Applications Mooly Sagiv Schrirber html:// Textbooks:Winskel The.
Operational Semantics Semantics with Applications Chapter 2 H. Nielson and F. Nielson
Describing Syntax and Semantics
C LAUS B RABRAND S EMANTICS (Q1,’06) S EP 21, 2006 C LAUS B RABRAND © , University of Aarhus [ ] [
C LAUS B RABRAND S EMANTICS (Q1,’05) S EP 22, 2005 C LAUS B RABRAND © 2005, University of Aarhus [ ] [
C LAUS B RABRAND © S EMANTICS (Q1,’05) S EP 1, 2005 C LAUS B RABRAND © 2005, University of Aarhus [ ] [
CSC 8310 Programming Languages Meeting 2 September 2/3, 2014.
Precision Going back to constant prop, in what cases would we lose precision?
CPSC 388 – Compiler Design and Construction Parsers – Context Free Grammars.
Program Analysis and Verification Spring 2015 Program Analysis and Verification Lecture 2: Operational Semantics I Roman Manevich Ben-Gurion University.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 2.
Chapter Twenty-ThreeModern Programming Languages1 Formal Semantics.
ISBN Chapter 3 Describing Semantics -Attribute Grammars -Dynamic Semantics.
CS 363 Comparative Programming Languages Semantics.
Formal Semantics Chapter Twenty-ThreeModern Programming Languages, 2nd ed.1.
© Kenneth C. Louden, Chapter 11 - Functional Programming, Part III: Theory Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
Program Analysis and Verification Spring 2014 Program Analysis and Verification Lecture 4: Axiomatic Semantics I Roman Manevich Ben-Gurion University.
3.2 Semantics. 2 Semantics Attribute Grammars The Meanings of Programs: Semantics Sebesta Chapter 3.
Chapter 3 Part II Describing Syntax and Semantics.
1 / 48 Formal a Language Theory and Describing Semantics Principles of Programming Languages 4.
Operational Semantics Mooly Sagiv Tel Aviv University Textbook: Semantics with Applications Chapter.
1 Programming Languages (CS 550) Lecture 2 Summary Mini Language Interpreter Jeremy R. Johnson.
Formal Semantics of Programming Languages 虞慧群 Topic 2: Operational Semantics.
Operational Semantics Mooly Sagiv Tel Aviv University Sunday Scrieber 8 Monday Schrieber.
Operational Semantics Mooly Sagiv Reference: Semantics with Applications Chapter 2 H. Nielson and F. Nielson
Operational Semantics Mooly Sagiv Reference: Semantics with Applications Chapter 2 H. Nielson and F. Nielson
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Program Analysis and Verification Noam Rinetzky Lecture 2: Operational Semantics 1 Slides credit: Tom Ball, Dawson Engler, Roman Manevich, Erik.
Operational Semantics of Scheme
Information Science and Engineering
CS510 Compiler Lecture 4.
Lecture 2 Introduction to Programming
Spring 2017 Program Analysis and Verification Operational Semantics
Mini Language Interpreter Programming Languages (CS 550)
The Metacircular Evaluator
Lecture 23 Pages : Separating Syntactic Analysis from Execution. We omit many details so you have to read the section in the book. The halting.
Programming Languages and Compilers (CS 421)
The Metacircular Evaluator
Programming Languages and Compilers (CS 421)
The Metacircular Evaluator (Continued)
6.001 SICP Further Variations on a Scheme
6.001 SICP Variations on a Scheme
Announcements Quiz 5 HW6 due October 23
Spring 2016 Program Analysis and Verification Operational Semantics
Presentation transcript:

C LAUS B RABRAND S EMANTICS (Q1,’06) S EP 7, 2006 C LAUS B RABRAND © , University of Aarhus [ ] [ ] S EMANTICS (Q1,’06) W EEK 2: ”STRUCTURAL OPERATIONAL SEMANTICS”

C LAUS B RABRAND [ 2 ] S EMANTICS (Q1,’06) S EP 7, 2006 Week 2 - Outline Syntax vs. Semantics Virtual Machine Semantics Structural Operational Semantics Expressions (Exp) Big-step vs. small-step semantics Side-effects Behavior and Equivalence, … Boolean Expressions (BExp) Lazy evaluation, … Commands (Com) …

C LAUS B RABRAND [ 3 ] S EMANTICS (Q1,’06) S EP 7, 2006 Active vs. Passive Learning: Correlation: Investment ~ Benefit Example: Studying ~ Exam result Active vs. Passive Learning Main Entry: os·mo·sis Pronunciation: äz-'mO-s&s, äs- Function: noun Etymology: New Latin, short for endosmosis 1 : movement of a solvent through a semipermeable membrane (as of a living cell) […] 2 : a process of absorption […] usually effortless often unconscious assimilation Active LearningPassive Learning aka. "Learning-by-Osmosis" vs.

C LAUS B RABRAND [ 4 ] S EMANTICS (Q1,’06) S EP 7, 2006 "Learning by Osmosis" It’s Amazing… Do Active Learning: Study = read + make exercises + reflect (for your own sake)! “Learn while you sleep!” “The Semantics Pillow” Only $19,95

C LAUS B RABRAND S EMANTICS (Q1,’06) S EP 7, 2006 W HY S EMANTICS ?!? Keywords: Why bother with semantics?!?

C LAUS B RABRAND [ 6 ] S EMANTICS (Q1,’06) S EP 7, 2006 Why bother learning Semantics?!? Why Semantics?!? Fully understand a prog. lang. Semantics- preserving prog. changes design new prog. lang.s rapidly learn new prog. lang. debugging programs cross- compilation automated optimization automated error detection write faster programs write "better" programs $$ $$$$ Pass this course

C LAUS B RABRAND S EMANTICS (Q1,’06) S EP 7, 2006 S YNTAX VS S EMANTICS Keywords: Structure vs. Meaning

C LAUS B RABRAND [ 8 ] S EMANTICS (Q1,’06) S EP 7, 2006 Syntax and Semantics Syntax: = Structure vs. Semantics: = Meaning "Eats shoots and leaves" "Colorless Green Ideas Sleep Furiously" -- N. Chomsky (1957) The syntactic structure affects our interpretation ; ; Has legal syntactic structure, but no meaning ( ),,

C LAUS B RABRAND [ 9 ] S EMANTICS (Q1,’06) S EP 7, 2006 Syntax vs. Semantics (cont'd) In natural languages (English, Danish,...): Syntax  Semantics In programming languages (C, Java, ML,...): Syntax  Semantics By design (for very good reasons...): "Time flies like an arrow " "Fruit flies like a banana " N V N P N V N But, if I replace two nouns (syntactic) replace noun

C LAUS B RABRAND [ 10 ] S EMANTICS (Q1,’06) S EP 7, 2006 Level of Abstraction Semantic specification(s): Appropriate level of abstraction:...so that we can use the semantics for something !!!  is_busy    is_busy    is_done  higher level of abstraction ..., transistor = 5V, transistor = 0V,...  ..., transistor = 5V, transistor = 5V,...   z z z  

C LAUS B RABRAND [ 11 ] S EMANTICS (Q1,’06) S EP 7, 2006 Glasses of Abstraction… It’s Amazing… See behind concrete details; Perceive only what is relevant and at the appropriate level of abstraction)! Only $12,95 “See only the relevant!” “ The Glasses of Abstraction ”

C LAUS B RABRAND S EMANTICS (Q1,’06) S EP 7, 2006 T HE L ANGUAGE “L” Keywords: Expressions, Boolean Expressions, Commands, Abstract Syntax, Concrete Syntax

C LAUS B RABRAND [ 13 ] S EMANTICS (Q1,’06) S EP 7, 2006 The Language ”L” Basic Syntactic Sets: Truthvalues: Set ranged over by: t, t’, t 0, … Numbers: Set ranged over by: m, n, … Variables: Set ranged over by: v, v’, … T = { tt, ff } N = { 0, 1, 2, …} VAR = { a, b, c, …, z } Meta-variables

C LAUS B RABRAND [ 14 ] S EMANTICS (Q1,’06) S EP 7, 2006 Alice in Wonderland Different levels of abstraction: Things ‘A Sitting on a Gate’ Names of things ‘The Aged Aged Man.’ Things are called something ‘Ways and Means’ Names are called something ‘Haddocks’ Eyes.’ "[...] The name of the song is called ‘Haddocks’ Eyes.’ ” “Oh, that’s the name of the song, is it?” Alice said, trying to feel interested. “No, you don’t understand,” the Kinght said, looking a little vexed. “That’s what the name is called. The name really is ‘The Aged Aged Man.’ ” “Then I ought to have said, ‘That’s what the song is called’?” Alice corrected herself. “No, you oughtn’t: that’s another thing. The song is called ‘Ways and Means’ : but that’s only what it’s called, you know!” “Well, what is the song, then?” said Alice, who was by this time completely bewildered. “I was coming to that,” the Knight said. “The song really is ‘A Sitting on a Gate’ : and the tune’s my own invention.”

C LAUS B RABRAND [ 15 ] S EMANTICS (Q1,’06) S EP 7, 2006 The Language ”L” Derived Syntactic Sets: Arithmetic Expressions ( e  Exp): Boolean Expressions ( b  BExp): Commands ( c  Com): e ::= n | v | e + e’ | e – e’ | e  e’ b ::= t | e = e’ | b or b’ | ~ b c ::= nil | v := e | c ; c’ | if b then c else c’ | while b do c

C LAUS B RABRAND S EMANTICS (Q1,’06) S EP 7, 2006 V IRTUAL M ACHINE S EMANTICS Keywords: The “SMC” Machine, Virtual Machine Semantics, Machine Code Semantics 'The world before 1981'

C LAUS B RABRAND [ 17 ] S EMANTICS (Q1,’06) S EP 7, 2006 Value Stack: Set ranged over by: S Memories: Functions ranged over by: M Control Stack: Set ranged over by: C SMC Machine (Trans. Sys. Semantics): Configurations: Virtual Machines ( SMC Expressions )  =  Value Stack  Memories  Control Stack  ( T  N ... )* VAR  N ( Exp  { +, –,  } ... )* Initially commands/expressions, and later bits of commands/expr's For accumulating partial results and other pieces needed

C LAUS B RABRAND [ 18 ] S EMANTICS (Q1,’06) S EP 7, 2006 Configurations: Notation:, M  VAR  N i.e., “Memory Update” More common syntax for this: Notation: Memory Update  =     ( T  N ... )* VAR  N ( Exp  { +, –,  } ...)* m, if v = v’ M’(v’) = M(v’), otherwise M[m/v] M[m/v] = M’ where M[m  v]

C LAUS B RABRAND [ 19 ] S EMANTICS (Q1,’06) S EP 7, 2006 Virtual Machine Transitions Configurations: Transitions: Defined "by case" according to top of control stack: [case n ]:  [case v ]:  [case e  e’ ]:  [case  ]:  …where n = m  m’ Syntactic ‘+’ Semantic ‘+’  =     ( T  N ... )* VAR  N ( Exp  { +, –,  } ...)*

C LAUS B RABRAND [ 20 ] S EMANTICS (Q1,’06) S EP 7, 2006 Virtual Machine: Example Given program: and memory: (((x + 1) – y)  7)  M = [x=9,y=4]         

C LAUS B RABRAND [ 21 ] S EMANTICS (Q1,’06) S EP 7, 2006 VM Semantics: Major Drawbacks! Advantage: Easy to implement (and efficient) Drawbacks: Non-intuitive  Too concrete (e.g., stack for computation fragments)  Indirect semantics (not syntax directed)  Computational step?  “High-level language understood in terms of low-level machine code” “Many other machine along these lines […]. They all have a tendency to pull the syntax into pieces or at any rate to wander around the syntax creating various complex symbolic structures which do not seem particularly forced by the demands of the language itself” - Gordon Plotkin, ‘81

C LAUS B RABRAND S EMANTICS (Q1,’06) S EP 7, 2006 A RITHMETIC E XPRESSIONS Keywords: structural operational semantics (sos) big-step & small-step semantics, side-effects

C LAUS B RABRAND [ 23 ] S EMANTICS (Q1,’06) S EP 7, 2006 Given program: and memory: VM: Processing of Additions           ((1 + (2 + 3)) + 4)M rearrange addition 1 ! addition 2 ! rearrange addition 3 ! rearrange 12 3 Note: Only three transitions are of real interest as “system events”

C LAUS B RABRAND [ 24 ] S EMANTICS (Q1,’06) S EP 7, 2006 Ideally we would like: …i.e., the following transition sequence:  Ideal Processing of Additions ((1 + (2 + 3)) + 4)((1 + 5) + 4) explanation ((1 + 5) + 4) explanation  (6 + 4) explanation  10  ((1 + (2 + 3)) + 4) explanation ((1 + 5) + 4) explanation  (6 + 4) explanation  10 (aka. derivation sequence) (aka. reduction sequence)

C LAUS B RABRAND [ 25 ] S EMANTICS (Q1,’06) S EP 7, 2006 Informal vs. Formal Specification Informally: “One evaluates from left to right...” Description (pseudo-formally): CONSTANTS: Any constant, n, is already evaluated (with itself as value) SUMS: Step 1. Evaluate to obtain result ; Step 2. Evaluate to obtain result ; Step 3. Add and to obtain final result. n e 0 + e 1 e0e0 n0n0 e1e1 n1n1 n0n0 n1n1 m

C LAUS B RABRAND [ 26 ] S EMANTICS (Q1,’06) S EP 7, 2006 Inference System Semantics Inference System Semantics: Abbreviate as Meaning: “ e terminates and evaluates to v” e  ve  v ‘  L ’  Exp  N ( e,v)  ‘  L ’ Q: Did I just solve the halting problem here?!? A: No; nobody can decide “ e  v ” automatically! A: Actually, for Exp termination is decidable…

C LAUS B RABRAND [ 27 ] S EMANTICS (Q1,’06) S EP 7, 2006 Inference System Sem. (cont’d) Inference System Semantics: e 0 + e 1  m m = n 0 + n 1 e0  n0e0  n0 e1  n1e1  n1 [ SUM ] [ CON ] n  n ‘  L ’  Exp  N Syntactic ‘ + ’ Semantic ‘+’ Live exercise: explain difference between syntactic and semantic entities above...

C LAUS B RABRAND [ 28 ] S EMANTICS (Q1,’06) S EP 7, 2006 Example Revisited (big-step) Inference Tree: ((1 + (2 + 3)) + 4)  10 (1 + (2 + 3))  6 4  4 1  1 (2 + 3)  5 2  2 3  3 e 0 + e 1  m m = n 0 + n 1 e0  n0e0  n0 e1  n1e1  n1 [ SUM ] [ CON ] n  n [ SUM ] [ CON ] [ SUM ] [ CON ] “big-step semantics” “small-step semantics” vs.

C LAUS B RABRAND [ 29 ] S EMANTICS (Q1,’06) S EP 7, 2006 Small-step Description Description: CONSTANTS: Any constant, n, is already evaluated (with itself as value) SUMS: Step 1. If is not a constant, evaluate it (one step) to obtain result ; Step 2. If is a constant, but is not, evaluate it (one step) to obtain result ; Step 3. If and are both constants, add them to obtain result, say. n e 0 + e 1 e0e0 e0’e0’ e0e0 e1e1 e1’e1’ e0e0 e1e1 m

C LAUS B RABRAND [ 30 ] S EMANTICS (Q1,’06) S EP 7, 2006 Small-step Formalization Transition System Semantics: Configurations: Final Configurations: Transition Relation: Abbreviate as Meaning: “ e evaluates to e’ in one step”  L := Exp T L := N  Exp  L  Exp  Exp e  e’ ( e, e’ )  ‘  L ’

C LAUS B RABRAND [ 31 ] S EMANTICS (Q1,’06) S EP 7, 2006 Small-step Semantics Transition System Semantics:  L := Exp T L := N  Exp  L  Exp  Exp [ SUM 1 ] e 0 + e 1  e 0 ’ + e 1 e 0  e 0 ’ [ SUM 2 ] n 0 + e 1  n 0 + e 1 ’ e 1  e 1 ’ [ SUM 3 ] n 0 + n 1  m m = n 0 + n 1 We call this a STRUCTURAL OPERATIONAL SEMANTICS

C LAUS B RABRAND [ 32 ] S EMANTICS (Q1,’06) S EP 7, 2006 Example Revisited (small-step) Transition sequence (with explanation): ((1 + (2 + 3)) + 4)  ((1 + 5) + 4) (1 + (2 + 3))  (1 + 5) (2 + 3)  5 [ SUM 1 ] [ SUM 2 ] [ SUM 3 ] “order of discovery” find predicatesmake conclusions

C LAUS B RABRAND [ 33 ] S EMANTICS (Q1,’06) S EP 7, 2006 Transition Sequence Information A transition sequence …specifies two things: 1. A “sequence of steps” themselves (here additions) 2. …and reasons why they should be performed e  e’  e’’  …  m  ((1 + (2 + 3)) + 4) explanation ((1 + 5) + 4)

C LAUS B RABRAND [ 34 ] S EMANTICS (Q1,’06) S EP 7, 2006 Adding Variables Language L’: Arithmetic Expressions ( e  Exp) Structural Operational Semantics: Configurations: Final Configurations: Transition Relation… e ::= n | v | e 0 + e 1  L’ := Exp  Store T L’ :=  L’ Note the change in terminology: Store <--- Memory  <--- M The term Store is more generally accepted (as an abstraction of a computer’s physical memory) Store = Var  N (= Memory) where A configuration now looks like:   L’

C LAUS B RABRAND [ 35 ] S EMANTICS (Q1,’06) S EP 7, 2006 Small-step Semantics w/ Stores Structural Operational Semantics (w/ Stores): [ SUM 1 ]  [ SUM 2 ] [ SUM 3 ]  m = n 0 + n 1  [ VAR ] Store = Var  N m =  (v)

C LAUS B RABRAND [ 36 ] S EMANTICS (Q1,’06) S EP 7, 2006 In fact: we have no Side-Effects! No side-effects!  [ SUM 1 ]  [ SUM 2 ] [ SUM 3 ]  m = n 0 + n 1  [ VAR ] Store = Var  N m =  (v)      e,  :  =>  =  ’ Easily proved by structural induction [later…]

C LAUS B RABRAND [ 37 ] S EMANTICS (Q1,’06) S EP 7, 2006 Explicit Absence of Side-effects SOS Semantics: [ SUM 1 ]  | _ e 0 + e 1  e 0 ’ + e 1  | _ e 0  e 0 ’ [ SUM 2 ] [ SUM 3 ]  | _ n 0 + n 1  m m = n 0 + n 1  | _ n 0 + e 1  n 0 + e 1 ’  | _ e 1  e 1 ’  | _ v  m [ VAR ] Store = Var  N m =  (v)  | _ e  e’  Note: Absence of side- effects is now explicit for

C LAUS B RABRAND [ 38 ] S EMANTICS (Q1,’06) S EP 7, 2006 Explicit Absence of Side-effects Terminology (more common): [ SUM 1 ]  | _ e 0 + e 1  e 0 ’ + e 1  | _ e 0  e 0 ’ [ SUM 2 ] [ SUM 3 ]  | _ n 0 + n 1  m m = n 0 + n 1  | _ n 0 + e 1  n 0 + e 1 ’  | _ e 1  e 1 ’  | _ v  m [ VAR ] Env = Var  N (= Store) m =  (v) Note the change in terminology: Env <--- Store  <--- 

C LAUS B RABRAND [ 39 ] S EMANTICS (Q1,’06) S EP 7, 2006 Exp: Behavior and Equivalence Definitions: DEF: "Behavior" ( eval ): Note: eval is only a partial function (e.g., if we have division; div-by-zero) DEF: "Program equivalence" ('  '): Examples: eval(e,  ) = m def  E * e  e’ def  : eval(e,  ) = eval(e’,  ) 1+1  2x+y  y+xz+z  2*z Live Exercise: Does "  " depend on the semantics (if so, how/where)?

C LAUS B RABRAND S EMANTICS (Q1,’06) S EP 7, 2006 B OOLEAN E XPRESSIONS Keywords: eager evaluation, lazy evaluation, parallel evaluation

C LAUS B RABRAND [ 41 ] S EMANTICS (Q1,’06) S EP 7, 2006 Boolean Expressions BExp Language B: Boolean Expressions ( b  BExp): Structural Operational Semantics: Configurations: where Final Configurations: Transition Relation… short-hand for b ::= t | e = e’ | b or b’ | ~ b  B := BExp  Env T B :=  B Env = Var  N  | _ b  B b’ We could also have used a Store (we actually have to if BExp had had side-effects) ((b,  ),(b’,  ))   B

C LAUS B RABRAND [ 42 ] S EMANTICS (Q1,’06) S EP 7, 2006 Boolean Expressions BExp ( cont’d ) Structural Operational Semantics: Note that: The (this) boolean expression transition system...: …uses the arithmetic expression transition system…:...and does it transitively (  E *) in “one step”!  | _ e 0 = e 1  B t [ EQ ] B  | _ e 0  E * n 0  | _ e 1  E * n 1   B, T B,  B    E, T E,  E  t = tt, n 0 = n 1 ff, n 0  n 1 See [Plotkin, p ] for alternative semantics (with a smaller step size)

C LAUS B RABRAND [ 43 ] S EMANTICS (Q1,’06) S EP 7, 2006 Disjunction: “or” SOS for Disjunction (“or”): What about the boolean expression:...?!?  | _ b 0 or b 1  B b 0 ’ or b 1  | _ b 0  B b 0 ’ [ OR 1 ] B  | _ t 0 or b 1  B t 0 or b 1 ’  | _ b 1  B b 1 ’ [ OR 2 ] B  | _ t 0 or t 1  B t [ OR 3 ] B t = t 0  t 1 "tt or b" Live Exercise: syntax vs semantic operators?

C LAUS B RABRAND [ 44 ] S EMANTICS (Q1,’06) S EP 7, 2006 Disjunction: Lazy Evaluation Lazy Semantics for Disjunction (“or”):  | _ b 0 or b 1  L b 0 ’ or b 1  | _ b 0  L b 0 ’ [ OR 1 ] L [ OR 2 ] L  | _ ff or b 1  L b 1 [ OR 3 ] L  b: tt or b  tt  | _ tt or b 1  L tt...exploiting:  b: ff or b  b...exploiting:

C LAUS B RABRAND [ 45 ] S EMANTICS (Q1,’06) S EP 7, 2006 Eager vs. Lazy Semantics for “or” Eager Semantics: ‘  B ’ Lazy Semantics: ‘  L ’ Relationship : ? Stuck configurations? …e.g. div-by-zero? Assignment? Non-termination? b := tt or (3 = 1-2)  b,  :  | _ b  B * t  | _ b  L * t b := tt or (1 = 2/0) b := tt or (x := tt) “side-effects” b := tt or loop()

C LAUS B RABRAND [ 46 ] S EMANTICS (Q1,’06) S EP 7, 2006 For the language, BExp, we have that: Whereas: Eager vs. Lazy Semantics (cont’d)  b,  :  B * ==>  L *  b,  :  B *  L * Can be proved by structural induction [later…] Easily disproved by a counterexample (e.g. ) Relationship : ? Stuck configurations? …e.g. div-by-zero? Assignment? Non-termination? b := tt or (3 = 1-2)  b,  :  | _ b  B * t  | _ b  L * t b := tt or (1 = 2/0) b := tt or (x := tt) “side-effects” b := tt or loop() b := tt or (3 = 1-2)

C LAUS B RABRAND [ 47 ] S EMANTICS (Q1,’06) S EP 7, 2006 Parallel Evaluation of Disjunction Live exercise: Evaluate BExps non-deterministically (via. interleaving or in parallel) left / right operands to or: [Think 3 mins; then interactively on the blackboard] ‘P’‘P’ b := tt or (3 = 1-2) b := tt or (1 = 2/0) b := tt or (x := tt) “side-effects” b := tt or loop() Relationship : ? Stuck configurations? …e.g. div-by-zero? Assignment? Non-termination?  b,  :  | _ b  B * t  | _ b  P * t

C LAUS B RABRAND [ 48 ] S EMANTICS (Q1,’06) S EP 7, 2006 Negation: “ ~b ” Boolean Expressions ( b  BExp): Live exercise: [Think 3 mins; then interactively on the blackboard] b ::= t | e = e’ | b or b’ | ~ b

C LAUS B RABRAND [ 49 ] S EMANTICS (Q1,’06) S EP 7, 2006 BExp: Behavior and Equivalence Definitions: DEF: "Boolean Expression Behavior": Note: eval is only a partial function (e.g., if we have division; div-by-zero) DEF: "Boolean Expression Equivalence": Examples: eval(b,  ) = t def  B * t b  b’ def  : eval(b,  ) = eval(b’,  ) b  ~~b1 = 2  ffx = x  tt

C LAUS B RABRAND S EMANTICS (Q1,’06) S EP 7, 2006 C OMMANDS Keywords: Assignments, Conditionals, Loops, Control flow

C LAUS B RABRAND [ 51 ] S EMANTICS (Q1,’06) S EP 7, 2006 SOS of Commands Commands ( c  Com): Structural Operational Semantics: Configurations: where Final Configurations: (?) Transition Relation… for c ::= nil | v := e | c ; c’ | if b then c else c’ | while b do c  C := (Com  Store) T C :=  C Store = Var  N  C Here we need a Store (since we have to model side-effects) ((c,  ),(c’,  ’))   C Problem: how do we end computation?!?

C LAUS B RABRAND [ 52 ] S EMANTICS (Q1,’06) S EP 7, 2006 SOS of Commands (cont’d) Commands ( c  Com): Structural Operational Semantics: Configurations: where Final Configurations: (!) Transition Relation… for c ::= nil | v := e | c ; c’ | if b then c else c’ | while b do c  C := (Com  Store)  Store T C := Store Store = Var  N  C ((c,  ),(c’,  ’))   C  C  ’ ((c,  ),  ’)   C (!)(!) for and Here we need a Store (since we have to model side-effects)

C LAUS B RABRAND [ 53 ] S EMANTICS (Q1,’06) S EP 7, 2006 Nil and Assignment (SOS) SOS for nil : SOS for assignment: v := e  C  ’  A * [ ASS ] C  ’ =  [m/v]  C  [ NIL ] C

C LAUS B RABRAND [ 54 ] S EMANTICS (Q1,’06) S EP 7, 2006 Sequential Composition (SOS) SOS for Sequential Composition: Diagramatically: “(Control) flow-charts” c 0 ; c 1  C [ SEQ 1 ] C  C  C  ’ [ SEQ 2 ] C c c’

C LAUS B RABRAND [ 55 ] S EMANTICS (Q1,’06) S EP 7, 2006 If-then-else (SOS) SOS for “ if-then-else ”: Diagramatically: “(Control) flow-charts” if b then c else c’  C  B * [ IF 1 ] C  C  B * [ IF 2 ] C c c’ b tt ff

C LAUS B RABRAND [ 56 ] S EMANTICS (Q1,’06) S EP 7, 2006 While-do (SOS) SOS for “ while-do ”: Diagramatically: “(Control) flow-charts” while b do c  C  B * [ WH 1 ] C  C   B * [ WH 2 ] C tt ff c b

C LAUS B RABRAND [ 57 ] S EMANTICS (Q1,’06) S EP 7, 2006 Com: Behavior and Equivalence Definitions: DEF: "Command Behavior": Note: exec is only a partial function (e.g. non-termination, div-by-zero, …) DEF: "Command Equivalence": Examples: exec(c,  ) =  ’ def  C *  ’ c  c’ def  : exec(c,  ) = exec(c’,  ) if b then c else c’  if ~b then c’ else c while ff do c  nil

C LAUS B RABRAND [ 58 ] S EMANTICS (Q1,’06) S EP 7, 2006 "Three minutes paper" Please spend three minutes writing down the most important things that you have learned today (now). After 1 day After 1 week After 3 weeks After 2 weeks Immediately

C LAUS B RABRAND [ 59 ] S EMANTICS (Q1,’06) S EP 7, 2006

C LAUS B RABRAND S EMANTICS (Q1,’06) S EP 7, 2006 Next week: big-step vs. small-step, errors, type checking Any Questions?