[racket] procedure to string
There are more complex closures, that share an external variable. For example:
;-----
#lang racket
(define-values (up! down!)
(let ()
(define counter 0)
(define (up!) (set! counter (add1 counter)) counter)
(define (down!) (set! counter (sub1 counter)) counter)
(values up! down!)))
(up!) ;==> 1
(up!) ;==> 2
(down!) ;==> 1
;-----
If you need to send “up!” and “down!” in different interactions but
they nevertheless have to modify the same counter then this problem is
very difficult.
For some cases, I guess that you can uses something similar to the
“Stateless Servlets”
http://docs.racket-lang.org/web-server/stateless.html#(part._considerations)
. I didn’t use it, but it can serialize and deserialize functions and
continuations. I don’t know how much transparent are the structures it
use, so I’m not sure if it’s possible to inspect them. I’m not sure
that you can deserialize the continuation in another server, but you
can deserialize it after a server reboot, so I guess it’s possible.
(And as Neil Toronto said, you should also use sandboxing to avoid
unexpected problems.)
Gustavo
On Fri, Jan 3, 2014 at 5:10 PM, Neil Toronto <neil.toronto at gmail.com> wrote:
> What kind of inspection?
>
> If it's for security purposes, you're better off running the procedures in a
> sandbox (see the `racket/sandbox' module). You can even control the amount
> of time something is allowed to run. Also, `serial-lambda' might be
> something you could use, if the sender and receiver are both running the
> same program. (It might work in other circumstances; I don't know.)
>
> Neil ⊥
>
>
> On 01/02/2014 09:41 AM, Alegria Baquero wrote:
>>
>> Thank you for your kind responses. Your solutions are good ones but
>> unfortunately don't fit my purpose. Imagine a mobile code scenario where
>> you have no control of the definition of a closure. What I want to do is
>> to be able to print the body of any incoming closure that arrives in a
>> message. So if server A sends server B a closure (lambda () (displayln
>> "hello world")), B could grab that incoming closure and capture it as
>> the string "(lambda () (displayln "hello world"))" so it can be
>> inspected before executing it. Does this make sense? How can I then
>> construct a function procedure->string that transforms at runtime
>> (without using macros) a closure's body to a string?
>>
>> Thank you again and happy new year.
>>
>> Alegria
>>
>>
>> On Tue, Dec 31, 2013 at 10:53 AM, Alexander D. Knauth
>> <alexander at knauth.org <mailto:alexander at knauth.org>> wrote:
>>
>> Yes, you can do it with a struct with the property prop:procedure.
>>
>> #lang racket
>>
>> (require rackunit)
>>
>> (struct my-proc (proc str)
>> #:property prop:procedure (struct-field-index proc))
>>
>> (define f
>> (my-proc (lambda (x) (+ x 1))
>> "(lambda (x) (+ x 1))"))
>>
>> (check-true (procedure? f))
>> (check-equal? (f 1) 2)
>> (check-equal? (my-proc-str f)
>> "(lambda (x) (+ x 1))")
>>
>> (define-syntax-rule (my-lambda args body ...)
>> (my-proc (lambda args body ...)
>> (substring (~v '(lambda args body ...)) 1)))
>>
>> (define f2
>> (my-lambda (x) (+ x 1)))
>>
>> (check-true (procedure? f2))
>> (check-equal? (f2 1) 2)
>> (check-equal? (my-proc-str f2)
>> "(lambda (x) (+ x 1))")
>>
>>
>> On Dec 29, 2013, at 10:45 PM, Alegria Baquero wrote:
>>
>>> Hello,
>>>
>>> is there any way to transform a function's body to a string such
>>> as "(lambda(x)...)"?
>>>
>>> Thanks
>>>
>>> Alegria
>>> ____________________
>>> Racket Users list:
>>> http://lists.racket-lang.org/users
>>
>>
>>
>>
>>
>> --
>>
>> ---------------------------------------------------------------------------------------
>> PhD candidate
>> Department of Informatics
>> Donald Bren School of Information and Computer Sciences
>> University of California, Irvine
>>
>>
>> ____________________
>> Racket Users list:
>> http://lists.racket-lang.org/users
>>
>
> ____________________
> Racket Users list:
> http://lists.racket-lang.org/users