delimited continuations for prolog · it allows the programmer to suspend and capture the remaining...

119

Upload: others

Post on 12-Jul-2020

12 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

Tom Schrijvers

Delimited Continuations for

Prolog

Kiel Declarative Programming Days 2013

Page 2: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

Motivation

2

Page 3: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

Delimited Continuations

3

! from Functional Programming

‣ Felleisen POPL’88

‣Danvy & Filinski LFP’90

! greatly underused and underappreciated

Page 4: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

4

Page 5: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

Prolog lacks infrastructure to capture control

patterns

5

Page 6: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

6

ModularSearch

Heuristics

PADL 2013 invited talk

Page 7: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

Existing Solutions

7

! Individual Language Extensions

! Awkward Assert/Retract scoping

! Meta-Programming / Program Transformation

• DCGs

• Extended DCGs

• Structured State threading

• Logical Loops

• ....

Page 8: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

8

Delimited Continuations

Page 9: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

ICLP 2013

9

BartDemoen

BenoitDesouter

JanWielemaker

Under consideration for publication in Theory and Practice of Logic Programming 1

Delimited Continuations for Prolog

TOM SCHRIJVERSGhent University, Belgium

(e-mail: [email protected])

BART DEMOENKU Leuven, Belgium

(e-mail: [email protected])

BENOIT DESOUTERGhent University, Belgium

(e-mail: [email protected])

JAN WIELEMAKERUniversity of Amsterdam, The Netherlands

(e-mail: [email protected])

submitted 1 January 2003; revised 1 January 2003; accepted 1 January 2003

Abstract

Delimited continuations are a famous control primitive that originates in the functional programming world.It allows the programmer to suspend and capture the remaining part of a computation in order to resume itlater. We put a new Prolog-compatible face on this primitive and specify its semantics by means of a meta-interpreter. Moreover, we establish the power of delimited continuations in Prolog with several exampledefinitions of high-level language features. Finally, we show how to easily and effectively add delimitedcontinuations support to the WAM.

KEYWORDS: delimited continuations, Prolog

1 Introduction

As a programming language Prolog is very lean. Essentially it consists of Horn clauses extendedwith mostly simple built-in predicates. While this minimality has several advantages, the lack ofinfrastructure to capture and facilitate common programming patterns can be quite frustrating.Fortunately, programmers can mitigate the tedious drudgery of encoding frequent programmingpatterns by automating them by means of Prolog’s rich meta-programming and program transfor-mation facilities. Well-known examples of these are definite clause grammars (DCGs), extendedDCGs (Roy 1989), Ciao Prolog’s structured state threading (Ivanovic et al. 2009) and logicalloops (Schimpf 2002).

However, non-local program transformations are not ideal for defining new language featuresfor several reasons. Firstly, the effort of defining a transformation is proportional to the number offeatures in the language – the more features are added, the harder it becomes. Secondly, programtransformations are fragile with respect to language evolution: they require amendments whenother features are added to the language. Thirdly, when the new feature is introduced in existing

Page 10: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

Ciao Prolog’sSignal Handling

ImplicitState

Iterators

ImplicitEnvironment

DefiniteClause

GrammarsLogging

ModularSearch

Heuristics

Iteratees Coroutines Transducers

Exceptions Tabling

Many Uses

Page 11: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

Delimited Continuations

! much easier than you think

! many applications

! just what Prolog needs

! for your language of choice too!

11

Page 12: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

Implementation

This Talk

12

Applications

Semantics

Page 13: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

Semantics

13

What are they?

Page 14: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

Two New Primitives

14

reset(Goal,Continuation,Term)

shift(Term)

Page 15: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

Plain Reset

15

main :- reset(p,_,_), writeln(c).

p :- writeln(a), writeln(b).

Page 16: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

Plain Reset

15

main :- reset(p,_,_), writeln(c).

p :- writeln(a), writeln(b).

?- main.abc

