On this page:
1.1 From Streams to amb
1.1.1 Suspension and the Order of Answers
1.2 Types
Literal
Term
Var
var
var=?
Substitution
State
Goal
1.3 The Core
==
call/  fresh
disj
conj
walk
ext-s
unify
1.4 User-Level Forms
Zzz
conj+
disj+
fresh
conde
run
run*
empty-state
call/  goal
take
take-all
reify-1st
walk*
reify-s
reify-name
fresh/  nf
9.3

1 microKanren🔗ℹ

 (require typed/microKanren/amb) package: ambKanren

This module is microKanren (Hemann and Friedman, μKanren: A Minimal Functional Core for Relational Programming, Scheme Workshop 2013) with its streams replaced by amb. It follows, definition by definition, typed/microKanren of the miniKanren-typed package, which is the Typed Racket version of the code at https://github.com/jasonhemann/microKanren. The definitions that only manipulate streams are gone.

1.1 From Streams to amb🔗ℹ

In microKanren, a goal returns a stream of states: all its answers, produced lazily. Conjunction uses stream bind to run one goal on each answer of another. With amb, a goal has a single state as its return type, chooses among its answers with amb, and fails by calling (amb-fail).

  

typed/microKanren

  

typed/microKanren/amb

Goal

  

(→ State (Streamof State))

  

(→ State State)

success

  

(unit s/c)

  

s/c

failure

  

mzero

  

(amb-fail)

disj

  

(mplus (g1 s/c) (g2 s/c))

  

((amb g1 g2) s/c)

conj

  

(bind (g1 s/c) g2)

  

(g2 (amb-local (g1 s/c)))

Zzz

  

(λ (s/c) (λ () (g s/c)))

  

(λ (s/c) (amb-pause) (g s/c))

take

  

force and collect the stream (g empty-state)

  

advance amb-generate* until enough answers arrive

Success returns the state unchanged, disjunction is a choice made by amb, and conjunction runs g2 on each answer of a local search of g1. amb-generate* provides a generator of answers and pauses. amb-local generates the events of g1 on demand, supplying each answer as an alternative of one choice that continues in g2. This implements the same scheduling as bind with the stream of g1 under amb-interleave and the default shuffler.

The definitions that remain are those of typed/microKanren, with these changes:

  • unit, mzero, mplus, bind, pull, and the stream types are removed.

  • take and take-all take a thunk that performs the nondeterministic computation, instead of a stream, and call/goal returns a state.

  • unify calls (amb-fail) instead of returning #f, so it returns a substitution, and == needs no test.

  • Zzz calls amb-pause (see below).

1.1.1 Suspension and the Order of Answers🔗ℹ

In microKanren, Zzz creates a suspended stream. Here it calls amb-pause before the goal. The wrapper forms that use Zzz inherit this pause. Calling a goal directly adds no pause of its own. The default merger of amb, amb-append, does not pass the turn at a pause, so the examples select amb-interleave explicitly.

