[plt-scheme] `shared' syntax confusing
Matthew Flatt <mflatt at cs.utah.edu> writes:
> The documentation for `scheme/shared' (or `scheme'), however, is
> considerably more detailed already:
>
> http://docs.plt-scheme.org/reference/shared.html
Thanks, I had missed that.
> It was easy to support the second example when `cons' created mutable
> pairs. Now that it creates immutable pairs, `shared' must be able to
> predict more of the shape of the resulting data to be able to construct
> the circular version, and there will obviously be approximations in its
> analysis of the shape. Not being able to see through `(if #t ...)' is
> one of the approximations.
What about post-processing the values instead of predicting
the shape? The following seems to work fine for me:
(define-syntax shared
(syntax-rules ()
((shared ((var expr) ...) body ...)
(shared-build () ((var expr) ...) body ...))))
(define-syntax shared-build
(syntax-rules ()
((shared-build ((var tmp expr) ...) ((var2 expr2) rest ...) body ...)
(shared-build ((var tmp expr) ... (var2 tmp2 expr2)) (rest ...) body ...))
((shared-build ((var tmp expr) ...) () body ...)
(let ((tmp (make-placeholder #f)) ...)
(let ((var #f) ...)
(set! var (let-syntax
((var (make-set!-transformer
(lambda (stx)
(syntax-case stx (set!)
((set! id val) #'(set! id val))
(id #'tmp)))))
...)
expr)) ...
(placeholder-set! tmp var) ...
(set! var (make-reader-graph var)) ...
body ...)))))
--
Alex