Debian Codegraph @

lab/SRFI-2.ss

raw
1 ; Checking of a LAND* special form
2 ;
3 ; LAND* is a generalized AND: it evaluates a sequence of forms one after another
4 ; till the first one that yields #f; the non-#f result of a form can be bound
5 ; to a fresh variable and used in the subsequent forms.
6 ;
7 ; When an ordinary AND is formed of _proper_ boolean expressions:
8 ; (AND E1 E2 ...)
9 ; expression E2, if it gets to be evaluated, knows that E1 has returned non-#f.
10 ; Moreover, E2 knows exactly what the result of E1 was - #t - so E2 can use
11 ; this knowledge to its advantage. If E1 however is an _extended_
12 ; boolean expression, E2 can no longer tell which particular non-#f
13 ; value E1 has returned. Chances are it took a lot of work to evaluate E1,
14 ; and the produced result (a number, a vector, a string, etc) may be of
15 ; value to E2. Alas, the AND form merely checks that the result is not an #f,
16 ; and throws it away. If E2 needs it, it has to recompute the value again.
17 ; This proposed LAND* special form lets constituent expressions get
18 ; hold of the results of already evaluated expressions, without re-doing
19 ; their work.
20 ;
21 ; Syntax:
22 ; LAND* (CLAWS) BODY
23 ;
24 ; where CLAWS is a list of expressions or bindings:
25 ; CLAWS ::= '() | (cons CLAW CLAWS)
26 ; Every element of the CLAWS list, a CLAW, must be one of the following:
27 ; (VARIABLE EXPRESSION)
28 ; or
29 ; (EXPRESSION)
30 ; or
31 ; BOUND-VARIABLE
32 ; These CLAWS are evaluated in the strict left-to-right order. For each
33 ; CLAW, the EXPRESSION part is evaluated first (or BOUND-VARIABLE is looked up).
34 ; If the result is #f, LAND* immediately returns #f, thus disregarding the rest
35 ; of the CLAWS and the BODY. If the EXPRESSION evaluates to not-#f, and
36 ; the CLAW is of the form
37 ; (VARIABLE EXPRESSION)
38 ; the EXPRESSION's value is bound to a freshly made VARIABLE. The VARIABLE is
39 ; available for _the rest_ of the CLAWS, and the BODY. As usual, all
40 ; VARIABLEs must be unique (like in let*).
41 ;
42 ; Thus LAND* is a sort of cross-breed between LET* and AND.
43 ;
44 ; Denotation semantics:
45 ;
46 ; Eval[ (LAND* (CLAW1 ...) BODY), Env] =
47 ; EvalClaw[ CLAW1, Env ] andalso
48 ; Eval[ (LAND* ( ...) BODY), ExtClawEnv[ CLAW1, Env]]
49 ;
50 ; Eval[ (LAND* (CLAW) ), Env] = EvalClaw[ CLAW, Env ]
51 ; Eval[ (LAND* () FORM1 ...), Env] = Eval[ (BEGIN FORM1 ...), Env ]
52 ; Eval[ (LAND* () ), Env] = #t
53 ;
54 ; EvalClaw[ BOUND-VARIABLE, Env ] = Eval[ BOUND-VARIABLE, Env ]
55 ; EvalClaw[ (EXPRESSION), Env ] = Eval[ EXPRESSION, Env ]
56 ; EvalClaw[ (VARIABLE EXPRESSION), Env ] = Eval[ EXPRESSION, Env ]
57 ;
58 ; ExtClawEnv[ BOUND-VARIABLE, Env ] = Env
59 ; ExtClawEnv[ (EXPRESSION), Env ] = EnvAfterEval[ EXPRESSION, Env ]
60 ; ExtClawEnv[ (VARIABLE EXPRESSION), Env ] =
61 ; ExtendEnv[ EnvAfterEval[ EXPRESSION, Env ],
62 ; VARIABLE boundto Eval[ EXPRESSION, Env ]]
63 ;
64 ;
65 ; If one has a Scheme interpreter written in Prolog/ML/Haskell, he can
66 ; implement the above semantics right away. Within Scheme, it is trivial to
67 ; code LAND* with R4RS "define-syntax". Alas, Gambit does not have this
68 ; facility. So this implementation uses macros instead.
69 ;
70 ; The following LAND* macro will convert a LAND* expression into a "tree" of
71 ; AND and LET expressions. For example,
72 ; (LAND* ((my-list (compute-list)) ((not (null? my-list))))
73 ; (do-something my-list))
74 ; is transformed into
75 ; (and (let ((my-list (compute-list)))
76 ; (and my-list (not (null? my-list)) (begin (do-something my-list)))))
77 ;
78 ; I must admit the LAND* macro is written in a pathetic anti-functional style.
79 ; To my excuse, the macro's goal is a syntactic transformation of source
80 ; code, that is, performing a re-writing. IMHO, rewriting kind of suggests
81 ; mutating.
82 ;
83 ; Sample applications:
84 ;
85 ; The following piece of code (from my treap package)
86 ; (let ((new-root (node:dispatch-on-key root key ...)))
87 ; (if new-root (set! root new-root)))
88 ; could be elegantly re-written as
89 ; (land* ((new-root (node:dispatch-on-key root key ...)))
90 ; (set! root new-root))
91 ;
92 ; A very common application of land* is looking up a value
93 ; associated with a given key in an assoc list, returning #f in case of a
94 ; look-up failure:
95 ;
96 ; ; Standard implementation
97 ; (define (look-up key alist)
98 ; (let ((found-assoc (assq key alist)))
99 ; (and found-assoc (cdr found-assoc))))
100 ;
101 ; ; A more elegant solution
102 ; (define (look-up key alist)
103 ; (cdr (or (assq key alist) '(#f . #f))))
104 ;
105 ; ; An implementation which is just as graceful as the latter
106 ; ; and just as efficient as the former:
107 ; (define (look-up key alist)
108 ; (land* ((x (assq key alist))) (cdr x)))
109 ;
110 ; Generalized cond:
111 ;
112 ; (or
113 ; (land* (bindings-cond1) body1)
114 ; (land* (bindings-cond2) body2)
115 ; (begin else-clause))
116 ;
117 ; Unlike => (cond's send), LAND* applies beyond cond. LAND* can also be used
118 ; to generalize cond, as => is limited to sending of only a single value;
119 ; LAND* allows as many bindings as necessary (which are performed in sequence)
120 ;
121 ; (or
122 ; (land* ((c (read-char)) ((not (eof-object? c))))
123 ; (string-set! some-str i c) (++! i))
124 ; (begin (do-process-eof)))
125 ;
126 ; Another concept LAND* is reminiscent of is programming with guards:
127 ; a LAND* form can be considered a sequence of _guarded_ expressions.
128 ; In a regular program, forms may produce results, bind them to variables
129 ; and let other forms use these results. LAND* differs in that it checks
130 ; to make sure that every produced result "makes sense" (that is, not an #f).
131 ; The first "failure" triggers the guard and aborts the rest of the
132 ; sequence (which presumably would not make any sense to execute anyway).
133 ;
134 ; $Id: vland-gambit.scm,v 1.1 1998/12/28 23:54:29 srfimgr Exp $
135
136
137 (library (SRFI-2)
138 (export land*)
139 (import (ikarus))
140 (define-syntax land*
141 (lambda (x)
142 (define free-id (car (generate-temporaries '(_))))
143 (define (bound-identifier? x)
144 (and (identifier? x)
145 (not
146 (free-identifier=? x
147 (datum->syntax free-id (syntax->datum x))))))
148 (syntax-case x ()
149 [(_ ()) #t]
150 [(_ (claws ...) b b* ...)
151 #'(land* (claws ... (tmp (begin b b* ...))))]
152 [(_ ([var expr])) #'expr]
153 [(_ ([expr])) #'expr]
154 [(_ (var))
155 (if (bound-identifier? #'var)
156 #'var
157 (syntax-error #'var "var is unbound in land* clause"))]
158 [(_ ([var expr] claws ...)) (identifier? #'var)
159 #'(cond
160 [expr => (lambda (var) (land* (claws ...)))]
161 [else #f])]
162 [(_ ([expr] claws ...))
163 #'(and expr (land* (claws ...)))]
164 [(_ (var claws ...))
165 (if (bound-identifier? #'var)
166 #'(and var (land* (claws ...)))
167 (syntax-error #'var "var is unbound in land*
168 clause"))]))))
169
170 #!eof
171 #|
172 (define-macro (LAND* claws . body)
173 (let* ((new-vars '()) (result (cons 'and '())) (growth-point result))
174
175 ; We need a way to report a syntax error
176 ; the following is how Gambit compiler does it...
177 (##define-macro (ct-error-syntax msg . args)
178 `(##signal '##signal.syntax-error #t ,msg ,@args))
179
180 (define (andjoin! clause)
181 (let ((prev-point growth-point) (clause-cell (cons clause '())))
182 (set-cdr! growth-point clause-cell)
183 (set! growth-point clause-cell)))
184
185 (if (not (list? claws))
186 (ct-error-syntax "bindings must be a list " bindings))
187 (for-each
188 (lambda (claw)
189 (cond
190 ((symbol? claw) ; BOUND-VARIABLE form
191 (andjoin! claw))
192 ((and (pair? claw) (null? (cdr claw))) ; (EXPRESSION) form
193 (andjoin! (car claw)))
194 ; (VARIABLE EXPRESSION) form
195 ((and (pair? claw) (symbol? (car claw))
196 (pair? (cdr claw)) (null? (cddr claw)))
197 (let* ((var (car claw)) (var-cell (cons var '())))
198 (if (memq var new-vars)
199 (ct-error-syntax "duplicate variable " var " in the bindings"))
200 (set! new-vars (cons var new-vars))
201 (set-cdr! growth-point `((LET (,claw) (AND . ,var-cell))))
202 (set! growth-point var-cell)))
203 (else
204 (ct-error-syntax "An ill-formed binding in a syntactic form land* "
205 claw))
206 ))
207 claws)
208 (if (not (null? body))
209 (andjoin! `(begin ,@body)))
210 result))
211 |#
212
213 ; Validation tests
214 (##include "myenv.scm")
215 (##include "catch-error.scm")
216
217 (display "\nValidating LAND*...\n\n")
218
219 ; make sure that the 'FORM' gave upon evaluation the
220 ; EXPECTED-RESULT
221 (define-macro (expect form expected-result)
222 `(begin
223 (display "evaluating ")
224 (write ',form)
225 (let ((real-result (eval ',form)))
226 (if (equal? real-result ,expected-result)
227 (cout "... gave the expected result: " real-result nl)
228 (error "... yielded: " real-result
229 " which differs from the expected result: " ,expected-result)
230 ))))
231
232 ; Check to see that 'form' has indeed a wrong syntax
233 (define-macro (must-be-a-syntax-error form)
234 `(call-with-current-continuation
235 (lambda (k)
236 (##catch-signal '##signal.syntax-error
237 (lambda x
238 (display "catching a syntax error: ") (display x) (newline)
239 (k #f))
240 (lambda ()
241 (eval ',form)
242 (error "No syntax error detected, unexpectedly"))))))
243
244 (expect (land* () 1) 1)
245 (expect (land* () 1 2) 2)
246 (expect (land* () ) #t)
247
248 (expect (let ((x #f)) (land* (x))) #f)
249 (expect (let ((x 1)) (land* (x))) 1)
250 (expect (land* ((x #f)) ) #f)
251 (expect (land* ((x 1)) ) 1)
252 (must-be-a-syntax-error (land* ( #f (x 1))) )
253 (expect (land* ( (#f) (x 1)) ) #f)
254 (must-be-a-syntax-error (land* (2 (x 1))) )
255 (expect (land* ( (2) (x 1)) ) 1)
256 (expect (land* ( (x 1) (2)) ) 2)
257 (expect (let ((x #f)) (land* (x) x)) #f)
258 (expect (let ((x "")) (land* (x) x)) "")
259 (expect (let ((x "")) (land* (x) )) "")
260 (expect (let ((x 1)) (land* (x) (+ x 1))) 2)
261 (expect (let ((x #f)) (land* (x) (+ x 1))) #f)
262 (expect (let ((x 1)) (land* (((positive? x))) (+ x 1))) 2)
263 (expect (let ((x 1)) (land* (((positive? x))) )) #t)
264 (expect (let ((x 0)) (land* (((positive? x))) (+ x 1))) #f)
265 (expect (let ((x 1)) (land* (((positive? x)) (x (+ x 1))) (+ x 1))) 3)
266 (must-be-a-syntax-error
267 (let ((x 1)) (land* (((positive? x)) (x (+ x 1)) (x (+ x 1))) (+ x 1)))
268 )
269
270 (expect (let ((x 1)) (land* (x ((positive? x))) (+ x 1))) 2)
271 (expect (let ((x 1)) (land* ( ((begin x)) ((positive? x))) (+ x 1))) 2)
272 (expect (let ((x 0)) (land* (x ((positive? x))) (+ x 1))) #f)
273 (expect (let ((x #f)) (land* (x ((positive? x))) (+ x 1))) #f)
274 (expect (let ((x #f)) (land* ( ((begin x)) ((positive? x))) (+ x 1))) #f)
275
276 (expect (let ((x 1)) (land* (x (y (- x 1)) ((positive? y))) (/ x y))) #f)
277 (expect (let ((x 0)) (land* (x (y (- x 1)) ((positive? y))) (/ x y))) #f)
278 (expect (let ((x #f)) (land* (x (y (- x 1)) ((positive? y))) (/ x y))) #f)
279 (expect (let ((x 3)) (land* (x (y (- x 1)) ((positive? y))) (/ x y))) 3/2)
280
281 (display "\nAll tests passed\n")
282
283 (let
284 ((a-definition
285 '(define (bbb)
286 (LAND* ((my-list (compute-list)) a-condition ((not (null? my-list)))
287 (my-list-tail (cdr my-list)))
288 (do-something my-list-tail)))))
289 (cout "The result of compiling of\n"
290 (lambda () (pp a-definition)) "\nis the following\n")
291 (eval a-definition)
292 (pp bbb)
293 )