Action Semantics
Total Page:16
File Type:pdf, Size:1020Kb
Action Semantics Formal Syntax BNF — In common use Formal Specification of Programming Languages Formal Semantics Advantages: Denotational semantics • Unambiguous definitions Structural operational semantics • Basis for proving properties of programs and languages Axiomatic semantics • Mechanical generation of language Algebraic semantics processors Used only by specialists in prog. languages. Disadvantages: Action Semantics • Notationally dense • Developed by Peter Mosses and David Watt • Often cryptic • Based on ordinary computation concepts • Unlike the way programmers view languages • Difficult to create and modify accurately • English-like notation (readable) • Completely formal, but can be understood informally • Reflects the ordinary computational concepts of programming languages Chapter 13 1 Chapter 13 2 Specifying a Programming Language Introduction to Action Semantics An action specification breaks into two parts: Three kinds of first-order entities: • Data: Basic mathematical values Programming Language • Yielders: Expressions that evaluate to data Definition of the constructs of a using current information Upper level language in terms of action notation. • Actions: Dynamic, computational entities Action Notation that model operational behavior Specification of the meaning Lower level of action notation. Data and Sorts Meaning of Actions Data manipulated by a programming language • integers • cells • booleans • tuples Meaning of a language is defined by mapping program phrases to actions • maps whose performance describes the execution of the program phrases. Chapter 13 3 Chapter 13 4 Classification of Data Data Specification Data classified according to how far it tends to module be propagated during action performance. TruthValues exports sort TruthValue Transient operations Tuples of data given as the immediate results true : TruthValue of action performance. Use them or lose false : TruthValue them. not _ : TruthValue → TruthValue both(_,_) : TruthValue,TruthValue → TruthValue either(_,_) TruthValue TruthValue → TruthValue Scoped : , _ is _ : TruthValue,TruthValue → TruthValue Data consisting of bindings of tokens end exports (identifiers) to data as in environments. equations … Stable end TruthValues Stable data model memory as values stored module Integers in cells (locations); may be altered by explicit imports TruthValues actions only. exports sort Integer Actions are also classified this way. Chapter 13 5 Chapter 13 6 operations 0 : Integer Yielders 1 : Integer Current information (maintained implicitly) 10 : Integer successor : Integer → Integer • the given transients, predecessor : Integer → Integer • the received bindings, and sum(_,_) : Integer, Integer → Integer • the current state of the storage. difference(_,_) : Integer, Integer → Integer → Yielders are terms that evaluate to data product(_,_) : Integer, Integer Integer dependent on the current information. integer-quotient(_,_) : Integer,Integer → Integer _ is _: Integer, Integer → TruthValue the given S : Data → Yielder _ is less than _ : Integer,Integer → TruthValue → Yield the transient data given to an action, _ is greater than _ : Integer, Integer provided it agrees with the sort S. TruthValue end exports the given S # n : Datum, PosInteger → Yielder equations Yield the nth item in tuple of transient data … given to action, provided it agrees with sort S. end Integers the _ bound to _ : Data, Token → Yielder Sort operations (a lattice) Yield the object bound to an identifier Join (union) of two sorts S1 and S2: S1 | S2. denoted by Token in current bindings, after verifying that its type is sort specified as Data. Meet (intersection) of sorts S1 and S2: S1 & S2. Bottom element: nothing Chapter 13 7 Chapter 13 8 → the _ stored in _ : Data, Yielder Yielder An action performance may Yield value of sort Data stored in memory complete (terminate normally), location denoted by the cell yielded by fail (terminate abnormally), or second argument. diverge (not terminate at all). Precedence Highest:Prefix (right-to-left) Facets of Action Semantics Infix (left-to-right) Actions are classified into facets, depending on Lowest: Outfix the main type of information processed. Actions • Functional Facet: actions that process transient information • When performed, actions accept the data passed to them as the current information • Imperative Facet: actions that affect memory the given transients, the received bindings, and • Declarative Facet: actions that process the current state of storage scoped information to give new transients, produce new bindings, and/or update the state of the storage. • Basic Facet: actions that principally specify flow of control Chapter 13 9 Chapter 13 10 Functional and Basic Facets Flow lines from the top to the bottom of the diagram show the behavior of the transients. Primitive functional action give Y Give value obtained by evaluating Concatenation: Join the data flow lines the yielder Y transients Action combinators are used to define control flow as well as to manage the movement of A1 information between actions. complete A1 and then A 2 combinator : Action, Action → Action A2 A1 and then A2 transients Perform the first action and then perform A1 and A2 the second. Allows the performance of the two actions Dashed line shows control flow to be interleaved. No control dependency in diagram, A so actions can be performed collaterally. 1 complete transients A1 and then A 2 A1 and A2 A2 A1 A2 transients Chapter 13 11 Chapter 13 12 A1 then A2 Example Perform the first action using the transients give sum(the given Integer#1, given to the combined action and then perform the second action using the the given Integer#2) transients given by the first action. and give (the given Integer#1 is the given Integer#2 The transients given by the combined action are the transients given by the second action. (3,5) transients give sum (the given Integer#1, the given Integer#2) A1 complete give (the given Integer#1 is the given Integer#2) A then A (8,false) 1 2 A2 transients Chapter 13 13 Chapter 13 14 Imperative Facet Imperative yielder the S stored in Y : Data, Yielder → Yielder Imperative facet deals with storage Yield the value of sort S stored in the • allocating memory locations cell yielded by Y. • updating the contents of locations • fetching values from memory Primitive Imperative Actions • deallocating memory locations allocate a cell Find an unused cell, storing undefined in Any action may alter the state of a cell, and it, and give cell as the transient of action. such a modification remains in effect until some other action modifies the cell again. store Y1 in Y2 Update the cell yielded by Y2 to Current storage is a finite mapping from cells contain the Storable yielded by Y1. to the sort (Storable | undefined). The imperative facet has no special action combinators, but any action has the potential of altering storage. Chapter 13 15 Chapter 13 16 Suppose that one location, denoted by Module for Imperative Features cell1, has been allocated and currently contains the value undefined. Also assume that the next cell to be module Imperative allocated will be cell2. imports Integers, Maps exports Initial storage: cell1 undefined sort Storable = Integer sort Storage = store 77 in cell1 cell1 77 map [Cell to (Storable | undefined)] and then sort Cell cell 77 operations allocate a cell 1 cell2 undefined cell1 : Cell then allocate a cell : Action store _ in _ : Yielder, Yielder → Action cell1 77 store 15 in the given Cell → cell2 15 the _ stored in _ : Storable, Yielder Yielder end exports and then equations store product (the Integer stored in cell1, cell1 1155 … the Integer stored in cell2) in cell1 cell2 15 end Imperative This module is defined to support the calculator specification that comes next. Chapter 13 17 Chapter 13 18 Action Semantics of a Calculator Concrete Syntax <program> ::= <expression sequence> <expression sequence> ::= <expression> 7 8 9 x Clear | <expression> <expression sequence> 4 5 6 – MR <expression> ::= <term> 1 2 3 + M+ | <expression> <operator> <term> | <expression> <answer> 0 +/– = | <expression> <answer> +/- A Three-function Calculator <term> ::= <numeral> | MR | Clear | <term> +/- A “program” on this calculator consists of a <operator> ::= + | – | x sequence of keystrokes generally alternating between operands and operators. <answer> ::= M+ | = 6 + 33 x 2 = produces the value 78. <numeral> ::= <digit> | <numeral> <digit> <digit> ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 Outlaw unusual combinations such as: 5 + + 6 = and 88 x +/- 11 + MR MR Chapter 13 19 Chapter 13 20 Abstract Syntax Example Abstract Syntactic Domains Following the concrete syntax for the calculator language, given the sequence of keystrokes, P : Program E: Expression D:Digit 10 M+ + 6 +/- = x MR = S : ExprSequence N : Numeral a parser will construct the abstract syntax tree shown below. Abstract Production Rules Program ::= ExprSequence expr ExprSequence ::= Expression expr = | Expression ExprSequence expr x MR R Expression ::= Numeral | M | Clear expr = | Expression + Expression | Expression – Expression expr + expr + | Expression x Expression 10 M 6 +/– | Expression M+ | Expression = An Abstract Syntax Tree | Expression +/- Numeral ::= Digit | Numeral Digit Digit ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 Chapter 13 21 Chapter 13 22 Semantic Functions Semantic Equations Meaning is ascribed to the calculator language via semantic functions, mostly mapping 1. Numeral syntactic domains to actions. To evaluate a numeral, we simply