Advertisement
logicmoo

V4.lsp

Dec 17th, 2013
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scheme 26.19 KB | None | 0 0
  1. ; Mini-PrologII
  2. ; boot.lsp
  3. ;
  4.  
  5. (defun mini-PrologII ()         ; to run it
  6.   (banner)
  7.   (format t "~A~%" (load "mlg.Start"))
  8.   (myloop (read_prompt)))
  9.  
  10. (defun read_prompt ()
  11.   (terpri)
  12.   (format t "| ?- ")
  13.   (read_code_tail))
  14.  
  15. (defun banner ()
  16.   (dotimes (i 2) (terpri))
  17.   (format t "Mini-PrologII~%")
  18.   (dotimes (i 2) (terpri)))
  19.  
  20. (defun l ()
  21.   (format t "Back to Mini-PrologII top-level~%")
  22.   (myloop (read_prompt)))
  23. ; mini-Cprolog & mini-PrologII
  24. ; cparser.lsp
  25. ;
  26.  
  27. (defvar *lvarloc nil)                   ; list of local vars
  28. (defvar *lvarglob nil)                  ; list of global vars
  29. (set-macro-character #\% (get-macro-character #\;))
  30.  
  31. (defun rch ()                           ; skips Newline
  32.   (do ((ch (read-char) (read-char)))
  33.       ((char/= ch #\Newline) ch)))
  34. (defun rchnsep ()                       ; skips Newline and Space
  35.   (do ((ch (rch) (rch)))
  36.       ((char/= ch #\space) ch)))
  37.  
  38. (defun special (ch) (char= ch '#\_))
  39. (defun alphanum (ch)                    ; symbol normal constituant
  40.   (or (alphanumericp ch) (special ch)))
  41. (defun valdigit (ch) (digit-char-p ch))
  42.  
  43. (defun read_number (ch)                 ; next integer
  44.   (do ((v (valdigit ch) (+ (* v 10) (valdigit (read-char)))))
  45.       ((not (digit-char-p (peek-char))) v)))
  46.  
  47. (defun implode (lch) (intern (map 'string #'identity lch)))
  48.  
  49. (defun read_atom (ch)                   ; next normal symbol
  50.   (do ((lch (list ch) (push (read-char) lch)))
  51.       ((not (alphanum (peek-char))) (implode (reverse lch)))))
  52.  
  53. (defun read_at (ch)                     ; next special symbol
  54.   (do ((lch (list ch) (push (read-char) lch)))
  55.       ((char= (peek-char) #\') (read-char) (implode (reverse lch)))))
  56.  
  57. (defun read_string (ch)                 ; Prolog list of ascii codes
  58.   (do ((lch (list (char-int ch)) (push (char-int (read-char)) lch)))
  59.       ((char= (peek-char) #\") (read-char) (do_l (reverse lch)))))
  60.  
  61. (defun read_var (ch n)                  ; next variable
  62.  (status (read_atom ch) n))
  63.  
  64. (defun status (nom n)                   ; n is the term's depth
  65.  (if (= n 1)                           ; local if not yet global
  66.      (unless (member nom *lvarglob) (pushnew nom *lvarloc))
  67.    (progn (if (member nom *lvarloc)    ; else global
  68.               (setq *lvarloc (delete nom *lvarloc)))
  69.           (pushnew nom *lvarglob)))
  70.  nom)
  71.  
  72. (defun read_simple (ch n)               ; next simple term
  73.  (cond
  74.   ((or (upper-case-p ch) (special ch)) (read_var ch n))
  75.   ((digit-char-p ch) (read_number ch))
  76.   ((char= ch #\") (read_string (read-char)))
  77.   ((char= ch #\') (read_at (read-char)))
  78.   (t (read_atom ch))))
  79.  
  80. (defun read_fct (ch n)                  ; next functional term
  81.  (let ((fct (read_simple ch n)) (c (rchnsep)))
  82.    (if (char= c #\()                   ; reads its arguments
  83.        (let ((la (read_args (rchnsep) (1+ n))))
  84.          (cons (list fct (length la)) la)) ; adds its descriptor
  85.      (progn (unread-char c) fct))))    ; else atom
  86.                
  87. (defun read_args (ch n)                 ; args of a functional term
  88.  (let ((arg (read_term ch n)))
  89.    (if (char= (rchnsep) #\,)
  90.        (cons arg (read_args (rchnsep) n))
  91.      (list arg))))
  92.  
  93. (defun read_list (ch n)                 ; next list
  94.  (if (char= ch #\])                    ; ending with the empty list
  95.      ()
  96.    (let ((te (read_term ch n)))        ; gets the head
  97.      (case (rchnsep)
  98.            (#\, (list '(\.  2) te (read_list (rchnsep) n)))
  99.            (#\| (prog1                 ; dotted pair
  100.                     (list '(\. 2) te (read_term (rchnsep) n))
  101.                   (rchnsep)))
  102.            (#\] (list '(\. 2) te nil))))))
  103.  
  104. (defun read_term (ch n)                 ; next term
  105.  (if (char= ch #\[) (read_list (rchnsep) (1+ n)) (read_fct ch n)))
  106.  
  107. (defun read_tail (ch)                   ; read the body of a rule
  108.  (let ((tete (read_pred ch)))          ; gets the first goal
  109.    (if (char= (rchnsep) #\.)
  110.        (list tete)
  111.      (cons tete (read_tail (rchnsep))))))
  112.  
  113. (defun read_clause (ch)                 ; reads a clause
  114.  (let ((tete (read_pred ch)))
  115.    (if (char= (rchnsep) #\.)
  116.        (list tete)
  117.      (progn (read-char) (cons tete (read_tail (rchnsep)))))))
  118.  
  119. (defun c (l)                            ; codes once read
  120.  (if (atom l)
  121.      (if (member l *lvarloc)
  122.          (cons 'L (position l *lvarloc))
  123.        (if (member l *lvarglob) (cons 'G (position l *lvarglob)) l))
  124.    (if (eq (car l) '!)                 ; the cut with its intern
  125.        (list '! (length *lvarloc))     ; argument
  126.      (cons (c (car l)) (c (cdr l))))))
  127. ; mini-PrologII
  128. ; parser.lsp
  129. ;
  130.  
  131. (defun read_code_cl ()          ; to read and code a clause
  132.  (let ((*lvarloc ()) (*lvarglob ()))
  133.    (let ((x (read_clause (rchnsep))))
  134.      (maj_locglob (car x) (car (last x)))
  135.       (cons             ; number of local and global
  136.       (cons (length *lvarloc) (length *lvarglob)) ; vars
  137.       (c x)))))
  138.              
  139. (defun read_code_tail ()        ; idem for a query
  140.  (setq *lvarloc () *lvarglob ())
  141.  (let ((x (read_tail (rchnsep))))
  142.    (cons
  143.     (cons (length *lvarloc) (length *lvarglob))
  144.     (append (c x) (list '(|true|))))))
  145.  
  146. (defun read_pred (ch)           ; to read a literal
  147.  (let ((nom (read_atom ch)) (c (rchnsep)))
  148.    (if (char= c #\()
  149.     (cons nom
  150.           (read_args (rchnsep)    
  151.              (if (member nom '(|dif| |freeze|))
  152.                  2      ; a var occurring in a dif
  153.                1)))     ; or in a freeze is global
  154.      (progn (unread-char c) (list nom)))))
  155.  
  156. (defun unsafe? (x h q)          ; occurs in the body
  157.  (and (member x q) (not (member x h)))) ; but not in the head
  158.  
  159. (defun maj_locglob (h q)        ; to classify the variables
  160.  (mapc #'(lambda (x)
  161.         (when (unsafe? x h q)
  162.           (setq *lvarloc (delete x *lvarloc))
  163.           (push x *lvarglob)))
  164.     *lvarloc))
  165. ; mini-PrologII
  166. ; blocks.lsp
  167. ;
  168.  
  169. ; I. Registers
  170. ;
  171. (defconstant BottomFR 1)        ; bottom of frozen goals stack
  172. (defconstant BottomG 3000)      ; bottom of global stack
  173. (defconstant BottomL 6000)      ; bottom of local stack
  174. (defconstant BottomTR 10000)        ; bottom of trail
  175. (defconstant A 12000)           ; max of trail
  176.  
  177. (defvar Mem (make-array 12050 :initial-element 0))
  178. (defvar FR)             ; top of frozen goals stack
  179. (defvar TR)             ; top of trail
  180. (defvar L)              ; top of local stack
  181. (defvar G)              ; top of global stack
  182. (defvar CP)             ; current continuation
  183. (defvar CL)
  184. (defvar Cut_pt)             ; specific cut
  185. (defvar FRCP)               ; awakened goals
  186. (defvar BL)             ; backtracking registers
  187. (defvar BG)
  188. (defvar PC)             ; currents goal and its
  189. (defvar PCE)                ; environments
  190. (defvar PCG)
  191. (defvar Duboulot)
  192.  
  193. (defmacro vset (v i x) `(setf (svref ,v ,i) ,x))
  194.  
  195. ; II. Local Stack
  196. ;
  197. ;deterministic block [CL CP G Cut E]
  198. ;
  199. (defmacro CL (b) `(svref Mem ,b))
  200. (defmacro CP (b) `(svref Mem (1+ ,b)))
  201. (defmacro G (b) `(svref Mem (+ ,b 2)))
  202. (defmacro Cut (b) `(svref Mem (+ ,b 3)))
  203. (defmacro E (b) `(+ ,b 4))            
  204.  
  205. (defmacro push_cont ()          ; saves continuation
  206.  `(progn (vset Mem L CL) (vset Mem (1+ L) CP)))
  207.  
  208. (defmacro push_E (n)            ; allocates local env
  209.   `(let ((top (+ L 4 ,n))) 
  210.     (if (>= top BottomTR)
  211.      (throw 'debord (print "Local Stack Overflow")))
  212.     (vset Mem (+ L 3) Cut_pt)
  213.      (dotimes (i ,n top)        ; n local free variables
  214.           (vset Mem (decf top) (cons 'LIBRE BottomG)))))
  215.  
  216. (defmacro maj_L (nl)            ; updates top of local stack
  217.  `(incf L (+ 4 ,nl)))
  218.  
  219.  
  220. ;choice-point : [a1 .. an A FR BCP BCL BG BL BP TR]
  221. ;
  222. (defmacro TR (b) `(svref Mem (1- ,b)))
  223. (defmacro BP (b) `(svref Mem (- ,b 2)))
  224. (defmacro BL (b) `(svref Mem (- ,b 3)))
  225. (defmacro BG (b) `(svref Mem (- ,b 4)))
  226. (defmacro BCL (b) `(svref Mem (- ,b 5)))
  227. (defmacro BCP (b) `(svref Mem (- ,b 6)))
  228. (defmacro FR (b) `(svref Mem (- ,b 7)))
  229. (defmacro A (b) `(svref Mem (- ,b 8)))
  230.  
  231. (defun save_args ()         ; save args of current goal
  232.  (dotimes (i (svref Mem A) (vset Mem (incf L i) i))
  233.        (vset Mem (+ L i) (svref Mem (+ A i 1)))))
  234.  
  235. (defun push_choix ()            ; allocates a choice point
  236.   (save_args)               ; goal args
  237.   (vset Mem (incf L) FR)        ; top of f.g. stack
  238.   (vset Mem (incf L) CP)        ; continuation
  239.  (vset Mem (incf L) CL)
  240.   (vset Mem (incf L) G)         ; top of global stack
  241.   (vset Mem (incf L) BL)        ; last choice point
  242.   (vset Mem (incf L 2) TR)      ; top of trail
  243.   (setq BL (incf L) BG G))      ; new last choice point
  244.        
  245. (defun push_bpr (reste) (vset Mem (- BL 2) reste))
  246.    
  247. (defmacro size_C (b) `(+ 8 (A ,b)))
  248.  
  249. (defun pop_choix ()            
  250.   (setq L (- BL (size_C BL))        ; pops the block
  251.     BL (BL BL)          ; updates backtrack reg.
  252.     BG (if (zerop BL) BottomG (BG BL))))
  253.  
  254. ; III. Global Stack
  255. ;
  256. (defmacro push_G (n)
  257.  `(let ((top (+ G ,n)))
  258.     (if (>= top BottomL)
  259.      (throw 'debord (print "Global Stack Overflow")))
  260.     (dotimes (i ,n (vset Mem (+ L 2) G)) ; n free global vars
  261.           (vset Mem (decf top) (cons 'LIBRE BottomG)))))
  262. (defmacro maj_G (n) `(incf G ,n))
  263.  
  264. ;IV. Trail
  265. ;
  266. (defmacro fgblock (x) `(cdr (svref Mem ,x)))
  267.  
  268. (defun pushtrail (x)
  269.  (if (>= TR A) (throw 'debord (print "Trail Overflow")))
  270.  (vset Mem TR x)
  271.  (incf TR))
  272.  
  273. (defun poptrail (top)          
  274.  (do () ((= TR top))
  275.      (let ((x (svref Mem (decf TR))))
  276.     (if (numberp x)         ; classical var else frozen
  277.         (vset Mem x (cons 'LIBRE BottomG))
  278.       (vset Mem (car x) (cons 'LIBRE (cdr x)))))))
  279.  
  280. ; V. Frozen Goals Stack
  281. ;
  282. (defmacro FGvar (x) `(svref Mem ,x))
  283. (defmacro FGtail (x) `(svref Mem (1+ ,x)))
  284. (defmacro FGgoal (x) `(svref Mem (+ 2 ,x)))
  285. (defmacro FGenv (x) `(svref Mem (+ 3 ,x)))
  286. (defmacro frozen? (x) `(< (fgblock ,x) BottomG))
  287.  
  288. (defmacro push_fg (v b eb r)        ; allocates a frozen block
  289.  `(if (>= (+ FR 3) BottomG)
  290.       (throw 'debord (print "Frozen Goals Stack Overflow"))
  291.     (progn (vset Mem FR ,v)
  292.         (vset Mem (incf FR) ,r)
  293.         (vset Mem (incf FR) ,b)
  294.         (vset Mem (incf FR) ,eb)
  295.         (incf FR))))
  296. ; miniCprolog & Mini-PrologII
  297. ; cutili.lsp
  298. ;
  299.  
  300. (defmacro nloc (c) `(caar ,c))          ; number of local vars
  301. (defmacro nglob (c) `(cdar ,c))         ; number of global vars
  302. (defmacro head (c) `(cadr ,c))          ; head of a clause
  303. (defmacro tail (c) `(cddr ,c))          ; body of a clause
  304. (defmacro pred (g) `(car ,g))           ; predicate symbol
  305. (defmacro largs (g) `(cdr ,g))          ; list of arguments
  306.  
  307. (defmacro functor (des) `(car ,des))
  308. (defmacro arity (des) `(cadr ,des))
  309. (defmacro des (te) `(car ,te))          ; functor/arity
  310. (defmacro var? (v) `(and (consp ,v) (numberp (cdr ,v))))
  311. (defmacro list? (x) `(eq (functor (des ,x)) '\.))
  312.  
  313. (defmacro user? (g)                     ; user defined predicate
  314.  `(get (pred ,g) 'def))
  315. (defmacro builtin? (g)                  ; builtin predicate
  316.  `(get (pred ,g) 'evaluable))
  317. (defmacro def_of (g)                    ; gets the subset of clauses
  318.  `(get (pred ,g)                       ; depending on the nature of
  319.        (if (largs ,g)                  ; the first arg of goal PC
  320.            (nature (car (ultimate (car (largs ,g)) PCE PCG)))
  321.          'def)))
  322.  
  323. (defun nature (te)                      ; determines the associated
  324.  (cond                                 ; subset according to
  325.   ((var? te) 'def)                     ; clause indexing
  326.   ((null te) 'empty)
  327.   ((atom te) 'atom)
  328.   ((list? te) 'list)
  329.   (t 'fonct)))
  330.  
  331. (defun add_cl (pred c ind)              ; adds a clause to the end
  332.  (setf (get pred ind) (append (get pred ind) (list c))))
  333.  
  334. (set-macro-character                    ; to automatically read,
  335. #\$                                    ; code and index
  336. #'(lambda (stream char)
  337.     (let* ( (*standard-input* stream) (c (read_code_cl)))
  338.       (add_cl (pred (head c)) c 'def)  ; always in the var subset
  339.       (if (largs (head c))
  340.           (let ((b (nature (car (largs (head c))))))
  341.             (if (eq b 'def)            ; first arg is a variable
  342.                 (mapc                  ; add c to all the subsets
  343.                  #' (lambda (x) (add_cl (pred (head c)) c x))
  344.                  '(atom empty list fonct))
  345.               (add_cl (pred (head c)) c b))))) ; add to only one subset
  346.     (values)))
  347.  
  348. (defun answer ()                        ; prints the values of vars
  349.  (printvar)                            ; occurring in the query
  350.  (if (zerop BL)                        ; no more choice point
  351.      (setq Duboulot nil)
  352.    (if (and (princ "More : ")          ; asks the user
  353.             (string= (read-line) ";")) ; if necessary
  354.         (backtrack)                     ; forces backtracking
  355.       (setq Duboulot nil))))
  356.        
  357. (defun printvar ()
  358.   (if (and (null *lvarloc)              ; ground query ?
  359.            (null *lvarglob))
  360.       (format t "Yes ~%")               ; success
  361.     (let ((nl -1) (ng -1))
  362.       (mapc                             ; first, local variables
  363.        #' (lambda (x)
  364.             (format t "~A = " x)
  365.             (write1 (ult (cons 'L (incf nl)) (E BottomL))) (terpri))
  366.        *lvarloc)
  367.       (mapc                             ; then global variables
  368.        #' (lambda (x)
  369.             (format t "~A = " x)
  370.             (write1 (ult (cons 'G (incf ng)) BottomG)) (terpri))
  371.        *lvarglob))))
  372. ; mini-PrologII
  373. ; unify.lsp
  374. ;
  375.  
  376. (defmacro bind (x sq e xt)      ; binds x to (sq,e)
  377.   `(progn (if (or (and (> ,x BottomL) (< ,x BL)) (< ,x BG))
  378.           (pushtrail ,xt))      ; if post-bound trail it
  379.       (rplaca (svref Mem ,x) ,sq)
  380.       (rplacd (svref Mem ,x) ,e)))
  381.  
  382. (defun bindte (x sq e)          ; binds x to (sq,e)
  383.   (if (frozen? x)
  384.       (let ((y (fgblock x)))
  385.     (push y FRCP)           ; awakes the delayed goals
  386.     (bind x sq e (cons x y)))   ; to trail the old value
  387.     (bind x sq e x)))
  388.  
  389. (defun bindfg (x b eb r)        ; binds x to the frozen goal b
  390.   (bind x 'LIBRE FR (if (frozen? x) (cons x r) x))
  391.   (push_fg x b eb r))           ; allocates a new frozen block
  392.  
  393. (defun unify_with (largs el eg)     ; unifies head of clause
  394.   (catch 'impossible            ; with registers Ai
  395.     (dotimes (i (svref Mem A))
  396.          (unif
  397.           (let ((te (svref Mem (+ A 1 i)))) (val (car te) (cdr te)))
  398.           (ultimate (pop largs) el eg)))))
  399.  
  400. ; mini-Cprolog & mini-PrologII
  401. ; cunify.lsp
  402. ;
  403. (defmacro adr (v e) `(+ (cdr ,v) ,e))
  404. (defmacro value (v e) `(svref Mem (adr ,v ,e)))
  405.      
  406. (defun ult (v e)                        ; dereferences variable v
  407.   (let ((te (value v e)))               ; in environment e
  408.     (cond
  409.      ((eq (car te) 'LIBRE) (cons v e))  ; if unbound, itself
  410.      ((var? (car te)) (ult (car te) (cdr te)))
  411.      ( te))))                           ; its value
  412.  
  413. (defun val (x e)                        ; generalises to a term
  414.   (if (var? x) (ult x e) (cons x e)))
  415.  
  416. (defun ultimate (x el eg)               ; idem but selects env
  417.   (if (var? x)  
  418.       (if (eq (car x) 'L) (ult x el) (ult x eg))
  419.     (cons x eg)))
  420.  
  421. (defmacro bindv (x ex y ey)             ; L2 Binding
  422.   `(let ((ax (adr ,x ,ex)) (ay (adr ,y ,ey)))
  423.      (if (< ax ay)                      ; the younger one is always
  424.          (bindte ay ,x ,ex)             ; bound to the senior one
  425.        (bindte ax ,y ,ey))))
  426.  
  427. (defun unif (t1 t2)                     ; unify two terms t1 and t2
  428.   (let ((x (car t1)) (ex (cdr t1)) (y (car t2)) (ey (cdr t2)))
  429.     (cond
  430.      ((var? y)  
  431.       (if (var? x)                      ; two variables
  432.           (if (= (adr x ex) (adr y ey)) t (bindv y ey x ex))
  433.         (bindte (adr y ey) x ex)))      ; binds y
  434.      ((var? x) (bindte (adr x ex) y ey))
  435.      ((and (atom x) (atom y))           ; two constants
  436.       (if (eql x y) t (throw 'impossible 'fail)))
  437.      ((or (atom x) (atom y)) (throw 'impossible 'fail))
  438.      ( (let ((dx (pop x)) (dy (pop y))) ; two structured terms
  439.          (if (and (eq (functor dx) (functor dy))
  440.                   (= (arity dx) (arity dy)))
  441.              (do () ((null x))          ; same functor and arity
  442.                  (unif (val (pop x) ex) (val (pop y) ey)))
  443.            (throw 'impossible 'fail)))))))
  444. ; mini-PrologII
  445. ; resol.lsp
  446. ;
  447.  
  448. (defun forward ()
  449.   (do () ((null Duboulot) (format t "no More~%"))      
  450.       (cond ((and (null CP)     ; empty continuation
  451.           (null FRCP))      ; no awaken goals
  452.          (answer))      
  453.         ((load_PC)          ; selects first goal
  454.          (cond                  
  455.           ((user? PC)       ; user defined
  456.            (let ((d (def_of PC)))   ; associated set of clauses
  457.          (if d (pr2 d) (backtrack))))
  458.           ((builtin? PC)        ; builtin
  459.            (if (eq (apply (car PC) (cdr PC)) 'fail)
  460.            (backtrack)      ; evaluation fails
  461.          (cont_eval)))              
  462.           ((backtrack)))))))    ; undefined predicate
  463.  
  464. (defun load_A (largs el eg)     ; loads Ai registers with
  465.   (dotimes (i (length largs) (vset Mem A i)) ; goal's args
  466.        (vset Mem (+ A i 1) (ultimate (pop largs) el eg))))
  467.  
  468. (defun load_PC ()
  469.   (if FRCP  
  470.       (let ((x ()))  
  471.     (do ()              ; sorts the goals depending on
  472.         ((null FRCP))       ; their freezing times
  473.         (setq x (add_fg (pop FRCP) x)))
  474.     (do ()              ; allocates blocks in the
  475.         ((null x))          ; corresponding order
  476.         (create_block (abs (pop x))))))
  477.   (setq PC (pop CP) PCE (E CL) PCG (G CL) Cut_pt BL))
  478.  
  479. (defun other_fg (b r)
  480.   (if (< (FGtail b) BottomG) (add_fg (FGtail b) r) r))
  481.  
  482. (defun add_fg (b r)         ; sorts the various awakened
  483.   (let ((b1 (if (numberp (FGgoal b)) (FGgoal b) b))) ; goals
  484.     (if (eq (pred (FGgoal b1)) '|dif|)  ; dif first
  485.     (insert (- b1) (other_fg b r))
  486.       (let* ((v (svref Mem (FGvar b1))) ; it is a freeze
  487.          (te (val (car v) (cdr v))))
  488.     (if (var? (car te))     ; the var is still unbound
  489.         (let ((y (adr (car te) (cdr te))))
  490.           (bindfg y b1 nil (fgblock y)) ; delaying is perpetuated
  491.           (other_fg b r))
  492.       (insert b1 (other_fg b r))))))) ; insert the goal
  493.  
  494. (defun insert (b l)         ; sorts the goals according to
  495.   (if (or (null l) (> b (car l)))   ; their freezing times
  496.       (cons b l)
  497.     (cons (car l) (insert b (cdr l)))))
  498.  
  499. (defmacro dec_goal (x)
  500.   `(if (atom ,x) (list ,x) (cons (caar ,x) (cdr ,x))))
  501.  
  502. (defun create_block (b)         ; block for an awakened goal
  503.   (push_cont)               ; saves current continuation
  504.   (vset Mem (+ L 2) (FGenv b))      ; its global env
  505.   (vset Mem (+ L 3) Cut_pt)     ; the corresponding cut point
  506.   (setq CP (list (FGgoal b)) CL L)  ; new continuation
  507.   (maj_L 0))                ; ends the block
  508.  
  509. (defun pr2 (paq)            ; proves PC
  510.   (load_A (largs PC) PCE PCG)       ; loads its arguments
  511.   (if CP  
  512.       (pr paq)
  513.     (progn              ; if last call and no interm.
  514.       (if (<= BL CL) (setq L CL))   ; choice point then LCO
  515.       (setq CP (CP CL) CL (CL CL))  ; next continuation
  516.       (pr paq))))
  517.          
  518. (defun cont_eval ()
  519.   (unless CP (if (<= BL CL) (setq L CL)) (setq CP (CP CL) CL (CL CL))))
  520.  
  521. (defun pr (paq)                  
  522.   (if (cdr paq)             ; alloc. choice point
  523.       (progn (push_choix) (pr_choice paq))
  524.     (pr_det (car paq))))        ; else deterministic
  525.  
  526. (defun pr_det (c)
  527.   (if (eq (unify_with           ; first tries to unify
  528.        (largs (head c))
  529.        (push_E (nloc c))
  530.        (push_G (nglob c)))  
  531.           'fail)
  532.       (backtrack)
  533.     (progn              ; success
  534.       (maj_G (nglob c))         ; terminates global env
  535.       (when (tail c)            ; c is rule
  536.         (push_cont) saves current cont.
  537.         (setq CP (tail c) CL L) ; new one
  538.         (maj_L (nloc c))))))    ; terminates local block
  539.  
  540. (defun pr_choice (paq)
  541.   (let* ((resu (shallow_backtrack paq)) (c (car resu)) (r (cdr resu)))
  542.     (cond ((null r)         ; only one candidate remains
  543.        (pop_choix)          ; pops the rerun part
  544.        (pr_det c))          ; proof is now deterministic
  545.       ( (push_bpr r)                  
  546.         (maj_G (nglob c))                    
  547.         (when (tail c)      ; c is a rule
  548.           (push_cont)       ; saves current cont.              
  549.           (setq CP (tail c) CL L) ; new one
  550.           (maj_L (nloc c))))))) ; terminates local block
  551.  
  552. (defun shallow_backtrack (paq)      ; looks for a first success
  553.   (if (and (cdr paq)            ; more than one candidate
  554.        (eq (unify_with      ; and unification fails
  555.         (largs (head (car paq)))
  556.         (push_E (nloc (car paq)))
  557.         (push_G (nglob (car paq))))
  558.            'fail))
  559.       (progn
  560.     (setq FRCP nil FR (FR BL))  ; restores registers
  561.     (poptrail (TR BL))      ; restores env
  562.     (shallow_backtrack (cdr paq)))
  563.     paq))
  564.  
  565. (defun backtrack ()            
  566.   (if (zerop BL)            ; no more choice points
  567.       (setq Duboulot nil)       ; restores registers
  568.     (progn (setq L BL G BG FR (FR L) FRCP nil Cut_pt (BL BL)
  569.          CP (BCP L) CL (BCL L) Cut_pt (BL BL))
  570.        (load_A2)            
  571.        (poptrail (TR BL))       ; restores env
  572.        (pr_choice (BP L)))))    ; relaunch the proof
  573.    
  574. (defun load_A2 ()           ; restores Ai registers
  575.   (let ((deb (- L (size_C L))))
  576.     (dotimes (i (A L) (vset Mem A i))
  577.          (vset Mem (+ A i 1) (svref Mem (+ deb i))))))
  578.  
  579. (defun myloop (c)
  580.   (setq FR BottomFR G BottomG L BottomL TR BottomTR Cut_pt 0
  581.         CP nil CL 0  BL 0 BG BottomG FRCP nil Duboulot t)
  582.   (push_cont)               ; initial continuation
  583.   (push_E (nloc c))         ; local env. for the query
  584.   (push_G (nglob c))            ; global env. for the query
  585.   (setq CP (cdr c) CL L)        ; current continuation
  586.   (maj_L (nloc c))          ; ends local block
  587.   (maj_G (nglob c)) (read-char)     ; ends global block
  588.   (catch 'debord (forward))
  589.   (myloop (read_prompt)))
  590.  
  591. ; Mini-PrologII
  592. ; pred.lsp
  593. ;
  594. (defvar Ob_Micro_Log
  595.       '(|write| |nl| |tab| |read| |get| |get0|
  596.     |var| |nonvar| |atomic| |atom| |number|
  597.     ! |fail| |true|
  598.     |divi| |mod| |plus| |minus| |mult| |le| |lt|
  599.     |name| |consult| |abolish| |cputime| |statistics|
  600.     |call| |freeze| |dif| |frozen_goals|))
  601. (mapc #'(lambda (x) (setf (get x 'evaluable) t)) Ob_Micro_Log)
  602.  
  603. ; !/0
  604. (defun ! (n)
  605.   (setq BL (Cut CL) BG (if (zerop BL) BottomG (BG BL))
  606.     L (+ CL 4 n)))          ; updates local stack
  607.  
  608. ; call/1 (+term)
  609. (defun |call| (x)
  610.   (if (var? x)
  611.       (let ((te (ultimate x PCE PCG)))  ; dereferences it
  612.     (unless CP
  613.         (if (<= BL CL) (setq L CL)) ; applies LCO
  614.         (setq CP (CP CL) CL (CL CL))) ; new continuation
  615.     (push_cont)         ; saves continuation
  616.     (vset Mem (+ L 2) (cdr te)) ; global env.
  617.     (vset Mem (+ L 3) Cut_pt)   ; cut point
  618.     (setq CP (list (dec_goal (car te))) CL L)
  619.     (maj_L 0))          ; ends local block
  620.     (push (dec_goal x) CP)))        ; adds it to CP
  621.  
  622. ; freeze/2 (?var,+term)
  623. (defun |freeze| (x p)
  624.   (let ((xte (ultimate x PCE PCG))) ; dereferences the var
  625.     (if (var? (car xte))        ; unbound
  626.     (let ((y (adr (car xte) (cdr xte))) ; the location
  627.           (pte (ultimate p PCE PCG))) ; dereferences the goal
  628.       (bindfg y (dec_goal (car pte)) (cdr pte) (fgblock y)))
  629.       (|call| p))))         ; else call p
  630.  
  631. ; dif/2 (?term,?term)
  632. (defun |dif| (x y)
  633.   (let ((BL L) (BG G) (str TR) (FRCP nil)) ; saves registers
  634.     (if (eq (uni x y) 'fail)        ; unification fails
  635.     (poptrail str)          ; restores env and succeeds
  636.       (if (/= TR str)           ; one var bound
  637.       (let* ((xv (svref Mem (1- TR))) ; selects one var
  638.          (v (if (numberp xv) xv (car xv))))
  639.         (poptrail str)      ; restores env
  640.         (bindfg v PC PCG (fgblock v))) ; perpetuates the delaying
  641.     'fail))))           ; fails if equals
  642.  
  643. ; statistics/0
  644. (defun |statistics| ()
  645.   (format t " local stack : ~A (~A used)~%"
  646.       (- BottomTR BottomL) (- L BottomL))
  647.   (format t " global stack : ~A (~A used)~%"
  648.       (- BottomL BottomG) (- G BottomG))
  649.   (format t " trail : ~A (~A used)~%"
  650.       (- A BottomTR) (- TR BottomTR))
  651.   (format t " frozen-goals stack : ~A (~A used)~%"
  652.       BottomG (- FR BottomFR)))
  653.  
  654. ; frozen_goals/0
  655. (defun |frozen_goals| ()
  656.   (do ((i (- FR 4) (- i 4)))        ; scans the frozen goals stack
  657.       ((< i 0))
  658.       (if (eq (car (svref Mem (FGvar i))) 'LIBRE) ; unbound
  659.       (let ((b (if (numberp (FGgoal i)) (FGgoal i) i)))
  660.         (writesf (pred (FGgoal b)) (largs (FGgoal b)) (FGenv b))
  661.         (format t " frozen upon X~A~%" (FGvar i))))))
  662. ; mini-Cprolog & mini-PrologII
  663. ; cpred.lsp
  664. ;
  665.  
  666. (defmacro value1 (x) `(car (ultimate ,x PCE PCG)))
  667. (defun uni (x y)
  668.   (catch 'impossible
  669.     (unif (ultimate x PCE PCG) (ultimate y PCE PCG))))
  670.                     ;write/1 (?term)
  671. (defun |write| (x)
  672.   (write1 (ultimate x PCE PCG)))
  673. (defun write1 (te)          ; depending on te
  674.   (let ((x (car te)) (e (cdr te)))
  675.     (cond
  676.      ((null x) (format t "[]"))
  677.      ((atom x) (format t "~A" x))
  678.      ((var? x) (format t "X~A" (adr x e)))
  679.      ((list? x) (format t "[")
  680.       (writesl (val (cadr x) e) (val (caddr x) e))
  681.       (format t "]"))
  682.      ((writesf (functor (des x)) (largs x) e)))))
  683. (defun writesl (te r)           ; for a list
  684.   (write1 te)
  685.   (let ((q (car r)) (e (cdr r)))
  686.     (cond
  687.      ((null q))
  688.      ((var? q) (format t "|X~A" (adr q e)))
  689.      (t (format t ",")
  690.     (writesl (val (cadr q) e) (val (caddr q) e))))))
  691. (defun writesf (fct largs e)        ; for a functional term
  692.   (format t "~A(" fct)
  693.   (write1 (val (car largs) e))
  694.   (mapc #' (lambda (x) (format t ",") (write1 (val x e)))
  695.        (cdr largs))
  696.   (format t ")"))
  697.                     ;nl/0
  698. (defun |nl| () (terpri))
  699.                     ;tab/1 (+int)
  700. (defun |tab| (x)
  701.   (dotimes (i (value1 x)) (format t " ")))
  702.                     ;read/1 (?term)
  703. (defun |read| (x)
  704.   (let ((te (read_terme)))      ; gets the term
  705.     (catch 'impossible
  706.       (unif (ultimate x PCE PCG)
  707.         (cons (cdr te) (push1_g (car te)))))))
  708. (defun read_terme ()
  709.   (let ((*lvarloc nil) (*lvarglob nil))
  710.     (let ((te (read_term (rchnsep) 2)))
  711.       (rchnsep) (cons (length *lvarglob) (c te)))))
  712. (defun push1_g (n)
  713.   (if (>= (+ G n) BottomL)      ; allocates a global env
  714.       (throw 'debord (print "Global Stack Overflow")))
  715.   (dotimes (i n (- G n))
  716.        (vset Mem G (cons 'LIBRE BottomG))
  717.        (incf G)))
  718.                     ;get/1 (?car)
  719. (defun |get| (x)
  720.   (uni x (char-int (rchnsep))))
  721.                     ;get0/1 (?car)
  722. (defun |get0| (x)
  723.   (uni x (char-int (read-char))))
  724.                     ;var/1 (?term)
  725. (defun |var| (x)
  726.   (unless (var? (value1 x)) 'fail))
  727.                     ;nonvar/1 (?term)
  728. (defun |nonvar| (x)
  729.   (if (var? (value1 x)) 'fail))
  730.                     ;atomic/1 (?term)
  731. (defun |atomic| (x)
  732.   (if (listp (value1 x)) 'fail))
  733.                     ;atom/1 (?term)
  734. (defun |atom| (x)
  735.   (unless (symbolp (value1 x)) 'fail))
  736.                     ;number/1 (?term)
  737. (defun |number| (x)
  738.   (unless (numberp (value1 x)) 'fail))
  739.                     ;fail/0
  740. (defun |fail| () 'fail)
  741.                     ;true/0
  742. (defun |true| ())
  743.                     ;divi/3 (+int,+int,?int)
  744. (defun |divi| (x y z)
  745.   (uni z (floor (value1 x) (value1 y))))
  746.                     ;mod/3 (+int,+int,?int)
  747. (defun |mod| (x y z)
  748.   (uni z (rem (value1 x) (value1 y))))
  749.                     ;plus/3 (+int,+int,?int)
  750. (defun |plus| (x y z)
  751.   (uni z (+ (value1 x) (value1 y))))
  752.                     ;minus/3 (+int,+int,?int)
  753. (defun |minus| (x y z)
  754.   (uni z (- (value1 x) (value1 y))))
  755.                     ;mult/3 (+int,+int,?int)
  756. (defun |mult| (x y z)
  757.   (uni z (* (value1 x) (value1 y))))
  758.                     ;le/2 (+int,+int)
  759. (defun |le| (x y)
  760.   (if (> (value1 x) (value1 y)) 'fail))
  761.                     ;lt/2 (+int,+int)
  762. (defun |lt| (x y)
  763.   (if (>= (value1 x) (value1 y)) 'fail))
  764.                     ;name/2 (?atom,?list)
  765. (defun |name| (x y)
  766.   (let ((b (value1 x)))
  767.      (if (var? b)
  768.          (uni x (impl (undo_l (ultimate y PCE PCG))))
  769.          (uni y (do_l (expl b))))))
  770.  
  771. (defun undo_l (te)
  772.   (let ((x (car te)) (e (cdr te)))
  773.     (if (atom x)
  774.     x
  775.       (cons (undo_l (val (cadr x) e)) (undo_l (val (caddr x) e))))))
  776. (defun do_l (x)
  777.   (if (atom x) x (list '(\. 2) (car x) (do_l (cdr x)))))
  778. (defun impl (l)
  779.   (intern (map 'string #'int-char l)))
  780. (defun expl (at)
  781.   (map 'list #'char-int (string at)))
  782.  
  783.                     ;consult/1 (+atom)
  784. (defun |consult| (f)
  785.   (format t "~A~%" (load (value1 f))))
  786.                     ; abolish/1 (+ atom)
  787. (defun |abolish| (p)
  788.   (mapc  #'(lambda (x) (setf (get p x) nil))
  789.      '(atom empty list fonct def)))
  790.                     ; cputime/1 (? int)
  791. (defun |cputime| (x)
  792.   (uni x (float (/ (get-internal-run-time)
  793.            internal-time-units-per-second))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement