-
Notifications
You must be signed in to change notification settings - Fork 1
/
sets.cl
416 lines (366 loc) · 12.7 KB
/
sets.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
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
;;; SNePS 3: Sets
;;; =============
;;; Written by Michael Kandefer
;;; with additional/modifications by Stuart C. Shapiro
;;; Implements sets in lisp using hash-tables
;;; Methods should be more efficient
;;; than the corresponding list methods for "set" operations.
;;; 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): ______________________________________.
(defpackage :sneps3-set
(:nicknames :set)
(:export
#:*show-singleton-parens*
#:add-item #:add-items #:and #:copy-add-item #:cardinality #:choose #:copy-set
#:difference #:emptyp
#:*emptyset* #:equal #:find-if #:if #:intersection #:intersection* #:member #:new-set
#:or.set #:print-set #:remove-if #:remove-item #:remove-items #:set
#:setp
#:set-every #:set-some #:set-to-list #:setof #:singleton
#:singletonp #:subset-of #:type #:union #:union* #:unless
#:make-set-if-not-set #:loopset #:when)
(:shadow
cl:and cl:equal cl:find-if cl:if cl:intersection cl:member cl:remove-if cl:set
cl:union cl:unless cl:when))
(in-package :sneps3-set)
(defparameter *show-singleton-parens* nil
"Set to t to indicate that sets should be printed with brackets,
even when singleton. This makes for easier debugging.")
(eval-when (:compile-toplevel :load-toplevel :execute)
(defstruct (set
(:print-function print-set)
(:copier nil))
"Set constructor definition.
Sets have a test function (test), hash table (the-set), and
element type (type)."
(table (make-hash-table))
(test #'eq)
(type t))
(defun new-set (&key (items '()) (test #'eq) (size 12 size-given)
(type t))
"Returns a new set.
If size is provided, the underlying hash-table representation is
set to that initial size;
else, and items is, then the size of the hash-table is set to the
length of items;
else, a default is used.
If items is a non-empty list,
then the elements of that list are added to the set."
(let ((new-set
(make-set
:test test
:type type
:table (make-hash-table
:test test
:size (cond
(size-given size)
(items (length items))
(t size))))))
(cl:when (consp items)
(add-items items new-set))
new-set)))
(eval-when (:load-toplevel :execute)
(defconstant *emptyset* (new-set :size 0)
"A constant empty set
to be used whenever an empty set is to be returned from some function.
It must never be destructively changed."))
(defmethod print-set (set stream depth)
"Print the hash set"
(declare (ignore depth))
(cl:if (emptyp set)
(format stream "()")
(let ((*print-length* nil))
(cl:if (cl:and (not *show-singleton-parens*) (singletonp set))
(format stream "~S" (choose set))
(format stream "~S"
(cons 'setof
(loop for e being each hash-key of (set-table set)
collect e)))))))
(defun copy-set (set)
"Returns a copy of the given set."
(check-type set set)
(let* ((set-copy (new-set :test (set-test set) :type (set-type set)
:size (cardinality set)))
(set-table-copy (set-table set-copy)))
(maphash #'(lambda (k v)
(setf (gethash k set-table-copy) v))
(set-table set))
set-copy))
(defun add-item (item set)
"Desctructively adds item to the set, if item is of the appropriate type."
(check-type set set)
(cl:unless (typep item (set-type set))
(error "~A is not of type ~A, which is required for candidacy in ~
this set.~%" item (set-type set)))
(setf (gethash item (set-table set)) t)
set)
(defun copy-add-item (item set)
"Non-desctructively adds item to the set and returns the new set."
(check-type set set)
(let ((new-set (copy-set set)))
(add-item item new-set)
new-set))
(defun add-items (items set)
"Destructively adds a list of items to the set."
(check-type set set)
(cl:when (consp items)
(loop
for item in items
do (add-item item set))))
(defun singleton (item &key (test #'eq) (type t))
"Creates a new set containing only item, and returns it."
(let ((s (new-set :size 1 :test test :type type)))
(add-item item s)
s))
(defun remove-item (item set)
"Destructively removes item from the set.
Returns the item if removal was successful."
(check-type set set)
(cl:when (remhash item (set-table set))
item))
(defun copy-remove-item (item set)
"Destructively removes item from the set.
Returns the item if removal was successful."
(check-type set set)
(cl:when (remhash item (set-table set))
item))
(defun remove-items (items set)
"Destructively remove items from the set."
(check-type set set)
(cl:when (consp items)
(loop
for item in items
if (remove-item item set)
collect item)))
(defun check-compatible (s1 s2 operation)
"Checks if two sets have compatible equality operators."
(check-type s1 set)
(check-type s2 set)
(cl:unless (eq (set-test s1) (set-test s2))
(warn (format nil
"Performing ~A on incompatible elements. Sets have ~
different element equality functions ~A ~
and ~A. ~
Consider changing (set-test <set>) of ~
one or both sets."
operation (set-test s1) (set-test s2))))
(cl:unless (eq (set-type s1) (set-type s2))
(warn (format nil
"Performing ~A on incompatible elements. Sets have ~
different element types ~A ~
and ~A. ~
Consider changing (set-type <set>) of ~
one or both sets."
operation (set-type s1) (set-type s2)))))
(defun union (s1 s2)
"Returns a set that is the union of the two argument sets."
(check-type s1 set)
(check-type s2 set)
(check-compatible s1 s2 "union")
(let ((s3 (copy-set s1)))
(loop for item being each hash-key of (set-table s2)
do (add-item item s3))
s3))
(defun union* (&rest sets)
(cl:if sets
(reduce #'union sets)
(new-set)))
(defun cardinality (set)
"Returns the set cardinality"
(check-type set set)
(hash-table-count (set-table set)))
(defun member (item set)
"Returns True if item is a member of the set;
else False."
(gethash item (set-table set)))
(defun intersection (s1 s2)
"Returns a set that is the intersection of the two argument sets."
(check-type s1 set)
(check-type s2 set)
(check-compatible s1 s2 "intersection")
(let ((s3 (new-set :test (set-test s1))))
(multiple-value-bind (small-set large-set)
(cl:if (< (cardinality s1) (cardinality s2))
(values s1 s2)
(values s2 s1))
(loop for item being each hash-key of (set-table small-set)
when (member item large-set)
do (add-item item s3)))
s3))
(defun intersection* (&rest sets)
(cl:if sets
(reduce #'intersection sets)
(new-set)))
(defun difference (s1 s2)
"Returns the set which contains
the elements of the set s1 that are not elements of the set s2."
(check-type s1 set)
(check-type s2 set)
(check-compatible s1 s2 "difference")
(let ((s3 (new-set :test (set-test s1))))
(loop for item being each hash-key of (set-table s1)
unless (member item s2)
do (add-item item s3))
s3))
(defun equal (s1 s2)
"Returns t if two sets are equal, nil otherwise."
(check-type s1 set)
(check-type s2 set)
(cl:and (= (cardinality s1) (cardinality s2))
(loop for item being each hash-key of (set-table s1)
unless (member item s2)
do (return nil)
finally (return t))))
(defun emptyp (set)
"Returns true if the set is the empty set"
(check-type set set)
(= (cardinality set) 0))
(defun subset-of (s1 s2)
"Returns t
if the set s1 is a subset (not necessarily proper) of the set s2;
nil otherwise."
(check-type s1 set)
(check-type s2 set)
(cl:and (<= (cardinality s1) (cardinality s2))
(loop for item being each hash-key of (set-table s1)
unless (member item s2)
do (return nil)
finally (return t))))
(defmacro loopset (for elt in set &body forms)
"Iterates the forms as a loop body,
with var taking on successive elements
of the set which is the value of setform.
Returns what the loop macro returns."
(declare (ignore for in))
`(loop for ,elt being each hash-key of (set::set-table ,set)
,@forms))
(defun singletonp (set)
"If the set has exactly one member, returns True;
else returns nil."
(check-type set set)
(= (cardinality set) 1))
(defun choose (set)
"Returns an arbitrary member of the given set."
(check-type set set)
(loop for e being each hash-key of (set-table set)
do (return e)))
(defun remove-if (pred set)
"Returns a new set without those elements in set that pass the pred test."
(let ((result (new-set :test (set-test set))))
(loopset for item in set
unless (funcall pred item)
do (add-item item result))
result))
(defun find-if (pred set)
"Returns an item in set that satisfies the pred test,
or nil if there is none."
(loopset for item in set
when (funcall pred item)
do (return-from find-if item)))
(defun set-to-list (set)
"Returns a list contianing the elements of this set."
(let ((result '()))
(maphash #'(lambda (k v)
(declare (ignore v))
(push k result))
(set-table set))
result))
(defun setp (set?)
"Returns t if set? is a set"
(typep set? 'set))
(defun make-set-if-not-set (element)
"Returns the argument if it is a set, and the set containing only that argument otherwise"
(cl:if (setp element)
element
(new-set :items
(cl:if (not (consp element))
(list element)
element))))
(defmacro or.set (&rest setexprs)
"Returns the evaluation of the first set expression
that evaluates to a non-empty set;
or the empty set if none do."
(cl:if (endp setexprs)
'*emptyset*
(let ((expr (gensym)))
`(let ((,expr ,(first setexprs)))
(cl:if (emptyp ,expr)
(or.set ,@(rest setexprs))
,expr)))))
(defun set-every (pred set)
"Given a predicate and a set, returns true
if the predicate holds for every element"
(loopset for item in set
unless (funcall pred item)
do (return-from set-every nil))
(return-from set-every t))
(defun set-some (pred set)
"Given a predicate and a set, returns true
if the predicate holds for some element"
(loopset for item in set
when (funcall pred item)
do (return-from set-some item)))
(defmacro when (expr &body forms)
"If expr evaluates to a non-empty set or a non-null non-set,
then the forms are evaluated;
otherwise the forms are not evaluated.
Returns the value of the last form evaluated."
(let ((exprval (gensym)))
`(let ((,exprval ,expr))
(cl:unless (or (cl:and (typep ,exprval 'set)
(set:emptyp ,exprval))
(null ,exprval))
,@forms))))
(defmacro unless (expr &body forms)
"If expr evaluates to an empty set or to nil,
then the forms are evaluated;
otherwise the forms are not evaluated.
Returns the value of the last form evaluated."
(let ((exprval (gensym)))
`(let ((,exprval ,expr))
(cl:when (or (cl:and (typep ,exprval 'set)
(set:emptyp ,exprval))
(null ,exprval))
,@forms))))
(defmacro if (expr ifform elseform)
"If expr evaluates to a non-empty set or a non-null non-set,
then the first form is evaluated;
otherwise the second form is evalulated.
Returns the value of the last form evaluated."
(let ((exprval (gensym)))
`(let ((,exprval ,expr))
(cl:if (or (cl:and (typep ,exprval 'set)
(set:emptyp ,exprval))
(null ,exprval))
,elseform
,ifform))))
(defmacro and (&rest exprs)
"Returns nil if any expr evaluates to nil or to an empty set;
otherwise returns t."
(cl:if (endp exprs)
t
(let ((expr (gensym)))
`(let ((,expr ,(first exprs)))
(cl:if (cl:or (null ,expr)
(cl:and (typep ,expr 'set)
(emptyp ,expr)))
nil
(and ,@(rest exprs)))))))