OSDN Git Service

Change project name to Thun.
[joypy/Thun.git] / README
1 --------------------------------------------------
2
3
4             Thun
5
6   A dialect of Joy in Python.
7
8
9 --------------------------------------------------
10
11
12 Copyright © 2014, 2015, 2017, 2018 Simon Forman
13
14 This file is part of Thun
15
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
19 version.
20
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.
24
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/>.
27
28
29 --------------------------------------------------
30
31
32 § Introduction
33
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".
41
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.
44
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.
47
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.
55
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
59 solutions.
60
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
65 itself.
66
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.
74
75
76 § Installation
77
78 From PyPI in the usual way, e.g.:
79
80     pip install Thun
81
82 Or if you have downloaded the source, from the joypy directory:
83
84     python ./setup.py install
85
86 Or you can run the module from the joypy directory (see below.)
87
88 To start a crude REPL:
89
90     python -m joy
91
92
93 § Basics of Joy
94
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.
98
99     23 1.8 'a string' "another" dup [21 18 /] [1 [2 [3]]]
100
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
106 stack.
107
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
121 is put there.
122
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.
128
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
134 or the other.
135
136 § Literals and Simple Functions
137
138     joy? 1 2 3
139     -> 3 2 1
140
141     joy? +
142     -> 5 1
143
144     joy? +
145     -> 6
146
147     joy? 7
148     -> 7 6
149
150     joy? *
151     -> 42
152
153     joy?
154
155
156 § Simple Combinators
157
158     joy? 23 [0 >] [dup --] while
159
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
161
162
163 TODO:
164
165 § Definitions and More Elaborate Functions
166   Refactoring
167
168 § Programming and Metaprogramming
169
170 § Further Reading
171
172
173 --------------------------------------------------
174
175
176 This Implementation
177
178 Run with:
179
180     python -m joy
181
182 joypy
183  |-- COPYING - license
184  |-- README - this file
185  |
186  |-- archive - info on Joy
187  |   |-- Joy-Programming.zip
188  |   `-- README
189  |
190  |-- docs - Various Examples and Demos
191  |   |-- * - Jupyter Notebooks on Thun and supporting modules
192  |   `-- README - Table of Contents
193  |
194  |-- joy
195  |   |-- joy.py - main loop, REPL
196  |   |-- library.py - Functions, Combinators, Definitions
197  |   |-- parser.py - convert text to Joy datastructures
198  |   |
199  |   `-- utils
200  |       |-- pretty_print.py - convert Joy datastructures to text
201  |       `-- stack.py - work with stacks
202  |
203  `-- setup.py
204
205
206 --------------------------------------------------
207
208
209 References
210
211
212 Wikipedia entry for Joy:
213 https://en.wikipedia.org/wiki/Joy_%28programming_language%29
214
215
216 Homepage at La Trobe University:
217 http://www.latrobe.edu.au/humanities/research/research-projects/past-projects/joy-programming-language
218
219
220 Misc...
221
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
228