Action Formal Syntax BNF — In common use Formal Specification of Programming Languages Formal Semantics Advantages: ¥ Unambiguous definitions Structural ¥ Basis for proving properties of programs and languages ¥ 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

::= ::=

7 8 9 x Clear | 4 5 6 – MR ::= 1 2 3 + M+ | | 0 +/– = | +/- A Three-function Calculator ::= | MR | Clear | +/- A “program” on this calculator consists of a ::= + | Ð | x sequence of keystrokes generally alternating between operands and operators. ::= M+ | = 6 + 33 x 2 = produces the value 78. ::= | ::= 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 display its meaning _ : Program → Action integer value on the display. perform _ : ExprSequence → Action evaluate N = give value of N evaluate _ : Expression → Action The value given as a transient by the action give is the displayed integer. Prefix operations are value of _ : Numeral → Integer evaluated from right to left, so omit the parentheses from “give (value of N)”. digit value _ : Digit → Integer

Action [outcome] [income]. 2. Memory Recall → Display the value stored in the single memory meaning : Program location that we assume has been allocated Action [completing | giving a Value | storing] [using current storage] initially and named cell1. The module Imperative asserts the existence of a constant cell, cell1, to perform_ : ExprSeq → serve this purpose. Action [completing | giving a Value | storing] [using current storage] evaluate MR = give the Integer stored in cell1 evaluate_: Expr → Action [completing | giving a Value | storing] [using current storage]

Chapter 13 23 Chapter 13 24 The first combinator forms a tuple (a pair) 3. Clear consisting of the values of the two expressions, which are evaluated from left to right. That tuple is The clear operation resets the memory location to given to the sum operation, which adds the two zero and displays zero. components. evaluate Clear = store 0 in cell1 and 5. Difference of Two Expressions give 0 6. Product of Two Expressions If interference were possible between the two These operations are handled in the same way as activities, we could use the combinator “and then” addition. to establish order.