Page 17: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

Plain Reset

15

main :- reset(p,_,_), writeln(c).

p :- writeln(a), writeln(b).

?- main.abc

Page 18: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

Plain Reset

15

main :- reset(p,_,_), writeln(c).

p :- writeln(a), writeln(b).

?- main.abc

Page 19: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

Plain Reset

15

main :- reset(p,_,_), writeln(c).

p :- writeln(a), writeln(b).

?- main.abc

Page 20: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

Plain Reset

15

main :- reset(p,_,_), writeln(c).

p :- writeln(a), writeln(b).

?- main.abc

Page 21: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

Plain Reset

15

main :- reset(p,_,_), writeln(c).

p :- writeln(a), writeln(b).

?- main.abc

Page 22: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

Aborting

16

main :- reset(p,_,_), writeln(c).

p :- writeln(a), shift(_), writeln(b).

?- main.ac

Page 23: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

Aborting

16

main :- reset(p,_,_), writeln(c).

p :- writeln(a), shift(_), writeln(b).

?- main.ac

Page 24: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

Aborting

16

main :- reset(p,_,_), writeln(c).

p :- writeln(a), shift(_), writeln(b).

?- main.ac

Page 25: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

Aborting

16

main :- reset(p,_,_), writeln(c).

p :- writeln(a), shift(_), writeln(b).

?- main.ac

Page 26: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

Aborting

16

main :- reset(p,_,_), writeln(c).

p :- writeln(a), shift(_), writeln(b).

?- main.ac

Page 27: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

Aborting

16

main :- reset(p,_,_), writeln(c).

p :- writeln(a), shift(_), writeln(b).

?- main.ac

Page 28: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

Aborting

16

main :- reset(p,_,_), writeln(c).

p :- writeln(a), shift(_), writeln(b).

?- main.ac

Page 29: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

Term Passing

17

main :- reset(p,_,X), writeln(X), writeln(c).

p :- writeln(a), shift(hello), writeln(b).

?- main.ahelloc

add transitions

Page 30: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

Term Passing

17

main :- reset(p,_,X), writeln(X), writeln(c).

p :- writeln(a), shift(hello), writeln(b).

?- main.ahelloc

add transitions

Page 31: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

Term Passing

17

main :- reset(p,_,X), writeln(X), writeln(c).

p :- writeln(a), shift(hello), writeln(b).

?- main.ahelloc

add transitions

Page 32: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

Term Passing

17

main :- reset(p,_,X), writeln(X), writeln(c).

p :- writeln(a), shift(hello), writeln(b).

?- main.ahelloc

add transitions

Page 33: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

Continuation

18

main :- reset(p,Cont,_), writeln(c), call(Cont).

p :- writeln(a), shift(_), writeln(b).

?- main.acb

add transitions

Page 34: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

Repeated Call

19

main :- reset(p,Cont,_), writeln(c), call(Cont), call(Cont).

p :- writeln(a), shift(_), writeln(b).

?- main.acbb

add transitions

Page 35: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

Repeated Call

19

main :- reset(p,Cont,_), writeln(c), call(Cont), call(Cont).

p :- writeln(a), shift(_), writeln(b).

?- main.acbb

add transitions

Page 36: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

Repeated Call

19

main :- reset(p,Cont,_), writeln(c), call(Cont), call(Cont).

p :- writeln(a), shift(_), writeln(b).

?- main.acbb

add transitions

Page 37: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

Repeated Call

19

main :- reset(p,Cont,_), writeln(c), call(Cont), call(Cont).

p :- writeln(a), shift(_), writeln(b).

?- main.acbb

add transitions

Page 38: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

Repeated Call

19

main :- reset(p,Cont,_), writeln(c), call(Cont), call(Cont).

p :- writeln(a), shift(_), writeln(b).

?- main.acbb

add transitions

Page 39: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

?- reset(true,Cont,Term).Cont = 0,Term = 0.

