Docsity

Operational Semantics of Bjarfk and Polygon Equivalence - Prof. David Evans, Exams of Programming Languages

Toggle sidebar
bg1
CS 655 Final Exam 2000 Page 1 of 9
CS655: Final Exam 2000 Solutions
You have 90 minutes to complete the written part of the exam. Write out answers to all the
questions on the answer sheets. Question values are percents of the total exam grade, the written
part counts for 50% of the exam.
Operational Semantics and Concurrency
In Lecture 19, we defined a standard operational semantics for Bjarfk, a language with
concurrency primitives shown below:
Program
::=
Instruction
* Program is a sequence of instructions
Instructions are numbered from 0.
Execution begins at instruction 0, and
completes with the initial thread halts.
Instruction
::=
Loc
:=
Expression Loc
gets the value of
Expression
|
Loc
:= FORK
Expression Loc
gets the value of the ThreadHandle returned
by FORK; Starts a new thread at instruction
numbered
Expression
.
|JOIN
Expression
Waits until thread associated with ThreadHandle
Expression
completes.
| HALT Stop thread execution.
Expression
::=
Literal
|
Expression
+
Expression | Expression * Expression
The relevant parts of the operational semantics (identical to Lecture 19) are shown on the next
page.
Ben Bitdiddle would like to extend Bjarfk to support SUSPEND and RESUME.Toprevent
mistakes, only a running thread may be suspended, and only a suspended thread may be resumed.
The new language, Bsjarrfk adds the following instructions to Bjarfk:
Instruction
::= SUSPEND
Expression
Temporarily stop execution of the thread identified by the
ThreadHandle
Expression.
The suspended thread must not execute
any more instructions until it is used as an operand of a RESUME
instruction.
| RESUME
Expression
Restart the thread identified by the ThreadHandle
Expression
.The
thread must currently be suspended.
1. (15%) Describe how to modify the operational semantics to describe Bsjarrfk. You may
modify the configurations of the virtual machine. Show the new transition rules for
Assignment, SUSPEND,andRESUME. If you find the informal descriptions to be
ambiguous, clarify any ambiguities in a sensible way in your rules.
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Operational Semantics of Bjarfk and Polygon Equivalence - Prof. David Evans and more Exams Programming Languages in PDF only on Docsity!

CS655: Final Exam 2000 Solutions

You have 90 minutes to complete the written part of the exam. Write out answers to all the questions on the answer sheets. Question values are percents of the total exam grade, the written part counts for 50% of the exam.

Operational Semantics and Concurrency

In Lecture 19, we defined a standard operational semantics for Bjarfk, a language with concurrency primitives shown below:

Program ::=Instruction* Program is a sequence of instructions Instructions are numbered from 0. Execution begins at instruction 0, and completes with the initial thread halts.

Instruction ::=Loc :=Expression Loc gets the value ofExpression |Loc := FORKExpression Loc gets the value of the ThreadHandle returned by FORK; Starts a new thread at instruction numberedExpression. | JOINExpression Waits until thread associated with ThreadHandle Expression completes. | HALT Stop thread execution.

Expression ::=Literal |Expression +Expression | Expression * Expression

The relevant parts of the operational semantics (identical to Lecture 19) are shown on the next page.

Ben Bitdiddle would like to extend Bjarfk to support SUSPEND and RESUME. To prevent mistakes, only a running thread may be suspended, and only a suspended thread may be resumed. The new language, Bsjarrfk adds the following instructions to Bjarfk:

Instruction ::= SUSPENDExpression Temporarily stop execution of the thread identified by the ThreadHandleExpression. The suspended thread must not execute any more instructions until it is used as an operand of a RESUME instruction. | RESUMEExpression Restart the thread identified by the ThreadHandleExpression. The thread must currently be suspended.

  1. (15%) Describe how to modify the operational semantics to describe Bsjarrfk. You may modify the configurations of the virtual machine. Show the new transition rules for Assignment, SUSPEND, and RESUME. If you find the informal descriptions to be ambiguous, clarify any ambiguities in a sensible way in your rules.

Answer:

