512 lines
36 KiB
HTML
512 lines
36 KiB
HTML
|
||
<!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 — 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 "warranty".
|
||
This is free software, and you are welcome to redistribute it
|
||
under certain conditions; type "sharing" for details.
|
||
Type "words" to see a list of all words, and "[<name>] help" to print the
|
||
docs for a word.
|
||
|
||
|
||
<-top
|
||
|
||
joy? _
|
||
</pre></div>
|
||
</div>
|
||
<p>The <code class="docutils literal notranslate"><span class="pre"><-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 <-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 "stack", "list", "sequence" and
|
||
"aggregate" 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 "tuple packing and unpacking" in its syntax which
|
||
means we can directly "unpack" the expected arguments to a Joy function.
|
||
|
||
For example:
|
||
|
||
def dup(stack):
|
||
head, tail = stack
|
||
return head, (head, tail)
|
||
|
||
We replace the argument "stack" by the expected structure of the stack,
|
||
in this case "(head, tail)", and Python takes care of de-structuring the
|
||
incoming argument and assigning values to the names. Note that Python
|
||
syntax doesn'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 <https://en.wikipedia.org/wiki/Purely_functional_data_structure>`__</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">'''</span>
|
||
<span class="sd"> Evaluate the Joy expression on the stack.</span>
|
||
<span class="sd"> '''</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. There’s 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">'''</span>
|
||
<span class="sd"> Return a stack/list expression of the tokens.</span>
|
||
<span class="sd"> '''</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">'['</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">']'</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">'One or more extra closing brackets.'</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">'One or more unclosed brackets.'</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>That’s 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">'1 2 3 4 5'</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">'[1 2 3] 4 5'</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">'1 23 ["four" [-5.0] cons] 8888'</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">'four'</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">'[][][][][]'</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">'[[[[[]]]]]'</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">' '</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>!= % & * *fraction *fraction0 + ++ - -- / < << <= <> = > >= >> ? ^ add anamorphism and app1 app2 app3 average b binary branch choice clear cleave concat cons dinfrirst dip dipd dipdd disenstacken div down_to_zero dudipd dup dupd dupdip enstacken eq first flatten floordiv gcd ge genrec getitem gt help i id ifte infra le least_fraction loop lshift lt map min mod modulus mul ne neg not nullary or over pam parse pm pop popd popdd popop pow pred primrec product quoted range range_to_zero rem remainder remove rest reverse roll< roll> rolldown rollup rshift run second select sharing shunt size sqr sqrt stack step sub succ sum swaack swap swoncat swons ternary third times truediv truthy tuck unary uncons unit unquoted unstack void warranty while words x xor zip •
|
||
</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 >] 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 >] [dup --] while
|
||
range_to_zero == unit [down_to_zero] infra
|
||
anamorphism == [pop []] swap [dip swons] genrec
|
||
range == [0 <=] [1 - dup] anamorphism
|
||
while == swap [nullary] cons dup dipd concat loop
|
||
dudipd == dup dipd
|
||
primrec == [i] genrec
|
||
</pre>
|
||
<p>Currently, there’s no function to add new definitions to the dictionary
|
||
from “within” Joy code itself. Adding new definitions remains a
|
||
meta-interpreter action. You have to do it yourself, in Python, and wash
|
||
your hands afterward.</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. There’s 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, that’s semi-annually) where any new definitions factored out of
|
||
the usage and macros of the previous time, along with new algorithms and
|
||
such, were entered into the dictionary and posted to e.g. IPFS.</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 shouldn’t be dozens of chat apps, JS
|
||
frameworks, programming languages. It’s 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 you’ll see that developing code in
|
||
Joy is a lot like doing simple mathematics, and the descriptions of the
|
||
code resemble math papers. The code also works the first time, no bugs.
|
||
If you have any experience programming at all, you are probably
|
||
skeptical, as I was, but it seems to work: deriving code mathematically
|
||
seems to lead to fewer errors.</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> |