File: syntax.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 (351 lines) | stat: -rw-r--r-- 16,180 bytes parent folder | download | duplicates (3)
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
;;;-*-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:syntax.lisp
=============================================================================|#
#-:chaos-debug
(declaim (optimize (speed 3) (safety 0) #-GCL (debug 0)))
#+:chaos-debug
(declaim (optimize (speed 1) (safety 3) #-GCL (debug 3)))

;;;
;;; INSTALL-FOPL-SENTENCE
;;;
(defun install-fopl-sentence (mod-name
                              &key (sentence-sort "FoplSentence")
                                   (vardecl-sort "VarDeclList")
                                   (var-decl-list '("_" "," "_"))
                                   (forall '("\\A" "[" "_" "]" "_"))
                                   (exists  '("\\E" "[" "_" "]" "_"))
                                   (and '("_" "&" "_"))
                                   (or '("_" "|" "_"))
                                   (imply '("_" "->" "_"))
                                   (iff '("_" "<->" "_"))
                                   (not '("~" "_"))
                                   (eq nil)
                                   (beq nil))
  (let ((fopl-sentence-module (eval-modexp mod-name)))
    (cond ((and fopl-sentence-module (not (modexp-is-error fopl-sentence-module)))
           (setq *fopl-sentence-module* fopl-sentence-module)
           (setf (module-type *fopl-sentence-module*) :system)
           ;; setup sorts
           (let ((fopl-sentence-sort
                  (find-sort-in fopl-sentence-module sentence-sort))
                 (var-decl-list-sort
                  (find-sort-in fopl-sentence-module vardecl-sort)))
             (if fopl-sentence-sort
                 (setq *fopl-sentence-sort* fopl-sentence-sort)
               (with-output-panic-message ()
                 (princ "could not find sort FoplSentence")))
             (if var-decl-list-sort
                 (setq *var-decl-list-sort* var-decl-list-sort)
               (with-output-panic-message ()
                 (princ "could not find sort VarDeclList")))
             )
           ;; set up operators
           (let ((var-decl-list (find-method-in fopl-sentence-module
                                                var-decl-list
                                                (list *cosmos*
                                                      *cosmos*)
                                                *var-decl-list-sort*))
                 (fopl-forall (find-method-in fopl-sentence-module
                                              forall
                                              (list *cosmos*
                                                    *fopl-sentence-sort*)
                                              *fopl-sentence-sort*))
                 (fopl-exists (find-method-in fopl-sentence-module
                                              exists
                                              (list *cosmos*
                                                    *fopl-sentence-sort*)
                                              *fopl-sentence-sort*))
                 (fopl-and (find-method-in fopl-sentence-module
                                           and
                                           (list *fopl-sentence-sort*
                                                 *fopl-sentence-sort*)
                                           *fopl-sentence-sort*))
                 (fopl-or (find-method-in fopl-sentence-module
                                          or
                                          (list *fopl-sentence-sort*
                                                *fopl-sentence-sort*)
                                          *fopl-sentence-sort*))
                 (fopl-imply (find-method-in fopl-sentence-module
                                             imply
                                             (list *fopl-sentence-sort*
                                                   *fopl-sentence-sort*)
                                             *fopl-sentence-sort*))
                 (fopl-iff (find-method-in fopl-sentence-module
                                           iff
                                           (list *fopl-sentence-sort*
                                                 *fopl-sentence-sort*)
                                           *fopl-sentence-sort*))
                 (fopl-neg (find-method-in fopl-sentence-module
                                           not
                                           (list *fopl-sentence-sort*)
                                           *fopl-sentence-sort*))
                 (fopl-eq (if eq
                              (find-method-in fopl-sentence-module
                                              eq
                                              (list *cosmos*
                                                    *cosmos*)
                                              *fopl-sentence-sort*)
                            :none))
                 (fopl-beq (if beq
                               (find-method-in fopl-sentence-module
                                               beq
                                               (list *cosmos*
                                                     *cosmos*)
                                               *fopl-sentence-sort*)
                             :none))
                 )
             ;;
             (if (eq fopl-beq :none)
                 (setq *fopl-two-equalities* nil)
               (setq *fopl-two-equalities* t))
             ;;
             (if (and var-decl-list fopl-forall fopl-exists fopl-and fopl-or
                      fopl-imply fopl-iff fopl-neg fopl-eq fopl-beq)
                 (setq *var-decl-list* var-decl-list
                       *fopl-forall* fopl-forall
                       *fopl-exists* fopl-exists
                       *fopl-and* fopl-and
                       *fopl-or* fopl-or
                       *fopl-imply* fopl-imply
                       *fopl-iff* fopl-iff
                       *fopl-neg* fopl-neg
                       *fopl-eq* (if (not (eq fopl-eq :none))
                                     fopl-eq
                                   nil)
                       *fopl-beq* (if (not (eq fopl-beq :none))
                                      fopl-beq
                                    nil))
               (with-output-panic-message ()
                 (princ "could not install some operators in FoplSentence"))))
           )
          (t (with-output-panic-message ()
               (princ "could not find FoplSentece module")))
          )))

(defun install-fopl-clause ()
  (let ((fopl-clause-module (eval-modexp "FOPL-CLAUSE")))
    (let ((answer-method (find-method-in fopl-clause-module
                                         '("$Ans")
                                         (list *cosmos*)
                                         *bool-sort*)))
      (unless answer-method
        (with-output-panic-message ()
          (princ "could not install $Ans.")))
      (setq *fopl-ans* answer-method))))

;;; NOT USED
(defun install-fopl-clause-form ()
  (let ((fopl-clause-form-module (eval-modexp "FOPL-CLAUSE-FORM")))
    (cond ((and fopl-clause-form-module
                (not (modexp-is-error fopl-clause-form-module)))
           (setq *fopl-clause-form-module* fopl-clause-form-module)
           ;; setup sorts
           (let ((fopl-clause-sort (find-sort-in fopl-clause-form-module
                                                "FoplClause"))
                 (fopl-sentence-seq-sort (find-sort-in fopl-clause-form-module
                                                     "FoplSentenceSeq")))
             (if (and fopl-clause-sort fopl-sentence-seq-sort)
                 (setq *fopl-clause-sort* fopl-clause-sort
                       *fopl-sentence-seq-sort* fopl-sentence-seq-sort)
               (with-output-panic-message ()
                 (princ "could not install some sorts of FOPL-CLAUSE-FORM"))))
           ;; setup operators
           (let ((clause-constructor (find-method-in fopl-clause-form-module
                                                     '("[" "_" "]")
                                                     (list *fopl-sentence-seq-sort*)
                                                     *fopl-clause-sort*))
                 (clause-constructor2 (find-method-in
                                       fopl-clause-form-module
                                       '("!" "[" "_" "]")
                                       (list *fopl-sentence-seq-sort*)
                                       *fopl-clause-sort*))
                 (fopl-sentence-seq (find-method-in
                                    fopl-clause-form-module
                                    '("_" ";" "_")
                                    (list *fopl-sentence-seq-sort*
                                          *fopl-sentence-seq-sort*)
                                    *fopl-sentence-seq-sort*))
                 )
             (if (and clause-constructor clause-constructor2
                      fopl-sentence-seq)
                 (setq *clause-constructor* clause-constructor
                       *clause-constructor2* clause-constructor2
                       *fopl-sentence-seq* fopl-sentence-seq)
               (with-output-panic-message ()
                 (princ "could not install some operators in FOPL-CLAUSE-FORM")))
             ))
          (t (with-output-panic-message ()
               (princ "could not find FOPL-CLAUSE-FORM")))
          )))

;;; ***************
;;; PRIMITIVE UTILS
;;; ***************

(defun fopl-sentence-type (sentence)
  (declare (type term sentence)
           (values symbol))
  (cond ((term-is-application-form? sentence)
         (let ((head (term-head sentence)))
           (cond ((eq head *fopl-forall*) :forall)
                 ((eq head *fopl-exists*) :exists)
                 ((eq head *fopl-and*)    :and)
                 ((eq head *fopl-or*)     :or)
                 ((eq head *fopl-imply*)  :imply)
                 ((eq head *fopl-iff*)    :iff)
                 ((eq head *fopl-neg*)    :not)
                 ((eq head *fopl-eq*)     :eq)
                 ((eq head *fopl-beq*)    :beq)
                 (t :atom))))
        (t :atom)
        ))

(declaim (inline fopl-forall?
                 fopl-exists?
                 fopl-and?
                 fopl-or?
                 fopl-imply?
                 fopl-iff?
                 fopl-not?
                 fopl-eq?
                 fopl-beq?
                 ))
(defun fopl-forall? (f)
  (declare (type term f))
  (and (term-is-application-form? f)
       (eq (term-head f) *fopl-forall*)))
(defun fopl-exists? (f)
  (declare (type term f))
  (and (term-is-application-form? f)
       (eq (term-head f) *fopl-exists*)))
(defun fopl-and? (f)
  (declare (type term f))
  (and (term-is-application-form? f)
       (eq (term-head f) *fopl-and*)))
(defun fopl-or? (f)
  (declare (type term f))
  (and (term-is-application-form? f)
       (eq (term-head f) *fopl-or*)))
(defun fopl-imply? (f)
  (declare (type term f))
  (and (term-is-application-form? f)
       (eq (term-head f) *fopl-imply*)))
(defun fopl-iff? (f)
  (declare (type term f))
  (and (term-is-application-form? f)
       (eq (term-head f) *fopl-iff*)))
(defun fopl-not? (f)
  (declare (type term f))
  (and (term-is-application-form? f)
       (eq (term-head f) *fopl-neg*)))
(defun fopl-eq? (f)
  (declare (type term f))
  (and (term-is-application-form? f)
       (eq (term-head f) *fopl-eq*)))
(defun fopl-beq? (f)
  (declare (type term f))
  (and (term-is-application-form? f)
       (eq (term-head f) *fopl-beq*)))
  
;;; IS-VALID-FORMULA? : Tterm -> Bool
;;;
(defun is-valid-formula? (term mod)
  (declare (type term term)
           (type (or null module) mod))
  (unless mod
    (with-output-chaos-error ('internal)
      (princ "is-valid-formua?: called with no context module given, this should not happen.")))
  (is-in-same-connected-component (term-sort term)
                                  *fopl-sentence-sort*
                                  (module-sort-order mod)))

;;;
;;; CHECK-FOPL-SYNTAX
;;;
(defun check-fopl-syntax (fopl-sentence &optional report-error)
  (declare (type term fopl-sentence))
  (if (and (term? fopl-sentence)
           (is-valid-formula? fopl-sentence *current-module*))
      (check-fopl-syntax-aux fopl-sentence report-error)
    (if report-error
        (with-output-chaos-error ('invalid-sentence)
          (princ "encoutered with illegal sentence: ")
          (print-next)
          (print-chaos-object fopl-sentence))
      nil)))

(defun check-fopl-syntax-aux (fopl-sentence report-error)
  (labels ((check-var-decl (var-decl)
             (cond ((term-is-variable? var-decl)
                    t)
                   ((and (term-is-application-form? var-decl)
                         (term-subterms var-decl))
                    (let ((top (term-head var-decl)))
                      (or (and (method-is-of-same-operator top
                                                           *var-decl-list*)
                               (every #'(lambda (x) (check-var-decl x))
                                      (term-subterms var-decl)))
                          (if report-error
                              (with-output-chaos-error ('invalid-formula)
                                (princ "invaid formula: ")
                                (print-chaos-object fopl-sentence))
                            nil))))
                   (t (if report-error
                          (with-output-chaos-error ('invalid-formula)
                            (princ "encounterd with illegal formula")
                            (print-chaos-object fopl-sentence))
                        nil)))))
    ;;
    (if (term-is-application-form? fopl-sentence)
        (let ((type (fopl-sentence-type fopl-sentence)))
          (case type
            (:atom t)
            ((:forall :exists)
             (and (check-var-decl (term-arg-1 fopl-sentence))
                  (check-fopl-syntax-aux (term-arg-2 fopl-sentence)
                                         report-error)))
            (:not
             (check-fopl-syntax-aux (term-arg-1 fopl-sentence)
                                    report-error))
            (otherwise
             (and (check-fopl-syntax-aux (term-arg-1 fopl-sentence)
                                         report-error)
                  (check-fopl-syntax-aux (term-arg-2 fopl-sentence)
                                         report-error)))))
      t)
    ))

;;; EOF