The first release of elm-repl
is now available. Like any traditional
REPL,
it lets you interact with functions and expressions buried deep inside a
large project with many modules.
After you install Node.js, you can install the REPL with:
cabal update ; cabal install elm-repl
From there, just run elm-repl
and start writing Elm expressions, definitions,
ADTs, and module imports.
It is just for the command line now, but I’d love to see elm-repl
integrated into editors. Modes for emacs,
vim, and
Sublime Text are still
maturing and improving, so if you are interested in working on any of these,
please get in contact with the authors. This is a great way to contribute to Elm!
Usage
The first thing to know about a REPL is how to exit:
press Ctrl-d
.
When you enter an expression, you get the result and its type:
> 1 + 1
2 : number
> "hello" ++ "world"
"helloworld" : String
The same can be done with definitions of values and functions:
> fortyOne = 41
41 : number
> increment n = n + 1
<function> : number -> number
> increment fortyOne
42 : number
> factorial n = \
| if n < 1 then 1 else n * factorial (n-1)
<function> : number -> number
> factorial 5
120 : number
You can also define union types:
> type List a = Nil | Cons a (List a)
> isNil xs = \
| case xs of \
| Nil -> True \
| Cons _ _ -> False
<function> : List a -> Bool
> isNil Nil
True : Bool
You can also import standard libraries and any library reachable
from the directory in which elm-repl
is running. Let's say you
are working on a module called Graph
:
> import String
> String.length "hello"
5 : Int
> String.reverse "flow"
"wolf" : String
> import Graph
> Graph.edges
<function> : Graph -> [Edge]
This means you can dig into large projects you are working on and see how a specific function behaves.
What happened to “A New Kind of REPL”?
When I announced hot-swapping in Elm, I called it a new kind of REPL. Riffing on Stephen Wolfram's New Kind of Science definitely makes for a provocative title, but perhaps unsurprisingly, the old kind of REPL is still very important.
It is clear that hot-swapping changes how we tweak and perfect our programs. It changes how we debug. It changes how beginners learn to program. It changes how developers dig into existing codebases. That is all great, but for some reason we still had folks on the list asking for a good old fashioned Read-eval-print-loop. My initial feeling was “Don't you see! REPLs are so 2000 and late” but I was missing the bigger picture.
When it comes to exploring functions deep inside a large codebase, a REPL is a great tool. Looking at the results of an entire program makes it hard to pin down specific functions, especially when they are more abstract or do not have direct impact on the things displayed by the program. In other words, a REPL is the unit test of interactive programming.
REPL : unit testing :: hot-swapping : system testing
Once I put together the basics of elm-repl
it was obvious that REPLs and
hot-swapping are great complements, both helping make developing and debugging
easier in their own way.
Thank you
Thank you to Joe Collard for explaining to me why he needed a
REPL.
Once I fully understood the problem, I had to do it. It was like a happier version
of The Tell-Tale Heart.
Thank you to Thomas Bereknyei for figuring out
how to catch Ctrl-c
presses in a platform independent way. Thanks to
the haskeline project
which provided lots of great infrastructure for this project.