More docs changes.

This commit is contained in:
Simon Forman 2018-04-30 10:12:56 -07:00
parent f3b72b1938
commit 04e8f70dd2
17 changed files with 513 additions and 627 deletions

View File

@ -75,11 +75,11 @@
<span class="sd"> or functions. Literals are put onto the stack and functions are</span> <span class="sd"> or functions. Literals are put onto the stack and functions are</span>
<span class="sd"> executed.</span> <span class="sd"> executed.</span>
<span class="sd"> :param stack stack: The stack.</span>
<span class="sd"> :param quote stack: The stack.</span> <span class="sd"> :param stack expression: The expression to evaluate.</span>
<span class="sd"> :param quote expression: The expression to evaluate.</span> <span class="sd"> :param dict dictionary: A ``dict`` mapping names to Joy functions.</span>
<span class="sd"> :param dict dictionary: A `dict` mapping names to Joy functions.</span>
<span class="sd"> :param function viewer: Optional viewer function.</span> <span class="sd"> :param function viewer: Optional viewer function.</span>
<span class="sd"> :rtype: (stack, (), dictionary)</span>
<span class="sd"> &#39;&#39;&#39;</span> <span class="sd"> &#39;&#39;&#39;</span>
<span class="k">while</span> <span class="n">expression</span><span class="p">:</span> <span class="k">while</span> <span class="n">expression</span><span class="p">:</span>
@ -100,6 +100,13 @@
<div class="viewcode-block" id="run"><a class="viewcode-back" href="../../joy.html#joy.joy.run">[docs]</a><span class="k">def</span> <span class="nf">run</span><span class="p">(</span><span class="n">text</span><span class="p">,</span> <span class="n">stack</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> <div class="viewcode-block" id="run"><a class="viewcode-back" href="../../joy.html#joy.joy.run">[docs]</a><span class="k">def</span> <span class="nf">run</span><span class="p">(</span><span class="n">text</span><span class="p">,</span> <span class="n">stack</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>
<span class="sd">&#39;&#39;&#39;</span> <span class="sd">&#39;&#39;&#39;</span>
<span class="sd"> Return the stack resulting from running the Joy code text on the stack.</span> <span class="sd"> Return the stack resulting from running the Joy code text on the stack.</span>
<span class="sd"> :param str text: Joy code.</span>
<span class="sd"> :param stack stack: The stack.</span>
<span class="sd"> :param dict dictionary: A ``dict`` mapping names to Joy functions.</span>
<span class="sd"> :param function viewer: Optional viewer function.</span>
<span class="sd"> :rtype: (stack, (), dictionary)</span>
<span class="sd"> &#39;&#39;&#39;</span> <span class="sd"> &#39;&#39;&#39;</span>
<span class="n">expression</span> <span class="o">=</span> <span class="n">text_to_expression</span><span class="p">(</span><span class="n">text</span><span class="p">)</span> <span class="n">expression</span> <span class="o">=</span> <span class="n">text_to_expression</span><span class="p">(</span><span class="n">text</span><span class="p">)</span>
<span class="k">return</span> <span class="n">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="p">)</span></div> <span class="k">return</span> <span class="n">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="p">)</span></div>
@ -110,6 +117,11 @@
<span class="sd"> Read-Evaluate-Print Loop</span> <span class="sd"> Read-Evaluate-Print Loop</span>
<span class="sd"> Accept input and run it on the stack, loop.</span> <span class="sd"> Accept input and run it on the stack, loop.</span>
<span class="sd"> :param stack stack: The stack.</span>
<span class="sd"> :param dict dictionary: A ``dict`` mapping names to Joy functions.</span>
<span class="sd"> :rtype: stack</span>
<span class="sd"> &#39;&#39;&#39;</span> <span class="sd"> &#39;&#39;&#39;</span>
<span class="k">if</span> <span class="n">dictionary</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span> <span class="k">if</span> <span class="n">dictionary</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
<span class="n">dictionary</span> <span class="o">=</span> <span class="p">{}</span> <span class="n">dictionary</span> <span class="o">=</span> <span class="p">{}</span>

View File

@ -157,6 +157,13 @@
<span class="s1">dudipd == dup dipd</span> <span class="s1">dudipd == dup dipd</span>
<span class="s1">primrec == [i] genrec</span> <span class="s1">primrec == [i] genrec</span>
<span class="s1">step_zero == 0 roll&gt; step</span> <span class="s1">step_zero == 0 roll&gt; step</span>
<span class="s1">direco == dip rest cons</span>
<span class="s1">make_generator == [direco] cons [swap] swoncat cons</span>
<span class="s1">gsra == 1 swap [over / + 2 /] cons [dup] swoncat make_generator</span>
<span class="s1">_within_P == [first - abs] dip &lt;=</span>
<span class="s1">_within_B == roll&lt; popop first</span>
<span class="s1">_within_R == [popd x] dip</span>
<span class="s1">within == x 0.000001 [_within_P] [_within_B] [_within_R] primrec</span>
<span class="s1">&#39;&#39;&#39;</span> <span class="s1">&#39;&#39;&#39;</span>
<span class="c1">##Zipper</span> <span class="c1">##Zipper</span>

View File

@ -84,7 +84,7 @@
<span class="sd"> Any unbalanced square brackets will raise a ParseError.</span> <span class="sd"> Any unbalanced square brackets will raise a ParseError.</span>
<span class="sd"> :param str text: Text to convert.</span> <span class="sd"> :param str text: Text to convert.</span>
<span class="sd"> :rtype: quote</span> <span class="sd"> :rtype: stack</span>
<span class="sd"> :raises ParseError: if the parse fails.</span> <span class="sd"> :raises ParseError: if the parse fails.</span>
<span class="sd"> &#39;&#39;&#39;</span> <span class="sd"> &#39;&#39;&#39;</span>
<span class="k">return</span> <span class="n">_parse</span><span class="p">(</span><span class="n">_tokenize</span><span class="p">(</span><span class="n">text</span><span class="p">))</span></div> <span class="k">return</span> <span class="n">_parse</span><span class="p">(</span><span class="n">_tokenize</span><span class="p">(</span><span class="n">text</span><span class="p">))</span></div>

View File

@ -51,7 +51,24 @@
<span class="c1"># along with Thun. If not see &lt;http://www.gnu.org/licenses/&gt;.</span> <span class="c1"># along with Thun. If not see &lt;http://www.gnu.org/licenses/&gt;.</span>
<span class="c1">#</span> <span class="c1">#</span>
<span class="sd">&#39;&#39;&#39;</span> <span class="sd">&#39;&#39;&#39;</span>
<span class="sd">Pretty printing support.</span> <span class="sd">Pretty printing support, e.g.::</span>
<span class="sd"> Joy? 23 18 * 99 +</span>
<span class="sd"> . 23 18 mul 99 add</span>
<span class="sd"> 23 . 18 mul 99 add</span>
<span class="sd"> 23 18 . mul 99 add</span>
<span class="sd"> 414 . 99 add</span>
<span class="sd"> 414 99 . add</span>
<span class="sd"> 513 . </span>
<span class="sd"> 513 &lt;-top</span>
<span class="sd"> joy? </span>
<span class="sd">On each line the stack is printed with the top to the right, then a ``.`` to</span>
<span class="sd">represent the current locus of processing, then the pending expression to the</span>
<span class="sd">left.</span>
<span class="sd">&#39;&#39;&#39;</span> <span class="sd">&#39;&#39;&#39;</span>
<span class="c1"># (Kinda clunky and hacky. This should be swapped out in favor of much</span> <span class="c1"># (Kinda clunky and hacky. This should be swapped out in favor of much</span>
<span class="c1"># smarter stuff.)</span> <span class="c1"># smarter stuff.)</span>
@ -62,29 +79,36 @@
<div class="viewcode-block" id="TracePrinter"><a class="viewcode-back" href="../../../pretty.html#joy.utils.pretty_print.TracePrinter">[docs]</a><span class="k">class</span> <span class="nc">TracePrinter</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span> <div class="viewcode-block" id="TracePrinter"><a class="viewcode-back" href="../../../pretty.html#joy.utils.pretty_print.TracePrinter">[docs]</a><span class="k">class</span> <span class="nc">TracePrinter</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="sd">&#39;&#39;&#39;</span> <span class="sd">&#39;&#39;&#39;</span>
<span class="sd"> This is what does the formatting, e.g.::</span> <span class="sd"> This is what does the formatting. You instantiate it and pass the ``viewer()``</span>
<span class="sd"> method to the :py:func:`joy.joy.joy` function, then print it to see the</span>
<span class="sd"> Joy? 23 18 * 99 +</span> <span class="sd"> trace.</span>
<span class="sd"> . 23 18 mul 99 add</span>
<span class="sd"> 23 . 18 mul 99 add</span>
<span class="sd"> 23 18 . mul 99 add</span>
<span class="sd"> 414 . 99 add</span>
<span class="sd"> 414 99 . add</span>
<span class="sd"> 513 . </span>
<span class="sd"> &#39;&#39;&#39;</span> <span class="sd"> &#39;&#39;&#39;</span>
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">history</span> <span class="o">=</span> <span class="p">[]</span> <span class="bp">self</span><span class="o">.</span><span class="n">history</span> <span class="o">=</span> <span class="p">[]</span>
<div class="viewcode-block" id="TracePrinter.viewer"><a class="viewcode-back" href="../../../pretty.html#joy.utils.pretty_print.TracePrinter.viewer">[docs]</a> <span class="k">def</span> <span class="nf">viewer</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">stack</span><span class="p">,</span> <span class="n">expression</span><span class="p">):</span> <div class="viewcode-block" id="TracePrinter.viewer"><a class="viewcode-back" href="../../../pretty.html#joy.utils.pretty_print.TracePrinter.viewer">[docs]</a> <span class="k">def</span> <span class="nf">viewer</span><span class="p">(</span><span class="bp">self</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="sd">&#39;&#39;&#39;Pass this method as the viewer to joy() function.&#39;&#39;&#39;</span> <span class="sd">&#39;&#39;&#39;</span>
<span class="sd"> Record the current stack and expression in the TracePrinter&#39;s history.</span>
<span class="sd"> Pass this method as the ``viewer`` argument to the :py:func:`joy.joy.joy` function.</span>
<span class="sd"> :param stack quote: A stack.</span>
<span class="sd"> :param stack expression: A stack.</span>
<span class="sd"> &#39;&#39;&#39;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">history</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">stack</span><span class="p">,</span> <span class="n">expression</span><span class="p">))</span></div> <span class="bp">self</span><span class="o">.</span><span class="n">history</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">stack</span><span class="p">,</span> <span class="n">expression</span><span class="p">))</span></div>
<span class="k">def</span> <span class="nf">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="k">def</span> <span class="nf">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">return</span> <span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">go</span><span class="p">())</span> <span class="k">return</span> <span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">go</span><span class="p">())</span>
<span class="k">def</span> <span class="nf">go</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <div class="viewcode-block" id="TracePrinter.go"><a class="viewcode-back" href="../../../pretty.html#joy.utils.pretty_print.TracePrinter.go">[docs]</a> <span class="k">def</span> <span class="nf">go</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="sd">&#39;&#39;&#39;</span>
<span class="sd"> Return a list of strings, one for each entry in the history, prefixed</span>
<span class="sd"> with enough spaces to align all the interpreter dots.</span>
<span class="sd"> This method is called internally by the ``__str__()`` method.</span>
<span class="sd"> :rtype: list(str)</span>
<span class="sd"> &#39;&#39;&#39;</span>
<span class="n">max_stack_length</span> <span class="o">=</span> <span class="mi">0</span> <span class="n">max_stack_length</span> <span class="o">=</span> <span class="mi">0</span>
<span class="n">lines</span> <span class="o">=</span> <span class="p">[]</span> <span class="n">lines</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">stack</span><span class="p">,</span> <span class="n">expression</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">history</span><span class="p">:</span> <span class="k">for</span> <span class="n">stack</span><span class="p">,</span> <span class="n">expression</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">history</span><span class="p">:</span>
@ -97,7 +121,7 @@
<span class="k">return</span> <span class="p">[</span> <span class="c1"># Prefix spaces to line up &#39;.&#39;s.</span> <span class="k">return</span> <span class="p">[</span> <span class="c1"># Prefix spaces to line up &#39;.&#39;s.</span>
<span class="p">(</span><span class="s1">&#39; &#39;</span> <span class="o">*</span> <span class="p">(</span><span class="n">max_stack_length</span> <span class="o">-</span> <span class="n">length</span><span class="p">)</span> <span class="o">+</span> <span class="n">line</span><span class="p">)</span> <span class="p">(</span><span class="s1">&#39; &#39;</span> <span class="o">*</span> <span class="p">(</span><span class="n">max_stack_length</span> <span class="o">-</span> <span class="n">length</span><span class="p">)</span> <span class="o">+</span> <span class="n">line</span><span class="p">)</span>
<span class="k">for</span> <span class="n">length</span><span class="p">,</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">lines</span> <span class="k">for</span> <span class="n">length</span><span class="p">,</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">lines</span>
<span class="p">]</span> <span class="p">]</span></div>
<span class="k">def</span> <span class="nf">print_</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="k">def</span> <span class="nf">print_</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">try</span><span class="p">:</span> <span class="k">try</span><span class="p">:</span>

