-
Notifications
You must be signed in to change notification settings - Fork 2
/
tools.lisp
1587 lines (1369 loc) · 59.9 KB
/
tools.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
;
;; *** TOOLS.LISP ***
(in-package "OSCAR")
(proclaim
'(special pause-flag *metered-calls* *callees* *blank-line* *line-columns* *uncalled-callers*))
(defvar *old-definitions* nil)
; *MACROS*
(defmacro mem1 (x) `(car ,x))
(defmacro mem2 (x) `(cadr ,x))
(defmacro mem3 (x) `(nth 2 ,x))
(defmacro mem4 (x) `(nth 3 ,x))
(defmacro mem5 (x) `(nth 4 ,x))
(defmacro mem6 (x) `(nth 5 ,x))
(defmacro mem7 (x) `(nth 6 ,x))
(defmacro mem8 (x) `(nth 7 ,x))
(defmacro mem9 (x) `(nth 8 ,x))
(defmacro mem10 (x) `(nth 9 ,x))
(defmacro mem11 (x) `(nth 10 ,x))
(defmacro mem12 (x) `(nth 11 ,x))
(defmacro mem13 (x) `(nth 12 ,x))
(defmacro mem14 (x) `(nth 13 ,x))
(defmacro mem15 (x) `(nth 14 ,x))
(defmacro mem16 (x) `(nth 15 ,x))
(defmacro mem17 (x) `(nth 16 ,x))
(defmacro mem18 (x) `(nth 17 ,x))
;nth element of sequence x:
(defmacro element (n x) `(nth ,n ,x))
(defmacro lastmember (x) `(car (last ,x)))
(defmacro for-all (x f) (list 'mapc f x))
(defmacro do-until (P Q)
(list 'loop Q (list 'if P '(return))))
;pretty print function definition (takes unquoted argument):
(defmacro pp (f) `(let ((pv *print-level*)
(pl *print-length*))
(setq *print-level* nil)
(setq *print-length* nil)
(pprint (symbol-function ,f))
(setq *print-level* pv)
(setq *print-length* pl)))
;to test the efficiency of different values of the parameter param in
;the list A. Takes unquoted arguments for param and prog:
(defmacro parameter-test (A param prog)
`(progn (o-terpri) (gc)
(for-all ,A #'(lambda (n)
(setq ,param n)
(o-princ "for ") (o-prin1 ',param)
(o-princ " = ") (o-prin1 n) (o-princ ":")
(time ,prog)
(gc)))))
(defmacro image (K f)
`(mapcar ,f ,K))
(defmacro unionimage (K f)
`(genunion (mapcar ,f ,K)))
#| The following is unnecessary, because genunion already deletes duplicates. |#
(defmacro unionimage+ (K f)
`(remove-duplicates (genunion (mapcar ,f ,K)) :test 'equal))
;This puts something at the front of a queue with index 0:
(defmacro 0-insert (F x A)
`(setf (,F ,A) (cons 0 (cons (cons 0 ,x) (cadr (,F ,A))))))
(defmacro pull (x s)
`(setf ,s (remove-if-equal ,x ,s)))
#| This redefines a constant defined by defconstant. |#
(defmacro redefine-constant (x val)
`(progn
(makunbound ',x)
(defconstant ,x ,val)))
(defmacro unionmapcar (f A) `(apply 'append (mapcar, f ,A)))
;This removes duplicates with test eq.
(defmacro unionmapcar+ (f X)
`(let ((U nil))
(dolist (y ,X)
(dolist (z (funcall ,f y))
(pushnew z U)))
U))
;This removes duplicates with test equal.
(defmacro unionmapcar= (f X)
`(let ((U nil))
(dolist (y ,X)
(dolist (z (funcall ,f y))
(pushnew z U :test 'equal)))
U))
;This removes duplicates with test equal.
(defmacro unionmapcar2= (f X Y)
`(let ((U nil)
(X* ,X)
(Y* ,Y))
(loop
(when (null X*) (return U))
(dolist (z (funcall ,f (mem1 X*) (mem1 Y*)))
(pushnew z U :test 'equal))
(setf X* (cdr X*))
(setf Y* (cdr Y*)))))
(defmacro unionmapcar- (f X &key (test 'eq))
`(let ((U nil))
(dolist (y ,X)
(dolist (z (funcall ,f y))
(pushnew z U :test ,test)))
U))
(when (not (boundp '*old-definitions*))
(proclaim '(special *old-definitions*))
(setf *old-definitions* nil))
;; This gives a horizontally compact display.
(defmacro stepper (form)
`(progn
(setf *print-pretty* nil)
(setf *print-right-margin* nil)
(unwind-protect (step ,form)
(setf *print-right-margin* 250)
(setf *print-pretty* t))))
#| This defines a function in the ordinary way, but also keeps a record of
its arglist and definition on the property list of the function name. When
definitions are changed, a record of the changes is kept in *old-definitions*.
(defmacro defunction (fun arg &rest body)
`(progn
(when (and (get (quote ,fun) 'definition)
(not (equal (get (quote ,fun) 'definition) (quote ,body))))
(push (cons (quote ,fun)
(list (append (list 'defun (quote ,fun) (get (quote ,fun) 'arglist))
(get (quote ,fun) 'definition))
(multiple-value-list (get-decoded-time))))
*old-definitions*))
(setf (get (quote ,fun) 'arglist) (quote ,arg))
(setf (get (quote ,fun) 'definition) (quote ,body))
(defun ,fun ,arg ,@body)))
|#
#+lispworks
(dspec:define-form-parser (defunction (:alias defun)))
#| This defines a function in the ordinary way, but also keeps a record of
its arglist and definition on the property list of the function name. When
definitions are changed, a record of the changes is kept in *old-definitions*.
Definitions created by defunction are saved in the file "Definitions History"
in the OSCAR Folder. The first time a function is defined in a session, the
definition is not saved in "Definitions History". |#
(defmacro defunction (fun arg &rest body)
`(progn
(defun ,fun ,arg ,@body)
(when (and (get (quote ,fun) 'definition)
(not (equal (get (quote ,fun) 'definition) (quote ,body))))
(let ((def (append (list 'defun (quote ,fun) (get (quote ,fun) 'arglist))
(get (quote ,fun) 'definition))))
(push (cons (quote ,fun)
(list def (multiple-value-list (get-decoded-time))))
*old-definitions*)
(with-open-file
(stream (make-pathname :name "definitions-history" :type "lisp"
:defaults #p"OSCAR:")
:direction :output :if-exists :append :if-does-not-exist :create)
(terpri stream) (princ "==============================================" stream)
(terpri stream)
(multiple-value-bind
(seconds minutes hour day month year)
(get-decoded-time)
(princ "Definition overwritten " stream) (princ month stream) (princ "/" stream)
(princ day stream) (princ "/" stream) (princ year stream)
(princ " " stream) (princ hour stream) (princ ":" stream)
(princ minutes stream) (princ ":" stream) (princ seconds stream) (terpri stream))
(princ "--------------------------------------------------------------------------------" stream)
(terpri stream)
(print-pretty def stream) (terpri stream)
)))
(setf (get (quote ,fun) 'arglist) (quote ,arg))
(setf (get (quote ,fun) 'definition) (quote ,body))
(setf (get (quote ,fun) 'source-file)
(make-pathname :name (pathname-name *load-pathname*) :type "lisp"
:defaults #p"OSCAR:"))
(quote ,fun)))
#| This returns and displays the definition history of fun. |#
(defun def-history (fun &optional do-not-print)
(let ((definitions nil))
(dolist (d *old-definitions*)
(when (equal (car d) fun) (push (cdr d) definitions)))
(setf definitions (reverse definitions))
(when (null do-not-print)
(princ "-----------------------------------------") (terpri)
(princ "Definition history for ") (princ fun)
(princ " (most recent definitions first):") (terpri) (terpri)
(dolist (d definitions)
(let ((time (mem2 d)))
(cond (time
(princ "Definition overwritten at ")
(princ (mem5 time)) (princ "/") (princ (mem4 time)) (princ "/")
(princ (mem6 time)) (princ " ") (princ (mem3 time))
(princ ":") (if (< (mem2 time) 10) (princ "0")) (princ (mem2 time))
(princ ":") (if (< (mem2 time) 10) (princ "0")) (princ (mem1 time))
(terpri) (print-pretty (mem1 d)) (terpri) (terpri))
(t
(print-pretty d) (terpri) (terpri)))))
(terpri))))
; definitions))
(defun decode-time (time)
(multiple-value-bind (second minute hour)
(decode-universal-time time)
(list hour minute second)))
(defun print-pretty (x &optional stream)
(let ((pp *print-pretty*))
(setf *print-pretty* t)
(prin1 x stream)
(setf *print-pretty* pp)))
#| This returns and displays a list of functions whose definitions have changed. |#
(defun changed-defs (&optional do-not-print-changes)
(let ((changes nil))
(dolist (d *old-definitions*)
(pushnew (list (car d) (mem3 d)) changes))
; (setf changes (order changes #'lessp))
(cond ((null do-not-print-changes)
(princ "CHANGED DEFINITIONS, MOST RECENT LISTED FIRST:") (terpri)
(dolist (c (reverse changes))
(let ((time (mem2 c)))
(princ (mem1 c))
(when time
(princ " Definition overwritten at ")
(princ (mem5 time)) (princ "/") (princ (mem4 time)) (princ "/")
(princ (mem6 time)) (princ " ") (princ (mem3 time))
(princ ":") (if (< (mem2 time) 10) (princ "0")) (princ (mem2 time))
(princ ":") (if (< (mem2 time) 10) (princ "0")) (princ (mem1 time))
(terpri)))))
(t changes))))
#| This displays the entire history of definition changes during the current session. |#
(defun show-history (&optional fun)
(let ((changed-defs (if fun (list fun) (changed-defs t))))
(dolist (c changed-defs)
(def-history c))
nil))
(defunction compare-trees (tree1 tree2)
(cond ((equal tree1 tree2) (princ "The trees are identical.") (terpri))
(t (princ "The following is the initial part of the trees on which they agree.")
(terpri)
(prin1 (tree-agreement tree1 tree2)))))
(defunction compare-last-def (fun)
(let ((def1 (definition fun))
(def2 (car (e-assoc fun *old-definitions*))))
(cond ((equal def1 def2) (princ "The definitions are identical.") (terpri))
(t (princ "The following is the initial part of the definitions on which they agree.")
(terpri)
(prin1 (tree-agreement def1 def2))))))
#| This returns the initial agreeing part of two trees that are in partial agreement. |#
(defunction tree-agreement (t1 t2)
(cond ((symbolp t1) (if (equal t1 t2) t1))
((listp t1)
(cond ((equal (car t1) (car t2))
(cons (car t1) (tree-agreement (cdr t1) (cdr t2))))
(t (list (tree-agreement (car t1) (car t2))))))))
;This is fast, but does not subtract time spent garbage collecting:
(defmacro elapsed-time (form time &optional increment-flag)
`(let* ((t0 (get-internal-run-time))
(val ,form))
(let ((t1 (- (get-internal-run-time) t0)))
(cond (,increment-flag
(setq ,time (+ t1 ,time)))
(t (setq ,time t1))))
val))
;This returns the length of a string representation of the tree s:
(defunction string-length (s)
(cond ((and s (listp s)) (+ 1 (length s) (apply #'+ (mapcar #'string-length s))))
((numberp s) (length (string-rep s)))
(t (length (string s)))))
;This takes unquoted arguments, and saves the number of calls and the
;elapsed time on the a-list *metered-calls*, associated with X.
(defmacro metered-call (fun def)
` (let* ((time 0)
(value nil)
(reading (assoc ,fun *metered-calls* :test 'equal))
(d-reading (mem2 reading)))
(when reading (setf (cdr reading) (cddr reading)))
(unwind-protect
(setf value (elapsed-time (progn ,def) time t)))
(let ((reading (assoc ,fun *metered-calls* :test 'equal)))
(cond (d-reading
(setf (mem1 d-reading) (1+ (mem1 d-reading)))
(setf (mem2 d-reading) (+ (mem2 d-reading) time))
(setf (cdr reading) (cons d-reading (cdr reading))))
(reading (push (list 1 time) (cdr reading)))
(t (push (list ,fun (list 1 time)) *metered-calls*))))
value))
#| This displays the values of a function for a specified range of values of the arguments.
Ranges is a list of individual values and lists (start end increment) or (start end). If increment is not given,
it is set to 1. The list of values is returned.|#
(defunction display-values (fun &rest ranges)
(terpri)
(setf ranges
(mapcar #'(lambda (R)
(cond ((listp R)
(let ((range (list (car R)))
(value (car R))
(increment (or (mem3 R) 1)))
(loop
(when (>= value (mem2 R)) (return range))
(setf value (+ value increment))
(push value range))))
(t (list R))))
ranges))
(let ((value-list (gencrossproduct (mapcar #'reverse ranges)))
(fun-values nil))
(dolist (values value-list)
(princ "(") (princ fun) (dolist (x values) (princ " ") (princ x)) (princ ") = ")
(let ((value (apply fun values)))
(push value fun-values)
(princ (apply fun values)) (terpri)))
(reverse fun-values)))
;; * LIST FUNCTIONS *
;
;
;(defun cadadr (x) (cadr (cadr x)))
;(defun cddddr (x) (cdr (cdddr x)))
(defun cdddddr (x) (cdr (cddddr x)))
(defun caddddr (x) (car (cddddr x)))
(defun cadddddr (x) (car (cdddddr x)))
(defun cddddddr (x) (cdr (cdddddr x)))
(defun caddddddr (x) (car (cddddddr x)))
(defun member1 (x) (car x))
(defun member2 (x) (cadr x))
(defun member3 (x) (nth 2 x))
(defun member4 (x) (nth 3 x))
(defun member5 (x) (nth 4 x))
(defun member6 (x) (nth 5 x))
(defun member7 (x) (nth 6 x))
(defun member8 (x) (nth 7 x))
(defun member9 (x) (nth 8 x))
(defun member10 (x) (nth 9 x))
(defun member11 (x) (nth 10 x))
(defun member12 (x) (nth 11 x))
(defun member13 (x) (nth 12 x))
(defun member14 (x) (nth 13 x))
(defun member15 (x) (nth 14 x))
(defun member16 (x) (nth 15 x))
(defun member17 (x) (nth 16 x))
(defun member18 (x) (nth 17 x))
;list of first n members of s:
(defun first-n (n s)
(subseq s 0 n))
;This returns the (max m n) if both are non-null:
(defunction max+ (m n)
(if m
(if n (max m n) m)
n))
#| This returns the maximum of an nonempty set of numbers. |#
(defun maximum (X) (apply #'max X))
#| This returns the maximum of an nonempty set of numbers. |#
(defun minimum (X) (apply #'min X))
#| This returns 0.0 if X is empty, otherwise the maximum of X. |#
(defun maximum0 (X) (if X (apply #'max X) 0.0))
#| This returns 0.0 if X is empty, otherwise the minimum of X. |#
(defun minimum0 (X) (if X (apply #'min X) 0.0))
#| This returns T if F is nil, otherwise it funcalls F. |#
(defun funcall* (f x) (or (null f) (funcall f x)))
(defmacro funcall+ (F &rest x)
`(or (null ,F) (funcall ,F ,@x)))
;Given a list of lists, this returns the (or a) longest member:
(defun longest (s) (prog (m n rest)
(setq rest (cdr s))
(setq m (car s))
(setq n (length m))
loop
(cond ((null rest) (return m)))
(cond ((> (length (car rest)) n)
(setq m (car rest)) (setq n (length m))))
(setq rest (cdr rest))
(go loop)))
;first member of sequence x having property p, or "none" if there is none:
(defun first-p (x P) (cond ((null x) "none")
((funcall P (car x)) (car x))
(t (first-p (cdr x) P))))
;R-first member of sequence x, or "none" if x is nil:
(defun r-first (x R)
(cond ((null x) "none")
(t
(do ((rest (cdr x) (cdr rest))
(first (car x) (cond
((funcall R first (car rest)) first)
(t (car rest)))))
((null rest) first)))))
(defun order (X R)
(let ((X* (copy-list X)))
(sort X* R)))
#| This returns the set of non-repeating subsets of length i of X. |#
(defun fixed-length-subsets (n set)
(cond ((> n (length set)) nil)
((zerop n) (list nil))
((= n 1) (mapcar #'list set))
(t (append (mapcar #'(lambda (a) (cons (car set) a))
(fixed-length-subsets (- n 1) (cdr set)))
(fixed-length-subsets n (cdr set))))))
#| This returns the set of all minimal subsets of X that have the property P. |#
(defunction minimal-subsets (X P)
(cond ((funcall P nil) (list nil))
(t
(let ((S nil))
(dotimes (i (length X))
(let ((candidates
(subset #'(lambda (fs)
(every #'(lambda (s*) (not (subsetp= s* fs))) S))
(fixed-length-subsets (1+ i) X))))
(when (null candidates) (return S))
(dolist (y candidates)
(when (funcall P y) (push y S)))))
S))))
#| This returns the set of all maximal subsets of X that have the property P. |#
(defunction maximal-subsets (X P)
(cond ((funcall P X) (list X))
(t
(let ((S nil))
(dotimes (i (length X))
(let ((candidates
(subset #'(lambda (fs)
(every #'(lambda (s*) (disjoint s* fs)) S))
(fixed-length-subsets (1+ i) X))))
(when (null candidates) (return S))
(dolist (y candidates)
(let ((y* (setdifference X y)))
(when (funcall P y*) (push y* S))))))
S))))
(defun ordered-insert (x queue R)
"queue is a list ordered by R, and x is a new member to be inserted
into the right position in the ordering. This returns the new ordered list."
(let ((head nil)
(tail queue))
(loop
(when (null tail) (return (reverse (cons x head))))
(let ((y (mem1 tail)))
(cond ((funcall R y x)
(push y head)
(setf tail (cdr tail)))
(t
(push x tail)
(dolist (z head) (push z tail))
(return tail)))))))
;depth of a list:
(defun depth (s)
(cond ((atom s) 1)
(t (max (1+ (depth (car s))) (depth (cdr s))))))
(defunction occur (x s &key (test 'eq))
(and s (listp s)
(or (funcall test (car s) x)
(occur x (car s) :test test)
(occur x (cdr s) :test test))))
(defun occur* (x s &key (test 'eq))
(or (funcall test x s) (occur x s :test test)))
#| x occurs as a function-call in x. |#
(defunction occur1 (x s &key (test 'eq))
(and s (listp s) (not (eq (car s) 'quote))
(cond ((eq (car s) 'dolist)
(occur1 x (cddr s)))
((or (eq (car s) 'let) (eq (car s) 'let*))
(or (occur1 x (cddr s))
(some #'(lambda (y) (occur1 x (mem2 y))) (cadr s))))
(t
(or (funcall test (car s) x)
(occur1 x (car s))
(and (listp (cdr s))
(some #'(lambda (y) (occur1 x y :test test)) (cdr s))))))))
;; the number of occurrences of x in s
(defunction number-of-occurrences (x s)
(cond ((atom s) (if (equal x s) 1 0))
((null s) 0)
((listp s) (+ (number-of-occurrences x (car s)) (number-of-occurrences x (cdr s))))))
(defun substructures (s)
(cond ((atom s) nil)
(t (cons s (unionmapcar #'substructures s)))))
;find substructures of s containing x:
(defun s-find (x s)
(subset #'(lambda (y) (mem x y)) (substructures s)))
;substitution of one subsequence for another in a sequence:
(defun seq-subst (new old s)
(declare (inline first-n))
(cond ((< (length s) (length old)) s)
((equal old (first-n (length old) s))
(append new (seq-subst new old (nthcdr (length old) s))))
(t (cons (car s) (seq-subst new old (cdr s))))))
(defun =subst (a b c)
(cond ((equal b c) a)
((listp c) (subst a b c :test 'equal))
(t c)))
(defun subst* (a b c &key (test 'eq))
(cond ((atom c) (if (funcall test b c) a c))
(t (subst a b c :test test))))
(defun sublis= (m x)
(cond ((listp x) (sublis m x :test 'equal))
(t (car (sublis m (list x) :test 'equal)))))
; * INSERTION AND DELETION *
;remove uses 'eql'. This uses 'equal':
(defun remove-if-equal (x y)
(remove-if #'(lambda (z) (equal z x)) y))
#| replace first occurrence of x by y in S. |#
(defunction replace-item-in-list (x y S)
(let ((S0 S)
(S* nil))
(loop
(when (equal x (car S0)) (return (append (reverse S*) (cons y (cdr S0)))))
(push (car S0) S*)
(setf S0 (cdr S0))
(when (null S0) (return S)))))
;nondestructively delete nth member of y:
(defun delete-n (n y)
(cond ((equal n (length y)) (first-n (1- n) y))
((> n (length y)) y)
(t (append (first-n (1- n) y) (nthcdr n y)))))
;nondestructively splice x into y at the nth place:
(defun splice (x n y)
(cond ((> n (length y)) (append y (list x)))
(t (append (first-n (1- n) y) (list x) (nthcdr (1- n) y)))))
;This inserts x into its appropriate place in A where A is ordered by R. If R
;is a < relation, this puts x at the end of the sublist of equivalent items, and if
;R is a <= relation, this puts it at the start of the sublist.
(defunction insert (x A R)
(let ((head nil)
(tail A))
(loop
(cond ((null tail) (setq tail (list x)) (return))
((funcall R x (mem1 tail))
(setq tail (cons x tail)) (return))
(t (setq head (cons (mem1 tail) head))
(setq tail (cdr tail)))))
(loop
(cond ((null head) (return))
(t (setq tail (cons (mem1 head) tail))
(setq head (cdr head)))))
tail))
; * SET FUNCTIONS *
(defun mem (element set)
(member element set :test 'equal))
;set-equality:
(defunction == (x y)
(or (eq x y)
(and (listp x) (listp y)
(subsetp x y :test 'equal)
(subsetp y x :test 'equal))))
;; this returns three values: (union x y), (setdifference x y), and (setdifference y x),
;; but if a symbol occurs multiple times in x or y, they are treated as different smbols.
(defunction compare-lists (x y &key (test #'eq))
(let ((xy nil))
(dolist (z x)
(block x+
(dolist (w y)
(cond ((funcall test z w)
(setf x (remove z x :count 1 :test test))
(setf y (remove z y :count 1 :test test))
(push z xy) (return-from x+ nil))))))
(values xy x y)))
#|
(compare-lists '(a a b c) '(a b c d)) returns
(c b a)
(a)
(d)
|#
(defunction === (x y &key (test 'equal))
(or (eq x y)
(and (listp x) (listp y)
(multiple-value-bind
(u d1 d2)
(compare-lists x y :test test)
(declare (ignore u))
(and (null d1) (null d2))))))
(defunction << (x y) (< (round (* 10000 x)) (round (* 10000 y))))
(defunction >> (x y) (> (round (* 10000 x)) (round (* 10000 y))))
(defunction <<= (x y)
(or (eql x y) (<= (round (* 10000 x)) (round (* 10000 y)))))
(defunction >>= (x y)
(or (eql x y) (>= (round (* 10000 x)) (round (* 10000 y)))))
(defunction >< (x y)
(or (eql x y) (eql (round (* 10000 x)) (round (* 10000 y)))))
(defun union= (x y) (union x y :test 'equal))
(defun adjoin= (x y) (adjoin x y :test 'equal))
(defunction remove-duplicates= (x)
(remove-duplicates x :test 'equal))
(defun subset (f l)
(remove-if-not f l))
(defun subsetp= (X Y)
(subsetp X Y :test 'equal))
(defun proper-subset (X Y)
(and (subsetp= X Y) (not (subsetp= Y X))))
;x and y are disjoint, with test 'equal:
(defun disjoint (x y)
(not (some #'(lambda (z) (mem z y)) x)))
;x and y are disjoint, with test 'eq:
(defun disjointp (x y)
(not (some #'(lambda (z) (member z y)) x)))
(defun crossproduct (A B)
(let ((U nil))
(dolist (x A)
(dolist (y B)
(push (list x y) U)))
U))
(defun dot-product (x y)
(unionmapcar #'(lambda (w) (mapcar #'(lambda (z) (cons w z)) y)) x))
;domain, range, and inverse of a set of ordered pairs:
(defun domain (x) (remove-duplicates (mapcar #'car x) :test 'equal))
(defun range (x) (remove-duplicates (mapcar #'cadr x) :test 'equal))
;range of an association list:
(defun a-range (x) (remove-duplicates (mapcar #'cdr x) :test 'equal))
(defun inverse (R) (mapcar #'reverse R))
;(defunction genunion (x) (apply 'append x))
#| The following removes duplicates too. |#
(defunction genunion (x)
(let ((union nil))
(dolist (y x)
(dolist (z y)
(when (not (mem z union)) (push z union))))
union))
(defunction genunion+ (x)
(let ((union nil))
(dolist (y x) (dolist (z y) (pushnew z union)))
union))
(defunction gen-intersection (x)
(cond ((null x) nil)
((equal (length x) 1) (mem1 x))
(t (=intersection (mem1 x) (gen-intersection (cdr x))))))
(defun gencrossproduct (A)
(let ((U nil))
(cond ((cddr A)
(dolist (x (car A))
(dolist (y (gencrossproduct (cdr A)))
(push (cons x y) U)))
U)
((cdr A) (crossproduct (car A) (cadr A)))
(t A))))
(defun powerset (X)
(cond ((null X) (list nil))
(t (let ((p (powerset (cdr X))))
(union= p (mapcar #'(lambda (Y) (cons (car X) Y)) p))))))
(defun setdifference (x y) (set-difference x y :test 'equal))
(defun =intersection (x y) (intersection x y :test 'equal))
(defun list-complexity (x)
(cond ((null X) 0)
((stringp x) 1)
((atom x) 1)
((listp x) (apply #'+ (mapcar #'list-complexity x)))))
; * QUANTIFICATION *
#|
(defun unionmapcar (f A) (apply 'append (mapcar f A)))
;This removes duplicates with test eq.
(defun unionmapcar+ (f X)
(let ((U nil))
(dolist (y X)
(dolist (z (funcall f y))
(pushnew z U)))
U))
;This removes duplicates with test equal.
(defun unionmapcar= (f X)
(let ((U nil))
(dolist (y X)
(dolist (z (funcall f y))
(pushnew z U :test 'equal)))
U))
;This removes duplicates with test equal.
(defun unionmapcar2= (f X Y)
(let ((U nil)
(X* X)
(Y* Y))
(loop
(when (null X*) (return U))
(dolist (z (funcall f (mem1 X*) (mem1 Y*)))
(pushnew z U :test 'equal))
(setf X* (cdr X*))
(setf Y* (cdr Y*)))))
|#
;an assignment is a function in extension. The following checks to see
;whether a putative assignment is consistent, in the sense of assigning
;only one object to each element of the domain:
(defun consistent-assignment (s)
(equal (length s) (length (domain s))))
;this returns the value of assignment for object obj:
(defun value (assignment obj)
(declare (inline subset))
(cadr (apply #'append
(subset #'(lambda (val-arg) (equal (car val-arg) obj))
assignment))))
;This maps a binary function f onto a set x, holding y fixed:
(defun mapcar1 (f x y) (mapcar #'(lambda (z) (apply f (list z y))) x))
; * STRINGS *
(defun explode (s)
(mapcar #'string (coerce s 'list)))
(defun char-list (x)
(declare (inline explode))
(cond ((numberp x) (list (string (code-char (+ 48 x)))))
((characterp x) (list x))
((atom x) (explode (string x)))
((stringp x) (explode x))))
(defun char-num (x)
(mapcar
#'(lambda (i) (char x i))
(mapcar #'1- (nseq (length x)))))
(defunction implode (x)
"where x is a list of strings, this concatenates them into a single string"
(if (null x) nil
(concatenate 'string (car x) (implode (cdr x)))))
(defun imp (s)
(cond ((symbolp s) (string s))
((numberp s) (string-rep s))
(t (coerce (mapcan #'char-num
(mapcan #'char-list s)) 'string))))
(defun string-rep (n) (write-to-string n))
;this returns the integer named by a string:
(defun named-integer (s)
(read-from-string s))
;this returns the decimal-number named by a string:
(defun named-decimal-number (string)
(float (read-from-string string)))
;concatenate two strings:
(defun cat (x y)
(concatenate 'string x y))
;(defun cat (x y)
; (imp (append (explode x) (explode y))))
;concatenate a list of strings:
(defun cat-list (s)
(cond ((null s) nil)
(t (cat (mem1 s) (cat-list (cdr s))))))
;;This returns the substring of s from n through m inclusive. If m is
;;omitted, it is set to the length of the string.
(defun substring (s n &optional (m))
(subseq s n m))
#| This returns the word-strings in a string with spaces. |#
(defun word-list (string)
(let ((letters (explode string)) ;; strings of length 1
(words nil)
(word nil))
(dolist (letter letters)
(cond ((equal letter " ")
(push (implode (reverse word)) words)
(setf word nil))
(t (push letter word))))
(if word (push (implode (reverse word)) words))
(reverse words)))
#| example:
? (word-list "Who is Henry's father")
("Who" "is" "Henry's" "father")
|#
#| This turns a list of strings into a string with spaces. |#
(defun concatenate-words (word-list)
(cond ((cdr word-list)
(cat (car word-list)
(cat " " (concatenate-words (cdr word-list)))))
(t (car word-list))))
#| example:
? (concatenate-words '("Who" "is" "Henry's" "father"))
"Who is Henry's father"
|#
; ** MATCHING **
(defun match (pat exp var)
(labels ((match* (pat exp var bindings)
(cond ((atom pat)
(cond ((mem pat var)
(let ((assoc (assoc pat bindings :test 'equal)))
(cond (assoc (equal exp (cdr assoc)))
(t (list (cons pat exp))))))
(t (equal pat exp))))
((listp pat)
(when (listp exp)
(let ((m (match* (car pat) (car exp) var bindings)))
(cond ((eq m t) (match* (cdr pat) (cdr exp) var bindings))
(m (let ((m* (match* (cdr pat) (cdr exp) var (append m bindings))))
(cond ((eq m* t) m)
(m* (union= m m*))))))))))))
(match* pat exp var nil)))
;this returns the association list of a match of variables to elements
;of s which, when substituted in l yields s. So l is the pattern and s
;is the target. If X is given, the match must be to members of X.
; This assumes that members of var do not occur in s.
;(defunction match (pattern expression var &optional X)
; (catch 'match (pattern-match pattern expression var X)))
;
;(defun pattern-match (l s var &optional X)
; (cond ((equal l s) t)
; ((atom l)
; (cond ((and (mem l var)
; (if X (mem s X) t))
; (list (cons l s)))
; (t (throw 'match nil))))
; ((listp l)
; (cond ((not (listp s)) (throw 'match nil))
; ((not (eq (length l) (length s))) (throw 'match nil))
; ((eql (length l) 1) (pattern-match (car l) (car s) var X))
; (t (let ((m (pattern-match (car l) (car s) var X)))
; (cond ((null m) (throw 'match nil)))
; (let ((l* (cond ((eq m t) (cdr l))
; (t (sublis= m (cdr l))))))
; (cond ((eq m t) (pattern-match l* (cdr s) var X))
; (t (let ((m* (pattern-match l* (cdr s)
; (setdifference var (domain m)) X)))
; (cond ((eq m* t) m)
; (t (append m m*)))))))))))))
(defun merge-matches (m m*)
(cond ((equal m t) m*)
((equal m* t) m)
(t (union= m m*))))
(defun nseq< (n)
(do ((i 0 (1+ i))
(s nil (cons i s)))
((>= i n) (reverse s))))
;this substitutes in accordance with a match m:
(defun match-sublis (m x &key (test 'eq))
(cond ((eq m t) x)
(t (sublis m x :test test))))
(defun match-domain (m)
(if (equal m t) nil (domain m)))
(defun consistent-match (p1 p2)
(not (some #'(lambda (s)
(some #'(lambda (v) (and (equal (car s) (car v))
(not (equal (cdr s) (cdr v)))))
p2)) p1)))
#| (set-match patterns data vars) returns the set of pairs (X m) where m is an a-list of
substitutions for members of vars and X is (mapcar #'(lambda (p) (match-sublis m p))
patterns), and X is a subset of data. This asssumes that vars do not occur in data. |#
;(defunction set-match (patterns data vars)
; (catch 'match (set-match-no-catch patterns data vars)))
;
;(defunction set-match-no-catch (patterns data vars)
; (let ((matches nil)
; (open nil)
; (closed nil))
; (dolist (P patterns)
; (if (some #'(lambda (v) (occur v P)) vars)
; (push P open)
; (if (mem P data)
; (push P closed)
; (throw 'match nil))))
; (cond (open
; (let ((P (mem1 open)))
; (dolist (Q data)
; (let ((m (match P Q vars)))
; (when m
; (dolist (sm (set-match-no-catch
; (match-sublis m (cdr open))
; data
; (setdifference vars (match-domain m))))
; (push (list (adjoin= Q (union= closed (mem1 sm)))
; (merge-matches m (mem2 sm)))
; matches)))))))
; (t (setf matches (list (list closed T)))))
; (when (null matches) (throw 'match nil))
; matches))
(defunction set-match (patterns data vars)
(catch 'match
(let ((matches nil)
(open nil)
(closed nil))
(dolist (P patterns)
(if (some #'(lambda (v) (occur v P)) vars)
(push P open)
(if (mem P data)
(push P closed)
(throw 'match nil))))
(cond (open
(let ((P (mem1 open)))
(dolist (Q data)
(let ((m (match P Q vars)))
(when m
(dolist (sm (set-match
(match-sublis m (cdr open))
data
(setdifference vars (match-domain m))))
(push (list (adjoin= Q (union= closed (mem1 sm)))
(merge-matches m (mem2 sm)))
matches)))))))
(t (setf matches (list (list closed T)))))
(when (null matches) (throw 'match nil))