4
$\begingroup$

.NET expression trees can be used to write self-modifying compiled programs -- because it is possible to construct and compile a method at runtime, it is also possible for a method to rewrite its own code at runtime.

.NET leverages this for dynamic typing in C#, taking advantage of the fact that even for dynamically-typed code, the vast majority of usages at a given call site will have the same types. Each dynamic call site has a corresponding expression tree, and a corresponding method. Every time a new set of types is encountered at this call site, additional logic which handles the new types will be added to the expression tree, which is then compiled into a new method; this new method becomes the method corresponding to this call site.

Aside from the fact that I haven't been able to see this mechanism running live, I feel it's too complex and distracting from the simplicity of the primary concept.

What relatively simple use case could I use to demonstrate the value of this property, of allowing the writing of a self-modifying program? Is there a standard example?

$\endgroup$
2
  • $\begingroup$ If you have to struggle to come up with a relatable, practical usage example, why teach the concept at all? $\endgroup$
    – ggorlen
    May 8 at 18:31
  • $\begingroup$ @ggorlen My goal here is to teach .NET expression trees, which are types and objects that model pieces of programs. Expression trees have two use cases: 1. translating various programming operations for use in another environment, and 2. enabling dynamic programming languages in a statically typed world. Self-modifying code is part and parcel of 2. $\endgroup$
    – Zev Spitz
    May 9 at 5:07
3
$\begingroup$

I haven't tried to build this but it seems to have the right characteristics

The Animal Game

In theory, new animals could be captured with new classes, extending the known list.

The user thinks of an animal and the program asked a series of yes-no questions to "guess" the animal: "Does it have antlers?" etc. Depending on the answer, it asks a different question. The structure of the run-time is a tree with concrete values at the leaves: "Is it a moose?".

If the machine "guesses" wrong, the user then supplies a question that would distinguish the guess from the animal chosen: "What question would distinguish an antelope from a moose?". Then the game restarts, remembering the new animal and where it fits in the hierarchy.

$\endgroup$
4
  • $\begingroup$ I still don't know what a di-monarchial caste colony is (from one of the questions). :) But this looks excellent, particularly as it neatly parallels the dynamic type handling I described in the question. $\endgroup$
    – Zev Spitz
    Mar 15 at 15:46
  • $\begingroup$ Since users supply the new "animals", some of them will just toy with the system. $\endgroup$
    – Buffy
    Mar 15 at 15:56
  • 1
    $\begingroup$ This was an early 1980's game on an Apple-II. But I think it just used a text file to store the Q's (but Apple-II's allowed self-modifying code, mostly as copy-protection). $\endgroup$ Mar 21 at 4:41
  • $\begingroup$ @OwenReynolds, it has a lot of possible implementations, including one using SQL in a database course. Another might use a spreadsheet. $\endgroup$
    – Buffy
    Mar 21 at 11:23
5
$\begingroup$

I didn't write any myself, but I encountered several self-modifying MS-DOS batch files (this is possible due to the OS closing the batch file before executing each line), especially during the $1980$'s. I'm not sure if batch files fit within your definition of "programs", or if this usage was from too long ago since the needs & reasons for this (i.e., primarily due to only a minimal amount of memory being available) don't generally apply now to modern computers. Nonetheless, having batch files modify themselves is a relatively simple concept, and was similarly done for fairly basic reasons as well, so this might make good examples for teaching purposes.

I recall self-modifying batch files were used in several situations, but the main one was to change options based on user feedback. Wikipedia's Self-modifying code gives an example where a batch file runs a program that changes the file itself to next run what the user requested. However, I also recall seeing & using a batch file with a fairly complicated (at least for a batch file) menu type system, with the commands directly modifying the file itself based on user feedback, return codes from the menu and/or other programs which were run. I no longer have a copy of this batch, or any other related, files. As this was over $30$ years ago, I don't recall any other details. Also, I haven't been able to find any such similar example online. Nonetheless, in Changing a batch file when its running, several answers, such as the answer by dbenham and the answer by jeb, give other examples of direct self-modifying batch files.

Note that, as the Wikipedia article section states, these batch file self-modifications were done primarily due to how limited memory generally was in personal computers back then. Most, if not all, of the other reasons people wrote & used self-modifying batch files back then, as I recall, were also due to these significant memory limitations.

$\endgroup$
0
$\begingroup$

On today's computers, in today's environment, it won't work. Program memory is out of bounds for writing, data memory is out of bounds for execution, both for very good reasons: They have been vehicles for grievous security failures.

It is hard enough to write a program, harder still to write a program that in turn writes a program. Getting it to reliably change itself is insane.

Teaching people to do this serves no purpose I can see.

$\endgroup$
1
  • 1
    $\begingroup$ I'm not asking about allowing a program to self-modify itself in a completely arbitrary way; I'm asking about using it as a technique for solving a specific problem for which there isn't enough information at the time the program is written. It's possible to establish -- and guard against -- invalid operations, just like it's possible to establish similar limits for other program operations. $\endgroup$
    – Zev Spitz
    May 4 at 5:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.