On space efficiency of Krivine’s abstract machine and ...terui/space2.pdf · On space...

38
On space efficiency of Krivine’s abstract machine and Hyland-Ong games Kazushige Terui [email protected] National Institute of Informatics, Tokyo Laboratoire d’Informatics de Paris Nord, CNRS 13/02/08, LIPN – p.1/28

Transcript of On space efficiency of Krivine’s abstract machine and ...terui/space2.pdf · On space...

Page 1: On space efficiency of Krivine’s abstract machine and ...terui/space2.pdf · On space efficiency of Krivine’s abstract machine and Hyland-Ong games Kazushige Terui terui@nii.ac.jp

On space efficiency ofKrivine’s abstract machine and

Hyland-Ong gamesKazushige Terui

[email protected]

National Institute of Informatics, Tokyo

Laboratoire d’Informatics de Paris Nord, CNRS

13/02/08, LIPN – p.1/28

Page 2: On space efficiency of Krivine’s abstract machine and ...terui/space2.pdf · On space efficiency of Krivine’s abstract machine and Hyland-Ong games Kazushige Terui terui@nii.ac.jp

ICC

Intensional Computational Complexity (ICC)

algorithms (eg. � terms)

evaluation mechanisms (eg. �-reduction)

13/02/08, LIPN – p.2/28

Page 3: On space efficiency of Krivine’s abstract machine and ...terui/space2.pdf · On space efficiency of Krivine’s abstract machine and Hyland-Ong games Kazushige Terui terui@nii.ac.jp

Space sensitive Turing machines

Input tape (read only)

Work tapes (read/write)

Output tape (write only)

Finite control

Space = the num of cells used on worktapes.

The input and output can be significantly larger than the space:

If � works in space ���� then the output is of size ��������.

Given two Turing machines � , � , we want to compose them.

How?

13/02/08, LIPN – p.3/28

Page 4: On space efficiency of Krivine’s abstract machine and ...terui/space2.pdf · On space efficiency of Krivine’s abstract machine and Hyland-Ong games Kazushige Terui terui@nii.ac.jp

Space sensitive Turing machines

Functional composition is not good:

If ��� work in space ����� ���, then � �� works in space

������ � ������� � ������.

In particular, logspace functions do not compose.

Composition must be interactive: � � � works in

������ � ��������.

From functional to interactive computation!

13/02/08, LIPN – p.4/28

Page 5: On space efficiency of Krivine’s abstract machine and ...terui/space2.pdf · On space efficiency of Krivine’s abstract machine and Hyland-Ong games Kazushige Terui terui@nii.ac.jp

Towards a logical theory of computational complexity

Shortcomings of Turing Machines:

Poor data structures (only tapes)

Poor control mechanisms (only transitions)

Ad hoc constructions: no canonical way of composition

Results in lack of beautiful mathematical theory

Logic and lambda calculus

Rich data structures and controls

Canonical composition Æ � � �� ������

Hope to make complexity theory more mathematical

13/02/08, LIPN – p.5/28

Page 6: On space efficiency of Krivine’s abstract machine and ...terui/space2.pdf · On space efficiency of Krivine’s abstract machine and Hyland-Ong games Kazushige Terui terui@nii.ac.jp

Space in lambda calculus

Composition does not preserve good space bounds when

using �-reduction, because �-reduction is not interactive!

Interactive evaluation mechanisms

Geometry of Interaction

Krivine’s abstract machine (KAM)

Game semantics

13/02/08, LIPN – p.6/28

Page 7: On space efficiency of Krivine’s abstract machine and ...terui/space2.pdf · On space efficiency of Krivine’s abstract machine and Hyland-Ong games Kazushige Terui terui@nii.ac.jp

KAM

KAM implements weak head linear reduction:

�� � � � �� � � � ���� � � � � � � �� �� � � � �� � � � ��� � � � � � �

GoI with “jumps” under � � � �����Æ�� (Danos-Regnier 94)

Syntax:

Terms � ��� � � �� �� � ����

Environments � ��� �� �� ����� � �� �� ����

Closures ��Stacks � ��� ���� � � � � � ����

States � ��� ���� ��

13/02/08, LIPN – p.7/28

Page 8: On space efficiency of Krivine’s abstract machine and ...terui/space2.pdf · On space efficiency of Krivine’s abstract machine and Hyland-Ong games Kazushige Terui terui@nii.ac.jp

KAM

Initial state: ��� ����

Transitions

���� �� �� ������ �� if � � ������

������� �� �� ���� �� � ��

