GNU TeXmacsUser manualReferenceAboutEnglish
Logical programming extensions

The Scheme kernel of TeXmacs contains a small Prolog-like engine for logical programming, which is implemented in the modules kernel/logic/logic-*.scm. It is mainly used for storing declarative data, such as tables and groups of symbols, and for the inheritance relations between modes (see contextual overloading). By convention, the names of logical predicates end with a %.

Rules and queries
(logic-rules rule …)
(declare rules)

Each rule is a list (head cond …), meaning that head holds if all conditions cond hold (facts are rules without conditions). Variables are written as quoted symbols 'x. A special rule (assume cond …) adds the given conditions to all subsequent rules in the same declaration. For instance (from

kernel/logic/logic-test.scm

):

(logic-rules
  ((sun% Joris Piet))
  ((sun% Piet Opa))
  ((daughter% Geeske Opa))
  ((child% 'x 'y) (sun% 'x 'y))
  ((child% 'x 'y) (daughter% 'x 'y))
  ((descends% 'x 'y) (child% 'x 'y))
  ((descends% 'x 'z) (child% 'x 'y) (descends% 'y 'z)))

The rules are quasi-quoted, so that values can be inserted using unquote. The macro (logic-rule head cond …) declares a single rule.

(logic-query goal extra …)

(query goal extra …)
(query the rule base)

Return the list of all solutions of goal; each solution is an association list with the values of the free variables of goal. The extra arguments are additional facts which are assumed to hold during the query. The macro logic-query quotes its arguments, whereas the function query evaluates them. With the above rules, (logic-query (descends% Joris 'x)) returns the two solutions with x bound to Piet and Opa.

(logic-test? name arg …)
(test a relation)

Test whether the relation (name arg …) holds. The results are cached, which makes repeated tests cheap; the underlying function is logic-holds?.

Groups, tables and dispatchers

The following macros provide convenient interfaces for common kinds of declarative data.

(logic-group name member …)

(logic-in? x name)
(groups)

Declare member … to belong to the group name, resp. test whether x belongs to the group. Groups may be extended by further logic-group declarations. Elements of other groups can be included using rules of the form ((name 'x) (other-group 'x)) in logic-rules.

(logic-table name (key value) …)

(logic-ref name key)

(logic-ref-list name key)
(tables)

Declare a table name which associates values to keys. A key of the form (:or key-1 … key-n) associates the same value to several keys. The macro logic-ref returns the unique value associated to key (or #f), whereas logic-ref-list returns the list of all associated values. For instance,

utils/base/environment.scm

declares

(logic-table env-var-description%
  ("color" "Foreground colour")
  ("bg-color" "Background colour")
  …)

and (logic-ref env-var-description% "color") returns "Foreground colour".

(logic-dispatcher name (key fun) …)

(logic-dispatch name key arg …)
(dispatchers)

A dispatcher is a table which associates functions to keys; contrary to logic-table, the values fun are evaluated. Converters such as

convert/html/tmhtml.scm

use dispatchers to associate a conversion routine to each tag, which is then retrieved using logic-ref. The macro logic-dispatch calls the function associated to key on the arguments arg …; it is currently not used in the TeXmacs sources and its behaviour when no argument arg is given is somewhat peculiar (the function associated to (car key) is applied to key itself).

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; with no Invariant Sections, with no Front-Cover Texts, and with no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License".