Glenn Reid wrote a PostScript partial evaluator in PostScript that optimized other PostScript drawning programs, called "The Distillery". You would send still.ps to your PostScript printer, and then send another PostScript file that drew something to the printer. The first PostScript Distillery program would then partially evaluate the second PostScript drawing program, and send back a third PostScript program, an optimized drawing program, with all the loops and conditionals unrolled, calculations and transformations pre-computed, all in the same coordinate system.

It was originally John Warnock's idea, that Glenn implemented. And it led to Adobe Acrobat's "Distiller". Acrobat is basically PostScript without the programming language.

No, you could not make it optimize itself by sending it to a PostScript printer two times in a row. It was not magic: all it did was intercept and capture the side-effects of the PostScript drawing commands (fill, stroke, show), read out the path, and optimize it in a uniform coordinate system. Since it didn't do any drawing, so it would just output an empty program if run on itself. (Take that, halting problem!)

https://donhopkins.com/home/archive/postscript/newerstill.ps...

>From: greid@adobe.com (Glenn Reid)
Newsgroups: comp.lang.postscript
Subject: release 10 of the Distillery
Date: 10 Mar 89 10:21:52 GMT

>Here is another release of the PostScript Language Distillery. I know
it's not terribly long after the last release, but there are some
significant enhancements, and I thought it would be worthwhile.

>I finally took a closer look at user-defined fonts, which now seem to
be working fairly well. In particular, it seems to handle the
Macintosh screen bitmap fonts that get used if the native font is
unavailable when the print file is genreated. The entire user-defined
font is reverse-engineered to the output file as it stands, and is used
exactly like the original file used it. I also fixed some rotate text
bugs, rotated charpath, and a few other things.

>I want to emphasize that probably the two best uses of this program,
currently, are to do speed comparisons of various PostScript language
drivers and to convert "non-conforming" files into "conforming" files.
It is not particularly well suited to carefully hand-written programs,
especially not those which use looping constructs. It works (usually),
but it unrolls the loops and makes the files much bigger.

It's also possible to simply write a metacircular PostScript evaluator in PostScript:

https://donhopkins.com/home/archive/NeWS/ps.ps

https://donhopkins.medium.com/the-shape-of-psiber-space-octo...

>Printing Distilled PostScript

>The data structure displays (including those of the Pseudo Scientific Visualizer, described below) can be printed on a PostScript printer by capturing the drawing commands in a file.

>Glenn Reid’s “Distillery” program is a PostScript optimizer, that executes a page description, and (in most cases) produces another smaller, more efficient PostScript program, that prints the same image. [Reid, The Distillery] The trick is to redefine the path consuming operators, like fill, stroke, and show, so they write out the path in device space, and incremental changes to the graphics state. Even though the program that computes the display may be quite complicated, the distilled graphical output is very simple and low level, with all the loops unrolled.

>The NeWS distillery uses the same basic technique as Glenn Reid’s Distillery, but it is much simpler, does not optimize as much, and is not as complete.

>The Metacircular Postscript Interpreter

>A program that interprets the language it is written in is said to be “metacircular”. [Abelson, Structure and Interpretation of Computer Programs] Since PostScript, like Scheme, is a simple yet powerful language, with procedures as first class data structures, implementing “ps.ps", a metacircular PostScript interpreter, turned out to be straightforward (or drawrofthgiarts, with respect to the syntax). A metacircular PostScript interpreter should be compatible with the "exec" operator (modulo bugs and limitations). Some of the key ideas came from Crispin Goswell's PostScript implementation. [Goswell, An Implementation of PostScript]

>The metacircular interpreter can be used as a debugging tool, to trace and single step through the execution of PostScript instructions. It calls a trace function before each instruction, that you can redefine to trace the execution in any way. One useful trace function animates the graphical stack on the PSIBER Space Deck step by step.

>The meta-execution stack is a PostScript array, into which the metacircular interpreter pushes continuations for control structures. (forall, loop, stopped, etc…) A continuation is represented as a dictionary in which the state needed by the control structure is stored (plus some other information to help with debugging).

>It is written in such a way that it can interpret itself: It has its own meta-execution stack to store the program’s state, and it stashes its own state on the execution stack of the interpreter that’s interpreting it, so the meta-interpreter’s state does not get in the way of the program it’s interpreting.

>It is possible to experiment with modifications and extensions to PostScript, by revectoring functions and operators, and modifying the metacircular interpreter.

>The metacircular interpreter can serve as a basis for PostScript algorithm animation. One very simple animation is a two dimensional plot of the operand stack depth (x), against the execution stack depth (y), over time.