What is the etymology of the term "top level"?

Hi, everyone.

Could someone please explain to me the etymology of the term top level? Most books and manuals define the top level as “basically the REPL for SML or Ocaml”, but I was wondering why it was called a top level in SML-like languages in the first place (my Google-fu didn’t help much, I’m afraid)?

Thanks.

I can’t give you an authoritative answer, but I assume it’s referring to the scope where the code executes. i.e. the top-level scope. I’ve also heard the “main” scope in other languages like Python or Julia referred to casually as the top level.

I think using this term to refer to the interactive interpreter is unique to the ML world, but it somehow makes sense if you think of it as the “interactive top-level scope”.

I suspect the use of that expression in ML comes from scheme. R4RS has a section entitled “Top level definitions” and refers elsewhere to the “top level environment”. It probably has older vintage than that: common lisp’s hyperspec has a definition of “top level form”, and CL is a mish-mash of earlier non-scheme lisps (lisp2s). It does not necessarily mean only the REPL: it has connotations also of the global namespace in other languages.

Edit: Because of the module system in ocaml/ML, “top level” there does I think of necessity mean the REPL or something defined at the top level of the program’s starting file.

I don’t know, but always assumed that the term came from Brian Smith’s reflective tower (see e.g. POPL 84), or the following exploration in the literature. The “top level” is the level of the tower where you run the interpreter for the language at the next level below, where you run the interpreter for the language at the level below that, and so on ad infinitum.

As @cvine mentioned, the “top level” idiom comes from Lisp. However, it predates Scheme and Common Lisp. Here is an occurence from the MACLisp reference manual (David A. Moon, 1974), section 12.1:

When LISP is at its “top level,” it continually evaluates the following form. […] which causes a “read-eval-print” loop, i.e. each S-expression that is typed in gets evaluated and the value is printed, then the next S-expression is read.

1 Like