1

Emacs starts up as an editor (which probably has m functions that takes ninputs) with an interpreter Elisp running in the background (which can be used to change the behavior of the program - probably so much so that it is no longer emacs :-)).

Why do programs need such an extension that is an interpreter all by itself? What is the theory and extra feature that it provides at a very fundamental level so that you can make a similar decision for your own project?

Assuming that this is how a (linux) program is in memory,

Program in memory

is it because without an interpreter (lying in the text segment), your program is just a finite machine that can execute a finite set of instructions in the text segment (real code a.k.a machine instructions) present in the program layout? However, when you add something like an interpreter, you can add new instructions (probably in the heap, because data and instruction, both are just bits?) and make it behave like an infinite machine?

I think it is the same as asking why do you need interpreter in the first place(!), but my question actually came from this specific scenario in Emacs like editors. So I would like to understand this from both perspectives.

  • 6
    Some people want turing-complete text editors... – Caleth Jan 3 at 11:46
  • 2
    Your picture is really naive (and actually wrong). Run cat /proc/$$/maps and cat /proc/$(pidof emacs)/maps; see also proc(5), elf(5), execve(2) – Basile Starynkevitch Jan 3 at 14:29
  • 2
    If you want some theory, read about partial evaluation and Turing completeness. I believe your question is too vague and too broad. – Basile Starynkevitch Jan 3 at 14:31
  • 1
    Note that you don't need to have machine instructions in "data" to have unbounded possibilities, given that most CPUs have instructions like "Jump to the address stored in this value", consider that "this value" may have been populated from user input. – Caleth Jan 3 at 16:00
5

your program is just a finite machine that can execute a finite set of instructions

Stricto sensu, this is true, but not very interesting. Indeed, my desktop has only three terabytes of memory (including RAM, disk, registers, etc...). So it has "only" 2(3*72057594037927936*8) states, but viewing my desktop as such a huge finite machine is not very interesting. Observe that I can buy some more memory (e.g. add more disk).

