This file is indexed.

/usr/share/common-lisp/source/metabang-bind/dev/bind.lisp is in cl-metabang-bind 20170124-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
;;;-*- Mode: Lisp; Package: bind -*-

#| simple-header

See the file COPYING for details

|#

(in-package #:metabang.bind) 

(defconstant +code-marker+ :XXX)
(defconstant +decl-marker+ :YYY)
    
(defgeneric binding-form-accepts-multiple-forms-p (binding-form)
  (:documentation "Returns true if a binding form can accept multiple forms
(e.g., :flet)"))

(defmethod binding-form-accepts-multiple-forms-p ((binding-form t))
  nil)

(defparameter *unused-declarations-behavior*
  :print-warning
  "Tells bind how to behave when it encounters an unused declaration.

The possible options are

* :print-warning (the current default) - print a warning about the problem 
   and signal a `bind-unused-declarations-condition`

* :warn - signal a `bind-unused-declarations-warning` warning

* :error - signal a `bind-unused-declarations-error` error")

(defparameter *bind-all-declarations*
  '(dynamic-extent ignore optimize ftype inline 
    special ignorable notinline type))

(defparameter *bind-non-var-declarations*
  '(optimize ftype inline notinline 
    #+allegro
    :explain))

(defparameter *bind-simple-var-declarations*
  (remove 'type
          (set-difference *bind-all-declarations* *bind-non-var-declarations*)))

(defparameter *bind-lambda-list-markers* 
  '(&key &body &rest &args &optional))

(define-condition simple-style-warning (style-warning simple-warning)
  ())

(defun simple-style-warning (message &rest args)
  (warn 'simple-style-warning :format-control message :format-arguments args))

(define-condition bind-missing-value-form-warning (simple-style-warning)
  ((variable-form :initform nil
		  :initarg :variable-form
		  :reader variable-form))
  (:report (lambda (c s)
	     (format s "Missing value form for ~s" (variable-form c)))))

(define-condition bind-too-many-value-forms-error (error)
  ((variable-form :initform nil
		  :initarg :variable-form
		  :reader variable-form)
   (value-form :initform nil
		  :initarg :value-form
		  :reader value-form))
  (:report (lambda (c s)
	     (format s "Two many value forms for ~s" (variable-form c)))))

(define-condition bind-error (error)
                  ((binding
		    :initform nil
		    :initarg :binding
		    :reader binding)))

(define-condition bind-keyword/optional-nil-with-default-error (bind-error)
                  ((bad-variable 
		    :initform nil
		    :initarg :bad-variable
		    :reader bad-variable))
  (:report (lambda (c s)
             (format s "Bad binding '~S' in '~A'; cannot use a default value for &key or &optional arguments."
                     (bad-variable c) (binding c)))))

(define-condition bind-unused-declarations-condition ()
  ((unused-declarations :initform (error "must supply unused-declarations")
			:initarg :unused-declarations
			:reader unused-declarations))
  (:report (lambda (c s)
	     (format s "Unused declarations in bind: ~{~s~^, ~}" (unused-declarations c)))))

(define-condition bind-unused-declarations-warning (bind-unused-declarations-condition
						   simple-style-warning)
  ())

(define-condition bind-unused-declarations-error (bind-unused-declarations-condition
						  error)
  ())

(defun binding-forms ()
  "Return a list of all binding-forms that bind supports in alphabetical order."
  (let* ((forms (get 'bind :binding-forms)))
    (sort (loop for form in forms collect (car form)) 'string-lessp)))

(defun binding-form-groups ()
  "Return a list of the available binding-forms grouped into their synonyms."
  (let ((binding-forms (get 'bind :binding-forms))
	(canonical-names
	 (sort
	  (delete-duplicates 
	   (mapcar #'second (get 'bind :binding-forms)))
	  #'string-lessp)))
    (loop for form in canonical-names collect
	 (cdr (assoc form binding-forms)))))

(defun binding-form-synonyms (name)
  "Return a list of synonyms for the binding-form `name`. 

For example

    > (binding-form-synonyms :accessors)
    (:accessors :writable-accessors)

"
  (let* ((forms (get 'bind :binding-forms))
	 (datum (assoc name forms)))
    (and datum
	 (rest datum))))

(defvar *all-declarations*)

(defmacro bind ((&rest bindings) &body body)
  "Bind is a replacement for let*, destructuring-bind, multiple-value-bind and more. 

An example is probably the best way to describe its syntax:

    \(bind \(\(a 2\)
           \(\(b &rest args &key \(c 2\) &allow-other-keys\) '\(:a :c 5 :d 10 :e 54\)\)
           \(\(:values d e\) \(truncate 4.5\)\)
           \(\(:structure xxx- slot1 slot2\) \(make-xxx\)\)
           \(\(:flet name \(arg1 arg2\)\) \(+ arg1 arg2\)\)\)
         \(list a b c d e args\)\)

Simple bindings are as in let*. Destructuring is done if the first item
in a binding is a list. Multiple value binding is done if the first item
in a binding is a list and the first item in the list is ':values'. Other
forms have their own syntax. For example, :structure first has the conc
name and then slot names whereas :flet has the function name and a list
of arguments and then the function body (in an implicit progn)."
  (let (declarations)
    (loop while (and (consp (car body)) (eq (caar body) 'declare)) do
          (push (first body) declarations)
          (setf body (rest body)))
    (if bindings
	(let ((*all-declarations* (bind-expand-declarations (nreverse declarations))))
	  (prog1
	      (bind-macro-helper bindings body)
	    (check-for-unused-variable-declarations *all-declarations*)))
        `(locally
             ,@declarations
           ,@body))))

(defun check-for-unused-variable-declarations (declarations)
  (when declarations
    (case *unused-declarations-behavior* 
      (:warn
       (warn 'bind-unused-declarations-warning :unused-declarations declarations))
      (:error
       (error 'bind-unused-declarations-error :unused-declarations declarations))
      (t
       (format *error-output* "~&;;; warning: unused declarations found in form: ~{~s~^, ~}."
	       declarations)
       (signal 'bind-unused-declarations-condition :unused-declarations declarations)))))

(defun bind-macro-helper (bindings body)
  (if bindings
      (let ((binding (first bindings))
	    (remaining-bindings (rest bindings))
	    variable-form value-form atomp binding-form)
	(if (consp binding)
	    (setf variable-form (first binding)
		  value-form (rest binding) ;; (second binding)
		  atomp (if (consp variable-form) nil (null value-form)))
	    (setf variable-form binding
		  atomp t))
	(unless (or atomp value-form)
	  (warn 'bind-missing-value-form-warning :variable-form variable-form))
	(setf binding-form (and (consp variable-form)
				(and (symbolp (first variable-form))
				     (eq (symbol-package (first variable-form))
					 (load-time-value (find-package :keyword)))
				     (first variable-form))))
	(when (and (consp value-form) 
		   (cdr value-form)
		   (or (null binding-form)
		       (not (binding-form-accepts-multiple-forms-p binding-form))))
	  (error 'bind-too-many-value-forms-error 
		:variable-form variable-form :value-form value-form))
	(let* ((body (bind-macro-helper remaining-bindings body))
	       (variables (if binding-form (rest variable-form) variable-form))
	       (decls (bind-filter-declarations variables)))
	  (multiple-value-bind (form double-indent)
	      (if binding-form
		  ;; e.g., (:values ...)
		  (bind-generate-bindings (first variable-form) (rest variable-form) value-form)
		  ;; e.g., #(a b c)
		  (bind-generate-bindings variable-form variable-form value-form))
	    (cond ((or (tree-find form +code-marker+)
		       (tree-find form +decl-marker+))
		   (setf form (subst body +code-marker+ form))
		   (setf form (subst decls +decl-marker+ form)))
		  (double-indent
		   `(,@(butlast form) (,@(first (last form)) ,@decls ,body)))
		  ((merge-binding-forms-p form body)
		   (destructuring-bind (head1 form1-bindings . form1-code)
		       form
		     (destructuring-bind (_ form2-bindings . form2-code)
			 body
		       (declare (ignore _))
		       `(,head1 (,@form1-bindings ,@form2-bindings)
				,@decls 
				,@form1-code
				,@form2-code))))
		  (t
		   `(,@form ,@decls ,body))))))
      `(progn ,@body)))

;;;;

(defun var-ignorable-p (var)
  (or (null var) 
      (and (symbolp var) (string= (symbol-name var) (symbol-name '_)))))

(defun mint-ignorable-variable ()
  (gensym (symbol-name '#:bind-ignore-)))

(defun bind-fix-nils (var-list)
  (let (vars ignores)
    (loop for v in var-list do
          (cond ((var-ignorable-p v)
		 (let ((ignore (mint-ignorable-variable)))
                     (push ignore vars)
                     (push ignore ignores)))
		(t (push v vars))))
    (values (nreverse vars) ignores)))

(defun bind-fix-nils-destructured (var-list)
  (let ((ignores nil))
    (flet ((maybe-handle-1 (x)
	     (if (var-ignorable-p x)
		 (let ((ignore (mint-ignorable-variable)))
		   (push ignore ignores)
		   ignore)
		 x)))
    (labels ((do-it (it key?)
	       (cond ((null it)
		      nil)
		     ((atom it)
		      (maybe-handle-1 it))
		     ((dotted-pair-p it)
		      (cons (do-it (car it) key?) (do-it (cdr it) key?)))
		     ((eq (first it) '&key)
		      (loop for x in it collect (do-it x t)))
		     (key?
		      it)
		     (t
		      (cons (do-it (car it) key?)
			    (do-it (cdr it) key?))))))
      (values (do-it var-list nil) ignores)))))

(defun dotted-pair-p (putative-pair)
  "Returns true if and only if `putative-pair` is a dotted-list. I.e., if `putative-pair` is a cons cell with a non-nil cdr."
  (and (consp putative-pair)
       (cdr putative-pair)
       (not (consp (cdr putative-pair)))))

(defmethod bind-collect-variables (kind variable-form)
  (declare (ignore kind))
  variable-form)

(defun bind-get-vars-from-lambda-list (lambda-list)
  (let ((result nil))
    (labels ((do-it (thing)
	       (cond ((arrayp thing)
		      (loop for i below (array-total-size thing)
			 for var = (row-major-aref thing i) do (do-it var)))
		     ((atom thing) 
		      (unless (or (member thing *bind-lambda-list-markers*)
				  (var-ignorable-p thing))
			(push thing result)))
		     ((dotted-pair-p thing)
		      (do-it (car thing)) 
		      (do-it (cdr thing)))
		     (t
		      (do-it (car thing))
		      (do-it (cdr thing))))))
      (do-it lambda-list))
    (nreverse result)))

(defun bind-expand-declarations (declarations)
  (loop for declaration in declarations append
        (loop for decl in (rest declaration) append
              (cond ((member (first decl) *bind-non-var-declarations*)
                     (list decl))
                    ((member (first decl) *bind-simple-var-declarations*)
                     (loop for var in (rest decl) collect
                           `(,(first decl) ,var)))
                    (t
                     ;; a type spec
                     (when (eq (first decl) 'type)
                       (setf decl (rest decl)))
                     (loop for var in (rest decl) collect
                           `(type ,(first decl) ,var)))))))

(defun bind-filter-declarations (var-names)
  (setf var-names (if (consp var-names) var-names (list var-names)))  
  (setf var-names (bind-get-vars-from-lambda-list var-names))
  ;; each declaration is separate
  (let ((declaration
         (loop for declaration in *all-declarations*
               when (or (member (first declaration)
				*bind-non-var-declarations*)
                        (and (member (first declaration)
				     *bind-simple-var-declarations*)
			     (member 
			      (if (atom (second declaration))
				  (second declaration)
				  ;; ... (function foo) ...)
			   	  (second (second declaration)))
			      var-names))
			;; type
                        (member (third declaration) var-names)) collect
	      (progn
		(setf *all-declarations* (remove declaration *all-declarations*))
		declaration))))
    (when declaration 
      `((declare ,@declaration)))))

(defun merge-binding-forms-p (form1 form2)
  (and (consp form1) (consp form2)
       (let ((tag1 (first form1))
	     (tag2 (first form2)))
	 (and (symbolp tag1)
	      (symbolp tag2)
	      (string-equal (symbol-name tag1) (symbol-name tag2))
	      (or (string-equal (symbol-name tag1) "let")
		  (string-equal (symbol-name tag1) "let*")
		  (string-equal (symbol-name tag1) "labels"))))))

(defun map-tree (fn object)
  "apply `fn` to every leaf of `object`."
  (cond ((consp object)
         (map-tree fn (car object))
         (map-tree fn (cdr object)))
        (object
         (funcall fn object))))

(defun tree-find (tree it &key (test #'eq) (key #'identity))
  (flet ((isit (atom)
           (when key (setf atom (funcall key atom)))
           (when (funcall test it atom) (return-from tree-find t))))
    (declare (dynamic-extent #'isit))
    (map-tree #'isit tree)))

;;; fluid-bind

(defmacro fluid-bind ((&rest bindings) &body body)
  "Fluid-bind is an extension of bind that handles setting and resetting places. For example, suppose that an object of class foo has a slot named bar whose value is currently 3. The following code would evaluate the inner body with bar bound to 17 and restore it when the inner body is exited. 

\(fluid-bind \(\(\(bar foo\) 17\)\)
  \(print \(bar foo\)\)\)
\(print \(bar foo\)\)
==> \(prints 17, then 3\)

This is similar to dynamic-binding but _much_ less robust."
  ;; does not handle declarations correctly
  (let ((setup-forms nil)
        (cleanup-forms nil)
        (gensyms nil))
    (loop for binding in bindings collect
          (destructuring-bind 
		(setup-form cleanup-form)
	      (cond ((consp binding)
		     (destructuring-bind (var value) binding
		       (let ((g (gensym)))
			 (push g gensyms)
			 (cond ((atom var)
				`((:bind (,var ,value)) nil)
				#+(or)
				;; lexical or special?
				(if (boundp var)
				    `((:bind (,var ,value)) nil)
				    `((:setf (setf ,g ,var ,var ,value))
				      (setf ,var ,g))))
			       ((and (fboundp (first var))
				     (not (eq (first var) 'values)))
				;; putative place
				`((:setf (setf ,g ,var ,var ,value))
				  (setf ,var ,g)))
			       (t
				`((:bind (,var ,value)) nil))))))
		    (t
		     `((:bind (,binding nil)) nil)))
            (push setup-form setup-forms)
            (push cleanup-form cleanup-forms)))
    (let ((result body))
      (mapc (lambda (setup cleanup)
              (setf result
                    (ecase (first setup)
                      (:setf `((unwind-protect
                                 (progn
                                   ,(second setup)
                                   ,@result)
                                 ,cleanup)))
                      (:bind `((bind (,(second setup))
                                 ,@result)))))
              result)
            setup-forms cleanup-forms)
      `(let ,gensyms
         (declare (ignorable ,@gensyms))
         ,@result))))

#|
(let ((a 2))
  (fluid-bind ((a 3))
    (print a))
  (print a))

(fluid-bind (((population (current-world-state)) t))
  (print (population (current-world-state))))

(fluid-bind ((a 3)
             (*last-world* t)
             (*foo* nil))
  (declare (fixnum a))
  (print (list *last-world* *foo* a))
  (error "Ouch"))

(defvar *foo* 3)

(unwind-protect
  (bind ((#:g1 *last-world*))
    (setf *last-world* t)
    (unwind-protect
      (bind ((#:2 *foo*))
        (setf *foo* nil)
        (bind ((a 3))
          (list *last-world* *foo* a)))
      (setf *foo #:2)))
  (set *last-world* #:g1))      
    
(fluid-bind (a b)
  (+ a a))
|#