OSDN Git Service

I narrowed down the bug.
[joypy/Thun.git] / docs / The_Four_Operations.ipynb
1 {
2  "cells": [
3   {
4    "cell_type": "markdown",
5    "metadata": {},
6    "source": [
7     "# The Four Fundamental Operations of Definite Action\n",
8     "\n",
9     "All definite actions (computer program) can be defined by four fundamental patterns of combination:\n",
10     "\n",
11     "1. Sequence\n",
12     "2. Branch\n",
13     "3. Loop\n",
14     "4. Parallel"
15    ]
16   },
17   {
18    "cell_type": "markdown",
19    "metadata": {},
20    "source": [
21     "## Sequence\n",
22     "\n",
23     "Do one thing after another.  In joy this is represented by putting two symbols together, juxtaposition:\n",
24     "\n",
25     "    foo bar\n",
26     "\n",
27     "Operations have inputs and outputs.  The outputs of `foo` must be compatible in \"arity\", type, and shape with the inputs of `bar`."
28    ]
29   },
30   {
31    "cell_type": "markdown",
32    "metadata": {},
33    "source": [
34     "## Branch\n",
35     "\n",
36     "Do one thing or another.\n",
37     "\n",
38     "    boolean [F] [T] branch\n",
39     "\n",
40     "\n",
41     "       t [F] [T] branch\n",
42     "    ----------------------\n",
43     "              T\n",
44     "\n",
45     "\n",
46     "       f [F] [T] branch\n",
47     "    ----------------------\n",
48     "          F\n",
49     "\n",
50     "\n",
51     "    branch == unit cons swap pick i\n",
52     "\n",
53     "    boolean [F] [T] branch\n",
54     "    boolean [F] [T] unit cons swap pick i\n",
55     "    boolean [F] [[T]] cons swap pick i\n",
56     "    boolean [[F] [T]] swap pick i\n",
57     "    [[F] [T]] boolean pick i\n",
58     "    [F-or-T] i"
59    ]
60   },
61   {
62    "cell_type": "markdown",
63    "metadata": {},
64    "source": [
65     "Given some branch function `G`:\n",
66     "\n",
67     "    G == [F] [T] branch\n",
68     "\n",
69     "Used in a sequence like so:\n",
70     "\n",
71     "    foo G bar\n",
72     "\n",
73     "The inputs and outputs of `F` and `T` must be compatible with the outputs for `foo` and the inputs of `bar`, respectively.\n",
74     "\n",
75     "    foo F bar\n",
76     "\n",
77     "    foo T bar"
78    ]
79   },
80   {
81    "cell_type": "markdown",
82    "metadata": {},
83    "source": [
84     "### `ifte`\n",
85     "\n",
86     "Often it will be easier on the programmer to write branching code with the predicate specified in a quote.  The `ifte` combinator provides this (`T` for \"then\" and `E` for \"else\"):\n",
87     "\n",
88     "    [P] [T] [E] ifte\n",
89     "\n",
90     "Defined in terms of `branch`:\n",
91     "\n",
92     "    ifte == [nullary not] dip branch\n",
93     "\n",
94     "\n",
95     "In this case, `P` must be compatible with the stack and return a Boolean value, and `T` and `E` both must be compatible with the preceeding and following functions, as described above for `F` and `T`.  (Note that in the current implementation we are depending on Python for the underlying semantics, so the Boolean value doesn't *have* to be Boolean because Python's rules for \"truthiness\" will be used to evaluate it.  I reflect this in the structure of the stack effect comment of `branch`, it will only accept Boolean values, and in the definition of `ifte` above by including `not` in the quote, which also has the effect that the subject quotes are in the proper order for `branch`.)"
96    ]
97   },
98   {
99    "cell_type": "markdown",
100    "metadata": {},
101    "source": [
102     "## Loop\n",
103     "\n",
104     "Do one thing zero or more times.\n",
105     "\n",
106     "    boolean [Q] loop\n",
107     "\n",
108     "\n",
109     "       t [Q] loop\n",
110     "    ----------------\n",
111     "       Q [Q] loop\n",
112     "\n",
113     "\n",
114     "       ... f [Q] loop\n",
115     "    --------------------\n",
116     "       ...\n",
117     "\n",
118     "The `loop` combinator generates a copy of itself in the true branch.  This is the hallmark of recursive defintions.  In Thun there is no equivalent to conventional loops.  (There is, however, the `x` combinator, defined as `x == dup i`, which permits recursive constructs that do not need to be directly self-referential, unlike `loop` and `genrec`.)\n",
119     "\n",
120     "    loop == [] swap [dup dip loop] cons branch\n",
121     "\n",
122     "    boolean [Q] loop\n",
123     "    boolean [Q] [] swap [dup dip loop] cons branch\n",
124     "    boolean [] [Q] [dup dip loop] cons branch\n",
125     "    boolean [] [[Q] dup dip loop] branch\n",
126     "\n",
127     "In action the false branch does nothing while the true branch does:\n",
128     "\n",
129     "    t [] [[Q] dup dip loop] branch\n",
130     "          [Q] dup dip loop\n",
131     "          [Q] [Q] dip loop\n",
132     "          Q [Q] loop"
133    ]
134   },
135   {
136    "cell_type": "markdown",
137    "metadata": {},
138    "source": [
139     "Because `loop` expects and consumes a Boolean value, the `Q` function must be compatible with the previous stack *and itself* with a boolean flag for the next iteration:\n",
140     "\n",
141     "    Q == G b\n",
142     "\n",
143     "    Q [Q] loop\n",
144     "    G b [Q] loop\n",
145     "    G Q [Q] loop\n",
146     "    G G b [Q] loop\n",
147     "    G G Q [Q] loop\n",
148     "    G G G b [Q] loop\n",
149     "    G G G\n"
150    ]
151   },
152   {
153    "cell_type": "markdown",
154    "metadata": {},
155    "source": [
156     "### `while`\n",
157     "\n",
158     "Keep doing `B` _while_ some predicate `P` is true.  This is convenient as the predicate function is made nullary automatically and the body function can be designed without regard to leaving a Boolean flag for the next iteration:\n",
159     "\n",
160     "\n",
161     "\n",
162     "                [P] [B] while\n",
163     "    --------------------------------------\n",
164     "       [P] nullary [B [P] nullary] loop\n",
165     "\n",
166     "\n",
167     "    while == swap [nullary] cons dup dipd concat loop\n",
168     "\n",
169     "\n",
170     "    [P] [B] while\n",
171     "    [P] [B] swap [nullary] cons dup dipd concat loop\n",
172     "    [B] [P] [nullary] cons dup dipd concat loop\n",
173     "    [B] [[P] nullary] dup dipd concat loop\n",
174     "    [B] [[P] nullary] [[P] nullary] dipd concat loop\n",
175     "    [P] nullary [B] [[P] nullary] concat loop\n",
176     "    [P] nullary [B [P] nullary] loop\n",
177     "\n"
178    ]
179   },
180   {
181    "cell_type": "markdown",
182    "metadata": {},
183    "source": [
184     "## Parallel\n",
185     "\n",
186     "The *parallel* operation indicates that two (or more) functions *do not interfere* with each other and so can run in parallel.  The main difficulty in this sort of thing is orchestrating the recombining (\"join\" or \"wait\") of the results of the functions after they finish.\n",
187     "\n",
188     "The current implementaions and the following definitions *are not actually parallel* (yet), but there is no reason they couldn't be reimplemented in terms of e.g. Python threads.  I am not concerned with performance of the system just yet, only the elegance of the code it allows us to write."
189    ]
190   },
191   {
192    "cell_type": "markdown",
193    "metadata": {},
194    "source": [
195     "### `cleave`\n",
196     "\n",
197     "Joy has a few parallel combinators, the main one being `cleave`:\n",
198     "\n",
199     "                   ... x [A] [B] cleave\n",
200     "    ---------------------------------------------------------\n",
201     "       ... [x ...] [A] infra first [x ...] [B] infra first\n",
202     "    ---------------------------------------------------------\n",
203     "                       ... a b\n",
204     "\n",
205     "The `cleave` combinator expects a value and two quotes and it executes each quote in \"separate universes\" such that neither can affect the other, then it takes the first item from the stack in each universe and replaces the value and quotes with their respective results.\n",
206     "\n",
207     "(I think this corresponds to the \"fork\" operator, the little upward-pointed triangle, that takes two functions `A :: x -> a` and `B :: x -> b` and returns a function `F :: x -> (a, b)`, in Conal Elliott's \"Compiling to Categories\" paper, et. al.)\n",
208     "\n",
209     "Just a thought, if you `cleave` two jobs and one requires more time to finish than the other you'd like to be able to assign resources accordingly so that they both finish at the same time."
210    ]
211   },
212   {
213    "cell_type": "markdown",
214    "metadata": {},
215    "source": [
216     "### \"Apply\" Functions\n",
217     "\n",
218     "There are also `app2` and `app3` which run a single quote on more than one value:\n",
219     "\n",
220     "                     ... y x [Q] app2\n",
221     "     ---------------------------------------------------------\n",
222     "        ... [y ...] [Q] infra first [x ...] [Q] infra first\n",
223     "\n",
224     "\n",
225     "            ... z y x [Q] app3\n",
226     "     ---------------------------------\n",
227     "        ... [z ...] [Q] infra first\n",
228     "            [y ...] [Q] infra first\n",
229     "            [x ...] [Q] infra first\n",
230     "\n",
231     "Because the quoted program can be `i` we can define `cleave` in terms of `app2`:\n",
232     "\n",
233     "    cleave == [i] app2 [popd] dip"
234    ]
235   },
236   {
237    "cell_type": "markdown",
238    "metadata": {},
239    "source": [
240     "(I'm not sure why `cleave` was specified to take that value, I may make a combinator that does the same thing but without expecting a value.)\n",
241     "\n",
242     "    clv == [i] app2\n",
243     "\n",
244     "       [A] [B] clv\n",
245     "    ------------------\n",
246     "         a b"
247    ]
248   },
249   {
250    "cell_type": "markdown",
251    "metadata": {},
252    "source": [
253     "### `map`\n",
254     "\n",
255     "The common `map` function in Joy should also be though of as a *parallel* operator:\n",
256     "\n",
257     "    [a b c ...] [Q] map\n",
258     "\n",
259     "There is no reason why the implementation of `map` couldn't distribute the `Q` function over e.g. a pool of worker CPUs."
260    ]
261   },
262   {
263    "cell_type": "markdown",
264    "metadata": {},
265    "source": [
266     "### `pam`\n",
267     "\n",
268     "One of my favorite combinators, the `pam` combinator is just:\n",
269     "\n",
270     "    pam == [i] map\n",
271     "\n",
272     "This can be used to run any number of programs separately on the current stack and combine their (first) outputs in a result list.\n",
273     "\n",
274     "       [[A] [B] [C] ...] [i] map\n",
275     "    -------------------------------\n",
276     "       [ a   b   c  ...]"
277    ]
278   },
279   {
280    "cell_type": "markdown",
281    "metadata": {},
282    "source": [
283     "### Handling Other Kinds of Join\n",
284     "\n",
285     "The `cleave` operators and others all have pretty brutal join semantics: everything works and we always wait for every sub-computation.  We can imagine a few different potentially useful patterns of \"joining\" results from parallel combinators."
286    ]
287   },
288   {
289    "cell_type": "markdown",
290    "metadata": {},
291    "source": [
292     "#### first-to-finish\n",
293     "\n",
294     "Thinking about variations of `pam` there could be one that only returns the first result of the first-to-finish sub-program, or the stack could be replaced by its output stack.\n",
295     "\n",
296     "The other sub-programs would be cancelled."
297    ]
298   },
299   {
300    "cell_type": "markdown",
301    "metadata": {},
302    "source": [
303     "#### \"Fulminators\"\n",
304     "\n",
305     "Also known as \"Futures\" or \"Promises\" (by *everybody* else.  \"Fulinators\" is what I was going to call them when I was thinking about implementing them in Thun.)\n",
306     "\n",
307     "The runtime could be amended to permit \"thunks\" representing the results of in-progress computations to be left on the stack and picked up by subsequent functions.  These would themselves be able to leave behind more \"thunks\", the values of which depend on the eventual resolution of the values of the previous thunks.\n",
308     "\n",
309     "In this way you can create \"chains\" (and more complex shapes) out of normal-looking code that consist of a kind of call-graph interspersed with \"asyncronous\" ... events?\n",
310     "\n",
311     "In any case, until I can find a rigorous theory that shows that this sort of thing works perfectly in Joy code I'm not going to worry about it.  (And I think the Categories can deal with it anyhow?  Incremental evaluation, yeah?)\n",
312     "\n"
313    ]
314   }
315  ],
316  "metadata": {
317   "kernelspec": {
318    "display_name": "Python 2",
319    "language": "python",
320    "name": "python2"
321   },
322   "language_info": {
323    "codemirror_mode": {
324     "name": "ipython",
325     "version": 2
326    },
327    "file_extension": ".py",
328    "mimetype": "text/x-python",
329    "name": "python",
330    "nbconvert_exporter": "python",
331    "pygments_lexer": "ipython2",
332    "version": "2.7.12"
333   }
334  },
335  "nbformat": 4,
336  "nbformat_minor": 2
337 }