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

From: Noel Welsh (noelwelsh at yahoo.com)
Date: Wed Apr 5 22:28:02 EDT 2006

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 


Posted on the users mailing list.