-
Notifications
You must be signed in to change notification settings - Fork 1
/
schema-util.lisp
2451 lines (2122 loc) · 66.4 KB
/
schema-util.lisp
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
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(load "ll-load.lisp")
(ll-load "old-ttt/src/load")
(ll-load "schema-el.lisp")
(ll-load "ll-cache.lisp")
(ll-load "ll-util.lisp")
(ll-load "schema-kb.lisp")
(ll-load "schema-subsumption.lisp")
(ll-load "schema-time.lisp")
(defparameter *BLANK-SCHEMA*
'(epi-schema ((?x blank.v) ** ?E) (:Roles))
)
(setf *DEFAULT-NECESSITY* 0.9)
(setf *DEFAULT-CERTAINTY* '(/ 1 1))
(defparameter *SCHEMA-MATCH-NUM* 0)
(defparameter *SEC-NAMES* '(
:Roles
:Goals
:Preconds
:Steps
:Postconds
:Paraphrases
:Episode-relations
:Certainties
:Necessities
:Subordinate-constraints
))
(defparameter *META-SECS* '(
:Subordinate-constraints
:Necessities
:Certainties
))
(defparameter *UNINTERESTING-PREDS* '(
HAS-DET.PR
ORIENTS
))
(defparameter *SCHEMAS-BY-PRED* (make-hash-table :test #'equal))
; (format t "resetting preds-by-schema~%")
(defparameter *PREDS-BY-SCHEMA* (make-hash-table :test #'equal))
; known-schema-pred takes a schema, ignores
; its predicate, looks it up in a database of
; known schemas, and returns the predicate it's
; known by in that database, or nil otherwise.
(ldefun known-schema-pred (schema)
(block outer
; We only disregard the match number of a given
; predicate; if a schema is identical but has
; a different predicate name, it's not identical.
(setf blanked-schema (replace-vals (schema-pred schema) (get-schema-match-name (schema-pred schema)) schema))
; (format t "looking schema up in known db:~%")
; (print-schema blanked-schema)
; (format t "~s~%" blanked-schema)
(if (not (null (gethash blanked-schema *PREDS-BY-SCHEMA*)))
; then
(progn
; (format t "schema ~s known by name ~s~%" (schema-pred schema) (gethash blanked-schema *PREDS-BY-SCHEMA*))
(return-from outer (gethash blanked-schema *PREDS-BY-SCHEMA*))
)
)
(return-from outer nil)
)
)
(ldefun lambdify-preds! (ps)
(lambdify-preds-maybe-colon ps nil)
)
(ldefun lambdify-preds-with-sym! (ps sym)
(lambdify-preds-maybe-colon-with-sym ps nil sym)
)
(ldefun lambdify-preds-colon! (ps)
(lambdify-preds-maybe-colon ps t)
)
(ldefun schemas-about-equal? (schema1 schema2)
(block outer
(loop for sec in *SEC-NAMES* do (block compare-sec
(if (contains *META-SECS* sec)
(return-from compare-sec))
(setf sec1 (get-section schema1 sec))
(setf sec2 (get-section schema2 sec))
(setf forms1 (section-formulas sec1))
(setf forms2 (section-formulas sec2))
(if (and (null forms1) (null forms2))
(return-from compare-sec))
(if (or (null forms1) (null forms2))
(return-from outer nil))
(if (not (equal 'FLUENT (section-type sec1)))
(progn
(setf forms1 (mapcar #'second forms1))
(setf forms2 (mapcar #'second forms2))))
(if (not (same-list-unordered forms1 forms2))
(return-from outer nil))
))
(return-from outer t)
)
)
(ldefun lambdify-preds-maybe-colon-with-sym (ps colon sym)
(let ((tmp-sym sym))
(list
(if colon ':L 'L)
tmp-sym
(if colon
; then
(append (list ':O 'AND) (mapcar (lambda (x) (list ':I tmp-sym x)) ps))
; else
(append (list 'AND) (mapcar (lambda (x) (list tmp-sym x)) ps))
)
)
)
)
(ldefun lambdify-preds-maybe-colon (ps colon)
(lambdify-preds-maybe-colon-with-sym ps colon (gensym))
)
(ldefun clear-registered-schemas ()
(block outer
(setf *SCHEMAS-BY-PRED* (make-hash-table :test #'equal))
(setf *PREDS-BY-SCHEMA (make-hash-table :test #'equal))
)
)
(ldefun register-schema (schema)
(block outer
; (format t "~d schemas registered~%" (ht-count *PREDS-BY-SCHEMA*))
(setf (gethash (schema-pred schema) *SCHEMAS-BY-PRED*) schema)
(set (schema-pred schema) schema)
(setf blanked-schema (replace-vals (schema-pred schema) (get-schema-match-name (schema-pred schema)) schema))
(if (null (gethash blanked-schema *PREDS-BY-SCHEMA*))
; then
(progn
; (format t "adding schema (~s) to known list:~%" blanked-schema)
; (format t "with pred ~s~%" (schema-pred schema))
; (print-schema blanked-schema)
; (format t "~s~%" blanked-schema)
(setf (gethash blanked-schema *PREDS-BY-SCHEMA*) (schema-pred schema))
; (format t "preds-by-schema size ~s~%" (ht-count *PREDS-BY-SCHEMA*))
; (format t "successfully added? ~s (val ~s)~%" (not (null (gethash blanked-schema *PREDS-BY-SCHEMA*))) (gethash blanked-schema *PREDS-BY-SCHEMA*))
)
)
)
)
; create-from-match takes a schema match and:
; 1. gives it a new name, or re-uses a name
; if the schema is known
; 2. generalizes constants to variables
; 3. registers the generalized schema under
; the new name
; 4. returns the generalized schema name
(ldefun create-from-match (match &optional return-bindings)
(create-from-match-maybe-gen match t return-bindings)
)
(ldefun create-from-match-maybe-gen (match should-gen &optional return-bindings)
(block outer
(setf is-new-schema nil)
; (maybe) generalize the schema
(setf new-schema match)
(setf new-bindings nil)
(if should-gen
; then
; (setf new-schema (clean-do-kas (rename-constraints (sort-steps (generalize-schema-constants new-schema)))))
(progn
(setf new-schema-pair (fully-clean-schema match t))
(setf new-schema (car new-schema-pair))
(setf new-bindings (second new-schema-pair))
)
)
; check whether we know the generalized schema
; by some name
(setf new-schema-name (known-schema-pred new-schema))
(if (null new-schema-name)
; then
(progn
(setf new-schema-name (new-schema-match-name (schema-pred match)))
(setf is-new-schema t)
)
)
; Replace the name and generalize the schema
(setf new-schema (replace-vals (schema-pred new-schema) new-schema-name new-schema))
(if is-new-schema
; then
(register-schema new-schema)
)
(if return-bindings
; then
(return-from outer (list new-schema-name new-bindings))
; else
(return-from outer new-schema-name)
)
)
)
; sec-formula-prefix tells you the prefix for condition
; formulas for a given section, e.g. !W for Episode-relations
; to yield !W1, ?E for Steps to yield ?E1, etc.
(ldefun sec-formula-prefix (sec-name)
(block outer
(if (null (member sec-name *SEC-NAMES* :test #'equal))
(return-from outer nil))
(cond
((equal sec-name ':Roles)
(return-from outer "!R"))
((equal sec-name ':Goals)
(return-from outer "?G"))
((equal sec-name ':Preconds)
(return-from outer "?I"))
((equal sec-name ':Postconds)
(return-from outer "?P"))
((equal sec-name ':Paraphrases)
(return-from outer "?H"))
((equal sec-name ':Steps)
(return-from outer "?E"))
((equal sec-name ':Episode-relations)
(return-from outer "!W"))
((equal sec-name ':Certainties)
(return-from outer "!C"))
((equal sec-name ':Necessities)
(return-from outer "!N"))
((equal sec-name ':Subordinate-constraints)
(return-from outer "!S"))
(t
(return-from outer nil))
)
)
)
(ldefun sec-name-from-prefix (prefix)
(block outer
(loop for sec-name in *SEC-NAMES*
if (equal (sec-formula-prefix sec-name) prefix)
do (return-from outer sec-name)
)
(return-from outer nil)
)
)
(ldefun sec-name-from-id (id)
(sec-name-from-prefix (subseq (string id) 0 2))
)
; schema-cond? reports whether phi is a well-formed
; pair comprising a schema condition "tag" and an
; EL formula. If fluent is non-nil, the "tag" will
; be an individual (generally an episode) which is
; characterized by the EL formula. If fluent is nil,
; the tag will be an EL metavariable standing for the
; EL formula.
;
; Fluent schema conditions represent temporally-bounded
; constraints on the individuals in the schema. Nonfluent
; conditions are atemporal, i.e. eternal.
(ldefun schema-cond? (phi fluent)
(ll-cache #'u-schema-cond? (list phi fluent)
100 nil)
)
(ldefun u-schema-cond? (phi fluent)
(and
(equal 2 (length phi))
(symbolp (car phi))
(if fluent
; then
;(or
;(has-prefix? (format nil "~s" (car phi)) "?")
;(lex-skolem? phi) ; skolemized episodes can sub in here
;)
(canon-individual? (car phi))
; else
(has-prefix? (string (car phi)) "!"))
(if (canon-prop? (second phi))
; then
t
; else
(progn
(format t "~s is an invalid formula~%" (second phi))
nil
)
)
)
)
(ldefun exc-to-var (s)
(if (exc-varp s)
; then
(intern (concat-strs "?" (remove-prefix (string s) "!")))
; else
(if (varp s)
; then
s
; else
nil
)
)
)
(ldefun var-to-exc (s)
(if (varp s)
; then
(intern (concat-strs "!" (remove-prefix (string s) "?")))
; else
(if (exc-varp s)
; then
s
; else
nil
)
)
)
(ldefun fluent-cond? (phi)
(schema-cond? phi t)
)
(ldefun nonfluent-cond? (phi)
(schema-cond? phi nil)
)
; schema-section? reports whether sec is a valid schema
; section, properly named and populated with valid schema
; conditions (fluent or nonfluent).
(ldefun schema-section? (sec)
(ll-cache #'u-schema-section? (list sec) 100 nil)
)
(ldefun u-schema-section? (sec)
(and
(> (length sec) 0)
(not (null (member (car sec) *SEC-NAMES*)))
(or
; These sections don't need to have valid formulas.
(equal (car sec) ':Necessities)
(equal (car sec) ':Certainties)
(loop for phi in (cdr sec)
if (not (or (nonfluent-cond? phi) (fluent-cond? phi)))
do (format t "phi ~s is invalid~%" phi)
always (or (nonfluent-cond? phi) (fluent-cond? phi)))
)
))
(ldefun section-name (sec)
(progn
(check #'schema-section? sec)
(car sec)
)
)
; section-formulas returns all conditions from a schema section.
(ldefun section-formulas (sec)
(block outer
(if (null sec)
(return-from outer nil))
(check #'schema-section? sec)
(cdr sec)
)
)
; section-type returns the symbol NONFLUENT if sec contains all
; nonfluent conditions, or FLUENT if it contains all fluent
; conditions.
(ldefun section-type (sec)
(block outer
(check #'schema-section? sec)
(setf sec-prefix (sec-formula-prefix (section-name sec)))
(if (contains *META-SECS* (section-name sec))
(return-from outer 'META))
(if (has-prefix? sec-prefix "?")
(return-from outer 'FLUENT))
(if (has-prefix? sec-prefix "!")
(return-from outer 'NONFLUENT))
(return-from outer nil)
)
)
; schema? reports whether s is a valid schema.
(ldefun schema? (s)
(and
(> (length s) 2)
(equal (car s) 'epi-schema)
(canon-charstar? (second s))
(loop for sec in (cddr s)
always (schema-section? sec)
)
)
)
(ldefun schema-name (schema)
(handler-case
(prop-pred (car (second schema)))
(error ()
nil))
)
(ldefun schema-sections (schema)
(progn
(check #'schema? schema)
(cddr schema)
)
)
(ldefun sort-sections (secs)
(sort (copy-list secs) (lambda (s1 s2)
(> (length (member (car s1) *SEC-NAMES* :test #'equal)) (length (member (car s2) *SEC-NAMES* :test #'equal)))
))
)
(ldefun nonmeta-sections (schema)
(loop for sec in (schema-sections schema)
if (and (or (equal (section-type sec) 'NONFLUENT)
(equal (section-type sec) 'FLUENT))
(not (contains *META-SECS* (car sec))))
; then
collect sec
)
)
(ldefun nonfluent-sections (schema)
(loop for sec in (schema-sections schema)
if (equal (section-type sec) 'NONFLUENT)
collect sec
)
)
(ldefun get-schema-ep-var-chars (schema v)
(block outer
(setf char-forms (list))
; Check if it characterizes the header formula, first.
(if (equal (third (schema-header schema)) v)
; then
(setf char-forms
(append char-forms
(list (car (schema-header schema)))))
)
(loop for sec in (fluent-sections schema)
do (loop for form in (section-formulas sec)
if (equal (car form) v)
do (setf char-forms
(append char-forms
(list (second form))))
)
)
(return-from outer char-forms)
)
)
(ldefun schema-ep-var? (schema v)
(not (null (get-schema-ep-var-chars schema v)))
)
(ldefun fluent-sections (schema)
(loop for sec in (schema-sections schema)
if (equal (section-type sec) 'FLUENT)
collect sec
)
)
(ldefun get-section (schema sec-name)
(block outer
(loop for sec in (schema-sections schema)
if (equal (section-name sec) sec-name)
do (return-from outer sec)
)
(return-from outer nil)
)
)
(ldefun renumber-schema-name (name num)
(block outer
(setf spl (split-str (string name) "."))
(if (equal 3 (length spl))
(setf spl (list (car spl) (third spl))))
(return-from outer (intern
(format nil "~a.~d.~a"
(car spl) num (second spl))))
)
)
(ldefun set-header (schema new-header)
(append (list
'epi-schema
(list new-header '** (third (second schema))))
(cddr schema)
)
)
; set-section returns a new schema, identical to the input schema,
; except with the section "sec-name" having the new value "new-sec".
(ldefun set-section (schema sec-name new-sec)
(let (new-schema found)
(block outer
(setf new-schema (list 'epi-schema (second schema)))
(loop for sec in (schema-sections schema) do (block inner
(if (equal (section-name sec) sec-name)
; then
(progn
(setf found t)
(setf new-schema (append new-schema (list new-sec)))
)
; else
(setf new-schema (append new-schema (list sec)))
)
))
; Add the section if it doesn't exist.
(if (not found)
; then
(setf new-schema (append new-schema (list new-sec)))
)
(return-from outer new-schema)
)
)
)
(ldefun add-constraint (schema sec-name constraint)
(add-constraint-with-const schema sec-name constraint nil)
)
(ldefun add-constraint-with-const (schema sec-name constraint new-const-id)
(block outer
(if (null (member sec-name *SEC-NAMES* :test #'equal))
; then
(progn
(error "Cannot add constraint: invalid section name ~s~%" sec-name)
(return-from outer nil)
)
)
(setf const-num (- (length (get-section schema sec-name)) 1))
(if (equal const-num -1)
(setf const-num 0)
)
(if (null new-const-id)
(setf new-const-id (intern (format nil "~a~d" (sec-formula-prefix sec-name) (+ const-num 1))))
)
(setf new-const (list new-const-id constraint))
(setf new-sec (append (get-section schema sec-name) (list new-const)))
(if (equal 1 (length new-sec))
; we're creating it, so add the name
(setf new-sec (append (list sec-name) new-sec))
)
(return-from outer (set-section schema sec-name new-sec))
)
)
; add-role-constraint adds a nonfluent condition to the schema with a unique
; metavariable.
(ldefun add-role-constraint (schema constraint)
;(let (new-roles role-num new-role)
;(block outer
; (setf role-num (- (length (get-section schema ':Roles)) 1))
; (setf new-role (list (intern (format nil "!R~d" (+ role-num 1))) constraint))
; (setf new-roles (append (get-section schema ':Roles) (list new-role)))
; (return-from outer (set-section schema ':Roles new-roles))
;)
;)
(block outer
(if (and (equal 2 (length constraint)) (equal (second constraint) 'PLUR))
; then
(progn
(format t "tried to add invalid RC~%")
(setf x 0)
(format t "~d~%" (/ 1 x))
)
)
(add-constraint schema ':Roles constraint)
)
)
; apply-bindings returns a schema where all variables have been replaced with
; their bound referents, given by the bindings map.
(ldefun apply-bindings (schema bindings)
(block outer
(setf cursor schema)
; We make some "intermediate" bindings to avoid situations like
; A -> B
; B -> C
; forcing all As to be Cs if done in order.
(setf alias-bindings (make-hash-table :test #'equal))
(loop for k being the hash-keys of bindings
if (symbolp (gethash k bindings))
do (block alias
(setf old-val (gethash k bindings))
(setf new-val (intern (concat-strs (string old-val) "_BINDTMP")))
(setf (gethash old-val alias-bindings) new-val)
)
)
(loop for key being the hash-keys of bindings do (block inner
(setf val (gethash key bindings))
(setf alias val)
(if (not (null (gethash val alias-bindings)))
(setf alias (gethash val alias-bindings))
)
(setf cursor (replace-vals key alias cursor))
))
; convert back
(loop for old-val being the hash-keys of alias-bindings do (block inner
(setf alias-val (gethash old-val alias-bindings))
(setf cursor (replace-vals alias-val old-val cursor))
))
(return-from outer cursor)
))
; print-schema prints a schema with proper formatting, for readability purposes.
(ldefun print-schema (schema &optional show-invisibles skip-ending-newline)
(block outer
(check #'schema? schema)
(format t "(~s ~s~%" (car schema) (second schema))
(loop for sec in (sort-sections (schema-sections schema))
if (and
(not (equal (section-name sec) ':Paraphrases))
(> (length (section-formulas sec)) 0))
; then
do (block print-sec
(format t " (~s~%" (section-name sec))
(loop for elem in (cdr sec)
; if (or show-invisibles (not (invisible-prop? (second elem))))
do (format t " ~s~%" elem))
(format t " )~%")
)
)
(if skip-ending-newline
; then
(format t ")")
; else
(format t ")~%")
)
)
)
(ldefun paper-print-schema (schema)
(check #'schema? schema)
(format t "(~s ~s~%" (car schema) (second schema))
(loop for sec in (sort-sections (schema-sections schema))
do (format t " ~s~%" (section-name sec))
do (loop for elem in (cdr sec)
do (format t " ~s ~s~%" (car elem) (second elem)))
do (format t " ~%")
)
(format t ")~%")
)
; temporal propositions characterize episodes, generally.
(ldefun temporal-prop? (prop)
; TODO: more nuanced temporal proposition identification
(has-element prop '**)
)
(ldefun lambda-prop? (prop)
; TODO: more nuanced lambda proposition identification
(has-element prop 'LAMBDA.EL)
)
; story-term-constraints takes an EL story, split into N "sentences",
; and returns a hash map where the keys are individual constants and
; the values are lists of atemporal story constraints on those constants.
(ldefun story-term-constraints (story)
(let (
;(gen-kb (list (make-hash-table :test #'equal)
; (make-hash-table :test #'equal)
; (make-hash-table :test #'equal)))
(gen-kb (story-to-kb story))
(constraints (make-hash-table :test #'equal))
)
(block outer
; Index the story into the knowledge base
;(loop for wff in story
; do (add-to-kb wff gen-kb))
; Reindex only small individuals by only non-lambda,
; non-temporal constraints
(loop for arg being the hash-keys of (kb-arg-ind gen-kb)
if (canon-small-individual? arg) do (block inner
(loop for pred in (gethash arg (kb-arg-ind gen-kb))
if (and
(not (temporal-prop? pred))
; TODO: allow lambdas that were in
; the stories, but not the ones we
; created during indexing.
(not (lambda-prop? pred)))
; then
do (if (not (member pred (gethash arg constraints) :test #'equal))
; then
(push pred (gethash arg constraints))
)
)
)
)
; Return the constraints
(return-from outer constraints)
)
)
)
(ldefun filter-invalid-wffs (wffs)
(loop for wff in wffs
if (canon-prop? wff)
collect wff
)
)
(ldefun interesting-constr? (constraint)
(and
(canon-prop? constraint)
(not (time-prop? constraint))
(not (contains *UNINTERESTING-PREDS*
(prop-pred constraint)))
)
)
(ldefun story-select-interesting-term-constraints (story terms)
(loop for sc in (story-select-term-constraints story terms)
if (interesting-constr? sc)
collect sc)
)
; story-select-term-constraints takes a story and a list of individual
; constants and returns a list of all atemporal constraints on any
; of those constants.
(ldefun story-select-term-constraints (story terms)
(let ((constraints (story-term-constraints story)))
(block outer
(setf ret-constrs (remove-duplicates (loop for term being the hash-keys of constraints
if (member term terms :test #'equal)
append (gethash term constraints)) :test #'equal))
; Transitively pull in constraints on all the new small individuals mentioned
; in the constraints we just pulled in.
(if (not (null ret-constrs)) (block pull-in-outer
(setf old-small-inds terms)
(setf new-small-inds (set-difference (extract-small-individuals ret-constrs) old-small-inds :test #'equal))
(loop while (not (null new-small-inds)) do (block pull-in
(setf new-ret-constrs (remove-duplicates (loop for term being the hash-keys of constraints
if (member term new-small-inds :test #'equal)
append (gethash term constraints)) :test #'equal))
(if (null new-ret-constrs)
(return-from pull-in-outer)
)
(setf ret-constrs (remove-duplicates (append ret-constrs new-ret-constrs) :test #'equal))
(setf old-small-inds (remove-duplicates (append old-small-inds new-small-inds) :test #'equal))
(setf new-small-inds (set-difference (extract-small-individuals new-ret-constrs) old-small-inds :test #'equal))
))
))
(return-from outer ret-constrs)
)
)
)
(ldefun schema-header (schema)
(second schema)
)
(ldefun schema-pred (schema)
(prop-pred (car (schema-header schema)))
)
; extract-small-individuals returns a list of the individual constants
; in an EL formula.
(ldefun extract-small-individuals (phi)
(block outer
; (format t "extracting from ~s~%" phi)
(return-from outer (get-elements-pred phi #'canon-small-individual?))
)
)
(ldefun var-to-sk-fn (var)
(block outer
(if (not (varp var))
; then
(return-from outer nil)
)
(return-from outer (intern (concat-strs (remove-prefix (format nil "~s" var) "?") "<-")))
)
)
(ldefun extract-section-vars (schema sec-name)
(block outer
(setf all-inds (list))
(loop for phi in (mapcar #'second (section-formulas (get-section schema sec-name)))
do (setf all-inds (remove-duplicates (append all-inds (get-elements-pred phi #'varp)) :test #'equal))
)
(return-from outer all-inds)
)
)
(ldefun extract-schema-small-individuals (schema)
(block outer
(setf all-inds (list))
; (loop for sec in (nonmeta-sections schema)
(loop for sec in (schema-sections schema)
do (loop for phi in (mapcar #'second (section-formulas sec))
do (setf all-inds (remove-duplicates (append all-inds (extract-small-individuals phi)) :test #'equal))
)
)
(setf all-inds (dedupe (append all-inds (extract-small-individuals (schema-header schema)))))
(return-from outer all-inds)
)
)
(ldefun schema-term-constraints (schema term)
(loop for sec in (nonfluent-sections schema)
append (loop for phi in (section-formulas sec)
; do (format t "second phi: ~s~%" (second phi))
; do (format t "~s contains ~s: ~s~%" (second phi) term (has-element (second phi) term))
if (has-element (second phi) term)
collect phi
)
)
)
(ldefun schema-term-type-constraints (schema term &optional include-ownership)
(loop for constr in (schema-term-constraints schema term)
if (and
(or
(equal 1 (length (prop-all-args (second constr))))
(and include-ownership
(equal (prop-pred (second constr)) 'PERTAIN-TO))
)
(equal term (car (second constr))))
collect constr)
)
(ldefun schema-terms-type-constraints (schema terms &optional include-ownership)
(loop for term in terms
append (schema-term-type-constraints schema term include-ownership))
)
(ldefun new-schema-match-name (pred)
(block outer
(setf spl (split-str (format nil "~s" pred) "."))
(if (and (symbolp pred)
(> (length spl) 2)
(is-num-str? (nth (- (length spl) 2) spl)))
(progn
(setf *SCHEMA-MATCH-NUM* (+ 1 *SCHEMA-MATCH-NUM*))
(return-from outer (intern (join-str-list "."
(append
(subseq spl 0 (- (length spl) 2))
; (list (+ 1 (parse-integer (nth (- (length spl) 2)))))
(list (format nil "~s" *SCHEMA-MATCH-NUM*))
(last spl)
)
)
)))
)
(if (and (symbolp pred) (equal 2 (length spl)))
(progn
(setf *SCHEMA-MATCH-NUM* (+ 1 *SCHEMA-MATCH-NUM*))
(return-from outer
(intern (join-str-list "."
(append
(list (car spl))
(list (format nil "~s" *SCHEMA-MATCH-NUM*))
(last spl)
)
)
))
)
)
)
)
(ldefun constr-name (pred)
(block outer
(if (symbolp pred)
(return-from outer (intern (car (split-str (format nil "~s" pred) "."))))
)
(if (and
(listp pred)
(equal 2 (length pred))
(equal 'PLUR (car pred))
(symbolp (second pred))
)
; then
(return-from outer (intern (concat-strs (car (split-str (format nil "~s" (second pred)) ".")) "S")))
)
(return-from outer nil)
)
)
(ldefun cached-generalize-schema-constants (schema)
(ll-cache
'uncached-generalize-schema-constants
(list schema)
128
)
)
(ldefun linearize-unspecified-steps (schema)
(let (
step-ids
ep-rels
unspec-step-ids
new-schema
new-ep-rels
)
(block outer
(setf step-ids (mapcar #'car (section-formulas (get-section schema ':Steps))))
(setf ep-rels (mapcar #'second (section-formulas (get-section schema ':Episode-relations))))
(setf unspec-step-ids
(loop for step-id in step-ids
if (not (has-element ep-rels step-id))
collect step-id
)
)
(setf deduped-unspec-step-ids (remove-duplicates unspec-step-ids :test #'equal :from-end t))
(if (not (equal deduped-unspec-step-ids unspec-step-ids))
; then
(progn
; (format t "step ID list ~s has duplicates; assuming the first occurrences specify the correct order~%" unspec-step-ids)
(setf unspec-step-ids deduped-unspec-step-ids)
)
)
(if (null unspec-step-ids)
(return-from outer schema)
)
(setf new-schema (copy-item schema))
(setf new-ep-rels
(loop for i from 1 to (- (length unspec-step-ids) 1)
collect (list
(nth (- i 1) unspec-step-ids)
'BEFORE
(nth i unspec-step-ids)
)
)
)
(loop for new-ep-rel in new-ep-rels
do (setf new-schema
(add-constraint new-schema ':Episode-relations new-ep-rel))
)
(return-from outer new-schema)
)
)
)
(ldefun invisible-prop? (prop)
(or
(has-element prop 'HAS-DET.PR)
(has-element prop 'ORIENTS)
)