/usr/lib/s9fes/prolog.scm is in scheme9 2010.11.13-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 | ; Scheme 9 from Empty Space, Function Library
; By Nils M Holm, 1998-2009
; See the LICENSE file of the S9fES package for terms of use
;
; (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!)
;
; Example: (begin (! (man socrates))
; (:- (mortal ?x)
; (man ?x))
; (query '(mortal ?who))) ==> (((who . socrates)))
(load-from-library "syntax-rules.scm")
(load-from-library "hash-table.scm")
(define *prolog-database* '())
(define (prolog q db)
(define empty-env '((())))
(define top-scope "")
(define true '(()))
(define false '())
(define (unique a)
(letrec
((unique2
(lambda (a r)
(cond ((null? a)
(reverse! r))
((member (car a) r)
(unique2 (cdr a) r))
(else
(unique2 (cdr a)
(cons (car a) r)))))))
(unique2 a '())))
(define (variable? x)
(and (symbol? x)
(char=? #\? (string-ref (symbol->string x) 0))))
(define (internal? x)
(and (symbol? x)
(char=? #\: (string-ref (symbol->string x) 0))))
(define (anonymous? x)
(eq? '_ x))
(define (extend n v env)
(cons (cons n v) env))
(define (new-scope env id)
(cond ((variable? env)
(string->symbol
(string-append (symbol->string env) id)))
((pair? env)
(cons (new-scope (car env) id)
(new-scope (cdr env) id)))
(else
env)))
(define (new-env-id x)
(string-append ";" x))
(define (value-of x env)
(if (variable? x)
(cond ((assq x env)
=> (lambda (v)
(value-of (cdr v) env)))
(else x))
x))
(define (unify x y env)
(let ((x (value-of x env))
(y (value-of y env)))
(cond ((variable? x) (extend x y env))
((variable? y) (extend y x env))
((or (anonymous? x)
(anonymous? y))
env)
((and (pair? x)
(pair? y))
(let ((new (unify (car x) (car y) env)))
(and new (unify (cdr x) (cdr y) new))))
((eq? x y) env)
(else #f))))
(define (check-args g n)
(if (not (= n (length g)))
(error "wrong number of arguments" g)))
(define (goal-unify rules goals env id result)
(check-args (car goals) 3)
(let* ((this-goal (car goals))
(new-env (unify (cadr this-goal) (caddr this-goal) env)))
(if new-env
(let ((r (prove (cdr goals)
new-env
(new-env-id id))))
(try-rules (cdr rules) goals env id (append result r)))
(try-rules (cdr rules) goals env id result))))
(define (goal-dif rules goals env id result)
(check-args (car goals) 3)
(let* ((this-goal (car goals))
(new-env (unify (cadr this-goal) (caddr this-goal) env)))
(if (not new-env)
(let ((r (prove (cdr goals)
env
(new-env-id id))))
(try-rules (cdr rules) goals env id (append result r)))
false)))
(define (goal* rules goals env id result)
(let* ((this-rule (new-scope (car rules) id))
(new-env (unify (car goals) (car this-rule) env)))
(if new-env
(let ((r (prove (append (cdr this-rule) (cdr goals))
new-env
(new-env-id id))))
(try-rules (cdr rules) goals env id (append result r)))
(try-rules (cdr rules) goals env id result))))
(define (try-rules rules goals env id result)
(if (null? rules)
result
(case (caar goals)
((==) (goal-unify rules goals env id result))
((dif) (goal-dif rules goals env id result))
(else (goal* rules goals env id result)))))
(define (list-env env)
(letrec
((this-id caar)
(scope-id caddr)
(top-level?
(lambda (x)
(not (memv #\; (string->list (symbol->string x))))))
(var-name
(lambda (x)
(let* ((s (symbol->string x))
(k (string-length s)))
(let loop ((i 0))
(if (or (>= i k)
(char=? #\; (string-ref s i)))
(string->symbol (substring s 1 i))
(loop (+ 1 i)))))))
(list-env2
(lambda (e r)
(cond ((null? (cdr e))
(list r))
((top-level? (this-id e))
(list-env2 (cdr e)
(extend (var-name (this-id e))
(value-of (this-id e) env)
r)))
(else
(list-env2 (cdr e) r))))))
(list-env2 env '())))
; version without memoization
(define (prove goals env id)
(if (null? goals)
(list-env env)
(try-rules db goals env id '())))
(define proven (make-hash-table))
(define (prove goals env id)
(if (null? goals)
(list-env env)
(let* ((k (append goals env))
(v (hash-table-ref proven k)))
(if v
(car v)
(let ((v (try-rules db goals env id '())))
(hash-table-set! proven k v)
v)))))
(define (any? p a)
(cond ((null? a) #f)
((p (car a)) #t)
(else (any? p (cdr a)))))
(define (cleanup env)
(apply append
(map (lambda (frame)
(if (or (any? (lambda (x) (variable? (cdr x))) frame)
(any? (lambda (x) (internal? (cdr x))) frame))
'()
(list frame)))
env)))
(cleanup (unique (prove (new-scope q top-scope)
empty-env
(new-env-id top-scope)))))
(define (new-database!)
(set! *prolog-database* '()))
(define (update! x)
(set! *prolog-database*
(cons x *prolog-database*)))
(define (fact! x)
(let ((update! update!))
(update! (list x))))
(define (predicate! head . clause*)
(let ((update! update!))
(update! (cons head clause*))))
(define (query . q)
(prolog q (reverse *prolog-database*)))
(define (print-frames env)
(cond ((equal? '(()) env)
(display "yes")
(newline))
((equal? '() env)
(display "no")
(newline))
(else
(for-each (lambda (frame)
(for-each (lambda (b)
(display (car b))
(display " = ")
(display (cdr b))
(display "; "))
frame)
(newline))
env))))
(define-syntax !
(syntax-rules ()
((_ token ...)
(fact! 'token ...))))
(define-syntax :-
(syntax-rules ()
((_ head clause clauses ...)
(predicate! 'head 'clause 'clauses ...))))
(define-syntax ?
(syntax-rules ()
((_ q ...)
(print-frames (query 'q ...)))))
|