View File

@ -51,12 +51,13 @@
<span class="c1"># along with Thun. If not see &lt;http://www.gnu.org/licenses/&gt;.</span> <span class="c1"># along with Thun. If not see &lt;http://www.gnu.org/licenses/&gt;.</span>
<span class="c1">#</span> <span class="c1">#</span>
<span class="sd">&#39;&#39;&#39;</span> <span class="sd">&#39;&#39;&#39;</span>
<span class="sd">When talking about Joy we use the terms &quot;stack&quot;, &quot;list&quot;, &quot;sequence&quot;,</span> <span class="sd">When talking about Joy we use the terms &quot;stack&quot;, &quot;quote&quot;, &quot;sequence&quot;,</span>
<span class="sd">&quot;quote&quot; and others to mean the same thing: a simple linear datatype that</span> <span class="sd">&quot;list&quot;, and others to mean the same thing: a simple linear datatype that</span>
<span class="sd">permits certain operations such as iterating and pushing and popping</span> <span class="sd">permits certain operations such as iterating and pushing and popping</span>
<span class="sd">values from (at least) one end.</span> <span class="sd">values from (at least) one end.</span>
<span class="sd">We use the `cons list`_, a venerable two-tuple recursive sequence datastructure, where the</span> <span class="sd">There is no &quot;Stack&quot; Python class, instead we use the `cons list`_, a </span>
<span class="sd">venerable two-tuple recursive sequence datastructure, where the</span>
<span class="sd">empty tuple ``()`` is the empty stack and ``(head, rest)`` gives the recursive</span> <span class="sd">empty tuple ``()`` is the empty stack and ``(head, rest)`` gives the recursive</span>
<span class="sd">form of a stack with one or more items on it::</span> <span class="sd">form of a stack with one or more items on it::</span>
@ -84,33 +85,36 @@
<span class="sd">syntax doesn&#39;t require parentheses around tuples used in expressions</span> <span class="sd">syntax doesn&#39;t require parentheses around tuples used in expressions</span>
<span class="sd">where they would be redundant.)</span> <span class="sd">where they would be redundant.)</span>
<span class="sd">Unfortunately, the Sphinx documentation generator, which is used to generate this</span>
<span class="sd">web page, doesn&#39;t handle tuples in the function parameters. And in Python 3, this</span>
<span class="sd">syntax was removed entirely. Instead you would have to write::</span>
<span class="sd"> def dup(stack):</span>
<span class="sd"> head, tail = stack</span>
<span class="sd"> return head, (head, tail)</span>
<span class="sd">We have two very simple functions, one to build up a stack from a Python</span>
<span class="sd">iterable and another to iterate through a stack and yield its items</span>
<span class="sd">one-by-one in order. There are also two functions to generate string representations</span>
<span class="sd">of stacks. They only differ in that one prints the terms in stack from left-to-right while the other prints from right-to-left. In both functions *internal stacks* are</span>
<span class="sd">printed left-to-right. These functions are written to support :doc:`../pretty`.</span>
<span class="sd">.. _cons list: https://en.wikipedia.org/wiki/Cons#Lists</span> <span class="sd">.. _cons list: https://en.wikipedia.org/wiki/Cons#Lists</span>
<span class="sd">&#39;&#39;&#39;</span> <span class="sd">&#39;&#39;&#39;</span>
<span class="c1">##We have two very simple functions to build up a stack from a Python</span>
<span class="c1">##iterable and also to iterate through a stack and yield its items</span>
<span class="c1">##one-by-one in order, and two functions to generate string representations</span>
<span class="c1">##of stacks::</span>
<span class="c1">##</span>
<span class="c1">## list_to_stack()</span>
<span class="c1">##</span>
<span class="c1">## iter_stack()</span>
<span class="c1">##</span>
<span class="c1">## expression_to_string() (prints left-to-right)</span>
<span class="c1">##</span>
<span class="c1">## stack_to_string() (prints right-to-left)</span>
<span class="c1">##</span>
<span class="c1">##</span>
<span class="c1">##A word about the stack data structure.</span>
<div class="viewcode-block" id="list_to_stack"><a class="viewcode-back" href="../../../stack.html#joy.utils.stack.list_to_stack">[docs]</a><span class="k">def</span> <span class="nf">list_to_stack</span><span class="p">(</span><span class="n">el</span><span class="p">,</span> <span class="n">stack</span><span class="o">=</span><span class="p">()):</span> <div class="viewcode-block" id="list_to_stack"><a class="viewcode-back" href="../../../stack.html#joy.utils.stack.list_to_stack">[docs]</a><span class="k">def</span> <span class="nf">list_to_stack</span><span class="p">(</span><span class="n">el</span><span class="p">,</span> <span class="n">stack</span><span class="o">=</span><span class="p">()):</span>
<span class="sd">&#39;&#39;&#39;Convert a Python list (or other sequence) to a Joy stack::</span> <span class="sd">&#39;&#39;&#39;Convert a Python list (or other sequence) to a Joy stack::</span>
<span class="sd"> [1, 2, 3] -&gt; (1, (2, (3, ())))</span> <span class="sd"> [1, 2, 3] -&gt; (1, (2, (3, ())))</span>
<span class="sd"> :param list el: A Python list or other sequence (iterators and generators</span>
<span class="sd"> won&#39;t work because ``reverse()`` is called on ``el``.)</span>
<span class="sd"> :param stack stack: A stack, optional, defaults to the empty stack.</span>
<span class="sd"> :rtype: stack</span>
<span class="sd"> &#39;&#39;&#39;</span> <span class="sd"> &#39;&#39;&#39;</span>
<span class="k">for</span> <span class="n">item</span> <span class="ow">in</span> <span class="nb">reversed</span><span class="p">(</span><span class="n">el</span><span class="p">):</span> <span class="k">for</span> <span class="n">item</span> <span class="ow">in</span> <span class="nb">reversed</span><span class="p">(</span><span class="n">el</span><span class="p">):</span>
<span class="n">stack</span> <span class="o">=</span> <span class="n">item</span><span class="p">,</span> <span class="n">stack</span> <span class="n">stack</span> <span class="o">=</span> <span class="n">item</span><span class="p">,</span> <span class="n">stack</span>
@ -118,7 +122,11 @@
<div class="viewcode-block" id="iter_stack"><a class="viewcode-back" href="../../../stack.html#joy.utils.stack.iter_stack">[docs]</a><span class="k">def</span> <span class="nf">iter_stack</span><span class="p">(</span><span class="n">stack</span><span class="p">):</span> <div class="viewcode-block" id="iter_stack"><a class="viewcode-back" href="../../../stack.html#joy.utils.stack.iter_stack">[docs]</a><span class="k">def</span> <span class="nf">iter_stack</span><span class="p">(</span><span class="n">stack</span><span class="p">):</span>
<span class="sd">&#39;&#39;&#39;Iterate through the items on the stack.&#39;&#39;&#39;</span> <span class="sd">&#39;&#39;&#39;Iterate through the items on the stack.</span>
<span class="sd"> :param stack stack: A stack.</span>
<span class="sd"> :rtype: iterator</span>
<span class="sd"> &#39;&#39;&#39;</span>
<span class="k">while</span> <span class="n">stack</span><span class="p">:</span> <span class="k">while</span> <span class="n">stack</span><span class="p">:</span>
<span class="n">item</span><span class="p">,</span> <span class="n">stack</span> <span class="o">=</span> <span class="n">stack</span> <span class="n">item</span><span class="p">,</span> <span class="n">stack</span> <span class="o">=</span> <span class="n">stack</span>
<span class="k">yield</span> <span class="n">item</span></div> <span class="k">yield</span> <span class="n">item</span></div>
@ -131,6 +139,9 @@
<span class="sd"> The items are written right-to-left::</span> <span class="sd"> The items are written right-to-left::</span>
<span class="sd"> (top, (second, ...)) -&gt; &#39;... second top&#39;</span> <span class="sd"> (top, (second, ...)) -&gt; &#39;... second top&#39;</span>
<span class="sd"> :param stack stack: A stack.</span>
<span class="sd"> :rtype: str</span>
<span class="sd"> &#39;&#39;&#39;</span> <span class="sd"> &#39;&#39;&#39;</span>
<span class="n">f</span> <span class="o">=</span> <span class="k">lambda</span> <span class="n">stack</span><span class="p">:</span> <span class="nb">reversed</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="n">iter_stack</span><span class="p">(</span><span class="n">stack</span><span class="p">)))</span> <span class="n">f</span> <span class="o">=</span> <span class="k">lambda</span> <span class="n">stack</span><span class="p">:</span> <span class="nb">reversed</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="n">iter_stack</span><span class="p">(</span><span class="n">stack</span><span class="p">)))</span>
<span class="k">return</span> <span class="n">_to_string</span><span class="p">(</span><span class="n">stack</span><span class="p">,</span> <span class="n">f</span><span class="p">)</span></div> <span class="k">return</span> <span class="n">_to_string</span><span class="p">(</span><span class="n">stack</span><span class="p">,</span> <span class="n">f</span><span class="p">)</span></div>
@ -143,6 +154,9 @@
<span class="sd"> The items are written left-to-right::</span> <span class="sd"> The items are written left-to-right::</span>
<span class="sd"> (top, (second, ...)) -&gt; &#39;top second ...&#39;</span> <span class="sd"> (top, (second, ...)) -&gt; &#39;top second ...&#39;</span>
<span class="sd"> :param stack expression: A stack.</span>
<span class="sd"> :rtype: str</span>
<span class="sd"> &#39;&#39;&#39;</span> <span class="sd"> &#39;&#39;&#39;</span>
<span class="k">return</span> <span class="n">_to_string</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">iter_stack</span><span class="p">)</span></div> <span class="k">return</span> <span class="n">_to_string</span><span class="p">(</span><span class="n">expression</span><span class="p">,</span> <span class="n">iter_stack</span><span class="p">)</span></div>
@ -165,7 +179,16 @@
<span class="sd">&#39;&#39;&#39;Concatinate quote onto expression.</span> <span class="sd">&#39;&#39;&#39;Concatinate quote onto expression.</span>
<span class="sd"> In joy [1 2] [3 4] would become [1 2 3 4].</span> <span class="sd"> In joy [1 2] [3 4] would become [1 2 3 4].</span>
<span class="sd"> :param stack quote: A stack.</span>
<span class="sd"> :param stack expression: A stack.</span>
<span class="sd"> :raises RuntimeError: if quote is larger than sys.getrecursionlimit().</span>
<span class="sd"> :rtype: stack</span>
<span class="sd"> &#39;&#39;&#39;</span> <span class="sd"> &#39;&#39;&#39;</span>
<span class="c1"># This is the fastest implementation, but will trigger</span>
<span class="c1"># RuntimeError: maximum recursion depth exceeded</span>
<span class="c1"># on quotes longer than sys.getrecursionlimit().</span>
<span class="k">return</span> <span class="p">(</span><span class="n">quote</span><span class="p">[</span><span class="mi">0</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="mi">1</span><span class="p">],</span> <span class="n">expression</span><span class="p">))</span> <span class="k">if</span> <span class="n">quote</span> <span class="k">else</span> <span class="n">expression</span></div>
<span class="c1"># Original implementation.</span> <span class="c1"># Original implementation.</span>
@ -182,15 +205,17 @@
<span class="c1">## expression = item, expression</span> <span class="c1">## expression = item, expression</span>
<span class="c1">## return expression</span> <span class="c1">## return expression</span>
<span class="c1"># This is the fastest, but will trigger</span>
<span class="c1"># RuntimeError: maximum recursion depth exceeded</span>
<span class="c1"># on quotes longer than sys.getrecursionlimit().</span>
<span class="k">return</span> <span class="p">(</span><span class="n">quote</span><span class="p">[</span><span class="mi">0</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="mi">1</span><span class="p">],</span> <span class="n">expression</span><span class="p">))</span> <span class="k">if</span> <span class="n">quote</span> <span class="k">else</span> <span class="n">expression</span></div>
<div class="viewcode-block" id="pick"><a class="viewcode-back" href="../../../stack.html#joy.utils.stack.pick">[docs]</a><span class="k">def</span> <span class="nf">pick</span><span class="p">(</span><span class="n">s</span><span class="p">,</span> <span class="n">n</span><span class="p">):</span> <div class="viewcode-block" id="pick"><a class="viewcode-back" href="../../../stack.html#joy.utils.stack.pick">[docs]</a><span class="k">def</span> <span class="nf">pick</span><span class="p">(</span><span class="n">s</span><span class="p">,</span> <span class="n">n</span><span class="p">):</span>
<span class="sd">&#39;&#39;&#39;</span> <span class="sd">&#39;&#39;&#39;</span>
<span class="sd"> Find the nth item on the stack. (Pick with zero is the same as &quot;dup&quot;.)</span> <span class="sd"> Return the nth item on the stack.</span>
<span class="sd"> :param stack s: A stack.</span>
<span class="sd"> :param int n: An index into the stack.</span>
<span class="sd"> :raises ValueError: if ``n`` is less than zero.</span>
<span class="sd"> :raises IndexError: if ``n`` is equal to or greater than the length of ``s``.</span>
<span class="sd"> :rtype: whatever</span>
<span class="sd"> &#39;&#39;&#39;</span> <span class="sd"> &#39;&#39;&#39;</span>
<span class="k">if</span> <span class="n">n</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">:</span> <span class="k">if</span> <span class="n">n</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">:</span>
<span class="k">raise</span> <span class="ne">ValueError</span> <span class="k">raise</span> <span class="ne">ValueError</span>

