gamesemanticsofimperativelanguagesusing regularexpressionsgam23/papers/regexps.pdf · 2002. 9....

Post on 12-Sep-2020

0 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Game Semantics of Imperative Languages usingRegular Expressions

Guy McCusker

Game Semantics of Imperative Languages using Regular Expressions – p.1/33

Making games tractable?

The game semantics universe has proved itself to be veryflexible: it provides accurate models for a wide variety ofprogramming languages.But how useful are these models? Can they be used toreason about programs? Do they tell us anything we didn’talready know?This talk describes some work which attempts to provide ausable reasoning technique for the games model ofIdealized Algol, developed in conjunction with DanGhica [1].

Game Semantics of Imperative Languages using Regular Expressions – p.2/33

Idealized Algol

Idealized Algol (IA) is Reynolds’s theoretical distillation ofAlgol 60 [9, 3]. IA can be seen as:

• a basic functional language extended with state• x := 3• !x• new x in . . .

• alternatively, a basic imperative language extendedwith• block structure (new)• higher-order procedures (λ-calculus).

Either way, it is very expressive, elegant and powerful.

Game Semantics of Imperative Languages using Regular Expressions – p.3/33

Capturing programming intuition

Programmers in Algol-like languages have many intuitionsabout the behaviour of programs.

• The use of local variables is invisible from outside ablock: privacy, modularity, representationindependence.

• State changes are irreversible: there is no “snapback”construct

snapback(P )

which runs P and then undoes all its state-changes.

A good semantics should capture and formalize theseintuitions, and the program optimizations which they justify.

Game Semantics of Imperative Languages using Regular Expressions – p.4/33

Some program equivalences

Garbage collection If x does not occur free in P then

new x in P ∼= P.

No snapback

new x in P (x := 1); if !x = 1 then Ω else skip ∼= P (Ω)

Representation Independence

new x : bool in x := true; P (!x, x := ¬!x)∼= new x : int in x := 1; P (!x > 0, x := −!x)

Game Semantics of Imperative Languages using Regular Expressions – p.5/33

Some program equivalences

Garbage collection If x does not occur free in P then

new x in P ∼= P.

No snapback

new x in P (x := 1); if !x = 1 then Ω else skip ∼= P (Ω)

Representation Independence

new x : bool in x := true; P (!x, x := ¬!x)∼= new x : int in x := 1; P (!x > 0, x := −!x)

Game Semantics of Imperative Languages using Regular Expressions – p.5/33

Some program equivalences

Garbage collection If x does not occur free in P then

new x in P ∼= P.

No snapback

new x in P (x := 1); if !x = 1 then Ω else skip ∼= P (Ω)

Representation Independence

new x : bool in x := true; P (!x, x := ¬!x)∼= new x : int in x := 1; P (!x > 0, x := −!x)

Game Semantics of Imperative Languages using Regular Expressions – p.5/33

Semantic approaches

• Classical Milne-Strachey “marked store” models.These can fail to validate garbage collection [2].

• Functor-category models handle locality well.Parametricity constraints can be added to handlerepresentation independence. Snapback remainsproblematic [6, 5].

• Reddy’s “object spaces” handle snapback too. Fullabstraction up to order 2 via the Yonedaembedding [8, 4].

• Operationally-based reasoning [7] .• Game semantics: full abstraction.

Game Semantics of Imperative Languages using Regular Expressions – p.6/33

Semantic approaches

• Classical Milne-Strachey “marked store” models.These can fail to validate garbage collection [2].

• Functor-category models handle locality well.Parametricity constraints can be added to handlerepresentation independence. Snapback remainsproblematic [6, 5].

• Reddy’s “object spaces” handle snapback too. Fullabstraction up to order 2 via the Yonedaembedding [8, 4].

• Operationally-based reasoning [7] .• Game semantics: full abstraction.

Game Semantics of Imperative Languages using Regular Expressions – p.6/33

Semantic approaches

• Classical Milne-Strachey “marked store” models.These can fail to validate garbage collection [2].