No Shift

20

Page 40: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

?- reset(true,Cont,Term).Cont = 0,Term = 0.

No Shift

20

Page 41: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

?- shift(x).ERROR: Unhandled shift: x

No Reset

21

Page 42: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

?- shift(x).ERROR: Unhandled shift: x

No Reset

21

Page 43: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

Backtracking

22

main :- reset(p,Cont,_), writeln(c), call(Cont).

p :- shift(_), writeln(a).p :- shift(_), writeln(b).

?- main.ca ;cb

add transitions

Page 44: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

Backtracking

22

main :- reset(p,Cont,_), writeln(c), call(Cont).

p :- shift(_), writeln(a).p :- shift(_), writeln(b).

?- main.ca ;cb

add transitions

Page 45: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

Backtracking

22

main :- reset(p,Cont,_), writeln(c), call(Cont).

p :- shift(_), writeln(a).p :- shift(_), writeln(b).

?- main.ca ;cb

add transitions

Page 46: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

Backtracking

22

main :- reset(p,Cont,_), writeln(c), call(Cont).

p :- shift(_), writeln(a).p :- shift(_), writeln(b).

?- main.ca ;cb

add transitions

Page 47: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

Backtracking

22

main :- reset(p,Cont,_), writeln(c), call(Cont).

p :- shift(_), writeln(a).p :- shift(_), writeln(b).

?- main.ca ;cb

add transitions

Page 48: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

Backtracking

22

main :- reset(p,Cont,_), writeln(c), call(Cont).

p :- shift(_), writeln(a).p :- shift(_), writeln(b).

?- main.ca ;cb

add transitions

Page 49: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

Implementation

This Talk

23

Applications

Semantics

Page 50: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

Applications

24

What are they useful for?

Page 51: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

25

Implicit State

DefiniteClause

Grammars

Page 52: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

26

DefiniteClause

Grammars

Page 53: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

Definite Clause Grammars

27

ab --> [].ab --> [a], [b], ab.

?- phrase(ab,[a,b,a,b],[]).true.

Page 54: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

Program Transformation

28

ab --> [].ab --> [a], [b], ab.

ab(L,L).ab([a,b|L],T) :- ab (L,T).

static program transformation

Page 55: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

Program Transformation

28

phrase(G,L,T) :- call(G,L,T).

ab --> [].ab --> [a], [b], ab.

ab(L,L).ab([a,b|L],T) :- ab (L,T).

static program transformation

Page 56: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

Disadvantages ofApproach

! Special Syntax: a lot of refactoring effort required to introduce in large programs

! Incompatibility

! existing control operations like catch/throw

! not robust wrt syntactic extensions

! potentially quadratic effort to make all syntax extensions compatible

29

Page 57: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

Delimited Continuations to the Rescue!

30

Page 58: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

Effect Handlers

31

‣McBride: Frank language‣ Pretnar & Bauer: Eff language‣ Kammar et al. ICFP’13‣ Brady ICFP’13‣ Kiselyov et al. Haskell’13

Page 59: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

Effect Handler Approach

! Command Syntax

! Command Semantics = Handler

32

Page 60: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

DCGs

c/1 phrase/3

ab.ab :- c(a), c(b), ab.

33

?- phrase(ab,[a,b,a,b],[]).true.

Page 61: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

DCGs

c/1 phrase/3

ab.ab :- c(a), c(b), ab.

33

?- phrase(ab,[a,b,a,b],[]).true.

command

Page 62: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

DCGs

c/1 phrase/3

ab.ab :- c(a), c(b), ab.

33

?- phrase(ab,[a,b,a,b],[]).true.

command handler

Page 63: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

DCGs

c/1 phrase/3

ab.ab :- c(a), c(b), ab.

33

?- phrase(ab,[a,b,a,b],[]).true.

command handler

example code

Page 64: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

DCGs

c/1 phrase/3

ab.ab :- c(a), c(b), ab.

