7 > Simple pleasures are the best.
9 [Joy](https://en.wikipedia.org/wiki/Joy_%28programming_language%29) is a
10 programming language created by Manfred von Thun that is easy to use and
11 understand and has many other nice properties. **Thun** is a dialect of
12 Joy that attempts to stay very close to the spirit of Joy but does not
13 precisely match the behaviour of the original version written in C.
18 The grammar of Joy is very simple. A Joy expression is zero or more Joy
19 terms separated by blanks. Terms can be integers in decimal notation,
20 Booleans `true` and `false`, lists enclosed by square brackets `[` and `]`,
21 or symbols (names of functions.)
25 term ::= integer | bool | '[' joy ']' | symbol
27 integer ::= [ '-' ] ('0'...'9')+
29 bool ::= 'true' | 'false'
33 char ::= <Any non-space other than '[' and ']'.>
35 Symbols can be composed of any characters except blanks and square
36 brackets. Integers can be prefixed with a minus sign to denote negative
37 numbers. The symbols `true` and `false` are reserved to denote their
38 respective Boolean values.
40 That's it. That's the whole of the grammar.
45 The original Joy has several datatypes (such as strings and sets)
46 but the Thun dialect currently only uses four:
48 * Integers, signed and unbounded by machine word length (they are
49 [bignums](https://en.wikipedia.org/wiki/Arbitrary-precision_arithmetic).)
50 * Boolean values ``true`` and ``false``.
51 * Lists quoted in `[` and `]` brackets.
55 ## Stack, Expression, Dictionary
57 Joy is built around three things: a __stack__ of data items, an
58 __expression__ representing a program to evaluate, and a __dictionary__
64 [stack-based](https://en.wikipedia.org/wiki/Stack-oriented_programming_language).
65 There is a single main __stack__ that holds data items, which can be
66 integers, bools, symbols (names), or sequences of data items enclosed in
67 square brackets (`[` or `]`).
69 We use the terms "stack", "quote", "sequence", "list", and others to mean
70 the same thing: a simple linear datatype that permits certain operations
71 such as iterating and pushing and popping values from (at least) one end.
73 > In describing Joy I have used the term quotation to describe all of the
74 > above, because I needed a word to describe the arguments to combinators
75 > which fulfill the same role in Joy as lambda abstractions (with
76 > variables) fulfill in the more familiar functional languages. I use the
77 > term list for those quotations whose members are what I call literals:
78 > numbers, characters, truth values, sets, strings and other quotations.
79 > All these I call literals because their occurrence in code results in
80 > them being pushed onto the stack. But I also call [London Paris] a
81 > list. So, [dup \*] is a quotation but not a list.
83 From ["A Conversation with Manfred von Thun" w/ Stevan Apter](http://archive.vector.org.uk/art10000350)
87 A Joy __expression__ is just a sequence or list of items. Sequences
88 intended as programs are called "quoted programs". Evaluation proceeds
89 by iterating through the terms in an expression putting all literals
90 (integers, bools, or lists) onto the main stack and executing functions
91 named by symbols as they are encountered. Functions receive the current
92 stack, expression, and dictionary and return the next stack, expression,
97 The __dictionary__ associates symbols (names) with Joy expressions that
98 define the available functions of the Joy system. Together the stack,
99 expression, and dictionary are the entire state of the Joy interpreter.
104 The Joy interpreter is extrememly simple. It accepts a stack, an
105 expression, and a dictionary, and it iterates through the expression
106 putting values onto the stack and delegating execution to functions which
107 it looks up in the dictionary.
109 ![Joy Interpreter Flowchart](https://git.sr.ht/~sforman/Thun/blob/trunk/joy_interpreter_flowchart.svg)
111 All control flow works by
112 [Continuation Passing Style](https://en.wikipedia.org/wiki/Continuation-passing_style).
113 __Combinators__ (see below) alter control flow by prepending quoted programs to the pending
114 expression (aka "continuation".)
117 ## Literals, Functions, Combinators
122 Literal values (integers, Booleans, lists) are put onto the stack.
126 Functions take values from the stack and push results onto it.
130 __Combinators__ are functions which accept quoted programs on the stack
131 and run them in various ways. These combinators reify specific
132 control-flow patterns (such as `ifte` which is like `if.. then.. else..`
133 in other languages.) Combinators receive the current expession in
134 addition to the stack and return the next expression. They work by
135 changing the pending expression the interpreter is about to execute.
139 Thun has a set of *basis* functions which are implemented in the host
140 language. The rest of functions in the Thun dialect are defined in terms
143 - Combinators: `branch` `dip` `i` `loop`
144 - Stack Chatter: `clear` `dup` `pop` `stack` `swaack` `swap`
145 - List Manipulation: `concat` `cons` `first` `rest`
146 - Math: `+` `-` `*` `/` `%`
147 - Comparison: `<` `>` `>=` `<=` `!=` `<>` `=`
148 - Logic: `truthy` `not`
149 - Programming: `inscribe`
153 Thun can be extended by adding new definitions to the `defs.txt` file and
154 rebuilding the binaries. Each line in the file is a definition
155 consisting of the new symbol name followed by an expression for the body
158 You can use the `inscribe` command to put new definitions into the
159 dictionary at runtime, but they will not persist after the program ends.
160 The `inscribe` function is the only function that changes the dictionary.
161 It's meant for prototyping. (You could abuse it to make variables by
162 storing "functions" in the dictionary that just contain literal values as
169 Nothing prevents you from using symbols as data:
174 But there's a potential pitfall: you might accidentally get a "bare"
175 unquoted symbol on the stack:
182 That by itself won't break anything (the stack is just a list.)
183 But if you were to use, say, `dip`, in such a way as to put the symbol
184 back onto the expression, then when the interpreter encounters it, it
185 will attempt to evaluate it, which is almost certainly not what you want.
192 At the very least you get an "Unknown" error, but if the symbol names a
193 function then the interpreter will attempt to evaluate it, probably
196 I don't see an easy way around this. Be careful? It's kind of against
197 the spirit of the thing to just leave a footgun like that laying around,
198 but perhaps in practice it won't come up. (Because writing Joy code by
199 derivation seems to lead to bug-free code, which is the kinda the point.)
202 --------------------------------------------------
204 Copyright © 2014 - 2023 Simon Forman
206 This file is part of Thun