• Functor-category models handle locality well.Parametricity constraints can be added to handlerepresentation independence. Snapback remainsproblematic [6, 5].

• Reddy’s “object spaces” handle snapback too. Fullabstraction up to order 2 via the Yonedaembedding [8, 4].

• Operationally-based reasoning [7] .• Game semantics: full abstraction.

Game Semantics of Imperative Languages using Regular Expressions – p.6/33

Semantic approaches

• Classical Milne-Strachey “marked store” models.These can fail to validate garbage collection [2].

• Functor-category models handle locality well.Parametricity constraints can be added to handlerepresentation independence. Snapback remainsproblematic [6, 5].

• Reddy’s “object spaces” handle snapback too. Fullabstraction up to order 2 via the Yonedaembedding [8, 4].

• Operationally-based reasoning [7] .

• Game semantics: full abstraction.

Game Semantics of Imperative Languages using Regular Expressions – p.6/33

Semantic approaches

• Classical Milne-Strachey “marked store” models.These can fail to validate garbage collection [2].

• Functor-category models handle locality well.Parametricity constraints can be added to handlerepresentation independence. Snapback remainsproblematic [6, 5].

• Reddy’s “object spaces” handle snapback too. Fullabstraction up to order 2 via the Yonedaembedding [8, 4].

• Operationally-based reasoning [7] .• Game semantics: full abstraction.

Game Semantics of Imperative Languages using Regular Expressions – p.6/33

Syntax of IA

The language we will consider is a simply-typed λ-calculuswith the following base types.

• Expression types N, B, with the usual constants andoperations, including dereferencing of storagevariables !x.

• The type comm of commands. Commands includeassignment x := M , sequential composition C1; C2,skip, and local blocks new x in M .

• The type var of storage variables. Such variables areallocated using new and manipulated via assignmentand dereferencing.

Game Semantics of Imperative Languages using Regular Expressions – p.7/33

Semantics of base types

The expression types are interpreted as usual. For thenatural numbers:

q

0 1 2 . . .

Commands are interpreted with a similar game:

run

done

Game Semantics of Imperative Languages using Regular Expressions – p.8/33

Semantics of var

Variables have two kinds of initial move, for reading and forwriting:

ok

write(0) write(1) write(2) . . . read

0 1 2 . . .

Game Semantics of Imperative Languages using Regular Expressions – p.9/33

Some examples

x : var, y : var ` x :=!y + 1 : comm

run

read

n

write(n + 1)

ok

done

Game Semantics of Imperative Languages using Regular Expressions – p.10/33

Some examples

c : comm, x : var ` x := 3; c; x :=!x + 1 : comm

run

write(3)

ok

run

done

read

n

(n need not be 3)

write(n + 1)

ok

done

Game Semantics of Imperative Languages using Regular Expressions – p.11/33

Some examples

c : comm, x : var ` x := 3; c; x :=!x + 1 : comm

run

write(3)

ok

run

done

read

n (n need not be 3)

write(n + 1)

ok

done

Game Semantics of Imperative Languages using Regular Expressions – p.11/33

Bad variable behaviour

In the previous example, O must be allowed to respond toread with any value, since the command c may later bebound to something like x := 19.If we wrap the command in a new x in . . ., this changes:the command c cannot now access x, and nor cananything else.The variable x is now a good variable.

Game Semantics of Imperative Languages using Regular Expressions – p.12/33

Variable allocation

The command new x in P is just like P , except that:• x is bound to a storage cell, so P ’s interactions with x

have the expected causal relationship between valueswritten and values read.

• The outside world can no longer see x.

Game Semantics of Imperative Languages using Regular Expressions – p.13/33

Semantics of allocation

In game semantics, variable allocation is handled bycomposition with this strategy:

(var ⇒ comm) ⇒ comm

run

run

s

done

done

where s is any sequence of reads and writes for which thevalues read match the last values written at all times.

Game Semantics of Imperative Languages using Regular Expressions – p.14/33

Allocation in action

comm =⇒ (var ⇒ comm) =⇒ comm

run

run

write(3)

ok

run

done

read

3

write(4)

ok

done

done

Game Semantics of Imperative Languages using Regular Expressions – p.15/33

Allocation in action

This composition achieves two things:• good variable behaviour is enforced.• the interactions in the var type are hidden.

Game Semantics of Imperative Languages using Regular Expressions – p.16/33

A restricted IA

We will now see how to reason about this games modelusing regular expressions, for a restricted language.

We consider only terms of the form

x1 : Θ1, . . . , xn : Θn ` M : B