33

?- phrase(ab,[a,b,a,b],[]).true.

command handler

example code

example query

Page 65: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

Syntax

c(X) :- shift(c(X)).

34

Page 66: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

Semantics: Handler

phrase(G,L,T) :- reset(G,Cont,Command), ( Command = c(X) -> L = [X|NL], phrase(Cont,NL,T) ; L = T ).

35

Page 67: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

ImplicitState

Page 68: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

Implicit Stateget/1, put/1 runState/3

inc :- get(S), NS is S + 1, put(NS).

37

?- runState((inc,inc),0,S).S = 2.

Page 69: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

Command Syntax

get(S) :- shift(get(S)).put(S) :- shift(put(S)).

38

Page 70: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

Handler

runState(G,Sin,Sout) :- reset(G,Cont,Command), ( Command = get(S) -> S = Sin, runState(Cont,Sin,Sout) ; Command = put(S) -> runState(Cont,S,Sout) ; Sout = Sin ).

39

Page 71: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

Alternative Semantics!

40

Page 72: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

Implicit Stateget/1, put/1 traceState/4

inc :- get(S), NS is S + 1, put(NS).

41

?- traceState((inc,inc),0,S,T).T = [0,1], S = 2.

Page 73: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

Alternative HandlertraceState(G,Sin,Sout,Trace) :- reset(G,Cont,Command), ( Command = get(S) -> S = Sin, traceState(Cont,Sin,Sout,Trace) ; Command = put(S) -> Trace = [Sin|NTrace], traceState(Cont,S,Sout,NTrace) ; Trace = [], Sout = Sin ).

42

Page 74: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

Compositional Handlers

!

43

Page 75: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

Example

inc :- get(S), NS is S + 1, put(NS).

ab.ab :- c(a), c(b), inc, ab.

44

?- runState( phrase(ab,[a,b,a,b],[]), 0,S).S = 2.

Page 76: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

Example

inc :- get(S), NS is S + 1, put(NS).

ab.ab :- c(a), c(b), inc, ab.

45

?- phrase( runState(ab,0,S), [a,b,a,b],[]).S = 2.

Page 77: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

Compositional Handlerphrase(G,L,T) :- reset(G,Cont,Command), ( Command = c(X) -> L = [X|NL], phrase(Cont,NL,T) ; Command = 0 -> L = T ; shift(Command), phrase(Cont,L,T) ).

46

Page 78: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

Ciao Prolog’sSignal Handling

Iterators

ImplicitEnvironment

DefiniteClause

Grammars

Logging

Iteratees Coroutines Transducers

Exceptions

Many UsesImplicitState

Page 79: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

Implementation

This Talk

48

Applications

Semantics

Page 80: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

Implementation

49

How to implement them?

Page 81: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

Meta-Interpreter

50

Page 82: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

Vanilla Interpreter

eval(true) :- !.eval((G1,G2)) :- !, eval(G1), eval(G2).eval(Goal) :- clause(Goal,Body), eval(Body).

51

Page 83: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

D.C. Interpreter

eval(+Goal,-Status)

Status:- ok- shift(Term,Cont)

52

Page 84: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

D.C. Interpretereval(shift(Term),Status) :- !, Status = shift(Term,true).eval(reset(G,Cont,Term),Status) :- !, Status = ok, eval(G,S), ( S == ok -> Cont = 0, Term = 0 ; S = shift(Term,Cont) ).

53

Page 85: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

D.C. Interpretereval(true,Status) :- !, Status = ok.eval((G1,G2),Status) :- !, eval(G1,S1), ( S1 == ok -> eval(G2,Status) ; S1 = shift(Term,Cont) -> NCont = (Cont,G2), Status = shift(Term,NCont) ).

54

Page 86: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

D.C. Interpreter

eval(Goal,Status) :- !, clause(Goal,Body), eval(Body,Status).

55

Page 87: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

Meta-Interpreter

