-
Notifications
You must be signed in to change notification settings - Fork 2
/
define-functions.lisp
313 lines (284 loc) · 11.9 KB
/
define-functions.lisp
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
;;;; Hairy function & macro definition macros
;;;
;;; define-functions.lisp is copyright 2020, 2022 by me, Tim Bradshaw,
;;; and may be used for any purpose whatsoever by anyone. It has no
;;; warranty whatsoever. I would appreciate acknowledgement if you use
;;; it in anger, and I would also very much appreciate any feedback or
;;; bug fixes.
;;;
(defpackage :org.tfeb.hax.define-functions
(:use :cl)
(:export
#:define-function
#:define-functions
#:define-macro-function
#:define-macro-functions))
(in-package :org.tfeb.hax.define-functions)
(provide :org.tfeb.hax.define-functions)
(defmacro define-functions (names/options &body fns/form)
"Hairy function-defining form.
NAMES/OPTIONS is a list of specifications for functions. Each element
may be either:
- a function name such as FOO or (SETF FOO);
- a lambda list (function-name &key ftype documentation);
In the second case:
- ftype specifies the function type, which is by default FUNCTION;
- documentation is function documentation (none by default).
If the body of the definition is a single form, this is assumed to
return as many values as there are functions to define. Otherwise
there should be as many forms in the body as there are functions to
define, and the value of each form is used for the corresponding
function."
(multiple-value-bind (names ftypes docs)
;; The parsing is just kind of horrid
(loop for n/o in names/options
for (name ftype doc)
= (cond
((or (symbolp n/o)
(and (consp n/o)
(eq (first n/o) 'setf)))
(list n/o 'function nil))
((consp n/o)
(destructuring-bind (name &key
(ftype 'function)
(documentation nil))
n/o
(list name ftype documentation)))
(t
(error "bad name/options ~A" n/o)))
collect name into the-names
collect ftype into the-ftypes
collect doc into the-docs
finally (return (values the-names the-ftypes the-docs)))
;; Make sure function definitions are
(assert (every (lambda (name)
(or (symbolp name)
(and (consp name)
(eq (first name) 'setf)
(symbolp (second name))
(= (list-length name) 2))))
names)
(names)
"bad function names in ~A" names)
`(progn
;; Type declaraions must happen at top level to teach the
;; compiler about the functions, and documentation might as
;; well.
(declaim ,@(loop for name in names
for ftype in ftypes
collect `(ftype ,ftype ,name)))
,@(loop for name in names
for doc in docs
when doc
collect `(setf (documentation ',name 'function)
,doc))
;; But the actual definition of them does not need to happen
;; until runtime
,@(if (= (length fns/form) 1)
;; values case
(let ((tmpns (loop for i from 0 below (length names)
collect (make-symbol (format nil "F~D" i)))))
`((multiple-value-bind ,tmpns ,(first fns/form)
,@(loop for n in names
for tmpn in tmpns
collect `(unless (functionp ,tmpn)
(error "function for ~S isn't" ',n))
collect `(setf (fdefinition ',n) ,tmpn)))))
;; listy case
(progn
(unless (= (length fns/form) (length names))
(error "expected ~D form~:P, got ~D"
(length names) (length fns/form) ))
(loop with tmpn = (make-symbol "F")
for name in names
for form in fns/form
collect `(let ((,tmpn ,form))
(unless (functionp ,tmpn)
(error "function for ~S isn't" ',name))
(setf (fdefinition ',name) ,tmpn)))))
(values ,@(loop for n in names collect `',n)))))
(defmacro define-function (name/options fn)
"Define a single function.
NAME/OPTIONS is interpreted as DEFINE-FUNCTIONS, which see."
`(define-functions (,name/options) ,fn))
#+LispWorks (editor:setup-indent "define-function" 1)
;;; Examples of define-functions / define-functions
;;;
#||
(define-functions (up down)
(let ((c 0))
(values
(lambda ()
(incf c))
(lambda ()
(decf c)))))
(define-function (inc
:ftype (function (number) number)
:documentation "increment a number")
(lambda (n)
(1+ n))) ||#
(defmacro define-macro-functions (names/options &body fns/form)
"Hairy macro-function defining form.
NAMES/OPTIONS is a list of specifications for macro functions. Each
element may be either: - a macro name such as FOO or (SETF FOO); - a
lambda list (macro-name &key documentation);
In the second case documentation is macro documentation (none by
default).
If the body of the definition is a single form, this is assumed to
return as many values as there are macros to define. Otherwise there
should be as many forms in the body as there are functions to define,
and the value of each form is used for the corresponding function.
Note that macro functions all take exactly two arguments."
;; This is much less useful than DEFINE-FUNCTION / DEFINE-FUNCTIONS
;; but I feel it should exist. It is at least simpler.
(multiple-value-bind (names docs)
;; The parsing is just kind of horrid
(loop for n/o in names/options
for (name doc)
= (cond
((symbolp n/o)
(list n/o nil))
((consp n/o)
(destructuring-bind (name &key
(documentation nil))
n/o
(list name documentation)))
(t
(error "bad name/options ~A" n/o)))
collect name into the-names
collect doc into the-docs
finally (return (values the-names the-docs)))
(assert (every #'symbolp names)
(names)
"bad macro names in ~A" names)
`(eval-when (:load-toplevel :compile-toplevel :execute)
;; Macros are needed at compile time as well
,@(if (= (length fns/form) 1)
;; values case
(let ((tmpns (loop for i from 0 below (length names)
collect (make-symbol (format nil "F~D" i)))))
`((multiple-value-bind ,tmpns ,(first fns/form)
,@(loop for n in names
for tmpn in tmpns
for d in docs
collect `(unless (functionp ,tmpn)
(error "macro function for ~S isn't" ',n))
collect `(setf (macro-function ',n) ,tmpn)
when d
collect `(setf (documentation ',n 'function)
,d)))))
;; listy case
(progn
(unless (= (length fns/form) (length names))
(error "expected ~D form~:P, got ~D"
(length names) (length fns/form)))
(loop with tmpn = (make-symbol "F")
for name in names
for d in docs
for form in fns/form
collect `(let ((,tmpn ,form))
(unless (functionp ,tmpn)
(error "macro function for ~S isn't" ',name))
(setf (macro-function ',name) ,tmpn))
when d
collect
`(setf (documentiation ',name 'function) d))))
(values ,@(loop for n in names collect `',n)))))
(defmacro define-macro-function (name/options fn)
"Define a single macro function.
NAME/OPTIONS is interpreted as DEFINE-MACRO-FUNCTIONS, which see."
`(define-macro-functions (,name/options) ,fn))
#+LispWorks
(editor:setup-indent "define-macro-function" 1)
;;; A silly example of define-macro-functions
;;;
#||
(define-macro-functions ((funging :documentation "incompatible with frobbing")
(frobbing :documentation "incompatible with funging"))
(let ((which nil))
(values
(lambda (form environment)
(declare (ignore environment))
(unless (or (not which) (eq which 'funging))
(error "can't funge and frob"))
(setf which 'funging)
`(progn ,@(rest form)))
(lambda (form environment)
(declare (ignore environment))
(unless (or (not which) (eq which 'frobbing))
(error "can't funge and frob"))
(setf which 'frobbing)
`(progn ,@(rest form))))))
||#
#||
;;; I am sure this serves no useful purpose: I thought it did but I
;;; was wrong
;;;
(defmacro define-variables (names/options &body vs/form)
"Hairy variable-defining form.
NAMES/OPTIONS is a list of specifications for variables. Each element
may be either:
- a variable name;
- a lambda list (variable-name &key type documentation).
In the second case:
- type specifies the variable's type;
- documentation is documentation (none by default).
If the body of the definition is a single form, this is assumed to
return as many values as there are variables to define. Otherwise
there should be as many forms in the body as there are functions to
define, and the value of each form is used for the corresponding
variable.
This is like DEFPARAMETER not DEFVAR."
(multiple-value-bind (names types docs)
;; The parsing is just kind of horrid
(loop for n/o in names/options
for (name type doc)
= (typecase n/o
(symbol
(list n/o t nil))
(cons
(destructuring-bind (name &key
(type 't)
(documentation nil))
n/o
(list name type documentation)))
(t
(error "bad name/options ~A" n/o)))
collect name into the-names
collect type into the-types
collect doc into the-docs
finally (return (values the-names the-types the-docs)))
(assert (every #'symbolp names)
(names)
"bad variable names in ~A" names)
`(progn
;; These must happen at top level to teach the compiler
(declaim
,@(loop for name in names
for type in types
unless (eq type t) ;no point if type is T
collect `(type ,type ,name)))
,@(loop for name in names
collect `(defvar ,name))
,@(loop for name in names
for doc in docs
when doc
collect `(setf (documentation ',name 'variable) ',doc))
,@(if (= (length vs/form) 1)
;; values case
(let ((tmpns (loop for i from 0 below (length names)
collect (make-symbol (format nil "V~D" i)))))
`((multiple-value-bind ,tmpns ,(first vs/form)
,@(loop for n in names
for tmpn in tmpns
collect `(setf ,n ,tmpn)))))
;; listy case
(progn
(unless (= (length vs/form) (length names))
(error "expected ~D form~:P, got ~D"
(length names) (length vs/form)))
(loop for name in names
for form in vs/form
collect `(setf ,name ,form))))
(values ,@(loop for n in names collect `',n)))))
||#