summaryrefslogtreecommitdiff
path: root/lambda.lisp
blob: 29dfb66ab0bc7637a5881a0018c5a628737a826d (plain)
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
;;; -*- mode: lisp -*-
;;; Copyright 2007, Matthias Andreas Benkard.

#| Basic usage
 | ===========
 |
 | (fn + _ 10)
 | (fn (+ _ 10))
 | (fn + _0 _1)
 | (fn + _ _1)
 | (mapcar (fn (cons _ _)) '(1 2 3))     ;=> ((1 . 1) (2 . 2) (3 . 3))
 | (funcall (fn + _ 10 _3) 20 30 40 50)  ;=> 80
 |
 |
 | Simple variant FN1
 | ==================
 |
 | (funcall (fn () _) 42)                ;=> 42
 | (funcall (fn _) 42)                   ;=> error (usually)
 | (funcall (fn1 _) 42)                  ;=> 42
 | (funcall (fn +))                      ;=> 0
 | (funcall (fn1 +))                     ;=> value of +
 |
 |
 | Argument-number-safe variant EFN
 | ================================
 |
 | (funcall (fn (fn () _)))              ;=> #<LAMBDA ...>
 | (funcall (efn (efn () _)))            ;=> error
 | (funcall (fn1 (fn1 _)))               ;=> #<LAMBDA ...>
 | (funcall (efn1 (efn1 _)))             ;=> error
 |#


(defpackage #:mulk.lambda
  (:use #:cl)
  (:export #:efn #:fn #:efn1 #:fn1))
(in-package #:mulk.lambda)


(defun collect-lambda-args (clause)
  (typecase clause
    (list   (mapcan #'collect-lambda-args clause))
    (symbol (let ((name (symbol-name clause)))
              (if (or (string= "_" name)
                      (and (char= #\_ (char name 0))
                           (ignore-errors
                             (parse-integer (subseq name 1)))))
                  (list clause)
                  nil)))))


(defun find-lambda-args (clause)
  (let* ((lambda-args (collect-lambda-args clause))
         (max-arg-no  (loop for lambda-arg in lambda-args
                            maximizing
                              (let ((name (symbol-name lambda-arg)))
                                (if (string= "_" name)
                                    0
                                    (parse-integer (subseq name 1))))))
         (arglist     (loop for i from 0 to (if (not (null lambda-args))
                                                max-arg-no
                                                -1)
                            collecting
                              (let ((symbol (find (format nil "_~D" i)
                                                  lambda-args
                                                  :key #'symbol-name
                                                  :test #'string=)))
                                ;; If the user does not use a particular
                                ;; positional argument, we shall not
                                ;; introduce its name, either.  Use a
                                ;; GENSYM instead.
                                (or symbol (gensym (format nil "_~D_" i)))))))
    (cond ((null lambda-args)           nil)
          ((position "_" lambda-args
                     :key #'symbol-name
                     :test #'string=)   (cons (intern "_" *package*) arglist))
          (t                            arglist))))


(defmacro efn (&body args)
  "A convenience wrapper for LAMBDA.

Positional arguments are numbered from 0 and follow the pattern given by the
format string \"_~D\".  _ can be used as an abbreviation for _0.

FN can be used recursively, but this has to be done with care, for the
macro will inevitably get confused about the number of arguments it
should use if a subform uses more lambda arguments than its enclosing FN
form.  For instance, the following will work:

 (funcall (efn (efn () 10) _) 'foo)

But the following will not, because the inner _ is detected by the outer
FN as an argument, making the latter expect an argument where none is
given:

 (funcall (efn (efn () _)))"
  (cond ((null args)                `#'(lambda ()))
        ((not (listp (first args))) `(efn ,args))
        (t
         (let* ((lambda-args      (find-lambda-args `(progn ,@args)))
                (real-lambda-args (remove "_" lambda-args
                                          :key #'symbol-name
                                          :test #'string=))
                (_                (find "_" lambda-args
                                        :key #'symbol-name
                                        :test #'string=)))
           `(symbol-macrolet ,(if _
                                  `((,_ ,(first real-lambda-args)))
                                  nil)
              (function (lambda ,real-lambda-args
                (declare (ignorable ,@real-lambda-args))
                ,@args)))))))


(defmacro fn (&body args)
  "A less safe but recursively callable variant of EFN.

This macro is like EFN save the fact that the anonymous functions it
produces do not check the number of their arguments, thereby
circumventing lambda argument misidentification errors in
COLLECT-LAMBDA-ARGS."
  (cond ((null args)                `#'(lambda ()))
        ((not (listp (first args))) `(fn ,args))
        (t
         (let* ((lambda-args      (find-lambda-args `(progn ,@args)))
                (real-lambda-args (remove "_" lambda-args
                                          :key #'symbol-name
                                          :test #'string=))
                (_                (find "_" lambda-args
                                        :key #'symbol-name
                                        :test #'string=))
                (args-sym         (gensym "FN-ARGS")))
           `(symbol-macrolet (,@(if _
                                    `((,_ ,(first real-lambda-args)))
                                    nil)
                              ,@(loop for i from 0
                                      for lambda-arg in real-lambda-args
                                      collect `(,lambda-arg
                                                 (nth ,i ,args-sym))))
              (function (lambda (&rest ,args-sym)
                (declare (ignorable ,args-sym))
                ,@args)))))))


(defmacro efn1 (&body args)
  "A variant of EFN that does not try to interpret its first argument as
  a function name.

  Useful for stuff like (EFN1 _3).

  (EFN1 a b c ...) is semantically equivalent to (EFN () a b c ...)."
  (cond ((null args)                `(efn))
        ((not (listp (first args))) `(efn () ,@args))
        (t                          `(efn ,@args))))


(defmacro fn1 (&body args)
  "A variant of FN that does not try to interpret its first argument as
  a function name.

  Useful for stuff like (FN1 _3).

  (FN1 a b c ...) is semantically equivalent to (FN () a b c ...)."
  (cond ((null args)                `(fn))
        ((not (listp (first args))) `(fn () ,@args))
        (t                          `(fn ,@args))))