-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcj-functional.scm
341 lines (283 loc) · 7.55 KB
/
cj-functional.scm
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
;;; Copyright 2010-2020 by Christian Jaeger <ch@christianjaeger.ch>
;;; This file is free software; you can redistribute it and/or modify
;;; it under the terms of the GNU General Public License (GPL) as published
;;; by the Free Software Foundation, either version 2 of the License, or
;;; (at your option) any later version.
(require define-macro-star
test
(fixnum inc dec)
cj-symbol
(list-util let-pair)
(values apply-values)
(lazy FV)
(cj-gambit-sys maybe-decompile)
;;(cj-source-util schemedefinition-arity:pattern->template)
(cj-functional-2 compose compose-function either)
(code-util early-bind-expressions))
(export right-associate
left-associate
syntax:right-associate
syntax:left-associate
(macro RA)
(macro LA)
compose**
(macro compose)
(macro compose*)
(macro compose//)
true/0
true/1
false/0
false/1
just?
maybe-function (macro maybe)
on* ;; for binary |on| see cj-env
(macro or**)
(macro or/)
<to<=
sorted-list-of)
;; n-ary:
(define (right-associate fn lis error)
(if (null? lis)
(error "got no element")
(if (null? (cdr lis))
(car lis)
(let rec ((lis lis))
(let* ((lis* (cdr lis))
(lis** (cdr lis*)))
(if (null? lis**)
(fn (car lis)
(car lis*))
(fn (car lis)
(rec lis*))))))))
(define (left-associate fn lis error)
(right-associate (lambda (a b)
(fn b a))
(reverse lis)
error))
(define (syntax:_-associate _-associate)
(lambda (op lis error)
(_-associate (lambda (a b)
(list op a b))
lis
error)))
(define syntax:right-associate (syntax:_-associate right-associate))
(define syntax:left-associate (syntax:_-associate left-associate))
(TEST
> (syntax:right-associate 'comp-function (list 'a 'b 'c) error)
(comp-function a (comp-function b c))
> (syntax:right-associate 'comp-function (list 'b 'c) error)
(comp-function b c)
;; and left...
> (syntax:left-associate 'comp-function (list 'a 'b 'c) error)
(comp-function (comp-function a b) c)
> (syntax:left-associate 'comp-function (list 'b 'c) error)
(comp-function b c)
;; and errors:
> (map (lambda (_)
(with-exception-catcher error-exception-message
(lambda () (_ 'comp-function (list 'c) error))))
(list syntax:right-associate syntax:left-associate))
(c c)
> (map (lambda (_)
(with-exception-catcher error-exception-message
(lambda () (_ 'comp-function (list) error))))
(list syntax:right-associate syntax:left-associate))
("got no element" "got no element")
)
(define-macro* (RA op . exprs)
(syntax:right-associate op exprs
(lambda (msg)
(raise-source-error stx msg))))
(define-macro* (LA op . exprs)
(syntax:left-associate op exprs
(lambda (msg)
(raise-source-error stx msg))))
(TEST
> (expansion#RA compose half x*y inc2values)
(compose half (compose x*y inc2values))
> (expansion#LA compose half x*y inc2values)
(compose (compose half x*y) inc2values)
)
;; as a function:
;; XX rename to compose*, or compose to compose* (no) or ?
(define (compose** . fs)
(right-associate compose-function fs error))
;; as macro for more performance, unary only:
(define-macro* (compose . es)
(early-bind-expressions
es
(with-gensym V
`(lambda (,V)
,(fold-right (lambda (e inner)
`(,e ,inner))
V
es)))))
;; same thing, n-ary: -- COPY PASTE
(define-macro* (compose* . es)
(let* ((es* (map (lambda (e)
(let ((e* (source-code e)))
;; pre-eval-sym, e
(list (if (symbol? e*)
#f
(gensym)) e)))
es))
(lam (with-gensym VS
`(lambda ,VS
,(let-pair
((e0 er) (reverse es*))
(fold (lambda (e inner)
`(,(or (car e)
(cadr e)) ,inner))
`(apply ,(or (car e0)
(cadr e0)) ,VS)
er)))))
(es*-pre-eval (filter car es*)))
(if (null? es*-pre-eval)
lam
`(let ,es*-pre-eval
,lam))))
;; with parametrizable arity:
(define-macro* (compose// n . es)
(early-bind-expressions
es
(assert* natural0? n
(lambda (n)
(let* ((ARGS (map (lambda (n) (gensym)) (iota n)))
(code (fold-right (lambda (e inner)
`((,e ,@inner)))
ARGS
es)))
(if (and (pair? code)
(null? (cdr code)))
`(lambda ,ARGS
,(car code))
(error "bug")))))))
(TEST
> (define TEST:equal? syntax-equal?)
> (expansion#compose a b c)
(lambda (GEN:X-3566) (a (b (c GEN:X-3566))))
> (expansion#compose a)
(lambda (GEN:X-3567) (a GEN:X-3567))
> (expansion#compose a (maybe b) (complement c))
(##let ((GEN:-546 (maybe b))
(GEN:-547 (complement c)))
(lambda (GEN:V-548) (a (GEN:-546 (GEN:-547 GEN:V-548)))))
> (expansion#compose// 1 a b c)
(lambda (GEN:-3382) (a (b (c GEN:-3382))))
> (expansion#compose// 3 a b c)
(lambda (GEN:-3383 GEN:-3384 GEN:-3385)
(a (b (c GEN:-3383 GEN:-3384 GEN:-3385)))))
(TEST
> (define (half x) (/ x 2))
> (define (square x) (* x x))
> (define x*y (lambda-values ((x y)) (* x y)))
> ((compose** half inc-function square) 10)
101/2
> ((compose half inc square) 10)
101/2
> ((compose// 1 half inc square) 10)
101/2
> ((compose** half inc-function x*y) (values 10 20))
201/2
> ((compose half inc-function x*y) (values 10 20))
201/2
> ((compose// 1 half inc-function x*y) (values 10 20))
201/2
> (define (inc2values x y) (values (inc x) (inc y)))
> ((compose** half x*y inc2values) 10 20)
231/2
> ((compose* half x*y inc2values) 10 20)
231/2
> ((compose// 2 half x*y inc2values) 10 20)
231/2
;; compose is ("fully") associative (left or right doesn't matter),
;; so choosing right-associate was arbitrary
> ((compose-function (compose half x*y) inc2values) 10 20)
231/2
> ((compose-function half (compose-function x*y inc2values)) 10 20)
231/2)
(define (true/0)
#t)
(define (true/1 x)
#t)
(define (false/0)
#f)
(define (false/1 x)
#f)
;; move to lib pure booleans?
(define (just? v)
(and v #t))
;; and to a lib for maybe handling?
(define (_-maybe fn)
(lambda (v)
(and v
(fn v))))
;; function variant of (maybe pred), now that maybe is a macro
(define (maybe-function pred)
(either not pred))
(define-macro* (maybe pred)
(early-bind-expressions
(pred)
`(either not ,pred)))
(TEST
> ((maybe number?) 1)
#t
> ((maybe number?) #f)
#t
> ((maybe number?) #t)
#f
)
;; n-ary "on"
;; for binary |on| see cj-env
(define (on* fn1 fn2)
(lambda args
(apply fn2 (map fn1 args))))
;; an "or" that triggers on something else than #f (but returns #f on
;; 'failure'):
(define-macro* (or** true? . clauses)
(with-gensyms
(TRUE? V)
`(let ((,TRUE? ,true?))
,(let rec ((clauses clauses))
(if (null? clauses)
`#f
`(let ((,V ,(car clauses)))
(if (,TRUE? ,V)
,V
,(rec (cdr clauses)))))))))
;; and build a true? predicate easily:
(define-macro* (or/ false-value . clauses)
(with-gensym
FALSE-VALUE
`(let ((,FALSE-VALUE ,false-value))
(or** (lambda (v)
(not (eq? v ,FALSE-VALUE)))
,@clauses))))
(TEST
> (or/ 'false 'false 'nonfalse 'anothernonfalse)
nonfalse
> (or/ 'false 'false 'false)
#f
)
(define <to<=
;; <-><= would be a fun name, wouldn't it?
(compose complement-2 flip))
(define (sorted-list-of el? <)
(strictly-monotonic-list-of el? (<to<= <)))
(TEST
> ((sorted-list-of number? <) '(1 2 3))
#t
> ((sorted-list-of number? <) '(3 2 1))
#f
> ((sorted-list-of number? >) '(3 2 1))
#t
> ((sorted-list-of number? >) '(3 2 2))
#t
> ((sorted-list-of number? >=) '(3 2 2))
#f ;; hehe kinda nonsensical now ok?
> ((sorted-list-of number? <) '(1 2 2))
#t
> ((sorted-list-of number? <=) '(1 2 2))
#f ;; ditto
> ((sorted-list-of number? >) '())
#t)