! easy to define and understand

! executable specification

! does not scale well to other features

! poor performance

56

Page 88: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

WAMWarren Abstract Machine

57

Page 89: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

Catch & Throw

1. unify a copy of Term with Ball

2. unwind environment & choice point stacks up to catch/3

3. Handler is called before control goes to ...

58

Goal :- ... throw(Term) ...

?- catch(Goal,Ball,Handler), ...

Page 90: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

Reset & Shift

1. unify Term with Ball

2. leave the stacks intact

3. unify Cont with a copy of the environment up to reset/3

4. Control goes to ...59

Goal :- ... shift(Term) ...

?- reset(Goal,Cont,Ball), ...

Page 91: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

Four Issues

1. up to reset/3

2. how to copy (a delimited part of) the environment stack

3. how to use this delimited continuation

4. fineprint

60

Page 92: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

Up to reset/3

61same principle as catch/throw

Term

Cont

Goal

marker

prevE

contCP

reset’s environment

Page 93: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

Up to reset/3

62

Term

Cont

Goal

marker

prevE

contCP

...

contCP

...

...

.

contCP

contCP

prevE

prevE

prevE

. E

Page 94: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

Continuation Term

63

Y

X

prevE

contCP

Environment Stack

Heap

$cont$(ContCP,[X,Y])

a(X) :- b, c(X,Y), shift(1), d(Y).

Page 95: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

Reified Environment

64

Y

X

prevE

contCP

Environment Stack

Heap

$cont$(ContCP,[X,Y])

Page 96: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

Environment Chain

65

Environment Stack Heap

[$cont$(CP1,Vars1), $cont$(CP2,Vars2), $cont$(CP3,Vars3)]Vars2

CP1

Vars3

Vars1

.

CP2

CP3

prevE

prevE

prevE

Page 97: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

Callable Continuation Term

66

Cont = call_continuation( [$cont$(CP1,Vars1), $cont$(CP2,Vars2), $cont$(CP3,Vars3)])

call_continuation([]).call_continuation([Chunk|Chunks]) :- call_chunk(Chunk), call_continuation(Chunks).

Page 98: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

Performance

! Not the main focus! Pretty Decent

67

Page 99: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

Shift Runtime (ms)

linear in delimited stack depth68

NativeNative TransformedTransformed

Depth hProlog SWI-Prolog hProlog SWI-Prolog

5,000 64 1,965 164 505

10,000 128 3,950 328 1,028

20,000 268 8,388 664 2,037

main :- reset(p1,_,_).

dummy.

p1 :- p2, dummy.p2 :- p3, dummy....p5000 :- shift(_), dummy.

Page 100: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

Shift Runtime (ms)

linear in delimited stack depth68

NativeNative TransformedTransformed

Depth hProlog SWI-Prolog hProlog SWI-Prolog

5,000 64 1,965 164 505

10,000 128 3,950 328 1,028

20,000 268 8,388 664 2,037

Page 101: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

Shift Runtime (ms)

linear in delimited stack depth68

NativeNative TransformedTransformed

Depth hProlog SWI-Prolog hProlog SWI-Prolog

5,000 64 1,965 164 505

10,000 128 3,950 328 1,028

20,000 268 8,388 664 2,037

specialization of meta-interpreter

Page 102: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

Shift Runtime (ms)

linear in delimited stack depth68

NativeNative TransformedTransformed

Depth hProlog SWI-Prolog hProlog SWI-Prolog

5,000 64 1,965 164 505

10,000 128 3,950 328 1,028

20,000 268 8,388 664 2,037

specialization of meta-interpreter

Page 103: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

Shift Runtime (ms)

linear in delimited stack depth68

NativeNative TransformedTransformed

Depth hProlog SWI-Prolog hProlog SWI-Prolog

5,000 64 1,965 164 505

10,000 128 3,950 328 1,028

20,000 268 8,388 664 2,037

