chiark / gitweb /
src/utilities.lisp, doc/misc.tex: Fix up `find-duplicates'.
[sod] / src / utilities.lisp
CommitLineData
dea4d055
MW
1;;; -*-lisp-*-
2;;;
3;;; Various handy utilities
4;;;
5;;; (c) 2009 Straylight/Edgeware
6;;;
7
8;;;----- Licensing notice ---------------------------------------------------
9;;;
e0808c47 10;;; This file is part of the Sensible Object Design, an object system for C.
dea4d055
MW
11;;;
12;;; SOD is free software; you can redistribute it and/or modify
13;;; it under the terms of the GNU General Public License as published by
14;;; the Free Software Foundation; either version 2 of the License, or
15;;; (at your option) any later version.
16;;;
17;;; SOD is distributed in the hope that it will be useful,
18;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20;;; GNU General Public License for more details.
21;;;
22;;; You should have received a copy of the GNU General Public License
23;;; along with SOD; if not, write to the Free Software Foundation,
24;;; Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25
bb99b695
MW
26(eval-when (:compile-toplevel :load-toplevel :execute)
27 (handler-bind ((warning #'muffle-warning))
28 (cl:defpackage #:sod-utilities
29 (:use #:common-lisp
30
31 ;; MOP from somewhere.
32 #+sbcl #:sb-mop
33 #+(or cmu clisp) #:mop
34 #+ecl #:clos))))
dea4d055
MW
35
36(cl:in-package #:sod-utilities)
37
8e3552b3
MW
38;;;--------------------------------------------------------------------------
39;;; Common symbols.
40;;;
41;;; Sometimes, logically independent packages will want to use the same
42;;; symbol, and these uses (by careful design) don't conflict with each
43;;; other. If we export the symbols here, then the necessary sharing will
44;;; happen automatically.
45
46(export 'int) ; used by c-types and optparse
47
dea4d055
MW
48;;;--------------------------------------------------------------------------
49;;; Macro hacks.
50
51(export 'with-gensyms)
52(defmacro with-gensyms ((&rest binds) &body body)
53 "Evaluate BODY with variables bound to fresh symbols.
54
55 The BINDS are a list of entries (VAR [NAME]), and a singleton list can be
56 replaced by just a symbol; each VAR is bound to a fresh symbol generated
57 by (gensym NAME), where NAME defaults to the symbol-name of VAR."
58 `(let (,@(mapcar (lambda (bind)
59 (multiple-value-bind (var name)
60 (if (atom bind)
61 (values bind (concatenate 'string
62 (symbol-name bind) "-"))
63 (destructuring-bind
64 (var &optional
65 (name (concatenate 'string
66 (symbol-name var) "-")))
67 bind
68 (values var name)))
69 `(,var (gensym ,name))))
70 binds))
71 ,@body))
72
73(eval-when (:compile-toplevel :load-toplevel :execute)
74 (defun strip-quote (form)
75 "If FORM looks like (quote FOO) for self-evaluating FOO, return FOO.
76
77 If FORM is a symbol whose constant value is `nil' then return `nil'.
78 Otherwise return FORM unchanged. This makes it easier to inspect constant
79 things. This is a utility for `once-only'."
80
81 (cond ((and (consp form)
82 (eq (car form) 'quote)
83 (cdr form)
84 (null (cddr form)))
85 (let ((body (cadr form)))
86 (if (or (not (or (consp body) (symbolp body)))
87 (member body '(t nil))
88 (keywordp body))
89 body
90 form)))
91 ((and (symbolp form) (boundp form) (null (symbol-value form)))
92 nil)
93 (t
94 form))))
95
96(export 'once-only)
74ca1bf5 97(defmacro once-only ((&rest binds) &body body)
dea4d055
MW
98 "Macro helper for preventing repeated evaluation.
99
100 The syntax is actually hairier than shown:
101
102 once-only ( [[ :environment ENV ]] { VAR | (VAR [VALUE-FORM]) }* )
103 { FORM }*
104
105 So, the BINDS are a list of entries (VAR [VALUE-FORM]); a singleton list
106 can be replaced by just a symbol VAR, and the VALUE-FORM defaults to VAR.
107 But before them you can have keyword arguments. Only one is defined so
108 far. See below for the crazy things that does.
109
110 The result of evaluating a ONCE-ONLY form is a form with the structure
111
112 (let ((#:GS1 VALUE-FORM1)
113 ...
114 (#:GSn VALUE-FORMn))
115 STUFF)
116
117 where STUFF is the value of the BODY forms, as an implicit progn, in an
118 environment with the VARs bound to the corresponding gensyms.
119
120 As additional magic, if any of the VALUE-FORMs is actually constant (as
121 determined by inspection, and aided by `constantp' if an :environment is
122 supplied, then no gensym is constructed for it, and the VAR is bound
123 directly to the constant form. Moreover, if the constant form looks like
124 (quote FOO) for a self-evaluating FOO then the outer layer of quoting is
125 stripped away."
126
127 ;; We need an extra layer of gensyms in our expansion: we'll want the
128 ;; expansion to examine the various VALUE-FORMs to find out whether they're
129 ;; constant without evaluating them repeatedly. This also helps with
130 ;; another problem: we explicitly encourage the rebinding of a VAR
131 ;; (probably a macro argument) to a gensym which will be bound to the value
132 ;; of the form previously held in VAR itself -- so the gensym and value
133 ;; form must exist at the same time and we need two distinct variables.
134
135 (with-gensyms ((envvar "ENV-") lets sym (bodyfunc "BODY-"))
136 (let ((env nil))
137
138 ;; First things first: let's pick up the keywords.
139 (loop
140 (unless (and binds (keywordp (car binds)))
141 (return))
142 (ecase (pop binds)
143 (:environment (setf env (pop binds)))))
144
145 ;; Now we'll investigate the bindings. Turn each one into a list (VAR
146 ;; VALUE-FORM TEMP) where TEMP is an appropriate gensym -- see the note
147 ;; above.
148 (let ((canon (mapcar (lambda (bind)
149 (multiple-value-bind (var form)
150 (if (atom bind)
151 (values bind bind)
152 (destructuring-bind
153 (var &optional (form var)) bind
154 (values var form)))
155 (list var form
156 (gensym (format nil "T-~A-"
157 (symbol-name var))))))
158 binds)))
159
160 `(let* (,@(and env `((,envvar ,env)))
161 (,lets nil)
162 ,@(mapcar (lambda (bind)
163 (destructuring-bind (var form temp) bind
164 (declare (ignore var))
165 `(,temp ,form)))
166 canon)
167 ,@(mapcar (lambda (bind)
168 (destructuring-bind (var form temp) bind
169 (declare (ignore form))
170 `(,var
171 (cond ((constantp ,temp
172 ,@(and env `(,envvar)))
173 (strip-quote ,temp))
174 ((symbolp ,temp)
175 ,temp)
176 (t
177 (let ((,sym (gensym
178 ,(concatenate 'string
179 (symbol-name var)
180 "-"))))
181 (push (list ,sym ,temp) ,lets)
182 ,sym))))))
183 canon))
184 (flet ((,bodyfunc () ,@body))
185 (if ,lets
186 `(let (,@(nreverse ,lets)) ,(,bodyfunc))
187 (,bodyfunc))))))))
188
189(export 'parse-body)
b8c698ee 190(defun parse-body (body &key (docp t) (declp t))
dea4d055
MW
191 "Parse the BODY into a docstring, declarations and the body forms.
192
193 These are returned as three lists, so that they can be spliced into a
194 macro expansion easily. The declarations are consolidated into a single
b8c698ee
MW
195 `declare' form. If DOCP is nil then a docstring is not permitted; if
196 DECLP is nil, then declarations are not permitted."
dea4d055
MW
197 (let ((decls nil)
198 (doc nil))
199 (loop
200 (cond ((null body) (return))
b8c698ee 201 ((and declp (consp (car body)) (eq (caar body) 'declare))
dea4d055 202 (setf decls (append decls (cdr (pop body)))))
b8c698ee 203 ((and docp (stringp (car body)) (not doc) (cdr body))
dea4d055
MW
204 (setf doc (pop body)))
205 (t (return))))
206 (values (and doc (list doc))
207 (and decls (list (cons 'declare decls)))
208 body)))
209
e8abb286
MW
210;;;--------------------------------------------------------------------------
211;;; Locatives.
212
213(export '(loc locp))
214(defstruct (loc (:predicate locp) (:constructor make-loc (reader writer)))
215 "Locative data type. See `locf' and `ref'."
216 (reader nil :type function)
217 (writer nil :type function))
218
219(export 'locf)
220(defmacro locf (place &environment env)
221 "Slightly cheesy locatives.
222
223 (locf PLACE) returns an object which, using the `ref' function, can be
224 used to read or set the value of PLACE. It's cheesy because it uses
225 closures rather than actually taking the address of something. Also,
226 unlike Zetalisp, we don't overload `car' to do our dirty work."
227 (multiple-value-bind
228 (valtmps valforms newtmps setform getform)
229 (get-setf-expansion place env)
230 `(let* (,@(mapcar #'list valtmps valforms))
231 (make-loc (lambda () ,getform)
232 (lambda (,@newtmps) ,setform)))))
233
234(export 'ref)
235(declaim (inline ref (setf ref)))
236(defun ref (loc)
237 "Fetch the value referred to by a locative."
238 (funcall (loc-reader loc)))
239(defun (setf ref) (new loc)
240 "Store a new value in the place referred to by a locative."
241 (funcall (loc-writer loc) new))
242
243(export 'with-locatives)
244(defmacro with-locatives (locs &body body)
245 "Evaluate BODY with implicit locatives.
246
247 LOCS is a list of items of the form (SYM [LOC-EXPR]), where SYM is a
248 symbol and LOC-EXPR evaluates to a locative. If LOC-EXPR is omitted, it
249 defaults to SYM. As an abbreviation for a common case, LOCS may be a
250 symbol instead of a list.
251
252 The BODY is evaluated in an environment where each SYM is a symbol macro
253 which expands to (ref LOC-EXPR) -- or, in fact, something similar which
254 doesn't break if LOC-EXPR has side-effects. Thus, references, including
255 `setf' forms, fetch or modify the thing referred to by the LOC-EXPR.
256 Useful for covering over where something uses a locative."
257 (setf locs (mapcar (lambda (item)
258 (cond ((atom item) (list item item))
259 ((null (cdr item)) (list (car item) (car item)))
260 (t item)))
261 (if (listp locs) locs (list locs))))
262 (let ((tt (mapcar (lambda (l) (declare (ignore l)) (gensym)) locs))
263 (ll (mapcar #'cadr locs))
264 (ss (mapcar #'car locs)))
265 `(let (,@(mapcar (lambda (tmp loc) `(,tmp ,loc)) tt ll))
266 (symbol-macrolet (,@(mapcar (lambda (sym tmp)
267 `(,sym (ref ,tmp))) ss tt))
268 ,@body))))
269
dea4d055
MW
270;;;--------------------------------------------------------------------------
271;;; Anaphorics.
272
273(export 'it)
274
275(export 'aif)
276(defmacro aif (cond cons &optional (alt nil altp))
277 "If COND is not nil, evaluate CONS with `it' bound to the value of COND.
278
279 Otherwise, if given, evaluate ALT; `it' isn't bound in ALT."
280 (once-only (cond)
281 `(if ,cond (let ((it ,cond)) ,cons) ,@(and altp `(,alt)))))
282
283(export 'awhen)
284(defmacro awhen (cond &body body)
285 "If COND, evaluate BODY as a progn with `it' bound to the value of COND."
286 `(let ((it ,cond)) (when it ,@body)))
287
3e166443
MW
288(export 'aand)
289(defmacro aand (&rest forms)
290 "Like `and', but anaphoric.
291
292 Each FORM except the first is evaluated with `it' bound to the value of
293 the previous one. If there are no forms, then the result it `t'; if there
294 is exactly one, then wrapping it in `aand' is pointless."
295 (labels ((doit (first rest)
296 (if (null rest)
297 first
298 `(let ((it ,first))
299 (if it ,(doit (car rest) (cdr rest)) nil)))))
300 (if (null forms)
301 't
302 (doit (car forms) (cdr forms)))))
303
dea4d055 304(export 'acond)
bf090e02 305(defmacro acond (&body clauses &environment env)
dea4d055
MW
306 "Like COND, but with `it' bound to the value of the condition.
307
308 Each of the CLAUSES has the form (CONDITION FORM*); if a CONDITION is
309 non-nil then evaluate the FORMs with `it' bound to the non-nil value, and
310 return the value of the last FORM; if there are no FORMs, then return `it'
311 itself. If the CONDITION is nil then continue with the next clause; if
312 all clauses evaluate to nil then the result is nil."
313 (labels ((walk (clauses)
314 (if (null clauses)
315 `nil
316 (once-only (:environment env (cond (caar clauses)))
317 (if (and (constantp cond)
318 (if (and (consp cond) (eq (car cond) 'quote))
319 (cadr cond) cond))
320 (if (cdar clauses)
321 `(let ((it ,cond))
322 (declare (ignorable it))
323 ,@(cdar clauses))
324 cond)
325 `(if ,cond
326 ,(if (cdar clauses)
327 `(let ((it ,cond))
328 (declare (ignorable it))
329 ,@(cdar clauses))
330 cond)
331 ,(walk (cdr clauses))))))))
332 (walk clauses)))
333
334(export '(acase aecase atypecase aetypecase))
335(defmacro acase (value &body clauses)
336 `(let ((it ,value)) (case it ,@clauses)))
337(defmacro aecase (value &body clauses)
338 `(let ((it ,value)) (ecase it ,@clauses)))
339(defmacro atypecase (value &body clauses)
340 `(let ((it ,value)) (typecase it ,@clauses)))
341(defmacro aetypecase (value &body clauses)
342 `(let ((it ,value)) (etypecase it ,@clauses)))
343
344(export 'asetf)
345(defmacro asetf (&rest places-and-values &environment env)
346 "Anaphoric update of places.
347
348 The PLACES-AND-VALUES are alternating PLACEs and VALUEs. Each VALUE is
349 evaluated with IT bound to the current value stored in the corresponding
350 PLACE."
351 `(progn ,@(loop for (place value) on places-and-values by #'cddr
352 collect (multiple-value-bind
353 (temps inits newtemps setform getform)
354 (get-setf-expansion place env)
355 `(let* (,@(mapcar #'list temps inits)
356 (it ,getform))
357 (multiple-value-bind ,newtemps ,value
358 ,setform))))))
359
360;;;--------------------------------------------------------------------------
361;;; MOP hacks (not terribly demanding).
362
bf090e02
MW
363(export 'instance-initargs)
364(defgeneric instance-initargs (instance)
365 (:documentation
366 "Return a plausble list of initargs for INSTANCE.
367
368 The idea is that you can make a copy of INSTANCE by invoking
369
370 (apply #'make-instance (class-of INSTANCE)
371 (instance-initargs INSTANCE))
372
373 The default implementation works by inspecting the slot definitions and
374 extracting suitable initargs, so this will only succeed if enough slots
375 actually have initargs specified that `initialize-instance' can fill in
376 the rest correctly.
377
378 The list returned is freshly consed, and you can destroy it if you like.")
379 (:method ((instance standard-object))
380 (mapcan (lambda (slot)
381 (aif (slot-definition-initargs slot)
382 (list (car it)
383 (slot-value instance (slot-definition-name slot)))
384 nil))
385 (class-slots (class-of instance)))))
386
dea4d055
MW
387(export '(copy-instance copy-instance-using-class))
388(defgeneric copy-instance-using-class (class instance &rest initargs)
389 (:documentation
390 "Metaobject protocol hook for `copy-instance'.")
391 (:method ((class standard-class) instance &rest initargs)
392 (let ((copy (allocate-instance class)))
393 (dolist (slot (class-slots class))
394 (let ((name (slot-definition-name slot)))
395 (when (slot-boundp instance name)
396 (setf (slot-value copy name) (slot-value instance name)))))
397 (apply #'shared-initialize copy nil initargs))))
398(defun copy-instance (object &rest initargs)
399 "Construct and return a copy of OBJECT.
400
401 The new object has the same class as OBJECT, and the same slot values
402 except where overridden by INITARGS."
403 (apply #'copy-instance-using-class (class-of object) object initargs))
404
9ec578d9
MW
405(export '(generic-function-methods method-specializers
406 eql-specializer eql-specializer-object))
407
dea4d055
MW
408;;;--------------------------------------------------------------------------
409;;; List utilities.
410
411(export 'make-list-builder)
412(defun make-list-builder (&optional initial)
413 "Return a simple list builder."
414
415 ;; The `builder' is just a cons cell whose cdr will be the list that's
416 ;; wanted. Effectively, then, we have a list that's one item longer than
417 ;; we actually want. The car of this extra initial cons cell is always the
418 ;; last cons in the list -- which is now well defined because there's
419 ;; always at least one.
420
421 (let ((builder (cons nil initial)))
422 (setf (car builder) (last builder))
423 builder))
424
425(export 'lbuild-add)
426(defun lbuild-add (builder item)
427 "Add an ITEM to the end of a list BUILDER."
428 (let ((new (cons item nil)))
429 (setf (cdar builder) new
430 (car builder) new))
431 builder)
432
433(export 'lbuild-add-list)
434(defun lbuild-add-list (builder list)
435 "Add a LIST to the end of a list BUILDER. The LIST will be clobbered."
436 (when list
437 (setf (cdar builder) list
438 (car builder) (last list)))
439 builder)
440
441(export 'lbuild-list)
442(defun lbuild-list (builder)
443 "Return the constructed list."
444 (cdr builder))
445
446(export 'mappend)
447(defun mappend (function list &rest more-lists)
69dda0c9 448 "Like a nondestructive `mapcan'.
dea4d055
MW
449
450 Map FUNCTION over the the corresponding elements of LIST and MORE-LISTS,
451 and return the result of appending all of the resulting lists."
452 (reduce #'append (apply #'mapcar function list more-lists) :from-end t))
453
17c7c784
MW
454(export 'distinguished-point-shortest-paths)
455(defun distinguished-point-shortest-paths (root neighbours-func)
456 "Moderately efficient shortest-paths-from-root computation.
457
458 The ROOT is a distinguished vertex in a graph. The NEIGHBOURS-FUNC
459 accepts a VERTEX as its only argument, and returns a list of conses (V .
460 C) for each of the VERTEX's neighbours, indicating that there is an edge
461 from VERTEX to V, with cost C.
462
463 The return value is a list of entries (COST . REV-PATH) for each vertex
464 reachable from the ROOT; the COST is the total cost of the shortest path,
465 and REV-PATH is the path from the ROOT, in reverse order -- so the first
466 element is the vertex itself and the last element is the ROOT.
467
468 The NEIGHBOURS-FUNC is called at most N times, and may take O(N) time to
469 produce its output list. The computation as a whole takes O(N^2) time,
470 where N is the number of vertices in the graph, assuming there is at most
471 one edge between any pair of vertices."
472
473 ;; This is a listish version of Dijkstra's shortest-path algorithm. It
474 ;; could be made more efficient by using a fancy priority queue rather than
475 ;; a linear search for finding the nearest live element (see below), but it
476 ;; still runs pretty well.
477
478 (let ((map (make-hash-table))
479 (dead nil)
480 (live (list (list 0 root))))
481 (setf (gethash root map) (cons :live (car live)))
482 (loop
483 ;; The dead list contains a record, in output format (COST . PATH), for
484 ;; each vertex whose shortest path has been finally decided. The live
485 ;; list contains a record for the vertices of current interest, also in
486 ;; output format; the COST for a live record shows the best cost for a
487 ;; path using only dead vertices.
488 ;;
489 ;; Each time through here, we pull an item off the live list and
490 ;; push it onto the dead list, so we do at most N iterations total.
491
492 ;; If there are no more live items, then we're done; the remaining
493 ;; vertices, if any, are unreachable from the ROOT.
494 (when (null live) (return))
495
496 ;; Find the closest live vertex to the root. The linear scan through
497 ;; the live list costs at most N time.
498 (let* ((best (reduce (lambda (x y) (if (< (car x) (car y)) x y)) live))
499 (best-cost (car best))
500 (best-path (cdr best))
501 (best-vertex (car best-path)))
502
503 ;; Remove the chosen vertex from the LIVE list, and add the
504 ;; appropriate record to the dead list. We must have the shortest
505 ;; path to this vertex now: we have the shortest path using currently
506 ;; dead vertices; any other path must use at least one live vertex,
507 ;; and, by construction, the path through any such vertex must be
508 ;; further than the path we already have.
509 ;;
510 ;; Removal from the live list uses a linear scan which costs N time.
511 (setf live (delete best live))
512 (push best dead)
513 (setf (car (gethash best-vertex map)) :dead)
514
515 ;; Work through the chosen vertex's neighbours, adding each of them
516 ;; to the live list if they're not already there. If a neighbour is
517 ;; already live, and we find a shorter path to it through our chosen
518 ;; vertex, then update the neighbour's record.
519 ;;
520 ;; The chosen vertex obviously has at most N neighbours. There's no
521 ;; more looping in here, so performance is as claimed.
522 (dolist (neigh (funcall neighbours-func best-vertex))
523 (let* ((neigh-vertex (car neigh))
524 (neigh-cost (+ best-cost (cdr neigh)))
525 (neigh-record (gethash neigh-vertex map)))
526 (cond ((null neigh-record)
527 ;; If the neighbour isn't known, then now's the time to
528 ;; make a fresh live record for it.
529 (let ((new-record (list* :live neigh-cost
530 neigh-vertex best-path)))
531 (push (cdr new-record) live)
532 (setf (gethash neigh-vertex map) new-record)))
533 ((and (eq (car neigh-record) :live)
534 (< neigh-cost (cadr neigh-record)))
535 ;; If the neighbour is live, and we've found a better path
536 ;; to it, then update its record.
537 (setf (cadr neigh-record) neigh-cost
538 (cdddr neigh-record) best-path)))))))
539 dead))
540
38b78e87
MW
541(export '(inconsistent-merge-error
542 merge-error-candidates merge-error-present-function))
dea4d055
MW
543(define-condition inconsistent-merge-error (error)
544 ((candidates :initarg :candidates
38b78e87
MW
545 :reader merge-error-candidates)
546 (present :initarg :present :initform #'identity
547 :reader merge-error-present-function))
dea4d055 548 (:documentation
9fb4a980 549 "Reports an inconsistency in the arguments passed to `merge-lists'.")
dea4d055 550 (:report (lambda (condition stream)
e2838dc5
MW
551 (format stream "Merge inconsistency: failed to decide between ~
552 ~{~#[~;~A~;~A and ~A~:;~
553 ~@{~A, ~#[~;and ~A~]~}~]~}"
38b78e87
MW
554 (mapcar (merge-error-present-function condition)
555 (merge-error-candidates condition))))))
dea4d055
MW
556
557(export 'merge-lists)
e2838dc5 558(defun merge-lists (lists &key pick (test #'eql) (present #'identity))
dea4d055
MW
559 "Return a merge of the given LISTS.
560
e8c5a09e 561 The resulting list contains the items of the given LISTS, with duplicates
dea4d055
MW
562 removed. The order of the resulting list is consistent with the orders of
563 the input LISTS in the sense that if A precedes B in some input list then
564 A will also precede B in the output list. If the lists aren't consistent
565 (e.g., some list contains A followed by B, and another contains B followed
e2838dc5
MW
566 by A) then an error of type `inconsistent-merge-error' is signalled. The
567 offending items are filtered for presentation through the PRESENT function
568 before being attached to the condition, so as to produce a more useful
569 diagnostic message.
dea4d055
MW
570
571 Item equality is determined by TEST.
572
573 If there is an ambiguity at any point -- i.e., a choice between two or
574 more possible next items to emit -- then PICK is called to arbitrate.
575 PICK is called with two arguments: the list of candidate next items, and
e8c5a09e
MW
576 the current output list. It should return one of the candidate items.
577 The order of the candidates in the list given to the PICK function
578 reflects their order in the input LISTS: item A will precede item B in the
579 candidates list if and only if an occurrence of A appears in an earlier
580 input list than any occurrence of item B. (This completely determines the
581 order of the candidates: it is not possible that two candidates appear in
c5ef873a
MW
582 the same input list, since that would resolve the ambiguity between them.)
583 If PICK is omitted then the item chosen is the one appearing in the
584 earliest of the input lists: i.e., effectively, the default PICK function
585 is
e8c5a09e
MW
586
587 (lambda (candidates output-so-far)
588 (declare (ignore output-so-far))
589 (car candidates))
dea4d055
MW
590
591 The primary use of this function is in computing class precedence lists.
592 By building the input lists and selecting the PICK function appropriately,
593 a variety of different CPL algorithms can be implemented."
594
022a3499
MW
595 (do ((lb (make-list-builder)))
596 ((null lists) (lbuild-list lb))
dea4d055
MW
597
598 ;; The candidate items are the ones at the front of the input lists.
599 ;; Gather them up, removing duplicates. If a candidate is somewhere in
600 ;; one of the other lists other than at the front then we reject it. If
601 ;; we've just rejected everything, then we can make no more progress and
602 ;; the input lists were inconsistent.
e8c5a09e
MW
603 (let* ((candidates (delete-duplicates (mapcar #'car lists)
604 :test test :from-end t))
dea4d055
MW
605 (leasts (remove-if (lambda (item)
606 (some (lambda (list)
607 (member item (cdr list) :test test))
608 lists))
609 candidates))
610 (winner (cond ((null leasts)
611 (error 'inconsistent-merge-error
38b78e87
MW
612 :candidates candidates
613 :present present))
dea4d055
MW
614 ((null (cdr leasts))
615 (car leasts))
616 (pick
617 (funcall pick leasts (lbuild-list lb)))
618 (t (car leasts)))))
619
620 ;; Check that the PICK function isn't conning us.
621 (assert (member winner leasts :test test))
622
623 ;; Update the output list and remove the winning item from the input
624 ;; lists. We know that it must be at the front of each input list
625 ;; containing it. At this point, we discard input lists entirely when
626 ;; they run out of entries. The loop ends when there are no more input
627 ;; lists left, i.e., when we've munched all of the input items.
628 (lbuild-add lb winner)
629 (setf lists (delete nil (mapcar (lambda (list)
630 (if (funcall test winner (car list))
631 (cdr list)
632 list))
633 lists))))))
634
635(export 'categorize)
636(defmacro categorize ((itemvar items &key bind) categories &body body)
637 "Categorize ITEMS into lists and invoke BODY.
638
639 The ITEMVAR is a symbol; as the macro iterates over the ITEMS, ITEMVAR
640 will contain the current item. The BIND argument is a list of LET*-like
641 clauses. The CATEGORIES are a list of clauses of the form (SYMBOL
642 PREDICATE).
643
644 The behaviour of the macro is as follows. ITEMVAR is assigned (not
645 bound), in turn, each item in the list ITEMS. The PREDICATEs in the
646 CATEGORIES list are evaluated in turn, in an environment containing
647 ITEMVAR and the BINDings, until one of them evaluates to a non-nil value.
648 At this point, the item is assigned to the category named by the
649 corresponding SYMBOL. If none of the PREDICATEs returns non-nil then an
650 error is signalled; a PREDICATE consisting only of T will (of course)
651 match anything; it is detected specially so as to avoid compiler warnings.
652
653 Once all of the ITEMS have been categorized in this fashion, the BODY is
654 evaluated as an implicit PROGN. For each SYMBOL naming a category, a
655 variable named after that symbol will be bound in the BODY's environment
656 to a list of the items in that category, in the same order in which they
657 were found in the list ITEMS. The final values of the macro are the final
658 values of the BODY."
659
660 (let* ((cat-names (mapcar #'car categories))
661 (cat-match-forms (mapcar #'cadr categories))
662 (cat-vars (mapcar (lambda (name) (gensym (concatenate 'string
663 (symbol-name name) "-")))
664 cat-names))
665 (items-var (gensym "ITEMS-")))
64a7e651
MW
666 `(let (,@(mapcar (lambda (cat-var) (list cat-var nil)) cat-vars))
667 (let ((,items-var ,items))
668 (dolist (,itemvar ,items-var)
669 (let* ,bind
670 (cond ,@(mapcar (lambda (cat-match-form cat-var)
671 `(,cat-match-form
672 (push ,itemvar ,cat-var)))
673 cat-match-forms cat-vars)
674 ,@(and (not (member t cat-match-forms))
675 `((t (error "Failed to categorize ~A"
676 ,itemvar))))))))
dea4d055
MW
677 (let ,(mapcar (lambda (name var)
678 `(,name (nreverse ,var)))
679 cat-names cat-vars)
680 ,@body))))
681
42291726
MW
682(export 'partial-order-minima)
683(defun partial-order-minima (items order)
684 "Return a list of minimal items according to the non-strict partial ORDER.
685
686 The ORDER function describes the partial order: (funcall ORDER X Y) should
687 return true if X precedes or is equal to Y in the order."
688 (reduce (lambda (tops this)
689 (let ((new nil) (keep t))
690 (dolist (top tops)
691 (cond ((funcall order top this)
692 (setf keep nil)
693 (push top new))
694 ((not (funcall order this top))
695 (push top new))))
696 (nreverse (if keep (cons this new) new))))
697 items
698 :initial-value nil))
699
64cbfb65
MW
700(export 'find-duplicates)
701(defun find-duplicates (report sequence &key (key #'identity) (test #'eql))
702 "Call REPORT on each pair of duplicate items in SEQUENCE.
703
704 Duplicates are determined according to the KEY and TEST funcitons."
705 (when (symbolp test) (setf test (symbol-function test)))
706 (cond ((zerop (length sequence)) nil)
707 ((or (eq test #'eq)
708 (eq test #'eql)
709 (eq test #'equal)
710 (eq test #'equalp))
711 (let ((seen (make-hash-table :test test)))
712 (map nil (lambda (item)
713 (let ((k (funcall key item)))
714 (multiple-value-bind (previous matchp)
715 (gethash k seen)
716 (if matchp (funcall report item previous)
717 (setf (gethash k seen) item)))))
718 sequence)))
719 ((listp sequence)
9a3cb461
MW
720 (do ((tail sequence (cdr tail))
721 (i 0 (1+ i)))
722 ((endp tail))
723 (let* ((item (car tail))
724 (match (find (funcall key item) sequence
725 :test test :key key :end i)))
726 (when match (funcall report item match)))))
64cbfb65
MW
727 ((vectorp sequence)
728 (dotimes (i (length sequence))
729 (let* ((item (aref sequence i))
730 (pos (position (funcall key item) sequence
9a3cb461 731 :key key :test test :end i)))
64cbfb65
MW
732 (when pos (funcall report item (aref sequence pos))))))
733 (t
734 (error 'type-error :datum sequence :expected-type 'sequence))))
735
dea4d055
MW
736;;;--------------------------------------------------------------------------
737;;; Strings and characters.
738
739(export 'frob-identifier)
740(defun frob-identifier (string &key (swap-case t) (swap-hyphen t))
741 "Twiddles the case of STRING.
742
743 If all the letters in STRING are uppercase, and SWAP-CASE is true, then
744 switch them to lowercase; if they're all lowercase then switch them to
745 uppercase. If there's a mix then leave them all alone. At the same time,
746 if there are underscores but no hyphens, and SWAP-HYPHEN is true, then
747 switch them to hyphens, if there are hyphens and no underscores, switch
748 them underscores, and if there are both then leave them alone.
749
750 This is an invertible transformation, which turns vaguely plausible Lisp
751 names into vaguely plausible C names and vice versa. Lisp names with
752 `funny characters' like stars and percent signs won't be any use, of
753 course."
754
755 ;; Work out what kind of a job we've got to do. Gather flags: bit 0 means
756 ;; there are upper-case letters; bit 1 means there are lower-case letters;
757 ;; bit 2 means there are hyphens; bit 3 means there are underscores.
758 ;;
759 ;; Consequently, (logxor flags (ash flags 1)) is interesting: bit 1 is set
760 ;; if we have to frob case; bit 3 is set if we have to swap hyphens and
761 ;; underscores. So use this to select functions which do bits of the
762 ;; mapping, and then compose them together.
763 (let* ((flags (reduce (lambda (state ch)
764 (logior state
765 (cond ((upper-case-p ch) 1)
766 ((lower-case-p ch) 2)
767 ((char= ch #\-) 4)
768 ((char= ch #\_) 8)
769 (t 0))))
770 string
771 :initial-value 0))
772 (mask (logxor flags (ash flags 1)))
773 (letter (cond ((or (not swap-case) (not (logbitp 1 mask)))
774 (constantly nil))
775 ((logbitp 0 flags)
776 (lambda (ch)
777 (and (alpha-char-p ch) (char-downcase ch))))
778 (t
779 (lambda (ch)
780 (and (alpha-char-p ch) (char-upcase ch))))))
781 (uscore-hyphen (cond ((or (not (logbitp 3 mask)) (not swap-hyphen))
782 (constantly nil))
783 ((logbitp 2 flags)
784 (lambda (ch) (and (char= ch #\-) #\_)))
785 (t
786 (lambda (ch) (and (char= ch #\_) #\-))))))
787
788 (if (logbitp 3 (logior mask (ash mask 2)))
789 (map 'string (lambda (ch)
790 (or (funcall letter ch)
791 (funcall uscore-hyphen ch)
792 ch))
793 string)
794 string)))
795
796(export 'whitespace-char-p)
797(declaim (inline whitespace-char-p))
798(defun whitespace-char-p (char)
799 "Returns whether CHAR is a whitespace character.
800
801 Whitespaceness is determined relative to the compile-time readtable, which
802 is probably good enough for most purposes."
803 (case char
804 (#.(loop for i below char-code-limit
805 for ch = (code-char i)
806 unless (with-input-from-string (in (string ch))
807 (peek-char t in nil))
808 collect ch) t)
809 (t nil)))
810
811(export 'update-position)
812(declaim (inline update-position))
813(defun update-position (char line column)
814 "Updates LINE and COLUMN appropriately for having read the character CHAR.
815
816 Returns the new LINE and COLUMN numbers."
817 (case char
818 ((#\newline #\vt #\page)
819 (values (1+ line) 0))
820 ((#\tab)
821 (values line (logandc2 (+ column 8) 7)))
822 (t
823 (values line (1+ column)))))
824
825(export 'backtrack-position)
826(declaim (inline backtrack-position))
827(defun backtrack-position (char line column)
828 "Updates LINE and COLUMN appropriately for having unread CHAR.
829
830 Well, actually an approximation for it; it will likely be wrong if the
831 last character was a tab. But when the character is read again, it will
832 be correct."
833
834 ;; This isn't perfect: if the character doesn't actually match what was
835 ;; really read then it might not actually be possible: for example, if we
836 ;; push back a newline while in the middle of a line, or a tab while not at
837 ;; a tab stop. In that case, we'll just lose, but hopefully not too badly.
838 (case char
839
840 ;; In the absence of better ideas, I'll set the column number to zero.
841 ;; This is almost certainly wrong, but with a little luck nobody will ask
842 ;; and it'll be all right soon.
843 ((#\newline #\vt #\page) (values (1- line) 0))
844
845 ;; Winding back a single space is sufficient. If the position is
846 ;; currently on a tab stop then it'll advance back here next time. If
847 ;; not, we're going to lose anyway because the previous character
848 ;; certainly couldn't have been a tab.
849 (#\tab (values line (1- column)))
850
851 ;; Anything else: just decrement the column and cross fingers.
852 (t (values line (1- column)))))
853
854;;;--------------------------------------------------------------------------
855;;; Functions.
856
857(export 'compose)
b0d4e74f 858(defun compose (&rest functions)
dea4d055
MW
859 "Composition of functions. Functions are applied left-to-right.
860
861 This is the reverse order of the usual mathematical notation, but I find
bf090e02
MW
862 it easier to read. It's also slightly easier to work with in programs.
863 That is, (compose F1 F2 ... Fn) is what a category theorist might write as
864 F1 ; F2 ; ... ; Fn, rather than F1 o F2 o ... o Fn."
865
dea4d055
MW
866 (labels ((compose1 (func-a func-b)
867 (lambda (&rest args)
868 (multiple-value-call func-b (apply func-a args)))))
b0d4e74f
MW
869 (if (null functions) #'values
870 (reduce #'compose1 (cdr functions)
871 :initial-value (car functions)))))
dea4d055 872
c34b237d
MW
873;;;--------------------------------------------------------------------------
874;;; Variables.
875
876(export 'defvar-unbound)
877(defmacro defvar-unbound (var doc)
878 "Make VAR a special variable with documentation DOC, but leave it unbound."
879 `(eval-when (:compile-toplevel :load-toplevel :execute)
880 (defvar ,var)
881 (setf (documentation ',var 'variable) ',doc)
882 ',var))
883
dea4d055
MW
884;;;--------------------------------------------------------------------------
885;;; Symbols.
886
887(export 'symbolicate)
888(defun symbolicate (&rest symbols)
889 "Return a symbol named after the concatenation of the names of the SYMBOLS.
890
3109662a 891 The symbol is interned in the current `*package*'. Trad."
dea4d055
MW
892 (intern (apply #'concatenate 'string (mapcar #'symbol-name symbols))))
893
894;;;--------------------------------------------------------------------------
895;;; Object printing.
896
897(export 'maybe-print-unreadable-object)
898(defmacro maybe-print-unreadable-object
899 ((object stream &rest args) &body body)
900 "Print helper for usually-unreadable objects.
901
3109662a 902 If `*print-escape*' is set then print OBJECT unreadably using BODY.
dea4d055
MW
903 Otherwise just print using BODY."
904 (with-gensyms (print)
905 `(flet ((,print () ,@body))
906 (if *print-escape*
907 (print-unreadable-object (,object ,stream ,@args)
908 (,print))
909 (,print)))))
910
08b6e064
MW
911(export 'print-ugly-stuff)
912(defun print-ugly-stuff (stream func)
913 "Print not-pretty things to the stream underlying STREAM.
914
915 The Lisp pretty-printing machinery, notably `pprint-logical-block', may
916 interpose additional streams between its body and the original target
917 stream. This makes it difficult to make use of the underlying stream's
918 special features, whatever they might be."
919
920 ;; This is unpleasant. Hacky hacky.
921 #.(or #+sbcl '(if (typep stream 'sb-pretty:pretty-stream)
922 (let ((target (sb-pretty::pretty-stream-target stream)))
923 (pprint-newline :mandatory stream)
924 (funcall func target))
925 (funcall func stream))
926 #+cmu '(if (typep stream 'pp:pretty-stream)
927 (let ((target (pp::pretty-stream-target stream)))
928 (pprint-newline :mandatory stream)
929 (funcall func target))
930 (funcall func stream))
931 '(funcall func stream)))
932
dea4d055
MW
933;;;--------------------------------------------------------------------------
934;;; Iteration macros.
935
936(export 'dosequence)
937(defmacro dosequence ((var seq &key (start 0) (end nil) indexvar)
938 &body body
939 &environment env)
940 "Macro for iterating over general sequences.
941
942 Iterates over a (sub)sequence SEQ, delimited by START and END (which are
943 evaluated). For each item of SEQ, BODY is invoked with VAR bound to the
944 item, and INDEXVAR (if requested) bound to the item's index. (Note that
945 this is different from most iteration constructs in Common Lisp, which
946 work by mutating the variable.)
947
948 The loop is surrounded by an anonymous BLOCK and the loop body forms an
949 implicit TAGBODY, as is usual. There is no result-form, however."
950
bacaaec3
MW
951 (once-only (:environment env start end)
952 (with-gensyms ((seqvar "SEQ-") (ivar "INDEX-")
953 (endvar "END-") (bodyfunc "BODY-"))
b8c698ee
MW
954 (multiple-value-bind (docs decls body) (parse-body body :docp nil)
955 (declare (ignore docs))
956
957 (flet ((loopguts (indexp listp endvar)
958 ;; Build a DO-loop to do what we want.
959 (let* ((do-vars nil)
960 (end-condition (if endvar
961 `(>= ,ivar ,endvar)
bacaaec3 962 `(endp ,seqvar)))
b8c698ee 963 (item (if listp
bacaaec3
MW
964 `(car ,seqvar)
965 `(aref ,seqvar ,ivar)))
b8c698ee
MW
966 (body-call `(,bodyfunc ,item)))
967 (when listp
bacaaec3 968 (push `(,seqvar (nthcdr ,start ,seqvar) (cdr ,seqvar))
b8c698ee
MW
969 do-vars))
970 (when indexp
971 (push `(,ivar ,start (1+ ,ivar)) do-vars))
972 (when indexvar
973 (setf body-call (append body-call (list ivar))))
974 `(do ,do-vars (,end-condition) ,body-call))))
975
976 `(block nil
bacaaec3
MW
977 (let ((,seqvar ,seq))
978 (flet ((,bodyfunc (,var ,@(and indexvar `(,indexvar)))
979 ,@decls
980 (tagbody ,@body)))
981 (etypecase ,seqvar
982 (vector
983 (let ((,endvar (or ,end (length ,seqvar))))
984 ,(loopguts t nil endvar)))
985 (list
986 (if ,end
987 ,(loopguts t t end)
988 ,(loopguts indexvar t nil))))))))))))
dea4d055 989
4b8e5c03
MW
990;;;--------------------------------------------------------------------------
991;;; Structure accessor hacks.
992
993(export 'define-access-wrapper)
994(defmacro define-access-wrapper (from to &key read-only)
995 "Make (FROM THING) work like (TO THING).
996
997 If not READ-ONLY, then also make (setf (FROM THING) VALUE) work like
998 (setf (TO THING) VALUE).
999
1000 This is mostly useful for structure slot accessors where the slot has to
1001 be given an unpleasant name to avoid it being an external symbol."
1002 `(progn
1003 (declaim (inline ,from ,@(and (not read-only) `((setf ,from)))))
1004 (defun ,from (object)
1005 (,to object))
1006 ,@(and (not read-only)
1007 `((defun (setf ,from) (value object)
1008 (setf (,to object) value))))))
1009
db6c3279
MW
1010;;;--------------------------------------------------------------------------
1011;;; Condition and error utilities.
1012
1013(export 'designated-condition)
1014(defun designated-condition (default-type datum arguments
1015 &key allow-pointless-arguments)
1016 "Return the condition designated by DATUM and ARGUMENTS.
1017
1018 DATUM and ARGUMENTS together are a `condition designator' of (some
1019 supertype of) DEFAULT-TYPE; return the condition so designated."
1020 (typecase datum
1021 (condition
1022 (unless (or allow-pointless-arguments (null arguments))
1023 (error "Argument list provided with specific condition"))
1024 datum)
1025 (symbol
1026 (apply #'make-condition datum arguments))
1027 ((or string function)
1028 (make-condition default-type
1029 :format-control datum
1030 :format-arguments arguments))
1031 (t
1032 (error "Unexpected condition designator datum ~S" datum))))
1033
f7b60deb
MW
1034(export 'simple-control-error)
1035(define-condition simple-control-error (control-error simple-error)
1036 ())
1037
1038(export 'invoke-associated-restart)
1039(defun invoke-associated-restart (restart condition &rest arguments)
1040 "Invoke the active RESTART associated with CONDITION, with the ARGUMENTS.
1041
1042 Find an active restart designated by RESTART; if CONDITION is not nil,
1043 then restrict the search to restarts associated with CONDITION, and
1044 restarts not associated with any condition. If no such restart is found
1045 then signal an error of type `control-error'; otherwise invoke the restart
1046 with the given ARGUMENTS."
1047 (apply #'invoke-restart
1048 (or (find-restart restart condition)
1049 (error 'simple-control-error
1050 :format-control "~:[Restart ~S is not active~;~
1051 No active `~(~A~)' restart~]~
1052 ~@[ for condition ~S~]"
1053 :format-arguments (list (symbolp restart)
1054 restart
1055 condition)))
1056 arguments))
1057
c884ec24
MW
1058(export '(enclosing-condition enclosed-condition))
1059(define-condition enclosing-condition (condition)
1060 ((%enclosed-condition :initarg :condition :type condition
1061 :reader enclosed-condition))
1062 (:documentation
1063 "A condition which encloses another condition
1064
1065 This is useful if one wants to attach additional information to an
1066 existing condition. The enclosed condition can be obtained using the
1067 `enclosed-condition' function.")
1068 (:report (lambda (condition stream)
1069 (princ (enclosed-condition condition) stream))))
1070
1071(export 'information)
1072(define-condition information (condition)
1073 ())
1074
1075(export 'simple-information)
1076(define-condition simple-information (simple-condition information)
1077 ())
1078
1079(export 'info)
1080(defun info (datum &rest arguments)
1081 "Report some useful diagnostic information.
1082
1083 Establish a simple restart named `noted', and signal the condition of type
1084 `information' designated by DATUM and ARGUMENTS. Return non-nil if the
1085 restart was invoked, otherwise nil."
1086 (restart-case
1087 (signal (designated-condition 'simple-information datum arguments))
1088 (noted () :report "Noted." t)))
1089
1090(export 'noted)
1091(defun noted (&optional condition)
1092 "Invoke the `noted' restart, possibly associated with the given CONDITION."
1093 (invoke-associated-restart 'noted condition))
1094
1095(export 'promiscuous-cerror)
1096(defun promiscuous-cerror (continue-string datum &rest arguments)
1097 "Like standard `cerror', but robust against sneaky changes of conditions.
1098
1099 It seems that `cerror' (well, at least the version in SBCL) is careful
1100 to limit its restart to the specific condition it signalled. But that's
1101 annoying, because `sod-parser:with-default-error-location' substitutes
1102 different conditions carrying the error-location information."
1103 (restart-case (apply #'error datum arguments)
1104 (continue ()
1105 :report (lambda (stream)
1106 (apply #'format stream continue-string datum arguments))
1107 nil)))
1108
1109(export 'cerror*)
1110(defun cerror* (datum &rest arguments)
1111 (apply #'promiscuous-cerror "Continue" datum arguments))
1112
dea4d055
MW
1113;;;--------------------------------------------------------------------------
1114;;; CLOS hacking.
1115
1116(export 'default-slot)
1117(defmacro default-slot ((instance slot &optional (slot-names t))
1118 &body value
1119 &environment env)
1120 "If INSTANCE's slot named SLOT is unbound, set it to VALUE.
1121
1122 Only set SLOT if it's listed in SLOT-NAMES, or SLOT-NAMES is `t' (i.e., we
1123 obey the `shared-initialize' protocol). SLOT-NAMES defaults to `t', so
1124 you can use it in `initialize-instance' or similar without ill effects.
1125 Both INSTANCE and SLOT are evaluated; VALUE is an implicit progn and only
1126 evaluated if it's needed."
1127
1128 (once-only (:environment env instance slot slot-names)
1129 `(when ,(if (eq slot-names t)
1130 `(not (slot-boundp ,instance ,slot))
1131 `(and (not (slot-boundp ,instance ,slot))
1132 (or (eq ,slot-names t)
1133 (member ,slot ,slot-names))))
1134 (setf (slot-value ,instance ,slot)
1135 (progn ,@value)))))
1136
141283ff
MW
1137(export 'define-on-demand-slot)
1138(defmacro define-on-demand-slot (class slot (instance) &body body)
1139 "Defines a slot which computes its initial value on demand.
1140
1141 Sets up the named SLOT of CLASS to establish its value as the implicit
1142 progn BODY, by defining an appropriate method on `slot-unbound'."
b8c698ee
MW
1143 (multiple-value-bind (docs decls body) (parse-body body)
1144 (with-gensyms (classvar slotvar)
1145 `(defmethod slot-unbound
1146 (,classvar (,instance ,class) (,slotvar (eql ',slot)))
1147 ,@docs ,@decls
1148 (declare (ignore ,classvar))
fc09e191 1149 (setf (slot-value ,instance ',slot) (block ,slot ,@body))))))
141283ff 1150
dea4d055 1151;;;----- That's all, folks --------------------------------------------------