Bumped version to 0.2.0; docs edits.
I've added cond and cmp to the library so that should bump the minor version no. I think.
This commit is contained in:
parent
22f7c6da00
commit
0de5029c98
2
Makefile
2
Makefile
|
|
@ -1,7 +1,7 @@
|
|||
# My make-fu style is old and tired. I just want to have a few helper commands.
|
||||
|
||||
TESTDIR = ./test00
|
||||
VERSION = 0.1.1
|
||||
VERSION = 0.2.0
|
||||
|
||||
.PHONY: clean sdist test docs
|
||||
|
||||
|
|
|
|||
2
README
2
README
|
|
@ -4,7 +4,7 @@
|
|||
|
||||
A dialect of Joy in Python.
|
||||
|
||||
v0.1.0
|
||||
v0.2.0
|
||||
|
||||
|
||||
--------------------------------------------------
|
||||
|
|
|
|||
|
|
@ -12286,7 +12286,7 @@ key new_value [ left right] con
|
|||
</div>
|
||||
<div class="inner_cell">
|
||||
<div class="text_cell_render border-box-sizing rendered_html">
|
||||
<h4 id="Now-we-can-define-Tree-add">Now we can define <code>Tree-add</code><a class="anchor-link" href="#Now-we-can-define-Tree-add">¶</a></h4>
|
||||
<h3 id="Now-we-can-define-Tree-add">Now we can define <code>Tree-add</code><a class="anchor-link" href="#Now-we-can-define-Tree-add">¶</a></h3>
|
||||
<pre><code>Tree-add == [popop not] [[pop] dipd Tree-new] [] [[P >] [T] [E] ifte] genrec
|
||||
|
||||
</code></pre>
|
||||
|
|
@ -12322,7 +12322,7 @@ Tree-add == [popop not] [[pop] dipd Tree-new] [] [R] genrec</code></pre>
|
|||
</div>
|
||||
<div class="inner_cell">
|
||||
<div class="text_cell_render border-box-sizing rendered_html">
|
||||
<h4 id="Examples">Examples<a class="anchor-link" href="#Examples">¶</a></h4>
|
||||
<h3 id="Examples">Examples<a class="anchor-link" href="#Examples">¶</a></h3>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
|
@ -12820,7 +12820,7 @@ Tree-add == [popop not] [[pop] dipd Tree-new] [] [R] genrec</code></pre>
|
|||
</div>
|
||||
<div class="inner_cell">
|
||||
<div class="text_cell_render border-box-sizing rendered_html">
|
||||
<h3 id="Processing-the-current-node.">Processing the current node.<a class="anchor-link" href="#Processing-the-current-node.">¶</a></h3><p><code>R0</code> is almost certainly going to use <code>dup</code> to make a copy of the node and then <code>dip</code> on some function to process the copy with it:</p>
|
||||
<h4 id="Processing-the-current-node.">Processing the current node.<a class="anchor-link" href="#Processing-the-current-node.">¶</a></h4><p><code>R0</code> is almost certainly going to use <code>dup</code> to make a copy of the node and then <code>dip</code> on some function to process the copy with it:</p>
|
||||
|
||||
<pre><code>[key value left right] [F] dupdip [Tree-iter] R1
|
||||
[key value left right] F [key value left right] [Tree-iter] R1
|
||||
|
|
@ -12841,7 +12841,7 @@ key [key value left right] [Tree-iter] R1</code></pre
|
|||
</div>
|
||||
<div class="inner_cell">
|
||||
<div class="text_cell_render border-box-sizing rendered_html">
|
||||
<h3 id="Recur">Recur<a class="anchor-link" href="#Recur">¶</a></h3><p>Now <code>R1</code> needs to apply <code>[Tree-iter]</code> to <code>left</code> and <code>right</code>. If we drop the key and value from the node using <code>rest</code> twice we are left with an interesting situation:</p>
|
||||
<h4 id="Recur">Recur<a class="anchor-link" href="#Recur">¶</a></h4><p>Now <code>R1</code> needs to apply <code>[Tree-iter]</code> to <code>left</code> and <code>right</code>. If we drop the key and value from the node using <code>rest</code> twice we are left with an interesting situation:</p>
|
||||
|
||||
<pre><code>key [key value left right] [Tree-iter] R1
|
||||
key [key value left right] [Tree-iter] [rest rest] dip
|
||||
|
|
@ -12907,7 +12907,7 @@ key left-keys right-keys
|
|||
</div>
|
||||
<div class="inner_cell">
|
||||
<div class="text_cell_render border-box-sizing rendered_html">
|
||||
<h2 id="Tree-iter"><code>Tree-iter</code><a class="anchor-link" href="#Tree-iter">¶</a></h2>
|
||||
<h3 id="Tree-iter"><code>Tree-iter</code><a class="anchor-link" href="#Tree-iter">¶</a></h3>
|
||||
<pre><code>Tree-iter == [not] [pop] roll< [dupdip rest rest] cons [step] genrec</code></pre>
|
||||
|
||||
</div>
|
||||
|
|
@ -12930,7 +12930,7 @@ key left-keys right-keys
|
|||
</div>
|
||||
<div class="inner_cell">
|
||||
<div class="text_cell_render border-box-sizing rendered_html">
|
||||
<h4 id="Examples">Examples<a class="anchor-link" href="#Examples">¶</a></h4>
|
||||
<h3 id="Examples">Examples<a class="anchor-link" href="#Examples">¶</a></h3>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
|
@ -13192,7 +13192,7 @@ key left-keys right-keys
|
|||
</div>
|
||||
<div class="inner_cell">
|
||||
<div class="text_cell_render border-box-sizing rendered_html">
|
||||
<h4 id="Process-the-left-child.">Process the left child.<a class="anchor-link" href="#Process-the-left-child.">¶</a></h4><p>Staring at this for a bit suggests <code>dup third</code> to start:</p>
|
||||
<h3 id="Process-the-left-child.">Process the left child.<a class="anchor-link" href="#Process-the-left-child.">¶</a></h3><p>Staring at this for a bit suggests <code>dup third</code> to start:</p>
|
||||
|
||||
<pre><code>[key value left right] R0 [Tree-iter-order] R1
|
||||
[key value left right] dup third [Tree-iter-order] R1
|
||||
|
|
@ -13219,7 +13219,7 @@ left Tree-iter-order [key value left right] [Tree-iter-order]</code>
|
|||
</div>
|
||||
<div class="inner_cell">
|
||||
<div class="text_cell_render border-box-sizing rendered_html">
|
||||
<h4 id="Process-the-current-node.">Process the current node.<a class="anchor-link" href="#Process-the-current-node.">¶</a></h4><p>So far, so good. Now we need to process the current node's values:</p>
|
||||
<h3 id="Process-the-current-node.">Process the current node.<a class="anchor-link" href="#Process-the-current-node.">¶</a></h3><p>So far, so good. Now we need to process the current node's values:</p>
|
||||
|
||||
<pre><code>left Tree-iter-order [key value left right] [Tree-iter-order] [[F] dupdip] dip
|
||||
left Tree-iter-order [key value left right] [F] dupdip [Tree-iter-order]
|
||||
|
|
@ -13238,7 +13238,7 @@ left Tree-iter-order key [key value left right] [Tree-iter-order]</code></pre>
|
|||
</div>
|
||||
<div class="inner_cell">
|
||||
<div class="text_cell_render border-box-sizing rendered_html">
|
||||
<h4 id="Process-the-right-child.">Process the right child.<a class="anchor-link" href="#Process-the-right-child.">¶</a></h4><p>First ditch the rest of the node and get the right child:</p>
|
||||
<h3 id="Process-the-right-child.">Process the right child.<a class="anchor-link" href="#Process-the-right-child.">¶</a></h3><p>First ditch the rest of the node and get the right child:</p>
|
||||
|
||||
<pre><code>left Tree-iter-order key [key value left right] [Tree-iter-order] [rest rest rest first] dip
|
||||
left Tree-iter-order key right [Tree-iter-order]
|
||||
|
|
@ -13256,7 +13256,7 @@ left Tree-iter-order key right Tree-iter-order</code></pre>
|
|||
</div>
|
||||
<div class="inner_cell">
|
||||
<div class="text_cell_render border-box-sizing rendered_html">
|
||||
<h4 id="Defining-Tree-iter-order">Defining <code>Tree-iter-order</code><a class="anchor-link" href="#Defining-Tree-iter-order">¶</a></h4><p>The result is a little awkward:</p>
|
||||
<h3 id="Defining-Tree-iter-order">Defining <code>Tree-iter-order</code><a class="anchor-link" href="#Defining-Tree-iter-order">¶</a></h3><p>The result is a little awkward:</p>
|
||||
|
||||
<pre><code>R1 == [cons dip] dupdip [[F] dupdip] dip [rest rest rest first] dip i
|
||||
|
||||
|
|
@ -13372,7 +13372,7 @@ Tree-iter-order == [not] [pop] [dup third] [proc_left proc_current proc_right] g
|
|||
</div>
|
||||
<div class="inner_cell">
|
||||
<div class="text_cell_render border-box-sizing rendered_html">
|
||||
<h4 id="The-base-case-[]">The base case <code>[]</code><a class="anchor-link" href="#The-base-case-[]">¶</a></h4><p>As before, the stopping predicate just has to detect the empty list:</p>
|
||||
<h3 id="The-base-case-[]">The base case <code>[]</code><a class="anchor-link" href="#The-base-case-[]">¶</a></h3><p>As before, the stopping predicate just has to detect the empty list:</p>
|
||||
|
||||
<pre><code>Tree-get == [pop not] [E] [R0] [R1] genrec
|
||||
|
||||
|
|
@ -13399,7 +13399,7 @@ tree key [pop not] [E] [R0] [R1] genrec
|
|||
</div>
|
||||
<div class="inner_cell">
|
||||
<div class="text_cell_render border-box-sizing rendered_html">
|
||||
<h4 id="Node-case-[key-value-left-right]">Node case <code>[key value left right]</code><a class="anchor-link" href="#Node-case-[key-value-left-right]">¶</a></h4><p>Now we need to figure out <code>R0</code> and <code>R1</code>:</p>
|
||||
<h3 id="Node-case-[key-value-left-right]">Node case <code>[key value left right]</code><a class="anchor-link" href="#Node-case-[key-value-left-right]">¶</a></h3><p>Now we need to figure out <code>R0</code> and <code>R1</code>:</p>
|
||||
|
||||
<pre><code>[key value left right] key R0 [BTree-get] R1
|
||||
|
||||
|
|
@ -13415,7 +13415,7 @@ tree key [pop not] [E] [R0] [R1] genrec
|
|||
</div>
|
||||
<div class="inner_cell">
|
||||
<div class="text_cell_render border-box-sizing rendered_html">
|
||||
<h4 id="Predicate:">Predicate:<a class="anchor-link" href="#Predicate:">¶</a></h4>
|
||||
<h4 id="Predicate">Predicate<a class="anchor-link" href="#Predicate">¶</a></h4>
|
||||
<pre><code>P == over [get-node-key] nullary
|
||||
get-node-key == pop popop first
|
||||
|
||||
|
|
@ -13670,7 +13670,7 @@ Tree-get == [pop not] swap [] [P [T>] [E] [T<] cmp] genrec</code></pre>
|
|||
</div>
|
||||
<div class="inner_cell">
|
||||
<div class="text_cell_render border-box-sizing rendered_html">
|
||||
<h1 id="Tree-delete">Tree-delete<a class="anchor-link" href="#Tree-delete">¶</a></h1><p>Now let's write a function that can return a tree datastructure with a key, value pair deleted:</p>
|
||||
<h2 id="Tree-delete">Tree-delete<a class="anchor-link" href="#Tree-delete">¶</a></h2><p>Now let's write a function that can return a tree datastructure with a key, value pair deleted:</p>
|
||||
|
||||
<pre><code> tree key Tree-delete
|
||||
---------------------------
|
||||
|
|
|
|||
|
|
@ -386,7 +386,7 @@
|
|||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"#### Now we can define `Tree-add`\n",
|
||||
"### Now we can define `Tree-add`\n",
|
||||
" Tree-add == [popop not] [[pop] dipd Tree-new] [] [[P >] [T] [E] ifte] genrec\n",
|
||||
"\n",
|
||||
"Putting it all together:\n",
|
||||
|
|
@ -415,7 +415,7 @@
|
|||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"#### Examples"
|
||||
"### Examples"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
@ -751,7 +751,7 @@
|
|||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Processing the current node.\n",
|
||||
"#### Processing the current node.\n",
|
||||
"\n",
|
||||
"`R0` is almost certainly going to use `dup` to make a copy of the node and then `dip` on some function to process the copy with it:\n",
|
||||
"\n",
|
||||
|
|
@ -771,7 +771,7 @@
|
|||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Recur\n",
|
||||
"#### Recur\n",
|
||||
"Now `R1` needs to apply `[Tree-iter]` to `left` and `right`. If we drop the key and value from the node using `rest` twice we are left with an interesting situation:\n",
|
||||
"\n",
|
||||
" key [key value left right] [Tree-iter] R1\n",
|
||||
|
|
@ -829,7 +829,7 @@
|
|||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## `Tree-iter`\n",
|
||||
"### `Tree-iter`\n",
|
||||
"\n",
|
||||
" Tree-iter == [not] [pop] roll< [dupdip rest rest] cons [step] genrec"
|
||||
]
|
||||
|
|
@ -847,7 +847,7 @@
|
|||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"#### Examples"
|
||||
"### Examples"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
@ -1011,7 +1011,7 @@
|
|||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"#### Process the left child.\n",
|
||||
"### Process the left child.\n",
|
||||
"Staring at this for a bit suggests `dup third` to start:\n",
|
||||
"\n",
|
||||
" [key value left right] R0 [Tree-iter-order] R1\n",
|
||||
|
|
@ -1035,7 +1035,7 @@
|
|||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"#### Process the current node.\n",
|
||||
"### Process the current node.\n",
|
||||
"So far, so good. Now we need to process the current node's values:\n",
|
||||
"\n",
|
||||
" left Tree-iter-order [key value left right] [Tree-iter-order] [[F] dupdip] dip\n",
|
||||
|
|
@ -1052,7 +1052,7 @@
|
|||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"#### Process the right child.\n",
|
||||
"### Process the right child.\n",
|
||||
"First ditch the rest of the node and get the right child:\n",
|
||||
"\n",
|
||||
" left Tree-iter-order key [key value left right] [Tree-iter-order] [rest rest rest first] dip\n",
|
||||
|
|
@ -1068,7 +1068,7 @@
|
|||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"#### Defining `Tree-iter-order`\n",
|
||||
"### Defining `Tree-iter-order`\n",
|
||||
"The result is a little awkward:\n",
|
||||
"\n",
|
||||
" R1 == [cons dip] dupdip [[F] dupdip] dip [rest rest rest first] dip i\n",
|
||||
|
|
@ -1163,7 +1163,7 @@
|
|||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"#### The base case `[]`\n",
|
||||
"### The base case `[]`\n",
|
||||
"As before, the stopping predicate just has to detect the empty list:\n",
|
||||
"\n",
|
||||
" Tree-get == [pop not] [E] [R0] [R1] genrec\n",
|
||||
|
|
@ -1186,7 +1186,7 @@
|
|||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"#### Node case `[key value left right]`\n",
|
||||
"### Node case `[key value left right]`\n",
|
||||
"Now we need to figure out `R0` and `R1`: \n",
|
||||
"\n",
|
||||
" [key value left right] key R0 [BTree-get] R1\n",
|
||||
|
|
@ -1200,7 +1200,7 @@
|
|||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"#### Predicate:\n",
|
||||
"#### Predicate\n",
|
||||
"\n",
|
||||
" P == over [get-node-key] nullary\n",
|
||||
" get-node-key == pop popop first\n",
|
||||
|
|
@ -1386,7 +1386,7 @@
|
|||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Tree-delete\n",
|
||||
"## Tree-delete\n",
|
||||
"\n",
|
||||
"Now let's write a function that can return a tree datastructure with a key, value pair deleted:\n",
|
||||
"\n",
|
||||
|
|
|
|||
|
|
@ -234,7 +234,7 @@ J('["k" "old_value" "left" "right"] "new_value" "k" ["Tree-add"] Ee')
|
|||
['k' 'new_value' 'left' 'right']
|
||||
|
||||
|
||||
#### Now we can define `Tree-add`
|
||||
### Now we can define `Tree-add`
|
||||
Tree-add == [popop not] [[pop] dipd Tree-new] [] [[P >] [T] [E] ifte] genrec
|
||||
|
||||
Putting it all together:
|
||||
|
|
@ -254,7 +254,7 @@ Putting it all together:
|
|||
define('Tree-add == [popop not] [[pop] dipd Tree-new] [] [[P >] [T] [E] ifte] genrec')
|
||||
```
|
||||
|
||||
#### Examples
|
||||
### Examples
|
||||
|
||||
|
||||
```python
|
||||
|
|
@ -426,7 +426,7 @@ Let's look at it *in situ*:
|
|||
|
||||
[key value left right] R0 [Tree-iter] R1
|
||||
|
||||
### Processing the current node.
|
||||
#### Processing the current node.
|
||||
|
||||
`R0` is almost certainly going to use `dup` to make a copy of the node and then `dip` on some function to process the copy with it:
|
||||
|
||||
|
|
@ -441,7 +441,7 @@ For example, if we're getting all the keys `F` would be `first`:
|
|||
[key value left right] first [key value left right] [Tree-iter] R1
|
||||
key [key value left right] [Tree-iter] R1
|
||||
|
||||
### Recur
|
||||
#### Recur
|
||||
Now `R1` needs to apply `[Tree-iter]` to `left` and `right`. If we drop the key and value from the node using `rest` twice we are left with an interesting situation:
|
||||
|
||||
key [key value left right] [Tree-iter] R1
|
||||
|
|
@ -484,7 +484,7 @@ Working backward:
|
|||
[not] [pop] [F] [dupdip rest rest] cons [step] genrec
|
||||
[F] [not] [pop] roll< [dupdip rest rest] cons [step] genrec
|
||||
|
||||
## `Tree-iter`
|
||||
### `Tree-iter`
|
||||
|
||||
Tree-iter == [not] [pop] roll< [dupdip rest rest] cons [step] genrec
|
||||
|
||||
|
|
@ -493,7 +493,7 @@ Working backward:
|
|||
define('Tree-iter == [not] [pop] roll< [dupdip rest rest] cons [step] genrec')
|
||||
```
|
||||
|
||||
#### Examples
|
||||
### Examples
|
||||
|
||||
|
||||
```python
|
||||
|
|
@ -568,7 +568,7 @@ To define `R0` and `R1` it helps to look at them as they will appear when they r
|
|||
|
||||
[key value left right] R0 [BTree-iter-order] R1
|
||||
|
||||
#### Process the left child.
|
||||
### Process the left child.
|
||||
Staring at this for a bit suggests `dup third` to start:
|
||||
|
||||
[key value left right] R0 [Tree-iter-order] R1
|
||||
|
|
@ -582,7 +582,7 @@ Now maybe:
|
|||
[key value left right] [left Tree-iter-order] dip [Tree-iter-order]
|
||||
left Tree-iter-order [key value left right] [Tree-iter-order]
|
||||
|
||||
#### Process the current node.
|
||||
### Process the current node.
|
||||
So far, so good. Now we need to process the current node's values:
|
||||
|
||||
left Tree-iter-order [key value left right] [Tree-iter-order] [[F] dupdip] dip
|
||||
|
|
@ -594,7 +594,7 @@ If `F` needs items from the stack below the left stuff it should have `cons`'d t
|
|||
left Tree-iter-order [key value left right] first [key value left right] [Tree-iter-order]
|
||||
left Tree-iter-order key [key value left right] [Tree-iter-order]
|
||||
|
||||
#### Process the right child.
|
||||
### Process the right child.
|
||||
First ditch the rest of the node and get the right child:
|
||||
|
||||
left Tree-iter-order key [key value left right] [Tree-iter-order] [rest rest rest first] dip
|
||||
|
|
@ -605,7 +605,7 @@ Then, of course, we just need `i` to run `Tree-iter-order` on the right side:
|
|||
left Tree-iter-order key right [Tree-iter-order] i
|
||||
left Tree-iter-order key right Tree-iter-order
|
||||
|
||||
#### Defining `Tree-iter-order`
|
||||
### Defining `Tree-iter-order`
|
||||
The result is a little awkward:
|
||||
|
||||
R1 == [cons dip] dupdip [[F] dupdip] dip [rest rest rest first] dip i
|
||||
|
|
@ -673,7 +673,7 @@ Let's pass the buck to the caller by making the base case a given, you have to d
|
|||
---------------------------- key not in tree
|
||||
[] key E
|
||||
|
||||
#### The base case `[]`
|
||||
### The base case `[]`
|
||||
As before, the stopping predicate just has to detect the empty list:
|
||||
|
||||
Tree-get == [pop not] [E] [R0] [R1] genrec
|
||||
|
|
@ -691,7 +691,7 @@ The anonymous specialized recursive function that will do the real work.
|
|||
|
||||
[pop not] [E] [R0] [R1] genrec
|
||||
|
||||
#### Node case `[key value left right]`
|
||||
### Node case `[key value left right]`
|
||||
Now we need to figure out `R0` and `R1`:
|
||||
|
||||
[key value left right] key R0 [BTree-get] R1
|
||||
|
|
@ -700,7 +700,7 @@ We want to compare the search key with the key in the node, and if they are the
|
|||
|
||||
[key value left right] key [BTree-get] P [T>] [E] [T<] cmp
|
||||
|
||||
#### Predicate:
|
||||
#### Predicate
|
||||
|
||||
P == over [get-node-key] nullary
|
||||
get-node-key == pop popop first
|
||||
|
|
@ -821,7 +821,7 @@ J('''
|
|||
'not found'
|
||||
|
||||
|
||||
# Tree-delete
|
||||
## Tree-delete
|
||||
|
||||
Now let's write a function that can return a tree datastructure with a key, value pair deleted:
|
||||
|
||||
|
|
|
|||
|
|
@ -336,7 +336,7 @@ Example:
|
|||
|
||||
|
||||
Now we can define ``Tree-add``
|
||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
::
|
||||
|
||||
|
|
@ -361,7 +361,7 @@ Putting it all together:
|
|||
define('Tree-add == [popop not] [[pop] dipd Tree-new] [] [[P >] [T] [E] ifte] genrec')
|
||||
|
||||
Examples
|
||||
^^^^^^^^
|
||||
~~~~~~~~
|
||||
|
||||
.. code:: ipython2
|
||||
|
||||
|
|
@ -594,7 +594,7 @@ Let's look at it *in situ*:
|
|||
[key value left right] R0 [Tree-iter] R1
|
||||
|
||||
Processing the current node.
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
``R0`` is almost certainly going to use ``dup`` to make a copy of the
|
||||
node and then ``dip`` on some function to process the copy with it:
|
||||
|
|
@ -615,7 +615,7 @@ For example, if we're getting all the keys ``F`` would be ``first``:
|
|||
key [key value left right] [Tree-iter] R1
|
||||
|
||||
Recur
|
||||
~~~~~
|
||||
^^^^^
|
||||
|
||||
Now ``R1`` needs to apply ``[Tree-iter]`` to ``left`` and ``right``. If
|
||||
we drop the key and value from the node using ``rest`` twice we are left
|
||||
|
|
@ -680,7 +680,7 @@ Working backward:
|
|||
[F] [not] [pop] roll< [dupdip rest rest] cons [step] genrec
|
||||
|
||||
``Tree-iter``
|
||||
-------------
|
||||
~~~~~~~~~~~~~
|
||||
|
||||
::
|
||||
|
||||
|
|
@ -691,7 +691,7 @@ Working backward:
|
|||
define('Tree-iter == [not] [pop] roll< [dupdip rest rest] cons [step] genrec')
|
||||
|
||||
Examples
|
||||
^^^^^^^^
|
||||
~~~~~~~~
|
||||
|
||||
.. code:: ipython2
|
||||
|
||||
|
|
@ -793,7 +793,7 @@ when they run:
|
|||
[key value left right] R0 [BTree-iter-order] R1
|
||||
|
||||
Process the left child.
|
||||
^^^^^^^^^^^^^^^^^^^^^^^
|
||||
~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
Staring at this for a bit suggests ``dup third`` to start:
|
||||
|
||||
|
|
@ -813,7 +813,7 @@ Now maybe:
|
|||
left Tree-iter-order [key value left right] [Tree-iter-order]
|
||||
|
||||
Process the current node.
|
||||
^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
So far, so good. Now we need to process the current node's values:
|
||||
|
||||
|
|
@ -833,7 +833,7 @@ works fine as-is.
|
|||
left Tree-iter-order key [key value left right] [Tree-iter-order]
|
||||
|
||||
Process the right child.
|
||||
^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
First ditch the rest of the node and get the right child:
|
||||
|
||||
|
|
@ -851,7 +851,7 @@ right side:
|
|||
left Tree-iter-order key right Tree-iter-order
|
||||
|
||||
Defining ``Tree-iter-order``
|
||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
The result is a little awkward:
|
||||
|
||||
|
|
@ -938,7 +938,7 @@ have to decide for yourself what ``[E]`` should be.
|
|||
[] key E
|
||||
|
||||
The base case ``[]``
|
||||
^^^^^^^^^^^^^^^^^^^^
|
||||
~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
As before, the stopping predicate just has to detect the empty list:
|
||||
|
||||
|
|
@ -969,7 +969,7 @@ The anonymous specialized recursive function that will do the real work.
|
|||
[pop not] [E] [R0] [R1] genrec
|
||||
|
||||
Node case ``[key value left right]``
|
||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
Now we need to figure out ``R0`` and ``R1``:
|
||||
|
||||
|
|
@ -986,8 +986,8 @@ and ``R1 == P [T>] [E] [T<] cmp``:
|
|||
|
||||
[key value left right] key [BTree-get] P [T>] [E] [T<] cmp
|
||||
|
||||
Predicate:
|
||||
^^^^^^^^^^
|
||||
Predicate
|
||||
^^^^^^^^^
|
||||
|
||||
::
|
||||
|
||||
|
|
@ -1142,7 +1142,7 @@ So:
|
|||
|
||||
|
||||
Tree-delete
|
||||
===========
|
||||
-----------
|
||||
|
||||
Now let's write a function that can return a tree datastructure with a
|
||||
key, value pair deleted:
|
||||
|
|
|
|||
|
|
@ -11775,7 +11775,7 @@ div#notebook {
|
|||
</div>
|
||||
<div class="inner_cell">
|
||||
<div class="text_cell_render border-box-sizing rendered_html">
|
||||
<h1 id="Treating-Trees-II">Treating Trees II<a class="anchor-link" href="#Treating-Trees-II">¶</a></h1><p>Let's consider a tree structure, similar to one described <a href="https://www.cs.kent.ac.uk/people/staff/dat/miranda/whyfp90.pdf">"Why functional programming matters" by John Hughes</a>, that consists of a node value followed by a sequence of zero or more child trees. (The asterisk is meant to indicate the <a href="https://en.wikipedia.org/wiki/Kleene_star">Kleene star</a>.)</p>
|
||||
<h1 id="Treating-Trees-II:-treestep">Treating Trees II: <code>treestep</code><a class="anchor-link" href="#Treating-Trees-II:-treestep">¶</a></h1><p>Let's consider a tree structure, similar to one described <a href="https://www.cs.kent.ac.uk/people/staff/dat/miranda/whyfp90.pdf">"Why functional programming matters" by John Hughes</a>, that consists of a node value followed by zero or more child trees. (The asterisk is meant to indicate the <a href="https://en.wikipedia.org/wiki/Kleene_star">Kleene star</a>.)</p>
|
||||
|
||||
<pre><code>tree = [] | [node tree*]</code></pre>
|
||||
|
||||
|
|
@ -11786,7 +11786,7 @@ div#notebook {
|
|||
</div>
|
||||
<div class="inner_cell">
|
||||
<div class="text_cell_render border-box-sizing rendered_html">
|
||||
<h2 id="treestep"><code>treestep</code><a class="anchor-link" href="#treestep">¶</a></h2><p>In the spirit of <code>step</code> we are going to define a combinator <code>treestep</code> which expects a tree and three additional items: a base-case function <code>[B]</code>, and two quoted programs <code>[N]</code> and <code>[C]</code>.</p>
|
||||
<p>In the spirit of <code>step</code> we are going to define a combinator <code>treestep</code> which expects a tree and three additional items: a base-case function <code>[B]</code>, and two quoted programs <code>[N]</code> and <code>[C]</code>.</p>
|
||||
|
||||
<pre><code>tree [B] [N] [C] treestep</code></pre>
|
||||
|
||||
|
|
@ -12927,46 +12927,6 @@ Tree-get == [P [T>] [E] [T<] cmp] treegrind
|
|||
<div class="cell border-box-sizing code_cell rendered">
|
||||
<div class="input">
|
||||
<div class="prompt input_prompt">In [23]:</div>
|
||||
<div class="inner_cell">
|
||||
<div class="input_area">
|
||||
<div class=" highlight hl-ipython2"><pre><span></span><span class="kn">from</span> <span class="nn">joy.library</span> <span class="kn">import</span> <span class="n">FunctionWrapper</span>
|
||||
<span class="kn">from</span> <span class="nn">joy.utils.stack</span> <span class="kn">import</span> <span class="n">pushback</span>
|
||||
|
||||
|
||||
<span class="nd">@FunctionWrapper</span>
|
||||
<span class="k">def</span> <span class="nf">cmp_</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="sd">'''</span>
|
||||
<span class="sd"> cmp takes two values and three quoted programs on the stack and runs</span>
|
||||
<span class="sd"> one of the three depending on the results of comparing the two values:</span>
|
||||
|
||||
<span class="sd"> a b [G] [E] [L] cmp</span>
|
||||
<span class="sd"> ------------------------- a > b</span>
|
||||
<span class="sd"> G</span>
|
||||
|
||||
<span class="sd"> a b [G] [E] [L] cmp</span>
|
||||
<span class="sd"> ------------------------- a = b</span>
|
||||
<span class="sd"> E</span>
|
||||
|
||||
<span class="sd"> a b [G] [E] [L] cmp</span>
|
||||
<span class="sd"> ------------------------- a < b</span>
|
||||
<span class="sd"> L</span>
|
||||
<span class="sd"> '''</span>
|
||||
<span class="n">L</span><span class="p">,</span> <span class="p">(</span><span class="n">E</span><span class="p">,</span> <span class="p">(</span><span class="n">G</span><span class="p">,</span> <span class="p">(</span><span class="n">b</span><span class="p">,</span> <span class="p">(</span><span class="n">a</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">pushback</span><span class="p">(</span><span class="n">G</span> <span class="k">if</span> <span class="n">a</span> <span class="o">></span> <span class="n">b</span> <span class="k">else</span> <span class="n">L</span> <span class="k">if</span> <span class="n">a</span> <span class="o"><</span> <span class="n">b</span> <span class="k">else</span> <span class="n">E</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>
|
||||
|
||||
|
||||
<span class="n">D</span><span class="p">[</span><span class="s1">'cmp'</span><span class="p">]</span> <span class="o">=</span> <span class="n">cmp_</span>
|
||||
</pre></div>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
<div class="cell border-box-sizing code_cell rendered">
|
||||
<div class="input">
|
||||
<div class="prompt input_prompt">In [24]:</div>
|
||||
<div class="inner_cell">
|
||||
<div class="input_area">
|
||||
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">'''</span><span class="se">\</span>
|
||||
|
|
@ -13003,7 +12963,7 @@ Tree-get == [P [T>] [E] [T<] cmp] treegrind
|
|||
</div>
|
||||
<div class="cell border-box-sizing code_cell rendered">
|
||||
<div class="input">
|
||||
<div class="prompt input_prompt">In [25]:</div>
|
||||
<div class="prompt input_prompt">In [24]:</div>
|
||||
<div class="inner_cell">
|
||||
<div class="input_area">
|
||||
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">'''</span><span class="se">\</span>
|
||||
|
|
|
|||
|
|
@ -4,8 +4,8 @@
|
|||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Treating Trees II\n",
|
||||
"Let's consider a tree structure, similar to one described [\"Why functional programming matters\" by John Hughes](https://www.cs.kent.ac.uk/people/staff/dat/miranda/whyfp90.pdf), that consists of a node value followed by a sequence of zero or more child trees. (The asterisk is meant to indicate the [Kleene star](https://en.wikipedia.org/wiki/Kleene_star).)\n",
|
||||
"# Treating Trees II: `treestep`\n",
|
||||
"Let's consider a tree structure, similar to one described [\"Why functional programming matters\" by John Hughes](https://www.cs.kent.ac.uk/people/staff/dat/miranda/whyfp90.pdf), that consists of a node value followed by zero or more child trees. (The asterisk is meant to indicate the [Kleene star](https://en.wikipedia.org/wiki/Kleene_star).)\n",
|
||||
"\n",
|
||||
" tree = [] | [node tree*]"
|
||||
]
|
||||
|
|
@ -14,7 +14,6 @@
|
|||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## `treestep`\n",
|
||||
"In the spirit of `step` we are going to define a combinator `treestep` which expects a tree and three additional items: a base-case function `[B]`, and two quoted programs `[N]` and `[C]`.\n",
|
||||
"\n",
|
||||
" tree [B] [N] [C] treestep"
|
||||
|
|
@ -829,42 +828,6 @@
|
|||
"cell_type": "code",
|
||||
"execution_count": 23,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from joy.library import FunctionWrapper\n",
|
||||
"from joy.utils.stack import pushback\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"@FunctionWrapper\n",
|
||||
"def cmp_(stack, expression, dictionary):\n",
|
||||
" '''\n",
|
||||
" cmp takes two values and three quoted programs on the stack and runs\n",
|
||||
" one of the three depending on the results of comparing the two values:\n",
|
||||
"\n",
|
||||
" a b [G] [E] [L] cmp\n",
|
||||
" ------------------------- a > b\n",
|
||||
" G\n",
|
||||
"\n",
|
||||
" a b [G] [E] [L] cmp\n",
|
||||
" ------------------------- a = b\n",
|
||||
" E\n",
|
||||
"\n",
|
||||
" a b [G] [E] [L] cmp\n",
|
||||
" ------------------------- a < b\n",
|
||||
" L\n",
|
||||
" '''\n",
|
||||
" L, (E, (G, (b, (a, stack)))) = stack\n",
|
||||
" expression = pushback(G if a > b else L if a < b else E, expression)\n",
|
||||
" return stack, expression, dictionary\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"D['cmp'] = cmp_"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 24,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
|
|
@ -886,7 +849,7 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 25,
|
||||
"execution_count": 24,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
|
|
@ -906,13 +869,6 @@
|
|||
"\n",
|
||||
"''')"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
|
|
|
|||
|
|
@ -1,10 +1,9 @@
|
|||
|
||||
# Treating Trees II
|
||||
Let's consider a tree structure, similar to one described ["Why functional programming matters" by John Hughes](https://www.cs.kent.ac.uk/people/staff/dat/miranda/whyfp90.pdf), that consists of a node value followed by a sequence of zero or more child trees. (The asterisk is meant to indicate the [Kleene star](https://en.wikipedia.org/wiki/Kleene_star).)
|
||||
# Treating Trees II: `treestep`
|
||||
Let's consider a tree structure, similar to one described ["Why functional programming matters" by John Hughes](https://www.cs.kent.ac.uk/people/staff/dat/miranda/whyfp90.pdf), that consists of a node value followed by zero or more child trees. (The asterisk is meant to indicate the [Kleene star](https://en.wikipedia.org/wiki/Kleene_star).)
|
||||
|
||||
tree = [] | [node tree*]
|
||||
|
||||
## `treestep`
|
||||
In the spirit of `step` we are going to define a combinator `treestep` which expects a tree and three additional items: a base-case function `[B]`, and two quoted programs `[N]` and `[C]`.
|
||||
|
||||
tree [B] [N] [C] treestep
|
||||
|
|
@ -439,38 +438,6 @@ DefinitionWrapper.add_definitions('''
|
|||
```
|
||||
|
||||
|
||||
```python
|
||||
from joy.library import FunctionWrapper
|
||||
from joy.utils.stack import pushback
|
||||
|
||||
|
||||
@FunctionWrapper
|
||||
def cmp_(stack, expression, dictionary):
|
||||
'''
|
||||
cmp takes two values and three quoted programs on the stack and runs
|
||||
one of the three depending on the results of comparing the two values:
|
||||
|
||||
a b [G] [E] [L] cmp
|
||||
------------------------- a > b
|
||||
G
|
||||
|
||||
a b [G] [E] [L] cmp
|
||||
------------------------- a = b
|
||||
E
|
||||
|
||||
a b [G] [E] [L] cmp
|
||||
------------------------- a < b
|
||||
L
|
||||
'''
|
||||
L, (E, (G, (b, (a, stack)))) = stack
|
||||
expression = pushback(G if a > b else L if a < b else E, expression)
|
||||
return stack, expression, dictionary
|
||||
|
||||
|
||||
D['cmp'] = cmp_
|
||||
```
|
||||
|
||||
|
||||
```python
|
||||
J('''\
|
||||
|
||||
|
|
|
|||
|
|
@ -1,21 +1,18 @@
|
|||
|
||||
Treating Trees II
|
||||
=================
|
||||
Treating Trees II: ``treestep``
|
||||
===============================
|
||||
|
||||
Let's consider a tree structure, similar to one described `"Why
|
||||
functional programming matters" by John
|
||||
Hughes <https://www.cs.kent.ac.uk/people/staff/dat/miranda/whyfp90.pdf>`__,
|
||||
that consists of a node value followed by a sequence of zero or more
|
||||
child trees. (The asterisk is meant to indicate the `Kleene
|
||||
that consists of a node value followed by zero or more child trees. (The
|
||||
asterisk is meant to indicate the `Kleene
|
||||
star <https://en.wikipedia.org/wiki/Kleene_star>`__.)
|
||||
|
||||
::
|
||||
|
||||
tree = [] | [node tree*]
|
||||
|
||||
``treestep``
|
||||
------------
|
||||
|
||||
In the spirit of ``step`` we are going to define a combinator
|
||||
``treestep`` which expects a tree and three additional items: a
|
||||
base-case function ``[B]``, and two quoted programs ``[N]`` and ``[C]``.
|
||||
|
|
@ -591,37 +588,6 @@ To me, that seems simpler than the ``genrec`` version.
|
|||
|
||||
''', D)
|
||||
|
||||
.. code:: ipython2
|
||||
|
||||
from joy.library import FunctionWrapper
|
||||
from joy.utils.stack import pushback
|
||||
|
||||
|
||||
@FunctionWrapper
|
||||
def cmp_(stack, expression, dictionary):
|
||||
'''
|
||||
cmp takes two values and three quoted programs on the stack and runs
|
||||
one of the three depending on the results of comparing the two values:
|
||||
|
||||
a b [G] [E] [L] cmp
|
||||
------------------------- a > b
|
||||
G
|
||||
|
||||
a b [G] [E] [L] cmp
|
||||
------------------------- a = b
|
||||
E
|
||||
|
||||
a b [G] [E] [L] cmp
|
||||
------------------------- a < b
|
||||
L
|
||||
'''
|
||||
L, (E, (G, (b, (a, stack)))) = stack
|
||||
expression = pushback(G if a > b else L if a < b else E, expression)
|
||||
return stack, expression, dictionary
|
||||
|
||||
|
||||
D['cmp'] = cmp_
|
||||
|
||||
.. code:: ipython2
|
||||
|
||||
J('''\
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
<head>
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=Edge" />
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<title>Overview: module code — Thun 0.1.1 documentation</title>
|
||||
<title>Overview: module code — Thun 0.2.0 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>
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
<head>
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=Edge" />
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<title>joy.joy — Thun 0.1.1 documentation</title>
|
||||
<title>joy.joy — Thun 0.2.0 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>
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
<head>
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=Edge" />
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<title>joy.library — Thun 0.1.1 documentation</title>
|
||||
<title>joy.library — Thun 0.2.0 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>
|
||||
|
|
@ -1173,6 +1173,43 @@
|
|||
<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></div>
|
||||
|
||||
|
||||
<div class="viewcode-block" id="cond"><a class="viewcode-back" href="../../library.html#joy.library.cond">[docs]</a><span class="nd">@inscribe</span>
|
||||
<span class="nd">@FunctionWrapper</span>
|
||||
<span class="k">def</span> <span class="nf">cond</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="sd">'''</span>
|
||||
<span class="sd"> like a case statement; works by rewriting into a chain of ifte.</span>
|
||||
|
||||
<span class="sd"> [..[[Bi] Ti]..[D]] -> ...</span>
|
||||
|
||||
|
||||
<span class="sd"> [[[B0] T0] [[B1] T1] [D]] cond</span>
|
||||
<span class="sd"> -----------------------------------------</span>
|
||||
<span class="sd"> [B0] [T0] [[B1] [T1] [D] ifte] ifte</span>
|
||||
|
||||
<span class="sd"> '''</span>
|
||||
<span class="n">conditions</span><span class="p">,</span> <span class="n">stack</span> <span class="o">=</span> <span class="n">stack</span>
|
||||
<span class="k">if</span> <span class="n">conditions</span><span class="p">:</span>
|
||||
<span class="n">expression</span> <span class="o">=</span> <span class="n">_cond</span><span class="p">(</span><span class="n">conditions</span><span class="p">,</span> <span class="n">expression</span><span class="p">)</span>
|
||||
<span class="k">try</span><span class="p">:</span>
|
||||
<span class="c1"># Attempt to preload the args to first ifte.</span>
|
||||
<span class="p">(</span><span class="n">P</span><span class="p">,</span> <span class="p">(</span><span class="n">T</span><span class="p">,</span> <span class="p">(</span><span class="n">E</span><span class="p">,</span> <span class="n">expression</span><span class="p">)))</span> <span class="o">=</span> <span class="n">expression</span>
|
||||
<span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
|
||||
<span class="c1"># If, for any reason, the argument to cond should happen to contain</span>
|
||||
<span class="c1"># only the default clause then this optimization will fail.</span>
|
||||
<span class="k">pass</span>
|
||||
<span class="k">else</span><span class="p">:</span>
|
||||
<span class="n">stack</span> <span class="o">=</span> <span class="p">(</span><span class="n">E</span><span class="p">,</span> <span class="p">(</span><span class="n">T</span><span class="p">,</span> <span class="p">(</span><span class="n">P</span><span class="p">,</span> <span class="n">stack</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></div>
|
||||
|
||||
|
||||
<span class="k">def</span> <span class="nf">_cond</span><span class="p">(</span><span class="n">conditions</span><span class="p">,</span> <span class="n">expression</span><span class="p">):</span>
|
||||
<span class="p">(</span><span class="n">clause</span><span class="p">,</span> <span class="n">rest</span><span class="p">)</span> <span class="o">=</span> <span class="n">conditions</span>
|
||||
<span class="k">if</span> <span class="ow">not</span> <span class="n">rest</span><span class="p">:</span> <span class="c1"># clause is [D]</span>
|
||||
<span class="k">return</span> <span class="n">clause</span>
|
||||
<span class="n">P</span><span class="p">,</span> <span class="n">T</span> <span class="o">=</span> <span class="n">clause</span>
|
||||
<span class="k">return</span> <span class="p">(</span><span class="n">P</span><span class="p">,</span> <span class="p">(</span><span class="n">T</span><span class="p">,</span> <span class="p">(</span><span class="n">_cond</span><span class="p">(</span><span class="n">rest</span><span class="p">,</span> <span class="p">()),</span> <span class="p">(</span><span class="n">S_ifte</span><span class="p">,</span> <span class="n">expression</span><span class="p">))))</span>
|
||||
|
||||
|
||||
<div class="viewcode-block" id="dip"><a class="viewcode-back" href="../../library.html#joy.library.dip">[docs]</a><span class="nd">@inscribe</span>
|
||||
<span class="nd">@FunctionWrapper</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>
|
||||
|
|
@ -1393,6 +1430,30 @@
|
|||
<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></div>
|
||||
|
||||
|
||||
<div class="viewcode-block" id="cmp_"><a class="viewcode-back" href="../../library.html#joy.library.cmp_">[docs]</a><span class="nd">@inscribe</span>
|
||||
<span class="nd">@FunctionWrapper</span>
|
||||
<span class="k">def</span> <span class="nf">cmp_</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="sd">'''</span>
|
||||
<span class="sd"> cmp takes two values and three quoted programs on the stack and runs</span>
|
||||
<span class="sd"> one of the three depending on the results of comparing the two values:</span>
|
||||
|
||||
<span class="sd"> a b [G] [E] [L] cmp</span>
|
||||
<span class="sd"> ------------------------- a > b</span>
|
||||
<span class="sd"> G</span>
|
||||
|
||||
<span class="sd"> a b [G] [E] [L] cmp</span>
|
||||
<span class="sd"> ------------------------- a = b</span>
|
||||
<span class="sd"> E</span>
|
||||
|
||||
<span class="sd"> a b [G] [E] [L] cmp</span>
|
||||
<span class="sd"> ------------------------- a < b</span>
|
||||
<span class="sd"> L</span>
|
||||
<span class="sd"> '''</span>
|
||||
<span class="n">L</span><span class="p">,</span> <span class="p">(</span><span class="n">E</span><span class="p">,</span> <span class="p">(</span><span class="n">G</span><span class="p">,</span> <span class="p">(</span><span class="n">b</span><span class="p">,</span> <span class="p">(</span><span class="n">a</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">pushback</span><span class="p">(</span><span class="n">G</span> <span class="k">if</span> <span class="n">a</span> <span class="o">></span> <span class="n">b</span> <span class="k">else</span> <span class="n">L</span> <span class="k">if</span> <span class="n">a</span> <span class="o"><</span> <span class="n">b</span> <span class="k">else</span> <span class="n">E</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></div>
|
||||
|
||||
|
||||
<span class="c1">#def nullary(S, expression, dictionary):</span>
|
||||
<span class="c1"># '''</span>
|
||||
<span class="c1"># Run the program on TOS and return its first result without consuming</span>
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
<head>
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=Edge" />
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<title>joy.parser — Thun 0.1.1 documentation</title>
|
||||
<title>joy.parser — Thun 0.2.0 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>
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
<head>
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=Edge" />
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<title>joy.utils.pretty_print — Thun 0.1.1 documentation</title>
|
||||
<title>joy.utils.pretty_print — Thun 0.2.0 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>
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
<head>
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=Edge" />
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<title>joy.utils.stack — Thun 0.1.1 documentation</title>
|
||||
<title>joy.utils.stack — Thun 0.2.0 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>
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
var DOCUMENTATION_OPTIONS = {
|
||||
URL_ROOT: '',
|
||||
VERSION: '0.1.1',
|
||||
VERSION: '0.2.0',
|
||||
LANGUAGE: 'None',
|
||||
COLLAPSE_INDEX: false,
|
||||
FILE_SUFFIX: '.html',
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@
|
|||
<head>
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=Edge" />
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<title>Index — Thun 0.1.1 documentation</title>
|
||||
<title>Index — Thun 0.2.0 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>
|
||||
|
|
@ -99,10 +99,14 @@
|
|||
<li><a href="library.html#joy.library.choice">choice() (in module joy.library)</a>
|
||||
</li>
|
||||
<li><a href="library.html#joy.library.clear">clear() (in module joy.library)</a>
|
||||
</li>
|
||||
<li><a href="library.html#joy.library.cmp_">cmp_() (in module joy.library)</a>
|
||||
</li>
|
||||
</ul></td>
|
||||
<td style="width: 33%; vertical-align: top;"><ul>
|
||||
<li><a href="library.html#joy.library.concat">concat() (in module joy.library)</a>
|
||||
</li>
|
||||
<li><a href="library.html#joy.library.cond">cond() (in module joy.library)</a>
|
||||
</li>
|
||||
<li><a href="library.html#joy.library.cons">cons() (in module joy.library)</a>
|
||||
</li>
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
<head>
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=Edge" />
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<title>Thun 0.1.1 Documentation — Thun 0.1.1 documentation</title>
|
||||
<title>Thun 0.2.0 Documentation — Thun 0.2.0 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>
|
||||
|
|
@ -32,7 +32,7 @@
|
|||
<div class="body" role="main">
|
||||
|
||||
<div class="section" id="thun-release-documentation">
|
||||
<h1>Thun 0.1.1 Documentation<a class="headerlink" href="#thun-release-documentation" title="Permalink to this headline">¶</a></h1>
|
||||
<h1>Thun 0.2.0 Documentation<a class="headerlink" href="#thun-release-documentation" title="Permalink to this headline">¶</a></h1>
|
||||
<p>Thun is dialect of Joy written in Python 2.</p>
|
||||
<p><a class="reference external" href="https://en.wikipedia.org/wiki/Joy_(programming_language)">Joy</a> is a programming language created by Manfred von Thun that is easy to
|
||||
use and understand and has many other nice properties. This Python
|
||||
|
|
@ -133,7 +133,8 @@ interesting aspects. It’s quite a treasure trove.</p>
|
|||
<li class="toctree-l1"><a class="reference internal" href="notebooks/index.html">Essays about Programming in Joy</a><ul>
|
||||
<li class="toctree-l2"><a class="reference internal" href="notebooks/Developing.html">Developing a Program in Joy</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="notebooks/Replacing.html">Replacing Functions in the Dictionary</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="notebooks/Trees.html">Treating Trees</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="notebooks/Ordered_Binary_Trees.html">Treating Trees I: Ordered Binary Trees</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="notebooks/Treestep.html">Treating Trees II: <code class="docutils literal notranslate"><span class="pre">treestep</span></code></a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="notebooks/Newton-Raphson.html">Newton’s method</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="notebooks/Quadratic.html">Quadratic formula</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="notebooks/NoUpdates.html">No Updates</a></li>
|
||||
|
|
@ -161,7 +162,7 @@ interesting aspects. It’s quite a treasure trove.</p>
|
|||
<div class="sphinxsidebarwrapper">
|
||||
<h3><a href="#">Table Of Contents</a></h3>
|
||||
<ul>
|
||||
<li><a class="reference internal" href="#">Thun 0.1.1 Documentation</a><ul>
|
||||
<li><a class="reference internal" href="#">Thun 0.2.0 Documentation</a><ul>
|
||||
<li><a class="reference internal" href="#quick-start">Quick Start</a></li>
|
||||
<li><a class="reference internal" href="#project-hosted-on-osdn">Project Hosted on OSDN</a></li>
|
||||
<li><a class="reference internal" href="#information-on-the-joy-language">Information on the Joy language</a></li>
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
<head>
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=Edge" />
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<title>Joy Interpreter — Thun 0.1.1 documentation</title>
|
||||
<title>Joy Interpreter — Thun 0.2.0 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>
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
<head>
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=Edge" />
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<title>Functions Grouped by, er, Function with Examples — Thun 0.1.1 documentation</title>
|
||||
<title>Functions Grouped by, er, Function with Examples — Thun 0.2.0 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>
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
<head>
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=Edge" />
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<title>Function Reference — Thun 0.1.1 documentation</title>
|
||||
<title>Function Reference — Thun 0.2.0 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>
|
||||
|
|
@ -193,12 +193,48 @@ Boolean value (so empty string, zero, etc. are counted as false, etc.)</p>
|
|||
</div>
|
||||
</dd></dl>
|
||||
|
||||
<dl class="function">
|
||||
<dt id="joy.library.cmp_">
|
||||
<code class="descclassname">joy.library.</code><code class="descname">cmp_</code><span class="sig-paren">(</span><em>stack</em>, <em>expression</em>, <em>dictionary</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/joy/library.html#cmp_"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#joy.library.cmp_" title="Permalink to this definition">¶</a></dt>
|
||||
<dd><p>cmp takes two values and three quoted programs on the stack and runs
|
||||
one of the three depending on the results of comparing the two values:</p>
|
||||
<blockquote>
|
||||
<div><blockquote>
|
||||
<div>a b [G] [E] [L] cmp</div></blockquote>
|
||||
<dl class="docutils">
|
||||
<dt>————————- a > b</dt>
|
||||
<dd><blockquote class="first">
|
||||
<div>G</div></blockquote>
|
||||
<p class="last">a b [G] [E] [L] cmp</p>
|
||||
</dd>
|
||||
<dt>————————- a = b</dt>
|
||||
<dd><blockquote class="first">
|
||||
<div>E</div></blockquote>
|
||||
<p class="last">a b [G] [E] [L] cmp</p>
|
||||
</dd>
|
||||
<dt>————————- a < b</dt>
|
||||
<dd>L</dd>
|
||||
</dl>
|
||||
</div></blockquote>
|
||||
</dd></dl>
|
||||
|
||||
<dl class="function">
|
||||
<dt id="joy.library.concat">
|
||||
<code class="descclassname">joy.library.</code><code class="descname">concat</code><span class="sig-paren">(</span><em>stack</em>, <em>expression</em>, <em>dictionary</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/joy/library.html#concat"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#joy.library.concat" title="Permalink to this definition">¶</a></dt>
|
||||
<dd><p>Concatinate the two lists on the top of the stack.</p>
|
||||
</dd></dl>
|
||||
|
||||
<dl class="function">
|
||||
<dt id="joy.library.cond">
|
||||
<code class="descclassname">joy.library.</code><code class="descname">cond</code><span class="sig-paren">(</span><em>stack</em>, <em>expression</em>, <em>dictionary</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/joy/library.html#cond"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#joy.library.cond" title="Permalink to this definition">¶</a></dt>
|
||||
<dd><p>like a case statement; works by rewriting into a chain of ifte.</p>
|
||||
<p>[..[[Bi] Ti]..[D]] -> …</p>
|
||||
<blockquote>
|
||||
<div>[[[B0] T0] [[B1] T1] [D]] cond</div></blockquote>
|
||||
<blockquote>
|
||||
<div>[B0] [T0] [[B1] [T1] [D] ifte] ifte</div></blockquote>
|
||||
</dd></dl>
|
||||
|
||||
<dl class="function">
|
||||
<dt id="joy.library.cons">
|
||||
<code class="descclassname">joy.library.</code><code class="descname">cons</code><span class="sig-paren">(</span><em>stack</em>, <em>expression</em>, <em>dictionary</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/joy/library.html#cons"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#joy.library.cons" title="Permalink to this definition">¶</a></dt>
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
<head>
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=Edge" />
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<title>Advent of Code 2017 — Thun 0.1.1 documentation</title>
|
||||
<title>Advent of Code 2017 — Thun 0.2.0 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>
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
<head>
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=Edge" />
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<title>Advent of Code 2017 — Thun 0.1.1 documentation</title>
|
||||
<title>Advent of Code 2017 — Thun 0.2.0 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>
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
<head>
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=Edge" />
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<title>Advent of Code 2017 — Thun 0.1.1 documentation</title>
|
||||
<title>Advent of Code 2017 — Thun 0.2.0 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>
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
<head>
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=Edge" />
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<title>Advent of Code 2017 — Thun 0.1.1 documentation</title>
|
||||
<title>Advent of Code 2017 — Thun 0.2.0 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>
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
<head>
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=Edge" />
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<title>Advent of Code 2017 — Thun 0.1.1 documentation</title>
|
||||
<title>Advent of Code 2017 — Thun 0.2.0 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>
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
<head>
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=Edge" />
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<title>Advent of Code 2017 — Thun 0.1.1 documentation</title>
|
||||
<title>Advent of Code 2017 — Thun 0.2.0 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>
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
<head>
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=Edge" />
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<title>Categorical Programming — Thun 0.1.1 documentation</title>
|
||||
<title>Categorical Programming — Thun 0.2.0 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>
|
||||
|
|
@ -39,7 +39,7 @@
|
|||
<p>In <a class="reference external" href="http://conal.net/papers/compiling-to-categories/">Compiling to categories</a> Conal Elliott give a taste of what this might mean.</p>
|
||||
<blockquote>
|
||||
<div>It is well-known that the simply typed lambda-calculus is modeled by any cartesian closed category (CCC). This correspondence suggests giving typed functional programs a variety of interpretations, each corresponding to a different category. A convenient way to realize this idea is as a collection of meaning-preserving transformations added to an existing compiler, such as GHC for Haskell. This paper describes such an implementation and demonstrates its use for a variety of interpretations including hardware circuits, automatic differentiation, incremental computation, and interval analysis. Each such interpretation is a category easily defined in Haskell (outside of the compiler). The general technique appears to provide a compelling alternative to deeply embedded domain-specific languages.</div></blockquote>
|
||||
<p>What he’s doing is translating labda forms into a kind of “point-free” style that is very close to Joy code (although more verbose) and then showing how to instantiate that code over different categories to get several different kinds of program out of the same code.</p>
|
||||
<p>What he’s doing is translating lambda forms into a kind of “point-free” style that is very close to Joy code (although more verbose) and then showing how to instantiate that code over different categories to get several different kinds of program out of the same code.</p>
|
||||
</div>
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
<head>
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=Edge" />
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<title>Developing a Program in Joy — Thun 0.1.1 documentation</title>
|
||||
<title>Developing a Program in Joy — Thun 0.2.0 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>
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
<head>
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=Edge" />
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<title>Using x to Generate Values — Thun 0.1.1 documentation</title>
|
||||
<title>Using x to Generate Values — Thun 0.2.0 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>
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
<head>
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=Edge" />
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<title>Hylomorphism — Thun 0.1.1 documentation</title>
|
||||
<title>Hylomorphism — Thun 0.2.0 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>
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
<head>
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=Edge" />
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<title>Thun: Joy in Python — Thun 0.1.1 documentation</title>
|
||||
<title>Thun: Joy in Python — Thun 0.2.0 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>
|
||||
|
|
@ -17,7 +17,7 @@
|
|||
<link rel="index" title="Index" href="../genindex.html" />
|
||||
<link rel="search" title="Search" href="../search.html" />
|
||||
<link rel="next" title="Joy Interpreter" href="../joy.html" />
|
||||
<link rel="prev" title="Thun 0.1.1 Documentation" href="../index.html" />
|
||||
<link rel="prev" title="Thun 0.2.0 Documentation" href="../index.html" />
|
||||
|
||||
<link rel="stylesheet" href="../_static/custom.css" type="text/css" />
|
||||
|
||||
|
|
@ -326,7 +326,7 @@ developing structured processes.</p>
|
|||
<h3>Related Topics</h3>
|
||||
<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="../index.html" title="previous chapter">Thun 0.2.0 Documentation</a></li>
|
||||
<li>Next: <a href="../joy.html" title="next chapter">Joy Interpreter</a></li>
|
||||
</ul></li>
|
||||
</ul>
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
<head>
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=Edge" />
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<title>Newton’s method — Thun 0.1.1 documentation</title>
|
||||
<title>Newton’s method — Thun 0.2.0 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>
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
<head>
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=Edge" />
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<title>No Updates — Thun 0.1.1 documentation</title>
|
||||
<title>No Updates — Thun 0.2.0 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>
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
<head>
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=Edge" />
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<title>Quadratic formula — Thun 0.1.1 documentation</title>
|
||||
<title>Quadratic formula — Thun 0.2.0 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>
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
<head>
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=Edge" />
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<title>Treating Trees — Thun 0.1.1 documentation</title>
|
||||
<title>Treating Trees — Thun 0.2.0 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>
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
<head>
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=Edge" />
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<title>Preamble — Thun 0.1.1 documentation</title>
|
||||
<title>Preamble — Thun 0.2.0 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>
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
<head>
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=Edge" />
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<title>Essays about Programming in Joy — Thun 0.1.1 documentation</title>
|
||||
<title>Essays about Programming in Joy — Thun 0.2.0 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>
|
||||
|
|
@ -50,18 +50,27 @@
|
|||
<li class="toctree-l2"><a class="reference internal" href="Replacing.html#a-shorter-trace">A shorter trace</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
<li class="toctree-l1"><a class="reference internal" href="Trees.html">Treating Trees</a><ul>
|
||||
<li class="toctree-l2"><a class="reference internal" href="Trees.html#a-function-to-traverse-a-btree">A Function to Traverse a BTree</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="Trees.html#adding-nodes-to-the-btree">Adding Nodes to the BTree</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="Trees.html#interlude-the-cmp-combinator">Interlude: The <code class="docutils literal notranslate"><span class="pre">cmp</span></code> combinator</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="Trees.html#interlude-factoring-and-naming">Interlude: Factoring and Naming</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="Trees.html#a-version-of-btree-iter-that-does-in-order-traversal">A Version of <code class="docutils literal notranslate"><span class="pre">BTree-iter</span></code> that does In-Order Traversal</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="Trees.html#getting-values-by-key">Getting values by key</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="Trees.html#todo-btree-delete">TODO: BTree-delete</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="Trees.html#tree-with-node-and-list-of-trees">Tree with node and list of trees.</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="Trees.html#miscellaneous-crap">Miscellaneous Crap</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="Trees.html#a-general-form-for-trees">A General Form for Trees</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="Trees.html#automatically-deriving-the-recursion-combinator-for-a-data-type">Automatically deriving the recursion combinator for a data type?</a></li>
|
||||
<li class="toctree-l1"><a class="reference internal" href="Ordered_Binary_Trees.html">Treating Trees I: Ordered Binary Trees</a><ul>
|
||||
<li class="toctree-l2"><a class="reference internal" href="Ordered_Binary_Trees.html#adding-nodes-to-the-tree">Adding Nodes to the Tree</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="Ordered_Binary_Trees.html#interlude-cmp-combinator">Interlude: <code class="docutils literal notranslate"><span class="pre">cmp</span></code> combinator</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="Ordered_Binary_Trees.html#a-function-to-traverse-this-structure">A Function to Traverse this Structure</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="Ordered_Binary_Trees.html#interlude-a-set-like-datastructure">Interlude: A Set-like Datastructure</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="Ordered_Binary_Trees.html#a-version-of-tree-iter-that-does-in-order-traversal">A Version of <code class="docutils literal notranslate"><span class="pre">Tree-iter</span></code> that does In-Order Traversal</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="Ordered_Binary_Trees.html#getting-values-by-key">Getting values by key</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="Ordered_Binary_Trees.html#tree-delete">Tree-delete</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="Ordered_Binary_Trees.html#appendix-the-source-code">Appendix: The source code.</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
<li class="toctree-l1"><a class="reference internal" href="Treestep.html">Treating Trees II: <code class="docutils literal notranslate"><span class="pre">treestep</span></code></a><ul>
|
||||
<li class="toctree-l2"><a class="reference internal" href="Treestep.html#derive-the-recursive-function">Derive the recursive function.</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="Treestep.html#extract-the-givens-to-parameterize-the-program">Extract the givens to parameterize the program.</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="Treestep.html#define-treestep">Define <code class="docutils literal notranslate"><span class="pre">treestep</span></code></a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="Treestep.html#examples">Examples</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="Treestep.html#redefining-the-ordered-binary-tree-in-terms-of-treestep">Redefining the Ordered Binary Tree in terms of <code class="docutils literal notranslate"><span class="pre">treestep</span></code>.</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="Treestep.html#with-treegrind">With <code class="docutils literal notranslate"><span class="pre">treegrind</span></code>?</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="Treestep.html#treegrind-with-step"><code class="docutils literal notranslate"><span class="pre">treegrind</span></code> with <code class="docutils literal notranslate"><span class="pre">step</span></code></a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="Treestep.html#do-we-have-the-flexibility-to-reimplement-tree-get">Do we have the flexibility to reimplement <code class="docutils literal notranslate"><span class="pre">Tree-get</span></code>?</a></li>
|
||||
<li class="toctree-l2"><a class="reference internal" href="Treestep.html#putting-it-together">Putting it together</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
<li class="toctree-l1"><a class="reference internal" href="Newton-Raphson.html">Newton’s method</a><ul>
|
||||
|
|
|
|||
Binary file not shown.
|
|
@ -6,7 +6,7 @@
|
|||
<head>
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=Edge" />
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<title>Parsing Text into Joy Expressions — Thun 0.1.1 documentation</title>
|
||||
<title>Parsing Text into Joy Expressions — Thun 0.2.0 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>
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
<head>
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=Edge" />
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<title>Tracing Joy Execution — Thun 0.1.1 documentation</title>
|
||||
<title>Tracing Joy Execution — Thun 0.2.0 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>
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
<head>
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=Edge" />
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<title>Python Module Index — Thun 0.1.1 documentation</title>
|
||||
<title>Python Module Index — Thun 0.2.0 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>
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
<head>
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=Edge" />
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<title>Search — Thun 0.1.1 documentation</title>
|
||||
<title>Search — Thun 0.2.0 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>
|
||||
|
|
|
|||
File diff suppressed because one or more lines are too long
|
|
@ -6,7 +6,7 @@
|
|||
<head>
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=Edge" />
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<title>Stack or Quote or Sequence or List… — Thun 0.1.1 documentation</title>
|
||||
<title>Stack or Quote or Sequence or List… — Thun 0.2.0 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>
|
||||
|
|
|
|||
|
|
@ -24,9 +24,9 @@ copyright = u'2018, Simon Forman'
|
|||
author = u'Simon Forman'
|
||||
|
||||
# The short X.Y version
|
||||
version = u'0.1'
|
||||
version = u'0.2'
|
||||
# The full version, including alpha/beta/rc tags
|
||||
release = u'0.1.1'
|
||||
release = u'0.2.0'
|
||||
|
||||
|
||||
# -- General configuration ---------------------------------------------------
|
||||
|
|
|
|||
|
|
@ -13,5 +13,5 @@ In `Compiling to categories <http://conal.net/papers/compiling-to-categories/>`_
|
|||
|
||||
It is well-known that the simply typed lambda-calculus is modeled by any cartesian closed category (CCC). This correspondence suggests giving typed functional programs a variety of interpretations, each corresponding to a different category. A convenient way to realize this idea is as a collection of meaning-preserving transformations added to an existing compiler, such as GHC for Haskell. This paper describes such an implementation and demonstrates its use for a variety of interpretations including hardware circuits, automatic differentiation, incremental computation, and interval analysis. Each such interpretation is a category easily defined in Haskell (outside of the compiler). The general technique appears to provide a compelling alternative to deeply embedded domain-specific languages.
|
||||
|
||||
What he's doing is translating labda forms into a kind of "point-free" style that is very close to Joy code (although more verbose) and then showing how to instantiate that code over different categories to get several different kinds of program out of the same code.
|
||||
What he's doing is translating lambda forms into a kind of "point-free" style that is very close to Joy code (although more verbose) and then showing how to instantiate that code over different categories to get several different kinds of program out of the same code.
|
||||
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load Diff
|
|
@ -0,0 +1,621 @@
|
|||
|
||||
Treating Trees II: ``treestep``
|
||||
===============================
|
||||
|
||||
Let's consider a tree structure, similar to one described `"Why
|
||||
functional programming matters" by John
|
||||
Hughes <https://www.cs.kent.ac.uk/people/staff/dat/miranda/whyfp90.pdf>`__,
|
||||
that consists of a node value followed by zero or more child trees. (The
|
||||
asterisk is meant to indicate the `Kleene
|
||||
star <https://en.wikipedia.org/wiki/Kleene_star>`__.)
|
||||
|
||||
::
|
||||
|
||||
tree = [] | [node tree*]
|
||||
|
||||
In the spirit of ``step`` we are going to define a combinator
|
||||
``treestep`` which expects a tree and three additional items: a
|
||||
base-case function ``[B]``, and two quoted programs ``[N]`` and ``[C]``.
|
||||
|
||||
::
|
||||
|
||||
tree [B] [N] [C] treestep
|
||||
|
||||
If the current tree node is empty then just execute ``B``:
|
||||
|
||||
::
|
||||
|
||||
[] [B] [N] [C] treestep
|
||||
---------------------------
|
||||
[] B
|
||||
|
||||
Otherwise, evaluate ``N`` on the node value, ``map`` the whole function
|
||||
(abbreviated here as ``K``) over the child trees recursively, and then
|
||||
combine the result with ``C``.
|
||||
|
||||
::
|
||||
|
||||
[node tree*] [B] [N] [C] treestep
|
||||
--------------------------------------- w/ K == [B] [N] [C] treestep
|
||||
node N [tree*] [K] map C
|
||||
|
||||
(Later on we'll experiment with making ``map`` part of ``C`` so you can
|
||||
use other combinators.)
|
||||
|
||||
Derive the recursive function.
|
||||
------------------------------
|
||||
|
||||
We can begin to derive it by finding the ``ifte`` stage that ``genrec``
|
||||
will produce.
|
||||
|
||||
::
|
||||
|
||||
K == [not] [B] [R0] [R1] genrec
|
||||
== [not] [B] [R0 [K] R1] ifte
|
||||
|
||||
So we just have to derive ``J``:
|
||||
|
||||
::
|
||||
|
||||
J == R0 [K] R1
|
||||
|
||||
The behavior of ``J`` is to accept a (non-empty) tree node and arrive at
|
||||
the desired outcome.
|
||||
|
||||
::
|
||||
|
||||
[node tree*] J
|
||||
------------------------------
|
||||
node N [tree*] [K] map C
|
||||
|
||||
So ``J`` will have some form like:
|
||||
|
||||
::
|
||||
|
||||
J == ... [N] ... [K] ... [C] ...
|
||||
|
||||
Let's dive in. First, unquote the node and ``dip`` ``N``.
|
||||
|
||||
::
|
||||
|
||||
[node tree*] uncons [N] dip
|
||||
node [tree*] [N] dip
|
||||
node N [tree*]
|
||||
|
||||
Next, ``map`` ``K`` over the child trees and combine with ``C``.
|
||||
|
||||
::
|
||||
|
||||
node N [tree*] [K] map C
|
||||
node N [tree*] [K] map C
|
||||
node N [K.tree*] C
|
||||
|
||||
So:
|
||||
|
||||
::
|
||||
|
||||
J == uncons [N] dip [K] map C
|
||||
|
||||
Plug it in and convert to ``genrec``:
|
||||
|
||||
::
|
||||
|
||||
K == [not] [B] [J ] ifte
|
||||
== [not] [B] [uncons [N] dip [K] map C] ifte
|
||||
== [not] [B] [uncons [N] dip] [map C] genrec
|
||||
|
||||
Extract the givens to parameterize the program.
|
||||
-----------------------------------------------
|
||||
|
||||
Working backwards:
|
||||
|
||||
::
|
||||
|
||||
[not] [B] [uncons [N] dip] [map C] genrec
|
||||
[B] [not] swap [uncons [N] dip] [map C] genrec
|
||||
[B] [uncons [N] dip] [[not] swap] dip [map C] genrec
|
||||
^^^^^^^^^^^^^^^^
|
||||
[B] [[N] dip] [uncons] swoncat [[not] swap] dip [map C] genrec
|
||||
[B] [N] [dip] cons [uncons] swoncat [[not] swap] dip [map C] genrec
|
||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
Extract a couple of auxiliary definitions:
|
||||
|
||||
::
|
||||
|
||||
TS.0 == [[not] swap] dip
|
||||
TS.1 == [dip] cons [uncons] swoncat
|
||||
|
||||
::
|
||||
|
||||
[B] [N] TS.1 TS.0 [map C] genrec
|
||||
[B] [N] [map C] [TS.1 TS.0] dip genrec
|
||||
[B] [N] [C] [map] swoncat [TS.1 TS.0] dip genrec
|
||||
|
||||
The givens are all to the left so we have our definition.
|
||||
|
||||
(alternate) Extract the givens to parameterize the program.
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
Working backwards:
|
||||
|
||||
::
|
||||
|
||||
[not] [B] [uncons [N] dip] [map C] genrec
|
||||
[not] [B] [N] [dip] cons [uncons] swoncat [map C] genrec
|
||||
[B] [N] [not] roll> [dip] cons [uncons] swoncat [map C] genrec
|
||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
Define ``treestep``
|
||||
-------------------
|
||||
|
||||
.. code:: ipython2
|
||||
|
||||
from notebook_preamble import D, J, V, define, DefinitionWrapper
|
||||
|
||||
.. code:: ipython2
|
||||
|
||||
DefinitionWrapper.add_definitions('''
|
||||
|
||||
_treestep_0 == [[not] swap] dip
|
||||
_treestep_1 == [dip] cons [uncons] swoncat
|
||||
treegrind == [_treestep_1 _treestep_0] dip genrec
|
||||
treestep == [map] swoncat treegrind
|
||||
|
||||
''', D)
|
||||
|
||||
Examples
|
||||
--------
|
||||
|
||||
Consider trees, the nodes of which are integers. We can find the sum of
|
||||
all nodes in a tree with this function:
|
||||
|
||||
::
|
||||
|
||||
sumtree == [pop 0] [] [sum +] treestep
|
||||
|
||||
.. code:: ipython2
|
||||
|
||||
define('sumtree == [pop 0] [] [sum +] treestep')
|
||||
|
||||
Running this function on an empty tree value gives zero:
|
||||
|
||||
::
|
||||
|
||||
[] [pop 0] [] [sum +] treestep
|
||||
------------------------------------
|
||||
0
|
||||
|
||||
.. code:: ipython2
|
||||
|
||||
J('[] sumtree') # Empty tree.
|
||||
|
||||
|
||||
.. parsed-literal::
|
||||
|
||||
0
|
||||
|
||||
|
||||
Running it on a non-empty node:
|
||||
|
||||
::
|
||||
|
||||
[n tree*] [pop 0] [] [sum +] treestep
|
||||
n [tree*] [[pop 0] [] [sum +] treestep] map sum +
|
||||
n [ ... ] sum +
|
||||
n m +
|
||||
n+m
|
||||
|
||||
.. code:: ipython2
|
||||
|
||||
J('[23] sumtree') # No child trees.
|
||||
|
||||
|
||||
.. parsed-literal::
|
||||
|
||||
23
|
||||
|
||||
|
||||
.. code:: ipython2
|
||||
|
||||
J('[23 []] sumtree') # Child tree, empty.
|
||||
|
||||
|
||||
.. parsed-literal::
|
||||
|
||||
23
|
||||
|
||||
|
||||
.. code:: ipython2
|
||||
|
||||
J('[23 [2 [4]] [3]] sumtree') # Non-empty child trees.
|
||||
|
||||
|
||||
.. parsed-literal::
|
||||
|
||||
32
|
||||
|
||||
|
||||
.. code:: ipython2
|
||||
|
||||
J('[23 [2 [8] [9]] [3] [4 []]] sumtree') # Etc...
|
||||
|
||||
|
||||
.. parsed-literal::
|
||||
|
||||
49
|
||||
|
||||
|
||||
.. code:: ipython2
|
||||
|
||||
J('[23 [2 [8] [9]] [3] [4 []]] [pop 0] [] [cons sum] treestep') # Alternate "spelling".
|
||||
|
||||
|
||||
.. parsed-literal::
|
||||
|
||||
49
|
||||
|
||||
|
||||
.. code:: ipython2
|
||||
|
||||
J('[23 [2 [8] [9]] [3] [4 []]] [] [pop 23] [cons] treestep') # Replace each node.
|
||||
|
||||
|
||||
.. parsed-literal::
|
||||
|
||||
[23 [23 [23] [23]] [23] [23 []]]
|
||||
|
||||
|
||||
.. code:: ipython2
|
||||
|
||||
J('[23 [2 [8] [9]] [3] [4 []]] [] [pop 1] [cons] treestep')
|
||||
|
||||
|
||||
.. parsed-literal::
|
||||
|
||||
[1 [1 [1] [1]] [1] [1 []]]
|
||||
|
||||
|
||||
.. code:: ipython2
|
||||
|
||||
J('[23 [2 [8] [9]] [3] [4 []]] [] [pop 1] [cons] treestep sumtree')
|
||||
|
||||
|
||||
.. parsed-literal::
|
||||
|
||||
6
|
||||
|
||||
|
||||
.. code:: ipython2
|
||||
|
||||
J('[23 [2 [8] [9]] [3] [4 []]] [pop 0] [pop 1] [sum +] treestep') # Combine replace and sum into one function.
|
||||
|
||||
|
||||
.. parsed-literal::
|
||||
|
||||
6
|
||||
|
||||
|
||||
.. code:: ipython2
|
||||
|
||||
J('[4 [3 [] [7]]] [pop 0] [pop 1] [sum +] treestep') # Combine replace and sum into one function.
|
||||
|
||||
|
||||
.. parsed-literal::
|
||||
|
||||
3
|
||||
|
||||
|
||||
Redefining the Ordered Binary Tree in terms of ``treestep``.
|
||||
------------------------------------------------------------
|
||||
|
||||
::
|
||||
|
||||
Tree = [] | [[key value] left right]
|
||||
|
||||
What kind of functions can we write for this with our ``treestep``?
|
||||
|
||||
The pattern for processing a non-empty node is:
|
||||
|
||||
::
|
||||
|
||||
node N [tree*] [K] map C
|
||||
|
||||
Plugging in our BTree structure:
|
||||
|
||||
::
|
||||
|
||||
[key value] N [left right] [K] map C
|
||||
|
||||
Traversal
|
||||
~~~~~~~~~
|
||||
|
||||
::
|
||||
|
||||
[key value] first [left right] [K] map i
|
||||
key [value] [left right] [K] map i
|
||||
key [left right] [K] map i
|
||||
key [lkey rkey ] i
|
||||
key lkey rkey
|
||||
|
||||
This doesn't quite work:
|
||||
|
||||
.. code:: ipython2
|
||||
|
||||
J('[[3 0] [[2 0] [][]] [[9 0] [[5 0] [[4 0] [][]] [[8 0] [[6 0] [] [[7 0] [][]]][]]][]]] ["B"] [first] [i] treestep')
|
||||
|
||||
|
||||
.. parsed-literal::
|
||||
|
||||
3 'B' 'B'
|
||||
|
||||
|
||||
Doesn't work because ``map`` extracts the ``first`` item of whatever its
|
||||
mapped function produces. We have to return a list, rather than
|
||||
depositing our results directly on the stack.
|
||||
|
||||
::
|
||||
|
||||
[key value] N [left right] [K] map C
|
||||
|
||||
[key value] first [left right] [K] map flatten cons
|
||||
key [left right] [K] map flatten cons
|
||||
key [[lk] [rk] ] flatten cons
|
||||
key [ lk rk ] cons
|
||||
[key lk rk ]
|
||||
|
||||
So:
|
||||
|
||||
::
|
||||
|
||||
[] [first] [flatten cons] treestep
|
||||
|
||||
.. code:: ipython2
|
||||
|
||||
J('[[3 0] [[2 0] [] []] [[9 0] [[5 0] [[4 0] [] []] [[8 0] [[6 0] [] [[7 0] [] []]] []]] []]] [] [first] [flatten cons] treestep')
|
||||
|
||||
|
||||
.. parsed-literal::
|
||||
|
||||
[3 2 9 5 4 8 6 7]
|
||||
|
||||
|
||||
There we go.
|
||||
|
||||
In-order traversal
|
||||
~~~~~~~~~~~~~~~~~~
|
||||
|
||||
From here:
|
||||
|
||||
::
|
||||
|
||||
key [[lk] [rk]] C
|
||||
key [[lk] [rk]] i
|
||||
key [lk] [rk] roll<
|
||||
[lk] [rk] key swons concat
|
||||
[lk] [key rk] concat
|
||||
[lk key rk]
|
||||
|
||||
So:
|
||||
|
||||
::
|
||||
|
||||
[] [i roll< swons concat] [first] treestep
|
||||
|
||||
.. code:: ipython2
|
||||
|
||||
J('[[3 0] [[2 0] [] []] [[9 0] [[5 0] [[4 0] [] []] [[8 0] [[6 0] [] [[7 0] [] []]] []]] []]] [] [uncons pop] [i roll< swons concat] treestep')
|
||||
|
||||
|
||||
.. parsed-literal::
|
||||
|
||||
[2 3 4 5 6 7 8 9]
|
||||
|
||||
|
||||
With ``treegrind``?
|
||||
-------------------
|
||||
|
||||
The ``treegrind`` function doesn't include the ``map`` combinator, so
|
||||
the ``[C]`` function must arrange to use some combinator on the quoted
|
||||
recursive copy ``[K]``. With this function, the pattern for processing a
|
||||
non-empty node is:
|
||||
|
||||
::
|
||||
|
||||
node N [tree*] [K] C
|
||||
|
||||
Plugging in our BTree structure:
|
||||
|
||||
::
|
||||
|
||||
[key value] N [left right] [K] C
|
||||
|
||||
.. code:: ipython2
|
||||
|
||||
J('[["key" "value"] ["left"] ["right"] ] ["B"] ["N"] ["C"] treegrind')
|
||||
|
||||
|
||||
.. parsed-literal::
|
||||
|
||||
['key' 'value'] 'N' [['left'] ['right']] [[not] ['B'] [uncons ['N'] dip] ['C'] genrec] 'C'
|
||||
|
||||
|
||||
``treegrind`` with ``step``
|
||||
---------------------------
|
||||
|
||||
Iteration through the nodes
|
||||
|
||||
.. code:: ipython2
|
||||
|
||||
J('[[3 0] [[2 0] [] []] [[9 0] [[5 0] [[4 0] [] []] [[8 0] [[6 0] [] [[7 0] [] []]] []]] []]] [pop] ["N"] [step] treegrind')
|
||||
|
||||
|
||||
.. parsed-literal::
|
||||
|
||||
[3 0] 'N' [2 0] 'N' [9 0] 'N' [5 0] 'N' [4 0] 'N' [8 0] 'N' [6 0] 'N' [7 0] 'N'
|
||||
|
||||
|
||||
Sum the nodes' keys.
|
||||
|
||||
.. code:: ipython2
|
||||
|
||||
J('0 [[3 0] [[2 0] [] []] [[9 0] [[5 0] [[4 0] [] []] [[8 0] [[6 0] [] [[7 0] [] []]] []]] []]] [pop] [first +] [step] treegrind')
|
||||
|
||||
|
||||
.. parsed-literal::
|
||||
|
||||
44
|
||||
|
||||
|
||||
Rebuild the tree using ``map`` (imitating ``treestep``.)
|
||||
|
||||
.. code:: ipython2
|
||||
|
||||
J('[[3 0] [[2 0] [] []] [[9 0] [[5 0] [[4 0] [] []] [[8 0] [[6 0] [] [[7 0] [] []]] []]] []]] [] [[100 +] infra] [map cons] treegrind')
|
||||
|
||||
|
||||
.. parsed-literal::
|
||||
|
||||
[[103 0] [[102 0] [] []] [[109 0] [[105 0] [[104 0] [] []] [[108 0] [[106 0] [] [[107 0] [] []]] []]] []]]
|
||||
|
||||
|
||||
Do we have the flexibility to reimplement ``Tree-get``?
|
||||
-------------------------------------------------------
|
||||
|
||||
I think we do:
|
||||
|
||||
::
|
||||
|
||||
[B] [N] [C] treegrind
|
||||
|
||||
We'll start by saying that the base-case (the key is not in the tree) is
|
||||
user defined, and the per-node function is just the query key literal:
|
||||
|
||||
::
|
||||
|
||||
[B] [query_key] [C] treegrind
|
||||
|
||||
This means we just have to define ``C`` from:
|
||||
|
||||
::
|
||||
|
||||
[key value] query_key [left right] [K] C
|
||||
|
||||
Let's try ``cmp``:
|
||||
|
||||
::
|
||||
|
||||
C == P [T>] [E] [T<] cmp
|
||||
|
||||
[key value] query_key [left right] [K] P [T>] [E] [T<] cmp
|
||||
|
||||
The predicate ``P``
|
||||
~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
Seems pretty easy (we must preserve the value in case the keys are
|
||||
equal):
|
||||
|
||||
::
|
||||
|
||||
[key value] query_key [left right] [K] P
|
||||
[key value] query_key [left right] [K] roll<
|
||||
[key value] [left right] [K] query_key [roll< uncons swap] dip
|
||||
|
||||
[key value] [left right] [K] roll< uncons swap query_key
|
||||
[left right] [K] [key value] uncons swap query_key
|
||||
[left right] [K] key [value] swap query_key
|
||||
[left right] [K] [value] key query_key
|
||||
|
||||
P == roll< [roll< uncons swap] dip
|
||||
|
||||
(Possibly with a swap at the end? Or just swap ``T<`` and ``T>``.)
|
||||
|
||||
So now:
|
||||
|
||||
::
|
||||
|
||||
[left right] [K] [value] key query_key [T>] [E] [T<] cmp
|
||||
|
||||
Becomes one of these three:
|
||||
|
||||
::
|
||||
|
||||
[left right] [K] [value] T>
|
||||
[left right] [K] [value] E
|
||||
[left right] [K] [value] T<
|
||||
|
||||
``E``
|
||||
~~~~~
|
||||
|
||||
Easy.
|
||||
|
||||
::
|
||||
|
||||
E == roll> popop first
|
||||
|
||||
``T<`` and ``T>``
|
||||
~~~~~~~~~~~~~~~~~
|
||||
|
||||
::
|
||||
|
||||
T< == pop [first] dip i
|
||||
T> == pop [second] dip i
|
||||
|
||||
Putting it together
|
||||
-------------------
|
||||
|
||||
::
|
||||
|
||||
T> == pop [first] dip i
|
||||
T< == pop [second] dip i
|
||||
E == roll> popop first
|
||||
P == roll< [roll< uncons swap] dip
|
||||
|
||||
Tree-get == [P [T>] [E] [T<] cmp] treegrind
|
||||
|
||||
To me, that seems simpler than the ``genrec`` version.
|
||||
|
||||
.. code:: ipython2
|
||||
|
||||
DefinitionWrapper.add_definitions('''
|
||||
|
||||
T> == pop [first] dip i
|
||||
T< == pop [second] dip i
|
||||
E == roll> popop first
|
||||
P == roll< [roll< uncons swap] dip
|
||||
|
||||
Tree-get == [P [T>] [E] [T<] cmp] treegrind
|
||||
|
||||
''', D)
|
||||
|
||||
.. code:: ipython2
|
||||
|
||||
J('''\
|
||||
|
||||
[[3 13] [[2 12] [] []] [[9 19] [[5 15] [[4 14] [] []] [[8 18] [[6 16] [] [[7 17] [] []]] []]] []]]
|
||||
|
||||
[] [5] Tree-get
|
||||
|
||||
''')
|
||||
|
||||
|
||||
.. parsed-literal::
|
||||
|
||||
15
|
||||
|
||||
|
||||
.. code:: ipython2
|
||||
|
||||
J('''\
|
||||
|
||||
[[3 13] [[2 12] [] []] [[9 19] [[5 15] [[4 14] [] []] [[8 18] [[6 16] [] [[7 17] [] []]] []]] []]]
|
||||
|
||||
[pop "nope"] [25] Tree-get
|
||||
|
||||
''')
|
||||
|
||||
|
||||
.. parsed-literal::
|
||||
|
||||
'nope'
|
||||
|
||||
|
|
@ -10,7 +10,8 @@ These essays are adapted from Jupyter notebooks. I hope to have those hosted so
|
|||
|
||||
Developing
|
||||
Replacing
|
||||
Trees
|
||||
Ordered_Binary_Trees
|
||||
Treestep
|
||||
Newton-Raphson
|
||||
Quadratic
|
||||
NoUpdates
|
||||
|
|
|
|||
Loading…
Reference in New Issue