Finished up Generator_Programs.md
This commit is contained in:
parent
8dfcbca2a8
commit
9998e7947b
|
|
@ -15,12 +15,15 @@
|
||||||
</code></pre>
|
</code></pre>
|
||||||
<p>We can apply it to a quoted program consisting of some value <code>a</code> and some function <code>B</code>:</p>
|
<p>We can apply it to a quoted program consisting of some value <code>a</code> and some function <code>B</code>:</p>
|
||||||
<pre><code>[a B] x
|
<pre><code>[a B] x
|
||||||
|
[a B] dup i
|
||||||
|
[a B] [a B] i
|
||||||
[a B] a B
|
[a B] a B
|
||||||
</code></pre>
|
</code></pre>
|
||||||
<p>Let <code>B</code> function <code>swap</code> the <code>a</code> with the quote and run some function <code>C</code> on it to generate a new value <code>b</code>:</p>
|
<p>Let <code>B</code> function <code>swap</code> the <code>a</code> with the quote and run some function <code>C</code> on it to generate a new value <code>b</code>:</p>
|
||||||
<pre><code>B == swap [C] dip
|
<pre><code>B == swap [C] dip
|
||||||
|
</code></pre>
|
||||||
[a B] a B
|
<p>Evaluation would go like this:</p>
|
||||||
|
<pre><code>[a B] a B
|
||||||
[a B] a swap [C] dip
|
[a B] a swap [C] dip
|
||||||
a [a B] [C] dip
|
a [a B] [C] dip
|
||||||
a C [a B]
|
a C [a B]
|
||||||
|
|
@ -345,45 +348,59 @@ otherwise we discard the term and the generator leaving the sum on the stack:</p
|
||||||
<pre><code>joy? [pop >4M] [popop] [[PE2.1] dip third-term] tailrec
|
<pre><code>joy? [pop >4M] [popop] [[PE2.1] dip third-term] tailrec
|
||||||
4613732
|
4613732
|
||||||
</code></pre>
|
</code></pre>
|
||||||
<h2>Math</h2>
|
<h2>Let's Use Math</h2>
|
||||||
|
<p>Consider the Fib seq with algebraic variables:</p>
|
||||||
<pre><code>a b
|
<pre><code>a b
|
||||||
b a+b
|
b a+b
|
||||||
a+b a+b+b
|
a+b a+b+b
|
||||||
a+b+b a+a+b+b+b
|
a+b+b a+a+b+b+b
|
||||||
</code></pre>
|
</code></pre>
|
||||||
<p>So if (a,b) and a is even then the next even term pair is (a+2b, 2a+3b)</p>
|
<p>So starting with <code>(a b)</code> and assuming <code>a</code> is even then the next even term pair is <code>(a+2b, 2a+3b)</code>.</p>
|
||||||
<p>Reconsider:</p>
|
<p>Reconsider:</p>
|
||||||
<pre><code>[b a F] x
|
<pre><code>[b a F] x
|
||||||
[b a F] b a F
|
[b a F] b a F
|
||||||
</code></pre>
|
</code></pre>
|
||||||
<p>From here we want to arrive at:</p>
|
<p>From here we want to arrive at:</p>
|
||||||
<pre><code>(a+2b) [(2a+3b) (a+2b) F]
|
<pre><code>(a+2b) [(2a+3b) (a+2b) F]
|
||||||
|
|
||||||
b a F
|
|
||||||
b a [F0] [F1] fork
|
|
||||||
|
|
||||||
b a over [+] ii
|
|
||||||
---------------------
|
|
||||||
a+2b
|
|
||||||
</code></pre>
|
</code></pre>
|
||||||
<p>And:</p>
|
<p>Let's derive <code>F</code>. We have two values and we want two new values so that's a <code>clop</code>:</p>
|
||||||
<pre><code> b a over [dup + +] ii
|
<pre><code>b a F
|
||||||
---------------------------
|
b a [F0] [F1] clop
|
||||||
2a+3b
|
</code></pre>
|
||||||
|
<p>Where <code>F0</code> computes <code>a+2b</code>:</p>
|
||||||
|
<pre><code>F0 == over [+] ii
|
||||||
|
|
||||||
|
b a over [+] ii
|
||||||
|
b a b [+] ii
|
||||||
|
b a + b +
|
||||||
|
b+a b +
|
||||||
|
a+2b
|
||||||
|
</code></pre>
|
||||||
|
<p>And <code>F1</code> computes <code>2a+3b</code>:</p>
|
||||||
|
<pre><code>F1 == over [dup + +] ii
|
||||||
|
|
||||||
[over [dup + +] ii] [over [+] ii] clop
|
b a over [dup + +] ii
|
||||||
roll< rrest [tuck] dip ccons
|
b a b [dup + +] ii
|
||||||
|
b a dup + + b dup + +
|
||||||
|
b a a + + b b + +
|
||||||
|
...
|
||||||
|
2a+3b
|
||||||
|
</code></pre>
|
||||||
|
<p>So after that we have</p>
|
||||||
|
<pre><code>[b a F] (a+2b) (2a+3b) F'
|
||||||
|
|
||||||
|
[b a F] b‴ a‴ roll<
|
||||||
[b a F] b a F
|
b‴ a‴ [b a F] rrest
|
||||||
|
b‴ a‴ [F] [tuck] dip ccons
|
||||||
[b a F] (2a+3b) (a+2b) roll<
|
b‴ a‴ tuck [F] ccons
|
||||||
(2a+3b) (a+2b) [b a F] rrest
|
a‴ b‴ a‴ [F] ccons
|
||||||
(2a+3b) (a+2b) [F] [tuck] dip ccons
|
a‴ [b‴ a‴ F]
|
||||||
|
</code></pre>
|
||||||
|
<p>Putting it all together (and deferring factoring) we have:</p>
|
||||||
joy? [1 0 [over [dup + +] ii] [over [+] ii] clop roll< rrest [tuck] dip ccons]
|
<pre><code>F == [over [dup + +] ii] [over [+] ii] clop roll< rrest [tuck] dip ccons
|
||||||
|
</code></pre>
|
||||||
|
<p>Let's try it out:</p>
|
||||||
|
<pre><code>joy? [1 0 [over [dup + +] ii] [over [+] ii] clop roll< rrest [tuck] dip ccons]
|
||||||
[1 0 [over [dup + +] ii] [over [+] ii] clop roll< rrest [tuck] dip ccons]
|
[1 0 [over [dup + +] ii] [over [+] ii] clop roll< rrest [tuck] dip ccons]
|
||||||
|
|
||||||
joy? x
|
joy? x
|
||||||
|
|
@ -399,5 +416,7 @@ joy? x
|
||||||
2 8 34 144 [233 144 [over [dup + +] ii] [over [+] ii] clop roll< rrest [tuck] dip ccons]
|
2 8 34 144 [233 144 [over [dup + +] ii] [over [+] ii] clop roll< rrest [tuck] dip ccons]
|
||||||
</code></pre>
|
</code></pre>
|
||||||
<p>And so it goes...</p>
|
<p>And so it goes...</p>
|
||||||
|
<h2>Conclusion</h2>
|
||||||
|
<p>Generator programs like these are fun and interesting.</p>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
|
||||||
|
|
@ -11,12 +11,16 @@ Consider the `x` combinator:
|
||||||
We can apply it to a quoted program consisting of some value `a` and some function `B`:
|
We can apply it to a quoted program consisting of some value `a` and some function `B`:
|
||||||
|
|
||||||
[a B] x
|
[a B] x
|
||||||
|
[a B] dup i
|
||||||
|
[a B] [a B] i
|
||||||
[a B] a B
|
[a B] a B
|
||||||
|
|
||||||
Let `B` function `swap` the `a` with the quote and run some function `C` on it to generate a new value `b`:
|
Let `B` function `swap` the `a` with the quote and run some function `C` on it to generate a new value `b`:
|
||||||
|
|
||||||
B == swap [C] dip
|
B == swap [C] dip
|
||||||
|
|
||||||
|
Evaluation would go like this:
|
||||||
|
|
||||||
[a B] a B
|
[a B] a B
|
||||||
[a B] a swap [C] dip
|
[a B] a swap [C] dip
|
||||||
a [a B] [C] dip
|
a [a B] [C] dip
|
||||||
|
|
@ -422,14 +426,16 @@ otherwise we discard the term and the generator leaving the sum on the stack:
|
||||||
joy? [pop >4M] [popop] [[PE2.1] dip third-term] tailrec
|
joy? [pop >4M] [popop] [[PE2.1] dip third-term] tailrec
|
||||||
4613732
|
4613732
|
||||||
|
|
||||||
## Math
|
## Let's Use Math
|
||||||
|
|
||||||
|
Consider the Fib seq with algebraic variables:
|
||||||
|
|
||||||
a b
|
a b
|
||||||
b a+b
|
b a+b
|
||||||
a+b a+b+b
|
a+b a+b+b
|
||||||
a+b+b a+a+b+b+b
|
a+b+b a+a+b+b+b
|
||||||
|
|
||||||
So if (a,b) and a is even then the next even term pair is (a+2b, 2a+3b)
|
So starting with `(a b)` and assuming `a` is even then the next even term pair is `(a+2b, 2a+3b)`.
|
||||||
|
|
||||||
Reconsider:
|
Reconsider:
|
||||||
|
|
||||||
|
|
@ -440,30 +446,48 @@ From here we want to arrive at:
|
||||||
|
|
||||||
(a+2b) [(2a+3b) (a+2b) F]
|
(a+2b) [(2a+3b) (a+2b) F]
|
||||||
|
|
||||||
|
Let's derive `F`. We have two values and we want two new values so that's a `clop`:
|
||||||
|
|
||||||
b a F
|
b a F
|
||||||
b a [F0] [F1] fork
|
b a [F0] [F1] clop
|
||||||
|
|
||||||
b a over [+] ii
|
Where `F0` computes `a+2b`:
|
||||||
---------------------
|
|
||||||
a+2b
|
|
||||||
|
|
||||||
And:
|
F0 == over [+] ii
|
||||||
|
|
||||||
b a over [dup + +] ii
|
b a over [+] ii
|
||||||
---------------------------
|
b a b [+] ii
|
||||||
2a+3b
|
b a + b +
|
||||||
|
b+a b +
|
||||||
|
a+2b
|
||||||
|
|
||||||
|
And `F1` computes `2a+3b`:
|
||||||
|
|
||||||
[over [dup + +] ii] [over [+] ii] clop
|
F1 == over [dup + +] ii
|
||||||
roll< rrest [tuck] dip ccons
|
|
||||||
|
|
||||||
|
b a over [dup + +] ii
|
||||||
|
b a b [dup + +] ii
|
||||||
|
b a dup + + b dup + +
|
||||||
|
b a a + + b b + +
|
||||||
|
...
|
||||||
|
2a+3b
|
||||||
|
|
||||||
[b a F] b a F
|
So after that we have
|
||||||
|
|
||||||
[b a F] (2a+3b) (a+2b) roll<
|
[b a F] (a+2b) (2a+3b) F'
|
||||||
(2a+3b) (a+2b) [b a F] rrest
|
|
||||||
(2a+3b) (a+2b) [F] [tuck] dip ccons
|
|
||||||
|
|
||||||
|
[b a F] b‴ a‴ roll<
|
||||||
|
b‴ a‴ [b a F] rrest
|
||||||
|
b‴ a‴ [F] [tuck] dip ccons
|
||||||
|
b‴ a‴ tuck [F] ccons
|
||||||
|
a‴ b‴ a‴ [F] ccons
|
||||||
|
a‴ [b‴ a‴ F]
|
||||||
|
|
||||||
|
Putting it all together (and deferring factoring) we have:
|
||||||
|
|
||||||
|
F == [over [dup + +] ii] [over [+] ii] clop roll< rrest [tuck] dip ccons
|
||||||
|
|
||||||
|
Let's try it out:
|
||||||
|
|
||||||
joy? [1 0 [over [dup + +] ii] [over [+] ii] clop roll< rrest [tuck] dip ccons]
|
joy? [1 0 [over [dup + +] ii] [over [+] ii] clop roll< rrest [tuck] dip ccons]
|
||||||
[1 0 [over [dup + +] ii] [over [+] ii] clop roll< rrest [tuck] dip ccons]
|
[1 0 [over [dup + +] ii] [over [+] ii] clop roll< rrest [tuck] dip ccons]
|
||||||
|
|
@ -482,3 +506,6 @@ And:
|
||||||
|
|
||||||
And so it goes...
|
And so it goes...
|
||||||
|
|
||||||
|
## Conclusion
|
||||||
|
|
||||||
|
Generator programs like these are fun and interesting.
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue