On this page:
2.1 Implementation Basis
2.2 Types
Literal
Data
data?
Term
Var
var
Substitution
Goal
2.3 Running Programs
run
run*
2.4 Goals
succeed
fail
==
fresh
λG
all
alli
conde
condi
conda
condu
ife
ifi
ifa
ifu
cond-aux
all-aux
2.4.1 Interleaving
2.5 Substitutions and Unification
empty-s
size-s
ext-s
walk
walk*
occurs?
unify
reify-name
reify-s
reify
sequence->amb
list->amb
9.3

2 First Edition🔗ℹ

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

This module implements the language of the first edition of The Reasoned Schemer on top of amb.

2.1 Implementation Basis🔗ℹ

The core follows microKanren: a goal returns one substitution and chooses among its answers with amb. Unlike microKanren, there is no state and no variable counter (fresh creates variables directly). Substitutions are association lists, as in miniKanren-typed.

The user-level language is that of the first edition of The Reasoned Schemer (Friedman, Byrd, and Kiselyov, MIT Press, 2005): run with a single query variable, conde with else, the interleaving forms condi and alli, and the committed-choice forms conda and condu. “Chapter n” below refers to that edition.

The differences from the book are:

  • Goals use amb in place of streams. Unification through == uses the occurs check, as in miniKanren-typed, and fails with (amb-fail).

  • conde is depth-first, as in the first edition, and uses amb-append. The interleaving forms condi and alli follow mplusi and bindi of the first edition (see Interleaving). With the default shuffler, the book examples covered by the tests give the same answers in the same order.

2.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. Variables are compared by identity with eq?; the name is only for printing.

Defined as (Listof (Pair Var Term)), searched with assq (variable identity). As in other miniKanren implementations, the substitution is triangular: a variable may be associated with a term containing other associated variables.

type

Goal

Defined as (→ Substitution Substitution). Applying a goal to a substitution returns one extension of it under which the goal holds, chosen with amb; the other extensions are explored when the search backtracks. A goal with no answer calls (amb-fail). Goals must therefore be applied within the dynamic extent of an amb search, such as in-amb/do.