7. Add to Memory 4. Addition of Two Expressions Display the value of the current expression and This binary operation gives the sum of the integers add it to the calculator memory. that results from the two expressions. The left expression must be carried out first since it may evaluate [E M+] = involve a side effect by storing a value in the evaluate E calculator memory. then store sum(the Integer stored in cell1, evaluate [E1 + E2] = the given Integer) in cell1 evaluate E1 and then and give the given Integer evaluate E2 then The second subaction to then must propagate the give sum(the given Integer#1, transient from the first subaction so that it can be the given Integer#2) given by the composite action.

Chapter 13 25 Chapter 13 26

The and combinator forms a tuple, in this case a The semantic function “meaning” initializes the singleton tuple, which action semantics does not calculator by storing zero in the memory location distinguish from a single datum. cell1 and then evaluates the expression sequence. The primitive action “store _ in _” yields no meaning P = transient, which is represented by an empty tuple. store 0 in cell1 Without the subaction “give the given Integer”, the and then value from E will be lost. perform P

“perform” evaluates the expressions in the sequence 8. Equal one at a time, ignoring the given transients. The equal key just terminates an evaluation, perform [E S] = displaying the value from the current expression. evaluate E and then evaluate [E =] = evaluate E perform S perform E = evaluate E 9. Change Sign The +/- key flips the sign of the integer produced by “value of” and “digit value” define the meaning of the latest expression evaluation. integers.

evaluate [E +/-] = value of [N D] = evaluate E sum(product(10,value N), value of D) then value of D = digit value D give difference(0, the given Integer) digit value 0 = 0 : digit value 9 = 9

Chapter 13 27 Chapter 13 28 A Sample Calculation The first expression begins with an empty transient and with cell1 containing the value 0. We show the transient given by each of the subactions as well as Consider the following calculator program: the value stored in cell1.

12 + 5 +/- = x 2 M+ 123 M+ MR +/- Ð 25 = + MR = Transient cell1 evaluate [12 + 5 +/- = x 2 M+] = ( ) 0 This sequence of calculator keystrokes parses into give value of 12 (12) 0 three expressions, so that the overall structure of and then the action semantics evaluation has the form: give value of 5 (5) 0 then + + + R + meaning [12 + 5 /- = x 2 M 123 M M /- give difference (0, the given Integer) (-5) 0 R Ð 25 = + M =] then (12,-5) 0 = store 0 in cell1 give sum (the given Integer#1, the given Integer#2) (7) 0 and then and then perform [12 + 5 +/- = x 2 M+ 123 M+ give value of 2 (2) 0 MR +/- Ð 25 = + MR =] then (7,2) 0 give product (the given Integer#1, the given Integer#2) (14) 0 = store 0 in cell1 then and then store sum (the Integer stored in cell1, evaluate [12 + 5 +/- = x 2 M+] the given Integer) in cell1 ( ) 14 and then and give the given Integer (14) 14 evaluate [123 M+] and then This action gives the value 14, which is also the value in cell1. evaluate [MR +/- Ð 25 = + MR =]

Chapter 13 29 Chapter 13 30

The second expression starts with 14 in memory, ignoring the given transient, and results in the following action: Wren and Pelican evaluate [123 M+] = For each syntactic construct, a brief informal give value of 123 (123) 14 description of its semantics and a definition in then store sum (the Integer stored in cell1, action semantics. the given Integer) in cell1 ( ) 137 and ¥ Sequence of Commands give the given Integer (123) 137

This action gives the value 123 and leaves 137 in cell1. Execute the first command and then execute the second. The third expression completes the evaluation, starting with 137 in memory, as follows: execute [C1 ; C2] = R R evaluate [M +/- Ð 25 = + M =] = execute C1 and then execute C2 give the Integer stored in cell1 (137) 137 then give difference (0, the given Integer) (-137) 137 ¥ Unary Minus and then give value of 25 (25) 137 Evaluate the expression and give the negation then (-137,25) 137 of the resulting value. give difference (the given Integer#1, evaluate [ ] = the given Integer#2) (-162) 137 – E and then evaluate E give the Integer stored in cell1 (137) 137 then then (-162,137) 137 give difference (0, the given Integer) give sum (the given Integer#1, the given Integer#2) (-25) 137 Value given by evaluating expression is given This final action gives the value -25, leaving 137 in cell1. as a transient to the difference operation,

Chapter 13 31 Chapter 13 32 whose value is given as the result of the action. ¥ Arithmetic on Two Expressions Evaluate the two expressions and give the sum of their values.

evaluate [E1 + E2] = evaluate E1 and evaluate E2 then give sum(the given Integer#1, the given Integer#2) Wren has no side effects in expressions

¥ Assignment Find the cell bound to the identifier and evaluate the expression. Then store the value of the expression in that cell. execute [I := E] = give the Cell bound to I and evaluate E then store the given Value#2 in the given Cell#1

Chapter 13 33 Chapter 13 34

Basic Facet Actions Primitive functional action: check Y Deal primarily with control flow. where Y is a yielder that gives a TruthValue, completes if Y yields true and fails if it yields A1 or A2 false. Arbitrarily choose one of the subactions and Acts as a guard when combined with the perform it with given transients and received composite action “or”. bindings. If the chosen action fails, perform the other ¥ If Commands subaction with original transients and Evaluate a Boolean expression and then bindings. perform then command or else command depending on the test. If the else part is

transients missing, do nothing.

A or A 1 2 execute [if E then C1 else C2] = bindings Ak evaluate E fail bindings then

bindings check (the given TruthValue is true) A 3-k and then execute C1

transients transients or check (the given TruthValue is false) and then execute C2

Chapter 13 35 Chapter 13 36 transients unfold

execute [if E then C] = unfolding A bindings evaluate E A then bindings check (the given TruthValue is true) unfold and then execute C or transients check (the given TruthValue is false) and then complete ¥ While Command Evaluate the Boolean expression; if its value Actions needed to define the while is true, execute the body of the loop and then command: start the while command again when the unfolding A execution of loop body completes; otherwise, Perform the action A, but whenever the the command terminates. dummy action “unfold” is encountered, the execute [while E do C] = action A is performed again in place of it. unfolding evaluate E unfold then A dummy action, standing for the argument check (the given TruthValue is true) action of the innermost enclosing “unfolding”. and then execute C and then unfold or check (the given TruthValue is false) and then complete

Chapter 13 37 Chapter 13 38

Declarative Facet Primitive declarative action bind T to Y Deals primarily with scoped information in the produces a singleton binding mapping that we form of bindings between identifiers and → semantic entities such as constants, variables, represent informally by [ T | B] . and procedures. module Declarative Declarative yielder imports Imperative, Mappings the S bound to T: Data, Token → Yielder exports evaluates to the entity bound to the Token T sorts provided it agrees with the sort S. Token, Variable = Cell, All action combinators process bindings as well Bindable = Variable, as transients. Bindings = Mapping[Token to (Bindable | unbound)] operations Combining bindings empty bindings : Bindings bind _ to _ : Token, Yielder → Action merge (bindings1, bindings2): the _ bound to _ : Data, Token → Yielder Form the (disjoint) union of the bindings so produce _ : Yielder → Action that if any identifier has bindings in both sets, … the operation fails, producing nothing. end exports Shown by having the lines for scoped equations information connected by a small circle : end Declarative

Chapter 13 39 Chapter 13 40 overlay (bindings1, bindings2):

Combine bindings so that the associations transients in bindings1 take precedence over those in A1 and then A2 bindings2. A1 Lines show a break suggesting which set of complete

bindings takes precedence. bindings bindings

A2 In diagrams, scoped information flows from left to right whereas transients flow from top to transients bottom.

transients

A1 and A2 transients A1 A1 then A2 A1 bindings bindings complete A 2 bindings bindings

A2 transients transients

Chapter 13 41 Chapter 13 42

Declarative Facet Actions ¥ Program Manipulate environments. Elaborate the declarations and execute the body of the program with the resulting bindings. rebind run [program I is D begin C end] = This primitive declarative action reproduces all of the received bindings. elaborate D hence execute C

A1 hence A2 A1 moreover A2 This combinator sequences the bindings and Allows the performance of the two actions concatenaties the transients. to be interleaved. Both actions use the transients and bindings transients passed to the combined action. A hence A 1 2 The bindings produced by the combined A1 bindings action are the bindings produced by the complete first action overlaid by those produced by the second. A2 bindings Transients are concatenated.

transients

Chapter 13 43 Chapter 13 44 transients

A1 moreover A2 A1 before A2 A1 Perform first action using transients and bindings bindings A bindings passed to the combined action, and 2 then perform second action using transients given to the combined action and the bindings transients received by the combined action overlaid by those produced by first action. Anonymous Block • (declare) Produces the bindings produced by first action Elaborate the declarations in the block overlaid with those produced by second. producing bindings that overlay the bindings received from the enclosing block and Transients are concatenated. execute the body of the block with the resulting bindings. transients A before A The bindings created by the local declaration 1 2 are lost after the block is executed. A1 complete bindings execute [declare D begin C end] = bindings rebind moreover elaborate D A2 hence execute C transients “rebind moreover elaborate D” overlays the received bindings with the local bindings from D to provide the environment for C.

Chapter 13 45 Chapter 13 46

¥ Sequence of Declarations ¥ Variable Declaration Elaborate the declarations sequentially. Allocate a cell from storage and then bind the identifier to that cell. elaborate [D1 ; D2] = elaborate D1 before elaborate D2 elaborate [var I : T] = allocate a cell “before” combines the bindings from the two then declarations so that D1 overlays the enclosing bind I to the given Cell environment and D2 overlays D1, producing the combined bindings. Each declaration has access to the identifiers ¥ Variable Name or Constant Identifier that were defined earlier in the same block as An identifier can be bound to a constant value well as those in any enclosing block. or to a variable. Evaluating an identifier gives the constant or the value assigned to the variable. ¥ Constant Declaration evaluate I = Evaluate the expression and then bind its give the Value stored in the Cell bound to I value to the identifier. or elaborate [const I = E] = give the Value bound to I evaluate E then bind I to the given Value

Chapter 13 47 Chapter 13 48 Visualizing Action Semantics

program scope is const c = 5; var n : integer; begin declare const m = c+8; -- D1 const n = 2*m; -- D2 begin : -- C end; : end Assume that the first cell allocated is cell1.

The action that elaborates the first two declarations produces the bindings [c | → 5, n | → cell1]. These bindings are received by the body of the program and by the declare command.

Chapter 13 49 Chapter 13 50

Following action models the declare command: The action elaborate [D1 ; D2] serves as the second subaction in execute [declare D1 ; D2 begin C end] = rebind moreover elaborate [D1 ; D2] rebind moreover elaborate [D1 ; D2], hence which is depicted in the next diagram. execute C

|→ |→ [c 5, n cell1] First elaborate the declarations rebind [c |→5, n |→cell ] [n |→26, m |→13, c |→5] elaborate [D1 ; D2] = 1 elaborate D1 before elaborate D2. elaborate [D1 D2] [n |→26, m |→13] Following diagram with empty transients omitted illustrates the activities carried out by The body of the anonymous block will execute the before combinator. in an environment containing three bindings, [n | → 26, m | → 13, c | → 5]. [m |→13] elaborate D |→ |→ 1 [c 5, n cell1] complete [n |→26, m |→13]

elaborate D2 |→ |→ |→ |→ [n 26] [m 13, c 5, n cell1]

Chapter 13 51 Chapter 13 52 Transients and bindings can be supplied after Reflective Facet and Procedures the abstraction is constructed. ¥ Subprogram declaration and invocation. ¥ Activity of a procedure modeled by the The current bindings are inserted into an performance of an action. abstraction using an operation on yielders. sorts Procedure = Abstraction closure of _ : Yielder → Yielder Bindable = Variable | Value | Procedure Attaching the declaration-time bindings provides static scoping for resolving references to Abstraction datum is an entity with three nonlocal variables. components: the action itself and the transients — and bindings, if any, that will be given to the A action when it is performed. StaticBindings A later performance of “closure of _” will have Transients Abstraction = Action no effect. Bindings Dynamic scoping ensues if bindings are attached at enaction-time (when the action in Creating an Abstraction the abstraction is performed). abstraction of _ : Action → Yielder The yielder “abstraction of A” encapsulates the Execute of a procedure using a reflective action action A into an abstraction together with no that takes as its parameter a yielder giving an transients and no bindings. abstraction.

— enact _ : Yielder → Action A —

Chapter 13 53 Chapter 13 54

The action “enact Y” activates the action encapsulated in the abstraction yielded by Y, using the transients and bindings that are ¥ Procedure Call (no parameter) included in the abstraction. Execute the procedure object bound to the If no transients or bindings have been identifier. incorporated into the abstraction, the enclosed execute I = enact the Procedure bound to I action is given empty transients or empty bindings. The procedure object, an abstraction, brings along its static environment. ¥ Procedure Declaration (no parameter) An operation on yielders constructs an Bind the identifier of the declaration to a unevaluated term that incorporates the current procedure object that incorporates the body of transient into the abstraction. the procedure, so that it will be executed in the declaration-time environment. application of _ to _ : Yielder, Yielder → Yielder elaborate [procedure I is D begin C end] = The yielder “application of Y1 to Y2” attaches bind I to the argument value yielded by Y2 as the closure of transient that will be given to the action abstraction of encapsulated in the abstraction yielded by Y1 rebind moreover elaborate D when that action is enacted. hence As with bindings, a second supply of transients execute C is ignored.

Chapter 13 55 Chapter 13 56 ¥ Procedure Call (one parameter) The combinator thence joins the behavior of then for transients and hence for bindings. Evaluate the actual parameter, an expression, and then execute the procedure bound to the transients identifier with the value of the expression. A1 execute [I (E)] = bindings evaluate E complete then A thence A 1 2 bindings enact application of (Procedure bound to I) A2 to the given Value transients Assuming that Abs, the abstraction bound to I, incorporates the action A and the bindings Declaration of Procedure (One Parameter) StaticBindings, and that Val is the value of the expression E, “application of Abs to the given The action encapsulated in an abstraction Value” creates the abstraction that will be expects a value, the actual parameter, to be enacted. given to it as a transient. This value is stored in a new memory location (Val) A allocated by the action. StaticBindings The command that constitutes the body of the procedure is executed in an environment that consists of the original static environment overlaid by the binding of the formal parameter to a local variable, and then overlaid by local declarations.

Chapter 13 57 Chapter 13 58

¥ Procedure Declaration (one parameter) Recursive Definitions Bind procedure identifier in the declaration to a The specifications of procedure declarations procedure object that incorporates the body of above do not allow recursive calls of the procedure, so that when it is called, it will procedures, since identifiers being declared are be executed in declaration-time environment not included in bindings in abstractions created and will allocate a local variable for the actual by declarations. parameter passed to procedure. A hybrid action for establishing recursive elaborate [procedure I1 (I2) is D begin C end] = bindings that is defined in terms of more bind I1 to primitive actions. closure of recursively bind _ to _ : Token, Bindable → Action abstraction of allocate a cell “recursively bind T to abstraction of A” and give the given Value produces the binding of T to an abstraction Abs and rebind so that the bindings attached to the action A thence incorporated in Abs include the binding being rebind produced.

moreover — Abs = A bind I2 to the given Cell#1 [T |→Abs] and store the given Value#2 in Therefore the action “recursively bind _ to _” the given Cell#1 permits the construction of a circular binding. hence rebind moreover elaborate D hence execute C

Chapter 13 59 Chapter 13 60 Action “closure of abstraction of A” creates the abstraction Abs, which does not allow a elaborate [procedure I is D begin C end] = recursive call of procedure. recursively bind I to — closure of Abs = A |→ |→ abstraction of [c 5, b cell1] rebind moreover elaborate D hence execute C Action “bind p to closure of abstraction of A” produces the binding [p|→Abs]. Example Any reference to procedure identifier p inside the procedure is an illegal reference, yielding program example is nothing. const c = 5; var b : boolean; Action “recursively bind p to closure of procedure p is abstraction of A” changes abstraction Abs into a : new abstraction Abs' whose attached bindings begin … end; include the association of procedure abstraction begin with p.

: — Abs' = A |→ |→ |→ end [c 5, b cell1, p Abs'] Let A denote the action corresponding to the body of the procedure. The recursive action produces the binding [p|→Abs'], which when overlaid on the previous bindings, produces the bindings [ c|→5,

Chapter 13 61 Chapter 13 62 b|→cell1, p|→Abs' ] to be received by the procedure p and the body of the program. Translating to Action Notation Action notation can be viewed as a metalanguage for the semantic specification of programming languages. Semantic equations define a translator from Pelican programs into action notation.

Consider interpreting or compiling action notation. The metalanguage of action semantics can also be used to verify semantic equivalence between language phrases. Translate a Pelican program into its equivalent action notation.

Task is aided by the property of compositionality: Each phrase is define solely in terms of the meaning of its immediate subphrases.

Chapter 13 63 Chapter 13 64 program action is The overall structure of the translation takes the form const max = 50; -- D1 var acc : integer; -- D2 run [program I is D1 D2 D3 D4 D5 var switch : boolean; -- D3 begin C1; C2; C3; C4 end] var n : integer; -- D4 = elaborate [D1 D2 D3 D4 D4] procedure change is -- D5 hence execute [C1; C2; C3; C4] begin n := n+3; = elaborate D1 switch := not(switch) before elaborate D2 end; before elaborate D3 begin before elaborate D4 acc := 0; -- C1 before elaborate D5 n := 1; -- C2 hence switch := true; -- C3 execute C1 and then execute C2 while n<=max do -- C4 and then execute C3 if switch then acc := acc+n end if; and then execute C4 change and then execute C5 end while The combinators and then and before are end associative.

Chapter 13 65 Chapter 13 66

elaborate [procedure change is The five declarations: begin n := n+3; switch := not(switch) end] = elaborate [const max = 50] = recursively bind change to closure of abstraction of give value of 50 rebind then moreover bind max to the given Value produce empty bindings hence give Cell bound to n elaborate [var acc : integer] = and allocate a cell give Value stored in Cell bound to n then or bind acc to the given Cell give Value bound to n and give value of 3 then elaborate [var switch : boolean] = give sum(the given Int#1,the given Int#2) allocate a cell then then store the given Value#2 in the given Cell#1 bind switch to the given Cell and then give Cell bound to switch and elaborate [var n : integer] = give Value stored in allocate a cell Cell bound to switch then or bind acc to the given Cell give Value bound to switch then give not(the given Truthvalue) then store the given Value#2 in the given Cell#1

Chapter 13 67 Chapter 13 68 The four commands: execute [while n<=max do if then end if execute [ ] = switch acc := acc+n ; acc := 0 change end while] = give Cell bound to acc unfolding and give Value stored in Cell bound to n give value of 0 or then give Value bound to n store given Value#2 in the given Cell#1 and give Value stored in Cell bound to max or execute [n := 1] = give Value bound to max give Cell bound to n then and give not(greaterthan(the given Integer#1, the given Integer#2)) give value of 1 then then check given Truthvalue is true store the given Value#2 in and then the given Cell#1 give Value stored in Cell bound to switch or give Value bound to switch execute [switch := true]= then give Cell bound to switch check given Truthvalue is true and and then give true give Cell bound to acc then and store the given Value#2 in give Value stored in Cell bound to acc the given Cell#1 or give Value bound to acc and

Chapter 13 69 Chapter 13 70

give Value stored in Cell bound to or LABORATORY give Value bound to n Translator from Pelican to action notation using then give sum(the given Int#1, Prolog. the given Int#2) The compositional definitions of the meaning of then Pelican convert to Prolog clauses directly. store the given Value#2 in the given Cell#1 The resulting action can be represented as a or Prolog structure by viewing actions, yielders, check given Truthvalue is false and auxilliary opertions with prefix syntax. and then complete >>> Translating Pelican into Action Semantics <<< and then Enter name of source file: small.wren enact Procedure bound to change program small is const c = 34; and then unfold var n : integer; or begin check given Truthvalue is false n := c+21 and then complete end Translated Action: hence( before( then(give(valueof(34)),bind(c,given(Value))), before( then(allocateacell,bind(n,given(Value))), produce(emptybindings))), andthen( then( and(give(boundto(Cell,n)), then( and(or(give(storedin(Value,boundto(Cell,c))), give(boundto(Value,c))),give(valueof(21))), give(sum(given(Integer,1),given(Integer,2))))), storein(given(Value,2),given(Cell,1))), complete)) yes

Chapter 13 71 Chapter 13 72 Translation is a static operation Declarations Need not be concerned with stores and elaborate([],produce(emptybindings)). environments—these are handled when action notation is interpreted or compiled. elaborate([Dec|Decs], before(ElaborateDec,ElaborateDecs)) :- We have dispensed with the syntactic category elaborate(Dec,ElaborateDec), of blocks to match the specification in Figure elaborate(Decs,ElaborateDecs). 13.6. elaborate(var(T,var(Ide)), then(allocateacell,bind(Ide,given('Value')))). run(prog(Decs,Cmds), hence(ElaborateD,ExecuteC)) :- elaborate(con(Ide,E), elaborate(Decs,ElaborateD), then(EvaluateE,bind(Ide,given('Value')))) :- execute(Cmds,ExecuteC). evaluate(E,EvaluateE).

Build Prolog structures that represent the elaborate(proc(Ide,param(Formal),Decs,Cmds), resulting action using calls to the predicates recursivelybind(Ide, elaborate and execute to construct pieces of closureof(abstractionof(hence(hence( the structure. thence(and(allocateacell, and(give(given('Value')), rebind)), moreover(rebind, and(bindto(Formal,given('Cell',1)), storein(given('Value',2),given('Cell',1))))), moreover(rebind,ElaborateD)),

Chapter 13 73 Chapter 13 74

ExecuteC))))) :- elaborate(Decs,ElaborateD), execute(Cmds,ExecuteC). execute(assign(Ide,Exp), then(and(give(boundto('Cell',Ide)),EvaluateE), storein(given('Value',2),given('Cell',1)))) Commands evaluate(Exp,EvaluateE). execute([Cmd|Cmds], andthen(ExecuteCmd,ExecuteCmds)) :- execute(if(Test,Then), execute(Cmd,ExecuteCmd), then(EvaluteE, execute(Cmds,ExecuteCmds). or(andthen(check( is(given('Truthvalue'),true)), ExecuteC), execute([],complete). andthen(check( is(given('Truthvalue'),false)), execute(declare(Decs,Cmds), complete)))) :- hence(moreover(rebind,ElaborateD), evaluate(Test,EvaluteE), ExecuteC)) :- execute(Then,ExecuteC). elaborate(Decs,ElaborateD), execute(Cmds,ExecuteC). execute(call(Ide,E), then(EvaluateE, execute(skip,complete). enact(application( boundto(procedure,Ide), given('Value'))))) :- evaluate(E,EvaluateE).

Chapter 13 75 Chapter 13 76 execute(while(Test,Body), unfolding( then(EvaluteE, evaluate(plus(E1,E2), or(andthen(check( then(and(EvaluateE1,EvaluateE2), is(given('Truthvalue'),true)), give(sum(given('Integer',1), andthen(ExecuteC,unfold)), given('Integer',2))))) :- andthen(check( evaluate(E1,EvaluateE1), is(given('Truthvalue'),false)), evaluate(E2,EvaluateE2). complete))))) :- evaluate(Test,EvaluteE), execute(Body,ExecuteC). evaluate(neq(E1,E2), then(and(EvaluateE1,EvaluateE2), give(not(is(given('Integer',1), Expressions given('Integer',2)))))) :- evaluate(ide(Ide), evaluate(E1,EvaluateE1), or(give(storedin('Value',boundto('Cell',Ide))) evaluate(E2,EvaluateE2). give(boundto('Value',Ide)))). evaluate(and(E1,E2), evaluate(num(N),give(valueof(N))). then(and(EvaluateE1,EvaluateE2), give(both(given('Truthvalue',1), given('Truthvalue',2))))) :- evaluate(minus(E), evaluate(E1,EvaluateE1), then(EvaluteE, evaluate(E2,EvaluateE2). give(sum(given('Integer',1), given('Integer',2))))) :- evaluate(E,EvaluateE).

Chapter 13 77 Chapter 13 78