Defining define (possibly a mad question) 
Author Message
 Defining define (possibly a mad question)

I should point out first of all that I'm a complete Scheme novice,
so apologies in advance if I'm barking up the wrong tree.

This project (a Scheme-based MUD) requires that procedures which change
the state of the world in some way (define, set! and so forth) be wrapped
to protect users from breaking the game. What I've done so far is to
start with define and set! it to a procedure of my own devising:

(set! define (lambda (x y)
                (if (protected-symbol x)
                        (display "No chance, fatso")
                        (define x y))))

or code to that effect. This works, to the extent that it successfully
binds define to my wrapper procedure which will happily redefine existing
symbols. However, if I try out my new define on an unbound symbol:

guile> (define foo 'bar)
ERROR: In expression (define foo (quote bar)):
ERROR: Unbound variable: foo
ABORT: (misc-error)

Given the requirement that the interface to 'define' must stay the same
to avoid breaking all existing code, how can I tell Scheme not to try
to evaluate the foo argument?

Or should I be doing this some other way?

JMY



Sat, 20 Oct 2001 03:00:00 GMT  
 Defining define (possibly a mad question)

Quote:
Jedi Master Yoda writes:
> This project (a Scheme-based MUD) requires that procedures which change
> the state of the world in some way (define, set! and so forth) be wrapped
> to protect users from breaking the game. What I've done so far is to
> start with define and set! it to a procedure of my own devising:
[...]
> Or should I be doing this some other way?

You should understand first that DEFINE and SET! aren't procedures
at all.  Read or reread the sections on expressions and program
structure in the Scheme language report.  (You'll find the current
R5RS somewhere at http://www.schemers.org/.)
   Then you may want to consider the module system your Scheme
provides or not, a metacircular evaluator (cf. the book by Abelson,
Sussman: Structure and Interpretation of Computer Programs), macros.

rthappe



Sat, 20 Oct 2001 03:00:00 GMT  
 Defining define (possibly a mad question)
On 04 May 1999 13:17:48 +0200, Rolf-Thomas Happe

Quote:
>You should understand first that DEFINE and SET! aren't procedures
>at all.  Read or reread the sections on expressions and program
>structure in the Scheme language report.  (You'll find the current
>R5RS somewhere at http://www.schemers.org/.)

Thanks; I re-read those sections, but I don't see what you're pointing
me at. I don't really mind how DEFINE and SET! are implemented as long
as I can provide wrappers for them.

You didn't say whether your fairly cryptic remarks were hints on how to
implement the idea I'm proposing, or hints that I'm doing this completely
the wrong way. Perhaps I should re-state what it is I'm trying to do
in general:

        Wrap a procedure call

and specifically:

        Prevent the arguments to a procedure call from being evaluated
        without changing the procedure call itself in any way

I suspect that this latter is impossible, and that therefore I should
try something else; any comments would be useful.

Quote:
>   Then you may want to consider the module system your Scheme
>provides or not, a metacircular evaluator (cf. the book by Abelson,
>Sussman: Structure and Interpretation of Computer Programs), macros.

I really appreciate you taking the time to help me; if only I had the
knowledge to interpret your answers! 'Consider a metacircular evaluator'
is a little too concise for me...

JMY



Sat, 20 Oct 2001 03:00:00 GMT  
 Defining define (possibly a mad question)

Quote:
>You didn't say whether your fairly cryptic remarks were hints on how to
>implement the idea I'm proposing, or hints that I'm doing this completely
>the wrong way. Perhaps I should re-state what it is I'm trying to do
>in general:

>    Wrap a procedure call

>and specifically:

>    Prevent the arguments to a procedure call from being evaluated
>    without changing the procedure call itself in any way

>I suspect that this latter is impossible, and that therefore I should
>try something else; any comments would be useful.

I would have thought that "Jedi Master Yoda" would not need any
assistance in these matters.

What the other respondent was trying to say is that define and set!
are not procedures in Scheme. They are "special forms," which don't
follow the same rules as procedures do. In order to accomplish what
you want, you have to either create your own special forms (using
macros), or else provide your own special-purpose interpreter inside
of Scheme that handles a language that is sort of like Scheme but has
a few modifications to prevent define from doing what it normally
does, etc. (this is what the suggestion about metacircular evaluators
is about).

My guess is that using macros would be easier in your case, but just
about every Scheme implementation has a different macro
implementation, so you're going to have to look at the documentation
for your particular flavor of Scheme to see how to use macros with it.

-Steve



Sat, 20 Oct 2001 03:00:00 GMT  
 Defining define (possibly a mad question)

saw fit to expound:

Quote:

>I would have thought that "Jedi Master Yoda" would not need any
>assistance in these matters.

Sadly, SICP and R5RS are not yet on the Jedi Studies reading list.

Quote:
>What the other respondent was trying to say is that define and set!
>are not procedures in Scheme. They are "special forms," which don't
>follow the same rules as procedures do. In order to accomplish what
>you want, you have to either create your own special forms (using
>macros), or else provide your own special-purpose interpreter inside
>of Scheme that handles a language that is sort of like Scheme but has
>a few modifications to prevent define from doing what it normally
>does, etc. (this is what the suggestion about metacircular evaluators
>is about).

I get it!

So if I were to redefine DEFINE as a macro instead of a procedure,
Scheme would not complain about unbound arguments? I'll try that.

Muchas gracias,
JMY



Sat, 20 Oct 2001 03:00:00 GMT  
 Defining define (possibly a mad question)

Quote:

>saw fit to expound:

>>I would have thought that "Jedi Master Yoda" would not need any
>>assistance in these matters.

>Sadly, SICP and R5RS are not yet on the Jedi Studies reading list.

>>What the other respondent was trying to say is that define and set!
>>are not procedures in Scheme. They are "special forms," which don't
>>follow the same rules as procedures do. In order to accomplish what
>>you want, you have to either create your own special forms (using
>>macros), or else provide your own special-purpose interpreter inside
>>of Scheme that handles a language that is sort of like Scheme but has
>>a few modifications to prevent define from doing what it normally
>>does, etc. (this is what the suggestion about metacircular evaluators
>>is about).

>I get it!

>So if I were to redefine DEFINE as a macro instead of a procedure,
>Scheme would not complain about unbound arguments? I'll try that.

>Muchas gracias,
>JMY

You clearly need a command interpreter to run your project. It doesn't take
a lot to build such a thing. Basically, you're replacing the READ/EVAL/PRINT
loop with one of your own devising, and it is almost as simple as:

(define (mud-repl)
    (let ((thing (read)))
      (if (mud-safe? thing)
          (display (eval thing))
          (display "Unsafe mud object"))
      (mud-repl)))

If you don't have some sort of command interpreter interface to the MUD
clients, you'll really not be able to prevent people from redefining DEFINE.



Sat, 20 Oct 2001 03:00:00 GMT  
 Defining define (possibly a mad question)


Quote:
>(set! define (lambda (x y)
>            (if (protected-symbol x)
>                    (display "No chance, fatso")
>                    (define x y))))

This has three problems.
1. The argument x will be evaluated, you don't want that.
3. This is recursive and will create an infinite loop if the symbol isn't
   protected.
2. Even if the "(define x y)" would have used the original define, is
  inside a procedure and will create a local binding.

If you redefine "define" to a macro instead, problem 1 and 3 can be
solved. To solve problem 2, you have to save the original "define"
somehow.

--
/****************************************************************\
* You have just read a message from Mikael St?ldal.              *
*                                                                *
* Remove "-ingen-reklam" from the address before mail replying.  *
\****************************************************************/



Mon, 22 Oct 2001 03:00:00 GMT  
 Defining define (possibly a mad question)

Quote:
>If you redefine "define" to a macro instead, problem 1 and 3 can be
>solved. To solve problem 2, you have to save the original "define"
>somehow.

It seems like continuations ought to be some help here, but I'm not sure
exactly how.

JMY



Mon, 22 Oct 2001 03:00:00 GMT  
 Defining define (possibly a mad question)

Quote:


> >If you redefine "define" to a macro instead, problem 1 and 3 can be
> >solved. To solve problem 2, you have to save the original "define"
> >somehow.

> It seems like continuations ought to be some help here, but I'm not sure
> exactly how.

Not really.

You are trying to redefine 'define' and 'set!' which are syntactic
constructs, and they happen to be part of the core language.

The ability to redefine these construct varies among Scheme
implementations.  More importantly, the impact of redefining
'define' and 'set!' is immense: you are changing the language
itself by redefining the meaning of fundamental constructs.

What you really need is a Scheme-like language for your MUD
client.  This is usually called a meta-circular evaluator.

The "Structure and Interpretation of Computer Programs" should
help.

Good luck,
--



Mon, 22 Oct 2001 03:00:00 GMT  
 Defining define (possibly a mad question)

saw fit to expound:

Quote:
>More importantly, the impact of redefining
>'define' and 'set!' is immense: you are changing the language
>itself by redefining the meaning of fundamental constructs.

Well, I don't want to _change_ anything - that's exactly the
point. Everything has to work exactly as it does in vanilla Scheme
(because part of the point of this is to produce a tool for teaching
Scheme). I just want to add sufficient security to the Scheme environment
that two or more users can share it at once without accidentally
redefining each other's symbols.

It may be that a simpler way to achieve the same result would be to
define a macro COOKED-DEFINE which implements the symbol protection
scheme, and simply transform all input code accordingly. The user program

(define foo 'bar)

would be munged by my parser and presented to the evaluator as

(cooked-define foo 'bar)

Any thoughts on this?

Quote:
>What you really need is a Scheme-like language

Yes - it's exactly the same as Scheme but adding symbol protection.

Quote:
> for your MUD
>client.

Server. Boy, do I wish I was writing a client instead.

Quote:
>The "Structure and Interpretation of Computer Programs" should
>help.

It's on my birthday list.

Thanks for all your help so far, and for putting up with my appalling
ignorance. I'm sure there'll be plenty more insufferable questions
to come...

JMY



Tue, 23 Oct 2001 03:00:00 GMT  
 Defining define (possibly a mad question)
You might well decide to associate a continuation of
some read-eval loop for processing, but this is a
different facet of your project.

Some Scheme implementations provide a way for evaluating
in a `safe' environment.  A read-eval loop in a safe
environment might be useful.  Only actions with global
consequences need to be checked; the user is free to
do as he will with his local environment.




Tue, 23 Oct 2001 03:00:00 GMT  
 Defining define (possibly a mad question)

Quote:

> >More importantly, the impact of redefining
> >'define' and 'set!' is immense: you are changing the language
> >itself by redefining the meaning of fundamental constructs.

>... Everything has to work exactly as it does in vanilla Scheme
> (because part of the point of this is to produce a tool for teaching
> Scheme).

> I just want to add sufficient security to the Scheme environment
> that two or more users can share it at once without accidentally
> redefining each other's symbols.

How about set-car!, set-cdr! and various other mutators?  These
are procedures not macros.

Quote:
> It may be that a simpler way to achieve the same result would be to
> define a macro COOKED-DEFINE which implements the symbol protection
> scheme, and simply transform all input code accordingly. The user program

> (define foo 'bar)

> would be munged by my parser and presented to the evaluator as

> (cooked-define foo 'bar)

> Any thoughts on this?

It is not clear to me what level of protection you want.
If you exclude 'eval', it's relatively simple to write
program transformations that will protect a finite set
of symbols.

If you want to protect the data objects accessible through
the protected symbols, you have some serious thinking to
do.

Example:
        (define my-own-pair-mangler (lambda (p) (set-car! p p) (set-cdr! p p)))

        (let ((x protected-symbol-foo)) (my-own-pair-mangler x))
--



Tue, 23 Oct 2001 03:00:00 GMT  
 Defining define (possibly a mad question)


Quote:
>On 04 May 1999 13:17:48 +0200, Rolf-Thomas Happe

...
>    Prevent the arguments to a procedure call from being evaluated
>    without changing the procedure call itself in any way

>I suspect that this latter is impossible, and that therefore I should
>try something else; any comments would be useful.

>>   Then you may want to consider the module system your Scheme
>>provides or not, a metacircular evaluator (cf. the book by Abelson,
>>Sussman: Structure and Interpretation of Computer Programs), macros.

>I really appreciate you taking the time to help me; if only I had the
>knowledge to interpret your answers! 'Consider a metacircular evaluator'
>is a little too concise for me...

a metacircular evaluator would be your own implementation of a scheme-like
language that has the procedure-wrapping behavior you want.  your evaluator
would be used to evaluate the scheme-like program (represented in list
form).

what makes it a "metacircular" evaluator is that you would implement it in
scheme and rely on scheme's support for symbol processing to simplify
your evaluator program.  you interpret (or translate) the scheme-like
program, looking for the special places where this procedure-wrapping feature
is needed.  for all other scheme-like constructs, you just shove it down
into the scheme system to evaluate it.

though they often get much more complicated, these types of evaluators are
one of the main draws of scheme for the programming language community.
people use scheme to implement their own pet research languages.

reading the above-mentioned SICP book chapters will provide much more detail
to give you a better sense of what is involved.

Quote:
>JMY

karl


Wed, 24 Oct 2001 03:00:00 GMT  
 Defining define (possibly a mad question)

Quote:

> what makes it a "metacircular" evaluator is that you would implement it in
> scheme and rely on scheme's support for symbol processing to simplify
> your evaluator program.  you interpret (or translate) the scheme-like
> program, looking for the special places where this procedure-wrapping feature
> is needed.  for all other scheme-like constructs, you just shove it down
> into the scheme system to evaluate it.

So, what's the difference between a metacircular evaluator and a
circular evaluator?  Would a circular evaluator wrap all procedures
without exception?

Just wondering where the name comes from.



Thu, 25 Oct 2001 03:00:00 GMT  
 Defining define (possibly a mad question)
On 09 May 1999 18:02:02 +0300, Kalle Olavi Niemitalo

Quote:

>So, what's the difference between a metacircular evaluator and a
>circular evaluator?  Would a circular evaluator wrap all procedures
>without exception?

A circular evaluator would be capable only of evaluating itself. That
is, the only program it could evaluate would be the program that
describes its own operation.

The "meta-" prefix is used because the evaluator goes a step beyond
that--it is capable of interpreting any programs expressed in the same
_language_ that it itself is expressed in.

-Steve



Thu, 25 Oct 2001 03:00:00 GMT  
 
 [ 17 post ]  Go to page: [1] [2]

 Relevant Pages 

1. &whole, defsetf, define-setf-method, define-setf-expansions, &environment and other Lispy Questions

2. Can TPB be defined outside defining modules?

3. shadows of define and define-syntax

4. define in define-syntax

5. define-macro -> define-syntax

6. is define-syntax inside define-syntax OK?

7. define-macro vs define-syntax

8. Define in a define

9. Scheme macro source: define/keyed, for defining keyword-triggered arguments

10. Macros defining macros with define-syntax

11. Simple persistent object storage: seeking advice on storing user defined, runtime defined abstract datatypes into Oracle

12. #define python-list@python.org '#define'

 

 
Powered by phpBB® Forum Software