���� ���� ��� � �� �� ���� �� ���� ��

Terminations

���� �� with � � ������ — Success with output �

���� ���� ���� — Failure

Not space efficient: � contains a lot of redundant bindings.

13/02/08, LIPN – p.8/28

Page 9: On space efficiency of Krivine’s abstract machine and ...terui/space2.pdf · On space efficiency of Krivine’s abstract machine and Hyland-Ong games Kazushige Terui terui@nii.ac.jp

Optimized KAM

Optimized transitions

���� �� �� ������ �� if � � ������

������� �� �� �������� ���� � �� if � � ������

������� �� �� �������� ������ � �� otherwise

���� ���� ��� � �� �� ���� �� ���� ��

��� is a restriction of � to the free variables of �.

13/02/08, LIPN – p.9/28

Page 10: On space efficiency of Krivine’s abstract machine and ...terui/space2.pdf · On space efficiency of Krivine’s abstract machine and Hyland-Ong games Kazushige Terui terui@nii.ac.jp

Example

Suppose ��� ���� ��� ��� �� ����, � � ��� �����.

����� ����

13/02/08, LIPN – p.10/28

Page 11: On space efficiency of Krivine’s abstract machine and ...terui/space2.pdf · On space efficiency of Krivine’s abstract machine and Hyland-Ong games Kazushige Terui terui@nii.ac.jp

Example

Suppose ��� ���� ��� ��� �� ����, � � ��� �����.

����� ����

���� ��

13/02/08, LIPN – p.10/28

Page 12: On space efficiency of Krivine’s abstract machine and ...terui/space2.pdf · On space efficiency of Krivine’s abstract machine and Hyland-Ong games Kazushige Terui terui@nii.ac.jp

Example

Suppose ��� ���� ��� ��� �� ����, � � ��� �����.

����� ����

���� ��

��� � � ��

13/02/08, LIPN – p.10/28

Page 13: On space efficiency of Krivine’s abstract machine and ...terui/space2.pdf · On space efficiency of Krivine’s abstract machine and Hyland-Ong games Kazushige Terui terui@nii.ac.jp

Example

Suppose ��� ���� ��� ��� �� ����, � � ��� �����.

����� ����

���� ��

��� � � ��

��� ������ �� �� ��

13/02/08, LIPN – p.10/28

Page 14: On space efficiency of Krivine’s abstract machine and ...terui/space2.pdf · On space efficiency of Krivine’s abstract machine and Hyland-Ong games Kazushige Terui terui@nii.ac.jp

Example

Suppose ��� ���� ��� ��� �� ����, � � ��� �����.

����� ����

���� ��

��� � � ��

��� ������ �� �� ��

������� �� ��� �� �� ����

13/02/08, LIPN – p.10/28

Page 15: On space efficiency of Krivine’s abstract machine and ...terui/space2.pdf · On space efficiency of Krivine’s abstract machine and Hyland-Ong games Kazushige Terui terui@nii.ac.jp

Example

Suppose ��� ���� ��� ��� �� ����, � � ��� �����.

����� ����

���� ��

��� � � ��

��� ������ �� �� ��

������� �� ��� �� �� ����

�� � �� �� ��� �� ��� �� ��

13/02/08, LIPN – p.10/28

Page 16: On space efficiency of Krivine’s abstract machine and ...terui/space2.pdf · On space efficiency of Krivine’s abstract machine and Hyland-Ong games Kazushige Terui terui@nii.ac.jp

Example

Suppose ��� ���� ��� ��� �� ����, � � ��� �����.

����� ����

���� ��

��� � � ��

��� ������ �� �� ��

������� �� ��� �� �� ����

�� � �� �� ��� �� ��� �� ��

��� ��� �� ��� �� ��

13/02/08, LIPN – p.10/28

Page 17: On space efficiency of Krivine’s abstract machine and ...terui/space2.pdf · On space efficiency of Krivine’s abstract machine and Hyland-Ong games Kazushige Terui terui@nii.ac.jp

Example

Suppose ��� ���� ��� ��� �� ����, � � ��� �����.

����� ����

���� ��

��� � � ��

��� ������ �� �� ��

������� �� ��� �� �� ����

�� � �� �� ��� �� ��� �� ��

��� ��� �� ��� �� ��

��� �� ��� �� ��� �� ��

13/02/08, LIPN – p.10/28

Page 18: On space efficiency of Krivine’s abstract machine and ...terui/space2.pdf · On space efficiency of Krivine’s abstract machine and Hyland-Ong games Kazushige Terui terui@nii.ac.jp

