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