Relating Models of Backtracking Mitchell Wand and Dale Vaillancourt Northeastern University
Relating Models of Backtracking – p.1/28
Outline
Introduce backtracking computation. Two well-known models. A monadic framework for backtracking.
How are the monads related? Past attempts. Our solution. Relate an implementation of one model to both models. Conclude.
Relating Models of Backtracking – p.2/28
Backtracking computation
Simply-typed λ-calculus with backtracking (and constants). Might write something like: nats-from n = n ∨ nats-from(n + 1) nats = nats-from 0
How might we model such a language?
Relating Models of Backtracking – p.3/28
Two models
Represent a backtracking computation as a stream of answers. failS = hi natsS = h0, 1, 2, · · ·i
Relating Models of Backtracking – p.4/28
Two models
Represent a backtracking computation as a stream of answers. failS = hi natsS = h0, 1, 2, · · ·i
Represent a backtracking computation as a procedure that consumes two values. fail
K
= λκφ.φ
natsK = λκφ.κ 0 ((nats-from 1)K κ φ)
Relating Models of Backtracking – p.4/28
Two models
Represent a backtracking computation as a stream of answers. failS = hi natsS = h0, 1, 2, · · ·i
Represent a backtracking computation as a procedure that consumes two values. fail
K
= λκφ.φ
natsK = λκφ.κ 0 ((nats-from 1)K κ φ)
How are these models related?
Relating Models of Backtracking – p.4/28
The models’ relationship Maybe the two-continuation terms arise by . . .
Church-encoding the streams?
Scott-encoding the streams?
Final algebra-encoding the streams?
Relating Models of Backtracking – p.5/28
They’re backtracking monads
Hughes ’95 defines a backtracking monad and notes that both models are backtracking monads. unit : α → Tα bind : Tβ → (β → Tα) → Tα disj : Tα → Tα → Tα fail : Tα
Relating Models of Backtracking – p.6/28
They’re backtracking monads We have some new monad laws: disj M fail = M disj fail M = M
Relating Models of Backtracking – p.7/28
They’re backtracking monads We have some new monad laws: disj M fail = M disj fail M = M disj (disj M1 M2 ) M3 = disj M1 (disj M2 M3 )
Relating Models of Backtracking – p.7/28
They’re backtracking monads We have some new monad laws: disj M fail = M disj fail M = M disj (disj M1 M2 ) M3 = disj M1 (disj M2 M3 ) bind (disj M1 M2 ) M3 = disj (bind M1 M3 ) (bind M2 M3 )
Relating Models of Backtracking – p.7/28
They’re backtracking monads We have some new monad laws: disj M fail = M disj fail M = M disj (disj M1 M2 ) M3 = disj M1 (disj M2 M3 ) bind (disj M1 M2 ) M3 = disj (bind M1 M3 ) (bind M2 M3 ) bind fail M = fail
Relating Models of Backtracking – p.7/28
Monadic Semantics
Relating Models of Backtracking – p.8/28
Two Monads The stream monad S: S
unit v = hvi bindS c f = flatten(map f c) disjS s t = s ˆ t failS = hi
Relating Models of Backtracking – p.9/28
Two Monads The two-continuation monad K: unitK v = λκφ.κvφ bindK c f = λκφ.c(λaφ.( f a)κφ)φ disjK c d = λκφ.cκ(dκφ) failK = λκφ.φ
Relating Models of Backtracking – p.10/28
Even more monads . . .
Backtracking Monad Transformers (Hinze 1999). The Algebra of Logic Programming, Embedding Prolog into Haskell (Seres & Spivey 1999). Typed Logical Variables in Haskell (Claessen et al. 2000).
Relating Models of Backtracking – p.11/28
The monads’ relationship
Hughes derives the two-continuation model as an “optimized” representation of streams. Unfortunately the derivation only works in one direction. Hinze’s extension to monad transformers suffers the same difficulty.
Relating Models of Backtracking – p.12/28
Danvy, Grobauer, Rhiger [2001] Their insight is that we really want: MK cons nil = MS
They connect a stream with its encoding using a monad morphism.
They claim to use a Church encoding.
Relating Models of Backtracking – p.13/28
DGR’s Encoding Our insight is that they encode: ha1 , a2 , · · · , an i as λκ.(κ a1 ) ◦ (κ a2 ) ◦ · · · ◦ (κ an )
Relating Models of Backtracking – p.14/28
Two Issues
Let M = unit(λx.(unit 42)). Then MK cons nil = hλx.(λκ.κ42)i MS = hλx.h42ii When M involves higher-order quantities, MK cons nil 6= MS
They do not consider recursion.
Relating Models of Backtracking – p.15/28
Relating the higher-order values We need Rα ⊆ αS × αK for each type α such that: f Rα→β g iff a Rα b =⇒ ( f a) R β (gb) A type-indexed family of relations satisfying this property is called a logical relation.
Relating Models of Backtracking – p.16/28
Our Logical Relation Define Rα ⊆ αS × αK : At scalar type σ, Rσ is the identity.
Relating Models of Backtracking – p.17/28
Our Logical Relation Define Rα ⊆ αS × αK : At scalar type σ, Rσ is the identity.
f Rα→β g iff a Rα b =⇒ ( f a) R β (gb)
Relating Models of Backtracking – p.17/28
Our Logical Relation Define Rα ⊆ αS × αK : At scalar type σ, Rσ is the identity.
f Rα→β g iff a Rα b =⇒ ( f a) R β (gb)
At computation type Tα: a1 Rα b1 , . . . , an Rα bn ha1 , . . . , an i RTα λκ.(κb1 ) ◦ · · · ◦ (κbn )
Relating Models of Backtracking – p.17/28
Relating Infinite Computations
Require ⊥TαS RTα ⊥TαK .
Close RTα under limits of ω-chains in TαS × TαK .
Relating Models of Backtracking – p.18/28
A Theorem Theorem. If M : α, then MS Rα MK . Proof: For each constant c : α, we must show cS Rα cK Then the result follows from the fundamental theorem of logical relations.
Relating Models of Backtracking – p.19/28
A Consequence We recover Danvy, Grobauer, and Rhiger’s result: If M : Tσ, then MK cons nil = MS MK = λκ.(κ 0) ◦ (κ 1) ◦ (κ 2) ◦ . . .
MS = h0, 1, 2, . . .i
Relating Models of Backtracking – p.20/28
A Consequence We recover Danvy, Grobauer, and Rhiger’s result: If M : Tσ, then MK cons nil = MS MK = λκ.(κ 0) ◦ (κ 1) ◦ (κ 2) ◦ . . . Rσ MS = h0, 1, 2, . . .i
Relating Models of Backtracking – p.20/28
A step further. . . We relate the stream model to an operational semantics that implements it.
Translate the monadic metalanguage terms into a lazy PCF called mPCF.
Extend a standard adequacy theorem for the semantics of lazy PCF.
Use both the logical relation and adequacy to relate the S semantics with the operational semantics.
Relating Models of Backtracking – p.21/28
mPCF extends lazy PCF
unit and bind to build and sequence possibly nonterminating computations.
A fixed-point operator.
Include cons and nil to represent initial continuations.
Relating Models of Backtracking – p.22/28
From the metalanguage to mPCF The translation looks much like the K semantics: dunite = λa.(λκφ.κaφ) ddisje = λab.(λκφ.aκ(bκφ)) .. .
Relating Models of Backtracking – p.23/28
mPCF Semantics
A call-by-name operational semantics.
The denotational semantics L interprets terms in the lifting monad. unitL a = lift(a) L
bind c f = case c of ⊥ → ⊥ | lift(d) → f (d)
Relating Models of Backtracking – p.24/28
Implementing the streams
Relating Models of Backtracking – p.25/28
Adequacy Theorem. M reduces to a value iff either M is of value type. M is of computation type and denotes a non-⊥ element. Proof: We adapt a standard proof technique (Winskel 1993). The reverse direction requires coinduction to relate infinite streams.
Relating Models of Backtracking – p.26/28
And finally. . . Assume M : Tσ and MS = hdi ˆ s. Then dMe L consL nilL = MK consL nilL = MS = hdi ˆ s By adequacy, dMe cons nil →∗ cons v M0 , where d = vL s = M0L
Relating Models of Backtracking – p.27/28
Contributions
We have related two models of backtracking computation.
We accommodate higher-order quantities and infinite streams. We related the stream model to an operational semantics that implements it.
Relating Models of Backtracking – p.28/28
Contributions
We have related two models of backtracking computation.
We accommodate higher-order quantities and infinite streams. We related the stream model to an operational semantics that implements it.
Thank you!
Relating Models of Backtracking – p.28/28