OSDN Git Service

Missed the notebooks *.html files.
[joypy/Thun.git] / docs / sphinx_docs / _build / html / notebooks / 0. This Implementation of Joy in Python.html
1
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
3   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4
5 <html xmlns="http://www.w3.org/1999/xhtml">
6   <head>
7     <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
8     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
9     <title>Joypy &#8212; Thun 0.1.1 documentation</title>
10     <link rel="stylesheet" href="../_static/alabaster.css" type="text/css" />
11     <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
12     <script type="text/javascript" src="../_static/documentation_options.js"></script>
13     <script type="text/javascript" src="../_static/jquery.js"></script>
14     <script type="text/javascript" src="../_static/underscore.js"></script>
15     <script type="text/javascript" src="../_static/doctools.js"></script>
16     <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
17     <link rel="index" title="Index" href="../genindex.html" />
18     <link rel="search" title="Search" href="../search.html" />
19    
20   <link rel="stylesheet" href="../_static/custom.css" type="text/css" />
21   
22   
23   <meta name="viewport" content="width=device-width, initial-scale=0.9, maximum-scale=0.9" />
24
25   </head><body>
26   
27
28     <div class="document">
29       <div class="documentwrapper">
30         <div class="bodywrapper">
31           <div class="body" role="main">
32             
33   <div class="section" id="joypy">
34 <h1>Joypy<a class="headerlink" href="#joypy" title="Permalink to this headline">¶</a></h1>
35 <div class="section" id="joy-in-python">
36 <h2>Joy in Python<a class="headerlink" href="#joy-in-python" title="Permalink to this headline">¶</a></h2>
37 <p>This implementation is meant as a tool for exploring the programming
38 model and method of Joy. Python seems like a great implementation
39 language for Joy for several reasons.</p>
40 <p>We can lean on the Python immutable types for our basic semantics and
41 types: ints, floats, strings, and tuples, which enforces functional
42 purity. We get garbage collection for free. Compilation via Cython. Glue
43 language with loads of libraries.</p>
44 <div class="section" id="read-eval-print-loop-repl">
45 <h3><a class="reference external" href="https://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop">Read-Eval-Print Loop (REPL)</a><a class="headerlink" href="#read-eval-print-loop-repl" title="Permalink to this headline">¶</a></h3>
46 <p>The main way to interact with the Joy interpreter is through a simple
47 <a class="reference external" href="https://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop">REPL</a>
48 that you start by running the package:</p>
49 <div class="highlight-default notranslate"><div class="highlight"><pre><span></span>$ python -m joy
50 Joypy - Copyright © 2017 Simon Forman
51 This program comes with ABSOLUTELY NO WARRANTY; for details type &quot;warranty&quot;.
52 This is free software, and you are welcome to redistribute it
53 under certain conditions; type &quot;sharing&quot; for details.
54 Type &quot;words&quot; to see a list of all words, and &quot;[&lt;name&gt;] help&quot; to print the
55 docs for a word.
56
57
58  &lt;-top
59
60 joy? _
61 </pre></div>
62 </div>
63 <p>The <code class="docutils literal notranslate"><span class="pre">&lt;-top</span></code> marker points to the top of the (initially empty) stack.
64 You can enter Joy notation at the prompt and a <a class="reference external" href="#The-TracePrinter.">trace of
65 evaluation</a> will be printed followed by the stack
66 and prompt again:</p>
67 <div class="highlight-default notranslate"><div class="highlight"><pre><span></span>joy? 23 sqr 18 +
68        . 23 sqr 18 +
69     23 . sqr 18 +
70     23 . dup mul 18 +
71  23 23 . mul 18 +
72    529 . 18 +
73 529 18 . +
74    547 .
75
76 547 &lt;-top
77
78 joy?
79 </pre></div>
80 </div>
81 </div>
82 </div>
83 </div>
84 <div class="section" id="stacks-aka-list-quote-sequence-etc">
85 <h1>Stacks (aka list, quote, sequence, etc.)<a class="headerlink" href="#stacks-aka-list-quote-sequence-etc" title="Permalink to this headline">¶</a></h1>
86 <p>In Joy, in addition to the types Boolean, integer, float, and string,
87 there is a single sequence type represented by enclosing a sequence of
88 terms in brackets <code class="docutils literal notranslate"><span class="pre">[...]</span></code>. This sequence type is used to represent
89 both the stack and the expression. It is a <a class="reference external" href="https://en.wikipedia.org/wiki/Cons#Lists">cons
90 list</a> made from Python
91 tuples.</p>
92 <div class="code ipython2 highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">inspect</span>
93 <span class="kn">import</span> <span class="nn">joy.utils.stack</span>
94
95
96 <span class="nb">print</span> <span class="n">inspect</span><span class="o">.</span><span class="n">getdoc</span><span class="p">(</span><span class="n">joy</span><span class="o">.</span><span class="n">utils</span><span class="o">.</span><span class="n">stack</span><span class="p">)</span>
97 </pre></div>
98 </div>
99 <div class="highlight-default notranslate"><div class="highlight"><pre><span></span>§ Stack
100
101
102 When talking about Joy we use the terms &quot;stack&quot;, &quot;list&quot;, &quot;sequence&quot; and
103 &quot;aggregate&quot; to mean the same thing: a simple datatype that permits
104 certain operations such as iterating and pushing and popping values from
105 (at least) one end.
106
107 We use the venerable two-tuple recursive form of sequences where the
108 empty tuple () is the empty stack and (head, rest) gives the recursive
109 form of a stack with one or more items on it.
110
111   ()
112   (1, ())
113   (2, (1, ()))
114   (3, (2, (1, ())))
115   ...
116
117 And so on.
118
119
120 We have two very simple functions to build up a stack from a Python
121 iterable and also to iterate through a stack and yield its items
122 one-by-one in order, and two functions to generate string representations
123 of stacks:
124
125   list_to_stack()
126
127   iter_stack()
128
129   expression_to_string()  (prints left-to-right)
130
131   stack_to_string()  (prints right-to-left)
132
133
134 A word about the stack data structure.
135
136 Python has very nice &quot;tuple packing and unpacking&quot; in its syntax which
137 means we can directly &quot;unpack&quot; the expected arguments to a Joy function.
138
139 For example:
140
141   def dup(stack):
142     head, tail = stack
143     return head, (head, tail)
144
145 We replace the argument &quot;stack&quot; by the expected structure of the stack,
146 in this case &quot;(head, tail)&quot;, and Python takes care of de-structuring the
147 incoming argument and assigning values to the names.  Note that Python
148 syntax doesn&#39;t require parentheses around tuples used in expressions
149 where they would be redundant.
150 </pre></div>
151 </div>
152 <p>The 0th item in the list will be on the top of the stack and <em>vise
153 versa</em>.</p>
154 <div class="code ipython2 highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">joy</span><span class="o">.</span><span class="n">utils</span><span class="o">.</span><span class="n">stack</span><span class="o">.</span><span class="n">list_to_stack</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">])</span>
155 </pre></div>
156 </div>
157 <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="p">())))</span>
158 </pre></div>
159 </div>
160 <div class="code ipython2 highlight-default notranslate"><div class="highlight"><pre><span></span><span class="nb">list</span><span class="p">(</span><span class="n">joy</span><span class="o">.</span><span class="n">utils</span><span class="o">.</span><span class="n">stack</span><span class="o">.</span><span class="n">iter_stack</span><span class="p">((</span><span class="mi">1</span><span class="p">,</span> <span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="p">())))))</span>
161 </pre></div>
162 </div>
163 <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">]</span>
164 </pre></div>
165 </div>
166 <p>This requires reversing the sequence (or iterating backwards) otherwise:</p>
167 <div class="code ipython2 highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">stack</span> <span class="o">=</span> <span class="p">()</span>
168
169 <span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">]:</span>
170     <span class="n">stack</span> <span class="o">=</span> <span class="n">n</span><span class="p">,</span> <span class="n">stack</span>
171
172 <span class="nb">print</span> <span class="n">stack</span>
173 <span class="nb">print</span> <span class="nb">list</span><span class="p">(</span><span class="n">joy</span><span class="o">.</span><span class="n">utils</span><span class="o">.</span><span class="n">stack</span><span class="o">.</span><span class="n">iter_stack</span><span class="p">(</span><span class="n">stack</span><span class="p">))</span>
174 </pre></div>
175 </div>
176 <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="p">())))</span>
177 <span class="p">[</span><span class="mi">3</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">1</span><span class="p">]</span>
178 </pre></div>
179 </div>
180 <p>Because Joy lists are made out of Python tuples they are immutable, so
181 all Joy datastructures are <em>`purely
182 functional &lt;https://en.wikipedia.org/wiki/Purely_functional_data_structure&gt;`__</em>.</p>
183 </div>
184 <div class="section" id="the-joy-function">
185 <h1>The <code class="docutils literal notranslate"><span class="pre">joy()</span></code> function.<a class="headerlink" href="#the-joy-function" title="Permalink to this headline">¶</a></h1>
186 <div class="section" id="an-interpreter">
187 <h2>An Interpreter<a class="headerlink" href="#an-interpreter" title="Permalink to this headline">¶</a></h2>
188 <p>The <code class="docutils literal notranslate"><span class="pre">joy()</span></code> function is extrememly simple. It accepts a stack, an
189 expression, and a dictionary, and it iterates through the expression
190 putting values onto the stack and delegating execution to functions it
191 looks up in the dictionary.</p>
192 <p>Each function is passed the stack, expression, and dictionary and
193 returns them. Whatever the function returns becomes the new stack,
194 expression, and dictionary. (The dictionary is passed to enable e.g.
195 writing words that let you enter new words into the dictionary at
196 runtime, which nothing does yet and may be a bad idea, and the <code class="docutils literal notranslate"><span class="pre">help</span></code>
197 command.)</p>
198 <div class="code ipython2 highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">joy.joy</span>
199
200 <span class="nb">print</span> <span class="n">inspect</span><span class="o">.</span><span class="n">getsource</span><span class="p">(</span><span class="n">joy</span><span class="o">.</span><span class="n">joy</span><span class="o">.</span><span class="n">joy</span><span class="p">)</span>
201 </pre></div>
202 </div>
203 <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">joy</span><span class="p">(</span><span class="n">stack</span><span class="p">,</span> <span class="n">expression</span><span class="p">,</span> <span class="n">dictionary</span><span class="p">,</span> <span class="n">viewer</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
204   <span class="sd">&#39;&#39;&#39;</span>
205 <span class="sd">  Evaluate the Joy expression on the stack.</span>
206 <span class="sd">  &#39;&#39;&#39;</span>
207   <span class="k">while</span> <span class="n">expression</span><span class="p">:</span>
208
209     <span class="k">if</span> <span class="n">viewer</span><span class="p">:</span> <span class="n">viewer</span><span class="p">(</span><span class="n">stack</span><span class="p">,</span> <span class="n">expression</span><span class="p">)</span>
210
211     <span class="n">term</span><span class="p">,</span> <span class="n">expression</span> <span class="o">=</span> <span class="n">expression</span>
212     <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">term</span><span class="p">,</span> <span class="n">Symbol</span><span class="p">):</span>
213       <span class="n">term</span> <span class="o">=</span> <span class="n">dictionary</span><span class="p">[</span><span class="n">term</span><span class="p">]</span>
214       <span class="n">stack</span><span class="p">,</span> <span class="n">expression</span><span class="p">,</span> <span class="n">dictionary</span> <span class="o">=</span> <span class="n">term</span><span class="p">(</span><span class="n">stack</span><span class="p">,</span> <span class="n">expression</span><span class="p">,</span> <span class="n">dictionary</span><span class="p">)</span>
215     <span class="k">else</span><span class="p">:</span>
216       <span class="n">stack</span> <span class="o">=</span> <span class="n">term</span><span class="p">,</span> <span class="n">stack</span>
217
218   <span class="k">if</span> <span class="n">viewer</span><span class="p">:</span> <span class="n">viewer</span><span class="p">(</span><span class="n">stack</span><span class="p">,</span> <span class="n">expression</span><span class="p">)</span>
219   <span class="k">return</span> <span class="n">stack</span><span class="p">,</span> <span class="n">expression</span><span class="p">,</span> <span class="n">dictionary</span>
220 </pre></div>
221 </div>
222 <div class="section" id="view-function">
223 <h3>View function<a class="headerlink" href="#view-function" title="Permalink to this headline">¶</a></h3>
224 <p>The <code class="docutils literal notranslate"><span class="pre">joy()</span></code> function accepts a “viewer” function which it calls on
225 each iteration passing the current stack and expression just before
226 evaluation. This can be used for tracing, breakpoints, retrying after
227 exceptions, or interrupting an evaluation and saving to disk or sending
228 over the network to resume later. The stack and expression together
229 contain all the state of the computation at each step.</p>
230 </div>
231 <div class="section" id="the-traceprinter">
232 <h3>The <code class="docutils literal notranslate"><span class="pre">TracePrinter</span></code>.<a class="headerlink" href="#the-traceprinter" title="Permalink to this headline">¶</a></h3>
233 <p>A <code class="docutils literal notranslate"><span class="pre">viewer</span></code> records each step of the evaluation of a Joy program. The
234 <code class="docutils literal notranslate"><span class="pre">TracePrinter</span></code> has a facility for printing out a trace of the
235 evaluation, one line per step. Each step is aligned to the current
236 interpreter position, signified by a period separating the stack on the
237 left from the pending expression (“continuation”) on the right.</p>
238 </div>
239 <div class="section" id="continuation-passing-style">
240 <h3><a class="reference external" href="https://en.wikipedia.org/wiki/Continuation-passing_style">Continuation-Passing Style</a><a class="headerlink" href="#continuation-passing-style" title="Permalink to this headline">¶</a></h3>
241 <p>One day I thought, What happens if you rewrite Joy to use
242 <a class="reference external" href="https://en.wikipedia.org/wiki/Continuation-passing_style">CSP</a>? I
243 made all the functions accept and return the expression as well as the
244 stack and found that all the combinators could be rewritten to work by
245 modifying the expression rather than making recursive calls to the
246 <code class="docutils literal notranslate"><span class="pre">joy()</span></code> function.</p>
247 </div>
248 </div>
249 </div>
250 <div class="section" id="parser">
251 <h1>Parser<a class="headerlink" href="#parser" title="Permalink to this headline">¶</a></h1>
252 <div class="code ipython2 highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">joy.parser</span>
253
254 <span class="nb">print</span> <span class="n">inspect</span><span class="o">.</span><span class="n">getdoc</span><span class="p">(</span><span class="n">joy</span><span class="o">.</span><span class="n">parser</span><span class="p">)</span>
255 </pre></div>
256 </div>
257 <div class="highlight-default notranslate"><div class="highlight"><pre><span></span>§ Converting text to a joy expression.
258
259 This module exports a single function:
260
261   text_to_expression(text)
262
263 As well as a single Symbol class and a single Exception type:
264
265   ParseError
266
267 When supplied with a string this function returns a Python datastructure
268 that represents the Joy datastructure described by the text expression.
269 Any unbalanced square brackets will raise a ParseError.
270 </pre></div>
271 </div>
272 <p>The parser is extremely simple, the undocumented <code class="docutils literal notranslate"><span class="pre">re.Scanner</span></code> class
273 does most of the tokenizing work and then you just build the tuple
274 structure out of the tokens. There’s no Abstract Syntax Tree or anything
275 like that.</p>
276 <div class="code ipython2 highlight-default notranslate"><div class="highlight"><pre><span></span><span class="nb">print</span> <span class="n">inspect</span><span class="o">.</span><span class="n">getsource</span><span class="p">(</span><span class="n">joy</span><span class="o">.</span><span class="n">parser</span><span class="o">.</span><span class="n">_parse</span><span class="p">)</span>
277 </pre></div>
278 </div>
279 <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">_parse</span><span class="p">(</span><span class="n">tokens</span><span class="p">):</span>
280   <span class="sd">&#39;&#39;&#39;</span>
281 <span class="sd">  Return a stack/list expression of the tokens.</span>
282 <span class="sd">  &#39;&#39;&#39;</span>
283   <span class="n">frame</span> <span class="o">=</span> <span class="p">[]</span>
284   <span class="n">stack</span> <span class="o">=</span> <span class="p">[]</span>
285   <span class="k">for</span> <span class="n">tok</span> <span class="ow">in</span> <span class="n">tokens</span><span class="p">:</span>
286     <span class="k">if</span> <span class="n">tok</span> <span class="o">==</span> <span class="s1">&#39;[&#39;</span><span class="p">:</span>
287       <span class="n">stack</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">frame</span><span class="p">)</span>
288       <span class="n">frame</span> <span class="o">=</span> <span class="p">[]</span>
289       <span class="n">stack</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">frame</span><span class="p">)</span>
290     <span class="k">elif</span> <span class="n">tok</span> <span class="o">==</span> <span class="s1">&#39;]&#39;</span><span class="p">:</span>
291       <span class="k">try</span><span class="p">:</span>
292         <span class="n">frame</span> <span class="o">=</span> <span class="n">stack</span><span class="o">.</span><span class="n">pop</span><span class="p">()</span>
293       <span class="k">except</span> <span class="ne">IndexError</span><span class="p">:</span>
294         <span class="k">raise</span> <span class="n">ParseError</span><span class="p">(</span><span class="s1">&#39;One or more extra closing brackets.&#39;</span><span class="p">)</span>
295       <span class="n">frame</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="n">list_to_stack</span><span class="p">(</span><span class="n">frame</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span>
296     <span class="k">else</span><span class="p">:</span>
297       <span class="n">frame</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">tok</span><span class="p">)</span>
298   <span class="k">if</span> <span class="n">stack</span><span class="p">:</span>
299     <span class="k">raise</span> <span class="n">ParseError</span><span class="p">(</span><span class="s1">&#39;One or more unclosed brackets.&#39;</span><span class="p">)</span>
300   <span class="k">return</span> <span class="n">list_to_stack</span><span class="p">(</span><span class="n">frame</span><span class="p">)</span>
301 </pre></div>
302 </div>
303 <p>That’s pretty much all there is to it.</p>
304 <div class="code ipython2 highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">joy</span><span class="o">.</span><span class="n">parser</span><span class="o">.</span><span class="n">text_to_expression</span><span class="p">(</span><span class="s1">&#39;1 2 3 4 5&#39;</span><span class="p">)</span>  <span class="c1"># A simple sequence.</span>
305 </pre></div>
306 </div>
307 <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="p">(</span><span class="mi">4</span><span class="p">,</span> <span class="p">(</span><span class="mi">5</span><span class="p">,</span> <span class="p">())))))</span>
308 </pre></div>
309 </div>
310 <div class="code ipython2 highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">joy</span><span class="o">.</span><span class="n">parser</span><span class="o">.</span><span class="n">text_to_expression</span><span class="p">(</span><span class="s1">&#39;[1 2 3] 4 5&#39;</span><span class="p">)</span>  <span class="c1"># Three items, the first is a list with three items</span>
311 </pre></div>
312 </div>
313 <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="p">((</span><span class="mi">1</span><span class="p">,</span> <span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="p">()))),</span> <span class="p">(</span><span class="mi">4</span><span class="p">,</span> <span class="p">(</span><span class="mi">5</span><span class="p">,</span> <span class="p">())))</span>
314 </pre></div>
315 </div>
316 <div class="code ipython2 highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">joy</span><span class="o">.</span><span class="n">parser</span><span class="o">.</span><span class="n">text_to_expression</span><span class="p">(</span><span class="s1">&#39;1 23 [&quot;four&quot; [-5.0] cons] 8888&#39;</span><span class="p">)</span>  <span class="c1"># A mixed bag. cons is</span>
317                                                                  <span class="c1"># a Symbol, no lookup at</span>
318                                                                  <span class="c1"># parse-time.  Haiku docs.</span>
319 </pre></div>
320 </div>
321 <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="p">(</span><span class="mi">23</span><span class="p">,</span> <span class="p">((</span><span class="s1">&#39;four&#39;</span><span class="p">,</span> <span class="p">((</span><span class="o">-</span><span class="mf">5.0</span><span class="p">,</span> <span class="p">()),</span> <span class="p">(</span><span class="n">cons</span><span class="p">,</span> <span class="p">()))),</span> <span class="p">(</span><span class="mi">8888</span><span class="p">,</span> <span class="p">()))))</span>
322 </pre></div>
323 </div>
324 <div class="code ipython2 highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">joy</span><span class="o">.</span><span class="n">parser</span><span class="o">.</span><span class="n">text_to_expression</span><span class="p">(</span><span class="s1">&#39;[][][][][]&#39;</span><span class="p">)</span>  <span class="c1"># Five empty lists.</span>
325 </pre></div>
326 </div>
327 <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="p">((),</span> <span class="p">((),</span> <span class="p">((),</span> <span class="p">((),</span> <span class="p">((),</span> <span class="p">())))))</span>
328 </pre></div>
329 </div>
330 <div class="code ipython2 highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">joy</span><span class="o">.</span><span class="n">parser</span><span class="o">.</span><span class="n">text_to_expression</span><span class="p">(</span><span class="s1">&#39;[[[[[]]]]]&#39;</span><span class="p">)</span>  <span class="c1"># Five nested lists.</span>
331 </pre></div>
332 </div>
333 <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="p">((((((),</span> <span class="p">()),</span> <span class="p">()),</span> <span class="p">()),</span> <span class="p">()),</span> <span class="p">())</span>
334 </pre></div>
335 </div>
336 </div>
337 <div class="section" id="library">
338 <h1>Library<a class="headerlink" href="#library" title="Permalink to this headline">¶</a></h1>
339 <p>The Joy library of functions (aka commands, or “words” after Forth
340 usage) encapsulates all the actual functionality (no pun intended) of
341 the Joy system. There are simple functions such as addition <code class="docutils literal notranslate"><span class="pre">add</span></code> (or
342 <code class="docutils literal notranslate"><span class="pre">+</span></code>, the library module supports aliases), and combinators which
343 provide control-flow and higher-order operations.</p>
344 <div class="code ipython2 highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">joy.library</span>
345
346 <span class="nb">print</span> <span class="s1">&#39; &#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="nb">sorted</span><span class="p">(</span><span class="n">joy</span><span class="o">.</span><span class="n">library</span><span class="o">.</span><span class="n">initialize</span><span class="p">()))</span>
347 </pre></div>
348 </div>
349 <div class="highlight-default notranslate"><div class="highlight"><pre><span></span>!= % &amp; * *fraction *fraction0 + ++ - -- / &lt; &lt;&lt; &lt;= &lt;&gt; = &gt; &gt;= &gt;&gt; ? ^ 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&lt; roll&gt; 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 •
350 </pre></div>
351 </div>
352 <p>Many of the functions are defined in Python, like <code class="docutils literal notranslate"><span class="pre">dip</span></code>:</p>
353 <div class="code ipython2 highlight-default notranslate"><div class="highlight"><pre><span></span><span class="nb">print</span> <span class="n">inspect</span><span class="o">.</span><span class="n">getsource</span><span class="p">(</span><span class="n">joy</span><span class="o">.</span><span class="n">library</span><span class="o">.</span><span class="n">dip</span><span class="p">)</span>
354 </pre></div>
355 </div>
356 <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">dip</span><span class="p">(</span><span class="n">stack</span><span class="p">,</span> <span class="n">expression</span><span class="p">,</span> <span class="n">dictionary</span><span class="p">):</span>
357   <span class="p">(</span><span class="n">quote</span><span class="p">,</span> <span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">stack</span><span class="p">))</span> <span class="o">=</span> <span class="n">stack</span>
358   <span class="n">expression</span> <span class="o">=</span> <span class="n">x</span><span class="p">,</span> <span class="n">expression</span>
359   <span class="k">return</span> <span class="n">stack</span><span class="p">,</span> <span class="n">pushback</span><span class="p">(</span><span class="n">quote</span><span class="p">,</span> <span class="n">expression</span><span class="p">),</span> <span class="n">dictionary</span>
360 </pre></div>
361 </div>
362 <p>Some functions are defined in equations in terms of other functions.
363 When the interpreter executes a definition function that function just
364 pushes its body expression onto the pending expression (the
365 continuation) and returns control to the interpreter.</p>
366 <div class="code ipython2 highlight-default notranslate"><div class="highlight"><pre><span></span><span class="nb">print</span> <span class="n">joy</span><span class="o">.</span><span class="n">library</span><span class="o">.</span><span class="n">definitions</span>
367 </pre></div>
368 </div>
369 <pre class="literal-block">
370 second == rest first
371 third == rest rest first
372 product == 1 swap [*] step
373 swons == swap cons
374 swoncat == swap concat
375 flatten == [] swap [concat] step
376 unit == [] cons
377 quoted == [unit] dip
378 unquoted == [i] dip
379 enstacken == stack [clear] dip
380 disenstacken == ? [uncons ?] loop pop
381 ? == dup truthy
382 dinfrirst == dip infra first
383 nullary == [stack] dinfrirst
384 unary == [stack [pop] dip] dinfrirst
385 binary == [stack [popop] dip] dinfrirst
386 ternary == [stack [popop pop] dip] dinfrirst
387 pam == [i] map
388 run == [] swap infra
389 sqr == dup mul
390 size == 0 swap [pop ++] step
391 cleave == [i] app2 [popd] dip
392 average == [sum 1.0 <em>] [size] cleave /
393 gcd == 1 [tuck modulus dup 0 &gt;] loop pop
394 least_fraction == dup [gcd] infra [div] concat map
395 *fraction == [uncons] dip uncons [swap] dip concat [</em>] infra [*] dip cons
396 <em>fraction0 == concat [[swap] dip * [</em>] dip] infra
397 down_to_zero == [0 &gt;] [dup --] while
398 range_to_zero == unit [down_to_zero] infra
399 anamorphism == [pop []] swap [dip swons] genrec
400 range == [0 &lt;=] [1 - dup] anamorphism
401 while == swap [nullary] cons dup dipd concat loop
402 dudipd == dup dipd
403 primrec == [i] genrec
404 </pre>
405 <p>Currently, there’s no function to add new definitions to the dictionary
406 from “within” Joy code itself. Adding new definitions remains a
407 meta-interpreter action. You have to do it yourself, in Python, and wash
408 your hands afterward.</p>
409 <p>It would be simple enough to define one, but it would open the door to
410 <em>name binding</em> and break the idea that all state is captured in the
411 stack and expression. There’s an implicit <em>standard dictionary</em> that
412 defines the actual semantics of the syntactic stack and expression
413 datastructures (which only contain symbols, not the actual functions.
414 Pickle some and see for yourself.)</p>
415 <p>Which brings me to talking about one of my hopes and dreams for this
416 notation: “There should be only one.” What I mean is that there should
417 be one universal standard dictionary of commands, and all bespoke work
418 done in a UI for purposes takes place by direct interaction and macros.
419 There would be a <em>Grand Refactoring</em> biannually (two years, not six
420 months, that’s semi-annually) where any new definitions factored out of
421 the usage and macros of the previous time, along with new algorithms and
422 such, were entered into the dictionary and posted to e.g. IPFS.</p>
423 <p>Code should not burgeon wildly, as it does today. The variety of code
424 should map more-or-less to the well-factored variety of human
425 computably-solvable problems. There shouldn’t be dozens of chat apps, JS
426 frameworks, programming languages. It’s a waste of time, a <a class="reference external" href="https://en.wikipedia.org/wiki/Thundering_herd_problem">fractal
427 “thundering herd”
428 attack</a> on
429 human mentality.</p>
430 <p>If you read over the other notebooks you’ll see that developing code in
431 Joy is a lot like doing simple mathematics, and the descriptions of the
432 code resemble math papers. The code also works the first time, no bugs.
433 If you have any experience programming at all, you are probably
434 skeptical, as I was, but it seems to work: deriving code mathematically
435 seems to lead to fewer errors.</p>
436 <p>But my point now is that this great ratio of textual explanation to wind
437 up with code that consists of a few equations and could fit on an index
438 card is highly desirable. Less code has fewer errors. The structure of
439 Joy engenders a kind of thinking that seems to be very effective for
440 developing structured processes.</p>
441 <p>There seems to be an elegance and power to the notation.</p>
442 </div>
443
444
445           </div>
446         </div>
447       </div>
448       <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
449         <div class="sphinxsidebarwrapper">
450   <h3><a href="../index.html">Table Of Contents</a></h3>
451   <ul>
452 <li><a class="reference internal" href="#">Joypy</a><ul>
453 <li><a class="reference internal" href="#joy-in-python">Joy in Python</a><ul>
454 <li><a class="reference internal" href="#read-eval-print-loop-repl">Read-Eval-Print Loop (REPL)</a></li>
455 </ul>
456 </li>
457 </ul>
458 </li>
459 <li><a class="reference internal" href="#stacks-aka-list-quote-sequence-etc">Stacks (aka list, quote, sequence, etc.)</a></li>
460 <li><a class="reference internal" href="#the-joy-function">The <code class="docutils literal notranslate"><span class="pre">joy()</span></code> function.</a><ul>
461 <li><a class="reference internal" href="#an-interpreter">An Interpreter</a><ul>
462 <li><a class="reference internal" href="#view-function">View function</a></li>
463 <li><a class="reference internal" href="#the-traceprinter">The <code class="docutils literal notranslate"><span class="pre">TracePrinter</span></code>.</a></li>
464 <li><a class="reference internal" href="#continuation-passing-style">Continuation-Passing Style</a></li>
465 </ul>
466 </li>
467 </ul>
468 </li>
469 <li><a class="reference internal" href="#parser">Parser</a></li>
470 <li><a class="reference internal" href="#library">Library</a></li>
471 </ul>
472 <div class="relations">
473 <h3>Related Topics</h3>
474 <ul>
475   <li><a href="../index.html">Documentation overview</a><ul>
476   </ul></li>
477 </ul>
478 </div>
479   <div role="note" aria-label="source link">
480     <h3>This Page</h3>
481     <ul class="this-page-menu">
482       <li><a href="../_sources/notebooks/0. This Implementation of Joy in Python.rst.txt"
483             rel="nofollow">Show Source</a></li>
484     </ul>
485    </div>
486 <div id="searchbox" style="display: none" role="search">
487   <h3>Quick search</h3>
488     <div class="searchformwrapper">
489     <form class="search" action="../search.html" method="get">
490       <input type="text" name="q" />
491       <input type="submit" value="Go" />
492       <input type="hidden" name="check_keywords" value="yes" />
493       <input type="hidden" name="area" value="default" />
494     </form>
495     </div>
496 </div>
497 <script type="text/javascript">$('#searchbox').show(0);</script>
498         </div>
499       </div>
500       <div class="clearer"></div>
501     </div>
502     <div class="footer" role="contentinfo">
503 <a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/4.0/">
504 <img alt="Creative Commons License" style="border-width:0" src="https://i.creativecommons.org/l/by-nc-sa/4.0/88x31.png" />
505 </a>
506 <br />
507 <span xmlns:dct="http://purl.org/dc/terms/" property="dct:title">Thun Documentation</span> by <a xmlns:cc="http://creativecommons.org/ns#" href="https://joypy.osdn.io/" property="cc:attributionName" rel="cc:attributionURL">Simon Forman</a> is licensed under a <a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/4.0/">Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License</a>.<br />Based on a work at <a xmlns:dct="http://purl.org/dc/terms/" href="https://osdn.net/projects/joypy/" rel="dct:source">https://osdn.net/projects/joypy/</a>.
508       Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.7.3.
509     </div>
510
511   </body>
512 </html>