▼ |  | Racklog: Prolog-Style Logic Programming

1 | Simple Goals and Queries
2 | Predicates
3 | Using Conventional Racket Expressions in Racklog
4 | Backtracking
5 | Unification
6 | Conjunctions and Disjunctions
7 | Manipulating Racklog Variables
8 | The Cut (!)
9 | Set Predicates
10 | Higher-order Predicates
11 | Racklog Module Language
12 | Glossary of Racklog Primitives
| Bibliography

► | 12 | Glossary of Racklog Primitives

12.1 | Racket Predicates
12.2 | User Interface
12.3 | Relations
12.4 | Racklog Variables
12.5 | Cut
12.6 | Racklog Operators
12.7 | Unification
12.8 | Numeric Predicates
12.9 | List Predicates
12.10 | Set Predicates
12.11 | Racklog Predicates
12.12 | Higher-order Predicates
12.13 | Racklog Variable Manipulation

On this page:

12.1`` Racket Predicates
logic-var?
atomic-struct?
atom?
compound-struct?
compound?
unifiable?
answer-value?
answer?
goal/ c
12.2`` User Interface
%which
%more
%find-all
12.3`` Relations
%rel
%empty-rel
%assert!
%assert-after!
12.4`` Racklog Variables
_
%let
12.5`` Cut
%cut-delimiter
!
12.6`` Racklog Operators
%fail
%true
%repeat
%and
%or
%not
%if-then-else
12.7`` Unification
%=
%/ =
%==
%/ ==
%is
use-occurs-check?
12.8`` Numeric Predicates
%<
%<=
%=/ =
%=: =
%>
%>=
12.9`` List Predicates
%append
%member
12.10`` Set Predicates
%set-of
%set-of-1
%bag-of
%bag-of-1
%free-vars
12.11`` Racklog Predicates
%compound
%constant
%var
%nonvar
12.12`` Higher-order Predicates
%apply
%andmap
12.13`` Racklog Variable Manipulation
%freeze
%melt
%melt-new
%copy

top ← prev up next →

### 12`` Glossary of Racklog Primitives

#### 12.1`` Racket Predicates

> > procedure

> > (logic-var? x) → boolean?

> x : any/c

Identifies a logic variable.

> > procedure

> > (atomic-struct? x) → boolean?

> x : any/c

Identifies structures that the (current-inspector) cannot inspect.

> > procedure

> > (atom? x) → boolean?

> x : any/c

Identifies atomic values that may appear in Racklog programs. Equivalent to the contract (or/c boolean? number? string? bytes? char? symbol? regexp? pregexp? byte-regexp? byte-pregexp? keyword? null? procedure? void? set? atomic-struct?).

> > procedure

> > (compound-struct? x) → boolean?

> x : any/c

Identifies structures that the (current-inspector) can inspect.

> > procedure

> > (compound? x) → boolean?

> x : any/c

Identifies compound values that may appear in Racklog programs. Equivalent to the contract (or/c pair? vector? mpair? box? hash? compound-struct?).

> > procedure

> > (unifiable? x) → boolean?

> x : any/c

Identifies values that may appear in Racklog programs. Essentially either an atom?, logic-var?, or compound? that contains unifiable?s.

> > procedure

> > (answer-value? x) → boolean?

> x : any/c

Identifies values that may appear in answer?. Essentially unifiable?s that do not contain logic-var?s.

> > procedure

> > (answer? x) → boolean?

> x : any/c

Identifies answers returned by %more and %which. Equivalent to the contract (or/c false/c (listof (cons/c symbol? answer-value?))).

> > value

> > goal/c : contract?

A contract for goals.

#### 12.2`` User Interface

> > syntax

> > (%which (V ...) G ...)

> > | V |  | : |  | identifier?

> > | G |  | : |  | goal/c

Returns an answer? of the variables V, ..., that satisfies all of G, ... If G, ..., cannot be satisfied, returns #f. Calling the thunk %more produces more instantiations, if available.

> > procedure

> > (%more) → answer?

The thunk %more produces more instantiations of the variables in the most recent %which-form that satisfy the goals in that %which-form. If no more solutions can be found, %more returns #f.

> > syntax

> > (%find-all (V ...) G ...)

> > | V |  | : |  | identifier?

> > | G |  | : |  | goal/c

Like (list (%which (V ...) G ...) (%more) ...) with as many (%more)s as there are answers. (This will not terminate if there are an infinite number of answers.)

#### 12.3`` Relations

> > syntax

> > (%rel (V ...) clause ...)

> clause |  | = |  | [(E ...) G ...]

> > | V |  | : |  | identifier?

> > | E |  | : |  | expression?

> > | G |  | : |  | goal/c

Returns a predicate function. Each clause C signifies that the goal created by applying the predicate object to anything that matches (E ...) is deemed to succeed if all the goals G, ..., can, in their turn, be shown to succeed. The variables V, ..., are local logic variables for clause, ....

