Hi Tom,

you wrote:
>if I may comment on this one...

You bet.

>As far as I can see/tell the point here is that the example isn't really
>SMC!

Well ...

>Sure lists is part of the code and lists is modified. BUT list is
>actually only
>data, 

I had to chuckle when I saw that example. Wickedly, dangerously
intelligent.  REBOL prides itself that its functions are first class, they
are data, nothing but data ... If someone were to say (which you just did),
that the example was not really self modifying code because it was only
data, then you would be (are) giving a go ahead for implementing self
modifying data (which includes REBOL functions).

>it is never really executed as I'd expect SMC would have to.
>That's why
>it doesn't really matter if lists is local to the function or not.

In my smf? example function I implicitly took advantage of your statement,
by using the links block (in the version ok'd by Ladislav) to have the
function treat as data (pick words random 4, pick values random 5)
functions that it subsequently evaluates using reduce (in my second example
do).

>
>If lists would contain some Rebol code and it would be 'done' somewhere

Exactly.

>I'd call
>it SMC, but even then you still could make lists global (I think). 

How does that help? Global lists can still be used to modify the function
locally. My smc? function randomly picks some functions and data from the
global words and values blocks and inserts them in the global links block
which it then evaluates using reduce.

>But
>then there
>would be a big difference considering the meaning of lists.

That I don't understand. Since words may have any values, you cannot tell
whether a block containing some words, when it is eventually reduced, will
end up dereferencing one or the other word to find that the value's
datatype is a function ;-).

>
>Any comments?

Sure. Now, for some comments ;-) We really need to define what self
modifying code is under REBOL. IMHO the distinction between code and data
doesn't work for REBOL, because functions are data. So, you have to be more
precise. 

Besides that we have the following options (and probably then some):

1. A function accesses its own code as data and modifies it. Subsequently
the words modified or added by the very same function are evaluated. I take
it that this certainly qualifies as self modifying code:

>> f: func [] [ insert tail second :f [print "hi"] ]
>> f
== []
>> f
hi
>> f
hi
hi

>> source f
f: func [][insert tail second :f [print "hi"] print "hi" print "hi" print
"hi"]

2. How is that safer from a function accessing another function as data and
then evaluating that function? Is the fact that the other function is
evaluated by the modifying function sufficient to speak of self modifying
code? Strictly speaking the function did not modify itself directly, it
modified a different function it later evaluated. But what the function
that does the modifying will ultimately do does depend to some degree on
the modifications it made to the function it later evaluates. So, self
modifying code or not:

>> f: func [] [insert second :g [print "hi"] g ]
>> g: func [] []
>> f
hi
>> f
hi
hi
>> f
hi
hi
hi
>> source f
f: func [][insert second :g [print "hi"] g]  ;- wasn't directly modified
>> source g
g: func [][print "hi" print "hi" print "hi"] ;- oh, oh.

3. How about a function that evaluates a function, which modifies a
function that the first function eventually calls. Self modifying code or not?
f: func [] [ g h ]
g: func [] [ insert tail :h [print "hi"] ]
h: func [] []

4. How about a function that selects code from another function that it
evaluates?
f: func [] [ do copy/part second :list-dir 6 ]

5. Now a function that creates a new function, by borrowing code from
another function (again list-dir):
f: func [] [
   g: make function! [dir [any-type!] ] copy/part second :list-dir 6
]

7. What about a function that does the exact same thing, but sets the word
that references this same function itself as a reference to the function it
creates. Here f is a reference to the function, which in turn sets f as a
reference to the function it creates:

f: func [] [
   f: make function! [dir [any-type!] ] copy/part second :list-dir 6
]

Since f is just a word, not a function (the function is the value
referenced by 'f), the qustion is whether this is self modifying code? The
function that creates the new function and assigns the word 'f as a
reference to the function, is not modifying itself! The function remains
the same function. 'f is just set as a reference to a different value.

Comments?

Elan

Reply via email to