On this page:
3.1 Implementation Basis
3.1.1 Order of Answers
3.2 Types
Literal
Data
data?
Term
Var
var
Substitution
Goal
3.3 Running Programs
run
run*
3.4 Goals and Relations
succeed
fail
==
defrel
fresh
conde
conda
condu
project
disj
conj
disj2
conj2
ifte
once
call/  fresh
run-goal
3.5 Substitutions and Unification
empty-s
walk
walk*
occurs?
ext-s
unify
reify-name
reify-s
reify
9.3

3 Second Edition🔗ℹ

 (require typed/miniKanren/amb/trs2) package: ambKanren

This module implements the language of the second edition of The Reasoned Schemer (Friedman, Byrd, Kiselyov, and Hemann, MIT Press, 2018) on top of amb, without streams.

3.1 Implementation Basis🔗ℹ

The interface (run, defrel, fresh, conde, conda, condu, project) and the definitions of walk, unify, ifte, once, and reification follow the official code of the second edition. What changes is the representation of goals:

  

second edition

  

typed/miniKanren/amb/trs2

goal

  

(→ Substitution (Streamof Substitution))

  

(→ Substitution Substitution)

disj2

  

append-inf

  

a choice made by amb

conj2

  

append-map-inf

  

a local search: (g2 (amb-local (g1 s)))

suspension

  

an immature stream, (λ () ...)

  

amb-pause: one logical pause

The second edition suspends every relation call. Here defrel calls amb-pause instead. When current-amb-merger is amb-interleave, that pause lets the other side of a binary choice run. The examples below select this merger explicitly:

Examples:
> (defrel (nevero) (nevero))
> (run 1 q (conde [(nevero)] [succeed]))

- : (Listof Term)

'(_0)

3.1.1 Order of Answers🔗ℹ

With amb-interleave and the default shuffler, all 285 examples of the book that the tests cover give the same answers in the same order.

This depends on conj2. Like append-map-inf, it searches g1 locally and continues with g2 from each answer, so the runs of g2 are interleaved in the order of the answers of g1, with the pauses of g1 in between. Plain function composition, (g2 (g1 s)), runs g2 inside the choices of g1, so the runs are interleaved following the shape of those choices. When both versions completely enumerate a pure search without pruning, they produce the same answers in possibly different orders. With composition, (conj2 (disj2 a b) g) is (disj2 (conj2 a g) (conj2 b g)), order included, whereas the book’s bind keeps the answers of (disj2 a b) together; two examples, in Chapter 8 and Chapter 9, return their answers in another order. The price of the local search is one more search per conjunction.

The other mergers give other orders: amb-alternate also passes the turn at answers, and amb-level runs in rounds of pauses. With them, a bounded run need not return the same answers as the book. The migration report of the package records the examples that differ.

3.2 Types🔗ℹ

Booleans, numbers, characters, byte strings, strings, keywords, '(), and symbols.

type

Data

Defined as (∪ Literal (Pair Data Data)): data without logic variables.

procedure

(data? v) → Boolean

  v : Any
Recognizes Data, with a Typed Racket occurrence-typing filter.

type

Term

The type of terms: booleans, numbers, characters, byte strings, strings, keywords, symbols, '(), logic variables (Var), and pairs of terms.

type

Var

struct

(struct var (name)
    #:transparent)
  name : Symbol
Logic variables, compared by identity.

Defined as (Listof (Pair Var Term)), searched with assq (variable identity).

type

Goal

Defined as (→ Substitution Substitution). A goal returns one extension of the substitution, chosen with amb, and calls (amb-fail) when it has none.

3.3 Running Programs🔗ℹ

syntax

(run n q goal ...)

(run n (x ...+) goal ...)
 
  n : (Option Natural)
Runs the conjunction of the goals and returns at most n reified answers, or all of them when n is #f. In the second form, each answer is the list of the values of the xs. (Chapter 1.)

Example:
> (run* (x y) (conde [(== x 'a)] [(== x 'b)]) (== y x))

- : (Listof Term)

'((a a) (b b))

syntax

(run* q goal ...)

Like run with n equal to #f.

3.4 Goals and Relations🔗ℹ

value

succeed : Goal

value

fail : Goal

procedure

(== u v) → Goal

  u : Term
  v : Term
Success, failure, and unification with the occurs check. (Chapter 1.)

syntax

(defrel (name arg ...) goal ...)

 
arg = x
  | [x : type]
Defines name as a relation whose result is a Goal that calls amb-pause before running the conjunction of the goals. A plain argument x has type Term; an argument written as [x : type] has the given type. (Chapter 1.)

syntax

(fresh (x ...) goal ...)

syntax

(conde [goal ...+] ...)

syntax

(conda [goal ...+] ...+)

syntax

(condu [goal ...+] ...+)

syntax

(project (x ...) goal ...)

As in the second edition. conde is the disjunction of the conjunctions of its lines; conda and condu commit to the first line whose first goal succeeds, keeping all of its answers or only the first. (fresh and conde, Chapter 1; conda and condu, Chapter 9; project, Chapter 10.)

syntax

(disj goal ...)

syntax

(conj goal ...)

procedure

(disj2 g1 g2) → Goal

  g1 : Goal
  g2 : Goal

procedure

(conj2 g1 g2) → Goal

  g1 : Goal
  g2 : Goal
Disjunction and conjunction. disj2 applies the goal chosen by (amb g1 g2), and conj2 applies g2 to each answer of a local search of g1, as (g2 (amb-local (g1 s))); see Order of Answers.

procedure

(ifte g1 g2 g3) → Goal

  g1 : Goal
  g2 : Goal
  g3 : Goal

procedure

(once g) → Goal

  g : Goal
The functions behind conda and condu. They search for the first answer with amb-generate* and pass on every pause with amb-pause, so the enclosing search can still schedule other alternatives while the test runs. A sequence observer that skipped the pauses would hide them from the outer merger.

procedure

(call/fresh name f) → Goal

  name : Symbol
  f : (→ Var Goal)

procedure

(run-goal n g) → (Listof Substitution)

  n : (Option Natural)
  g : Goal
The functions behind fresh and run.

3.5 Substitutions and Unification🔗ℹ

value

empty-s : Substitution

procedure

(walk v s) → Term

  v : Term
  s : Substitution

procedure

(walk* v s) → Term

  v : Term
  s : Substitution

procedure

(occurs? x v s) → Boolean

  x : Var
  v : Term
  s : Substitution

procedure

(ext-s x v s) → Substitution

  x : Var
  v : Term
  s : Substitution

procedure

(unify u v s) → Substitution

  u : Term
  v : Term
  s : Substitution
As in the second edition, except that ext-s and unify call (amb-fail) instead of returning #f. As in miniKanren-typed, ext-s skips the occurs check for ground data recognized by data?.

procedure

(reify-name n) → Symbol

  n : Index

procedure

(reify-s v r) → Substitution

  v : Term
  r : Substitution

procedure

(reify v) → (→ Substitution Term)

  v : Term
Reification, with the names '0, '1, and so on.