Many physicists have a quantum-mechanical view of the entire universe, and might explain you that the universe itself is a huge finite state machine (after all, it has less that 10100 particles, each of them having a quantum state related to Planck's constant, etc...)

In practice, better look at your laptop as a Turing machine and consider that its memory is potentially "infinite".

(your question is messing different levels of abstractions)


Why do programs need such an extension that is an interpreter all by itself?

Embedding an interpreter is related to partial evaluation and Turing completeness. In practice it is very convenient (but might facilitate malware, given as "data" interpreted by your extensible application). You could embed Lua or Guile in your application (but that is a major design choice, that you need to make very early because of its architectural implications, notably related to garbage collection). You could design your application to accept plugins (see dlopen(3) and dlsym(3); and my manydl.c program shows that it could have many hundred thousands of generated plugins). It might even use JIT compilation techniques (see libgccjit, LLVM, asmjit, ...)

Read SICP, then also Scott's Programming Language Pragmatics, Queinnec's Lisp In Small Pieces, and R & A Arpaci-Dusseau's Operating Systems: Three Easy Pieces and Pitrat's blog.

Play with SBCL (a good Common Lisp implementation). It is generating (dynamically) machine code at every REPL interaction. Be aware of homoiconicity and metaprogramming and multi-stage programming. Read about Greenspun's tenth rule and Gall's law and about accidentally turing-complete things.

Since Turing (and his halting problem, see also Rice's theorem) and Gödel (and his incompleteness theorem) we are aware that code is data (and is proof) and data is code (self-reference is a related concept, see also Richard's paradox and read about Curry-Howard correspondance). Read Hofstadter's Gödel, Escher, Bach book.

BTW, your picture of the virtual address space of some Linux process is really naive (it was sometimes true in the previous century; today things are much more complex). See proc(5), elf(5), execve(2), mmap(2), ld-linux(8) and try cat /proc/$$/maps, cat /proc/self/maps, cat /proc/$(pidof emacs)/maps on your Linux system.

See also this answer to a related question (and perhaps this one).

  • Thanks for your time. I really am a beginner. What I meant was that unless I reload my program, I am just limited to the finite set of instructions that is there in the "text segment" and no more than that (i.e it is finite till next reload). If I have to overcome that, I need to have a code that converts strings to machine instructions at run rime which is what an interpreter does. I am sorry, but I do understand that this a broad question, but I thought there are simple explanations for all this! Anyway thanks for pointing out on further readings related to this. I will read up further. – Nishant Jan 3 at 14:54
  • 1
    Your comment above is still naive. I improved my answer to give more reference. – Basile Starynkevitch Jan 3 at 15:05
  • 1
    +1 for the GEB reference – Dave M Jan 4 at 0:50
9

Elisp is not embedded in Emacs. Emacs is written in Elisp. In some sense, Emacs is embedded in Elisp.

Emacs started out as a set of Editor MACroS for the TECO editor on ITS. It was then ported to Multics. However, the macro language of TECO is a terrible, terrible language, so the authors of Multics Emacs were looking for a better language to re-implement Emacs in. It turned out that the Multics port of MacLisp had much more performant function calls, and native text handling functionalities than the "standard" system programming language on Multics, PL/I. So, it was decided to implement Emacs in MacLisp.

Later, when Emacs was ported to Unix, they decided that it was easier to write a Lisp implementation and port only that Lisp implementation to new operating systems than always porting the entire Emacs system to a new Lisp implementation … especially since many of the systems they wanted to port Emacs to (like Unix) didn't even have a platform-native Lisp system as part of the OS, like earlier systems had.

Writing their own implementation also allowed them to adapt the language specifically for implementing a text editor.

Note that Emacs is really not special in this regard. Being able to control an application with a program is called "scripting" and there are many scripting languages and scripting hosts. You may have heard of a language called JavaScript that was originally created to script browsers and web servers. Lua is a language that is specifically designed as a scripting language and it is used in e.g. World of Warcraft and Adobe Lightroom. AutoCAD has its own scripting language called AutoLisp. Windows has PowerShell, Unix has the POSIX shell, macOS has AppleScript.

5

Because with a fully compiled tool, what it can do is limited to what the distributor thinks is useful.

Sure, in theory everyone can write their own extensions in C, recompile and happily use them. But in practice, compiling and reinstalling a tool takes time and effort, and is in fact nigh-impossible for most people who just get their software from a distributor or, worse, from an app store. So the difference between writing an extension function in Lisp and in C is the difference between some minutes of effort and hours, days or weeks of effort or even impossibility - nobody wants to wait until Apple has decided that a new version of a tool can be allowed into their sacred halls, just to use a labot-saving function that they need now.

And why do we need personalizable extension functions? We don't - I can program perfectly fine in most IDEs even though their Emacs "emulation" is ridiculously less powerful then the real thing. But I will be slower and much less happy doing it, and the one thing you don't want to do to expensive specialists like software developers is to make them less efficient. Learning ELisp is a challenge, but if you work with text in any serious way, it pays for itself literally a million times over.

  • Thanks! So the ability to mutate a program without modifying the text segment is actually what you are saying right? Unless you are a nerd, that is something that would be done by the package creator - no? ( I wanted to connect the answer to the memory layout of the program so that is more clear. ) – Nishant Jan 3 at 12:14
  • 1
    @Nishant Yes, any code that you add in the interpreted language ends up as dynamically allocated memory, i.e. in the data segment. – Kilian Foth Jan 3 at 12:42
  • 2
    @Nishant: the goal is to allow users of a certain program to add programmatic extensions, independently from the vendor. An interpreter is not the only possible solution for this. For example, Adobe Photoshop has a script interpreter, but also a plugin mechanism. MS Excel (as well as other programs from the Office suite) has a VBA interpreter, but also a COM API and a plugin API to allow programmatic extensions written in arbitrary interpreted or compiled languages. – Doc Brown Jan 3 at 15:53
  • 2
    ... Opposed to this, if a program's vendor does not want to provide this kind of extensibility to their users, the vendor could hypothetically implement every feature in "the native language" in which the core program was written. But the answer above (as well as Jörg W Mittag's) answer give some possible reasons why even then providing an interpreter - internally, for vendor extensions exclusively - can make sense (like portability or simplicity of adding new features). – Doc Brown Jan 3 at 16:27
5

Why do we need to embed an interpreter in a program?

Perhaps the simplest answer is that it makes it easy to extend the program to do things it wasn't originally designed to do, and without having to rebuild the program to do it.

There are countless examples. One of the most obvious is the web browser. It has an embedded javascript interpreter. Without it, all we would be able to see are static web pages. With it, however, developers around the world are able to do things that the original web browsers without an embedded language could never do.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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