We can describe suspend by keeping additional information about each thread, indicating whether it is suspended or running. We change the configurations to be:

C = Instructions x Threads x RegisterFile where Instructions: array[Instruction] Threads: array[<ThreadHandle, PC, status ∈ { suspended, running } >] RegisterFile: array[int]

The input function will set the status of the main thread to running:

Threads = [ <0, 0, running> ]

The final configurations not need to match a 3-tuple:

F = Instructions x Threads x RegisterFile where <0, PC, running> ∈ Threads and Instructions[PC] = HALT

The transition rules need to be extended to check the thread is running:

Assignment: <t, PCt, running> ∈ Threads & Instructions[PCt] = Loc := Value < Instructions x Threads x RegisterFile > Þ < Instructions x Threads’ x RegisterFile’ > where Threads = Threads – {<t, PCt, running >} + {<t, PCt + 1, running } RegisterFile’[n] = RegisterFile[n] if n ≠ Loc RegisterFile’[n] = value of Value if n = Loc

The rule for SUSPEND:

<t, PCt, running> ∈ Threads & Instructions[PCt] = SUSPENDValue & <value, PCs, running> ∈ Threads & value is the ThreadHandle value denoted byValue

< Instructions x Threads x RegisterFile > Þ < Instructions x Threads’ x RegisterFile’ > where if (value = t) // Note: PCs must equal PCt (it would be worth checking this) Threads = Threads – {<t, PCt, running >} + {<t, PCt + 1, suspended } else Threads = Threads – {<t, PCt, running >} + {<t, PCt + 1, running }

  • {<value, PCs, running >} + {<value, PCs, suspended> }

Note that we need to handle the case where a thread suspends itself specially. The most sensible solution is to suspend the thread with the PC advanced, as shown by this rule.

The rule for RESUME:

<t, PCt, running> ∈ Threads & Instructions[PCt] = RESUMEValue & <value, PCs, suspended> ∈ Threads & value is the ThreadHandle value denoted byValue < Instructions x Threads x RegisterFile > Þ < Instructions x Threads’ x RegisterFile’ > where Threads = Threads – {<t, PCt, running >} + {<t, PCt + 1, running }

  • {<value, PCs, suspended>} + {<value, PCs, running> }

Reasoning about Datatypes

Consider the polygon type specified below. Note that we assume constructors are defined outside the datatype, so this is a complete specification for the polygon type.

polygon = datatype is equivalent A polygon is with n sides is described by [ <l (^) 0, a (^) 0>, <l (^) 1, a (^) 1>, …, <l (^) n-1, a (^) n-1> ] where l (^) i is an integer giving the length of the ith^ edge of the polygon and ai is an integer giving the angle in degrees between the ith^ edge and the ((i + 1) mod n) th^ edge.

For example, the rectangle with edges length 5 and 6 could be represented by the polygon [ <5, 90>, <6, 90>, <5, 90>, <6, 90> ] or the equivalent polygon [ <6, 90>, <5, 90>, <6, 90>, <5, 90> ].

equivalent = proc (arg: polygon) returns (bool) modifies nothing ensures Returns true if and only if this and arg represent identical polygons. Two polygons, p = [ <p (^) 0, a 0 >, <p 1 , a (^) 1>, …, <pn-1, a (^) n-1> ] and q = [ <q 0 , b (^) 0>, <q (^) 1, b1>, …, <qm-1, b (^) m-1> ] are equivalent if m = n and ∃ k: 0 ≤ k< n such that ∀ i, 0 ≤ i< n: pi = q(i + k) mod n ∧ a (^) i = b (^) (i + k) mod n.

Lucy Reasoner produced a Java implementation of polygon shown on the next page.

  1. (5%) What is the rep invariant for polygon? Do not worry about any geometrical constraints on polygons, only what is needed for the abstraction function and code.

Answer: I (r) ≡ r.edges.length = r.angles.length