Examples:
> (define x (var 'x))
> (for/list : (Listof Term)
            ([s : Substitution
                (in-amb/do ((conde [(== x 1)] [(== x 2)]) empty-s))])
    (walk x s))

- : (Listof Term)

'(1 2)

2.3 Running Programs🔗ℹ

syntax

(run n (x) goal ...)

 
  n : (Option Natural)

syntax

(run* (x) goal ...)

Introduces a fresh variable x, applies the conjunction of the goals to the empty substitution inside amb-generate*, and returns a list of at most n reified values of x. When n is #f, all the values are returned, and the computation does not terminate if there are infinitely many. The limit is checked before each answer is requested, so run never searches for an answer that it will not return. (Chapter 1.)

Example:
> (run* (q) (fresh (a b) (== q (list a b)) (== a 'left)))

- : (Listof Term)

'((left _.0))

2.4 Goals🔗ℹ

value

succeed : Goal

value

fail : Goal

succeed is the identity function; fail calls (amb-fail). (Chapter 1.)

procedure

(== u v) → Goal

  u : Term
  v : Term
The goal that extends the substitution by unifying u with v, and fails if they do not unify. (Chapter 1.)

Examples:
> (run* (q) (fresh (x) (== q (cons x 2)) (== x 1)))

- : (Listof Term)

'((1 . 2))

> (run* (q) (== q (list q)))

- : (Listof Term)

'()

syntax

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

Binds each x to a new variable and behaves as (all goal ...). Each application creates the variables before running the conjunction. The form with no variables also delays the conjunction until application. (Chapter 1.)

syntax

(λG (s) body ...+)

A function annotated with the type Goal. For example, (λG (s) ((== x 1) s)) is a goal.

syntax

(all goal ...)

syntax

(alli goal ...)

Conjunction. With no goals, both forms are succeed. A single goal is returned unchanged. With multiple goals, the first goal expression is evaluated when the conjunction is built; the rest is built when the resulting goal is applied, as in the reference implementation. all explores answers depth-first with amb-append; alli interleaves them with amb-alternate, binding answers through a local search. (Chapter 6.)

syntax

(conde clause ...)

 
clause = [goal ...+]
  | [else goal ...]

syntax

(condi clause ...)

Disjunction of clauses, each of which is a conjunction. An else clause, if present, comes last. conde tries the clauses depth-first; condi interleaves their answers. (conde, Chapter 1; condi, Chapter 6.)

Examples:
> (: repeato (→ Term Term Goal))
> (define (repeato x l)
    (conde
      [(== l '())]
      [(fresh (d) (== l (cons x d)) (repeato x d))]))
> (run 6 (q) (conde [(repeato 'a q)] [(repeato 'b q)]))

- : (Listof Term)

'(() (a) (a a) (a a a) (a a a a) (a a a a a))

> (run 6 (q) (condi [(repeato 'a q)] [(repeato 'b q)]))

- : (Listof Term)

'(() () (a) (b) (a a) (b b))

syntax

(conda clause ...)

syntax

(condu clause ...)

Committed choice. The first goal of each non-final clause is a test. Once a test succeeds, that clause is the only one used. conda continues with every answer of the test and condu continues with its first answer. If no earlier test succeeds, the final clause runs as an ordinary all, including when it is the only clause. (Chapter 10.)

Examples:
> (run* (q) (conda [(conde [(== q 1)] [(== q 2)])] [(== q 3)]))

- : (Listof Term)

'(1 2)

> (run* (q) (condu [(conde [(== q 1)] [(== q 2)])] [(== q 3)]))

- : (Listof Term)

'(1)

> (run* (q) (condu [(conde [(== q 1)] [(== q 2)])]))

- : (Listof Term)

'(1 2)

syntax

(ife g0 g1 g2)

syntax

(ifi g0 g1 g2)

syntax

(ifa g0 g1 g2)

syntax

(ifu g0 g1 g2)

syntax

(cond-aux ifer clause ...)

syntax

(all-aux bnd goal ...)

The building blocks of the forms above, with the structure of the first edition’s implementation. cond-aux turns each non-final clause into (ifer test (all goal ...) rest) and the final clause into all. all-aux follows miniKanren-typed: its empty case is succeed, its single-goal case is that goal unchanged, and its recursive case applies (bnd (g^ s) (all-aux bnd goal ...)). The private binding forms used by all and alli capture the first argument in a local amb search; they are not exported stream-binding functions. ifa and ifu search for the first answer of the test with amb-generate*, passing on every pause, so that they know whether it has an answer before they commit.

2.4.1 Interleaving🔗ℹ

condi chooses clauses with amb-alternate, which passes the turn at every answer. alli binds the answers of each non-final goal through a local search (amb-local) under the same merger and continues from each answer. These forms take the place of the stream-based mplusi and bindi.

In the example below, counto has an answer for every length, and the answers with 'R are still reached:

Examples:
> (: counto (→ Term Goal))
> (define (counto n)
    (conde
      [(== n '())]
      [(fresh (m) (== n (cons 'I m)) (counto m))]))
> (run 6 (q)
    (fresh (n side)
      (alli (counto n) (conde [(== side 'L)] [(== side 'R)]))
      (== q (cons side n))))

- : (Listof Term)

'((L) (L I) (R) (L I I) (R I) (L I I I))

2.5 Substitutions and Unification🔗ℹ

value

empty-s : Substitution

procedure

(size-s s) → Index

  s : Substitution

procedure

(ext-s x v s) → Substitution

  x : Var
  v : Term
  s : Substitution
Operations on substitutions. ext-s associates x with v. Ground data (recognized by data?) skips the occurs check; otherwise the operation fails with (amb-fail) when x occurs in v.

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

(unify u v s) → Substitution

  u : Term
  v : Term
  s : Substitution
walk follows the associations of a variable until it reaches a term that is not an associated variable; walk* does so throughout a term. unify returns an extension of s that makes u and v equal, and calls (amb-fail) if there is none. (Chapter 9.)

procedure

(reify-name n) → Symbol

  n : Index

procedure

(reify-s v s) → Substitution

  v : Term
  s : Substitution

procedure

(reify v) → Term

  v : Term
Reification: unassociated variables are replaced by '.0, '.1, and so on, in order of first appearance. (Chapter 9.)

procedure

(sequence->amb s) → a

  s : (Sequenceof a)

procedure

(list->amb l) → a

  l : (Listof a)
Choose an element with amb. These operations belong to the amb representation and have no stream counterpart in this module’s interface.