File: prolog

package info (click to toggle)
scheme9 2025.08.12-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,080 kB
  • sloc: lisp: 16,752; ansic: 11,869; sh: 806; makefile: 237; sed: 6
file content (45 lines) | stat: -rw-r--r-- 1,580 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
S9 LIB  (prolog list1 list2)          ==>  list
        (new-database!)               ==>  unspecific
        (fact! list)                  ==>  unspecific
        (predicate! list1 list2 ...)  ==>  unspecific
        (query list)                  ==>  list

        (load-from-library "prolog.scm")

This is a tiny PROLOG interpreter that is based on an even
tinier PROLOG interpreter written in MACLISP by Ken Kahn.

The PROLOG procedures takes a query LIST1 and a database
LIST2 as arguments, attempts to prove LIST1 in LIST2, and
returns the result(s).

NEW-DATABASE! sets up a fresh PROLOG database (thereby
deleting any existing one).

FACT! adds a new fact to the database.

PREDICATE! adds a predicate with the head LIST1 and the
clauses LIST2 ... to the database.

QUERY attempts to prove LIST1. It returns a list of results.
An empty list indicates that LIST1 could not be proven.

See "prolog-test.scm" for an example program.

The following macros add some syntactic sugar for interactive
use; they allows you to write, for instance, (! (man socrates))
instead of (fact! '(man socrates)).

(! fact)              ==>  unspecific
(:- list1 list2 ...)  ==>  unspecific
(? query)             ==>  unspecific

The following special predicates are implemented in the
interpreter: (== A B) returns a new environment if A can be
unified with B, else NO. (Dif A B) returns NO if A can be
unified with B, else YES (use only at the end of a clause!)

(begin (! (man socrates))
       (:- (mortal ?x)
           (man ?x))
       (query '(mortal ?who)))  ==>  (((who . socrates)))