View File

@ -165,6 +165,8 @@
</ul></td> </ul></td>
<td style="width: 33%; vertical-align: top;"><ul> <td style="width: 33%; vertical-align: top;"><ul>
<li><a href="library.html#joy.library.getitem">getitem() (in module joy.library)</a> <li><a href="library.html#joy.library.getitem">getitem() (in module joy.library)</a>
</li>
<li><a href="pretty.html#joy.utils.pretty_print.TracePrinter.go">go() (joy.utils.pretty_print.TracePrinter method)</a>
</li> </li>
</ul></td> </ul></td>
</tr></table> </tr></table>

View File

@ -93,7 +93,7 @@ interesting aspects. Its quite a treasure trove.</p>
<li class="toctree-l1"><a class="reference internal" href="notebooks/Intro.html">Thun: Joy in Python</a><ul> <li class="toctree-l1"><a class="reference internal" href="notebooks/Intro.html">Thun: Joy in Python</a><ul>
<li class="toctree-l2"><a class="reference internal" href="notebooks/Intro.html#read-eval-print-loop-repl">Read-Eval-Print Loop (REPL)</a></li> <li class="toctree-l2"><a class="reference internal" href="notebooks/Intro.html#read-eval-print-loop-repl">Read-Eval-Print Loop (REPL)</a></li>
<li class="toctree-l2"><a class="reference internal" href="notebooks/Intro.html#the-stack">The Stack</a></li> <li class="toctree-l2"><a class="reference internal" href="notebooks/Intro.html#the-stack">The Stack</a></li>
<li class="toctree-l2"><a class="reference internal" href="notebooks/Intro.html#purely-functional-datastructures">Purely Functional Datastructures.</a></li> <li class="toctree-l2"><a class="reference internal" href="notebooks/Intro.html#purely-functional-datastructures">Purely Functional Datastructures</a></li>
<li class="toctree-l2"><a class="reference internal" href="notebooks/Intro.html#the-joy-function">The <code class="docutils literal notranslate"><span class="pre">joy()</span></code> function</a></li> <li class="toctree-l2"><a class="reference internal" href="notebooks/Intro.html#the-joy-function">The <code class="docutils literal notranslate"><span class="pre">joy()</span></code> function</a></li>
<li class="toctree-l2"><a class="reference internal" href="notebooks/Intro.html#parser">Parser</a></li> <li class="toctree-l2"><a class="reference internal" href="notebooks/Intro.html#parser">Parser</a></li>
<li class="toctree-l2"><a class="reference internal" href="notebooks/Intro.html#library">Library</a></li> <li class="toctree-l2"><a class="reference internal" href="notebooks/Intro.html#library">Library</a></li>

View File