Example

Suppose ��� ���� ��� ��� �� ����, � � ��� �����.

����� ����

���� ��

��� � � ��

��� ������ �� �� ��

������� �� ��� �� �� ����

�� � �� �� ��� �� ��� �� ��

��� ��� �� ��� �� ��

��� �� ��� �� ��� �� ��

���� �� ��� �� �� ����

13/02/08, LIPN – p.10/28

Page 19: On space efficiency of Krivine’s abstract machine and ...terui/space2.pdf · On space efficiency of Krivine’s abstract machine and Hyland-Ong games Kazushige Terui terui@nii.ac.jp

Example

Suppose ��� ���� ��� ��� �� ����, � � ��� �����.

����� ����

���� ��

��� � � ��

��� ������ �� �� ��

������� �� ��� �� �� ����

�� � �� �� ��� �� ��� �� ��

��� ��� �� ��� �� ��

��� �� ��� �� ��� �� ��

���� �� ��� �� �� ����

�� � �� �� ��13/02/08, LIPN – p.10/28

Page 20: On space efficiency of Krivine’s abstract machine and ...terui/space2.pdf · On space efficiency of Krivine’s abstract machine and Hyland-Ong games Kazushige Terui terui@nii.ac.jp

Example

Suppose ��� ���� ��� ��� �� ����, � � ��� �����.

����� ����

���� ��

��� � � ��

��� ������ �� �� ��

������� �� ��� �� �� ����

�� � �� �� ��� �� ��� �� ��

��� ��� �� ��� �� ��

��� �� ��� �� ��� �� ��

���� �� ��� �� �� ����

�� � �� �� ��

��� ��

13/02/08, LIPN – p.10/28

Page 21: On space efficiency of Krivine’s abstract machine and ...terui/space2.pdf · On space efficiency of Krivine’s abstract machine and Hyland-Ong games Kazushige Terui terui@nii.ac.jp

Size of a state

Subterm property: if ��� ���� ��� �, all terms occurring in �

are subterms of �.

Allows us to think of each state as made of pointers rather than

actual terms.

�� = the number of pointers in �:

�� � ��� � � � �� ��� � � if � � �� �� ����� � �� �� ����

�� � ��� � � � �� ��� � � if ���� � � � � � ����

����� �� � ��� �� � �

In particular, ���� ���� � �.

KAM evaluates � with � pointers if for any intermediate state

��� ���� ��� �, �� �.13/02/08, LIPN – p.11/28

Page 22: On space efficiency of Krivine’s abstract machine and ...terui/space2.pdf · On space efficiency of Krivine’s abstract machine and Hyland-Ong games Kazushige Terui terui@nii.ac.jp

TM space-efficiently simulates KAM

Naively, TM requires of � � � overhead to simulate KAM:

if KAM evaluates � with � pointers, then ���� works in

space ��� � � � ����.

We are interested in terms ��, where � (program) is fixed while

� (input) is varying.

Consider a huge alphabet

� � �� � � is a subterm occ. of �� ��� ��

13/02/08, LIPN – p.12/28

Page 23: On space efficiency of Krivine’s abstract machine and ...terui/space2.pdf · On space efficiency of Krivine’s abstract machine and Hyland-Ong games Kazushige Terui terui@nii.ac.jp

TM space-efficiently simulates KAM

� satisfies the input condition if in any intermediate state

���� ���� ��� �, the number of pointers pointing subterms of �

is constant (independent of �).

Analogy: the num of heads on input tape is fixed.

Theorem: There is a TM � which simulates KAM linearly:

if KAM evaluates �� with ������ pointers, � satisfies the input

condition and ���� � � � �, then ���� works in space ���������.

13/02/08, LIPN – p.13/28

Page 24: On space efficiency of Krivine’s abstract machine and ...terui/space2.pdf · On space efficiency of Krivine’s abstract machine and Hyland-Ong games Kazushige Terui terui@nii.ac.jp

Data structures

Booleans: � � ��� �, � � ��� �

Scott numerals: � for � � ��� ���

� ������� ��

� �� � ������� ���

� �� � ������� ���

Purely linear. Successor, predecessor and conditional are also

linear. Useful for worktapes.

What are read-only/write-only tapes?

They should be interactive entities (analogy: Book vs Dialogue)

13/02/08, LIPN – p.14/28

Page 25: On space efficiency of Krivine’s abstract machine and ...terui/space2.pdf · On space efficiency of Krivine’s abstract machine and Hyland-Ong games Kazushige Terui terui@nii.ac.jp

