#How the playground works
elucid8 playground is itself a program in eLucid8: a program.el8 and its deploy.json, run by elucid8 window. The command is its launcher: it runs the program in a window, in the folder that keeps your projects. You can read the whole program, or run a changed copy:
elucid8 playground --source my-playground
elucid8 playground --program my-playground
This page walks through how it is built, with lines from the program as it is.
#Every event is data; every state is a value
Each key you press and each click is a turn of the program's input keys, a row per field: which control, what kind of event, which key. Everything the window shows is a value of the program at the newest turn, computed from the turns before it. The text in the editor is the value doc, over turns, lines and columns; the document before an event is the one at the turn before:
bdoc = eod fby.t doc
So Cmd-Z needs no stack of copies: the playground remembers which turn made the document shown, and an undo shows the document of an earlier turn.
#Two helpers, asked through the window
The program does not compute your programs itself. It asks two helpers, both the elucid8 command: the runner, which keeps your projects, compiles and runs your programs, answers questions and builds Why? trees; and the writer, which writes programs from a description on your own Anthropic API key. A request is an ordinary output of the program, a value at the turn it asks:
output("run") = run_request
output("write") = write_request
The window sends each request to its helper, and the reply comes back as a turn of its own, its fields and texts as rows of the program's inputs. The program goes on taking your keys meanwhile, so the window never waits for an answer; a reply to a request that a newer one has replaced is left out.
#Versions
Every Update is saved by the runner as the next version of the project, in its folder. The version the window shows is the one the runner's last reply names.
Undo, Undo changes and the drafts of changes not yet applied are requests to the runner too; the folder always holds what the window shows, so elucid8 run in a project's folder runs what you see.
#Projects and examples
A project is a name until something is put in it: the runner opens new_1 without writing anything (on a later start, the project open when the playground last closed, when it still has work), and makes its folder when you generate or update a program, choose an example or save it. An example is copied into the project, never opened in place, and the runner refuses any write into templates/, where the examples are kept. Every reply of the runner's says whether the project open is blank, so the program knows whether New program or an example has work to save first; a reply that opens another project clears every view of the one before, the question, the answer, the picture, Why? and the data files.
The description's box is one flat text, its characters over a single dimension i; a selection is two positions in it, the anchor and the cursor, so a drag across the box's wrapped lines selects across them.
#One column or two
The playground chooses its layout by the window's width over the text size. From about 1,100 such pixels it has two columns: on the left the description, Generate program and Examples ▾, then the program; on the right the question, its answer, Visualize and Trace. Narrower, the same parts are in one column, the question and its answer before the program, and the page below the top bar scrolls as a whole. The columns the description's box shows, which the window reports when it is resized, give that width whatever the text size: two columns from 136 when the box spans the window, one again below 54 when it is in the left column. Each report is read against the layout the box was in when it was measured:
wide_b = false fby.s wide_now
wide_now = if vkind == "resize" and vnode == "prompt"
then view[f = "cols"] >= (if wide_b then narrow_cols else wide_cols)
else wide_b
one_col = not default(asof.s wide_now at time(kind) end, false)
The window shows the newest report's layout at once; the equations that follow your keys take it as of each key.
#Why? and the picture
A Why? reply carries the tree of demands behind an answer: a line of words per row, and each row's depth, its children and where its subtree ends. Which row each line of the tab shows follows from the folds: a folded row's subtree is skipped to its end.
vnext = if folded[row = vrow] then w_end[row = vrow] + 1 else vrow + 1
vrow = 0 fby.line vnext
The picture is the value asked for and the values around it, from the reply's cells: a row of numbers as a line chart, a grid as coloured cells with a column beside it (the title, the colour scale as a bar with its two ends, a key for the asked cell, which is marked, and for eod and error cells, then the axes and ◀ ▶ Play) and the view's work in a line under both. Your program's outcomes are data here: an eod value and an error or a late answer are texts, drawn grey and red, so nothing your program gives can stop the playground's own program.
picture = restrict
if pic_line then eod
else if default(pic_a0_has and (mk_e == 0 or mk_e == 1), false) then "marker"
else if default(pic_a0_has and mk_e == 2, false) then "marker_in"
else if not pic_in then eod
else if pic_edge then eod
else if iseod(pic_code) then eod
else if pic_code == 0 then run_cell[t = pic_turn, x = pic_vx, y = pic_vy, f = "value"]
else if pic_code == 1 then "eod value"
else if pic_code == 3 then "late" else "failed"
to (x : 256, y : 256) end
The numbers are drawn as the playground in your browser draws them: on one scale from the least value to the greatest, the window's own; the bar beside the picture is a picture of its own (legend, from the least to the greatest), so it takes the same colours. pic_edge is the thin edge on two sides of each cell, drawn where a cell is 12 pixels or more on the screen, as the web playground draws its cells, so the cells can be counted; smaller cells are a smooth field. The window tells the program the picture's size whenever it changes, so the edges come and go as the window is resized. mk_e is how far a picture cell is inside the box round the asked cell, so the two rings that mark it are drawn there, kept inside the plate; pic_a0_has is true when the asked cell is in the picture, so the rings are drawn only then.
#How each turn is computed
At each turn, eLucid8 decides how the values the window needs are computed: which to keep from the turns before, which to compute again, in what order. Whatever it chooses, the values are the same: the program says what each value is, never how it is computed. Each key you type in the editor is answered in about 6 milliseconds on an Apple M5 Max, though the program is more than 1,300 lines.
#What keeps you safe
The playground runs on your computer with your rights, so it keeps to limits that protect your files, your computer and your key:
- Your files: it opens and saves files only in its own folder (
~/eLucid8, or the one you give) and the folders under it. To use your own data, copy it into a project'sdata/folder. - Your results: a project keeps them on your computer, in its own folder; it never writes files elsewhere or sends its output over the network, even when a description or its data asks it to.
elucid8 rundoes that, where a project'sdeploy.jsonsays so explicitly. - Programs you did not choose to start: a project whose deployment starts other programs starts nothing until you allow it in the window, for that project only. A changed copy of the playground cannot allow anything for you.
- Your computer's memory and time: your programs run in a separate process, each with a share of your computer's memory and a time limit; Esc stops a question, and a program that needs too much is stopped, never the playground.
- Your key: your Anthropic API key stays in a file only you can read, in eLucid8's settings folder, and is sent only to Anthropic; it is never part of a program, a project's files or the screen, and spending is capped.
Is it safe to run? gives the details, and The playground on your computer shows how to use it.