> > procedure

> > (%empty-rel E ...) → goal/c

> E : unifiable?

The goal (%empty-rel E ...) always fails. The value %empty-rel is used as a starting value for predicates that can later be enhanced with %assert! and %assert-after!.

> > syntax

> > (%assert! Pname (V ...) clause ...)

> > | Pname |  | : |  | identifier?

> > | V |  | : |  | identifier?

Adds the clauses clauses, ..., to the end of the predicate that is the value of the Racket variable Pname. The variables V, ..., are local logic variables for clause, ....

> > syntax

> > (%assert-after! Pname (V ...) clause ...)

> > | Pname |  | : |  | identifier?

> > | V |  | : |  | identifier?

Like %assert!, but adds the new clauses to the front of the existing predicate.

#### 12.4`` Racklog Variables

> > procedure

> > (_) → logic-var?

A thunk that produces a new logic variable. Can be used in situations where we want a logic variable but don’t want to name it. (%let, in contrast, introduces new lexical names for the logic variables it creates.)

> > syntax

> > (%let (V ...) expr ...)

> | V |  | : |  | identifier?

Introduces V, ..., as lexically scoped logic variables to be used in expr, ...

#### 12.5`` Cut

> > syntax

> > (%cut-delimiter . any)

Introduces a cut point. See The Cut (!).

> > syntax

> > !

The cut goal, see The Cut (!).

May only be used syntactically inside %cut-delimiter or %rel.

#### 12.6`` Racklog Operators

> > value

> > %fail : goal/c

The goal %fail always fails.

> > value

> > %true : goal/c

The goal %true succeeds. Fails on retry.

> > procedure

> > (%repeat) → goal/c

The goal (%repeat) always succeeds (even on retries). Useful for failure-driven loops.

> > syntax

> > (%and G ...)

> | G |  | : |  | goal/c

The goal (%and G ...) succeeds if all the goals G, ..., succeed.

> > syntax

> > (%or G ...)

> | G |  | : |  | goal/c

The goal (%or G ...) succeeds if one of G, ..., tried in that order, succeeds.

> > procedure

> > (%not G) → goal/c

> G : goal/c

The goal (%not G) succeeds if G fails.

> > procedure

> > (%if-then-else G1 G2 G3) → goal/c

> G1 : goal/c
> G2 : goal/c
> G3 : goal/c

The goal (%if-then-else G1 G2 G3) tries G1 first: if it succeeds, tries G2; if not, tries G3.

#### 12.7`` Unification

> > procedure

> > (%= E1 E2) → goal/c

> E1 : unifiable?
> E2 : unifiable?

The goal (%= E1 E2) succeeds if E1 can be unified with E2. Any resulting bindings for logic variables are kept.

> > procedure

> > (%/= E1 E2) → goal/c

> E1 : unifiable?
> E2 : unifiable?

%/= is the negation of %=. The goal (%/= E1 E2) succeeds if E1 can not be unified with E2.

> > procedure

> > (%== E1 E2) → goal/c

> E1 : unifiable?
> E2 : unifiable?

The goal (%== E1 E2) succeeds if E1 is identical to E2. They should be structurally equal. If containing logic variables, they should have the same variables in the same position. Unlike a %=-call, this goal will not bind any logic variables.

> > procedure

> > (%/== E1 E2) → goal/c

> E1 : unifiable?
> E2 : unifiable?

%/== is the negation of %==. The goal (%/== E1 E2) succeeds if E1 and E2 are not identical.

> > syntax

> > (%is E1 E2)

The goal (%is E1 E2) unifies with E1 the result of evaluating E2 as a Racket expression. E2 may contain logic variables, which are dereferenced automatically. Fails if E2 contains unbound logic variables.

> > parameter

> > (use-occurs-check?) → boolean?

> (use-occurs-check? on?) → void?
> on? : boolean?

If this is false (the default), Racklog’s unification will not use the occurs check. If it is true, the occurs check is enabled.

#### 12.8`` Numeric Predicates

> > procedure

> > (%< E1 E2) → goal/c

> E1 : unifiable?
> E2 : unifiable?

The goal (%< E1 E2) succeeds if E1 and E2 are bound to numbers and E1 is less than E2.

> > procedure

> > (%<= E1 E2) → goal/c

> E1 : unifiable?
> E2 : unifiable?

The goal (%<= E1 E2) succeeds if E1 and E2 are bound to numbers and E1 is less than or equal to E2.

> > procedure

> > (%=/= E1 E2) → goal/c

> E1 : unifiable?
> E2 : unifiable?

The goal (%=/= E1 E2) succeeds if E1 and E2 are bound to numbers and E1 is not equal to E2.

> > procedure

> > (%=:= E1 E2) → goal/c

> E1 : unifiable?
> E2 : unifiable?

The goal (%=:= E1 E2) succeeds if E1 and E2 are bound to numbers and E1 is equal to E2.

