A
read–eval–print loop, also known as an
interactive toplevel, is a simple, interactive
computer programmingComputer programming is the process of designing, writing, testing, debugging, and maintaining the source code of computer programs. This source code is written in one or more programming languages. The purpose of programming is to create a program that performs specific operations or exhibits a...
environment. The term is most usually used to refer to a
LispLisp is a family of computer programming languages with a long history and a distinctive, fully parenthesized syntax. Originally specified in 1958, Lisp is the second-oldest high-level programming language in widespread use today; only Fortran is older...
interactive environment, but can be applied to command line
shellsA shell is a piece of software that provides an interface for users of an operating system which provides access to the services of a kernel. However, the term is also applied very loosely to applications and may include any software that is "built around" a particular component, such as web...
and similar environments for F#,
SmalltalkSmalltalk is an object-oriented, dynamically typed, reflective programming language. Smalltalk was created as the language to underpin the "new world" of computing exemplified by "human–computer symbiosis." It was designed and created in part for educational use, more so for constructionist...
,
Standard MLStandard ML is a general-purpose, modular, functional programming language with compile-time type checking and type inference. It is popular among compiler writers and programming language researchers, as well as in the development of theorem provers.SML is a modern descendant of the ML...
,
PerlPerl is a high-level, general-purpose, interpreted, dynamic programming language. Perl was originally developed by Larry Wall in 1987 as a general-purpose Unix scripting language to make report processing easier. Since then, it has undergone many changes and revisions and become widely popular...
,
PrologProlog is a general purpose logic programming language associated with artificial intelligence and computational linguistics.Prolog has its roots in first-order logic, a formal logic, and unlike many other programming languages, Prolog is declarative: the program logic is expressed in terms of...
, Scala,
PythonPython is a general-purpose, high-level programming language whose design philosophy emphasizes code readability. Python claims to "[combine] remarkable power with very clear syntax", and its standard library is large and comprehensive...
,
RubyRuby is a dynamic, reflective, general-purpose object-oriented programming language that combines syntax inspired by Perl with Smalltalk-like features. Ruby originated in Japan during the mid-1990s and was first developed and designed by Yukihiro "Matz" Matsumoto...
,
HaskellHaskell is a standardized, general-purpose purely functional programming language, with non-strict semantics and strong static typing. It is named after logician Haskell Curry. In Haskell, "a function is a first-class citizen" of the programming language. As a functional programming language, the...
,
APLAPL is an interactive array-oriented language and integrated development environment, which is available from a number of commercial and noncommercial vendors and for most computer platforms. It is based on a mathematical notation developed by Kenneth E...
,
BASICBASIC is a family of general-purpose, high-level programming languages whose design philosophy emphasizes ease of use - the name is an acronym from Beginner's All-purpose Symbolic Instruction Code....
,
JThe J programming language, developed in the early 1990s by Kenneth E. Iverson and Roger Hui, is a synthesis of APL and the FP and FL function-level languages created by John Backus....
,
TclTcl is a scripting language created by John Ousterhout. Originally "born out of frustration", according to the author, with programmers devising their own languages intended to be embedded into applications, Tcl gained acceptance on its own...
, 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:
- The read function accepts an expression from the user, and parses
In computer science and linguistics, parsing, or, more formally, syntactic analysis, is the process of analyzing a text, made of a sequence of tokens , to determine its grammatical structure with respect to a given formal grammar...
it into a data structure in memory. For instance, the user may enter the s-expressionS-expressions or sexps are list-based data structures that represent semi-structured data. An S-expression may be a nested list of smaller S-expressions. S-expressions are probably best known for their use in the Lisp family of programming languages...
(+ 1 2 3), which is parsed into a linked listIn computer science, a linked list is a data structure consisting of a group of nodes which together represent a sequence. Under the simplest form, each node is composed of a datum and a reference to the next node in the sequence; more complex variants add additional links...
containing four data elements.
- The eval
In some programming languages, eval is a function which evaluates a string as though it were an expression and returns a result; in others, it executes multiple lines of code as though they had been included instead of the line including the eval...
function takes this internal data structure and evaluates it. In Lisp, evaluating an s-expression beginning with the name of a function means calling that function on the arguments that make up the rest of the expression. So the function + is called on the arguments 1 2 3, yielding the result 6.
- The print function takes the result yielded by eval, and prints it out to the user. If it is a complex expression, it may be pretty-printed
Prettyprint is the application of any of various stylistic formatting conventions to text, source code, markup, and other similar kinds of content. These formatting conventions usually consist of changes in positioning, spacing, color, contrast, size and similar modifications intended to make the...
to make it easier to understand. In this example, though, the number 6 does not need much formatting to print.
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
# form. The REPL of CLIM,
SLIMESLIME, the Superior Lisp Interaction Mode for Emacs, is an Emacs mode for developing Common Lisp applications. SLIME originates in an Emacs mode called SLIM written by Eric Marsden and developed as an open-source project by Luke Gorrie and Helmut Eller. Over 100 Lisp developers have contributed...
, and the
SymbolicsSymbolics refers to two companies: now-defunct computer manufacturer Symbolics, Inc., and a privately held company that acquired the assets of the former company and continues to sell and maintain the Open Genera Lisp system and the Macsyma computer algebra system.The symbolics.com domain was...
Lisp MachineLisp machines were general-purpose computers designed to efficiently run Lisp as their main software language. In a sense, they were the first commercial single-user workstations...
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.
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
MATLABMATLAB is a numerical computing environment and fourth-generation programming language. Developed by MathWorks, MATLAB allows matrix manipulations, plotting of functions and data, implementation of algorithms, creation of user interfaces, and interfacing with programs written in other languages,...
,
SciPySciPy is an open source library of algorithms and mathematical tools for the Python programming language.SciPy contains modules for optimization, linear algebra, integration, interpolation, special functions, FFT, signal and image processing, ODE solvers and other tasks common in science and...
and
IPythonIPython is an interactive shell for the Python programming language that offers enhanced introspection, additional shell syntax, tab completion and rich history.- Other features :...
. The
doctestdoctest is a module included in the Python programming language's standard library that allows the easy generation of tests based on output from the standard Python interpreter shell, cut and pasted into docstrings.-Implementation specifics:...
module of the
PythonPython is a general-purpose, high-level programming language whose design philosophy emphasizes code readability. Python claims to "[combine] remarkable power with very clear syntax", and its standard library is large and comprehensive...
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-loopAn infinite loop is a sequence of instructions in a computer program which loops endlessly, either due to the loop having no terminating condition, having one that can never be met, or one that causes the loop to start over...
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 codeMachine code or machine language is a system of impartible instructions executed directly by a computer's central processing unit. Each instruction performs a very specific task, typically either an operation on a unit of data Machine code or machine language is a system of impartible instructions...
and execute it.
Real REPL implementations in Lisp are often much more complicated. Typical functionality provided by a Lisp REPL includes:
- History of inputs and outputs.
- Variables are set for the input expressions and results. These variables are also available in the REPL. For example in Common Lisp * refers to the last result, ** and *** to the results before that.
- Levels of REPLs. In many Lisp systems if an error occurs during the reading, evaluation or printing of an expression, the system is not thrown back to the top-level with an error message. Instead a new REPL, one level deeper, is started in the error context. The user can then inspect the problem, fix it and continue - if possible. If an error occurs in such a debug REPL, another REPL, again a level deeper, is started. Often the REPL offers special debug commands.
- Error handling. The REPL provides restarts. These restarts can be used, when an error occurs to go back to a certain REPL level.
- Mouse
In computing, a mouse is a pointing device that functions by detecting two-dimensional motion relative to its supporting surface. Physically, a mouse consists of an object held under one of the user's hands, with one or more buttons...
sensitive input and output of data objects.
- Input editing and context specific completion over symbols, pathnames, class names and other objects.
- Help and documentation for commands.
- Variable
In computer programming, a variable is a symbolic name given to some known or unknown quantity or information, for the purpose of allowing the name to be used independently of the information it represents...
s are controlling the reader. For example the variable *read-base* controls in which base numbers are read by default.
- Variables are controlling the printer. Example: maximum length or maximum depth of expressions to print.
- Additional command syntax. Some REPLs have commands that follow not the s-expression syntax, but often work with Lisp data as arguments.
- Graphical REPLs. Some Lisp REPLs (the CLIM Listener is an example) accept also graphical input and output.
Major language environments and associated REPLs
- C
C is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system....
does not have a built-in REPL, but third-party REPLs exist, like c-repl and ccons.
- C++ does not have a built-in REPL, but third-party REPLs exist, like cint.
- C# does not have built-in REPL but has an implementation in Mono
Mono, pronounced , is a free and open source project led by Xamarin to create an Ecma standard compliant .NET-compatible set of tools including, among others, a C# compiler and a Common Language Runtime....
through CsharpRepl. To some extent REPL can also be imitated in LINQPadLINQPad is a free software utility for Microsoft .NET developers using LINQ. It allows developers to interactively query SQL databases and other data sources such as OData or WCF Data Services using LINQ...
free interactive utility. The Microsoft Roslyn CTP contains a C# REPL which can be accessed through Visual Studio (View > Other Windows > C# Interactive Window).
- Clojure
Clojure |closure]]") is a recent dialect of the Lisp programming language created by Rich Hickey. It is a general-purpose language supporting interactive development that encourages a functional programming style, and simplifies multithreaded programming....
, a recent Lisp dialect, provides a REPL.
- CoffeeScript
CoffeeScript is a programming language that transcompiles to JavaScript. The language adds syntactic sugar inspired by Ruby, Python and Haskell to enhance JavaScript's brevity and readability, as well as adding more sophisticated features like array comprehension and pattern matching...
provides a REPL with mutli-line input support and syntax-highlighted output (coffee).
- Common Lisp
Common Lisp, commonly abbreviated CL, is a dialect of the Lisp programming language, published in ANSI standard document ANSI INCITS 226-1994 , . From the ANSI Common Lisp standard the Common Lisp HyperSpec has been derived for use with web browsers...
, most implementations have built-in REPLs.
- Emacs Lisp
Emacs Lisp is a dialect of the Lisp programming language used by the GNU Emacs and XEmacs text editors . It is used for implementing most of the editing functionality built into Emacs, the remainder being written in C...
, the EmacsEmacs is a class of text editors, usually characterized by their extensibility. GNU Emacs has over 1,000 commands. It also allows the user to combine these commands into macros to automate work.Development began in the mid-1970s and continues actively...
editor features a REPL for elisp called ielm
- Erlang provides a REPL (erl).
- F# has a REPL in the form of a command called fsi (F# interactive).
- Frink
Frink, named after the fictional mad scientist Professor John Frink from The Simpsons, is a calculating tool and programming language designed by Alan Eliasen. It is built on the Java Virtual Machine and incorporates features similar to Java, Perl, Ruby, Smalltalk, and various BASIC implementations...
has a built-in REPL. The command-line version provides a REPL interface if no input file is given, and the default GUIs provide a REPL or more traditional programming mode.
- Groovy has a built-in REPL (http://groovy.codehaus.org/Groovy+Shell), as well as a graphical console, which can run locally or on the web (http://groovyconsole.appspot.com/).
- Haskell
Haskell is a standardized, general-purpose purely functional programming language, with non-strict semantics and strong static typing. It is named after logician Haskell Curry. In Haskell, "a function is a first-class citizen" of the programming language. As a functional programming language, the...
has at least two implementations provide REPL functionality. The Glasgow Haskell CompilerThe Glorious Glasgow Haskell Compilation System, more commonly known as the Glasgow Haskell Compiler or GHC, is an open source native code compiler for the functional programming language Haskell. The lead developers are Simon Peyton Jones and Simon Marlow...
provides ghci and the HugsHugs , also Hugs 98, is a bytecode interpreter for the functional programming language Haskell. Hugs is the successor to Gofer, and was originally derived from Gofer version 2.30b. Hugs and Gofer were originally developed by Mark P. Jones, now a professor at Portland State University.Hugs comes...
interpreter is often used by beginners as a Haskell REPL.
- Java
Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities...
does not have a built-in REPL, but has a pure Java implementation in BeanShellBeanShell is a Java scripting language, invented by Patrick Niemeyer. It runs in the Java Runtime Environment and uses Java syntax, in addition to scripting commands and syntax.- Features :...
. It can also be driven via scripting languages ported to the Java platform like JythonJython, successor of JPython, is an implementation of the Python programming language written in Java.-Overview:Jython programs can seamlessly import and use any Java class. Except for some standard modules, Jython programs use Java classes instead of Python modules...
or Groovy. There is a Java REPL implementation in DrJavaDrJava is a lightweight Java IDE designed primarily for students and beginners in Java that is actively developed and maintained by the JavaPLT group at Rice University. Its interface has been developed using Sun Microsystems' Swing toolkit and therefore it has a consistent appearance on different...
accessible from the Interactions pane and in JGrasp IDE http://www.jgrasp.org/jgrasp/interactions.htm#interactions_top.
- A JavaScript
JavaScript is a prototype-based scripting language that is dynamic, weakly typed and has first-class functions. It is a multi-paradigm language, supporting object-oriented, imperative, and functional programming styles....
REPL is provided by Node.jsNode.js is a software system designed for writing highly-scalable internet applications, notably web servers.Programs are written in JavaScript, using event-driven, asynchronous I/O to minimize overhead and maximize scalability....
and in the browser using JSConsole.
- Lua has an optional REPL.
- MATLAB
MATLAB is a numerical computing environment and fourth-generation programming language. Developed by MathWorks, MATLAB allows matrix manipulations, plotting of functions and data, implementation of algorithms, creation of user interfaces, and interfacing with programs written in other languages,...
, MathematicaMathematica is a computational software program used in scientific, engineering, and mathematical fields and other areas of technical computing...
, Sage (mathematics software) and other computer algebra systemA computer algebra system is a software program that facilitates symbolic mathematics. The core functionality of a CAS is manipulation of mathematical expressions in symbolic form.-Symbolic manipulations:...
s are mainly accessed through REPL which allow for the evaluation of mathematical expressions and invocation of system commands.
- OCaml has a built-in REPL (called a toplevel system in OCaml terminology), via the command ocaml. You can also build a customized REPL with custom code preloaded using the ocamlmktop utility.
- Perl
Perl is a high-level, general-purpose, interpreted, dynamic programming language. Perl was originally developed by Larry Wall in 1987 as a general-purpose Unix scripting language to make report processing easier. Since then, it has undergone many changes and revisions and become widely popular...
provides a REPL via the module Devel::REPL, also Perl Console.
- PHP
PHP is a general-purpose server-side scripting language originally designed for web development to produce dynamic web pages. For this purpose, PHP code is embedded into the HTML source document and interpreted by a web server with a PHP processor module, which generates the web page document...
's command-line interface has an interactive mode, which you can run by doing php -a (assuming php is your PHP-CLI executable, not PHP-CGI), but it does not load library functions by default. There is also phpsh, which has tab completion and other features.
- Python
Python is a general-purpose, high-level programming language whose design philosophy emphasizes code readability. Python claims to "[combine] remarkable power with very clear syntax", and its standard library is large and comprehensive...
has a built-in REPL and a more enhanced version in IPythonIPython is an interactive shell for the Python programming language that offers enhanced introspection, additional shell syntax, tab completion and rich history.- Other features :...
.
- R
R is a programming language and software environment for statistical computing and graphics. The R language is widely used among statisticians for developing statistical software, and R is widely used for statistical software development and data analysis....
statistical programming language, by default, boots into a REPL interface.
- Ruby
Ruby is a dynamic, reflective, general-purpose object-oriented programming language that combines syntax inspired by Perl with Smalltalk-like features. Ruby originated in Japan during the mid-1990s and was first developed and designed by Yukihiro "Matz" Matsumoto...
's standard libraryA standard library for a programming language is the library that is conventionally made available in every implementation of that language. In some cases, the library is described directly in the programming language specification; in other cases, the contents of the standard library are...
contains a module called IRB which stands for Interactive Ruby ShellInteractive Ruby Shell is a shell for programming in the object-oriented scripting language Ruby. The program is launched from a command line and allows the execution of Ruby commands with immediate response, experimenting in real-time...
which provides a REPL. The standard Ruby distribution packages a standalone utility called irb which simply spawns an IRB instance. Even more powerful than irb is PRY, which can be easily installed as a gem.
- Scala (a language on the JVM) provides a REPL; start it at the command line by typing "scala" (as opposed to scalac, the compiler).
- Scheme, most implementations have built-in REPLs.
- Smalltalk, many implementations also have a command line REPL built in. All Smalltalks with a GUI also have the "workspace" built in which is a more advanced form of the REPL where you select code and "do it" or "print it" or "inspect it" to evaluate the code.
- SuperCollider
A Supercollider is a high energy particle accelerator. The term may refer to:* Superconducting Super Collider, planned 80 km project in Texas, canceled in 1993...
, a language for sound synthesis, analysis and algorithmic composition.
- Windows PowerShell
Windows PowerShell is Microsoft's task automation framework, consisting of a command-line shell and associated scripting language built on top of, and integrated with the .NET Framework...
, part of Microsoft Windows, provides a REPL.
Applications with an REPL
- Mozilla
Mozilla is a term used in a number of ways in relation to the Mozilla.org project and the Mozilla Foundation, their defunct commercial predecessor Netscape Communications Corporation, and their related application software....
's internal JavaScript engine can be interactively manipulated through extensions such as FirebugFirebug is a web development tool that facilitates the debugging, editing, and monitoring of any website's CSS, HTML, DOM, XHR, and JavaScript; it also provides other web development tools. Firebug's JavaScript panel can log errors, profile function calls, and enable the developer to run arbitrary...
and VimperatorVimperator is a Mozilla Firefox extension forked from the original Firefox extension version of Conkeror and designed to provide a more efficient user interface for keyboard-fluent users...
, or externally via MozRepl.
- Apple's Safari web browser
Safari is a web browser developed by Apple Inc. and included with the Mac OS X and iOS operating systems. First released as a public beta on January 7, 2003 on the company's Mac OS X operating system, it became Apple's default browser beginning with Mac OS X v10.3 "Panther". Safari is also the...
has a command prompt under the Develop menu, Show Error Console window.
- Some window managers have an REPL, for example Sawfish
Sawfish is an extensible window manager for the X Window System. Its aim is simply to manage windows in the most flexible and attractive manner possible. Formerly known as Sawmill, the name was changed because another software program had the same name....
and StumpWM.
External links