Discovering the "Core" Entities and Macros of Common Lisp
Asked Answered
B

2

5

While reading Peter Seibel's "Practical Common Lisp", I learned that aside from the core parts of the language like list processing and evaluating, there are macros like loop, do, etc that were written using those core constructs.

My question is two-fold. First is what exactly is the "core" of Lisp? What is the bare minimum from which other things can be re-created, if needed? The second part is where can one look at the code of the macros which come as part of Common Lisp, but were actually written in Lisp? As a side question, when one writes a Lisp implementation, in what language does he do it?

Buber answered 27/10, 2017 at 16:30 Comment(1)
You can learn just that in books, like in the popular "SICP Structure and Interpretation of Computer Programs", that I also liked. Some lisp books: communitypicks.com/r/lispSoilure
C
5

what exactly is the "core" of Lisp? What is the bare minimum from which other things can be re-created, if needed?

The minimum set of syntactic operators were called "special forms" in CLtL. The term was renamed to "special operators" in ANSI CL. There are 24 of them. This is well explained in CLtL section "Special Forms". In ANSI CL they are 25.

where can one look at the code of the macros which come as part of Common Lisp, but were actually written in Lisp?

Many Common Lisp implementations are free software (list); you can look at their source code. For example, here for SBCL, here for GNU clisp, etc.

when one writes a Lisp implementation, in what language does he do it?

Usually a Lisp implementation consists of

  • a lower-level part, written in a system programming language. This part includes the implementation of the function call mechanism and of the runtime part of the 24 special forms. And
  • a higher-level part, for which Lisp itself is used because it would be too tedious to write everything in the system programming language. This usually includes the macros and the compiler.

The choice of the system programming language depends. For implementations that are built on top of the Java VM, for example, the natural choice is Java. For implementations that include their own memory management, it is often C, or some Lisp extensions with similar semantics than C (i.e. where you have fixed-width integer types, explicit pointers etc.).

Cancer answered 28/10, 2017 at 1:46 Comment(0)
A
3

First is what exactly is the "core" of Lisp? What is the bare minimum from which other things can be re-created, if needed?

Most Lisps have a core of primitive constructs, which is usually written in C (or maybe assembly). The usual reason for choosing those languages is performance. The bare minimum from which other things can be re-created depends on how bare-minimum you want to go. That is to say, you don't need much to be Turing-complete. You really only need lambdas for your language to have a bare minimum, from which other things can be created. Though, typically, people also include defmacro, cond, defun, etc. Those things aren't strictly necessary, but are probably what you mean by "bare minimum" and what people usually include as primitive language constructs.

The second part is where can one look at the code of the macros which come as part of Common Lisp, but were actually written in Lisp?

Typically, you look in in the Lisp sources of the language. Sometimes, though, your macro is not a genuine macro and is a primitive language construct. For such things, you may also need to look in the C sources to see how these really primitive things are implemented.

Of course, if your Lisp implementation is not open-source, you need to disassemble its binary files and look at them piece-by-piece in order to understand how primitives are implemented.

As a side question, when one writes a Lisp implementation, in what language does he do it?

As I said above, C is a common choice, and assembly used to be more common. Though, there are Lisps written in high-level languages like Ruby, Python, Haskell, and even Lisp itself. The trade-off here is performance vs. readability and comprehensibility.

If you want a more-or-less canonical example of a Lisp to look at which is totally open-source, check out Emacs' source code. Of course, this isn't Common Lisp, although there is a cl package in the Emacs core which implements a fairly large subset of Common Lisp.

Arlinearlington answered 27/10, 2017 at 16:51 Comment(3)
'even Lisp itself' -> I would guess that the majority of Lisp implementations has most of their code implemented in Lisp. Typically a Lisp compiler will be implemented in Lisp and create C or machine code.Tica
I'd suggest not looking at elisp. Instead look at one of the open source CLs, or a Scheme-derivative or, really, the sources for an implementation of any language which isn't as antique and idiosyncratic as elisp.Nazareth
@tfb I think Emacs Lisp's disadvantages are somewhat exaggerated, but looking at other Lisps as well certainly isn't a bad idea. I'd suggest Racket, Chicken, or Gambit for Scheme-derivatives. I'd also recommend CCL or SBCL for Common Lisps.Arlinearlington

© 2022 - 2024 — McMap. All rights reserved.