CS 655: Programming Languages David Evans Office 236A, 982-2218 [email protected] evans...

34
CS 655: Programming Languages David Evans Office 236A, 982-2218 [email protected] http:// www.cs.virginia.edu/ ~evans University of Virginia Computer Science Lecture 2: 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

Transcript of CS 655: Programming Languages David Evans Office 236A, 982-2218 [email protected] evans...

CS 655: Programming Languages

David EvansOffice 236A, [email protected]://www.cs.virginia.edu/~evans

University of VirginiaComputer Science

Lecture 2: 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

27 Jan 2000 CS 655: Lecture 2 2

Menu

• Survey Summary

• Position Papers Results

• Intro to Formal Semantics

• Operational Semantics

27 Jan 2000 CS 655: Lecture 2 3

Students’ ExperienceC - 92.5 years, C++ - 78, BASIC 64,

Pascal – 45, Java - 26.5, assemblies – 26, FORTRAN – 22, LISP – 17, Perl – 14.5

Ada, ADAMS, Common LISP, Delphi, Elf, Forth, Haskell, HP Calculator, Hypertalk, JavaScript (large system for DOD!), ksh, LOGO, ML, nawk, ObjectPal, Python, Prolog, REBOL, Relay Ladder Logic, Sather, Scheme, Smalltalk, SQL, Tcl/Tk

27 Jan 2000 CS 655: Lecture 2 4

• No +, 5 +2 (= = 1.4…)

• Our not favorite languages:BASIC (1) C (8 - 1)C++ (3) FORTRAN (2)Java (8 - 2) Perl (2 - 2)

• Good papers had to get at underlying issues, not just symptoms

• Some misconceptions about GC

Position Paper 1

2

27 Jan 2000 CS 655: Lecture 2 5

Project Teams

• You have until noon Monday to submit team requests

• Use the discussion board if you are looking for teammates(link off ~cs655 page)

27 Jan 2000 CS 655: Lecture 2 6

Formal Semantics

27 Jan 2000 CS 655: Lecture 2 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

27 Jan 2000 CS 655: Lecture 2 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

27 Jan 2000 CS 655: Lecture 2 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.

27 Jan 2000 CS 655: Lecture 2 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)

27 Jan 2000 CS 655: Lecture 2 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

27 Jan 2000 CS 655: Lecture 2 12

Operational Semantics Game

Input Function

Abstract Machine

InitialConfiguration

FinalConfiguration

Output FunctionAnswer

IntermediateConfiguration

IntermediateConfiguration

TransitionRules

Real World

Program

27 Jan 2000 CS 655: Lecture 2 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)

27 Jan 2000 CS 655: Lecture 2 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]

….

27 Jan 2000 CS 655: Lecture 2 15

Input Function: I: Program C

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 = 0

RegisterFile[n] = 0 for all integers n

27 Jan 2000 CS 655: Lecture 2 16

Final ConfigurationsF = Instructions x PC x RegisterFile

where Instructions[PC] = HALT

Output Function

O:F Answer

Answer = value in RegisterFile[0]

27 Jan 2000 CS 655: Lecture 2 17

Operational Semantics Game

Input Function

Abstract Machine

InitialConfiguration

FinalConfiguration

Output FunctionAnswer

IntermediateConfiguration

IntermediateConfiguration

TransitionRules

Real World

Program

27 Jan 2000 CS 655: Lecture 2 18

Form of Transition Rules

Antecedents

c c’

Where c is a member of C .

27 Jan 2000 CS 655: Lecture 2 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

27 Jan 2000 CS 655: Lecture 2 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

27 Jan 2000 CS 655: Lecture 2 21

GOTO Loc

Instructions[PC] = GOTO Loc

< Instructions x PC x RegisterFile > < Instructions x PC’ x RegisterFile > where PC’ = value in RegisterFile[Loc]

27 Jan 2000 CS 655: Lecture 2 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

27 Jan 2000 CS 655: Lecture 2 23

What’s it good for?

• Understanding programs

• Write a compiler or interpreter (?)

• Prove properties about programs and languages

27 Jan 2000 CS 655: Lecture 2 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 forward

value in Loc2. instructions. Loc2

must be positive.

27 Jan 2000 CS 655: Lecture 2 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]

27 Jan 2000 CS 655: Lecture 2 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

27 Jan 2000 CS 655: Lecture 2 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

27 Jan 2000 CS 655: Lecture 2 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 = 0

RegisterFile[n] = 0 for all integers n

27 Jan 2000 CS 655: Lecture 2 29

Initial Configuration Energy

Energy (C ) = n

where n is number of program instructions

PC = 0

Instruction[m] = ERROR for m >= n

27 Jan 2000 CS 655: Lecture 2 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

27 Jan 2000 CS 655: Lecture 2 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

27 Jan 2000 CS 655: Lecture 2 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.

27 Jan 2000 CS 655: Lecture 2 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)

27 Jan 2000 CS 655: Lecture 2 34

Charge

• Problem Set 1:– Develop Operational Semantics for simplified

PostFix using RVM– Prove termination property

• Final project team requests by Monday• Next time:

– Projects– Discuss Wenger paper: come to class prepared to

discuss how well his milestones work today