Examples:
> (: relo (→ Term Goal))
> (define (relo x) (fresh (a d) (== x `(,a . ,d)) (relo x)))
> (run 1 (x) (disj+ (relo '(5 . 6)) (== x 3)))

- : (Listof Term)

'(3)

With amb-interleave and the default shuffler, conjunction uses a local search to follow the answer order of the stream-based implementation. Plain function composition, (g2 (g1 s/c)), runs g2 inside the choices of g1, so the merger interleaves the runs of g2 following the shape of those choices. bind interleaves them in the order of the answers of g1, with the pauses of g1 in between. When both versions completely enumerate a pure search without pruning, they produce the same answers in possibly different orders. A bounded run observes the difference.

Stream bind and conj can both change the order of answers when conjunctions are regrouped. Conjunction is therefore not associative under equality of ordered answers.

Examples:
> (: association-query (→ Boolean (Listof Term)))
> (define (association-query left?)
    (run 2 (q x y z)
      (== q (list x y z))
      (let ([a (disj (== x 'a) (== x 'b))]
            [b (disj (== y 'l) (== y 'r))]
            [c (disj (== z 'u) (Zzz (== z 'v)))])
        (if left? (conj (conj a b) c) (conj a (conj b c))))))
> (association-query #t)

- : (Listof Term)

'((a l u) (a r u))

> (association-query #f)

- : (Listof Term)

'((a l u) (b l u))

1.2 Types🔗ℹ

type

Literal

type

Term

Literal is the type of atomic values: booleans, numbers, characters, byte strings, strings, keywords, symbols, and '(). Term is the type of all terms: literals, variables, and pairs of terms.

type

Var

struct

(struct var (c)
    #:transparent)
  c : Natural

procedure

(var=? x1 x2) → Boolean

  x1 : Var
  x2 : Var
Logic variables. A variable is identified by its counter c, and var=? compares counters.

Substitution is defined as (Listof (Pair Var Term)), a triangular association list. State is defined as (Pair Substitution Natural): a substitution and the counter of the next fresh variable.

type

Goal

Defined as (→ State State).

Examples:
> ((== 1 1) empty-state)

- : State

'(() . 0)

> ((call/fresh (λ (q) (== q 5))) empty-state)

- : State

(cons (list (cons (var 0) 5)) 1)

> (take-all (λ () ((call/fresh (λ (q) (disj (== q 5) (== q 6)))) empty-state)))

- : (Listof State)

(list (cons (list (cons (var 0) 5)) 1) (cons (list (cons (var 0) 6)) 1))

1.3 The Core🔗ℹ

procedure

(== u v) → Goal

  u : Term
  v : Term
Unifies u and v in the substitution of the state, and calls (amb-fail) if they cannot be unified.

procedure

(call/fresh f) → Goal

  f : (→ Var Goal)
Applies f to a new variable, numbered by the counter of the state, and runs the resulting goal in a state whose counter is incremented.

procedure

(disj g1 g2) → Goal

  g1 : Goal
  g2 : Goal

procedure

(conj g1 g2) → Goal

  g1 : Goal
  g2 : Goal
Disjunction runs g1 or g2, as chosen by amb; conjunction runs g2 on each state that a local search of g1 returns, as (g2 (amb-local (g1 s/c))).

Example:
> (take-all
   (λ ()
     ((conj (call/fresh (λ (a) (== a 7)))
            (call/fresh (λ (b) (disj (== b 5) (== b 6)))))
      empty-state)))

- : (Listof State)

(list

 (cons (list (cons (var 1) 5) (cons (var 0) 7)) 2)

 (cons (list (cons (var 1) 6) (cons (var 0) 7)) 2))

procedure

(walk u s) → Term

  u : 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
Walking, extending a substitution, and unification. There is no occurs check. unify returns the resulting substitution on success. When u and v cannot be unified, unify calls (amb-fail). Call it within a search to handle that case as search failure.

1.4 User-Level Forms🔗ℹ

These are the forms of miniKanren-wrappers.scm.

syntax

(Zzz goal)

The inverse-η-delay: a goal that calls amb-pause before running goal, so that a recursive relation gives the other alternatives a turn instead of looping.

syntax

(conj+ goal ...+)

syntax

(disj+ goal ...+)

syntax

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

syntax

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

N-ary conjunction and disjunction of suspended goals, fresh variables, and disjunction of conjunctions.

Examples:
> (: appendo (→ Term Term Term Goal))
> (define (appendo l s out)
    (conde
      [(== '() l) (== s out)]
      [(fresh (a d res)
         (== `(,a . ,d) l)
         (== `(,a . ,res) out)
         (appendo d s res))]))
> (run* (x y) (appendo x y '(1 2 3)))

- : (Listof Term)

'(() (1) (1 2) (1 2 3))

syntax

(run n (x ...+) goal ...+)

 
  n : Natural

syntax

(run* (x ...+) goal ...+)

Runs the goals with fresh variables x ... and returns at most n (or all) values of the first variable, reified.

value

empty-state : State

procedure

(call/goal g) → State

  g : Goal

procedure

(take n th) → (Listof a)

  n : Natural
  th : (→ a)

procedure

(take-all th) → (Listof a)

  th : (→ a)
Running goals. call/goal runs g in empty-state. take and take-all collect at most n (or all) results of (th) with amb-generate*; they skip pauses only there, at the outermost level, so the pauses still schedule the search inside.

procedure

(reify-1st s/c) → Term

  s/c : State

procedure

(walk* v s) → Term

  v : Term
  s : Substitution

procedure

(reify-s v s) → Substitution

  v : Term
  s : Substitution

procedure

(reify-name n) → Symbol

  n : Natural
Reification of the value of the first variable, with the names '.0, '.1, and so on.

procedure

(fresh/nf n f) → Goal

  n : Natural
  f : (→ Var * Goal)
Applies f to n fresh variables.