Search code examples
lisp

The concept of Self-Hosting


So I'm developing a small programming language, and am trying to grasp around the concept of "Self-Hosting".

Wikipedia states:

The first self-hosting compiler (excluding assemblers) was written for Lisp by Hart and Levin at MIT in 1962. They wrote a Lisp compiler in Lisp, testing it inside an existing Lisp interpreter. Once they had improved the compiler to the point where it could compile its own source code, it was self-hosting.

From this, I understand that someone had a Lisp interpreter, (lets say in Python).
The Python program then reads a Lisp program which in turn can also read Lisp programs.

By the term, "Self-Hosting", this surely can't mean the Python program can cease to be of use, because removing that would remove the ability to run the Lisp program which reads other Lisp programs!

So by this, how does a program become able to host itself directly on the OS? Maybe I'm just not understanding it correctly.


Solution

  • In this case, the term self-hosting applies to the Lisp compiler they wrote, not the interpreter.

    The Python Lisp interpreter (as in your example) would take Lisp source as input, and execute it directly.

    The Lisp compiler (written in lisp) can take any Lisp source as input and generate a native machine binary[1] as output (which could then run without an interpreter).

    With those two pieces, eliminating Python becomes feasible. The process would go as follows:

    python.exe lispinterpret.py lispcompiler.lisp -i lispcompiler.lisp -o lispcompiler.exe
    

    We ask Python to interpret a lisp program from source (lispcompiler.lisp), and we pass lispcompiler.lisp itself as input. lispcompiler.lisp then outputs lispcompiler.exe as output, which is a native machine binary (and doesn't depend on Python).

    The next time you want to compile the compiler, the command is:

    lispcompiler.exe -i lispcompiler.lisp -o lispcompiler2.exe
    

    And you will have a new compiler without the use of Python.

    [1] Or you could generate assembly code, which is passed to an assembler.