Debian Codegraph @

comlib/let-over-lambda.lisp

raw
1 ;;;; -*- Mode: LISP; Syntax: COMMON-LISP; Package: LET-OVER-LAMBDA; Base: 10 -*- file: let-over-lambda.lisp
2
3 ;; (in-package #:let-over-lambda)
4 (in-package :chaos)
5
6 ;; Antiweb (C) Doug Hoyte
7
8 ;; This is a "production" version of LOL with bug-fixes
9 ;; and new features in the spirit of the book.
10
11 ;; See http://letoverlambda.com
12
13 ;; This is the source code for the book
14 ;; _Let_Over_Lambda_ by Doug Hoyte.
15 ;; This code is (C) 2002-2008, Doug Hoyte.
16 ;;
17 ;; You are free to use, modify, and re-distribute
18 ;; this code however you want, except that any
19 ;; modifications must be clearly indicated before
20 ;; re-distribution. There is no warranty,
21 ;; expressed nor implied.
22 ;;
23 ;; Attribution of this code to me, Doug Hoyte, is
24 ;; appreciated but not necessary. If you find the
25 ;; code useful, or would like documentation,
26 ;; please consider buying the book!
27
28 ;; Safety feature for SBCL>=v1.2.2
29 #+sbcl
30 (if (string-lessp (lisp-implementation-version) "1.2.2")
31 (pushnew :safe-sbcl *features*)
32 (setq *features* (remove :safe-sbcl *features*)))
33
34 (defun group (source n)
35 (if (zerop n) (error "zero length"))
36 (labels ((rec (source acc)
37 (let ((rest (nthcdr n source)))
38 (if (consp rest)
39 (rec rest (cons
40 (subseq source 0 n)
41 acc))
42 (nreverse
43 (cons source acc))))))
44 (if source (rec source nil) nil)))
45
46 (eval-when (:compile-toplevel :execute :load-toplevel)
47 (defun mkstr (&rest args)
48 (with-output-to-string (s)
49 (dolist (a args) (princ a s))))
50
51 (defun symb (&rest args)
52 (values (intern (apply #'mkstr args))))
53
54 (defun flatten (x)
55 (labels ((rec (x acc)
56 (cond ((null x) acc)
57 #+(and sbcl (not safe-sbcl))
58 ((typep x 'sb-impl::comma) (rec (sb-impl::comma-expr x) acc))
59 ((atom x) (cons x acc))
60 (t (rec
61 (car x)
62 (rec (cdr x) acc))))))
63 (rec x nil)))
64
65 (defun g!-symbol-p (s)
66 (and (symbolp s)
67 (> (length (symbol-name s)) 2)
68 (string= (symbol-name s)
69 "G!"
70 :start1 0
71 :end1 2)))
72
73 (defun o!-symbol-p (s)
74 (and (symbolp s)
75 (> (length (symbol-name s)) 2)
76 (string= (symbol-name s)
77 "O!"
78 :start1 0
79 :end1 2)))
80
81 (defun o!-symbol-to-g!-symbol (s)
82 (symb "G!"
83 (subseq (symbol-name s) 2))))
84
85 (defmacro defmacro/g! (name args &rest body)
86 (let ((syms (remove-duplicates
87 (remove-if-not #'g!-symbol-p
88 (flatten body)))))
89 `(defmacro ,name ,args
90 (let ,(mapcar
91 (lambda (s)
92 `(,s (gensym ,(subseq
93 (symbol-name s)
94 2))))
95 syms)
96 ,@body))))
97
98 (defmacro defmacro! (name args &rest body)
99 (let* ((os (remove-if-not #'o!-symbol-p args))
100 (gs (mapcar #'o!-symbol-to-g!-symbol os)))
101 `(defmacro/g! ,name ,args
102 `(let ,(mapcar #'list (list ,@gs) (list ,@os))
103 ,(progn ,@body)))))
104
105 ;; Nestable suggestion from Daniel Herring
106 (defun |#"-reader| (stream sub-char numarg)
107 (declare (ignore sub-char numarg))
108 (let (chars (state 'normal) (depth 1))
109 (loop do
110 (let ((curr (read-char stream)))
111 (cond ((eq state 'normal)
112 (cond ((char= curr #\#)
113 (push #\# chars)
114 (setq state 'read-sharp))
115 ((char= curr #\")
116 (setq state 'read-quote))
117 (t
118 (push curr chars))))
119 ((eq state 'read-sharp)
120 (cond ((char= curr #\")
121 (push #\" chars)
122 (incf depth)
123 (setq state 'normal))
124 (t
125 (push curr chars)
126 (setq state 'normal))))
127 ((eq state 'read-quote)
128 (cond ((char= curr #\#)
129 (decf depth)
130 (if (zerop depth) (return))
131 (push #\" chars)
132 (push #\# chars)
133 (setq state 'normal))
134 (t
135 (push #\" chars)
136 (if (char= curr #\")
137 (setq state 'read-quote)
138 (progn
139 (push curr chars)
140 (setq state 'normal)))))))))
141 (coerce (nreverse chars) 'string)))
142
143 (set-dispatch-macro-character
144 #\# #\" #'|#"-reader|)
145
146 ; This version is from Martin Dirichs
147 (defun |#>-reader| (stream sub-char numarg)
148 (declare (ignore sub-char numarg))
149 (let (chars)
150 (do ((curr (read-char stream)
151 (read-char stream)))
152 ((char= #\newline curr))
153 (push curr chars))
154 (let ((pattern (nreverse chars))
155 output)
156 (labels ((match (pos chars)
157 (if (null chars)
158 pos
159 (if (char= (nth pos pattern) (car chars))
160 (match (1+ pos) (cdr chars))
161 (match 0 (cdr (append (subseq pattern 0 pos) chars)))))))
162 (do (curr
163 (pos 0))
164 ((= pos (length pattern)))
165 (setf curr (read-char stream)
166 pos (match pos (list curr)))
167 (push curr output))
168 (coerce
169 (nreverse
170 (nthcdr (length pattern) output))
171 'string)))))
172
173 (set-dispatch-macro-character
174 #\# #\> #'|#>-reader|)
175
176 (defun segment-reader (stream ch n)
177 (if (> n 0)
178 (let ((chars))
179 (do ((curr (read-char stream)
180 (read-char stream)))
181 ((char= ch curr))
182 (push curr chars))
183 (cons (coerce (nreverse chars) 'string)
184 (segment-reader stream ch (- n 1))))))
185
186 #+cl-ppcre
187 (defmacro! match-mode-ppcre-lambda-form (o!args o!mods)
188 ``(lambda (,',g!str)
189 (cl-ppcre:scan
190 ,(if (zerop (length ,g!mods))
191 (car ,g!args)
192 (format nil "(?~a)~a" ,g!mods (car ,g!args)))
193 ,',g!str)))
194
195 #+cl-ppcre
196 (defmacro! subst-mode-ppcre-lambda-form (o!args)
197 ``(lambda (,',g!str)
198 (cl-ppcre:regex-replace-all
199 ,(car ,g!args)
200 ,',g!str
201 ,(cadr ,g!args))))
202
203 #+cl-ppcre
204 (defun |#~-reader| (stream sub-char numarg)
205 (declare (ignore sub-char numarg))
206 (let ((mode-char (read-char stream)))
207 (cond
208 ((char= mode-char #\m)
209 (match-mode-ppcre-lambda-form
210 (segment-reader stream
211 (read-char stream)
212 1)
213 (coerce (loop for c = (read-char stream)
214 while (alpha-char-p c)
215 collect c
216 finally (unread-char c stream))
217 'string)))
218 ((char= mode-char #\s)
219 (subst-mode-ppcre-lambda-form
220 (segment-reader stream
221 (read-char stream)
222 2)))
223 (t (error "Unknown #~~ mode character")))))
224
225 #+cl-ppcre
226 (set-dispatch-macro-character #\# #\~ #'|#~-reader|)
227
228 (defmacro! dlambda (&rest ds)
229 `(lambda (&rest ,g!args)
230 (case (car ,g!args)
231 ,@(mapcar
232 (lambda (d)
233 `(,(if (eq t (car d))
234 t
235 (list (car d)))
236 (apply (lambda ,@(cdr d))
237 ,(if (eq t (car d))
238 g!args
239 `(cdr ,g!args)))))
240 ds))))
241
242 ;; Graham's alambda
243 (defmacro alambda (parms &body body)
244 `(labels ((self ,parms ,@body))
245 #'self))
246
247 ;; Graham's aif
248 (defmacro aif (test then &optional else)
249 `(let ((it ,test))
250 (if it ,then ,else)))
251
252 (eval-when (:compile-toplevel :execute :load-toplevel)
253 (defun |#`-reader| (stream sub-char numarg)
254 (declare (ignore sub-char))
255 (unless numarg (setq numarg 1))
256 `(lambda ,(loop for i from 1 to numarg
257 collect (symb 'a i))
258 ,(funcall
259 (get-macro-character #\`) stream nil)))
260
261 (set-dispatch-macro-character
262 #\# #\` #'|#`-reader|))
263
264 (defmacro alet% (letargs &rest body)
265 `(let ((this) ,@letargs)
266 (setq this ,@(last body))
267 ,@(butlast body)
268 this))
269
270 (defmacro alet (letargs &rest body)
271 `(let ((this) ,@letargs)
272 (setq this ,@(last body))
273 ,@(butlast body)
274 (lambda (&rest params)
275 (apply this params))))
276
277 (defun let-binding-transform (bs)
278 (if bs
279 (cons
280 (cond ((symbolp (car bs))
281 (list (car bs)))
282 ((consp (car bs))
283 (car bs))
284 (t
285 (error "Bad let bindings")))
286 (let-binding-transform (cdr bs)))))
287
288 (defmacro pandoriclet (letargs &rest body)
289 (let ((letargs (cons
290 '(this)
291 (let-binding-transform
292 letargs))))
293 `(let (,@letargs)
294 (setq this ,@(last body))
295 ,@(butlast body)
296 (dlambda
297 (:pandoric-get (sym)
298 ,(pandoriclet-get letargs))
299 (:pandoric-set (sym val)
300 ,(pandoriclet-set letargs))
301 (t (&rest args)
302 (apply this args))))))
303
304 (defun pandoriclet-get (letargs)
305 `(case sym
306 ,@(mapcar #`((,(car a1)) ,(car a1))
307 letargs)
308 (t (error
309 "Unknown pandoric get: ~a"
310 sym))))
311
312 (defun pandoriclet-set (letargs)
313 `(case sym
314 ,@(mapcar #`((,(car a1))
315 (setq ,(car a1) val))
316 letargs)
317 (t (error
318 "Unknown pandoric set: ~a"
319 sym))))
320
321 (declaim (inline get-pandoric))
322
323 (defun get-pandoric (box sym)
324 (funcall box :pandoric-get sym))
325
326 (defsetf get-pandoric (box sym) (val)
327 `(progn
328 (funcall ,box :pandoric-set ,sym ,val)
329 ,val))
330
331 (defmacro with-pandoric (syms box &rest body)
332 (let ((g!box (gensym "box")))
333 `(let ((,g!box ,box))
334 (declare (ignorable ,g!box))
335 (symbol-macrolet
336 (,@(mapcar #`(,a1 (get-pandoric ,g!box ',a1))
337 syms))
338 ,@body))))
339
340 (defun pandoric-hotpatch (box new)
341 (with-pandoric (this) box
342 (setq this new)))
343
344 (defmacro pandoric-recode (vars box new)
345 `(with-pandoric (this ,@vars) ,box
346 (setq this ,new)))
347
348 (defmacro plambda (largs pargs &rest body)
349 (let ((pargs (mapcar #'list pargs)))
350 `(let (this self)
351 (setq
352 this (lambda ,largs ,@body)
353 self (dlambda
354 (:pandoric-get (sym)
355 ,(pandoriclet-get pargs))
356 (:pandoric-set (sym val)
357 ,(pandoriclet-set pargs))
358 (t (&rest args)
359 (apply this args)))))))
360
361 (defvar pandoric-eval-tunnel)
362
363 (defmacro pandoric-eval (vars expr)
364 `(let ((pandoric-eval-tunnel
365 (plambda () ,vars t)))
366 (eval `(with-pandoric
367 ,',vars pandoric-eval-tunnel
368 ,,expr))))
369
370 ;; Chapter 7
371 (eval-when (:compile-toplevel :execute :load-toplevel)
372 (set-dispatch-macro-character #\# #\f
373 (lambda (stream sub-char numarg)
374 (declare (ignore stream sub-char))
375 (setq numarg (or numarg 3))
376 (unless (<= numarg 3)
377 (error "Bad value for #f: ~a" numarg))
378 `(declare (optimize (speed ,numarg)
379 (safety ,(- 3 numarg)))))))
380
381 (defmacro fast-progn (&rest body)
382 `(locally #f ,@body))
383
384 (defmacro safe-progn (&rest body)
385 `(locally #0f ,@body))
386
387 (defun fformat (&rest all)
388 (apply #'format all))
389
390 (define-compiler-macro fformat
391 (&whole form
392 stream fmt &rest args)
393 (if (constantp fmt)
394 (if stream
395 `(funcall (formatter ,fmt)
396 ,stream ,@args)
397 (let ((g!stream (gensym "stream")))
398 `(with-output-to-string (,g!stream)
399 (funcall (formatter ,fmt)
400 ,g!stream ,@args))))
401 form))
402
403 (declaim (inline make-tlist tlist-left
404 tlist-right tlist-empty-p))
405
406 (defun make-tlist () (cons nil nil))
407 (defun tlist-left (tl) (caar tl))
408 (defun tlist-right (tl) (cadr tl))
409 (defun tlist-empty-p (tl) (null (car tl)))
410
411 (declaim (inline tlist-add-left
412 tlist-add-right))
413
414 (defun tlist-add-left (tl it)
415 (let ((x (cons it (car tl))))
416 (if (tlist-empty-p tl)
417 (setf (cdr tl) x))
418 (setf (car tl) x)))
419
420 (defun tlist-add-right (tl it)
421 (let ((x (cons it nil)))
422 (if (tlist-empty-p tl)
423 (setf (car tl) x)
424 (setf (cddr tl) x))
425 (setf (cdr tl) x)))
426
427 (declaim (inline tlist-rem-left))
428
429 (defun tlist-rem-left (tl)
430 (if (tlist-empty-p tl)
431 (error "Remove from empty tlist")
432 (let ((x (car tl)))
433 (setf (car tl) (cdar tl))
434 (if (tlist-empty-p tl)
435 (setf (cdr tl) nil)) ;; For gc
436 (car x))))
437
438 (declaim (inline tlist-update))
439
440 (defun tlist-update (tl)
441 (setf (cdr tl) (last (car tl))))
442
443 (defun build-batcher-sn (n)
444 (let* (network
445 (tee (ceiling (log n 2)))
446 (p (ash 1 (- tee 1))))
447 (loop while (> p 0) do
448 (let ((q (ash 1 (- tee 1)))
449 (r 0)
450 (d p))
451 (loop while (> d 0) do
452 (loop for i from 0 to (- n d 1) do
453 (if (= (logand i p) r)
454 (push (list i (+ i d))
455 network)))
456 (setf d (- q p)
457 q (ash q -1)
458 r p)))
459 (setf p (ash p -1)))
460 (nreverse network)))
461
462 (defmacro! sortf (comparator &rest places)
463 (if places
464 `(tagbody
465 ,@(mapcar
466 #`(let ((,g!a #1=,(nth (car a1) places))
467 (,g!b #2=,(nth (cadr a1) places)))
468 (if (,comparator ,g!b ,g!a)
469 (setf #1# ,g!b
470 #2# ,g!a)))
471 (build-batcher-sn (length places))))))
472
473 ;;;;;; NEW CODE FOR ANTIWEB
474 #+cl-ppcre
475 (defun dollar-symbol-p (s)
476 (and (symbolp s)
477 (> (length (symbol-name s)) 1)
478 (string= (symbol-name s)
479 "$"
480 :start1 0
481 :end1 1)
482 (ignore-errors (parse-integer (subseq (symbol-name s) 1)))))
483
484 (defun prune-if-match-bodies-from-sub-lexical-scope (tree)
485 (if (consp tree)
486 (if (or (eq (car tree) 'if-match)
487 (eq (car tree) 'when-match))
488 (cddr tree)
489 (cons (prune-if-match-bodies-from-sub-lexical-scope (car tree))
490 (prune-if-match-bodies-from-sub-lexical-scope (cdr tree))))
491 tree))
492
493 ;; WARNING: Not %100 correct. Removes forms like (... if-match ...) from the
494 ;; sub-lexical scope even though this isn't an invocation of the macro.
495 #+cl-ppcre
496 (defmacro! if-match ((test str) conseq &optional altern)
497 (let ((dollars (remove-duplicates
498 (remove-if-not #'dollar-symbol-p
499 (flatten (prune-if-match-bodies-from-sub-lexical-scope conseq))))))
500 (let ((top (or (car (sort (mapcar #'dollar-symbol-p dollars) #'>)) 0)))
501 `(let ((,g!str ,str))
502 (multiple-value-bind (,g!s ,g!e ,g!ms ,g!me) (,test ,g!str)
503 (declare (ignorable ,g!e ,g!me))
504 (if ,g!s
505 (if (< (length ,g!ms) ,top)
506 (error "ifmatch: too few matches")
507 (let ,(mapcar #`(,(symb "$" a1) (subseq ,g!str (aref ,g!ms ,(1- a1))
508 (aref ,g!me ,(1- a1))))
509 (loop for i from 1 to top collect i))
510 ,conseq))
511 ,altern))))))
512
513 (defmacro when-match ((test str) conseq &rest more-conseq)
514 `(if-match (,test ,str)
515 (progn ,conseq ,@more-conseq)))
516
517 ;; EOF