@ -17,7 +17,7 @@
<link rel="index" title="Index" href="genindex.html" /> <link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" /> <link rel="search" title="Search" href="search.html" />
<link rel="next" title="Stack or Quote or Sequence or List…" href="stack.html" /> <link rel="next" title="Stack or Quote or Sequence or List…" href="stack.html" />
<link rel="prev" title="Thun 0.1.1 Documentation" href="index.html" /> <link rel="prev" title="Thun: Joy in Python" href="notebooks/Intro.html" />
<link rel="stylesheet" href="_static/custom.css" type="text/css" /> <link rel="stylesheet" href="_static/custom.css" type="text/css" />
@ -51,14 +51,17 @@ executed.</p>
<col class="field-name" /> <col class="field-name" />
<col class="field-body" /> <col class="field-body" />
<tbody valign="top"> <tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple"> <tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple">
<li><strong>stack</strong> (<em>quote</em>) The stack.</li> <li><strong>stack</strong> (<em>stack</em>) The stack.</li>
<li><strong>expression</strong> (<em>quote</em>) The expression to evaluate.</li> <li><strong>expression</strong> (<em>stack</em>) The expression to evaluate.</li>
<li><strong>dictionary</strong> (<em>dict</em>) A <cite>dict</cite> mapping names to Joy functions.</li> <li><strong>dictionary</strong> (<em>dict</em>) A <code class="docutils literal notranslate"><span class="pre">dict</span></code> mapping names to Joy functions.</li>
<li><strong>viewer</strong> (<em>function</em>) Optional viewer function.</li> <li><strong>viewer</strong> (<em>function</em>) Optional viewer function.</li>
</ul> </ul>
</td> </td>
</tr> </tr>
<tr class="field-even field"><th class="field-name">Return type:</th><td class="field-body"><p class="first last">(stack, (), dictionary)</p>
</td>
</tr>
</tbody> </tbody>
</table> </table>
</dd></dl> </dd></dl>
@ -68,12 +71,44 @@ executed.</p>
<code class="descclassname">joy.joy.</code><code class="descname">repl</code><span class="sig-paren">(</span><em>stack=()</em>, <em>dictionary=None</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/joy/joy.html#repl"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#joy.joy.repl" title="Permalink to this definition"></a></dt> <code class="descclassname">joy.joy.</code><code class="descname">repl</code><span class="sig-paren">(</span><em>stack=()</em>, <em>dictionary=None</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/joy/joy.html#repl"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#joy.joy.repl" title="Permalink to this definition"></a></dt>
<dd><p>Read-Evaluate-Print Loop</p> <dd><p>Read-Evaluate-Print Loop</p>
<p>Accept input and run it on the stack, loop.</p> <p>Accept input and run it on the stack, loop.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple">
<li><strong>stack</strong> (<em>stack</em>) The stack.</li>
<li><strong>dictionary</strong> (<em>dict</em>) A <code class="docutils literal notranslate"><span class="pre">dict</span></code> mapping names to Joy functions.</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">Return type:</th><td class="field-body"><p class="first last">stack</p>
</td>
</tr>
</tbody>
</table>
</dd></dl> </dd></dl>
<dl class="function"> <dl class="function">
<dt id="joy.joy.run"> <dt id="joy.joy.run">
<code class="descclassname">joy.joy.</code><code class="descname">run</code><span class="sig-paren">(</span><em>text</em>, <em>stack</em>, <em>dictionary</em>, <em>viewer=None</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/joy/joy.html#run"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#joy.joy.run" title="Permalink to this definition"></a></dt> <code class="descclassname">joy.joy.</code><code class="descname">run</code><span class="sig-paren">(</span><em>text</em>, <em>stack</em>, <em>dictionary</em>, <em>viewer=None</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/joy/joy.html#run"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#joy.joy.run" title="Permalink to this definition"></a></dt>
<dd><p>Return the stack resulting from running the Joy code text on the stack.</p> <dd><p>Return the stack resulting from running the Joy code text on the stack.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple">
<li><strong>text</strong> (<em>str</em>) Joy code.</li>
<li><strong>stack</strong> (<em>stack</em>) The stack.</li>
<li><strong>dictionary</strong> (<em>dict</em>) A <code class="docutils literal notranslate"><span class="pre">dict</span></code> mapping names to Joy functions.</li>
<li><strong>viewer</strong> (<em>function</em>) Optional viewer function.</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">Return type:</th><td class="field-body"><p class="first last">(stack, (), dictionary)</p>
</td>
</tr>
</tbody>
</table>
</dd></dl> </dd></dl>
</div> </div>
@ -96,7 +131,7 @@ executed.</p>
<h3>Related Topics</h3> <h3>Related Topics</h3>
<ul> <ul>
<li><a href="index.html">Documentation overview</a><ul> <li><a href="index.html">Documentation overview</a><ul>
<li>Previous: <a href="index.html" title="previous chapter">Thun 0.1.1 Documentation</a></li> <li>Previous: <a href="notebooks/Intro.html" title="previous chapter">Thun: Joy in Python</a></li>
<li>Next: <a href="stack.html" title="next chapter">Stack or Quote or Sequence or List…</a></li> <li>Next: <a href="stack.html" title="next chapter">Stack or Quote or Sequence or List…</a></li>
</ul></li> </ul></li>
</ul> </ul>

View File

@ -1,512 +0,0 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=Edge" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Joypy &#8212; Thun 0.1.1 documentation</title>
<link rel="stylesheet" href="../_static/alabaster.css" type="text/css" />
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<script type="text/javascript" src="../_static/documentation_options.js"></script>
<script type="text/javascript" src="../_static/jquery.js"></script>
<script type="text/javascript" src="../_static/underscore.js"></script>
<script type="text/javascript" src="../_static/doctools.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
<link rel="index" title="Index" href="../genindex.html" />
<link rel="search" title="Search" href="../search.html" />
<link rel="stylesheet" href="../_static/custom.css" type="text/css" />
<meta name="viewport" content="width=device-width, initial-scale=0.9, maximum-scale=0.9" />
</head><body>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<div class="section" id="joypy">
<h1>Joypy<a class="headerlink" href="#joypy" title="Permalink to this headline"></a></h1>
<div class="section" id="joy-in-python">
<h2>Joy in Python<a class="headerlink" href="#joy-in-python" title="Permalink to this headline"></a></h2>
<p>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.</p>
<p>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.</p>
<div class="section" id="read-eval-print-loop-repl">
<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>
<p>The main way to interact with the Joy interpreter is through a simple
<a class="reference external" href="https://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop">REPL</a>
that you start by running the package:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>$ python -m joy
Joypy - Copyright © 2017 Simon Forman
This program comes with ABSOLUTELY NO WARRANTY; for details type &quot;warranty&quot;.
This is free software, and you are welcome to redistribute it
under certain conditions; type &quot;sharing&quot; for details.
Type &quot;words&quot; to see a list of all words, and &quot;[&lt;name&gt;] help&quot; to print the
docs for a word.
&lt;-top
joy? _
</pre></div>
</div>
<p>The <code class="docutils literal notranslate"><span class="pre">&lt;-top</span></code> marker points to the top of the (initially empty) stack.
You can enter Joy notation at the prompt and a <a class="reference external" href="#The-TracePrinter.">trace of
evaluation</a> will be printed followed by the stack
and prompt again:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>joy? 23 sqr 18 +
. 23 sqr 18 +
23 . sqr 18 +
23 . dup mul 18 +
23 23 . mul 18 +
529 . 18 +
529 18 . +
547 .
547 &lt;-top
joy?
</pre></div>
</div>
</div>
</div>
</div>
<div class="section" id="stacks-aka-list-quote-sequence-etc">
<h1>Stacks (aka list, quote, sequence, etc.)<a class="headerlink" href="#stacks-aka-list-quote-sequence-etc" title="Permalink to this headline"></a></h1>
<p>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 <code class="docutils literal notranslate"><span class="pre">[...]</span></code>. This sequence type is used to represent
both the stack and the expression. It is a <a class="reference external" href="https://en.wikipedia.org/wiki/Cons#Lists">cons
list</a> made from Python
tuples.</p>
<div class="code ipython2 highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">inspect</span>
<span class="kn">import</span> <span class="nn">joy.utils.stack</span>
<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>
</pre></div>
</div>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>§ Stack
When talking about Joy we use the terms &quot;stack&quot;, &quot;list&quot;, &quot;sequence&quot; and
&quot;aggregate&quot; to mean the same thing: a simple datatype that permits
certain operations such as iterating and pushing and popping values from
(at least) one end.
We use the venerable two-tuple recursive form of sequences where the
empty tuple () is the empty stack and (head, rest) gives the recursive
form of a stack with one or more items on it.
()
(1, ())
(2, (1, ()))
(3, (2, (1, ())))
...
And so on.
We have two very simple functions to build up a stack from a Python
iterable and also to iterate through a stack and yield its items
one-by-one in order, and two functions to generate string representations
of stacks:
list_to_stack()
iter_stack()
expression_to_string() (prints left-to-right)
stack_to_string() (prints right-to-left)
A word about the stack data structure.
Python has very nice &quot;tuple packing and unpacking&quot; in its syntax which
means we can directly &quot;unpack&quot; the expected arguments to a Joy function.
For example:
def dup(stack):
head, tail = stack
return head, (head, tail)
We replace the argument &quot;stack&quot; by the expected structure of the stack,
in this case &quot;(head, tail)&quot;, and Python takes care of de-structuring the
incoming argument and assigning values to the names. Note that Python
syntax doesn&#39;t require parentheses around tuples used in expressions
where they would be redundant.
</pre></div>
</div>
<p>The 0th item in the list will be on the top of the stack and <em>vise
versa</em>.</p>
<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>
</pre></div>
</div>
<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>
</pre></div>
</div>
<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>
</pre></div>
</div>
<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>
</pre></div>
</div>
<p>This requires reversing the sequence (or iterating backwards) otherwise:</p>
<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>
<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>
<span class="n">stack</span> <span class="o">=</span> <span class="n">n</span><span class="p">,</span> <span class="n">stack</span>
<span class="nb">print</span> <span class="n">stack</span>
<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>
</pre></div>
</div>
<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>
<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>
</pre></div>
</div>
<p>Because Joy lists are made out of Python tuples they are immutable, so
all Joy datastructures are <em>`purely
functional &lt;https://en.wikipedia.org/wiki/Purely_functional_data_structure&gt;`__</em>.</p>
</div>
<div class="section" id="the-joy-function">
<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>
<div class="section" id="an-interpreter">
<h2>An Interpreter<a class="headerlink" href="#an-interpreter" title="Permalink to this headline"></a></h2>
<p>The <code class="docutils literal notranslate"><span class="pre">joy()</span></code> 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.</p>
<p>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 <code class="docutils literal notranslate"><span class="pre">help</span></code>
command.)</p>
<div class="code ipython2 highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">joy.joy</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">joy</span><span class="o">.</span><span class="n">joy</span><span class="p">)</span>
</pre></div>
</div>
<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>
<span class="sd">&#39;&#39;&#39;</span>
<span class="sd"> Evaluate the Joy expression on the stack.</span>
<span class="sd"> &#39;&#39;&#39;</span>
<span class="k">while</span> <span class="n">expression</span><span class="p">:</span>
<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>
<span class="n">term</span><span class="p">,</span> <span class="n">expression</span> <span class="o">=</span> <span class="n">expression</span>
<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>
<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>
<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>
<span class="k">else</span><span class="p">:</span>
<span class="n">stack</span> <span class="o">=</span> <span class="n">term</span><span class="p">,</span> <span class="n">stack</span>
<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>
<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>
</pre></div>
</div>
<div class="section" id="view-function">
<h3>View function<a class="headerlink" href="#view-function" title="Permalink to this headline"></a></h3>
<p>The <code class="docutils literal notranslate"><span class="pre">joy()</span></code> 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.</p>
</div>
<div class="section" id="the-traceprinter">
<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>
<p>A <code class="docutils literal notranslate"><span class="pre">viewer</span></code> records each step of the evaluation of a Joy program. The
<code class="docutils literal notranslate"><span class="pre">TracePrinter</span></code> 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.</p>
</div>
<div class="section" id="continuation-passing-style">
<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>
<p>One day I thought, What happens if you rewrite Joy to use
<a class="reference external" href="https://en.wikipedia.org/wiki/Continuation-passing_style">CSP</a>? 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
<code class="docutils literal notranslate"><span class="pre">joy()</span></code> function.</p>
</div>
</div>
</div>
<div class="section" id="parser">
<h1>Parser<a class="headerlink" href="#parser" title="Permalink to this headline"></a></h1>
<div class="code ipython2 highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">joy.parser</span>
<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>
</pre></div>
</div>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>§ Converting text to a joy expression.
This module exports a single function:
text_to_expression(text)
As well as a single Symbol class and a single Exception type:
ParseError
When supplied with a string this function returns a Python datastructure
that represents the Joy datastructure described by the text expression.
Any unbalanced square brackets will raise a ParseError.
</pre></div>
</div>
<p>The parser is extremely simple, the undocumented <code class="docutils literal notranslate"><span class="pre">re.Scanner</span></code> class
does most of the tokenizing work and then you just build the tuple
structure out of the tokens. Theres no Abstract Syntax Tree or anything
like that.</p>
<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>
</pre></div>
</div>
<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>
<span class="sd">&#39;&#39;&#39;</span>
<span class="sd"> Return a stack/list expression of the tokens.</span>
<span class="sd"> &#39;&#39;&#39;</span>
<span class="n">frame</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">stack</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">tok</span> <span class="ow">in</span> <span class="n">tokens</span><span class="p">:</span>
<span class="k">if</span> <span class="n">tok</span> <span class="o">==</span> <span class="s1">&#39;[&#39;</span><span class="p">:</span>
<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>
<span class="n">frame</span> <span class="o">=</span> <span class="p">[]</span>
<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>
<span class="k">elif</span> <span class="n">tok</span> <span class="o">==</span> <span class="s1">&#39;]&#39;</span><span class="p">:</span>
<span class="k">try</span><span class="p">:</span>
<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>
<span class="k">except</span> <span class="ne">IndexError</span><span class="p">:</span>
<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>
<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>
<span class="k">else</span><span class="p">:</span>
<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>
<span class="k">if</span> <span class="n">stack</span><span class="p">:</span>
<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>
<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>
</pre></div>
</div>
<p>Thats pretty much all there is to it.</p>
<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>
</pre></div>
</div>
<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>
</pre></div>
</div>
<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>
</pre></div>
</div>
<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>
</pre></div>
</div>
<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>
<span class="c1"># a Symbol, no lookup at</span>
<span class="c1"># parse-time. Haiku docs.</span>
</pre></div>
</div>
<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>
</pre></div>
</div>
<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>
</pre></div>
</div>
<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>
</pre></div>
</div>
<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>
</pre></div>
</div>
<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>
</pre></div>
</div>
</div>
<div class="section" id="library">
<h1>Library<a class="headerlink" href="#library" title="Permalink to this headline"></a></h1>
<p>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 <code class="docutils literal notranslate"><span class="pre">add</span></code> (or
<code class="docutils literal notranslate"><span class="pre">+</span></code>, the library module supports aliases), and combinators which
provide control-flow and higher-order operations.</p>
<div class="code ipython2 highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">joy.library</span>
<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>
</pre></div>
</div>
<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 •
</pre></div>
</div>
<p>Many of the functions are defined in Python, like <code class="docutils literal notranslate"><span class="pre">dip</span></code>:</p>
<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>
</pre></div>
</div>
<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>
<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>
<span class="n">expression</span> <span class="o">=</span> <span class="n">x</span><span class="p">,</span> <span class="n">expression</span>
<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>
</pre></div>
</div>
<p>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.</p>
<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>
</pre></div>
</div>
<pre class="literal-block">
second == rest first
third == rest rest first
product == 1 swap [*] step
swons == swap cons
swoncat == swap concat
flatten == [] swap [concat] step
unit == [] cons
quoted == [unit] dip
unquoted == [i] dip
enstacken == stack [clear] dip
disenstacken == ? [uncons ?] loop pop
? == dup truthy
dinfrirst == dip infra first
nullary == [stack] dinfrirst
unary == [stack [pop] dip] dinfrirst
binary == [stack [popop] dip] dinfrirst
ternary == [stack [popop pop] dip] dinfrirst
pam == [i] map
run == [] swap infra
sqr == dup mul
size == 0 swap [pop ++] step
cleave == [i] app2 [popd] dip
average == [sum 1.0 <em>] [size] cleave /
gcd == 1 [tuck modulus dup 0 &gt;] loop pop
least_fraction == dup [gcd] infra [div] concat map
*fraction == [uncons] dip uncons [swap] dip concat [</em>] infra [*] dip cons
<em>fraction0 == concat [[swap] dip * [</em>] dip] infra
down_to_zero == [0 &gt;] [dup --] while
range_to_zero == unit [down_to_zero] infra
anamorphism == [pop []] swap [dip swons] genrec
range == [0 &lt;=] [1 - dup] anamorphism
while == swap [nullary] cons dup dipd concat loop
dudipd == dup dipd
primrec == [i] genrec
</pre>
<p>Currently, theres 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.</p>
<p>It would be simple enough to define one, but it would open the door to
<em>name binding</em> and break the idea that all state is captured in the
stack and expression. Theres an implicit <em>standard dictionary</em> 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.)</p>
<p>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 <em>Grand Refactoring</em> biannually (two years, not six
months, thats 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.</p>
<p>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 shouldnt be dozens of chat apps, JS
frameworks, programming languages. Its a waste of time, a <a class="reference external" href="https://en.wikipedia.org/wiki/Thundering_herd_problem">fractal
“thundering herd”
attack</a> on
human mentality.</p>
<p>If you read over the other notebooks youll 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.</p>
<p>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.</p>
<p>There seems to be an elegance and power to the notation.</p>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<h3><a href="../index.html">Table Of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#">Joypy</a><ul>
<li><a class="reference internal" href="#joy-in-python">Joy in Python</a><ul>
<li><a class="reference internal" href="#read-eval-print-loop-repl">Read-Eval-Print Loop (REPL)</a></li>
</ul>
</li>
</ul>
</li>
<li><a class="reference internal" href="#stacks-aka-list-quote-sequence-etc">Stacks (aka list, quote, sequence, etc.)</a></li>
<li><a class="reference internal" href="#the-joy-function">The <code class="docutils literal notranslate"><span class="pre">joy()</span></code> function.</a><ul>
<li><a class="reference internal" href="#an-interpreter">An Interpreter</a><ul>
<li><a class="reference internal" href="#view-function">View function</a></li>
<li><a class="reference internal" href="#the-traceprinter">The <code class="docutils literal notranslate"><span class="pre">TracePrinter</span></code>.</a></li>
<li><a class="reference internal" href="#continuation-passing-style">Continuation-Passing Style</a></li>
</ul>
</li>
</ul>
</li>
<li><a class="reference internal" href="#parser">Parser</a></li>
<li><a class="reference internal" href="#library">Library</a></li>
</ul>
<div class="relations">
<h3>Related Topics</h3>
<ul>
<li><a href="../index.html">Documentation overview</a><ul>
</ul></li>
</ul>
</div>
<div role="note" aria-label="source link">
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="../_sources/notebooks/0. This Implementation of Joy in Python.rst.txt"
rel="nofollow">Show Source</a></li>
</ul>
</div>
<div id="searchbox" style="display: none" role="search">
<h3>Quick search</h3>
<div class="searchformwrapper">
<form class="search" action="../search.html" method="get">
<input type="text" name="q" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="footer" role="contentinfo">
<a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/4.0/">
<img alt="Creative Commons License" style="border-width:0" src="https://i.creativecommons.org/l/by-nc-sa/4.0/88x31.png" />
</a>
<br />
<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>.
Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.7.3.
</div>
</body>
</html>

View File

@ -6,7 +6,7 @@
<head> <head>
<meta http-equiv="X-UA-Compatible" content="IE=Edge" /> <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>within &#8212; Thun 0.1.1 documentation</title> <title>A Generator for Approximations &#8212; Thun 0.1.1 documentation</title>
<link rel="stylesheet" href="../_static/alabaster.css" type="text/css" /> <link rel="stylesheet" href="../_static/alabaster.css" type="text/css" />
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" /> <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<script type="text/javascript" src="../_static/documentation_options.js"></script> <script type="text/javascript" src="../_static/documentation_options.js"></script>
@ -30,25 +30,88 @@
<div class="bodywrapper"> <div class="bodywrapper">
<div class="body" role="main"> <div class="body" role="main">
<div class="section" id="within"> <div class="section" id="a-generator-for-approximations">
<h1><code class="docutils literal notranslate"><span class="pre">within</span></code><a class="headerlink" href="#within" title="Permalink to this headline"></a></h1> <h1>A Generator for Approximations<a class="headerlink" href="#a-generator-for-approximations" title="Permalink to this headline"></a></h1>
<p>In <a class="reference internal" href="Generator Programs.html"><span class="doc">Using x to Generate Values</span></a> we derive a function <code class="docutils literal notranslate"><span class="pre">G</span></code> (called <code class="docutils literal notranslate"><span class="pre">make_generator</span></code> in the dictionary) that accepts an initial value and a quoted program and returns a new quoted program that, when driven by the <code class="docutils literal notranslate"><span class="pre">x</span></code> combinator (<a class="reference internal" href="../library.html#joy.library.x" title="joy.library.x"><code class="xref py py-func docutils literal notranslate"><span class="pre">joy.library.x()</span></code></a>), acts like a lazy stream.</p>
<p>To make a generator that generates successive approximations lets start by assuming an initial approximation and then derive the function that computes the next approximation:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span> <span class="n">a</span> <span class="n">F</span>
<span class="o">---------</span>
<span class="n">a</span><span class="s1">&#39;</span>
</pre></div>
</div>
<div class="section" id="a-function-to-compute-the-next-approximation">
<h2>A Function to Compute the Next Approximation<a class="headerlink" href="#a-function-to-compute-the-next-approximation" title="Permalink to this headline"></a></h2>
<p>Looking at the equation again:</p>
<p><span class="math notranslate nohighlight">\(a_{i+1} = \frac{(a_i+\frac{n}{a_i})}{2}\)</span></p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">a</span> <span class="n">n</span> <span class="n">over</span> <span class="o">/</span> <span class="o">+</span> <span class="mi">2</span> <span class="o">/</span>
<span class="n">a</span> <span class="n">n</span> <span class="n">a</span> <span class="o">/</span> <span class="o">+</span> <span class="mi">2</span> <span class="o">/</span>
<span class="n">a</span> <span class="n">n</span><span class="o">/</span><span class="n">a</span> <span class="o">+</span> <span class="mi">2</span> <span class="o">/</span>
<span class="n">a</span><span class="o">+</span><span class="n">n</span><span class="o">/</span><span class="n">a</span> <span class="mi">2</span> <span class="o">/</span>
<span class="p">(</span><span class="n">a</span><span class="o">+</span><span class="n">n</span><span class="o">/</span><span class="n">a</span><span class="p">)</span><span class="o">/</span><span class="mi">2</span>
</pre></div>
</div>
<p>The function we want has the argument <code class="docutils literal notranslate"><span class="pre">n</span></code> in it:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">F</span> <span class="o">==</span> <span class="n">n</span> <span class="n">over</span> <span class="o">/</span> <span class="o">+</span> <span class="mi">2</span> <span class="o">/</span>
</pre></div>
</div>
</div>
<div class="section" id="make-it-into-a-generator">
<h2>Make it into a Generator<a class="headerlink" href="#make-it-into-a-generator" title="Permalink to this headline"></a></h2>
<p>Our generator would be created by:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">a</span> <span class="p">[</span><span class="n">dup</span> <span class="n">F</span><span class="p">]</span> <span class="n">make_generator</span>
</pre></div>
</div>
<p>With <code class="docutils literal notranslate"><span class="pre">n</span></code> as part of the function <code class="docutils literal notranslate"><span class="pre">F</span></code>, but <code class="docutils literal notranslate"><span class="pre">n</span></code> is the input to the <code class="docutils literal notranslate"><span class="pre">sqrt</span></code> function were writing. If we let 1 be the initial approximation:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="mi">1</span> <span class="n">n</span> <span class="mi">1</span> <span class="o">/</span> <span class="o">+</span> <span class="mi">2</span> <span class="o">/</span>
<span class="mi">1</span> <span class="n">n</span><span class="o">/</span><span class="mi">1</span> <span class="o">+</span> <span class="mi">2</span> <span class="o">/</span>
<span class="mi">1</span> <span class="n">n</span> <span class="o">+</span> <span class="mi">2</span> <span class="o">/</span>
<span class="n">n</span><span class="o">+</span><span class="mi">1</span> <span class="mi">2</span> <span class="o">/</span>
<span class="p">(</span><span class="n">n</span><span class="o">+</span><span class="mi">1</span><span class="p">)</span><span class="o">/</span><span class="mi">2</span>
</pre></div>
</div>
<p>The generator can be written as:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="mi">1</span> <span class="n">swap</span> <span class="p">[</span><span class="n">over</span> <span class="o">/</span> <span class="o">+</span> <span class="mi">2</span> <span class="o">/</span><span class="p">]</span> <span class="n">cons</span> <span class="p">[</span><span class="n">dup</span><span class="p">]</span> <span class="n">swoncat</span> <span class="n">make_generator</span>
</pre></div>
</div>
<p>Example:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="mi">23</span> <span class="mi">1</span> <span class="n">swap</span> <span class="p">[</span><span class="n">over</span> <span class="o">/</span> <span class="o">+</span> <span class="mi">2</span> <span class="o">/</span><span class="p">]</span> <span class="n">cons</span> <span class="p">[</span><span class="n">dup</span><span class="p">]</span> <span class="n">swoncat</span> <span class="n">make_generator</span>
<span class="mi">1</span> <span class="mi">23</span> <span class="p">[</span><span class="n">over</span> <span class="o">/</span> <span class="o">+</span> <span class="mi">2</span> <span class="o">/</span><span class="p">]</span> <span class="n">cons</span> <span class="p">[</span><span class="n">dup</span><span class="p">]</span> <span class="n">swoncat</span> <span class="n">make_generator</span>
<span class="mi">1</span> <span class="p">[</span><span class="mi">23</span> <span class="n">over</span> <span class="o">/</span> <span class="o">+</span> <span class="mi">2</span> <span class="o">/</span><span class="p">]</span> <span class="p">[</span><span class="n">dup</span><span class="p">]</span> <span class="n">swoncat</span> <span class="n">make_generator</span>
<span class="mi">1</span> <span class="p">[</span><span class="n">dup</span> <span class="mi">23</span> <span class="n">over</span> <span class="o">/</span> <span class="o">+</span> <span class="mi">2</span> <span class="o">/</span><span class="p">]</span> <span class="n">make_generator</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="o">.</span>
<span class="p">[</span><span class="mi">1</span> <span class="n">swap</span> <span class="p">[</span><span class="n">dup</span> <span class="mi">23</span> <span class="n">over</span> <span class="o">/</span> <span class="o">+</span> <span class="mi">2</span> <span class="o">/</span><span class="p">]</span> <span class="n">direco</span><span class="p">]</span>
</pre></div>
</div>
</div>
<div class="section" id="a-generator-of-square-root-approximations">
<h2>A Generator of Square Root Approximations<a class="headerlink" href="#a-generator-of-square-root-approximations" title="Permalink to this headline"></a></h2>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">gsra</span> <span class="o">==</span> <span class="mi">1</span> <span class="n">swap</span> <span class="p">[</span><span class="n">over</span> <span class="o">/</span> <span class="o">+</span> <span class="mi">2</span> <span class="o">/</span><span class="p">]</span> <span class="n">cons</span> <span class="p">[</span><span class="n">dup</span><span class="p">]</span> <span class="n">swoncat</span> <span class="n">make_generator</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="finding-consecutive-approximations-within-a-tolerance">
<h1>Finding Consecutive Approximations <code class="docutils literal notranslate"><span class="pre">within</span></code> a Tolerance<a class="headerlink" href="#finding-consecutive-approximations-within-a-tolerance" title="Permalink to this headline"></a></h1>
<blockquote> <blockquote>
<div>The remainder of a square root finder is a function <em>within</em>, which takes a tolerance and a list of approximations and looks down the list for two successive approximations that differ by no more than the given tolerance.</div></blockquote> <div>The remainder of a square root finder is a function <em>within</em>, which takes a tolerance and a list of approximations and looks down the list for two successive approximations that differ by no more than the given tolerance.</div></blockquote>
<p>From <a class="reference external" href="https://www.cs.kent.ac.uk/people/staff/dat/miranda/whyfp90.pdf">“Why Functional Programming Matters” by John <p>From <a class="reference external" href="https://www.cs.kent.ac.uk/people/staff/dat/miranda/whyfp90.pdf">“Why Functional Programming Matters” by John
Hughes</a></p> Hughes</a></p>
<p>(And note that by “list” he means a lazily-evaluated list.)</p> <p>(And note that by “list” he means a lazily-evaluated list.)</p>
<p>Using a <a class="reference internal" href="Generator Programs.html"><span class="doc">generator</span></a> driven by <code class="docutils literal notranslate"><span class="pre">x</span></code> and assuming such for square root approximations (or whatever) <code class="docutils literal notranslate"><span class="pre">G</span></code>, and further assuming that the first term <code class="docutils literal notranslate"><span class="pre">a</span></code> has been generated already and epsilon <code class="docutils literal notranslate"><span class="pre">ε</span></code> is handy on the stack…</p> <p>Using the <em>output</em> <code class="docutils literal notranslate"><span class="pre">[a</span> <span class="pre">G]</span></code> of the above <a class="reference internal" href="Generator Programs.html"><span class="doc">generator</span></a> for square root approximations, and further assuming that the first term <code class="docutils literal notranslate"><span class="pre">a</span></code> has been generated already and epsilon <code class="docutils literal notranslate"><span class="pre">ε</span></code> is handy on the stack…</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span> <span class="n">a</span> <span class="p">[</span><span class="n">b</span> <span class="n">G</span><span class="p">]</span> <span class="n">ε</span> <span class="n">within</span> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span> <span class="n">a</span> <span class="p">[</span><span class="n">b</span> <span class="n">G</span><span class="p">]</span> <span class="n">ε</span> <span class="n">within</span>
<span class="o">--------------------</span> <span class="n">a</span> <span class="n">b</span> <span class="o">-</span> <span class="nb">abs</span> <span class="n">ε</span> <span class="o">&lt;=</span> <span class="o">----------------------</span> <span class="n">a</span> <span class="n">b</span> <span class="o">-</span> <span class="nb">abs</span> <span class="n">ε</span> <span class="o">&lt;=</span>
<span class="n">b</span> <span class="n">b</span>
</pre></div>
<span class="n">a</span> <span class="p">[</span><span class="n">b</span> <span class="n">G</span><span class="p">]</span> <span class="n">ε</span> <span class="n">within</span> </div>
<span class="o">--------------------</span> <span class="n">a</span> <span class="n">b</span> <span class="o">-</span> <span class="nb">abs</span> <span class="n">ε</span> <span class="o">&gt;</span> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span> <span class="n">a</span> <span class="p">[</span><span class="n">b</span> <span class="n">G</span><span class="p">]</span> <span class="n">ε</span> <span class="n">within</span>
<span class="o">----------------------</span> <span class="n">a</span> <span class="n">b</span> <span class="o">-</span> <span class="nb">abs</span> <span class="n">ε</span> <span class="o">&gt;</span>
<span class="o">.</span> <span class="o">.</span>
<span class="p">[</span><span class="n">b</span> <span class="n">G</span><span class="p">]</span> <span class="n">x</span> <span class="n">ε</span> <span class="o">...</span> <span class="p">[</span><span class="n">b</span> <span class="n">G</span><span class="p">]</span> <span class="n">x</span> <span class="n">ε</span> <span class="o">...</span>
<span class="n">b</span> <span class="p">[</span><span class="n">c</span> <span class="n">G</span><span class="p">]</span> <span class="n">ε</span> <span class="o">...</span> <span class="n">b</span> <span class="p">[</span><span class="n">c</span> <span class="n">G</span><span class="p">]</span> <span class="n">ε</span> <span class="o">...</span>
<span class="o">.</span> <span class="o">.</span>
<span class="o">--------------------</span> <span class="o">----------------------</span>
<span class="n">b</span> <span class="p">[</span><span class="n">c</span> <span class="n">G</span><span class="p">]</span> <span class="n">ε</span> <span class="n">within</span> <span class="n">b</span> <span class="p">[</span><span class="n">c</span> <span class="n">G</span><span class="p">]</span> <span class="n">ε</span> <span class="n">within</span>
</pre></div> </pre></div>
</div> </div>
@ -85,7 +148,7 @@ Hughes</a></p>
</div> </div>
<ol class="arabic simple"> <ol class="arabic simple">
<li>Discard <code class="docutils literal notranslate"><span class="pre">a</span></code>.</li> <li>Discard <code class="docutils literal notranslate"><span class="pre">a</span></code>.</li>
<li>Use <code class="docutils literal notranslate"><span class="pre">x</span></code> combinator to generate next term from G.</li> <li>Use <code class="docutils literal notranslate"><span class="pre">x</span></code> combinator to generate next term from <code class="docutils literal notranslate"><span class="pre">G</span></code>.</li>
<li>Run <code class="docutils literal notranslate"><span class="pre">within</span></code> with <code class="docutils literal notranslate"><span class="pre">i</span></code> (it is a <code class="docutils literal notranslate"><span class="pre">primrec</span></code> function.)</li> <li>Run <code class="docutils literal notranslate"><span class="pre">within</span></code> with <code class="docutils literal notranslate"><span class="pre">i</span></code> (it is a <code class="docutils literal notranslate"><span class="pre">primrec</span></code> function.)</li>
</ol> </ol>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">a</span> <span class="p">[</span><span class="n">b</span> <span class="n">G</span><span class="p">]</span> <span class="n">ε</span> <span class="n">R0</span> <span class="p">[</span><span class="n">within</span><span class="p">]</span> <span class="n">R1</span> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">a</span> <span class="p">[</span><span class="n">b</span> <span class="n">G</span><span class="p">]</span> <span class="n">ε</span> <span class="n">R0</span> <span class="p">[</span><span class="n">within</span><span class="p">]</span> <span class="n">R1</span>
@ -104,14 +167,28 @@ Hughes</a></p>
</div> </div>
<div class="section" id="setting-up"> <div class="section" id="setting-up">
<h2>Setting up<a class="headerlink" href="#setting-up" title="Permalink to this headline"></a></h2> <h2>Setting up<a class="headerlink" href="#setting-up" title="Permalink to this headline"></a></h2>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="p">[</span><span class="n">a</span> <span class="n">G</span><span class="p">]</span> <span class="n">x</span> <span class="n">ε</span> <p>The recursive function we have defined so far needs a slight preamble: <code class="docutils literal notranslate"><span class="pre">x</span></code> to prime the generator and the epsilon value to use:</p>
<span class="n">a</span> <span class="p">[</span><span class="n">b</span> <span class="n">G</span><span class="p">]</span> <span class="n">ε</span> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="p">[</span><span class="n">a</span> <span class="n">G</span><span class="p">]</span> <span class="n">x</span> <span class="n">ε</span> <span class="o">...</span>
<span class="n">a</span> <span class="p">[</span><span class="n">b</span> <span class="n">G</span><span class="p">]</span> <span class="n">ε</span> <span class="o">...</span>
</pre></div> </pre></div>
</div> </div>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">within</span> <span class="o">==</span> <span class="n">x</span> <span class="n">ε</span> <span class="p">[[</span><span class="n">first</span> <span class="o">-</span> <span class="nb">abs</span><span class="p">]</span> <span class="n">dip</span> <span class="o">&lt;=</span><span class="p">]</span> <span class="p">[</span><span class="n">roll</span><span class="o">&lt;</span> <span class="n">popop</span> <span class="n">first</span><span class="p">]</span> <span class="p">[[</span><span class="n">popd</span> <span class="n">x</span><span class="p">]</span> <span class="n">dip</span><span class="p">]</span> <span class="n">primrec</span> </div>
<div class="section" id="within">
<h2><code class="docutils literal notranslate"><span class="pre">within</span></code><a class="headerlink" href="#within" title="Permalink to this headline"></a></h2>
<p>Giving us the following definitions:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">_within_P</span> <span class="o">==</span> <span class="p">[</span><span class="n">first</span> <span class="o">-</span> <span class="nb">abs</span><span class="p">]</span> <span class="n">dip</span> <span class="o">&lt;=</span>
<span class="n">_within_B</span> <span class="o">==</span> <span class="n">roll</span><span class="o">&lt;</span> <span class="n">popop</span> <span class="n">first</span>
<span class="n">_within_R</span> <span class="o">==</span> <span class="p">[</span><span class="n">popd</span> <span class="n">x</span><span class="p">]</span> <span class="n">dip</span>
<span class="n">within</span> <span class="o">==</span> <span class="n">x</span> <span class="n">ε</span> <span class="p">[</span><span class="n">_within_P</span><span class="p">]</span> <span class="p">[</span><span class="n">_within_B</span><span class="p">]</span> <span class="p">[</span><span class="n">_within_R</span><span class="p">]</span> <span class="n">primrec</span>
</pre></div> </pre></div>
</div> </div>
</div> </div>
</div>
<div class="section" id="finding-square-roots">
<h1>Finding Square Roots<a class="headerlink" href="#finding-square-roots" title="Permalink to this headline"></a></h1>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">sqrt</span> <span class="o">==</span> <span class="n">gsra</span> <span class="n">within</span>
</pre></div>
</div>
</div> </div>
@ -122,13 +199,21 @@ Hughes</a></p>
<div class="sphinxsidebarwrapper"> <div class="sphinxsidebarwrapper">
<h3><a href="../index.html">Table Of Contents</a></h3> <h3><a href="../index.html">Table Of Contents</a></h3>
<ul> <ul>
<li><a class="reference internal" href="#"><code class="docutils literal notranslate"><span class="pre">within</span></code></a><ul> <li><a class="reference internal" href="#">A Generator for Approximations</a><ul>
<li><a class="reference internal" href="#a-function-to-compute-the-next-approximation">A Function to Compute the Next Approximation</a></li>
<li><a class="reference internal" href="#make-it-into-a-generator">Make it into a Generator</a></li>
<li><a class="reference internal" href="#a-generator-of-square-root-approximations">A Generator of Square Root Approximations</a></li>
</ul>
</li>
<li><a class="reference internal" href="#finding-consecutive-approximations-within-a-tolerance">Finding Consecutive Approximations <code class="docutils literal notranslate"><span class="pre">within</span></code> a Tolerance</a><ul>
<li><a class="reference internal" href="#predicate">Predicate</a></li> <li><a class="reference internal" href="#predicate">Predicate</a></li>
<li><a class="reference internal" href="#base-case">Base-Case</a></li> <li><a class="reference internal" href="#base-case">Base-Case</a></li>
<li><a class="reference internal" href="#recur">Recur</a></li> <li><a class="reference internal" href="#recur">Recur</a></li>
<li><a class="reference internal" href="#setting-up">Setting up</a></li> <li><a class="reference internal" href="#setting-up">Setting up</a></li>
<li><a class="reference internal" href="#within"><code class="docutils literal notranslate"><span class="pre">within</span></code></a></li>
</ul> </ul>
</li> </li>
<li><a class="reference internal" href="#finding-square-roots">Finding Square Roots</a></li>
</ul> </ul>
<div class="relations"> <div class="relations">
<h3>Related Topics</h3> <h3>Related Topics</h3>

View File

@ -90,7 +90,7 @@ list</a> made from Python
tuples.</p> tuples.</p>
</div> </div>
<div class="section" id="purely-functional-datastructures"> <div class="section" id="purely-functional-datastructures">
<h2>Purely Functional Datastructures.<a class="headerlink" href="#purely-functional-datastructures" title="Permalink to this headline"></a></h2> <h2>Purely Functional Datastructures<a class="headerlink" href="#purely-functional-datastructures" title="Permalink to this headline"></a></h2>
<p>Because Joy stacks are made out of Python tuples they are immutable, as are the other Python types we “borrow” for Joy, so all Joy datastructures are <a class="reference external" href="https://en.wikipedia.org/wiki/Purely_functional_data_structure">purely functional</a>.</p> <p>Because Joy stacks are made out of Python tuples they are immutable, as are the other Python types we “borrow” for Joy, so all Joy datastructures are <a class="reference external" href="https://en.wikipedia.org/wiki/Purely_functional_data_structure">purely functional</a>.</p>
</div> </div>
<div class="section" id="the-joy-function"> <div class="section" id="the-joy-function">
@ -138,7 +138,7 @@ structure out of the tokens. Theres no Abstract Syntax Tree or anything
like that.</p> like that.</p>
<div class="section" id="symbols"> <div class="section" id="symbols">
<h3>Symbols<a class="headerlink" href="#symbols" title="Permalink to this headline"></a></h3> <h3>Symbols<a class="headerlink" href="#symbols" title="Permalink to this headline"></a></h3>
<p>TODO: Symbols are just a string subclass; used by the parser to represent function names and by the interpreter to look up functions in the dictionary. N.B.: Symbols are not looked up at parse-time. You <em>could</em> define recursive functions, er, recusively, without <code class="docutils literal notranslate"><span class="pre">genrec</span></code> or other recursion combinators <code class="docutils literal notranslate"><span class="pre">foo</span> <span class="pre">==</span> <span class="pre">...</span> <span class="pre">fooo</span> <span class="pre">...</span></code> but dont do that.</p> <p>TODO: Symbols are just a string subclass; used by the parser to represent function names and by the interpreter to look up functions in the dictionary. N.B.: Symbols are not looked up at parse-time. You <em>could</em> define recursive functions, er, recusively, without <code class="docutils literal notranslate"><span class="pre">genrec</span></code> or other recursion combinators <code class="docutils literal notranslate"><span class="pre">foo</span> <span class="pre">==</span> <span class="pre">...</span> <span class="pre">foo</span> <span class="pre">...</span></code> but dont do that.</p>
</div> </div>
<div class="section" id="token-regular-expressions"> <div class="section" id="token-regular-expressions">
<h3>Token Regular Expressions<a class="headerlink" href="#token-regular-expressions" title="Permalink to this headline"></a></h3> <h3>Token Regular Expressions<a class="headerlink" href="#token-regular-expressions" title="Permalink to this headline"></a></h3>
@ -300,7 +300,7 @@ developing structured processes.</p>
<li><a class="reference internal" href="#">Thun: Joy in Python</a><ul> <li><a class="reference internal" href="#">Thun: Joy in Python</a><ul>
<li><a class="reference internal" href="#read-eval-print-loop-repl">Read-Eval-Print Loop (REPL)</a></li> <li><a class="reference internal" href="#read-eval-print-loop-repl">Read-Eval-Print Loop (REPL)</a></li>
<li><a class="reference internal" href="#the-stack">The Stack</a></li> <li><a class="reference internal" href="#the-stack">The Stack</a></li>
<li><a class="reference internal" href="#purely-functional-datastructures">Purely Functional Datastructures.</a></li> <li><a class="reference internal" href="#purely-functional-datastructures">Purely Functional Datastructures</a></li>
<li><a class="reference internal" href="#the-joy-function">The <code class="docutils literal notranslate"><span class="pre">joy()</span></code> function</a><ul> <li><a class="reference internal" href="#the-joy-function">The <code class="docutils literal notranslate"><span class="pre">joy()</span></code> function</a><ul>
<li><a class="reference internal" href="#an-interpreter">An Interpreter</a></li> <li><a class="reference internal" href="#an-interpreter">An Interpreter</a></li>
<li><a class="reference internal" href="#continuation-passing-style">Continuation-Passing Style</a></li> <li><a class="reference internal" href="#continuation-passing-style">Continuation-Passing Style</a></li>

View File

@ -72,7 +72,7 @@ Any unbalanced square brackets will raise a ParseError.</p>
<tbody valign="top"> <tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><strong>text</strong> (<em>str</em>) Text to convert.</td> <tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><strong>text</strong> (<em>str</em>) Text to convert.</td>
</tr> </tr>
<tr class="field-even field"><th class="field-name">Return type:</th><td class="field-body">quote</td> <tr class="field-even field"><th class="field-name">Return type:</th><td class="field-body">stack</td>
</tr> </tr>
<tr class="field-odd field"><th class="field-name">Raises:</th><td class="field-body"><a class="reference internal" href="#joy.parser.ParseError" title="joy.parser.ParseError"><strong>ParseError</strong></a> if the parse fails.</td> <tr class="field-odd field"><th class="field-name">Raises:</th><td class="field-body"><a class="reference internal" href="#joy.parser.ParseError" title="joy.parser.ParseError"><strong>ParseError</strong></a> if the parse fails.</td>
</tr> </tr>

View File

@ -36,11 +36,7 @@
<h1>Tracing Joy Execution<a class="headerlink" href="#tracing-joy-execution" title="Permalink to this headline"></a></h1> <h1>Tracing Joy Execution<a class="headerlink" href="#tracing-joy-execution" title="Permalink to this headline"></a></h1>
<div class="section" id="module-joy.utils.pretty_print"> <div class="section" id="module-joy.utils.pretty_print">
<span id="joy-utils-pretty-print"></span><h2><code class="docutils literal notranslate"><span class="pre">joy.utils.pretty_print</span></code><a class="headerlink" href="#module-joy.utils.pretty_print" title="Permalink to this headline"></a></h2> <span id="joy-utils-pretty-print"></span><h2><code class="docutils literal notranslate"><span class="pre">joy.utils.pretty_print</span></code><a class="headerlink" href="#module-joy.utils.pretty_print" title="Permalink to this headline"></a></h2>
<p>Pretty printing support.</p> <p>Pretty printing support, e.g.:</p>
<dl class="class">
<dt id="joy.utils.pretty_print.TracePrinter">
<em class="property">class </em><code class="descclassname">joy.utils.pretty_print.</code><code class="descname">TracePrinter</code><a class="reference internal" href="_modules/joy/utils/pretty_print.html#TracePrinter"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#joy.utils.pretty_print.TracePrinter" title="Permalink to this definition"></a></dt>
<dd><p>This is what does the formatting, e.g.:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>Joy? 23 18 * 99 + <div class="highlight-default notranslate"><div class="highlight"><pre><span></span>Joy? 23 18 * 99 +
. 23 18 mul 99 add . 23 18 mul 99 add
23 . 18 mul 99 add 23 . 18 mul 99 add
@ -48,12 +44,54 @@
414 . 99 add 414 . 99 add
414 99 . add 414 99 . add
513 . 513 .
513 &lt;-top
joy?
</pre></div> </pre></div>
</div> </div>
<p>On each line the stack is printed with the top to the right, then a <code class="docutils literal notranslate"><span class="pre">.</span></code> to
represent the current locus of processing, then the pending expression to the
left.</p>
<dl class="class">
<dt id="joy.utils.pretty_print.TracePrinter">
<em class="property">class </em><code class="descclassname">joy.utils.pretty_print.</code><code class="descname">TracePrinter</code><a class="reference internal" href="_modules/joy/utils/pretty_print.html#TracePrinter"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#joy.utils.pretty_print.TracePrinter" title="Permalink to this definition"></a></dt>
<dd><p>This is what does the formatting. You instantiate it and pass the <code class="docutils literal notranslate"><span class="pre">viewer()</span></code>
method to the <a class="reference internal" href="joy.html#joy.joy.joy" title="joy.joy.joy"><code class="xref py py-func docutils literal notranslate"><span class="pre">joy.joy.joy()</span></code></a> function, then print it to see the
trace.</p>
<dl class="method">
<dt id="joy.utils.pretty_print.TracePrinter.go">
<code class="descname">go</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="reference internal" href="_modules/joy/utils/pretty_print.html#TracePrinter.go"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#joy.utils.pretty_print.TracePrinter.go" title="Permalink to this definition"></a></dt>
<dd><p>Return a list of strings, one for each entry in the history, prefixed
with enough spaces to align all the interpreter dots.</p>
<p>This method is called internally by the <code class="docutils literal notranslate"><span class="pre">__str__()</span></code> method.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Return type:</th><td class="field-body">list(str)</td>
</tr>
</tbody>
</table>
</dd></dl>
<dl class="method"> <dl class="method">
<dt id="joy.utils.pretty_print.TracePrinter.viewer"> <dt id="joy.utils.pretty_print.TracePrinter.viewer">
<code class="descname">viewer</code><span class="sig-paren">(</span><em>stack</em>, <em>expression</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/joy/utils/pretty_print.html#TracePrinter.viewer"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#joy.utils.pretty_print.TracePrinter.viewer" title="Permalink to this definition"></a></dt> <code class="descname">viewer</code><span class="sig-paren">(</span><em>stack</em>, <em>expression</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/joy/utils/pretty_print.html#TracePrinter.viewer"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#joy.utils.pretty_print.TracePrinter.viewer" title="Permalink to this definition"></a></dt>
<dd><p>Pass this method as the viewer to joy() function.</p> <dd><p>Record the current stack and expression in the TracePrinters history.
Pass this method as the <code class="docutils literal notranslate"><span class="pre">viewer</span></code> argument to the <a class="reference internal" href="joy.html#joy.joy.joy" title="joy.joy.joy"><code class="xref py py-func docutils literal notranslate"><span class="pre">joy.joy.joy()</span></code></a> function.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>quote</strong> (<em>stack</em>) A stack.</li>
<li><strong>expression</strong> (<em>stack</em>) A stack.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl> </dd></dl>
</dd></dl> </dd></dl>

File diff suppressed because one or more lines are too long

View File

@ -36,11 +36,12 @@
<h1>Stack or Quote or Sequence or List…<a class="headerlink" href="#stack-or-quote-or-sequence-or-list" title="Permalink to this headline"></a></h1> <h1>Stack or Quote or Sequence or List…<a class="headerlink" href="#stack-or-quote-or-sequence-or-list" title="Permalink to this headline"></a></h1>
<div class="section" id="module-joy.utils.stack"> <div class="section" id="module-joy.utils.stack">
<span id="joy-utils-stack"></span><h2><code class="docutils literal notranslate"><span class="pre">joy.utils.stack</span></code><a class="headerlink" href="#module-joy.utils.stack" title="Permalink to this headline"></a></h2> <span id="joy-utils-stack"></span><h2><code class="docutils literal notranslate"><span class="pre">joy.utils.stack</span></code><a class="headerlink" href="#module-joy.utils.stack" title="Permalink to this headline"></a></h2>
<p>When talking about Joy we use the terms “stack”, “list”, “sequence”, <p>When talking about Joy we use the terms “stack”, “quote”, “sequence”,
quote” and others to mean the same thing: a simple linear datatype that list”, and others to mean the same thing: a simple linear datatype that
permits certain operations such as iterating and pushing and popping permits certain operations such as iterating and pushing and popping
values from (at least) one end.</p> values from (at least) one end.</p>
<p>We use the <a class="reference external" href="https://en.wikipedia.org/wiki/Cons#Lists">cons list</a>, a venerable two-tuple recursive sequence datastructure, where the <p>There is no “Stack” Python class, instead we use the <a class="reference external" href="https://en.wikipedia.org/wiki/Cons#Lists">cons list</a>, a
venerable two-tuple recursive sequence datastructure, where the
empty tuple <code class="docutils literal notranslate"><span class="pre">()</span></code> is the empty stack and <code class="docutils literal notranslate"><span class="pre">(head,</span> <span class="pre">rest)</span></code> gives the recursive empty tuple <code class="docutils literal notranslate"><span class="pre">()</span></code> is the empty stack and <code class="docutils literal notranslate"><span class="pre">(head,</span> <span class="pre">rest)</span></code> gives the recursive
form of a stack with one or more items on it:</p> form of a stack with one or more items on it:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">stack</span> <span class="p">:</span><span class="o">=</span> <span class="p">()</span> <span class="o">|</span> <span class="p">(</span><span class="n">item</span><span class="p">,</span> <span class="n">stack</span><span class="p">)</span> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">stack</span> <span class="p">:</span><span class="o">=</span> <span class="p">()</span> <span class="o">|</span> <span class="p">(</span><span class="n">item</span><span class="p">,</span> <span class="n">stack</span><span class="p">)</span>
@ -66,6 +67,19 @@ in this case “(head, tail)”, and Python takes care of unpacking the
incoming tuple and assigning values to the names. (Note that Python incoming tuple and assigning values to the names. (Note that Python
syntax doesnt require parentheses around tuples used in expressions syntax doesnt require parentheses around tuples used in expressions
where they would be redundant.)</p> where they would be redundant.)</p>
<p>Unfortunately, the Sphinx documentation generator, which is used to generate this
web page, doesnt handle tuples in the function parameters. And in Python 3, this
syntax was removed entirely. Instead you would have to write:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">dup</span><span class="p">(</span><span class="n">stack</span><span class="p">):</span>
<span class="n">head</span><span class="p">,</span> <span class="n">tail</span> <span class="o">=</span> <span class="n">stack</span>
<span class="k">return</span> <span class="n">head</span><span class="p">,</span> <span class="p">(</span><span class="n">head</span><span class="p">,</span> <span class="n">tail</span><span class="p">)</span>
</pre></div>
</div>
<p>We have two very simple functions, one to build up a stack from a Python
iterable and another to iterate through a stack and yield its items
one-by-one in order. There are also two functions to generate string representations
of stacks. They only differ in that one prints the terms in stack from left-to-right while the other prints from right-to-left. In both functions <em>internal stacks</em> are
printed left-to-right. These functions are written to support <a class="reference internal" href="pretty.html"><span class="doc">Tracing Joy Execution</span></a>.</p>
<dl class="function"> <dl class="function">
<dt id="joy.utils.stack.expression_to_string"> <dt id="joy.utils.stack.expression_to_string">
<code class="descclassname">joy.utils.stack.</code><code class="descname">expression_to_string</code><span class="sig-paren">(</span><em>expression</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/joy/utils/stack.html#expression_to_string"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#joy.utils.stack.expression_to_string" title="Permalink to this definition"></a></dt> <code class="descclassname">joy.utils.stack.</code><code class="descname">expression_to_string</code><span class="sig-paren">(</span><em>expression</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/joy/utils/stack.html#expression_to_string"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#joy.utils.stack.expression_to_string" title="Permalink to this definition"></a></dt>
@ -74,12 +88,32 @@ where they would be redundant.)</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="p">(</span><span class="n">top</span><span class="p">,</span> <span class="p">(</span><span class="n">second</span><span class="p">,</span> <span class="o">...</span><span class="p">))</span> <span class="o">-&gt;</span> <span class="s1">&#39;top second ...&#39;</span> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="p">(</span><span class="n">top</span><span class="p">,</span> <span class="p">(</span><span class="n">second</span><span class="p">,</span> <span class="o">...</span><span class="p">))</span> <span class="o">-&gt;</span> <span class="s1">&#39;top second ...&#39;</span>
</pre></div> </pre></div>
</div> </div>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><strong>expression</strong> (<em>stack</em>) A stack.</td>
</tr>
<tr class="field-even field"><th class="field-name">Return type:</th><td class="field-body">str</td>
</tr>
</tbody>
</table>
</dd></dl> </dd></dl>
<dl class="function"> <dl class="function">
<dt id="joy.utils.stack.iter_stack"> <dt id="joy.utils.stack.iter_stack">
<code class="descclassname">joy.utils.stack.</code><code class="descname">iter_stack</code><span class="sig-paren">(</span><em>stack</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/joy/utils/stack.html#iter_stack"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#joy.utils.stack.iter_stack" title="Permalink to this definition"></a></dt> <code class="descclassname">joy.utils.stack.</code><code class="descname">iter_stack</code><span class="sig-paren">(</span><em>stack</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/joy/utils/stack.html#iter_stack"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#joy.utils.stack.iter_stack" title="Permalink to this definition"></a></dt>
<dd><p>Iterate through the items on the stack.</p> <dd><p>Iterate through the items on the stack.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><strong>stack</strong> (<em>stack</em>) A stack.</td>
</tr>
<tr class="field-even field"><th class="field-name">Return type:</th><td class="field-body">iterator</td>
</tr>
</tbody>
</table>
</dd></dl> </dd></dl>
<dl class="function"> <dl class="function">
@ -89,12 +123,49 @@ where they would be redundant.)</p>
<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> <span class="o">-&gt;</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> <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> <span class="o">-&gt;</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>
</pre></div> </pre></div>
</div> </div>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple">
<li><strong>el</strong> (<em>list</em>) A Python list or other sequence (iterators and generators
wont work because <code class="docutils literal notranslate"><span class="pre">reverse()</span></code> is called on <code class="docutils literal notranslate"><span class="pre">el</span></code>.)</li>
<li><strong>stack</strong> (<em>stack</em>) A stack, optional, defaults to the empty stack.</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">Return type:</th><td class="field-body"><p class="first last">stack</p>
</td>
</tr>
</tbody>
</table>
</dd></dl> </dd></dl>
<dl class="function"> <dl class="function">
<dt id="joy.utils.stack.pick"> <dt id="joy.utils.stack.pick">
<code class="descclassname">joy.utils.stack.</code><code class="descname">pick</code><span class="sig-paren">(</span><em>s</em>, <em>n</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/joy/utils/stack.html#pick"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#joy.utils.stack.pick" title="Permalink to this definition"></a></dt> <code class="descclassname">joy.utils.stack.</code><code class="descname">pick</code><span class="sig-paren">(</span><em>s</em>, <em>n</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/joy/utils/stack.html#pick"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#joy.utils.stack.pick" title="Permalink to this definition"></a></dt>
<dd><p>Find the nth item on the stack. (Pick with zero is the same as “dup”.)</p> <dd><p>Return the nth item on the stack.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple">
<li><strong>s</strong> (<em>stack</em>) A stack.</li>
<li><strong>n</strong> (<em>int</em>) An index into the stack.</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">Raises:</th><td class="field-body"><ul class="first simple">
<li><strong>ValueError</strong> if <code class="docutils literal notranslate"><span class="pre">n</span></code> is less than zero.</li>
<li><strong>IndexError</strong> if <code class="docutils literal notranslate"><span class="pre">n</span></code> is equal to or greater than the length of <code class="docutils literal notranslate"><span class="pre">s</span></code>.</li>
</ul>
</td>
</tr>
<tr class="field-odd field"><th class="field-name">Return type:</th><td class="field-body"><p class="first last">whatever</p>
</td>
</tr>
</tbody>
</table>
</dd></dl> </dd></dl>
<dl class="function"> <dl class="function">
@ -102,6 +173,24 @@ where they would be redundant.)</p>
<code class="descclassname">joy.utils.stack.</code><code class="descname">pushback</code><span class="sig-paren">(</span><em>quote</em>, <em>expression</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/joy/utils/stack.html#pushback"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#joy.utils.stack.pushback" title="Permalink to this definition"></a></dt> <code class="descclassname">joy.utils.stack.</code><code class="descname">pushback</code><span class="sig-paren">(</span><em>quote</em>, <em>expression</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/joy/utils/stack.html#pushback"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#joy.utils.stack.pushback" title="Permalink to this definition"></a></dt>
<dd><p>Concatinate quote onto expression.</p> <dd><p>Concatinate quote onto expression.</p>
<p>In joy [1 2] [3 4] would become [1 2 3 4].</p> <p>In joy [1 2] [3 4] would become [1 2 3 4].</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple">
<li><strong>quote</strong> (<em>stack</em>) A stack.</li>
<li><strong>expression</strong> (<em>stack</em>) A stack.</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">Raises:</th><td class="field-body"><p class="first"><strong>RuntimeError</strong> if quote is larger than sys.getrecursionlimit().</p>
</td>
</tr>
<tr class="field-odd field"><th class="field-name">Return type:</th><td class="field-body"><p class="first last">stack</p>
</td>
</tr>
</tbody>
</table>
</dd></dl> </dd></dl>
<dl class="function"> <dl class="function">
@ -112,6 +201,16 @@ where they would be redundant.)</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="p">(</span><span class="n">top</span><span class="p">,</span> <span class="p">(</span><span class="n">second</span><span class="p">,</span> <span class="o">...</span><span class="p">))</span> <span class="o">-&gt;</span> <span class="s1">&#39;... second top&#39;</span> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="p">(</span><span class="n">top</span><span class="p">,</span> <span class="p">(</span><span class="n">second</span><span class="p">,</span> <span class="o">...</span><span class="p">))</span> <span class="o">-&gt;</span> <span class="s1">&#39;... second top&#39;</span>
</pre></div> </pre></div>
</div> </div>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><strong>stack</strong> (<em>stack</em>) A stack.</td>
</tr>
<tr class="field-even field"><th class="field-name">Return type:</th><td class="field-body">str</td>
</tr>
</tbody>
</table>
</dd></dl> </dd></dl>
</div> </div>

View File

@ -1,6 +1,80 @@
***************
``within``
***************
A Generator for Approximations
==============================
In :doc:`Generator Programs` we derive a function ``G`` (called ``make_generator`` in the dictionary) that accepts an initial value and a quoted program and returns a new quoted program that, when driven by the ``x`` combinator (:py:func:`joy.library.x`), acts like a lazy stream.
To make a generator that generates successive approximations let's start by assuming an initial approximation and then derive the function that computes the next approximation::
a F
---------
a'
A Function to Compute the Next Approximation
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Looking at the equation again:
:math:`a_{i+1} = \frac{(a_i+\frac{n}{a_i})}{2}`
::
a n over / + 2 /
a n a / + 2 /
a n/a + 2 /
a+n/a 2 /
(a+n/a)/2
The function we want has the argument ``n`` in it::
F == n over / + 2 /
Make it into a Generator
^^^^^^^^^^^^^^^^^^^^^^^^
Our generator would be created by::
a [dup F] make_generator
With ``n`` as part of the function ``F``, but ``n`` is the input to the ``sqrt`` function we're writing. If we let 1 be the initial approximation::
1 n 1 / + 2 /
1 n/1 + 2 /
1 n + 2 /
n+1 2 /
(n+1)/2
The generator can be written as::
1 swap [over / + 2 /] cons [dup] swoncat make_generator
Example::
23 1 swap [over / + 2 /] cons [dup] swoncat make_generator
1 23 [over / + 2 /] cons [dup] swoncat make_generator
1 [23 over / + 2 /] [dup] swoncat make_generator
1 [dup 23 over / + 2 /] make_generator
.
.
.
[1 swap [dup 23 over / + 2 /] direco]
A Generator of Square Root Approximations
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
::
gsra == 1 swap [over / + 2 /] cons [dup] swoncat make_generator
Finding Consecutive Approximations ``within`` a Tolerance
=========================================================
The remainder of a square root finder is a function *within*, which takes a tolerance and a list of approximations and looks down the list for two successive approximations that differ by no more than the given tolerance. The remainder of a square root finder is a function *within*, which takes a tolerance and a list of approximations and looks down the list for two successive approximations that differ by no more than the given tolerance.
@ -9,21 +83,23 @@ Hughes <https://www.cs.kent.ac.uk/people/staff/dat/miranda/whyfp90.pdf>`__
(And note that by "list" he means a lazily-evaluated list.) (And note that by "list" he means a lazily-evaluated list.)
Using a :doc:`generator <Generator Programs>` driven by ``x`` and assuming such for square root approximations (or whatever) ``G``, and further assuming that the first term ``a`` has been generated already and epsilon ``ε`` is handy on the stack... Using the *output* ``[a G]`` of the above :doc:`generator <Generator Programs>` for square root approximations, and further assuming that the first term ``a`` has been generated already and epsilon ``ε`` is handy on the stack...
:: ::
a [b G] ε within a [b G] ε within
-------------------- a b - abs ε <= ---------------------- a b - abs ε <=
b b
::
a [b G] ε within a [b G] ε within
-------------------- a b - abs ε > ---------------------- a b - abs ε >
. .
[b G] x ε ... [b G] x ε ...
b [c G] ε ... b [c G] ε ...
. .
-------------------- ----------------------
b [c G] ε within b [c G] ε within
@ -60,6 +136,7 @@ Base-Case
B == roll< popop first B == roll< popop first
Recur Recur
^^^^^^^^^^^^^ ^^^^^^^^^^^^^
@ -69,7 +146,7 @@ Recur
1. Discard ``a``. 1. Discard ``a``.
2. Use ``x`` combinator to generate next term from G. 2. Use ``x`` combinator to generate next term from ``G``.
3. Run ``within`` with ``i`` (it is a ``primrec`` function.) 3. Run ``within`` with ``i`` (it is a ``primrec`` function.)
:: ::
@ -87,38 +164,32 @@ Recur
R0 == [popd x] dip R0 == [popd x] dip
Setting up Setting up
^^^^^^^^^^ ^^^^^^^^^^
:: The recursive function we have defined so far needs a slight preamble: ``x`` to prime the generator and the epsilon value to use::
[a G] x ε [a G] x ε ...
a [b G] ε a [b G] ε ...
``within``
^^^^^^^^^^
Giving us the following definitions::
_within_P == [first - abs] dip <=
_within_B == roll< popop first
_within_R == [popd x] dip
within == x ε [_within_P] [_within_B] [_within_R] primrec
Finding Square Roots
====================
:: ::
within == x ε [[first - abs] dip <=] [roll< popop first] [[popd x] dip] primrec sqrt == gsra within