Read–eval–print loop

A read–eval–print loop (REPL), also known as an interactive toplevel, is a simple, interactive computer programming environment. The term is most usually used to refer to a Lisp interactive environment, but can be applied to command line shells and similar environments for F#, Smalltalk, Standard ML, Perl, Prolog, Scala, Python, Ruby, Haskell, APL, BASIC, J, Tcl, and other languages as well.

In a REPL, the user may enter expressions, which are then evaluated, and the results displayed. The name read–eval–print loop comes from the names of the Lisp primitive functions which implement this functionality:

Because the print function outputs in the same textual format that the read function uses for input, most results are printed in a form that could (if it's useful) be copied and pasted back into the REPL. However, it's sometimes necessary to print representations of opaque data elements that can't sensibly be read back in, such as a socket handle or a complex class instance. In these cases, there must exist a syntax for unreadable objects. In Python, it's the <__module__.class instance> notation, and in Common Lisp, the #<whatever> form. The REPL of CLIM, SLIME, and the Symbolics Lisp Machine can also read back unreadable objects. They record for each output which object was printed. Later when the code is read back, the object will be retrieved from the printed output.

Contents

Advantages

A REPL can become an essential part of learning a new language as it gives quick feedback to the novice. Many tool-suites as well as programming languages use a REPL to allow algorithm exploration and debug, such as MATLAB, SciPy and IPython. The doctest module of the Python programming language allows tests to be easily generated from the captured output of its REPL command line shell.

Implementation

To implement a Lisp REPL, it is necessary only to implement these three functions and an infinite-loop function. (Naturally, the implementation of eval will be complicated, since it must also implement all the primitive functions like car and + and special operators like if.) This done, a basic REPL itself is but a single line of code: (loop (print (eval (read)))).

One possible implementation of eval is as a recursive interpreter that acts on the syntax tree created by read. Another possibility is to compile the syntax tree into machine code and execute it.

Real REPL implementations in Lisp are often much more complicated. Typical functionality provided by a Lisp REPL includes:

Major language environments and associated REPLs

Applications with an REPL

External links