Data structures

Databases: For � � ��� ����

, define a term � � Word � Bool s.t.

Given � � ��� ���, �� returns the �th bit of �.

� � �� ���� if � � ! � ��� ��

� �� ������ if �� � !�� � � � !������,

�� � !���� � � � !�, � � ����

13/02/08, LIPN – p.15/28

Page 26: On space efficiency of Krivine’s abstract machine and ...terui/space2.pdf · On space efficiency of Krivine’s abstract machine and Hyland-Ong games Kazushige Terui terui@nii.ac.jp

KAM space-efficiently simulates TM

Theorem: For any TM � , there is a term �� s.t.

Given � � ��� ���, ��� �� �� returns �� or �� according to

the output of ����.

When � works in space ���� � � � �, KAM evaluates

��� �� �� with ������� pointers.

Now the space hierarchy theorem implies

There is no TM � that evaluates ��� in space ��������

with " # �.

Corollary: There is no evaluator that is uniformly (i.e. for all

terms) and significantly (i.e. super-linearly) more

space-efficient than KAM.

13/02/08, LIPN – p.16/28

Page 27: On space efficiency of Krivine’s abstract machine and ...terui/space2.pdf · On space efficiency of Krivine’s abstract machine and Hyland-Ong games Kazushige Terui terui@nii.ac.jp

Composition

Let ��� be transducer TMs which work in space ����� ���

and produce outputs of size ����� and �����.

�� � �Word � Bool� �� �Word � Bool�

� � �Word � Bool� �� �Word � Bool�

�� Æ � � �Word � Bool� �� �Word � Bool�

�� Æ �� ��� �� �� returns the �th bit of � Æ����.

KAM evaluates it with �������� � ������� pointers, where �� is

the output of ����.

KAM + canonical composition simulates the interactive

composition of TMs!

13/02/08, LIPN – p.17/28

Page 28: On space efficiency of Krivine’s abstract machine and ...terui/space2.pdf · On space efficiency of Krivine’s abstract machine and Hyland-Ong games Kazushige Terui terui@nii.ac.jp

Moral

Turing Machines �-calculus

Composition non canonical canonical

Evaluation canonical non canonical

In �-calculus, TIME-SPACE tradeoff shows up at the stage of

evaluation:

KAM is space-efficient

CBV seems to be time-efficient (cf. Dal Lago-Martini).

Work to be done: identify a subclass of �-terms (via a type

system) on which space bounds are well preserved by KAM

composition.

13/02/08, LIPN – p.18/28

Page 29: On space efficiency of Krivine’s abstract machine and ...terui/space2.pdf · On space efficiency of Krivine’s abstract machine and Hyland-Ong games Kazushige Terui terui@nii.ac.jp

Warning

There is no uniformly more efficient evaluator than KAM.

KAM is like a student Bob with score

Math Science Grammer Latin Music

1 1 1 1 10

Although there is no uniformly better student than Bob, this

does not mean Bob is the best student.

13/02/08, LIPN – p.19/28

Page 30: On space efficiency of Krivine’s abstract machine and ...terui/space2.pdf · On space efficiency of Krivine’s abstract machine and Hyland-Ong games Kazushige Terui terui@nii.ac.jp

From KAM to HO games

Empirically, KAM is good at “tall” terms:

�� � ���� ��������� ���KAM evaluates �� with ���� pointers.

KAM is not good at “wide” terms:

�� � ���� � � � ��� ��� �� � � � ��

KAM evaluates �� with ���� pointers.

For simply typed terms with unbounded width and fixed rank,

HO games seem to be more efficient.

13/02/08, LIPN – p.20/28

Page 31: On space efficiency of Krivine’s abstract machine and ...terui/space2.pdf · On space efficiency of Krivine’s abstract machine and Hyland-Ong games Kazushige Terui terui@nii.ac.jp

HO games

A fully abstract model of PCF (Hyland-Ong 00). Successfully

applied to various programming languages.

Useful for compositional and higher-order model checking

(Ghica-Mckusker 00, Ong 04, etc.)

Types = Arenas

Terms = Strategies

Computation = play

Play = interactive composition of two strategies

Composition is effective (cf. Ong 04). Corresponds to Pointer

Abstract Machine (Danos-Herbelin-Regnier 96).

13/02/08, LIPN – p.21/28

Page 32: On space efficiency of Krivine’s abstract machine and ...terui/space2.pdf · On space efficiency of Krivine’s abstract machine and Hyland-Ong games Kazushige Terui terui@nii.ac.jp