Page 104: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

Shift Runtime (ms)

linear in delimited stack depth68

NativeNative TransformedTransformed

Depth hProlog SWI-Prolog hProlog SWI-Prolog

5,000 64 1,965 164 505

10,000 128 3,950 328 1,028

20,000 268 8,388 664 2,037

Page 105: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

Shift Runtime (ms)

linear in delimited stack depth68

NativeNative TransformedTransformed

Depth hProlog SWI-Prolog hProlog SWI-Prolog

5,000 64 1,965 164 505

10,000 128 3,950 328 1,028

20,000 268 8,388 664 2,037

WAMarchitecture

Page 106: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

Shift Runtime (ms)

linear in delimited stack depth68

NativeNative TransformedTransformed

Depth hProlog SWI-Prolog hProlog SWI-Prolog

5,000 64 1,965 164 505

10,000 128 3,950 328 1,028

20,000 268 8,388 664 2,037

WAMarchitecture

Page 107: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

Shift Runtime (ms)

linear in delimited stack depth68

NativeNative TransformedTransformed

Depth hProlog SWI-Prolog hProlog SWI-Prolog

5,000 64 1,965 164 505

10,000 128 3,950 328 1,028

20,000 268 8,388 664 2,037

Page 108: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

Shift Runtime (ms)

linear in delimited stack depth68

NativeNative TransformedTransformed

Depth hProlog SWI-Prolog hProlog SWI-Prolog

5,000 64 1,965 164 505

10,000 128 3,950 328 1,028

20,000 268 8,388 664 2,037

Page 109: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

Shift Runtime (ms)

linear in delimited stack depth68

NativeNative TransformedTransformed

Depth hProlog SWI-Prolog hProlog SWI-Prolog

5,000 64 1,965 164 505

10,000 128 3,950 328 1,028

20,000 268 8,388 664 2,037

ZIParchitecture

Page 110: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

Shift Runtime (ms)

linear in delimited stack depth68

NativeNative TransformedTransformed

Depth hProlog SWI-Prolog hProlog SWI-Prolog

5,000 64 1,965 164 505

10,000 128 3,950 328 1,028

20,000 268 8,388 664 2,037

ZIParchitecture

Page 111: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

Shift Runtime (ms)

linear in delimited stack depth68

NativeNative TransformedTransformed

Depth hProlog SWI-Prolog hProlog SWI-Prolog

5,000 64 1,965 164 505

10,000 128 3,950 328 1,028

20,000 268 8,388 664 2,037

Page 112: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

hProloghProlog

Depth ContinuationCall

Meta-Call

5,000 248 398

10,000 492 796

20,000 992 1,586

call(Cont) in the WAM

69

Page 113: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

hProloghProlog

Depth ContinuationCall

Meta-Call

5,000 248 398

10,000 492 796

20,000 992 1,586

call(Cont) in the WAM

69

call((dummy,dummy,...,dummy))

Page 114: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

hProloghProlog

Depth ContinuationCall

Meta-Call

5,000 248 398

10,000 492 796

20,000 992 1,586

call(Cont) in the WAM

69

call((dummy,dummy,...,dummy))

Page 115: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

hProloghProlog

Depth ContinuationCall

Meta-Call

5,000 248 398

10,000 492 796

20,000 992 1,586

call(Cont) in the WAM

69

linear and 1.6x faster than meta-call

Page 116: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

Summary

70

Page 117: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

! simple Prolog interface for delimited continuations

! many examples of applications

! lightweight implementation in the WAM

71

Summary

Page 118: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

Ongoing/Future Work! additional features

! prompts

! hierarchies

! failure continuation

! new applications! tabling

! implementation improvements! program analysis (e.g., abstract interpretation)

! program specialization

72

Page 119: Delimited Continuations for Prolog · It allows the programmer to suspend and capture the remaining part of a computation in order to resume it later. We put a new Prolog-compatible

73

Thank You!