1 / 34

David Evans cs.virginia/~evans

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.”

cooper
Download Presentation

David Evans cs.virginia/~evans

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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 David Evans http://www.cs.virginia.edu/~evans CS655: Programming Languages University of Virginia Computer Science

  2. Menu • Quiz Results • Intro to Formal Semantics • Operational Semantics 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.5 No, research 0.5 No, sleeping 1 • Speed: way too fast (0), too fast (6), wrote in just right/both (3), too slow (1) 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 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”) 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.” 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 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 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? CS 655: Lecture13

  10. A Toy Language: BARK (Beginner’s All-Purpose Register Kode) Program ::= Instruction* Program is a sequence of instructions Instructions are numbered from 0. Instruction ::= STORELocLiteralLoc gets the value of Literal | ADDLoc1Loc2Loc1 gets the value of Loc1 + Loc2 | MULLoc1Loc2 Loc1 gets the value of Loc1 * Loc2 | HALT Stop program execution, answer in R0 | ERROR Stop program execution, error | GOTOLoc Jump to instruction corresponding to value in Loc. | IFLoc1THENLoc1 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]* 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 CS 655: Lecture13

  12. Operational Semantics Game Real World Abstract Machine Program Initial Configuration Input Function Intermediate Configuration Transition Rules Intermediate Configuration Answer Final Configuration Output Function CS 655: Lecture13

  13. Structured Operational Semantics SOS for a language is five-tuple: CSet of configurations for an abstract machine  Transition relation (subset of C x C) I Program  C (input function) F Set of final configurations OF  Answer (output function) 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[-1] Register[-1] Instruction[0] Register[0] PC Instruction[1] Register[1] Instruction[2] Register[2] …. …. CS 655: Lecture13

  15. Input Function: I: Program  C C = Instructions x PC x RegisterFile where For 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 = 0 RegisterFile[n] = 0 for all integers n CS 655: Lecture13

  16. Final Configurations Output Function F = Instructions x PC x RegisterFile where Instructions[PC] = HALT O:F  Answer Answer = value in RegisterFile[0] CS 655: Lecture13

  17. Operational Semantics Game Real World Abstract Machine Program Initial Configuration Input Function Intermediate Configuration Transition Rules Intermediate Configuration Answer Final Configuration Output Function CS 655: Lecture13

  18. Form of Transition Rules Antecedents c c’ Where c is a member of C . CS 655: Lecture13

  19. STORELoc Literal Instructions[PC] = STORELoc Literal < Instructions x PC x RegisterFile >  < Instructions x PC’ x RegisterFile’ > where PC’ = PC + 1 RegisterFile’[n] = RegisterFile[n] if n  Loc RegisterFile’[n] = value of Literal if n  Loc CS 655: Lecture13

  20. ADDLoc1 Loc2 Instructions[PC] = ADDLoc1 Loc2 < Instructions x PC x RegisterFile >  < Instructions x PC’ x RegisterFile’ > where PC’ = PC + 1 RegisterFile’[n] = RegisterFile[n] if n  Loc RegisterFile’[n] = RegisterFile[Loc2] if n  Loc1 CS 655: Lecture13

  21. GOTOLoc Instructions[PC] = GOTOLoc < Instructions x PC x RegisterFile >  < Instructions x PC’ x RegisterFile > where PC’ = value in RegisterFile[Loc] CS 655: Lecture13

  22. IFLoc1THEN Loc2 Instructions[PC] = IFLoc1THEN Loc2 < Instructions x PC x RegisterFile >  < Instructions x PC’ x RegisterFile > where PC’ = value in RegisterFile[Loc2] if Loc1 is non-zero PC’ = PC + 1 otherwise CS 655: Lecture13

  23. What’s it good for? • Understanding programs • Write a compiler or interpreter (?) • Prove properties about programs and languages CS 655: Lecture13

  24. Variation: BARK-forward Same as BARK except: GOTOLoc Jump forward Loc instructions. Loc must be positive. | IFLoc1THENLoc2 If value in Loc1 is non-zero, jump forward value in Loc2. instructions. Loc2 must be positive. CS 655: Lecture13

  25. BARK: GOTOLoc < Instructions x PC x RegisterFile >  < Instructions x PC’ x RegisterFile > where PC’ = value in RegisterFile[Loc] Instructions[PC] = GOTOLoc Instructions[PC] = GOTOLoc,value in Loc > 0 < Instructions x PC x RegisterFile >  < Instructions x PC’ x RegisterFile > where PC’ = PC + value in RegisterFile[Loc] 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 CS 655: Lecture13

  27. Energy Function Energy: C  positive integer C = < Instructions x PC x RegisterFile > Energy = h – PC where h is an integer such that Instructions[k] = error for all k >= h CS 655: Lecture13

  28. Initial Configurations For all Initial Configurations C , Energy is finite. Consider Input Function: C = Instructions x PC x RegisterFile where For 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 = 0 RegisterFile[n] = 0 for all integers n CS 655: Lecture13

  29. Initial Configuration Energy Energy (C ) = n where n is number of program instructions PC = 0 Instruction[m] = ERROR for m >= n CS 655: Lecture13

  30. Energy = 0  Terminated Energy = h – PC where h 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 CS 655: Lecture13

  31. STORE reduces Energy Instructions[PC] = STORELoc Literal < Instructions x PC x RegisterFile >  < Instructions x PC’ x RegisterFile’ > where PC’ = PC + 1 RegisterFile’[n] = RegisterFile[n] if n  Loc RegisterFile’[n] = value of Literal if n  Loc Energy (<Instructions x PC x RegisterFile>) = h – PC Energy (<Instructions x PC’ x RegisterFile>) = h – (PC + 1) h depends only on Instructions, doesn’t change Therefore: Energy’ < Energy CS 655: Lecture13

  32. GOTO reduces Energy Instructions[PC] = GOTOLoc,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 - PC Energy(<Instructions x PC’ x RegisterFile>) = h – (PC + RegisterFile[Loc]) but antecedent says RegisterFile[Loc] > 0, so PC + RegisterFile[Loc] > PC and Energy’ < Energy. 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) 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 CS 655: Lecture13

More Related