where B is a base type and the Θi are first-order types.We do not handle general recursion, but we do includewhile-loops.We assume a finite set of data-values.For convenience, we assume all terms are β-normal, sothere are no λ-abstractions, and the only application termswe consider are those of the form

xM1 . . . Mn.

Game Semantics of Imperative Languages using Regular Expressions – p.17/33

A restricted IA

We will now see how to reason about this games modelusing regular expressions, for a restricted language.We consider only terms of the form

x1 : Θ1, . . . , xn : Θn ` M : B

where B is a base type and the Θi are first-order types.

We do not handle general recursion, but we do includewhile-loops.We assume a finite set of data-values.For convenience, we assume all terms are β-normal, sothere are no λ-abstractions, and the only application termswe consider are those of the form

xM1 . . . Mn.

Game Semantics of Imperative Languages using Regular Expressions – p.17/33

A restricted IA

We will now see how to reason about this games modelusing regular expressions, for a restricted language.We consider only terms of the form

x1 : Θ1, . . . , xn : Θn ` M : B

where B is a base type and the Θi are first-order types.We do not handle general recursion, but we do includewhile-loops.

We assume a finite set of data-values.For convenience, we assume all terms are β-normal, sothere are no λ-abstractions, and the only application termswe consider are those of the form

xM1 . . . Mn.

Game Semantics of Imperative Languages using Regular Expressions – p.17/33

A restricted IA

We will now see how to reason about this games modelusing regular expressions, for a restricted language.We consider only terms of the form

x1 : Θ1, . . . , xn : Θn ` M : B

where B is a base type and the Θi are first-order types.We do not handle general recursion, but we do includewhile-loops.We assume a finite set of data-values.

For convenience, we assume all terms are β-normal, sothere are no λ-abstractions, and the only application termswe consider are those of the form

xM1 . . . Mn.

Game Semantics of Imperative Languages using Regular Expressions – p.17/33

A restricted IA

We will now see how to reason about this games modelusing regular expressions, for a restricted language.We consider only terms of the form

x1 : Θ1, . . . , xn : Θn ` M : B

where B is a base type and the Θi are first-order types.We do not handle general recursion, but we do includewhile-loops.We assume a finite set of data-values.For convenience, we assume all terms are β-normal, sothere are no λ-abstractions, and the only application termswe consider are those of the form

xM1 . . . Mn.

Game Semantics of Imperative Languages using Regular Expressions – p.17/33

RegExps++

We will give the game semantics of this subset using amildly extended syntax of regular expressions.

Constants a (singleton), ε (empty string), ⊥ (emptylanguage).

Standard operations R + S (union), R · S (concatenation), R∗

(repetition).

Intersection R ∩ S.

Hiding R \ α: delete all symbols in the set α.

Game Semantics of Imperative Languages using Regular Expressions – p.18/33

Encoding of plays

We must make the disjoint union operation from gamesemantics explicit. We annotate moves as follows:

x : N, f : N ⇒ N ` f(x) : N

q

qf

q1f

qx

nx

n1f

mf

m

Game Semantics of Imperative Languages using Regular Expressions – p.19/33

Denotation of terms

The strategy for a term

x1 : Θ1, . . . , xn : Θn ` M : N

consists of a set of sequences of the form

q . . . n

We will give a set of regular languages ([M ])n such that

[[M ]] =∑

n

q · ([M ])n · n

Game Semantics of Imperative Languages using Regular Expressions – p.20/33

Denotation of terms

For M : comm,

[[M ]] = run · ([M ]) · done.

For M : var,

