-
Notifications
You must be signed in to change notification settings - Fork 1
/
print.cl
377 lines (332 loc) · 14.2 KB
/
print.cl
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
;;; SNePS 3: Print & Description Methods
;;; ====================================
;;; Stuart C. Shapiro
;;; Department of Computer Science and Engineering
;;; State University of New York at Buffalo
;;; The contents of this file are subject to the University at Buffalo
;;; Public License Version 1.0 (the "License"); you may not use this file
;;; except in compliance with the License. You may obtain a copy of the
;;; License at http://www.cse.buffalo.edu/sneps/Downloads/ubpl.pdf.
;;;
;;; Software distributed under the License is distributed on an "AS IS"
;;; basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
;;; the License for the specific language governing rights and limitations
;;; under the License.
;;;
;;; The Original Code is SNePS 3.
;;;
;;; The Initial Developer of the Original Code is Research Foundation of
;;; State University of New York, on behalf of University at Buffalo.
;;;
;;; Portions created by the Initial Developer are Copyright (C) 2007
;;; Research Foundation of State University of New York, on behalf of
;;; University at Buffalo. All Rights Reserved.
;;;
;;; Contributor(s): ______________________________________.
(in-package :sneps3)
;;; Print-object Methods
;;; ====================
(defmethod print-object ((term atom) stream)
(format stream "~@<~S~:[~;!~]~:>"
(name term)
(and (typep term 'Proposition)
(ct:assertedp term (ct:currentContext)))))
(defmethod print-object ((term molecular) stream)
(top-level-named-molecular-term term stream))
(defmethod print-object ((term indefinite) stream)
(top-level-named-variable-term term 'some stream))
(defmethod print-object ((term arbitrary) stream)
(top-level-named-variable-term term 'every stream))
(defmethod print-object ((ct ct:context) stream)
(format stream "~<:~S~:>" (ct:ct-name ct)))
;;; Utilities for print-object methods
;;; ==================================
(defun top-level-named-variable-term (term quant stream)
"Prints to the stream the variable term
preceded only at the top level by its wft name
and an indication of its assert status."
;; Does this by first replacing its own code by the unnamed version,
;;; and then restoring it.
(setf (symbol-function 'top-level-named-variable-term)
(symbol-function 'print-unnamed-variable-term))
;; This needs to be done with the other top-level-functions
(setf (symbol-function 'top-level-named-molecular-term)
(symbol-function 'print-unnamed-molecular-term))
;; Also initializes and desconstructs a structures used for
;; printing variable nodes fully the first time, and the label
;; otherwise
(setf *PRINTED-VARIABLES* (set:new-set))
(unwind-protect
(print-named-variable-term term quant stream)
(setf (symbol-function 'top-level-named-variable-term)
(symbol-function 'top-level-named-variable-term-code))
(setf (symbol-function 'top-level-named-molecular-term)
(symbol-function 'top-level-named-molecular-term-code))
(setf *PRINTED-VARIABLES* nil)))
(defun top-level-named-variable-term-code (term quant stream)
"Function repository for top-level-named-variable-term."
(setf (symbol-function 'top-level-named-variable-term)
(symbol-function 'print-unnamed-variable-term))
(setf (symbol-function 'top-level-named-molecular-term)
(symbol-function 'print-unnamed-molecular-term))
(setf *PRINTED-VARIABLES* (set:new-set))
(unwind-protect
(print-named-variable-term term quant stream)
(setf (symbol-function 'top-level-named-variable-term)
(symbol-function 'top-level-named-variable-term-code))
(setf (symbol-function 'top-level-named-molecular-term)
(symbol-function 'top-level-named-molecular-term-code))
(setf *PRINTED-VARIABLES* nil)))
(defun top-level-named-molecular-term (term stream)
"Prints to the stream the molecular term
preceded only at the top level by its wft name
and an indication of its assert status."
;; Does this by first replacing its own code by the unnamed version,
;;; and then restoring it.
(setf (symbol-function 'top-level-named-molecular-term)
(symbol-function 'print-unnamed-molecular-term))
;; This needs to be done with the other top-level-functions
(setf (symbol-function 'top-level-named-variable-term)
(symbol-function 'print-unnamed-variable-term))
;; Also initializes and desconstructs a structure used for
;; printing variable nodes fully the first time, and the label
;; otherwise
(setf *PRINTED-VARIABLES* (set:new-set))
(unwind-protect
(print-named-molecular-term term stream)
(setf (symbol-function 'top-level-named-molecular-term)
(symbol-function 'top-level-named-molecular-term-code))
(setf (symbol-function 'top-level-named-variable-term)
(symbol-function 'top-level-named-variable-term-code))
(setf *PRINTED-VARIABLES* nil)))
(defun top-level-named-molecular-term-code (term stream)
"Function repository for top-level-named-molecular-term."
(setf (symbol-function 'top-level-named-molecular-term)
(symbol-function 'print-unnamed-molecular-term))
(setf (symbol-function 'top-level-named-variable-term)
(symbol-function 'print-unnamed-variable-term))
(setf *PRINTED-VARIABLES* (set:new-set))
(unwind-protect
(print-named-molecular-term term stream)
(setf (symbol-function 'top-level-named-molecular-term)
(symbol-function 'top-level-named-molecular-term-code))
(setf (symbol-function 'top-level-named-variable-term)
(symbol-function 'top-level-named-variable-term-code))
(setf *PRINTED-VARIABLES* nil)))
(defun print-unnamed-molecular-term (term stream)
"Prints to the stream
the molecular term not preceded by its wft name
nor an indication of its assert status."
(typecase term
(negation
(print-negation (elt (down-cableset term) 0) stream))
(negationbyfailure
(print-negationbyfailure (elt (down-cableset term) 0) stream))
(conjunction
(print-nary 'and (elt (down-cableset term) 0) stream))
(disjunction
(print-nary 'or (elt (down-cableset term) 0) stream))
(equivalence
(print-nary 'iff (elt (down-cableset term) 0) stream))
(xor
(print-nary 'xor (elt (down-cableset term) 0) stream))
(nand
(print-nary 'nand (elt (down-cableset term) 0) stream))
(andor
(print-param2op 'andor (minparam term) (maxparam term)
(elt (down-cableset term) 0) stream))
(thresh
(print-param2op 'thresh (minparam term) (maxparam term)
(elt (down-cableset term) 0) stream))
(implication
(format stream "~@<(if ~W~_ ~W)~:>"
(elt (down-cableset term) 0)
(elt (down-cableset term) 1)))
(numericalentailment
(format stream "~@<(~A=> ~_ ~S~_ ~S)~:>"
(if (= (minparam term) 1) :v (minparam term))
(elt (down-cableset term) 0)
(elt (down-cableset term) 1)))
(t
(let ((cf (caseframe term)))
(cond ((cf:hasOneArgumentSlot cf)
(let ((fsymbol (first (cf:caseframe-print-pattern cf))))
(if (and (consp fsymbol)
(eq (first fsymbol) 'quote))
(print-nary (second fsymbol)
(elt (down-cableset term) 0)
stream)
(print-nary (elt (down-cableset term) 0)
(elt (down-cableset term) 1)
stream))))
(t (print-molecular (cf:caseframe-print-pattern cf)
(cf::caseframe-slots cf)
(down-cableset term)
stream)))))))
(defun print-unnamed-term (term stream)
"Prints the given term, without wft-names on any level
to the given stream."
(setf (symbol-function 'top-level-named-molecular-term)
(symbol-function 'print-unnamed-molecular-term)
(symbol-function 'top-level-named-variable-term)
(symbol-function 'print-unnamed-variable-term))
(setf *PRINTED-VARIABLES* (set:new-set))
(unwind-protect
(print-unnamed-molecular-term term stream)
(setf (symbol-function 'top-level-named-molecular-term)
(symbol-function 'top-level-named-molecular-term-code)
(symbol-function 'top-level-named-variable-term)
(symbol-function 'top-level-named-variable-term-code)
*PRINTED-VARIABLES* nil)))
;;; Methods for printing various types of molecular terms
;;; =====================================================
(defun print-named-variable-term (term quant stream)
"Prints the variable term with unique identifier in front."
(format stream "~A: " (name term))
(print-unnamed-variable-term term quant stream))
(defun print-unnamed-variable-term (term quant stream)
"Prints the variable term without unique identifier in front."
(cond
((and *PRINTED-VARIABLES* (set:member term *PRINTED-VARIABLES*))
(format stream "~A" (var-label term)))
((and *PRINTED-VARIABLES* (eq quant 'some))
(set:add-item term *PRINTED-VARIABLES*)
(format stream "(some ~A(~{~A~^ ~}) ~{~A~^ ~})"
(var-label term)
(set:set-to-list (dependencies term))
(set:set-to-list (restriction-set term))))
((and *PRINTED-VARIABLES* (eq quant 'every))
(set:add-item term *PRINTED-VARIABLES*)
(format stream "(every ~A ~{~A~^ ~})"
(var-label term)
(set:set-to-list (restriction-set term))))))
(defun print-molecular (pattern slots cs stream)
"Prints to the stream
the molecular term containing
the given print-pattern,caseframe slots, and down-cableset.
Prints it not preceded by its wft name
nor an indication of its assert status."
;; Default method for printing a molecular term.
(format stream "~@<(~{~S~^~_ ~})~:>"
(loop for p in pattern
if (and (consp p) (eq (first p) 'quote))
collect (second p)
else if (symbolp p)
collect (elt cs
(position p slots
:key #'slot:slot-name))
else do (error
"Bad pattern part ~S in the pattern ~S."
p pattern))))
(defun print-negation (args stream)
"Prints to the stream
the negation (not or nor) whose arguments are args.
Prints it not preceded by its wft name
nor an indication of its assert status."
;; This semi-pretty prints because the form is translated into a list.
;; It would be better
;; if the Lisp pretty printing features were used directly.
(format stream "~@<~W~:>"
(cons (if (set:singletonp args) 'not 'nor)
(set:set-to-list args))))
(defun print-negationbyfailure (args stream)
"Prints to the stream
the negationbyfailure (thnot or thnor) whose arguments are args.
Prints it not preceded by its wft name
nor an indication of its assert status."
;; This semi-pretty prints because the form is translated into a list.
;; It would be better
;; if the Lisp pretty printing features were used directly.
(format stream "~@<~W~:>"
(cons (if (set:singletonp args) 'thnot 'thnor)
(set:set-to-list args))))
(defun print-nary (fn args stream)
"Prints to the stream
the term whose function symbol is fn, and whose arguments are args,
without using setof for the args.
Prints it not preceded by its wft name
nor an indication of its assert status."
;; This semi-pretty prints because the form is translated into a list.
;; It would be better
;; if the Lisp pretty printing features were used directly.
(format stream "~@<~W~:>" (cons fn (set:set-to-list args))))
(defun print-param2op (fn min max args stream)
"Prints to the stream
the param2op term whose function symbol is fn,
whose parameters are (min max),
and whose arguments are args,
without using setof for the args.
Prints it not preceded by its wft name
nor an indication of its assert status."
;; This semi-pretty prints because the form is translated into a list.
;; It would be better
;; if the Lisp pretty printing features were used directly.
(format stream "~@<~W~:>" `(,fn (,min ,max) ,@(set:set-to-list args))))
(defun print-named-molecular-term (term stream)
"Prints to the stream
the molecular term preceded by its wft name
and an indication of its assert status."
(format stream "~@<~W~:[~*~;~:[?~;!~]~]: ~W~:>"
(name term)
(typep term 'Proposition)
(ct:assertedp term (ct:currentContext)) term))
;;; Description Methods
;;; ===================
(defgeneric description (term)
(:documentation
"Returns a description string for term."))
(defmethod description ((term atom))
(format nil "~<~S~:>" term))
(defmethod description ((term molecular))
(funcall (cf:caseframe-descfun (caseframe term)) term))
(defmethod description ((termset set:set))
(let ((desc ""))
(set:loopset for trm in termset
do (setf desc
(concatenate 'string
desc ", and " (description trm))))
(subseq desc 6)))
;;; Functions for Writing to Files
;;; ==============================
(defun writeKBToTextFile (file &optional headerfile)
"Writes the KB to the given text file,
so that when that file is loaded,
all the propositions asserted in the current KB
will be asserted in the new KB.
If the headerfile is included,
a load of that file will be written before any of the asserts."
;; Assumes that all required Types, Contexts, Slots, and Caseframes
;; will be defined before the first assert in the file is performed.
(with-open-file (opstream file
:direction :output
:if-exists :supersede
:if-does-not-exist :create)
(format opstream ";;; SNePS 3 KB~%~
;;; ==========~%")
(multiple-value-bind (sec min hr date mon year) (get-decoded-time)
(format opstream ";;; ~d/~d/~d ~d:~d:~d~%"
mon date year hr min sec))
(when headerfile (format opstream "~&(load ~S)~%" headerfile))
(format opstream ";;; Assumes that all required Contexts, Types, Slots, ~
and Caseframes have now been loaded.~%~
(in-package :snuser)~%")
(let ((*package* (find-package :snuser)))
(maphash
#'(lambda (cname context)
(set:loopset for hyp in (util:resource-value (ct::ct-hyps context))
do
(format opstream "~&(ct:assert '")
(if (typep hyp 'atom)
(print-object hyp opstream)
(print-unnamed-term hyp opstream))
(format opstream " '~S :origintag :hyp)" cname))
(set:loopset for der in (util:resource-value (ct::ct-ders context))
do
(format opstream "~&(ct:assert '")
(if (typep der 'atom)
(print-object der opstream)
(print-unnamed-term der opstream))
(format opstream " '~S :origintag :der)" cname))
)
ct:*CONTEXTS*))))