4 "cell_type": "markdown",
16 "import logging, sys\n",
18 "logging.basicConfig(\n",
19 " format='%(message)s',\n",
20 " stream=sys.stdout,\n",
21 " level=logging.INFO,\n",
31 "from joy.utils.polytypes import (\n",
32 " doc_from_stack_effect, \n",
47 "D = FUNCTIONS.copy()\n",
53 "cell_type": "markdown",
66 "output_type": "stream",
68 " 25 (--) ∘ pop swap rolldown rrest ccons\n",
69 " 28 (a1 --) ∘ swap rolldown rrest ccons\n",
70 " 31 (a3 a2 a1 -- a2 a3) ∘ rolldown rrest ccons\n",
71 " 34 (a4 a3 a2 a1 -- a2 a3 a4) ∘ rrest ccons\n",
72 " 37 ([a4 a5 ...1] a3 a2 a1 -- a2 a3 [...1]) ∘ ccons\n",
73 " 40 ([a4 a5 ...1] a3 a2 a1 -- [a2 a3 ...1]) ∘ \n"
78 "fi, fo = infer(pop, swap, rolldown, rrest, ccons)[0]"
88 "output_type": "stream",
90 "([a4 a5 ...1] a3 a2 a1 -- [a2 a3 ...1])\n"
95 "print doc_from_stack_effect(fi, fo)"
100 "execution_count": 6,
104 "from joy.parser import text_to_expression\n",
105 "from joy.utils.stack import stack_to_string\n"
110 "execution_count": 7,
115 "output_type": "stream",
122 "e = text_to_expression('0 1 2 [3 4]') # reverse order\n",
123 "print stack_to_string(e)"
128 "execution_count": 8,
134 "{a1: 0, a2: 1, a3: 2, a4: 3, a5: 4, s2: (), s1: ()}"
137 "execution_count": 8,
139 "output_type": "execute_result"
143 "u = unify(e, fi)[0]\n",
149 "execution_count": 9,
154 "output_type": "stream",
156 "(... [3 4 ] 2 1 0 -- ... [1 2 ])\n"
161 "g = reify(u, (fi, fo))\n",
162 "print doc_from_stack_effect(*g)"
166 "cell_type": "markdown",
169 "## Unification Works \"in Reverse\""
174 "execution_count": 10,
178 "e = text_to_expression('[2 3]')"
183 "execution_count": 11,
189 "{a2: 2, a3: 3, s2: (), s1: ()}"
192 "execution_count": 11,
194 "output_type": "execute_result"
198 "u = unify(e, fo)[0] # output side, not input side\n",
204 "execution_count": 12,
209 "output_type": "stream",
211 "(... [a4 a5 ] 3 2 a1 -- ... [2 3 ])\n"
216 "g = reify(u, (fi, fo))\n",
217 "print doc_from_stack_effect(*g)"
221 "cell_type": "markdown",
229 "execution_count": 13,
234 "output_type": "stream",
236 " 25 (--) ∘ dup mul\n",
237 " 28 (a1 -- a1 a1) ∘ mul\n",
238 " 31 (f1 -- f2) ∘ \n",
239 " 31 (i1 -- i2) ∘ \n"
244 "fi, fo = infer(dup, mul)[0]"
249 "execution_count": 14,
254 "output_type": "stream",
261 "e = text_to_expression('\"two\"')\n",
262 "print stack_to_string(e)"
267 "execution_count": 15,
272 "output_type": "stream",
274 "Cannot unify 'two' and f1.\n"
281 "except JoyTypeError, err:\n",
288 "display_name": "Python 2",
289 "language": "python",
297 "file_extension": ".py",
298 "mimetype": "text/x-python",
300 "nbconvert_exporter": "python",
301 "pygments_lexer": "ipython2",