[[M ]] =∑

n

read · ([M ])read(n) · n

+∑

n

write(n) · ([M ])write(n) · ok

(Note: this notation differs from that used in the paper).

Game Semantics of Imperative Languages using Regular Expressions – p.21/33

Constants

In the usual game semantics, [[n]] = q · n .We define

([n])n = ε, ([n])m = ⊥ for m 6= n.

Similarly:([skip]) = ε, ([Ω]) = ⊥.

Game Semantics of Imperative Languages using Regular Expressions – p.22/33

Variables

For variables x : N, the semantics is the copycat strategy

x : N ` x : N

q

qx

nx

n

so we define([x : N])n = qx · nx.

Game Semantics of Imperative Languages using Regular Expressions – p.23/33

More semantic definitions

([C; C ′]) = ([C]) · ([C ′])

([M + M ′])n =∑

m+m′=n

([M ])m · ([M ′])m′ .

([if B then C else C ′]) = ([B])true · ([C]) + ([B])false · ([C′])

([while B do C]) = (([B])true · ([C]))∗ · ([B])false

([V := M ]) =∑

n

([M ])n · ([V ])write(n)

([!V ])n = ([V ])read(n)

Game Semantics of Imperative Languages using Regular Expressions – p.24/33

Semantics of Application

Given x : comm ⇒ comm ⇒ comm, M1, M2 : comm, wedefine

([xM1M2]) = runx·(run1x·([M1])·done1

x+run2x·([M2])·done2

x)∗·donex.

Similarly, if x : var ⇒ N and M : var, ([xM ])k is

qx·(∑

n read1x · ([M ])read(n) · n

1x +

n write(n)1x · ([M ])write(n) · ok1

x

)∗·

kx.

Game Semantics of Imperative Languages using Regular Expressions – p.25/33

Semantics of allocation

Let X denote the set of symbols tagged with an x. Let Y

be the rest of the alphabet.

The regular language

G = Y ∗·(readx·0x·Y∗)∗·(

n

write(n)x·okx·Y∗·(readx·nx·Y

∗)∗)∗

describes all those strings in which x has good-variablebehaviour.Now we can define

([new x in P ]) = (([P ]) ∩ G) \ X.

Game Semantics of Imperative Languages using Regular Expressions – p.26/33

Semantics of allocation

Let X denote the set of symbols tagged with an x. Let Y

be the rest of the alphabet.The regular language

G = Y ∗·(readx·0x·Y∗)∗·(

n

write(n)x·okx·Y∗·(readx·nx·Y

∗)∗)∗

describes all those strings in which x has good-variablebehaviour.

Now we can define

([new x in P ]) = (([P ]) ∩ G) \ X.

Game Semantics of Imperative Languages using Regular Expressions – p.26/33

Semantics of allocation

Let X denote the set of symbols tagged with an x. Let Y

be the rest of the alphabet.The regular language

G = Y ∗·(readx·0x·Y∗)∗·(

n

write(n)x·okx·Y∗·(readx·nx·Y

∗)∗)∗

describes all those strings in which x has good-variablebehaviour.Now we can define

([new x in P ]) = (([P ]) ∩ G) \ X.

Game Semantics of Imperative Languages using Regular Expressions – p.26/33

A theorem

P ∼= Q ⇔ [[P ]] = [[Q]] ⇔ ([P ]) = ([Q]).

Game Semantics of Imperative Languages using Regular Expressions – p.27/33

A simple example

([while true do C]) = (([true])true · ([C]))∗ · ([true])false

= (ε · ([C]))∗ · ⊥

= ⊥

= ([Ω]).

So while true do C ∼= Ω.

Game Semantics of Imperative Languages using Regular Expressions – p.28/33

Garbage collection

([new x in M ]) = (([M ]) ∩ G) \ X

= ([M ])

since x is not free in M so no move of ([M ]) is tagged withan x.

Game Semantics of Imperative Languages using Regular Expressions – p.29/33

No snapback

Let M be P (x := 1); if !x = 1 then Ω else skip.We will show that new x in M ∼= P (Ω).

([x := 1]) = write(1)x · okx

([P (x := 1)]) = runP ·

(run1P · write(1)x · okx · done1

P )∗ ·

doneP

([!x = 1])true = readx · 1x

([!x = 1])false =∑

n6=1

readx · nx

Game Semantics of Imperative Languages using Regular Expressions – p.30/33

No snapback, continued

([if !x = 1 then Ω else skip]) = ([!x = 1])true · ([Ω])

+ ([!x = 1])false · ([skip])

= ([!x = 1])true · ⊥ + ([!x = 1])false · ε

= ([!x = 1])false

=∑

n6=1

readx · nx

Therefore

([M ]) =∑

n6=1

runP ·(run1P ·write(1)x ·okx ·done1

P )∗ ·doneP ·readx ·nx

Game Semantics of Imperative Languages using Regular Expressions – p.31/33

No snapback, concluded

([M ]) =∑

n6=1

runP ·(run1P ·write(1)x ·okx ·done1

P )∗ ·doneP ·readx ·nx

We therefore have

([M ]) ∩ G = runP · doneP · readx · 0x

([new x in M ]) = (([M ]) ∩ G) \ X = runP · doneP .

On the other hand, ([Ω]) = ⊥ so

([P (Ω)]) = runP · (run1P · ⊥ · done1

P )∗ · doneP

= runP · doneP .

Game Semantics of Imperative Languages using Regular Expressions – p.32/33

Decidability

Our theorem shows that observational equivalence for thisfragment of Idealized Algol is decidable.In principle, we could mechanize this reasoning to build amodel-checker.However, language equivalence for regular expressionswith intersection is EXPSPACE complete. . .

More research needed!

Game Semantics of Imperative Languages using Regular Expressions – p.33/33

References

[1] Dan R. Ghica and Guy McCusker. Reasoning about Ide-

alized Algol using regular languages. In Proceedings,

Twenty-Seventh International Colloquium on Automata,

Languages and Programming, pages 103–115, 2000.

[2] R.E. Milne and C. Strachey. A Theory of Programming Lan-

guage Semantics. Chapman and Hall, London, 1976.

[3] Peter Naur, J. W. Backus, F. L. Bauer, J. Green, C. Katz,

J. McCarthy, A. J. Perlis, H. Rutishauer, K. Samelson,

B. Vauquois, J. H. Wegstein, A. van Wijngaarden, and

M. Woodger. Revised report on the algorithmic language

ALGOL 60. Communications of the ACM, 6(1):1–17, Jan-

uary 1963.

[4] P. W. O’Hearn and U. Reddy. Objects, interference and

the Yoneda embedding. In M. Main and S. Brookes, ed-

itors, Mathematical Foundations of Programming Seman-

tics: Proceedings of 11th International Conference, Elec-

tronic Notes in Theoretical Computer Science. Elsevier Sci-

ence Publishers B.V., 1995.

[5] P. W. O’Hearn and R. D. Tennent. Parametricity and local

variables. Journal of the ACM, 42(3):658–709, May 1995.

[6] Peter W. O’Hearn and R. D. Tennent. Semantics of local

variables. In M. P. Fourman, P. T. Johnstone, and A. M.

33-1

Pitts, editors, Applications of Categories in Computer Sci-

ence: Proceedings of the LMS Symposium, Durham, 1991.

Cambridge University Press, 1992. LMS Lecture Notes Se-

ries, 177.

[7] A. M. Pitts. Reasoning about local variables with

operationally-based logical relations. In Proceedings,

Eleventh Annual IEEE Symposium on Logic in Computer

Science, pages 152–163. IEEE Computer Society Press,

1996.

[8] Uday S. Reddy. Global state considered unnecessary:

Object-based semantics for interference-free imperative

programs. Lisp and Symbolic Computation, 9(1), 1996.

[9] John C. Reynolds. The essence of Algol. In Proceedings

of the 1981 International Symposium on Algorithmic Lan-

guages, pages 345–372. North-Holland, 1981.

33-1

top related