[racket] How can I unsplice a list of expression into code?

From: John Clements (clements at brinckerhoff.org)
Date: Sun Nov 18 00:48:55 EST 2012

On Nov 16, 2012, at 10:05 PM, Monica Tomson wrote:

> Hi,
> 
> I have an experiment for my project, basically, I need to embedded some s-expression into the code and make it run, like this,
> 
> (define (test lst)
> 
>     
> (define num 1)
> 
>     
> (define l (list))
> 
>     
> `@lst) ; oh, this is not the right way to go.
> 
> 
>   
> (define
>  lst
>     
> `( (define num2 (add1 num))
> 
>       
> (displayln num2)))
> I want the test function be like after test(lst) in racket code:
> 
> (define (test lst)
> 
>     
> (define num 1)
> 
>     
> (define l (list))
> 
>     
> (define num2 (add1 num)
> 
>     
> (displayln num2))
> How can I do this in racket?

I'm following up to your post to include the explanation that you posted on StackOverflow:


> Update The reason I would like to use eval or the previous questions is that I am using Z3 racket binding, I need to generate formulas (which uses racket binding APIs), and then I will fire the query at some point, that's when I need to evaluate those code. I have not figured out other ways to go in my case... One super simple example is, imagine
> (let ([arr (array-alloc 10)])
> (array-set! arr 3 4))
> 
> I have some model to analyze the constructs (so I am not using racketZ3 directly), during each analyzing point, I will map the data types in the program into the Z3 types, and made some assertions, like this:
> 
> I will generate something like:
> 
> 	• At allocation site, I will need to make the following formula:
> 
> (smt:declare-fun arr_z3 () IntList)
> (define len (make-length 10))
> 
> 	• Then at the array set site, I will have the following assertions and to check whether the 3 less then the length*
> 
> (smt:assert (< 3 (smt:eval (len arr_z3))
> (smt:check-sat)
> 
> 	• Then finally, I will gather the formulas generated as above, and wrap them in the form which is able to fire Z3 binding to run the following gathered information as code:
> 
> (smt:with-context
> (smt:new-context)
> (define len (make-length 10))
> (smt:assert (< 3 (smt:eval (len arr_z3))
> (smt:check-sat))
> 
> This is the super simple example I can think of... making sense?
> 


Ah! are you automatically generating a z3 program from a Racket program? In this case, it makes perfect sense that you'd be mucking around at the syntactic level. In fact, if you want to do the job right, you might want to be taking expanded syntax as inputs, rather than s-expressions. It sounds like you don't want macros here so much as you want a complete syntactic rewrite?

John Clements



-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 4370 bytes
Desc: not available
URL: <http://lists.racket-lang.org/users/archive/attachments/20121117/eb2a7f94/attachment.p7s>

Posted on the users mailing list.