[plt-scheme] Mutation of expansion time values not visible outside module

From: Robby Findler (robby at cs.uchicago.edu)
Date: Wed Apr 5 22:45:40 EDT 2006

You probably want to read Matthew's ICFP paper about macros (subtitle:
"you want it when?"). It's thesis is that using state to do that kind
of staging is a bad idea. It shows how to use macro-defining macros to
accomplish some of that, but I guess it isn't possible in general to do
it and to preserve separate compilation (I'm not sure how far you're
going, tho).

Robby

At Wed, 5 Apr 2006 19:28:02 -0700 (PDT), Noel Welsh wrote:
> Hi all,
> 
> I'm trying to write what is effectively a staged
> computation.  As stage 1 I want to define some data,
> possibly including mutation, and at stage 2 I want to use
> this data for another task (The real task: stage 1 is the
> definition of persistent data, stage 2 mapping to database
> tables and scheme values.  By separating the stages I
> should avoid order of evaluation issues if I guarantee
> mutation only occurs in stage 1.  This is rather
> desirable.)
> 
> Anyhow, I've written some dummy code to test out my ideas. 
> I'm not getting the results I expect.  I define an
> expansion time value foo, which is a box of symbols.  I
> then mutate the contents of the box.  When I'm inside the
> module defining foo, if I lower foo to a run-time value and
> display it, it has the expected value.  Outside of the
> module, however, foo only contains the values I initially
> created it with.  What's going on?
> 
> Code is below.  
> 
> TIA,
> Noel
> 
> ;; Messing around with a staged implementation of entities
> ;; and persistent structs
> 
> ;; To use
> ;;
> ;;   > (require "staged.ss")
> ;;   > (lower-entity foo)
> ;;     Should output #&(e d a b c) but only outputs #&(a b
> c)
> (module staged mzscheme
> 
>   (provide lower-entity
>            foo
>            add-val!
>            define-entity)
> 
>   ;; defines an entity, which is just a box of values
>   (define-syntax define-entity
>     (syntax-rules ()
>       ((define-entity name val ...)
>        (define-syntax name (box (list val ...))))))
> 
>   ;; adds a value to an entity
>   (define-syntax (add-val! stx)
>     (syntax-case stx ()
>       ((add-val! entity val)
>        (with-syntax
>            ((result
>              (set-box! (syntax-local-value (syntax entity))
>                        (cons (syntax-object->datum (syntax
> val))
>                              (unbox (syntax-local-value
> (syntax entity)))))))
>          (syntax result)))))
> 
>   ;; lowers an entity from an expansion time to a run time
>   ;; value
>   (define-syntax (lower-entity stx)
>     (syntax-case stx ()
>       ((lower-entity entity)
>        (with-syntax
>            ((val (syntax-local-value (syntax entity))))
>          (syntax val)))))
> 
>   ;; Test order of evalution.  Will both add-val! be
>   ;; reflected in the lowered value?
>   (define-entity foo 'a 'b 'c)
> 
>   (add-val! foo d)
> 
>   (display (lower-entity foo))
> 
>   (add-val! foo e)
> 
>   )
> 
> 
> 
>  
> 
> Email: noelwelsh <at> yahoo <dot> com   noel <at> untyped <dot> com
> AIM: noelhwelsh
> Blogs: http://monospaced.blogspot.com/  http://www.untyped.com/untyping/
> 
> __________________________________________________
> Do You Yahoo!?
> Tired of spam?  Yahoo! Mail has the best spam protection around 
> http://mail.yahoo.com 
> _________________________________________________
>   For list-related administrative tasks:
>   http://list.cs.brown.edu/mailman/listinfo/plt-scheme


Posted on the users mailing list.