3 ;This file contains 2 packages for Set Theory -
4 ;the first is for LISP and the second is for MACSYMA.
9 ;contents of the LISP Set Package:
10 ;INTERSECT+ INTERSECT2
24 ;auxiliary functions: XEROX
27 (DECLARE #-NIL
(MAPEX T
)
28 (SPECIAL CANONLT $CANONLT SUPERCANONLT
)
29 #-NIL
(*FEXPR ERRM1 ERRM2
)
34 (LIST 'APPEND
(CADR X
) NIL
))
40 (DO ((X (LISTIFY N
) (DO ((Y X
(CDDR Y
))
41 (A NIL
(CONS (INTERSECT2 (CAR Y
) (CADR Y
)) A
)))
43 ((NULL (CDR X
)) (CAR X
))))
46 (DEFUN INTERSECT2
(A B
)
47 (SETQ A
(SETIFY A
) B
(SETIFY B
))
49 1 (COND ((OR (NULL A
) (NULL B
))
50 (RETURN (NREVERSE U
)))
51 ((FUNCALL CANONLT
(CAR A
) (CAR B
))
53 ((FUNCALL CANONLT
(CAR B
) (CAR A
))
55 (T (SETQ U
(CONS (CAR A
) U
)
63 (DO ((X (LISTIFY N
) (DO ((Y X
(CDDR Y
))
64 (A NIL
(CONS (UNION2 (CAR Y
) (CADR Y
)) A
)))
66 ((NULL (CDR X
)) (CAR X
))))
70 (SETQ A
(SETIFY A
) B
(SETIFY B
))
72 1 (COND ((NULL A
) (RETURN (APPEND (NREVERSE U
) B
)))
73 ((NULL B
) (RETURN (APPEND (NREVERSE U
) A
)))
74 ((FUNCALL CANONLT
(CAR A
) (CAR B
))
75 (SETQ U
(CONS (CAR A
) U
)
77 ((FUNCALL CANONLT
(CAR B
) (CAR A
))
78 (SETQ U
(CONS (CAR B
) U
)
80 (T (SETQ U
(CONS (CAR A
) U
)
86 (DEFUN COMPLEMENT
(A B
)
87 (SETQ A
(SETIFY A
) B
(SETIFY B
))
89 1 (COND ((NULL A
) (RETURN (APPEND (NREVERSE U
) B
)))
90 ((NULL B
) (RETURN (NREVERSE U
)))
91 ((FUNCALL CANONLT
(CAR A
) (CAR B
))
93 ((FUNCALL CANONLT
(CAR B
) (CAR A
))
94 (SETQ U
(CONS (CAR B
) U
)
96 (T (SETQ A
(CDR A
) B
(CDR B
))))
100 (DEFUN SETDIFFERENCE
(A B
) (COMPLEMENT B A
))
103 (DEFUN SYMMDIFFERENCE
(A B
)
104 (UNION2 (COMPLEMENT A B
) (COMPLEMENT B A
)))
112 ((NULL X
) (MAPCAR (FUNCTION REVERSE
) (NREVERSE A
)))
118 ((NULL A
) (NREVERSE B
))
119 (SETQ B
(CONS (CONS Y
(CAR A
)) B
)))
127 (T (SETQ X
(SORT (XEROX X
) SUPERCANONLT
))
128 (DO ((X (CDR X
) (CDR X
))
131 ((NULL X
) (NREVERSE Y
))
132 (COND ((FUNCALL CANONLT A
(CAR X
))
133 (SETQ A
(CAR X
) Y
(CONS A Y
))))))))
137 (DEFUN SUBSET
(A F
) (SUBLIST (SETIFY A
) F
))
142 ((NULL A
) (NREVERSE X
))
143 (COND ((FUNCALL F
(CAR A
))
144 (SETQ X
(CONS (CAR A
) X
))))))
149 (DO ((L (CDR L
) (CDR L
))
152 (OR (FUNCALL CANONLT A
(CAR L
))
157 (DEFUN SETEQUALP
(A B
)
158 (AND (= (LENGTH A
) (LENGTH B
))
162 (COND ((OR (FUNCALL CANONLT
(CAR A
) (CAR B
))
163 (FUNCALL CANONLT
(CAR B
) (CAR A
)))
167 ;SETEQUALP ASSUMES A AND B ARE SETS
170 (SETQ A
(SETIFY A
) B
(SETIFY B
))
171 (SETEQUALP A
(INTERSECT2 A B
)))
174 (DEFUN DISJOINTP
(A B
)
175 (NULL (INTERSECT2 A B
)))
178 ;The MACSYMA Set Package:
180 ;contents of MACSYMA Set Package:
182 ;$COMPLEMENT $SETDIFFERENCE $SYMMDIFFERENCE
185 ;$SETP $SUBSETP $DISJOINTP
188 ;auxiliary functions:
189 ;INLIST OUTLIST SUBLISTM CANONLT-SET
198 (DEFMACRO OUTLIST
(X)
207 ((LAMBDA (X CANONLT SUPERCANONLT
)
211 (APPLY (FUNCTION INTERSECT
+)
212 (MAPCAR (FUNCTION INLIST
) X
))))
213 (T (ERRM2 NIL
'|INTERSECT|
'|lists|
))))
220 ((LAMBDA (X CANONLT SUPERCANONLT
)
224 (APPLY (FUNCTION UNION
+)
225 (MAPCAR (FUNCTION INLIST
) X
))))
226 (T (ERRM2 NIL
'|UNION|
'|lists|
))))
232 (DEFUN $COMPLEMENT
(A B
)
233 (COND ((AND ($LISTP A
) ($LISTP B
))
235 ((LAMBDA (CANONLT SUPERCANONLT
)
237 (COMPLEMENT (INLIST A
) (INLIST B
)))
240 (T (ERRM2 NIL
'|COMPLEMENT|
'|lists|
))))
244 (DEFUN $SETDIFFERENCE
(A B
)
245 (COND ((AND ($LISTP A
) ($LISTP B
))
247 ((LAMBDA (CANONLT SUPERCANONLT
)
249 (COMPLEMENT (INLIST B
) (INLIST A
)))
252 (T (ERRM2 NIL
'|SETDIFFERENCE|
'|lists|
))))
256 (DEFUN $SYMMDIFFERENCE
(A B
)
257 (COND ((AND ($LISTP A
) ($LISTP B
))
259 ((LAMBDA (CANONLT SUPERCANONLT
)
261 (SYMMDIFFERENCE (INLIST A
) (INLIST B
)))
264 (T (ERRM2 NIL
'|SYMMDIFFERENCE|
'|lists|
))))
271 ((LAMBDA (CANONLT SUPERCANONLT
)
274 (MAPCAR (FUNCTION OUTLIST
)
275 (POWERSET (INLIST X
)))))
278 (T (ERRM1 NIL
'|POWERSET|
'|a list|
))))
285 ((LAMBDA (CANONLT SUPERCANONLT
)
287 (OUTLIST (SETIFY (INLIST X
))))
290 (T (ERRM1 NIL
'|SETIFY|
'|a list|
))))
297 ((LAMBDA (CANONLT SUPERCANONLT
)
299 (SUBLISTM (INLIST A
) F
))
302 (T (ERRM1 '|first|
'|SUBSET|
'|a list|
))))
310 ((LAMBDA (CANONLT SUPERCANONLT
)
315 (T (ERRM1 NIL
'|SETP|
'|a list|
))))
319 (DEFUN $SUBSETP
(A B
)
320 (COND ((AND ($LISTP A
) ($LISTP B
))
321 ((LAMBDA (CANONLT SUPERCANONLT
)
323 (SUBSETP (INLIST A
) (INLIST B
)))
326 (T (ERRM2 NIL
'|SUBSETP|
'|lists|
))))
330 (DEFUN $DISJOINTP
(A B
)
331 (COND ((AND ($LISTP A
) ($LISTP B
))
332 ((LAMBDA (CANONLT SUPERCANONLT
)
334 (DISJOINTP (INLIST A
) (INLIST B
)))
337 (T (ERRM2 NIL
'|DISJOINTP|
'|lists|
))))
343 (OR ($LISTP
(CAR L
)) (RETURN NIL
))))
348 (DEFUN SUBLISTM
(A F
)
351 ((NULL A
) (NREVERSE X
))
352 (COND ((MEVALP (LIST (NCONS F
) (CAR A
)))
353 (SETQ X
(CONS (CAR A
) X
))))))
358 (DEFUN CANONLT-SET NIL
359 (OR (EQ $CANONLT
'$ORDERLESSP
)
360 (SETQ CANONLT
'(LAMBDA (X Y
) (MCALL $CANONLT X Y
))
361 SUPERCANONLT
'(LAMBDA (X Y
)
362 (COND ((MCALL $CANONLT X Y
) T
)
363 ((MCALL $CANONLT Y X
) NIL
)
364 (T ($ORDERLESSP X Y
)))))))
368 #-NIL
(DECLARE (READ) (READ))
370 (DEFUN INLIST
(X) (CDR X
))
372 (DEFUN OUTLIST
(X) (CONS '(MLIST SIMP
) X
))
375 (SETQ $CANONLT
'$ORDERLESSP
377 SUPERCANONLT
'$ORDERLESSP
)
379 (DEFUN ERRM0
(&rest X
)
380 (PRINC '|Incorrect argument syntax to function |
)
386 (DEFUN ERRM1
(&rest X
)
391 (PRINC '|argument to |
)
398 (DEFUN ERRM2
(&rest X
)
403 (PRINC '|arguments to |
)