> > procedure

> > (%> E1 E2) → goal/c

> E1 : unifiable?
> E2 : unifiable?

The goal (%> E1 E2) succeeds if E1 and E2 are bound to numbers and E1 is greater than E2.

> > procedure

> > (%>= E1 E2) → goal/c

> E1 : unifiable?
> E2 : unifiable?

The goal (%>= E1 E2) succeeds if E1 and E2 are bound to numbers and E1 is greater than or equal to E2.

#### 12.9`` List Predicates

> > procedure

> > (%append E1 E2 E3) → goal/c

> E1 : unifiable?
> E2 : unifiable?
> E3 : unifiable?

The goal (%append E1 E2 E3) succeeds if E3 is unifiable with the list obtained by appending E1 and E2.

> > procedure

> > (%member E1 E2) → goal/c

> E1 : unifiable?
> E2 : unifiable?

The goal (%member E1 E2) succeeds if E1 is a member of the list in E2.

#### 12.10`` Set Predicates

> > procedure

> > (%set-of E1 G E2) → goal/c

> E1 : unifiable?
> G : goal/c
> E2 : unifiable?

The goal (%set-of E1 G E2) unifies with E2 the set of all the instantiations of E1 for which goal G succeeds.

> > procedure

> > (%set-of-1 E1 G E2) → goal/c

> E1 : unifiable?
> G : goal/c
> E2 : unifiable?

Similar to %set-of, but fails if the set is empty.

> > procedure

> > (%bag-of E1 G E2) → goal/c

> E1 : unifiable?
> G : goal/c
> E2 : unifiable?

The goal (%bag-of E1 G E2) unifies with E2 the bag (multiset) of all the instantiations of E1 for which goal G succeeds.

> > procedure

> > (%bag-of-1 E1 G E2) → goal/c

> E1 : unifiable?
> G : goal/c
> E2 : unifiable?

Similar to %bag-of, but fails if the bag is empty.

> > syntax

> > (%free-vars (V ...) G)

> > | V |  | : |  | identifier?

> > | G |  | : |  | goal/c

Identifies the occurrences of the variables V, ..., in goal G as free. It is used to avoid existential quantification in calls to set predicates (%bag-of, %set-of, &c.).

#### 12.11`` Racklog Predicates

> > procedure

> > (%compound E) → goal/c

> E : unifiable?

The goal (%compound E) succeeds if E is a compound value.

> > procedure

> > (%constant E) → goal/c

> E : unifiable?

The goal (%constant E) succeeds if E is an atomic value.

> > procedure

> > (%var E) → goal/c

> E : unifiable?

The goal (%var E) succeeds if E is not completely instantiated, ie, it has at least one unbound variable in it.

> > procedure

> > (%nonvar E) → goal/c

> E : unifiable?

%nonvar is the negation of %var. The goal (%nonvar E) succeeds if E is completely instantiated, ie, it has no unbound variable in it.

#### 12.12`` Higher-order Predicates

> > procedure

> > (%apply P L) → goal/c

> P : unifiable?
> L : unifiable?

The goal (%apply P L) succeeds if L is a list with elements E, ..., and if P is a predicate accepting as many arguments as there are Es, and if the goal (P E ...) succeeds.

The goal will fail if L is not sufficiently instantiated to determine its length.

For example, the goal

> (%apply %= (list 1 X))

is equivalent to

> (%= 1 X)

which succeeds if X can be unified with 1.

> > procedure

> > (%andmap P L ...+) → goal/c

> P : unifiable?
> L : unifiable?

The goal (%andmap P L ...) succeeds if all the values L, ..., are lists of equal length, and if the goal (P E ...) succeeds for each set of values E, ..., taken in turn from each of the lists L, ...

As an example, in particular the goal (%andmap %<= '(1 2 3) '(4 5 6)) is equivalent to

> (%and (%<= 1 4)
> (%<= 2 5)
> (%<= 3 6))

#### 12.13`` Racklog Variable Manipulation

> > procedure

> > (%freeze S F) → goal/c

> S : unifiable?
> F : unifiable?

The goal (%freeze S F) unifies with F a new frozen version of the structure in S. Freezing implies that all the unbound variables are preserved. F can henceforth be used as bound object with no fear of its variables getting bound by unification.

> > procedure

> > (%melt F S) → goal/c

> F : unifiable?
> S : unifiable?

The goal (%melt F S) unifies S with the thawed (original) form of the frozen structure in F.

> > procedure

> > (%melt-new F S) → goal/c

> F : unifiable?
> S : unifiable?

The goal (%melt-new F S) unifies S with a thawed copy of the frozen structure in F. This means new logic variables are used for unbound logic variables in F.

> > procedure

> > (%copy F S) → goal/c

> F : unifiable?
> S : unifiable?

The goal (%copy F S) unifies with S a copy of the frozen structure in F.
top ← prev up next →