File: types.lisp

package info (click to toggle)
cafeobj 1.6.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 19,900 kB
  • sloc: lisp: 85,055; sh: 659; makefile: 437; perl: 147
file content (918 lines) | stat: -rw-r--r-- 29,785 bytes parent folder | download | duplicates (2)
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
;;;-*-Mode:LISP; Package: CHAOS; Base:10; Syntax:Common-lisp -*-
;;;
;;; Copyright (c) 2000-2015, Toshimi Sawada. All rights reserved.
;;;
;;; Redistribution and use in source and binary forms, with or without
;;; modification, are permitted provided that the following conditions
;;; are met:
;;;
;;;   * Redistributions of source code must retain the above copyright
;;;     notice, this list of conditions and the following disclaimer.
;;;
;;;   * Redistributions in binary form must reproduce the above
;;;     copyright notice, this list of conditions and the following
;;;     disclaimer in the documentation and/or other materials
;;;     provided with the distribution.
;;;
;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED
;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
;;; ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
;;;
;;;
(in-package :chaos)
#|=============================================================================
                             System:Chaos
                            Module:BigPink
                           File:types.lisp
=============================================================================|#
#-:chaos-debug
(declaim (optimize (speed 3) (safety 0) #-GCL (debug 0)))
#+:chaos-debug
(declaim (optimize (speed 1) (safety 3) #-GCL (debug 3)))

;;;                     *********************
;;;                     BASIC Data Structures
;;;                     *********************

#-(or draft-ansi-cl-2 ansi-cl :clisp)
;; (deftype boolean () '(or null (not null)))

;; #+:cmu
;; (deftype boolean () '(or null (not null)))

;;; =====
;;; QUEUE
;;; =====
;;; not used now.

;;; q -> (front-ptr . last-ptr)
;;;          |            |
;;;          V            V
;;;      (item1, ...., last-item)
;;;
(defun make-queue () (cons nil nil))

(defun list->queue (list)
  (cons list
        (last list)))

(defmacro queue-front-ptr (q)
  `(car ,q))

(defmacro queue-rear-ptr (q)
  `(cdr ,q))

(defmacro queue-set-front-ptr (q item)
  `(setf (car ,q) ,item))

(defmacro queue-set-rear-ptr (q item)
  `(setf (cdr ,q) ,item))

(defmacro empty-queue? (q)
  `(null (queue-front-ptr ,q)))

(defun queue-front (q)
  (if (empty-queue? q)
      nil
    (car (queue-front-ptr q))))

(defun queue-insert (q item)
  (let ((new-pair (cons item nil)))
    (cond ((empty-queue? q)
           (queue-set-front-ptr q new-pair)
           (queue-set-rear-ptr q new-pair)
           q)
          (t
           (setf (cdr (queue-rear-ptr q)) new-pair)
           (queue-set-rear-ptr q new-pair)
           q))))

(defun delete-queue (q)
  (cond ((empty-queue? q) nil)
        (t (queue-set-front-ptr q
                                (cdr (queue-front-ptr q))))))

;;; =======
;;; LITERAL 
;;; =======
(defstruct (literal (:print-function pr-literal) (:copier nil))
  (clause nil :type (or null clause))   ; containing clause
  (atom nil :type (or null term))       ; the body -- term
  (sign t :type symbol)                 ; nil if negation
  (type nil :type symbol)               ; :pos-eq, :neg-eq, :evaluable
                                        ; :conditional-demod
                                        ; :normal-atom
  (stat-bits 0 :type fixnum)            ; various bit flags
  )

;;; STAT-BITS
(defconstant scratch-bit #x01)
(defconstant oriented-eq-bit #x02)

(defmacro test-bit (x bit)
  `(not (= 0 (logand (the fixnum ,x) (the fixnum ,bit)))))

(defmacro set-bit (x y)
  `(setf ,x (logior ,x (the fixnum ,y))))

(defmacro clear-bit (x y)
  `(setf ,x (logand ,x (lognot (the fixnum ,y)))))


;;; types of non variable term containing in a literal.
;;; :term          : not an atom
;;; :normal-atom   : normal atom
;;; :pos-eq        : psitive equality atom
;;; :neg-eq        : negative equality atom
;;; :answer        : answer lieteral atom
;;; :lex-dep-demod : lex-dependent domodulator atom
;;; :evaluable     : evaluable term
;;; :conditional-demod : conditional demodulator

;;; POSITIVE-LITERAL?
;;;
(defmacro positive-literal? (_lit)
  `(literal-sign ,_lit))

(defmacro negative-literal? (_lit)
  `(null (literal-sign ,_lit)))

;;; ANSWER-LITERAL? : Literal -> Bool
;;; *NOT YET*
(defmacro answer-literal? (_lit)
  `(eq (literal-type ,_lit) :answer))

;;; POSITIVE-EQ-LITERAL? : Literal -> Bool
;;; returns no-nil iff literal is a positive equality literal.
;;;
(defmacro positive-eq-literal? (lit)
  `(eq :pos-eq (literal-type ,lit)))

;;; NEGATIVE-EQ-LITERAL? : Literal -> Bool
;;; returns no-nil iff literal is a negative equality literal
;;;
(defmacro negative-eq-literal? (lit)
  `(eq :neg-eq (literal-type ,lit)))

;;; EQ-LITERAL? : Literal -> Bool
;;; returns no-nil iff literal is an equality literal (pos or neg)
;;;
(defmacro eq-literal? (literal)
  (once-only (literal)
   `(or (positive-eq-literal? ,literal)
        (negative-eq-literal? ,literal))))

;;; PROPOSITIONAL-LITERAL?
;;;
(declaim (inline propositional-literal?))

(defun propositional-literal? (lit)
  (declare (type literal lit))
  (null (term-variables (literal-atom lit))))

;;; LITERAL PRINTER

#||
(defun pr-literal (lit stream &rest ignore)
  (declare (type literal lit)
           (type stream stream)
           (ignore ignore))
  (let ((.printed-vars-so-far. .printed-vars-so-far.))
    (unless (literal-sign lit)
      (princ "~(" stream)
      (setq .file-col. (1+ .file-col.)))
    (with-in-module ((get-context-module))
      (cond ((eq-literal? lit)
             (let* ((lhs (term-arg-1 (literal-atom lit)))
                    (rhs (term-arg-2 (literal-atom lit)))
                    (*print-indent* *print-indent*))
               (setq *print-indent* (max *print-indent*
                                          .file-col.))
               (setq .printed-vars-so-far.
                 (append .printed-vars-so-far.
                         (term-print lhs stream)))
               (setq .file-col. (file-column stream))
               (princ " = ")
               #||
               (if (print-check 0 30) ; 30?
                   (princ "= ")
                 (princ " = "))
               ||#
               (setq *print-indent*
                 (max *print-indent*
                      (setq .file-col. (file-column stream))))
               (setq .printed-vars-so-far.
                 (append .printed-vars-so-far.
                         (term-print rhs stream)))
               ))
            (t (setq .printed-vars-so-far.
                 (append .printed-vars-so-far.
                         (term-print (literal-atom lit) stream))))
            )
      )
    (unless (literal-sign lit)
      (princ ")" stream))
    .printed-vars-so-far.)
  )
||#

(defun pr-literal (lit stream &rest ignore)
  (declare (type literal lit)
           (type stream stream)
           (ignore ignore))
  (let ((.printed-vars-so-far. .printed-vars-so-far.))
    (unless (literal-sign lit)
      (princ "~(" stream)
      (setq .file-col. (1+ .file-col.)))
    (with-in-module ((get-context-module))
      (setq .printed-vars-so-far.
        (append .printed-vars-so-far.
                (term-print (literal-atom lit) stream))))
    (unless (literal-sign lit)
      (princ ")" stream))
    .printed-vars-so-far.))

;;; 
;;; some gobal flags
;;; t if our logic has two diffrent types of equality.
;; (declaim (type boolean *fopl-two-equalities*))
(defvar *fopl-two-equalities* nil)

;;; primitive built-in modules for supporting inference in FOPL
(defvar *fopl-sentence-module* nil)
(defvar *fopl-clause-form-module* nil)
;;; primitive sorts declared in the aboves.
(defvar *fopl-sentence-sort* nil)
(defvar *var-decl-list-sort* nil)
(defvar *fopl-clause-sort* nil)
(defvar *fopl-sentence-seq-sort* nil)
;;; primitive formula constructors
(defvar *var-decl-list* nil)
(defvar *fopl-and* nil)
(defvar *fopl-or* nil)
(defvar *fopl-imply* nil)
(defvar *fopl-iff* nil)
(defvar *fopl-neg* nil)
(defvar *fopl-forall* nil)
(defvar *fopl-exists* nil)
(defvar *fopl-eq* nil)
(defvar *fopl-beq* nil)
(defvar *fopl-ans* nil)

;;; not used 
(defvar *clause-constructor* nil)
(defvar *clause-constructor2* nil)
(defvar *fopl-sentence-seq* nil)
;;;

(declaim (inline term-is-atom?))
(defun term-is-atom? (term)
  (declare (type term term))
  (sort<= (term-sort term) *fopl-sentence-sort* *current-sort-order*))

;;; ======
;;; CLAUSE
;;; ======

(defstruct (clause (:print-function print-clause)
            ;; copier is defined in `clause.lisp'
            (:copier nil))
  (parents nil :type list)              ; parents produces this clause
  (literals nil :type list)             ; list of literal
  (id -1 :type fixnum)
  (pick-weight -1 :type fixnum)
  (attributes nil :type list)
  (type nil :type symbol)
  (bits 0 :type fixnum)
  (heat-level 0 :type fixnum)
  (formula nil :type (or null term))    ; original formula
  (axiom nil :type (or null axiom))     ; derived axiom if any.
  (container nil :type symbol)          ; containing list, one of
                                        ; :sos, :usable, :other...
  )

;;; GET-CLAUSE : id -> Clause
;;;
(defmacro get-clause (_id _hash)
  `(gethash ,_id ,_hash))

;;; types of members of clause's parent lists.
;;; positive integers are clause IDs.
;;;
;;; :BINARY-RES-RULE
;;; :HYPER-RES-RULE
;;; :NEG-HYPER-RES-RULE
;;; :UR-RES-RULE
;;; :PARA-INTO-RULE
;;; :PARA-FROM-RULE
;;; :LINKED-UR-RES-RULE
;;; :GEO-RULE
;;; :FACTOR-RULE
;;; :NEW-DEMOD-RULE
;;; :BACK-DEMOD-RULE
;;; :DEMOD-RULE
;;; :UNIT-DEL-RULE
;;; :EVAL-RULE
;;; :GEO-ID-RULE
;;; :FACTOR-SIMP-RULE
;;; :COPY-RULE
;;; :FLIP-EQ-RULE
;;; :CLAUSIFY-RULE
;;; :BACK-UNIT-DEL-RULE
;;; :SPLIT-RULE
;;; :SPLIT-NEG-RULE
;;; :LIST-RULE
;;; :DISTINCT-CONSTANTS
;;;
(defparameter cl-print-mergine 30)

(defun print-clause (cl &optional (stream *standard-output*) &rest ignore)
  (declare (type clause cl)
           (type stream stream)
           (ignore ignore))
  (let ((*print-pretty* nil)
        (.printed-vars-so-far. nil)
        (*print-xmode* :fancy)
        (*standard-output* stream)
        (fcol-1 0))
    #||
    (when (symbolp cl)
      (format stream "~a" cl)
      (return-from print-clause nil))
    ||#
    (let ((flg nil))
      (declare (type symbol flg))
      (when (<= 0 (clause-id cl))
        (format stream "~d:" (clause-id cl)))
      (setq fcol-1 (file-column stream))
      ;; (when (< 0 (clause-heat-level cl))
      ;;    (format t "(heat=~D) " (clause-heat-level cl)))
      (princ "[" stream)
      (dolist (ips (clause-parents cl))
        (declare (type list ips))
        (dolist (ip ips)
          (declare (type (or symbol fixnum list) ip))
          (if (eq flg :colon)
              (princ ":" stream)
            (if (eq flg :comma)
                (princ "," stream)))
          (cond ((symbolp ip)
                 (setq flg :colon)
                 (case ip
                   (:binary-res-rule (princ "binary" stream))
                   (:pbinary-res-rule (princ "prop-res" stream))
                   (:hyper-res-rule (princ "hyper" stream))
                   (:neg-hyper-res-rule (princ "neg-hyper" stream))
                   (:ur-res-rule (princ "ur" stream))
                   (:para-into-rule (princ "para-into" stream))
                   (:para-from-rule (princ "para-from" stream))
                   (:factor-rule (princ "factor" stream))
                   ;; (:factor-simp-rule (princ "factor-simp" stream))
                   (:factor-simp-rule (princ "fsimp" stream))
                   (:distinct-constants (princ "dconst" stream))
                   (:new-demod-rule (princ "new-demod" stream))
                   (:back-demod-rule (princ "back-demod" stream))
                   (:demod-rule (princ "demod" stream))
                   (:unit-del-rule (princ "unit-del" stream))
                   (:eval-rule (princ "eval" stream))
                   (:copy-rule (princ "copy" stream))
                   (:flip-eq-rule (princ "flip" stream))
                   (:back-unit-del-rule (princ "back-unit-del" stream))
                   (otherwise (princ ip stream))))
                ((atom ip)
                 (setq flg :comma)
                 (princ ip stream))
                ;; list
                (t (setq flg :comma)
                   (format stream "~a.~a" (car ip) (cdr ip)))))))
    ;;
    (princ "] " stream)
    (let* ((.file-col. (file-column stream))
           (flg nil)
           (*print-indent* *print-indent*)
           (ind-check 0))
      (declare (type symbol flg))
      (setq *print-indent* fcol-1)
      (if (print-check fcol-1 cl-print-mergine stream)
          (setq ind-check fcol-1)
        (setq ind-check .file-col.))
      (setq *print-indent* ind-check)
      (dolist (lit (clause-literals cl))
        (setq .file-col. (file-column stream))
        (if flg
            (progn 
              (princ " | " stream)
              (setq .file-col. (+ 3 .file-col.))
              (if (print-check ind-check 20 stream)
                  (setq .file-col. (file-column stream))
                )
              )
          (setq flg t))
        (setq .printed-vars-so-far.
          (append .printed-vars-so-far.
                  (pr-literal lit stream))))
      )))

#||
(defun print-clause (cl &optional (stream *standard-output*) &rest ignore)
  (declare (type clause cl)
           (type stream stream)
           (ignore ignore))
  (let ((*print-pretty* nil)
        (.printed-vars-so-far. nil)
        (*standard-output* stream)
        (fcol-1 0))
    (declare (special *print-pretty*))
    (let ((flg nil))
      (declare (type symbol flg))
      (format stream "~d:" (clause-id cl))
      (setq fcol-1 (file-column stream))
      ;; (when (< 0 (clause-heat-level cl))
      ;;    (format t "(heat=~D) " (clause-heat-level cl)))
      (princ "[" stream)
      (dolist (ips (clause-parents cl))
        (declare (type list ips))
        (dolist (ip ips)
          (declare (type (or symbol fixnum list) ip))
          (if (eq flg :colon)
              (princ ":" stream)
            (if (eq flg :comma)
                (princ "," stream)))
          (cond ((symbolp ip)
                 (setq flg :colon)
                 (case ip
                   (:binary-res-rule (princ "binary" stream))
                   (:pbinary-res-rule (princ "prop-res" stream))
                   (:hyper-res-rule (princ "hyper" stream))
                   (:neg-hyper-res-rule (princ "neg-hyper" stream))
                   (:ur-res-rule (princ "ur" stream))
                   (:para-into-rule (princ "para-into" stream))
                   (:para-from-rule (princ "para-from" stream))
                   (:factor-rule (princ "factor" stream))
                   ;; (:factor-simp-rule (princ "factor-simp" stream))
                   (:factor-simp-rule (princ "fsimp" stream))
                   (:distinct-constants (princ "dconst" stream))
                   (:new-demod-rule (princ "new-demod" stream))
                   (:back-demod-rule (princ "back-demod" stream))
                   (:demod-rule (princ "demod" stream))
                   (:unit-del-rule (princ "unit-del" stream))
                   (:eval-rule (princ "eval" stream))
                   (:copy-rule (princ "copy" stream))
                   (:flip-eq-rule (princ "flip" stream))
                   (:back-unit-del-rule (princ "back-unit-del" stream))
                   (otherwise (princ ip stream))))
                ((atom ip)
                 (setq flg :comma)
                 (princ ip stream))
                ;; list
                (t (setq flg :comma)
                   (format stream "~a.~a" (car ip) (cdr ip)))
                ))
        ))
    ;;
    (princ "] " stream)
    ))
||#

(defun pr-clause-list (cl &optional (detail nil))
  (declare (ignore detail)
           (type list cl))
  (dolist (c cl)
    (print-next)
    (print-clause c)
    ))

(defun print-clause-attributes (clause)
  (declare (type clause clause))
  (princ (clause-attributes clause)))

;;; CLAUSE-TO-TERM : Clause -> Term
;;; translate clause to term
;;;
(defun literals-to-term (lit-list)
  (declare (type list lit-list)
           (values term))
  (if (null lit-list)
      *bool-false*
    (let ((res nil))
      (declare (type (or null term) res))
      (do* ((lits lit-list (cdr lits))
            (l (car lits) (car lits)))
          ((null lits))
        (declare (type literal l))
        (if (literal-sign l)
            (push (make-term-with-sort-check
                   *fopl-neg*
                   (list (literal-atom l)))
                  res)
          (push (literal-atom l) res)))
      (if (cdr res)
          (setq res (make-right-assoc-normal-form-with-sort-check
                     *fopl-or*
                     (nreverse res)))
        (setq res (car res)))
      ;;
      res)))

(defun clause-to-term (cl)
  (declare (type clause cl)
           (values term))
  (literals-to-term (clause-literals cl)))

;;; LITERAL COPIER
;;;
(defun copy-literal (lit &optional variables clause subst)
  (declare (type literal lit)
           (type list variables subst)
           (type (or null clause) clause)
           (values literal))
  (let ((atom (literal-atom lit)))
    (declare (type term atom))
    (when subst
      (setq atom (apply-subst subst atom)))
    (make-literal :clause (if clause
                              clause
                            (literal-clause lit))
                  :atom (if variables
                            (copy-term-using-variable atom
                                                      variables)
                          (copy-term-reusing-variables atom
                                                      (term-variables atom)))
                  :sign (literal-sign lit)
                  :type (literal-type lit)))
  )

(defun shallow-copy-literal (lit &optional clause)
  (declare (type literal lit)
           (type (or null clause) clause)
           (values literal))
  (make-literal :clause (if clause
                            clause
                          (literal-clause lit))
                :atom (literal-atom lit)
                :sign (literal-sign lit)
                :type (literal-type lit)))


;;; CLAUSE-VARIABLES : Clause -> List[Variable]
;;;
(defun clause-variables (clause)
  (declare (type clause clause)
           (values list))
  (let ((vars nil))
    (declare (type list vars))
    (dolist (lit (clause-literals clause))
      (declare (type literal lit))
      (setq vars (nunion vars (term-variables (literal-atom lit))
                         :test #'(lambda(x y) (term-eq x y)))))
    vars))

;;; CLAUSE-DISTINCT-VARIABLES (clause)
(declaim (inline clause-distinct-variables))

(defun clause-distinct-variables (clause)
  (declare (type clause clause)
           (values fixnum))
  (length (clause-variables clause)))

;;; GROUND-CLAUSE? : Clause -> Bool
;;;
(declaim (inline ground-clause?))
         
(defun ground-clause? (clause)
  (declare (type clause clause))
  (null (clause-variables clause)))

;;; NUM-LITERALS : Clause -> Nat
;;;
(declaim (inline num-literals))

(defun num-literals (clause)
  (declare (type clause clause)
           (values fixnum))
  (let ((num 0))
    (declare (type fixnum num))
    (dolist (lit (clause-literals clause))
      (declare (type literal lit))
      (unless (answer-literal? lit)
        (incf num)))
    num))

;;; NUM-ANSWERS : Clause -> Nat
;;;
(declaim (inline num-answers))

(defun num-answers (clause)
  (declare (type clause clause)
           (values fixnum))
  (let ((i 0))
    (declare (type fixnum i))
    (dolist (lit (clause-literals clause))
      (declare (type literal lit))
      (when (answer-literal? lit)
        (incf i)))
    i))

;;; NUM-LITERALS-ALL : Clause -> Nat
;;;
(declaim (inline num-literals-all))

(defun num-literals-all (clause)
  (declare (type clause clause)
           (values fixnum))
  (the fixnum (length (clause-literals clause))))

;;; UNIT-CLAUSE? : Clause -> Bool
;;; t if clause is a unit clause (exclude answer literals)
;;;
(declaim (inline unit-clause?))

(defun unit-clause? (clause)
  (declare (type clause clause))
  (= 1 (the fixnum (num-literals clause))))

;;; POSITIVE-CLAUSE? : Clause -> Bool
;;; returns t iff given clause is positive otherwise nil.
;;; (excludes answer literals.)
;;;
(declaim (inline positive-clause?))

(defun positive-clause? (clause)
  (declare (type clause clause))
  (every #'(lambda (lit)
             (declare (type literal lit))
             (or (positive-literal? lit)
                 (answer-literal? lit)))
         (clause-literals clause)))

;;; NEGATIVE-CLAUSE? : Clause -> Bool
;;;
(declaim (inline negative-clause?))

(defun negative-clause? (clause)
  (declare (type clause clause))
  (every #'(lambda (lit)
             (declare (type literal lit))
             (or (negative-literal? lit)
                 (answer-literal? lit)))
         (clause-literals clause)))

;;; PROPOSITIONAL-CLAUSE? : Clause -> Bool
;;; 
(declaim (inline propositional-clause?))

(defun propositional-clause? (clause)
  (declare (type clause clause))
  (every #'(lambda (lit)
             (declare (type literal lit))
             (let ((atom (literal-atom lit)))
               (and (not (term-is-variable? atom))
                    (term-is-constant? atom))))
         (clause-literals clause)))

;;; HORN-CLAUSE? : Clause -> Bool
;;; t if clause is a Horn Clause (at most one positive literal).
;;; (ignore answer literals).
;;;
(declaim (inline horn-clause?))

(defun horn-clause? (clause)
  (declare (type clause clause))
  (let ((i 0))
    (declare (type fixnum i))
    (dolist (lit (clause-literals clause))
      (declare (type literal lit))
      (when (and (positive-literal? lit)
                 (not (answer-literal? lit)))
        (incf i)))
    (<= i 1)))

;;; EQUALITY-CLAUSE? : Clause -> Bool
;;; t iff clause contains any equality literals (pos or neg).
;;;
(declaim (inline equality-clause?))

(defun equality-clause? (clause)
  (declare (type clause clause))
  (dolist (lit (clause-literals clause))
    (declare (type literal lit))
    (if (or (positive-eq-literal? lit)
            (negative-eq-literal? lit))
        (return-from equality-clause? t)))
  nil)

;;; SYMMETRY-CLAUSE? : Clause -> Bool
;;; t iff given clause is for symmetry of equality.
;;; i.e.,  X = Y  -> Y = X 
;;;       ~(X = Y) | Y = X
(defun symmetry-clause? (clause)
  (declare (type clause clause))
  (let ((lits (clause-literals clause)))
    (unless (= 2 (num-literals clause))
      (return-from symmetry-clause? nil))
    (let ((l1 (first lits))
          (l2 (second lits)))
      (declare (type literal l1 l2))
      (when (eq (literal-sign l1)
                (literal-sign l2))
        (return-from symmetry-clause? nil))
      (and (eq-literal? l1)
           (eq-literal? l2)
           (let ((t1 (literal-atom l1))
                 (t2 (literal-atom l2)))
             (and (term-is-variable? (term-arg-1 t1))
                  (variable-eq (term-arg-1 t1)
                               (term-arg-2 t2))
                  (term-is-variable? (term-arg-2 t1))
                  (variable-eq (term-arg-2 t1)
                               (term-arg-1 t2))))))))
               
;;; XX-RESOLVABLE : Clause -> Bool
;;; t if the non unit clause have a literal that can
;;; resolve with x = x.
;;;
(defun xx-resolvable (clause)
  (declare (type clause clause))
  (dolist (lit (clause-literals clause))
    (declare (type literal lit))
    (when (negative-eq-literal? lit)
      (let* ((atom (literal-atom lit))
             (a1 (term-arg-1 atom))
             (a2 (term-arg-2 atom)))
        (if (and (term-is-variable? a1)
                 (not (occurs-in a1 a2)))
            (return-from xx-resolvable t)
          (if (and (term-is-variable? a2)
                   (not (occurs-in a2 a1)))
              (return-from xx-resolvable t))))))
  nil)


;;; ============
;;; proof system
;;; ============
(defstruct psystem 
  (module nil)                          ; context module
  (sos nil)                             ; list of sos clause
  (usable nil)                          ; list of usable clause
  (passive nil)                         ; list of passive clause
  (axioms nil)                          ; list of axioms in clause form
  (demods nil)                          ; list of demod clauses
  (bi-demods nil)                       ; list of builtin demod clauses
  (clause-hash nil)                     ; hash table of clauses
  (demodulators nil)                    ; (make-hash-table :test #'eq))
                                        ; demodulator hash table
  (clause-counter 1)                    ; clause identifier counter
  )

(defun initialize-psystem (psys mod)
  (declare (type psystem psys)
           (type module mod)
           (values psystem))
  (setf (psystem-module psys) mod
        (psystem-sos psys) nil
        (psystem-usable psys) nil
        (psystem-axioms psys) nil
        (psystem-clause-counter psys) 1)
  (clrhash (psystem-clause-hash psys))
  (clrhash (psystem-demodulators psys))
  psys)

;;; CLASH
;;; used by hyper and UR resolution
;;; allocated one for each clashable literal of nucleus.
;;;
(defstruct (clash (:print-function print-clash))
  (literal nil :type (or null literal)) ; literal from nucleus
  (db nil :type (or null hash-table))   ; indexed table to use for
                                        ;  finding satellites
  (subst nil :type list)                ; unifying substitution
  (clashables nil :type list)
  (found-lit nil :type (or null literal)) ; unifying literal
  (evaluable nil)                       ; bi-demod
  (evaluation nil)                      ; ditto
  (already-evaluated nil)               ; ditto
  (prev nil :type (or null clash))      ; links
  (next nil :type (or null clash))
  )

(defun print-clash (obj &optional (stream *standard-output*)
                    &rest ignore)
  (declare (ignore ignore))
  (let* ((*standard-output* stream)
         (fcol (file-column stream))
         (*print-indent* (if (not (= 0 fcol))
                             fcol
                           (+ *print-indent* 4))))
    ;;
    (declare (type fixnum fcol *print-indent*))
    (do ((clash obj (clash-next clash))
         (num 0 (1+ num)))
        ((null clash))
      (declare (type fixnum num))
      (format t "#~d<clash: lit = " num)
      (prin1 (clash-literal clash))
      (print-next)
      (format t "clause-id = ~d" (clause-id (literal-clause
                                             (clash-literal clash))))
      (print-next)
      (princ "subst = ") (print-substitution (clash-subst clash))
      (print-next)
      (princ "found-lit = ") (prin1 (clash-found-lit clash))
      (print-next)
      (when (clash-found-lit clash)
        (format t "found clause-id = ~d"
                (clause-id (literal-clause (clash-found-lit clash)))))
      (when (clash-evaluable clash)
        (format t "evaluable: value = ~a" (clash-evaluation clash))
        (print-next)
        (format t "already evaled? = ~a" (clash-already-evaluated clash)))
      (princ ">")
      (print-next)
    )))

;;;
;;; PARAMODULATOR
;;;
(defstruct (paramod (:print-function pr-paramod))
  (lhs nil :type (or null term))
  (rhs nil :type (or null term))
  (literal nil :type (or null literal)))

(defun pr-paramod (obj &optional (stream *standard-output*) &rest ignore)
  (declare (ignore ignore))
  (let ((*standard-output* stream))
    (format t "#<paramod-rule: ")
    (term-print (paramod-lhs obj))
    (princ " = ")
    (term-print (paramod-rhs obj))
    (format t " (~D)>" (clause-id (literal-clause (paramod-literal obj))))
    ))

;;;
;;; DEMODULATOR
;;;
(defstruct (demod
            (:copier nil)
            (:constructor make-demod)
            (:print-function print-demodulator))
  (axiom nil :type (or null axiom))
  (order :normal :type symbol)
  (clause nil)
  )

(defmacro demod-lhs (_demod)
  `(axiom-lhs (demod-axiom ,_demod)))
(defmacro demod-rhs (_demod)
  `(axiom-rhs (demod-axiom ,_demod)))
(defmacro demod-condition (_demod)
  `(axiom-condition (demod-axiom ,_demod)))

(eval-when (:execute :load-toplevel)
  (setf (get 'demod :print) 'print-demod-internal)
  )

(defun print-demodulator (demod &optional (stream *standard-output*) &rest ignore)
  (declare (ignore ignore)
           (type demod demod)
           (type stream stream))
  (let* ((lhs (demod-lhs demod))
         (rhs (demod-rhs demod))
         (clause (demod-clause demod))
         (clause-id (if (not (clause-p clause))
                        :*
                      (clause-id clause)))
         (.printed-vars-so-far. nil))
    (let ((*standard-output* stream)
          (.file-col. .file-col.)
          (indent 0))
      (format t "(~a) " clause-id)
      (setq indent (file-column stream))
      (setq .printed-vars-so-far.
        (term-print lhs))
      (setq .file-col. (file-column stream))
      (print-check indent .file-col.)
      (princ " --> ")
      (setq .file-col. (+ 5 .file-col.))
      (term-print rhs stream)
      (format stream " [~a]" (demod-order demod))
      )))

;;; ----------
;;; OPTION SET
;;; -----------
(defstruct (option-set)
  (name "" :type simple-string)
  (flags nil :type (or null simple-array))
  (parameters nil :type (or null simple-array))
  )

;;; EOF