Ranks and complexity

The rank of a type: $%�&�'� � �,

$%�&�� � �� � �%��$%�&��� � �� $%�&����A term is at rank � if all of its subterms have rank � types.

Rank Quantative Qualitative

1 boolean formulas (open) addition

�( � ��� (����

2 boolean circuits multiplication

��� ) ��� � ������ ���� *��� � �

3 QBFs �� ) ��� � exponentiation

�������� �������������������������� ��� �����������

13/02/08, LIPN – p.22/28

Page 33: On space efficiency of Krivine’s abstract machine and ...terui/space2.pdf · On space efficiency of Krivine’s abstract machine and Hyland-Ong games Kazushige Terui terui@nii.ac.jp

Ranks and complexity

Normalization problem:

Given two terms �� � where � is normal,

does � reduce to �?

(Schubert 2001) shows that this problem is

1. in �+�,�� � � �� for rank 1 terms

2. P-complete for rank 2 terms

3. PSPACE-complete for rank 3 terms

Hardness is easy. Membership is difficult, since �-reduction

does not work.

Schubert uses graph rewriting. We use HO games.

13/02/08, LIPN – p.23/28

Page 34: On space efficiency of Krivine’s abstract machine and ...terui/space2.pdf · On space efficiency of Krivine’s abstract machine and Hyland-Ong games Kazushige Terui terui@nii.ac.jp

HO games for simple types

Arena for type �:

Moves = occurrences of atomic types in �

P-moves = negative occurrences

O-moves = positive occurrences

Initial move = �%!-���

�%!-��� � �%!-�.� where . is an immediate subformula of �

Legal play: a PO-alternating pointing sequence like

/ � ���� � � ��� � � ��� � � �

�� is the initial O-move

each �� (� !) is justified by a preceding move ��

(�� � ��, � 0 # !).13/02/08, LIPN – p.24/28

Page 35: On space efficiency of Krivine’s abstract machine and ...terui/space2.pdf · On space efficiency of Krivine’s abstract machine and Hyland-Ong games Kazushige Terui terui@nii.ac.jp

Let’s play the game!

P-view: a legal play in which any O-move is justfied by the

move immediately before.

Given an arbitrary legal play /, a P-view �/� can be extracted.

Innocent strategy (as view function): a P-deterministic tree

made of P-ending P-views.

Every closed normal term � � � can be interpreted by an

innocent strategy.

HO-dialogue: Given innocent strategies 1 � � � � and 2 � �,

1. Start with the initial move �� � �%!-���

2. Expand an odd length play / to / �� such that �/� �� � 1

3. Expand an even length play �� � / to �� � / �� such that

�/� �� � 2

13/02/08, LIPN – p.25/28

Page 36: On space efficiency of Krivine’s abstract machine and ...terui/space2.pdf · On space efficiency of Krivine’s abstract machine and Hyland-Ong games Kazushige Terui terui@nii.ac.jp

A lemma in game semantics

Difficulty: HO game is history sensitive. But recording the

whole history leads to TIME = SPACE.

Lemma (taught by P. Boude): For any legal play satisfying P-

and O-visibility

/ � � � ���� � � � � ��� � � � � � �

if �� justifies no moves, it never happens that

/ � � � ���� � � � � ��� � � � � � �

So one can safely contract / to/� � � � ����� � � �

and continue the play.

13/02/08, LIPN – p.26/28

Page 37: On space efficiency of Krivine’s abstract machine and ...terui/space2.pdf · On space efficiency of Krivine’s abstract machine and Hyland-Ong games Kazushige Terui terui@nii.ac.jp

Complexity of Rank 3 games

Theorem: Given a term � at rank 3 (which is an applicative

combination of closed normal terms), the length of play can be

kepth within ������.

Corollary: One can determine the head variable of �����

in polynomial space via HO games.

Work to be done: What about rank � �? Do we have a

hierarchy result as in Kristiansen’s talk?

13/02/08, LIPN – p.27/28

Page 38: On space efficiency of Krivine’s abstract machine and ...terui/space2.pdf · On space efficiency of Krivine’s abstract machine and Hyland-Ong games Kazushige Terui terui@nii.ac.jp

Conclusion

Lambda calculus admits a canonical composition � �

KAM is optimal for simulating TM.

Composition works nicely with KAM.

(KAM allows natural programming eg. boolean matrix closure)

HO games seem to be good at least for rank bounded terms.

Good understanding of game semantics leads to clever space

management. Game semantics may implicitly explain

complexity.

13/02/08, LIPN – p.28/28