The only thing we need in the rep invariant is that the size of the edges and angles arrays are equal. A correct answer could include geometrical constrains also (such as the number of edges being at least 3), but the question did not ask for this, and the correct geometric constraints are very complicated (e.g., knowing the edges don’t cross based on their angles and lengths, and knowing the polygon is closed).

  1. (20%) Sketch a proof that the result returned on line 18 satisfies the ensures clause. You may assume that Lucy has already proved the predicate Q on line 14 is true if the predicate P on line 8 is true. Your proof need not be at the low level of detail, but should explain how you prove the predicate P on line 8 is true and how you prove the result satisfies the postcondition.

You may find the partial correctness rule for while useful:

P Þ Inv, Inv {Pred } Inv, Inv ∧ Pred {StatementList }Inv, (Inv & ¬ Pred) Þ Q

P { while (Pred) { StatementList } }Q

Answer:

To show the result is correct, we need to construct a proof that the post-condition of the outer while loop (from line 5 – line 17) leaves result with a value that satisfies the ensures clause:

result ≡ m = n and ∃ k: 0 ≤ k< n such that ∀ i, 0 ≤ i< n: p (^) i = q(i + k) mod n ∧ a (^) i = b (^) (i + k) mod n.

The ensures clause is at the level of abstract objects, so we will need to prove a similar property in terms of the concrete rep, and then use the abstraction function to map it to the ensures clause. The similar property we need is:

result ≡ this .edges.length = arg.edges.length and ∃ k: 0 ≤ k< this.edges.length such that ∀ i, 0 ≤ i< this.edges.length: this.edges[i] = args.edges[(i + k) mod this.edges.length] ∧ this.angles[i] = arg.angles[(i+k) mod this.edges.length]

The abstraction function is A(r) = [ < <p 0 , a (^) 0>, <p (^) 1, a 1 >, …, <pn-1, a (^) n-1> ] where n = r.edges.length and pi = r.edges[i] and ai = r.angles[i]

So, using A( this ) and A(arg) we can map the concrete property to:

result ≡ m = n and ∃ k: 0 ≤ k< n such that ∀ i, 0 ≤ i< n: p (^) i = q (^) (i + k) mod n ∧ a (^) i = b(i + k) mod n

which matches the ensures clause exactly. ( Note : it was not necessary to show the mapping between the concrete object and abstract ensures clause to get full credit for this question.)

Now, we need to show that the desired property is true for result returned on line 18. We need to construct an axiomatic proof that the post-condition of the outer loop implies the result property. For this, we need a loop invariant. Informally, the loop invariant should tell us that result is true if all the edges and angles matched correctly on a previous iteration of the loop. We can write this as:

result = ∨ ( ∧ (this.edges[i] = arg.edges[(i + j) % nedges]

∧ this.angles[i] = arg.angles[(i + j) % nedges]) )

We have taken the expression for isequiv in Q (line 4) for the inner predicate, and replaced k with j. We have a problem if k = 0, since the normal meaning of an or evaluation with no terms is true , but result should be false. So, we need to add a conjunct: ∧ k > 0. (One could argue strictly that two polygons with no edges should be view are equivalent, and the result should be true according to the ensures clause, so the code is incorrect if the polygons have zero edges; a better solution would be to add a requirement that the size of edges must be > 0.)

In addition, our invariant needs to preserve the values of nedges, this, arg and ensure k ≤ nedges (since we will need this for the post-condition).

i = 0

nedges - 1

j = 0

k - 1

Next, we show: Q14 = k 0 = k ∧ nedges 0 = nedges ∧ result 0 = result ∧ this 0 = this ∧ arg 0 = arg

∧ isequiv = ∧ (this.edges[i] = arg.edges[(i + k) % nedges]

∧ this.angles[i] = arg.angles[(i + k) % nedges])

{ if (isequiv) result = true ; k = k + 1; } Inv

If isequiv is false, result does not change, and we increment k. Since we are or-ing with false, the invariant is preserved.

If isequiv is true, result becomes true, and we increment k. Since we are or-ing with true, the result clause in the invariant becomes true, and the invariant is preserved.

The k ≤ nedges term is satisfied because the predicate requires k < nedges before k is incremented.

(Inv & ¬ Pred) Þ Q:

