1 --------------------------------------------------
6 A dialect of Joy in Python.
9 --------------------------------------------------
12 Copyright © 2014, 2015, 2017, 2018 Simon Forman
14 This file is part of Thun
16 Thun is free software: you can redistribute it and/or modify it under the
17 terms of the GNU General Public License as published by the Free Software
18 Foundation, either version 3 of the License, or (at your option) any later
21 Thun is distributed in the hope that it will be useful, but WITHOUT ANY
22 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
23 PARTICULAR PURPOSE. See the GNU General Public License for more details.
25 You should have received a copy of the GNU General Public License along with
26 Thun. If not see <http://www.gnu.org/licenses/>.
29 --------------------------------------------------
34 Joy is a programming language created by Manfred von Thun that is easy to
35 use and understand and has many other nice properties. This Python
36 package implements an interpreter for a dialect of Joy that attempts to
37 stay very close to the spirit of Joy but does not precisely match the
38 behaviour of the original version(s) written in C. The main difference
39 between Thun and the originals, other than being written in Python, is
40 that it works by the "Continuation-Passing Style".
42 As I study Joy I find that it is very aptly named. It is clear, concise,
43 and ameniable to advanced techniques for constructing bug-free software.
45 Developed by Manfred von Thun, don't know much about him, not much on
46 the web about Joy and von Thun (Von Thun?) See references below.
48 Because it has desirable properties (concise, highly factored) the
49 programming process changes, the ratio of designing to writing code
50 shifts in favor of design. The documentation becomes extensive while
51 the code shrinks to stable bodies of small well-factored incantations
52 that are highly expressive, much like mathematical papers consist of
53 large bodies of exposition interlaced with mathematical formula that
54 concisely and precisely express the meaning of the text.
56 The time and attention of the programmer shifts from thinking about the
57 language to thinking in the language, and the development process feels
58 more like deriving mathematical truths than like writing ad-hoc
61 I hope that this package is useful in the sense that it provides an
62 additional joy interpreter (the binary in the archive from La Trobe seems
63 to run just fine on my modern Linux machine!) But I also hope that you
64 can read and understand the Python code and play with the implementation
67 The best source (no pun intended) for learning about Joy is the
68 information made available at the website of La Trobe University (see the
69 references section below for the URL) which contains source code for the
70 original C interpreter, Joy language source code for various functions,
71 and a great deal of fascinating material mostly written by Von Thun on
72 Joy and its deeper facets as well as how to program in it and several
73 interesting aspects. It's quite a treasure trove.
78 From PyPI in the usual way, e.g.:
82 Or if you have downloaded the source, from the joypy directory:
84 python ./setup.py install
86 Or you can run the module from the joypy directory (see below.)
88 To start a crude REPL:
95 Joy is stack-based. There is a main stack that holds data items:
96 integers, floats, strings, functions, and sequences or quotes which hold
97 data items themselves.
99 23 1.8 'a string' "another" dup [21 18 /] [1 [2 [3]]]
101 A Joy expression is just a sequence of items, also called lists.
102 Sequences intended as programs are called "quoted programs". The
103 evaluation proceeds by iterating through the terms in the expression,
104 putting all literals onto the main stack and executing functions as they
105 are encountered. Functions receive the current stack and return the next
108 The main loop is very simple as most of the action happens through what
109 are called "combinators", which accept quoted programs on the stack and
110 run them in various ways. These combinators factor specific patterns
111 that provide the effect of control-flow in other languages (such as ifte
112 which is like if..then..else..) Combinators receive the current
113 expession in addition to the stack and return the next expression.
114 In Joy control-flow is done by combinators that expect quoted programs
115 on the stack and execute them in various ways. In Thun they work by
116 changing the pending expression that the interpreter is about to execute.
117 In concrete terms, the combinators could work by making recursive calls
118 to the interpreter and all intermediate state would be held in the call
119 stack of the implementation language, in this joy implementation they
120 work instead by changing the pending expression and intermediate state
123 In general, where otherwise unspecified, the semantics of Thun are that
124 of the underlying Python. That means, for example, that integers are
125 unbounded (whatever your machine can handle), strings cannot be added to
126 integers but can be multiplied, Boolean True and False are effectively
127 identical to ints 1 and 0, empty sequences are considered False, etc.
129 Nothing is done about Python exceptions currently, although it would be
130 possible to capture the stack and expression just before the exception
131 and build a robust and flexible error handler. Because they are both
132 just datastructures, you could immediately retry them under a debugger,
133 or edit either or both of the stack and expression. All state is in one
136 § Literals and Simple Functions
158 joy? 23 [0 >] [dup --] while
160 -> 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
165 § Definitions and More Elaborate Functions
168 § Programming and Metaprogramming
173 --------------------------------------------------
183 |-- COPYING - license
184 |-- README - this file
186 |-- archive - info on Joy
187 | |-- Joy-Programming.zip
190 |-- docs - Various Examples and Demos
191 | |-- * - Jupyter Notebooks on Thun and supporting modules
192 | `-- README - Table of Contents
195 | |-- joy.py - main loop, REPL
196 | |-- library.py - Functions, Combinators, Definitions
197 | |-- parser.py - convert text to Joy datastructures
200 | |-- pretty_print.py - convert Joy datastructures to text
201 | `-- stack.py - work with stacks
206 --------------------------------------------------
212 Wikipedia entry for Joy:
213 https://en.wikipedia.org/wiki/Joy_%28programming_language%29
216 Homepage at La Trobe University:
217 http://www.latrobe.edu.au/humanities/research/research-projects/past-projects/joy-programming-language
222 Stack based - literals (as functions) - functions - combinators -
223 Refactoring and making new definitions - traces and comparing
224 performance - metaprogramming as programming, even the lowly integer
225 range function can be expressed in two phases: building a specialized
226 program and then executing it with a combinator - ?Partial evaluation?
227 - ?memoized dynamic dependency graphs? - algebra