david evans cs.virginia/~evans
DESCRIPTION
Lecture 13: Operational Semantics. “Then you should say what you mean,” the March Hare went on. “I do,” Alice hastily replied; “at least – at least I mean what I say – that’s the same thing, you know.” - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: David Evans cs.virginia/~evans](https://reader035.vdocuments.us/reader035/viewer/2022062316/56815677550346895dc42bc4/html5/thumbnails/1.jpg)
David Evanshttp://www.cs.virginia.edu/~evans
CS655: Programming LanguagesUniversity of VirginiaComputer Science
Lecture 13: Operational Semantics
“Then you should say what you mean,” the March Hare went on.
“I do,” Alice hastily replied; “at least – at least I mean what I say – that’s the same thing, you know.”
“Not the same thing a bit!” said the Hatter. “Why, you might as well say that ‘I see what I eat’ is the same thing as ‘I eat what I see’!"
Lewis Carrol, Alice in Wonderland
“When I use a word,” Humpty Dumpty said, in a rather scornful tone, “it means just what I choose it to mean – neither more nor less.”
Lewis Carroll, Through the Looking Glass
![Page 2: David Evans cs.virginia/~evans](https://reader035.vdocuments.us/reader035/viewer/2022062316/56815677550346895dc42bc4/html5/thumbnails/2.jpg)
1 March 2001 CS 655: Lecture13 2
Menu
• Quiz Results• Intro to Formal Semantics• Operational Semantics
![Page 3: David Evans cs.virginia/~evans](https://reader035.vdocuments.us/reader035/viewer/2022062316/56815677550346895dc42bc4/html5/thumbnails/3.jpg)
1 March 2001 CS 655: Lecture13 3
Quiz Results• Preferences
– Mostly formal techniques x– Balanced xx– Mostly language design xxxxx– Just language design xx
• Mock Trial– Heard about Yes 6 No 3
Yes, very 1 Yes 2
Maybe 1.5No, research 0.5No, sleeping 1
• Speed: way too fast (0), too fast (6), wrote in just right/both (3), too slow (1)
![Page 4: David Evans cs.virginia/~evans](https://reader035.vdocuments.us/reader035/viewer/2022062316/56815677550346895dc42bc4/html5/thumbnails/4.jpg)
1 March 2001 CS 655: Lecture13 4
Other Comments“disappointed to hear that there were no more
programming assignments...I’d be interested in playing with and analyzing languages like Smalltalk, CLU, and APL...”
“though I’ve found a good deal of the material interesting, I don’t necessarily feel entirely prepared to write a Why X is not my favorite Programming Language”
“I’m tired of Scheme...mostly because the MIT scheme interpreter is poorly written and doesn’t allow you to easily perform editing operations...”
Run M-x scheme in emacs
![Page 5: David Evans cs.virginia/~evans](https://reader035.vdocuments.us/reader035/viewer/2022062316/56815677550346895dc42bc4/html5/thumbnails/5.jpg)
1 March 2001 CS 655: Lecture13 5
Other Comments“complaint is that we’re doing homework on old stuff that is no longer focused on in class, making it difficult for me to devote my energies to the new stuff.”“hasn’t taken the approach I would’ve expected – such as covering PL concepts (types, binding, etc.) in the context of a range of languages”“hope we can get lectures before class, so that we can have some preparation before class, ..., and take some notes beside the lectures.”“there are too many readings. I don’t have enough time to read them all.” (also checked “Mostly language design and history”)
![Page 6: David Evans cs.virginia/~evans](https://reader035.vdocuments.us/reader035/viewer/2022062316/56815677550346895dc42bc4/html5/thumbnails/6.jpg)
1 March 2001 CS 655: Lecture13 6
Find a Lambda calculus term that has a normal form, but that will not be reduced to that form if beta reductions are not done in normal order.
Like asking: “Find a Scheme program that terminates with lazy evaluation, but doesn’t terminate with eager evaluation. Translate it into Lambda calculus.”
![Page 7: David Evans cs.virginia/~evans](https://reader035.vdocuments.us/reader035/viewer/2022062316/56815677550346895dc42bc4/html5/thumbnails/7.jpg)
1 March 2001 CS 655: Lecture13 7
What does a program mean?
• Compile and run– Implementation dependencies– Not useful for reasoning
• Informal Semantics– Natural language description of PL
• Formal Semantics– Description in terms of notation with
formally understood meaning
![Page 8: David Evans cs.virginia/~evans](https://reader035.vdocuments.us/reader035/viewer/2022062316/56815677550346895dc42bc4/html5/thumbnails/8.jpg)
1 March 2001 CS 655: Lecture13 8
Why not informal semantics?
Two types have compatible types if their types are the same [footnote: Two types need not be identical to be compatible.].
ANSI C Standard, 3.1.2.6
![Page 9: David Evans cs.virginia/~evans](https://reader035.vdocuments.us/reader035/viewer/2022062316/56815677550346895dc42bc4/html5/thumbnails/9.jpg)
1 March 2001 CS 655: Lecture13 9
Formal Semantics Approaches• Operational
– Map to execution of a virtual machine– Easier to understand, harder to reason about– Depends on informal understanding of machine
• Denotational– Map parts to mathematical meanings, rules for
composing meanings of pieces– Harder to understand, easier to reason about– Depends on informal understanding of mathematics
• Lots of others: Algebraic, Translator, etc.• What about Lambda Calculus?
![Page 10: David Evans cs.virginia/~evans](https://reader035.vdocuments.us/reader035/viewer/2022062316/56815677550346895dc42bc4/html5/thumbnails/10.jpg)
1 March 2001 CS 655: Lecture13 10
A Toy Language: BARK
Program ::= Instruction* Program is a sequence of instructionsInstructions are numbered from 0.
Instruction ::= STORE Loc Literal Loc gets the value of Literal
| ADD Loc1 Loc2 Loc1 gets the value of Loc1 + Loc2
| MUL Loc1 Loc2 Loc1 gets the value of Loc1 * Loc2
| HALT Stop program execution, answer in R0 | ERROR Stop program execution, error | GOTO Loc Jump to instruction corresponding to
value in Loc. | IF Loc1 THEN Loc1 If value in Loc1 is non-zero,
jump to instruction corresponding to value in Loc2.
Loc ::= R[-]?[0-9][0-9]*Literal ::= [-]?[0-9][0-9]*
(Beginner’s All-Purpose Register Kode)
![Page 11: David Evans cs.virginia/~evans](https://reader035.vdocuments.us/reader035/viewer/2022062316/56815677550346895dc42bc4/html5/thumbnails/11.jpg)
1 March 2001 CS 655: Lecture13 11
A BARK Program[0] STORE R0 1[1] STORE R1 10[3] STORE R2 –1[4] STORE R3 6[5] STORE R4 8[6] IF R1 THEN R4[7] HALT[8] MUL R0 R1 [9] ADD R1 R2[10] GOTO R3
![Page 12: David Evans cs.virginia/~evans](https://reader035.vdocuments.us/reader035/viewer/2022062316/56815677550346895dc42bc4/html5/thumbnails/12.jpg)
1 March 2001 CS 655: Lecture13 12
Operational Semantics Game
Input Function
Abstract Machine
InitialConfiguration
FinalConfiguration
Output FunctionAnswer
IntermediateConfiguration
IntermediateConfiguration
TransitionRules
Real World
Program
![Page 13: David Evans cs.virginia/~evans](https://reader035.vdocuments.us/reader035/viewer/2022062316/56815677550346895dc42bc4/html5/thumbnails/13.jpg)
1 March 2001 CS 655: Lecture13 13
Structured Operational Semantics
SOS for a language is five-tuple:
C Set of configurations for an abstract machine
Transition relation (subset of C x C)I Program C (input function)F Set of final configurationsO F Answer (output function)
![Page 14: David Evans cs.virginia/~evans](https://reader035.vdocuments.us/reader035/viewer/2022062316/56815677550346895dc42bc4/html5/thumbnails/14.jpg)
1 March 2001 CS 655: Lecture13 14
Abstract Machine: Register Virtual Machine (RVM)
Configuration defined by:– Array of Instructions– Program counter– Values in registers
(any integer)
C = Instructions x PC x RegisterFile
Instruction[0]
Instruction[1]
Instruction[2]
….
Instruction[-1]
….
PC
Register[0]
Register[1]
Register[2]
….
Register[-1]
….
![Page 15: David Evans cs.virginia/~evans](https://reader035.vdocuments.us/reader035/viewer/2022062316/56815677550346895dc42bc4/html5/thumbnails/15.jpg)
1 March 2001 CS 655: Lecture13 15
Input Function: I: Program CC = Instructions x PC x RegisterFile whereFor a Program with n instructions numbered from
0 to n - 1:Instructions[m] = Program[m] for m >= 0 && m < n
Instructions[m] = ERROR otherwise PC = 0RegisterFile[n] = 0 for all integers n
![Page 16: David Evans cs.virginia/~evans](https://reader035.vdocuments.us/reader035/viewer/2022062316/56815677550346895dc42bc4/html5/thumbnails/16.jpg)
1 March 2001 CS 655: Lecture13 16
Final ConfigurationsF = Instructions x PC x RegisterFile
where Instructions[PC] = HALT
Output FunctionO:F AnswerAnswer = value in RegisterFile[0]
![Page 17: David Evans cs.virginia/~evans](https://reader035.vdocuments.us/reader035/viewer/2022062316/56815677550346895dc42bc4/html5/thumbnails/17.jpg)
1 March 2001 CS 655: Lecture13 17
Operational Semantics Game
Input Function
Abstract Machine
InitialConfiguration
FinalConfiguration
Output FunctionAnswer
IntermediateConfiguration
IntermediateConfiguration
TransitionRules
Real World
Program
![Page 18: David Evans cs.virginia/~evans](https://reader035.vdocuments.us/reader035/viewer/2022062316/56815677550346895dc42bc4/html5/thumbnails/18.jpg)
1 March 2001 CS 655: Lecture13 18
Form of Transition Rules
Antecedents
c c’
Where c is a member of C .
![Page 19: David Evans cs.virginia/~evans](https://reader035.vdocuments.us/reader035/viewer/2022062316/56815677550346895dc42bc4/html5/thumbnails/19.jpg)
1 March 2001 CS 655: Lecture13 19
STORE Loc Literal
Instructions[PC] = STORE Loc Literal
< Instructions x PC x RegisterFile > < Instructions x PC’ x RegisterFile’ >where
PC’ = PC + 1RegisterFile’[n] = RegisterFile[n] if n LocRegisterFile’[n] = value of Literal if n Loc
![Page 20: David Evans cs.virginia/~evans](https://reader035.vdocuments.us/reader035/viewer/2022062316/56815677550346895dc42bc4/html5/thumbnails/20.jpg)
1 March 2001 CS 655: Lecture13 20
ADD Loc1 Loc2
Instructions[PC] = ADD Loc1 Loc2
< Instructions x PC x RegisterFile > < Instructions x PC’ x RegisterFile’ >where
PC’ = PC + 1RegisterFile’[n] = RegisterFile[n] if n LocRegisterFile’[n] = RegisterFile[Loc2] if n Loc1
![Page 21: David Evans cs.virginia/~evans](https://reader035.vdocuments.us/reader035/viewer/2022062316/56815677550346895dc42bc4/html5/thumbnails/21.jpg)
1 March 2001 CS 655: Lecture13 21
GOTO Loc
Instructions[PC] = GOTO Loc
< Instructions x PC x RegisterFile > < Instructions x PC’ x RegisterFile > where PC’ = value in RegisterFile[Loc]
![Page 22: David Evans cs.virginia/~evans](https://reader035.vdocuments.us/reader035/viewer/2022062316/56815677550346895dc42bc4/html5/thumbnails/22.jpg)
1 March 2001 CS 655: Lecture13 22
IF Loc1 THEN Loc2
Instructions[PC] = IF Loc1 THEN Loc2
< Instructions x PC x RegisterFile > < Instructions x PC’ x RegisterFile > where
PC’ = value in RegisterFile[Loc2] if Loc1 is non-
zeroPC’ = PC + 1 otherwise
![Page 23: David Evans cs.virginia/~evans](https://reader035.vdocuments.us/reader035/viewer/2022062316/56815677550346895dc42bc4/html5/thumbnails/23.jpg)
1 March 2001 CS 655: Lecture13 23
What’s it good for?
• Understanding programs• Write a compiler or interpreter (?)• Prove properties about programs and
languages
![Page 24: David Evans cs.virginia/~evans](https://reader035.vdocuments.us/reader035/viewer/2022062316/56815677550346895dc42bc4/html5/thumbnails/24.jpg)
1 March 2001 CS 655: Lecture13 24
Variation: BARK-forward
Same as BARK except:GOTO Loc Jump forward Loc instructions. Loc must
be positive. | IF Loc1 THEN Loc2 If value in Loc1 is non-zero, jump
forwardvalue in Loc2. instructions. Loc2
must be positive.
![Page 25: David Evans cs.virginia/~evans](https://reader035.vdocuments.us/reader035/viewer/2022062316/56815677550346895dc42bc4/html5/thumbnails/25.jpg)
1 March 2001 CS 655: Lecture13 25
GOTO Loc Instructions[PC] = GOTO Loc
< Instructions x PC x RegisterFile > < Instructions x PC’ x RegisterFile > where PC’ = value in RegisterFile[Loc]
BARK:
Instructions[PC] = GOTO Loc,value in Loc > 0
< Instructions x PC x RegisterFile > < Instructions x PC’ x RegisterFile > where
PC’ = PC + value in RegisterFile[Loc]
![Page 26: David Evans cs.virginia/~evans](https://reader035.vdocuments.us/reader035/viewer/2022062316/56815677550346895dc42bc4/html5/thumbnails/26.jpg)
1 March 2001 CS 655: Lecture13 26
Proving Termination
• Idea: Prove by Induction– Define a function
Energy: C positive integer– Show Energy of all Initial Configurations is
finite– Show there are no transitions from a
configuration with Energy = 0– If C C’ is a valid transition step, Energy
of C’ must be less than Energy of C
![Page 27: David Evans cs.virginia/~evans](https://reader035.vdocuments.us/reader035/viewer/2022062316/56815677550346895dc42bc4/html5/thumbnails/27.jpg)
1 March 2001 CS 655: Lecture13 27
Energy Function
Energy: C positive integerC = < Instructions x PC x RegisterFile > Energy = h – PC where
h is an integer such that Instructions[k] = error for all k >= h
![Page 28: David Evans cs.virginia/~evans](https://reader035.vdocuments.us/reader035/viewer/2022062316/56815677550346895dc42bc4/html5/thumbnails/28.jpg)
1 March 2001 CS 655: Lecture13 28
Initial ConfigurationsFor all Initial Configurations C , Energy is
finite. Consider Input Function:
C = Instructions x PC x RegisterFile whereFor a Program with n instructions numbered from 0 to n - 1:
Instructions[m] = Program[m] for m >= 0 && m < n
Instructions[m] = ERROR otherwise PC = 0RegisterFile[n] = 0 for all integers n
![Page 29: David Evans cs.virginia/~evans](https://reader035.vdocuments.us/reader035/viewer/2022062316/56815677550346895dc42bc4/html5/thumbnails/29.jpg)
1 March 2001 CS 655: Lecture13 29
Initial Configuration Energy
Energy (C ) = nwhere n is number of program instructions
PC = 0Instruction[m] = ERROR for m >= n
![Page 30: David Evans cs.virginia/~evans](https://reader035.vdocuments.us/reader035/viewer/2022062316/56815677550346895dc42bc4/html5/thumbnails/30.jpg)
1 March 2001 CS 655: Lecture13 30
Energy = 0 Terminated
Energy = h – PC whereh is an integer such that Instructions[k] = error for all k >= h
so, Energy = 0 h = PC and Instructions[PC] = ERROR
No transitions for configuration where Instructions[PC] = ERROR
![Page 31: David Evans cs.virginia/~evans](https://reader035.vdocuments.us/reader035/viewer/2022062316/56815677550346895dc42bc4/html5/thumbnails/31.jpg)
1 March 2001 CS 655: Lecture13 31
STORE reduces EnergyInstructions[PC] = STORE Loc Literal
< Instructions x PC x RegisterFile > < Instructions x PC’ x RegisterFile’ >where
PC’ = PC + 1RegisterFile’[n] = RegisterFile[n] if n LocRegisterFile’[n] = value of Literal if n Loc
Energy (<Instructions x PC x RegisterFile>) = h – PCEnergy (<Instructions x PC’ x RegisterFile>) = h – (PC + 1)h depends only on Instructions, doesn’t changeTherefore: Energy’ < Energy
![Page 32: David Evans cs.virginia/~evans](https://reader035.vdocuments.us/reader035/viewer/2022062316/56815677550346895dc42bc4/html5/thumbnails/32.jpg)
1 March 2001 CS 655: Lecture13 32
GOTO reduces EnergyInstructions[PC] = GOTO Loc,value in Loc > 0
< Instructions x PC x RegisterFile > < Instructions x PC’ x RegisterFile > where PC’ = PC + value in RegisterFile[Loc]
Energy(<Instructions x PC x RegisterFile>) = h - PCEnergy(<Instructions x PC’ x RegisterFile>) =
h – (PC + RegisterFile[Loc])but antecedent says RegisterFile[Loc] > 0,so PC + RegisterFile[Loc] > PCand Energy’ < Energy.
![Page 33: David Evans cs.virginia/~evans](https://reader035.vdocuments.us/reader035/viewer/2022062316/56815677550346895dc42bc4/html5/thumbnails/33.jpg)
1 March 2001 CS 655: Lecture13 33
To complete proof…
• Show the same for every transition rule.• Then:
– Start with finite energy,– Every transition reduces energy,– Energy must eventually reach 0.– And energy 0 means we terminated.
• Minor flaw? could skip 0 (e.g., Energy = –1)
![Page 34: David Evans cs.virginia/~evans](https://reader035.vdocuments.us/reader035/viewer/2022062316/56815677550346895dc42bc4/html5/thumbnails/34.jpg)
1 March 2001 CS 655: Lecture13 34
Charge
• Gifford’s Ch 3 uses a stack-based virtual machine to provide an operational semantics for a stack-based language
• Next time:– Projects Kickoff – start thinking about things you
want to do– Types and Static Semantics