k ≤ nedges & ¬ (k < nedges) Þ k = nedges.

Substituting in Inv gives,

Inv ≡ result = nedges > 0 ∧ ∨ ( ∧ (this.edges[i] = arg.edges[(i + j) % nedges]

∧ this.angles[i] = arg.angles[(i + j) % nedges]) )

∧ nedges = this .edges.length ∧ nedges = arg.edges.length ∧ nedges = this .angles.length ∧ nedges = arg.angles.length ∧ this 0 = this ∧ arg 0 = arg ∧ k ≤ nedges

The get the result condition, we need to convert the outer ∨ to

∃ k: 0 ≤ k< this.edges.length such that ∀ i, 0 ≤ i< this.edges.length: this.edges[i] = args.edges[(i + k) mod this.edges.length] ∧ this.angles[i] = arg.angles[(i+k) mod this.edges.length]

This is true, since if the outer ∨ is true, there must be some inner term that is true (corresponding to the ∃ k); if the outer ∨ is false, then no inner term is true, so no such k exists. The only discrepancy remaning is the nedges > 0 clause – we can either add a rep invariant to make this always true, or argue that in fact the implementation is incorrect because it returns the wrong result for a 0-edged polygon.

Note: This answer gives far more detail that was necessary to get full credit for this question. To get full credit, you needed to: come up with an invariant for the loop on line 5, explain how you use the rep invariant to prove P8, and explain how you use Q14 to show the loop preserves the invariant for the outer loop.

i = 0

nedges - 1

i = 0

nedges - 1

j = 0

nedges - 1

  1. (10%) Lucy specifies a new type, triangle as shown below:

triangle = datatype is equivalent A triangle is described by [ l 0 , l 1 , l 2 ] where l (^) i is an integer giving the length of the ith side of the triangle. (Recall that the angles of a triangle are determined by the edges.)

equivalent = proc (t: triangle) returns (bool) modifies nothing ensures Returns true if and only if this and t represent identical triangles. Two triangles, p = [ p 0 , p (^) 1, p2 ] and q = [ q 0 , q (^) 1, q2 ] are equivalent if ∃ k: 0 ≤ k< 2 such that ∀ i, 0 ≤ i< 2: pi = q(i + k) mod n.

According to the behavioral notion of subtyping introduced by Liskov and Wing, would it be safe to introduce the type rule: triangle ⊆ polygon? If so, produce a proof that it satisfies the subtype requirements; if not, show how it should be changed in order to satisfy them.

Answer:

No. It is not a subtype because the type of the parameter for the triangle equivalent method violates the contravariant requirement.

To fix the problem, we need to change the type of the parameter to polygon (or any supertype of polygon), and change the specification accordingly.

(That is all that you needed for a full credit answer.)

The other requirements of Liskov and Wing’s notion of subtyping are:

  • The precondition of the subtype methods must be implied by the precondition of the corresponding supertype method. This is trivially true since both preconditions are true (there is no requires clause).
  • The postcondition of the subtype methods implies the postcondition of the supertype methods. This is harder since we need to convert the abstract triangle to an abstract polygon:

[p (^) 0, p1, p2 ] Þ [ <l 0 , a (^) 0>, <l (^) 1, a (^) 1>, <l (^) 2, a (^) 2> ]

We can use l (^) i = p (^) i and a (^) 0, a (^) 1, a 2 are the angles determined by the triangle. (Some of you revealed impressive memory of trigonometry actually calculating the angles, but this should have given you some sense that it probably wasn’t the correct answer.)

Then, we need to show that after mapping the abstract objects, the postcondition of triangle.equivalent implies the postcondition of polygon.equivalent. This follows since we replace 2 with n to get:

∃ k: 0 ≤ k< n such that ∀ i, 0 ≤ i< n: pi = q (^) (i + k) mod n.

and add the clause ∧ a (^) i = b(i + k) mod n because the triangle sides determine the angles (so triangles with the same sides must also have the same angles.) (Partial credit was rewarded for Yes answers that explained this, but it was new necessary to explain any of this since the type property is enough to say it is not a subtype.)