OSDN Git Service

Make cannot deal with spaces in filenames.
[joypy/Thun.git] / docs / 0._This_Implementation_of_Joy_in_Python.md
1
2 # Joypy
3
4 ## Joy in Python
5
6 This implementation is meant as a tool for exploring the programming model and method of Joy.  Python seems like a great implementation language for Joy for several reasons.
7
8 We can lean on the Python immutable types for our basic semantics and types: ints, floats, strings, and tuples, which enforces functional purity.  We get garbage collection for free.  Compilation via Cython.  Glue language with loads of libraries.
9
10 ### [Read-Eval-Print Loop (REPL)](https://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop)
11 The main way to interact with the Joy interpreter is through a simple [REPL](https://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop) that you start by running the package:
12
13     $ python -m joy
14     Joypy - Copyright © 2017 Simon Forman
15     This program comes with ABSOLUTELY NO WARRANTY; for details type "warranty".
16     This is free software, and you are welcome to redistribute it
17     under certain conditions; type "sharing" for details.
18     Type "words" to see a list of all words, and "[<name>] help" to print the
19     docs for a word.
20
21
22      <-top
23
24     joy? _
25
26 The `<-top` marker points to the top of the (initially empty) stack.  You can enter Joy notation at the prompt and a [trace of evaluation](#The-TracePrinter.) will be printed followed by the stack and prompt again:
27
28     joy? 23 sqr 18 +
29            . 23 sqr 18 +
30         23 . sqr 18 +
31         23 . dup mul 18 +
32      23 23 . mul 18 +
33        529 . 18 +
34     529 18 . +
35        547 . 
36
37     547 <-top
38
39     joy? 
40
41
42 # Stacks (aka list, quote, sequence, etc.)
43
44 In Joy, in addition to the types Boolean, integer, float, and string, there is a single sequence type represented by enclosing a sequence of terms in brackets `[...]`.  This sequence type is used to represent both the stack and the expression.  It is a [cons list](https://en.wikipedia.org/wiki/Cons#Lists) made from Python tuples.
45
46
47 ```python
48 import inspect
49 import joy.utils.stack
50
51
52 print inspect.getdoc(joy.utils.stack)
53 ```
54
55     § Stack
56     
57     
58     When talking about Joy we use the terms "stack", "list", "sequence" and
59     "aggregate" to mean the same thing: a simple datatype that permits
60     certain operations such as iterating and pushing and popping values from
61     (at least) one end.
62     
63     We use the venerable two-tuple recursive form of sequences where the
64     empty tuple () is the empty stack and (head, rest) gives the recursive
65     form of a stack with one or more items on it.
66     
67       ()
68       (1, ())
69       (2, (1, ()))
70       (3, (2, (1, ())))
71       ...
72     
73     And so on.
74     
75     
76     We have two very simple functions to build up a stack from a Python
77     iterable and also to iterate through a stack and yield its items
78     one-by-one in order, and two functions to generate string representations
79     of stacks:
80     
81       list_to_stack()
82     
83       iter_stack()
84     
85       expression_to_string()  (prints left-to-right)
86     
87       stack_to_string()  (prints right-to-left)
88     
89     
90     A word about the stack data structure.
91     
92     Python has very nice "tuple packing and unpacking" in its syntax which
93     means we can directly "unpack" the expected arguments to a Joy function.
94     
95     For example:
96     
97       def dup(stack):
98         head, tail = stack
99         return head, (head, tail)
100     
101     We replace the argument "stack" by the expected structure of the stack,
102     in this case "(head, tail)", and Python takes care of de-structuring the
103     incoming argument and assigning values to the names.  Note that Python
104     syntax doesn't require parentheses around tuples used in expressions
105     where they would be redundant.
106
107
108 ### The utility functions maintain order.
109 The 0th item in the list will be on the top of the stack and *vise versa*.
110
111
112 ```python
113 joy.utils.stack.list_to_stack([1, 2, 3])
114 ```
115
116
117
118
119     (1, (2, (3, ())))
120
121
122
123
124 ```python
125 list(joy.utils.stack.iter_stack((1, (2, (3, ())))))
126 ```
127
128
129
130
131     [1, 2, 3]
132
133
134
135 This requires reversing the sequence (or iterating backwards) otherwise:
136
137
138 ```python
139 stack = ()
140
141 for n in [1, 2, 3]:
142     stack = n, stack
143
144 print stack
145 print list(joy.utils.stack.iter_stack(stack))
146 ```
147
148     (3, (2, (1, ())))
149     [3, 2, 1]
150
151
152 ### Purely Functional Datastructures.
153 Because Joy lists are made out of Python tuples they are immutable, so all Joy datastructures are *[purely functional](https://en.wikipedia.org/wiki/Purely_functional_data_structure)*.
154
155 # The `joy()` function.
156 ## An Interpreter
157 The `joy()` function is extrememly simple.  It accepts a stack, an expression, and a dictionary, and it iterates through the expression putting values onto the stack and delegating execution to functions it looks up in the dictionary.
158
159 Each function is passed the stack, expression, and dictionary and returns them.  Whatever the function returns becomes the new stack, expression, and dictionary.  (The dictionary is passed to enable e.g. writing words that let you enter new words into the dictionary at runtime, which nothing does yet and may be a bad idea, and the `help` command.)
160
161
162 ```python
163 import joy.joy
164
165 print inspect.getsource(joy.joy.joy)
166 ```
167
168     def joy(stack, expression, dictionary, viewer=None):
169       '''
170       Evaluate the Joy expression on the stack.
171       '''
172       while expression:
173     
174         if viewer: viewer(stack, expression)
175     
176         term, expression = expression
177         if isinstance(term, Symbol):
178           term = dictionary[term]
179           stack, expression, dictionary = term(stack, expression, dictionary)
180         else:
181           stack = term, stack
182     
183       if viewer: viewer(stack, expression)
184       return stack, expression, dictionary
185     
186
187
188 ### View function
189 The `joy()` function accepts a "viewer" function which it calls on each iteration passing the current stack and expression just before evaluation.  This can be used for tracing, breakpoints, retrying after exceptions, or interrupting an evaluation and saving to disk or sending over the network to resume later.  The stack and expression together contain all the state of the computation at each step.
190
191 ### The `TracePrinter`.
192
193 A `viewer` records each step of the evaluation of a Joy program.  The `TracePrinter` has a facility for printing out a trace of the evaluation, one line per step.  Each step is aligned to the current interpreter position, signified by a period separating the stack on the left from the pending expression ("continuation") on the right.
194
195 ### [Continuation-Passing Style](https://en.wikipedia.org/wiki/Continuation-passing_style)
196 One day I thought, What happens if you rewrite Joy to use [CSP](https://en.wikipedia.org/wiki/Continuation-passing_style)?  I made all the functions accept and return the expression as well as the stack and found that all the combinators could be rewritten to work by modifying the expression rather than making recursive calls to the `joy()` function.
197
198 # Parser
199
200
201 ```python
202 import joy.parser
203
204 print inspect.getdoc(joy.parser)
205 ```
206
207     § Converting text to a joy expression.
208     
209     This module exports a single function:
210     
211       text_to_expression(text)
212     
213     As well as a single Symbol class and a single Exception type:
214     
215       ParseError
216     
217     When supplied with a string this function returns a Python datastructure
218     that represents the Joy datastructure described by the text expression.
219     Any unbalanced square brackets will raise a ParseError.
220
221
222 The parser is extremely simple, the undocumented `re.Scanner` class does most of the tokenizing work and then you just build the tuple structure out of the tokens.  There's no Abstract Syntax Tree or anything like that.
223
224
225 ```python
226 print inspect.getsource(joy.parser._parse)
227 ```
228
229     def _parse(tokens):
230       '''
231       Return a stack/list expression of the tokens.
232       '''
233       frame = []
234       stack = []
235       for tok in tokens:
236         if tok == '[':
237           stack.append(frame)
238           frame = []
239           stack[-1].append(frame)
240         elif tok == ']':
241           try:
242             frame = stack.pop()
243           except IndexError:
244             raise ParseError('One or more extra closing brackets.')
245           frame[-1] = list_to_stack(frame[-1])
246         else:
247           frame.append(tok)
248       if stack:
249         raise ParseError('One or more unclosed brackets.')
250       return list_to_stack(frame)
251     
252
253
254 That's pretty much all there is to it.
255
256
257 ```python
258 joy.parser.text_to_expression('1 2 3 4 5')  # A simple sequence.
259 ```
260
261
262
263
264     (1, (2, (3, (4, (5, ())))))
265
266
267
268
269 ```python
270 joy.parser.text_to_expression('[1 2 3] 4 5')  # Three items, the first is a list with three items
271 ```
272
273
274
275
276     ((1, (2, (3, ()))), (4, (5, ())))
277
278
279
280
281 ```python
282 joy.parser.text_to_expression('1 23 ["four" [-5.0] cons] 8888')  # A mixed bag. cons is
283                                                                  # a Symbol, no lookup at
284                                                                  # parse-time.  Haiku docs.
285 ```
286
287
288
289
290     (1, (23, (('four', ((-5.0, ()), (cons, ()))), (8888, ()))))
291
292
293
294
295 ```python
296 joy.parser.text_to_expression('[][][][][]')  # Five empty lists.
297 ```
298
299
300
301
302     ((), ((), ((), ((), ((), ())))))
303
304
305
306
307 ```python
308 joy.parser.text_to_expression('[[[[[]]]]]')  # Five nested lists.
309 ```
310
311
312
313
314     ((((((), ()), ()), ()), ()), ())
315
316
317
318 # Library
319 The Joy library of functions (aka commands, or "words" after Forth usage) encapsulates all the actual functionality (no pun intended) of the Joy system.  There are simple functions such as addition `add` (or `+`, the library module supports aliases), and combinators which provide control-flow and higher-order operations.
320
321
322 ```python
323 import joy.library
324
325 print ' '.join(sorted(joy.library.initialize()))
326 ```
327
328     != % & * *fraction *fraction0 + ++ - -- / < << <= <> = > >= >> ? ^ add anamorphism and app1 app2 app3 average b binary branch choice clear cleave concat cons dinfrirst dip dipd dipdd disenstacken div down_to_zero dudipd dup dupd dupdip enstacken eq first flatten floordiv gcd ge genrec getitem gt help i id ifte infra le least_fraction loop lshift lt map min mod modulus mul ne neg not nullary or over pam parse pm pop popd popdd popop pow pred primrec product quoted range range_to_zero rem remainder remove rest reverse roll< roll> rolldown rollup rshift run second select sharing shunt size sqr sqrt stack step sub succ sum swaack swap swoncat swons ternary third times truediv truthy tuck unary uncons unit unquoted unstack void warranty while words x xor zip •
329
330
331 Many of the functions are defined in Python, like `dip`:
332
333
334 ```python
335 print inspect.getsource(joy.library.dip)
336 ```
337
338     def dip(stack, expression, dictionary):
339       (quote, (x, stack)) = stack
340       expression = x, expression
341       return stack, pushback(quote, expression), dictionary
342     
343
344
345 Some functions are defined in equations in terms of other functions.  When the interpreter executes a definition function that function just pushes its body expression onto the pending expression (the continuation) and returns control to the interpreter.
346
347
348 ```python
349 print joy.library.definitions
350 ```
351
352     second == rest first
353     third == rest rest first
354     product == 1 swap [*] step
355     swons == swap cons
356     swoncat == swap concat
357     flatten == [] swap [concat] step
358     unit == [] cons
359     quoted == [unit] dip
360     unquoted == [i] dip
361     enstacken == stack [clear] dip
362     disenstacken == ? [uncons ?] loop pop
363     ? == dup truthy
364     dinfrirst == dip infra first
365     nullary == [stack] dinfrirst
366     unary == [stack [pop] dip] dinfrirst
367     binary == [stack [popop] dip] dinfrirst
368     ternary == [stack [popop pop] dip] dinfrirst
369     pam == [i] map
370     run == [] swap infra
371     sqr == dup mul
372     size == 0 swap [pop ++] step
373     cleave == [i] app2 [popd] dip
374     average == [sum 1.0 *] [size] cleave /
375     gcd == 1 [tuck modulus dup 0 >] loop pop
376     least_fraction == dup [gcd] infra [div] concat map
377     *fraction == [uncons] dip uncons [swap] dip concat [*] infra [*] dip cons
378     *fraction0 == concat [[swap] dip * [*] dip] infra
379     down_to_zero == [0 >] [dup --] while
380     range_to_zero == unit [down_to_zero] infra
381     anamorphism == [pop []] swap [dip swons] genrec
382     range == [0 <=] [1 - dup] anamorphism
383     while == swap [nullary] cons dup dipd concat loop
384     dudipd == dup dipd
385     primrec == [i] genrec
386     
387
388
389 Currently, there's no function to add new definitions to the dictionary from "within" Joy code itself.  Adding new definitions remains a meta-interpreter action.  You have to do it yourself, in Python, and wash your hands afterward.
390
391 It would be simple enough to define one, but it would open the door to *name binding* and break the idea that all state is captured in the stack and expression.  There's an implicit *standard dictionary* that defines the actual semantics of the syntactic stack and expression datastructures (which only contain symbols, not the actual functions.  Pickle some and see for yourself.)
392
393 #### "There should be only one."
394
395 Which brings me to talking about one of my hopes and dreams for this notation:  "There should be only one."  What I mean is that there should be one universal standard dictionary of commands, and all bespoke work done in a UI for purposes takes place by direct interaction and macros.  There would be a *Grand Refactoring* biannually (two years, not six months, that's semi-annually) where any new definitions factored out of the usage and macros of the previous time, along with new algorithms and such, were entered into the dictionary and posted to e.g. IPFS.
396
397 Code should not burgeon wildly, as it does today.  The variety of code should map more-or-less to the well-factored variety of human computably-solvable problems.  There shouldn't be dozens of chat apps, JS frameworks, programming languages.  It's a waste of time, a [fractal "thundering herd" attack](https://en.wikipedia.org/wiki/Thundering_herd_problem) on human mentality.
398
399 #### Literary Code Library
400
401 If you read over the other notebooks you'll see that developing code in Joy is a lot like doing simple mathematics, and the descriptions of the code resemble math papers.  The code also works the first time, no bugs.  If you have any experience programming at all, you are probably skeptical, as I was, but it seems to work: deriving code mathematically seems to lead to fewer errors.
402
403 But my point now is that this great ratio of textual explanation to wind up with code that consists of a few equations and could fit on an index card is highly desirable.  Less code has fewer errors.  The structure of Joy engenders a kind of thinking that seems to be very effective for developing structured processes.
404
405 There seems to be an elegance and power to the notation.
406
407
408
409 ```python
410   
411 ```