diff --git a/docs/Types.html b/docs/Types.html index ad4e69a..e7bf01a 100644 --- a/docs/Types.html +++ b/docs/Types.html @@ -11976,7 +11976,7 @@ w/ {
def poswrd(s, e, d):
- return roll_down(*swap(*pop(s, e, d)))
+ return rolldown(*swap(*pop(s, e, d)))
def defs():
- roll_down = (1, 2, 3), (2, 3, 1)
+ rolldown = (1, 2, 3), (2, 3, 1)
- roll_up = (1, 2, 3), (3, 1, 2)
+ rollup = (1, 2, 3), (3, 1, 2)
pop = (1,), ()
@@ -13500,13 +13500,13 @@ def rest(stack):
return (a1, stack)
-def roll_down(stack):
+def rolldown(stack):
"""(1 2 3 -- 2 3 1)"""
(a2, (a1, (a0, stack))) = stack
return (a0, (a2, (a1, stack)))
-def roll_up(stack):
+def rollup(stack):
"""(1 2 3 -- 3 1 2)"""
(a2, (a1, (a0, stack))) = stack
return (a1, (a0, (a2, stack)))
@@ -14099,9 +14099,9 @@ i0 >= f0 -> False
def defs():
- roll_down = (A[1], A[2], A[3]), (A[2], A[3], A[1])
+ rolldown = (A[1], A[2], A[3]), (A[2], A[3], A[1])
- roll_up = (A[1], A[2], A[3]), (A[3], A[1], A[2])
+ rollup = (A[1], A[2], A[3]), (A[3], A[1], A[2])
pop = (A[1],), ()
@@ -14206,8 +14206,8 @@ popdd = (a3 a2 a1 -- a2 a1)
popop = (a2 a1 --)
pred = (n1 -- n2)
rest = ([a1 .1.] -- [.1.])
-roll_down = (a1 a2 a3 -- a2 a3 a1)
-roll_up = (a1 a2 a3 -- a3 a1 a2)
+rolldown = (a1 a2 a3 -- a2 a3 a1)
+rollup = (a1 a2 a3 -- a3 a1 a2)
rrest = ([a0 a1 .0.] -- [.0.])
second = ([a0 a1 .0.] -- a1)
sqrt = (n0 -- n1)
@@ -14293,7 +14293,7 @@ uncons = ([a1 .1.] -- a1 [.1.])
In [53]:
-F = reduce(C, (pop, swap, roll_down, rest, rest, cons, cons))
+F = reduce(C, (pop, swap, rolldown, rest, rest, cons, cons))
F
@@ -14382,7 +14382,7 @@ uncons = ([a1 .1.] -- a1 [.1.])
# e.g. [swap] dip
-neato(roll_up, swap, roll_down)
+neato(rollup, swap, rolldown)
@@ -14414,7 +14414,7 @@ uncons = ([a1 .1.] -- a1 [.1.])
# e.g. [popop] dipd
-neato(popdd, roll_down, pop)
+neato(popdd, rolldown, pop)
@@ -14446,7 +14446,7 @@ uncons = ([a1 .1.] -- a1 [.1.])
# Reverse the order of the top three items.
-neato(roll_up, swap)
+neato(rollup, swap)
@@ -14653,8 +14653,8 @@ popd = (a2 a1 -- a1)
popdd = (a3 a2 a1 -- a2 a1)
popop = (a2 a1 --)
rest = ([a1 .1.] -- [.1.])
-roll_down = (a1 a2 a3 -- a2 a3 a1)
-roll_up = (a1 a2 a3 -- a3 a1 a2)
+rolldown = (a1 a2 a3 -- a2 a3 a1)
+rollup = (a1 a2 a3 -- a3 a1 a2)
rrest = ([a0 a1 .0.] -- [.0.])
second = ([a0 a1 .0.] -- a1)
swap = (a1 a2 -- a2 a1)
@@ -14812,7 +14812,8 @@ uncons = ((a, Z), S) -- (Z, (a, S))
name: (sequence_to_stack(i), sequence_to_stack(o))
for name, (i, o) in DEFS.iteritems()
}
-
+NEW_DEFS['stack'] = S[0], (S[0], S[0])
+NEW_DEFS['swaack'] = (S[1], S[0]), (S[0], S[1])
globals().update(NEW_DEFS)
@@ -14824,19 +14825,6 @@ uncons = ((a, Z), S) -- (Z, (a, S))
In [66]:
-
-
-stack = S[0], (S[0], S[0])
-
-
-
-
-
-
-
-
-
-In [67]:
C(stack, uncons)
@@ -14852,7 +14840,7 @@ uncons = ((a, Z), S) -- (Z, (a, S))
-Out[67]:
+Out[66]:
@@ -14869,7 +14857,7 @@ uncons = ((a, Z), S) -- (Z, (a, S))
-In [68]:
+In [67]:
C(C(stack, uncons), uncons)
@@ -14885,7 +14873,7 @@ uncons = ((a, Z), S) -- (Z, (a, S))
-Out[68]:
+Out[67]:
@@ -14920,7 +14908,7 @@ uncons = ((a, Z), S) -- (Z, (a, S))
-In [69]:
+In [68]:
def doc_from_stack_effect(inputs, outputs):
@@ -14977,7 +14965,7 @@ uncons = ((a, Z), S) -- (Z, (a, S))
-In [70]:
+In [69]:
for name, stack_effect_comment in sorted(NEW_DEFS.items()):
@@ -15013,12 +15001,14 @@ popdd = (a3 a2 a1 -- a2 a1)
popop = (a2 a1 --)
pred = (n1 -- n2)
rest = ([a1 .1.] -- [.1.])
-roll_down = (a1 a2 a3 -- a2 a3 a1)
-roll_up = (a1 a2 a3 -- a3 a1 a2)
+rolldown = (a1 a2 a3 -- a2 a3 a1)
+rollup = (a1 a2 a3 -- a3 a1 a2)
rrest = ([a0 a1 .0.] -- [.0.])
second = ([a0 a1 .0.] -- a1)
sqrt = (n0 -- n1)
+stack = (... -- ... [...])
succ = (n1 -- n2)
+swaack = ([.1.] -- [.0.])
swap = (a1 a2 -- a2 a1)
swons = ([.0.] a0 -- [a0 .0.])
third = ([a0 a1 a2 .0.] -- a2)
@@ -15034,7 +15024,7 @@ uncons = ([a1 .1.] -- a1 [.1.])
-In [71]:
+In [70]:
print ; print doc_from_stack_effect(*stack)
@@ -15075,7 +15065,7 @@ uncons = ([a1 .1.] -- a1 [.1.])
-In [72]:
+In [71]:
print doc_from_stack_effect(*C(ccons, stack))
@@ -15106,7 +15096,7 @@ uncons = ([a1 .1.] -- a1 [.1.])
-In [73]:
+In [72]:
Q = C(ccons, stack)
@@ -15124,7 +15114,7 @@ uncons = ([a1 .1.] -- a1 [.1.])
-Out[73]:
+Out[72]:
@@ -15150,7 +15140,7 @@ uncons = ([a1 .1.] -- a1 [.1.])
-In [74]:
+In [73]:
def compile_(name, f, doc=None):
@@ -15170,7 +15160,7 @@ uncons = ([a1 .1.] -- a1 [.1.])
-In [75]:
+In [74]:
print compile_('Q', Q)
@@ -15204,7 +15194,7 @@ uncons = ([a1 .1.] -- a1 [.1.])
-In [76]:
+In [75]:
unstack = (S[1], S[0]), S[1]
@@ -15218,7 +15208,7 @@ uncons = ([a1 .1.] -- a1 [.1.])
-In [77]:
+In [76]:
print doc_from_stack_effect(*unstack)
@@ -15249,7 +15239,7 @@ uncons = ([a1 .1.] -- a1 [.1.])
-In [78]:
+In [77]:
print doc_from_stack_effect(*enstacken)
@@ -15280,7 +15270,7 @@ uncons = ([a1 .1.] -- a1 [.1.])
-In [79]:
+In [78]:
print doc_from_stack_effect(*C(cons, unstack))
@@ -15311,7 +15301,7 @@ uncons = ([a1 .1.] -- a1 [.1.])
-In [80]:
+In [79]:
print doc_from_stack_effect(*C(cons, enstacken))
@@ -15342,7 +15332,7 @@ uncons = ([a1 .1.] -- a1 [.1.])
-In [81]:
+In [80]:
C(cons, unstack)
@@ -15358,7 +15348,7 @@ uncons = ([a1 .1.] -- a1 [.1.])
-Out[81]:
+Out[80]:
@@ -15384,7 +15374,7 @@ uncons = ([a1 .1.] -- a1 [.1.])
-In [82]:
+In [81]:
class IntJoyType(NumberJoyType): prefix = 'i'
@@ -15401,7 +15391,7 @@ uncons = ([a1 .1.] -- a1 [.1.])
-In [83]:
+In [82]:
muls = [
@@ -15419,7 +15409,7 @@ uncons = ([a1 .1.] -- a1 [.1.])
-In [84]:
+In [83]:
for f in muls:
@@ -15454,7 +15444,7 @@ uncons = ([a1 .1.] -- a1 [.1.])
-In [85]:
+In [84]:
for f in muls:
@@ -15491,7 +15481,7 @@ uncons = ([a1 .1.] -- a1 [.1.])
-In [86]:
+In [85]:
from itertools import product
@@ -15516,7 +15506,7 @@ uncons = ([a1 .1.] -- a1 [.1.])
-In [87]:
+In [86]:
for f in MC([dup], muls):
@@ -15549,7 +15539,7 @@ uncons = ([a1 .1.] -- a1 [.1.])
-In [88]:
+In [87]:
for f in MC([dup], [mul]):
@@ -15659,7 +15649,7 @@ uncons = ([a1 .1.] -- a1 [.1.])
-In [89]:
+In [88]:
class KleeneStar(object):
@@ -15722,7 +15712,7 @@ uncons = ([a1 .1.] -- a1 [.1.])
-In [90]:
+In [89]:
def unify(u, v, s=None):
@@ -15803,7 +15793,7 @@ uncons = ([a1 .1.] -- a1 [.1.])
-In [91]:
+In [90]:
a = (As[1], S[1])
@@ -15818,6 +15808,40 @@ uncons = ([a1 .1.] -- a1 [.1.])
+
+
+Out[90]:
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+In [91]:
+
+
+b = (A[1], S[2])
+b
+
+
+
+
+
+
+
+
+
+
Out[91]:
@@ -15825,40 +15849,6 @@ uncons = ([a1 .1.] -- a1 [.1.])
-
-
-
-
-
-
-
-
-
-
-In [92]:
-
-
-b = (A[1], S[2])
-b
-
-
-
-
-
-
-
-
-
-
-
-
-Out[92]:
-
-
-
-
@@ -15871,7 +15861,7 @@ uncons = ([a1 .1.] -- a1 [.1.])
-In [93]:
+In [92]:
for result in unify(b, a):
@@ -15904,7 +15894,7 @@ uncons = ([a1 .1.] -- a1 [.1.])
-In [94]:
+In [93]:
for result in unify(a, b):
@@ -15951,7 +15941,7 @@ uncons = ([a1 .1.] -- a1 [.1.])
-In [95]:
+In [94]:
sum_ = ((Ns[1], S[1]), S[0]), (N[0], S[0])
@@ -15984,7 +15974,7 @@ uncons = ([a1 .1.] -- a1 [.1.])
-In [96]:
+In [95]:
f = (N[1], (N[2], (N[3], S[1]))), S[0]
@@ -16017,7 +16007,7 @@ uncons = ([a1 .1.] -- a1 [.1.])
-In [97]:
+In [96]:
for result in unify(sum_[0], f):
@@ -16061,7 +16051,7 @@ uncons = ([a1 .1.] -- a1 [.1.])
-In [98]:
+In [97]:
def compose(f, g):
@@ -16080,7 +16070,7 @@ uncons = ([a1 .1.] -- a1 [.1.])
-In [99]:
+In [98]:
def meta_compose(F, G):
@@ -16105,7 +16095,7 @@ uncons = ([a1 .1.] -- a1 [.1.])
-In [100]:
+In [99]:
for f in MC([dup], muls):
@@ -16138,7 +16128,7 @@ uncons = ([a1 .1.] -- a1 [.1.])
-In [101]:
+In [100]:
for f in MC([dup], [sum_]):
@@ -16170,7 +16160,7 @@ uncons = ([a1 .1.] -- a1 [.1.])
-In [102]:
+In [101]:
for f in MC([cons], [sum_]):
@@ -16203,7 +16193,7 @@ uncons = ([a1 .1.] -- a1 [.1.])
-In [103]:
+In [102]:
sum_ = (((N[1], (Ns[1], S[1])), S[0]), (N[0], S[0]))
@@ -16239,7 +16229,7 @@ uncons = ([a1 .1.] -- a1 [.1.])
-In [104]:
+In [103]:
a = (A[4], (As[1], (A[3], S[1])))
@@ -16254,6 +16244,40 @@ uncons = ([a1 .1.] -- a1 [.1.])
+
+
+Out[103]:
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+In [104]:
+
+
+b = (A[1], (A[2], S[2]))
+b
+
+
+
+
+
+
+
+
+
+
Out[104]:
@@ -16261,40 +16285,6 @@ uncons = ([a1 .1.] -- a1 [.1.])
-
-
-
-
-
-
-
-
-
-
-In [105]:
-
-
-b = (A[1], (A[2], S[2]))
-b
-
-
-
-
-
-
-
-
-
-
-
-
-Out[105]:
-
-
-
-
@@ -16307,7 +16297,7 @@ uncons = ([a1 .1.] -- a1 [.1.])
-In [106]:
+In [105]:
for result in unify(b, a):
@@ -16340,7 +16330,7 @@ uncons = ([a1 .1.] -- a1 [.1.])
-In [107]:
+In [106]:
for result in unify(a, b):
@@ -16410,17 +16400,74 @@ uncons = ([a1 .1.] -- a1 [.1.])
-I think there's a way forward. If we convert our list of terms we are composing into a stack structure we can use it as a Joy expression, then we can treat the output half of a function's stack effect comment as a Joy interpreter stack, and just execute combinators directly. We can hybridize the compostition function with an interpreter to evaluate combinators, compose non-combinator functions, and put type variables on the stack. For combinators like branch that can have more than one stack effect we have to "split universes" again and return both. (Note: bug! If one branch doesn't type check the currect code ignores it, so you can think things are okay but have a type error waiting in the faled branch, I think... D'oh! FIXME!!!)
+I think there's a way forward. If we convert our list of terms we are composing into a stack structure we can use it as a Joy expression, then we can treat the output half of a function's stack effect comment as a Joy interpreter stack, and just execute combinators directly. We can hybridize the compostition function with an interpreter to evaluate combinators, compose non-combinator functions, and put type variables on the stack. For combinators like branch that can have more than one stack effect we have to "split universes" again and return both.
+
+
+
+In [107]:
+
+
+class FunctionJoyType(AnyJoyType):
+
+ def __init__(self, name, sec, number):
+ self.name = name
+ self.stack_effects = sec
+ self.number = number
+
+ def __add__(self, other):
+ return self
+ __radd__ = __add__
+
+ def __repr__(self):
+ return self.name
+
+
+class SymbolJoyType(FunctionJoyType): prefix = 'F'
+class CombinatorJoyType(FunctionJoyType): prefix = 'C'
+
+
+
+
+
+
In [108]:
-stack_concat = lambda q, e: (q[0], stack_concat(q[1], e)) if q else e
+def flatten(g):
+ return list(chain.from_iterable(g))
+
+
+ID = S[0], S[0] # Identity function.
+
+
+def infer(e, F=ID):
+ if not e:
+ return [F]
+
+ n, e = e
+
+ if isinstance(n, SymbolJoyType):
+ res = flatten(infer(e, Fn) for Fn in MC([F], n.stack_effects))
+
+ elif isinstance(n, CombinatorJoyType):
+ res = []
+ for combinator in n.stack_effects:
+ fi, fo = F
+ new_fo, ee, _ = combinator(fo, e, {})
+ ee = update(FUNCTIONS, ee) # Fix Symbols.
+ new_F = fi, new_fo
+ res.extend(infer(ee, new_F))
+ else:
+ lit = s9, (n, s9)
+ res = flatten(infer(e, Fn) for Fn in MC([F], [lit]))
+
+ return res
@@ -16431,6 +16478,195 @@ uncons = ([a1 .1.] -- a1 [.1.])
In [109]:
+
+
+f0, f1, f2, f3, f4, f5, f6, f7, f8, f9 = F = map(FloatJoyType, _R)
+i0, i1, i2, i3, i4, i5, i6, i7, i8, i9 = I = map(IntJoyType, _R)
+n0, n1, n2, n3, n4, n5, n6, n7, n8, n9 = N
+s0, s1, s2, s3, s4, s5, s6, s7, s8, s9 = S
+
+
+
+
+
+
+
+
+
+In [110]:
+
+
+import joy.library
+
+FNs = '''ccons cons divmod_ dup dupd first
+ over pm pop popd popdd popop pred
+ rest rolldown rollup rrest second
+ sqrt stack succ swaack swap swons
+ third tuck uncons'''
+
+FUNCTIONS = {
+ name: SymbolJoyType(name, [NEW_DEFS[name]], i)
+ for i, name in enumerate(FNs.strip().split())
+ }
+FUNCTIONS['sum'] = SymbolJoyType('sum', [(((Ns[1], s1), s0), (n0, s0))], 100)
+FUNCTIONS['mul'] = SymbolJoyType('mul', [
+ ((i2, (i1, s0)), (i3, s0)),
+ ((f2, (i1, s0)), (f3, s0)),
+ ((i2, (f1, s0)), (f3, s0)),
+ ((f2, (f1, s0)), (f3, s0)),
+], 101)
+FUNCTIONS.update({
+ combo.__name__: CombinatorJoyType(combo.__name__, [combo], i)
+ for i, combo in enumerate((
+ joy.library.i,
+ joy.library.dip,
+ joy.library.dipd,
+ joy.library.dipdd,
+ joy.library.dupdip,
+ joy.library.b,
+ joy.library.x,
+ joy.library.infra,
+ ))
+ })
+
+def branch_true(stack, expression, dictionary):
+ (then, (else_, (flag, stack))) = stack
+ return stack, CONCAT(then, expression), dictionary
+
+def branch_false(stack, expression, dictionary):
+ (then, (else_, (flag, stack))) = stack
+ return stack, CONCAT(else_, expression), dictionary
+
+FUNCTIONS['branch'] = CombinatorJoyType('branch', [branch_true, branch_false], 100)
+
+
+
+
+
+
+
+
+
+In [111]:
+
+
+globals().update(FUNCTIONS)
+
+
+
+
+
+
+
+
+
+In [112]:
+
+
+from itertools import chain
+from joy.utils.stack import list_to_stack as l2s
+
+
+
+
+
+
+
+
+
+In [113]:
+
+
+expression = l2s([n1, n2, (mul, s2), (stack, s3), dip, infra, first])
+
+
+
+
+
+
+
+
+
+In [114]:
+
+
+expression
+
+
+
+
+
+
+
+
+
+
+
+
+Out[114]:
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+In [120]:
+
+
+expression = l2s([n1, n2, mul])
+
+
+
+
+
+
+
+
+
+In [121]:
+
+
+infer(expression)
+
+
+
+
+
+
+
+
+
+
+
+
+Out[121]:
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+In [116]:
class SymbolJoyType(AnyJoyType):
@@ -16489,7 +16725,7 @@ uncons = ([a1 .1.] -- a1 [.1.])
-In [110]:
+In [117]:
def dip_t(stack, expression):
@@ -16505,7 +16741,7 @@ uncons = ([a1 .1.] -- a1 [.1.])
-In [111]:
+In [118]:
def dip(stack, expression, dictionary):
@@ -16548,10 +16784,10 @@ uncons = ([a1 .1.] -- a1 [.1.])
-In [112]:
+In [119]:
-F = reduce(C, (pop, swap, roll_down, rest, rest, cons, cons))
+F = reduce(C, (pop, swap, rolldown, rest, rest, cons, cons))
print doc_from_stack_effect(*F)
@@ -16572,78 +16808,91 @@ uncons = ([a1 .1.] -- a1 [.1.])
diff --git a/docs/Types.ipynb b/docs/Types.ipynb
index d72550e..e58ba4a 100644
--- a/docs/Types.ipynb
+++ b/docs/Types.ipynb
@@ -6,15 +6,10 @@
"source": [
"# Type Inference\n",
"\n",
- "This notebook presents a simple type inferencer for Joy code. It can infer the stack effect of most Joy expressions. It built largely by means of existing ideas and research (some of it may be original but I'm not able to say, as I don't fully understand the all of the source material in the depth required to make that call.) A great overview of the existing knowledge is a talk [\"Type Inference in Stack-Based Programming Languages\"](http://prl.ccs.neu.edu/blog/2017/03/10/type-inference-in-stack-based-programming-languages/) given by Rob Kleffner on or about 2017-03-10 as part of a course on the history of programming languages.\n",
+ "This notebook presents a simple type inferencer for Joy code. It can infer the stack effect of most Joy expressions. It's built largely by means of existing ideas and research. (A great overview of the existing knowledge is a talk [\"Type Inference in Stack-Based Programming Languages\"](http://prl.ccs.neu.edu/blog/2017/03/10/type-inference-in-stack-based-programming-languages/) given by Rob Kleffner on or about 2017-03-10 as part of a course on the history of programming languages.)\n",
"\n",
- "The notebook starts with a simple inferencer based on the work of Jaanus Pöial which we then progressively elaborate to cover more Joy semantics. Along the way we write a simple \"compiler\" that emits Python code for what I like to call Yin functions.\n",
- "\n",
- "Yin functions are those that only rearrange values in stacks, as opposed to Yang functions that actually work on the values themselves. It's interesting to note that a Joy with *only* stacks (no other kinds of values) can be made and is Turing-complete, therefore all Yang functions are actually Yin functions, and all computation can be performed by manipulations of structures of containers, which is a restatement of the Laws of Form. (Also, this implies that every program can be put into a form such that it can be computed in a single step, although that step may be enormous or unending.)\n",
- "\n",
- "Although I haven't completed it yet, a Joy based on Laws of Form provides the foundation for a provably correct computing system \"down to the metal\". This is my original and still primary motivation for developing Joy. (I want a proven-correct Operating System for a swarm of trash-collecting recycler robots. To trust it I have to implementment it myself from first principles, and I'm not smart enough to truly grok the existing literature and software, so I had to go look for and find LoF and Joy. Now that I have the mental tools to build my robot OS I can get down to it.\n",
- "\n",
- "Anyhow, here's type inference..."
+ "The notebook starts with a simple inferencer based on the work of Jaanus Pöial which we then progressively elaborate to cover more Joy semantics. Along the way we write a simple \"compiler\" that emits Python code for what I like to call Yin functions. (Yin functions are those that only rearrange values in stacks, as opposed to Yang functions that actually work on the values themselves.)\n",
+ "\n"
]
},
{
@@ -192,7 +187,7 @@
"outputs": [],
"source": [
"def poswrd(s, e, d):\n",
- " return roll_down(*swap(*pop(s, e, d)))"
+ " return rolldown(*swap(*pop(s, e, d)))"
]
},
{
@@ -230,7 +225,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
- "### Functions on Lists\n",
+ "### Functions on Stacks\n",
"These are slightly tricky.\n",
"\n",
" rest ( [1 ...] -- [...] )\n",
@@ -1289,9 +1284,9 @@
"source": [
"def defs():\n",
"\n",
- " roll_down = (1, 2, 3), (2, 3, 1)\n",
+ " rolldown = (1, 2, 3), (2, 3, 1)\n",
"\n",
- " roll_up = (1, 2, 3), (3, 1, 2)\n",
+ " rollup = (1, 2, 3), (3, 1, 2)\n",
"\n",
" pop = (1,), ()\n",
"\n",
@@ -1340,13 +1335,13 @@
" return (a1, stack)\n",
"\n",
"\n",
- "def roll_down(stack):\n",
+ "def rolldown(stack):\n",
" \"\"\"(1 2 3 -- 2 3 1)\"\"\"\n",
" (a2, (a1, (a0, stack))) = stack\n",
" return (a0, (a2, (a1, stack)))\n",
"\n",
"\n",
- "def roll_up(stack):\n",
+ "def rollup(stack):\n",
" \"\"\"(1 2 3 -- 3 1 2)\"\"\"\n",
" (a2, (a1, (a0, stack))) = stack\n",
" return (a1, (a0, (a2, stack)))\n",
@@ -1720,7 +1715,7 @@
" pass\n",
"\n",
" if not isinstance(f, tuple):\n",
- " seen[f] = f.__class__(c[f.prefix])\n",
+ " seen[f] = f.__class__(c[f.prefix] + 1)\n",
" c[f.prefix] += 1\n",
" return seen[f]\n",
"\n",
@@ -1735,7 +1730,7 @@
{
"data": {
"text/plain": [
- "(((a0,), (a0, a0)), ((n0, n1), (n2,)))"
+ "(((a1,), (a1, a1)), ((n1, n2), (n3,)))"
]
},
"execution_count": 46,
@@ -1822,9 +1817,9 @@
"source": [
"def defs():\n",
"\n",
- " roll_down = (A[1], A[2], A[3]), (A[2], A[3], A[1])\n",
+ " rolldown = (A[1], A[2], A[3]), (A[2], A[3], A[1])\n",
"\n",
- " roll_up = (A[1], A[2], A[3]), (A[3], A[1], A[2])\n",
+ " rollup = (A[1], A[2], A[3]), (A[3], A[1], A[2])\n",
"\n",
" pop = (A[1],), ()\n",
"\n",
@@ -1891,7 +1886,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
- "ccons = (a0 a1 [.0.] -- [a0 a1 .0.])\n",
+ "ccons = (a1 a2 [.1.] -- [a1 a2 .1.])\n",
"cons = (a1 [.1.] -- [a1 .1.])\n",
"divmod_ = (n2 n1 -- n4 n3)\n",
"dup = (a1 -- a1 a1)\n",
@@ -1906,15 +1901,15 @@
"popop = (a2 a1 --)\n",
"pred = (n1 -- n2)\n",
"rest = ([a1 .1.] -- [.1.])\n",
- "roll_down = (a1 a2 a3 -- a2 a3 a1)\n",
- "roll_up = (a1 a2 a3 -- a3 a1 a2)\n",
- "rrest = ([a0 a1 .0.] -- [.0.])\n",
- "second = ([a0 a1 .0.] -- a1)\n",
- "sqrt = (n0 -- n1)\n",
+ "rolldown = (a1 a2 a3 -- a2 a3 a1)\n",
+ "rollup = (a1 a2 a3 -- a3 a1 a2)\n",
+ "rrest = ([a1 a2 .1.] -- [.1.])\n",
+ "second = ([a1 a2 .1.] -- a2)\n",
+ "sqrt = (n1 -- n2)\n",
"succ = (n1 -- n2)\n",
"swap = (a1 a2 -- a2 a1)\n",
- "swons = ([.0.] a0 -- [a0 .0.])\n",
- "third = ([a0 a1 a2 .0.] -- a2)\n",
+ "swons = ([.1.] a1 -- [a1 .1.])\n",
+ "third = ([a1 a2 a3 .1.] -- a3)\n",
"tuck = (a2 a1 -- a1 a2 a1)\n",
"uncons = ([a1 .1.] -- a1 [.1.])\n"
]
@@ -1949,7 +1944,7 @@
{
"data": {
"text/plain": [
- "((n0,), (n1,))"
+ "((n1,), (n2,))"
]
},
"execution_count": 52,
@@ -1976,7 +1971,7 @@
{
"data": {
"text/plain": [
- "(((a0, (a1, s0)), a2, a3, a4), ((a3, (a2, s0)),))"
+ "(((a1, (a2, s1)), a3, a4, a5), ((a4, (a3, s1)),))"
]
},
"execution_count": 53,
@@ -1985,7 +1980,7 @@
}
],
"source": [
- "F = reduce(C, (pop, swap, roll_down, rest, rest, cons, cons))\n",
+ "F = reduce(C, (pop, swap, rolldown, rest, rest, cons, cons))\n",
"F"
]
},
@@ -1998,7 +1993,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
- "([a0 a1 .0.] a2 a3 a4 -- [a3 a2 .0.])\n"
+ "([a1 a2 .1.] a3 a4 a5 -- [a4 a3 .1.])\n"
]
}
],
@@ -2032,13 +2027,13 @@
"name": "stdout",
"output_type": "stream",
"text": [
- "(a0 a1 a2 -- a1 a0 a2)\n"
+ "(a1 a2 a3 -- a2 a1 a3)\n"
]
}
],
"source": [
"# e.g. [swap] dip\n",
- "neato(roll_up, swap, roll_down)"
+ "neato(rollup, swap, rolldown)"
]
},
{
@@ -2050,13 +2045,13 @@
"name": "stdout",
"output_type": "stream",
"text": [
- "(a0 a1 a2 a3 -- a2 a3)\n"
+ "(a1 a2 a3 a4 -- a3 a4)\n"
]
}
],
"source": [
"# e.g. [popop] dipd\n",
- "neato(popdd, roll_down, pop)"
+ "neato(popdd, rolldown, pop)"
]
},
{
@@ -2068,13 +2063,13 @@
"name": "stdout",
"output_type": "stream",
"text": [
- "(a0 a1 a2 -- a2 a1 a0)\n"
+ "(a1 a2 a3 -- a3 a2 a1)\n"
]
}
],
"source": [
"# Reverse the order of the top three items.\n",
- "neato(roll_up, swap)"
+ "neato(rollup, swap)"
]
},
{
@@ -2116,9 +2111,9 @@
"output_type": "stream",
"text": [
"def F(stack):\n",
- " \"\"\"([a0 a1 .0.] a2 a3 a4 -- [a3 a2 .0.])\"\"\"\n",
- " (a4, (a3, (a2, ((a0, (a1, s0)), stack)))) = stack\n",
- " return ((a3, (a2, s0)), stack)\n"
+ " \"\"\"([a1 a2 .1.] a3 a4 a5 -- [a4 a3 .1.])\"\"\"\n",
+ " (a5, (a4, (a3, ((a1, (a2, s1)), stack)))) = stack\n",
+ " return ((a4, (a3, s1)), stack)\n"
]
}
],
@@ -2143,9 +2138,9 @@
"output_type": "stream",
"text": [
"def sqr(stack):\n",
- " \"\"\"(n0 -- n1)\"\"\"\n",
- " (n0, stack) = stack\n",
- " return (n1, stack)\n"
+ " \"\"\"(n1 -- n2)\"\"\"\n",
+ " (n1, stack) = stack\n",
+ " return (n2, stack)\n"
]
}
],
@@ -2192,7 +2187,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
- "ccons = (a0 a1 [.0.] -- [a0 a1 .0.])\n",
+ "ccons = (a1 a2 [.1.] -- [a1 a2 .1.])\n",
"cons = (a1 [.1.] -- [a1 .1.])\n",
"dup = (a1 -- a1 a1)\n",
"dupd = (a2 a1 -- a2 a2 a1)\n",
@@ -2203,13 +2198,13 @@
"popdd = (a3 a2 a1 -- a2 a1)\n",
"popop = (a2 a1 --)\n",
"rest = ([a1 .1.] -- [.1.])\n",
- "roll_down = (a1 a2 a3 -- a2 a3 a1)\n",
- "roll_up = (a1 a2 a3 -- a3 a1 a2)\n",
- "rrest = ([a0 a1 .0.] -- [.0.])\n",
- "second = ([a0 a1 .0.] -- a1)\n",
+ "rolldown = (a1 a2 a3 -- a2 a3 a1)\n",
+ "rollup = (a1 a2 a3 -- a3 a1 a2)\n",
+ "rrest = ([a1 a2 .1.] -- [.1.])\n",
+ "second = ([a1 a2 .1.] -- a2)\n",
"swap = (a1 a2 -- a2 a1)\n",
- "swons = ([.0.] a0 -- [a0 .0.])\n",
- "third = ([a0 a1 a2 .0.] -- a2)\n",
+ "swons = ([.1.] a1 -- [a1 .1.])\n",
+ "third = ([a1 a2 a3 .1.] -- a3)\n",
"tuck = (a2 a1 -- a1 a2 a1)\n",
"uncons = ([a1 .1.] -- a1 [.1.])\n"
]
@@ -2346,7 +2341,8 @@
" name: (sequence_to_stack(i), sequence_to_stack(o))\n",
" for name, (i, o) in DEFS.iteritems()\n",
"}\n",
- "\n",
+ "NEW_DEFS['stack'] = S[0], (S[0], S[0])\n",
+ "NEW_DEFS['swaack'] = (S[1], S[0]), (S[0], S[1])\n",
"globals().update(NEW_DEFS)"
]
},
@@ -2354,23 +2350,14 @@
"cell_type": "code",
"execution_count": 66,
"metadata": {},
- "outputs": [],
- "source": [
- "stack = S[0], (S[0], S[0])"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 67,
- "metadata": {},
"outputs": [
{
"data": {
"text/plain": [
- "((a0, s0), (s0, (a0, (a0, s0))))"
+ "((a1, s1), (s1, (a1, (a1, s1))))"
]
},
- "execution_count": 67,
+ "execution_count": 66,
"metadata": {},
"output_type": "execute_result"
}
@@ -2381,16 +2368,16 @@
},
{
"cell_type": "code",
- "execution_count": 68,
+ "execution_count": 67,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
- "((a0, (a1, s0)), (s0, (a1, (a0, (a0, (a1, s0))))))"
+ "((a1, (a2, s1)), (s1, (a2, (a1, (a1, (a2, s1))))))"
]
},
- "execution_count": 68,
+ "execution_count": 67,
"metadata": {},
"output_type": "execute_result"
}
@@ -2416,7 +2403,7 @@
},
{
"cell_type": "code",
- "execution_count": 69,
+ "execution_count": 68,
"metadata": {},
"outputs": [],
"source": [
@@ -2469,7 +2456,7 @@
},
{
"cell_type": "code",
- "execution_count": 70,
+ "execution_count": 69,
"metadata": {
"scrolled": false
},
@@ -2478,7 +2465,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
- "ccons = (a0 a1 [.0.] -- [a0 a1 .0.])\n",
+ "ccons = (a1 a2 [.1.] -- [a1 a2 .1.])\n",
"cons = (a1 [.1.] -- [a1 .1.])\n",
"divmod_ = (n2 n1 -- n4 n3)\n",
"dup = (a1 -- a1 a1)\n",
@@ -2493,15 +2480,17 @@
"popop = (a2 a1 --)\n",
"pred = (n1 -- n2)\n",
"rest = ([a1 .1.] -- [.1.])\n",
- "roll_down = (a1 a2 a3 -- a2 a3 a1)\n",
- "roll_up = (a1 a2 a3 -- a3 a1 a2)\n",
- "rrest = ([a0 a1 .0.] -- [.0.])\n",
- "second = ([a0 a1 .0.] -- a1)\n",
- "sqrt = (n0 -- n1)\n",
+ "rolldown = (a1 a2 a3 -- a2 a3 a1)\n",
+ "rollup = (a1 a2 a3 -- a3 a1 a2)\n",
+ "rrest = ([a1 a2 .1.] -- [.1.])\n",
+ "second = ([a1 a2 .1.] -- a2)\n",
+ "sqrt = (n1 -- n2)\n",
+ "stack = (... -- ... [...])\n",
"succ = (n1 -- n2)\n",
+ "swaack = ([.1.] -- [.0.])\n",
"swap = (a1 a2 -- a2 a1)\n",
- "swons = ([.0.] a0 -- [a0 .0.])\n",
- "third = ([a0 a1 a2 .0.] -- a2)\n",
+ "swons = ([.1.] a1 -- [a1 .1.])\n",
+ "third = ([a1 a2 a3 .1.] -- a3)\n",
"tuck = (a2 a1 -- a1 a2 a1)\n",
"uncons = ([a1 .1.] -- a1 [.1.])\n"
]
@@ -2514,7 +2503,7 @@
},
{
"cell_type": "code",
- "execution_count": 71,
+ "execution_count": 70,
"metadata": {},
"outputs": [
{
@@ -2524,11 +2513,11 @@
"\n",
"(... -- ... [...])\n",
"\n",
- "(... a0 -- ... a0 a0 [...])\n",
+ "(... a1 -- ... a1 a1 [...])\n",
"\n",
- "(... a1 a0 -- ... a1 a0 a0 a1 [...])\n",
+ "(... a2 a1 -- ... a2 a1 a1 a2 [...])\n",
"\n",
- "(... a0 -- ... a0 [a0 ...])\n"
+ "(... a1 -- ... a1 [a1 ...])\n"
]
}
],
@@ -2541,14 +2530,14 @@
},
{
"cell_type": "code",
- "execution_count": 72,
+ "execution_count": 71,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
- "(... a1 a0 [.0.] -- ... [a1 a0 .0.] [[a1 a0 .0.] ...])\n"
+ "(... a2 a1 [.1.] -- ... [a2 a1 .1.] [[a2 a1 .1.] ...])\n"
]
}
],
@@ -2558,16 +2547,16 @@
},
{
"cell_type": "code",
- "execution_count": 73,
+ "execution_count": 72,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
- "((s0, (a0, (a1, s1))), (((a1, (a0, s0)), s1), ((a1, (a0, s0)), s1)))"
+ "((s1, (a1, (a2, s2))), (((a2, (a1, s1)), s2), ((a2, (a1, s1)), s2)))"
]
},
- "execution_count": 73,
+ "execution_count": 72,
"metadata": {},
"output_type": "execute_result"
}
@@ -2588,7 +2577,7 @@
},
{
"cell_type": "code",
- "execution_count": 74,
+ "execution_count": 73,
"metadata": {},
"outputs": [],
"source": [
@@ -2604,7 +2593,7 @@
},
{
"cell_type": "code",
- "execution_count": 75,
+ "execution_count": 74,
"metadata": {},
"outputs": [
{
@@ -2612,9 +2601,9 @@
"output_type": "stream",
"text": [
"def Q(stack):\n",
- " \"\"\"(... a1 a0 [.0.] -- ... [a1 a0 .0.] [[a1 a0 .0.] ...])\"\"\"\n",
- " (s0, (a0, (a1, s1))) = stack\n",
- " return (((a1, (a0, s0)), s1), ((a1, (a0, s0)), s1))\n"
+ " \"\"\"(... a2 a1 [.1.] -- ... [a2 a1 .1.] [[a2 a1 .1.] ...])\"\"\"\n",
+ " (s1, (a1, (a2, s2))) = stack\n",
+ " return (((a2, (a1, s1)), s2), ((a2, (a1, s1)), s2))\n"
]
}
],
@@ -2659,7 +2648,7 @@
},
{
"cell_type": "code",
- "execution_count": 76,
+ "execution_count": 75,
"metadata": {},
"outputs": [],
"source": [
@@ -2669,7 +2658,7 @@
},
{
"cell_type": "code",
- "execution_count": 77,
+ "execution_count": 76,
"metadata": {},
"outputs": [
{
@@ -2686,7 +2675,7 @@
},
{
"cell_type": "code",
- "execution_count": 78,
+ "execution_count": 77,
"metadata": {},
"outputs": [
{
@@ -2703,14 +2692,14 @@
},
{
"cell_type": "code",
- "execution_count": 79,
+ "execution_count": 78,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
- "(a0 [.0.] -- a0)\n"
+ "(a1 [.1.] -- a1)\n"
]
}
],
@@ -2720,14 +2709,14 @@
},
{
"cell_type": "code",
- "execution_count": 80,
+ "execution_count": 79,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
- "(a0 [.0.] -- [[a0 .0.] .1.])\n"
+ "(a1 [.1.] -- [[a1 .1.] .2.])\n"
]
}
],
@@ -2737,16 +2726,16 @@
},
{
"cell_type": "code",
- "execution_count": 81,
+ "execution_count": 80,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
- "((s0, (a0, s1)), (a0, s0))"
+ "((s1, (a1, s2)), (a1, s1))"
]
},
- "execution_count": 81,
+ "execution_count": 80,
"metadata": {},
"output_type": "execute_result"
}
@@ -2772,7 +2761,7 @@
},
{
"cell_type": "code",
- "execution_count": 82,
+ "execution_count": 81,
"metadata": {},
"outputs": [],
"source": [
@@ -2785,7 +2774,7 @@
},
{
"cell_type": "code",
- "execution_count": 83,
+ "execution_count": 82,
"metadata": {},
"outputs": [],
"source": [
@@ -2799,7 +2788,7 @@
},
{
"cell_type": "code",
- "execution_count": 84,
+ "execution_count": 83,
"metadata": {},
"outputs": [
{
@@ -2820,15 +2809,15 @@
},
{
"cell_type": "code",
- "execution_count": 85,
+ "execution_count": 84,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
- "(a1 -- a1 a1) (i1 i2 -- i3) (i0 -- i1)\n",
- "(a1 -- a1 a1) (f1 f2 -- f3) (f0 -- f1)\n"
+ "(a1 -- a1 a1) (i1 i2 -- i3) (i1 -- i2)\n",
+ "(a1 -- a1 a1) (f1 f2 -- f3) (f1 -- f2)\n"
]
}
],
@@ -2843,7 +2832,7 @@
},
{
"cell_type": "code",
- "execution_count": 86,
+ "execution_count": 85,
"metadata": {},
"outputs": [],
"source": [
@@ -2864,15 +2853,15 @@
},
{
"cell_type": "code",
- "execution_count": 87,
+ "execution_count": 86,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
- "(f0 -- f1)\n",
- "(i0 -- i1)\n"
+ "(f1 -- f2)\n",
+ "(i1 -- i2)\n"
]
}
],
@@ -2883,14 +2872,14 @@
},
{
"cell_type": "code",
- "execution_count": 88,
+ "execution_count": 87,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
- "(n0 -- n1)\n"
+ "(n1 -- n2)\n"
]
}
],
@@ -3017,7 +3006,7 @@
},
{
"cell_type": "code",
- "execution_count": 89,
+ "execution_count": 88,
"metadata": {},
"outputs": [],
"source": [
@@ -3082,7 +3071,7 @@
},
{
"cell_type": "code",
- "execution_count": 90,
+ "execution_count": 89,
"metadata": {},
"outputs": [],
"source": [
@@ -3159,7 +3148,7 @@
},
{
"cell_type": "code",
- "execution_count": 91,
+ "execution_count": 90,
"metadata": {},
"outputs": [
{
@@ -3168,7 +3157,7 @@
"(a1*, s1)"
]
},
- "execution_count": 91,
+ "execution_count": 90,
"metadata": {},
"output_type": "execute_result"
}
@@ -3180,7 +3169,7 @@
},
{
"cell_type": "code",
- "execution_count": 92,
+ "execution_count": 91,
"metadata": {},
"outputs": [
{
@@ -3189,7 +3178,7 @@
"(a1, s2)"
]
},
- "execution_count": 92,
+ "execution_count": 91,
"metadata": {},
"output_type": "execute_result"
}
@@ -3201,7 +3190,7 @@
},
{
"cell_type": "code",
- "execution_count": 93,
+ "execution_count": 92,
"metadata": {},
"outputs": [
{
@@ -3220,7 +3209,7 @@
},
{
"cell_type": "code",
- "execution_count": 94,
+ "execution_count": 93,
"metadata": {},
"outputs": [
{
@@ -3251,7 +3240,7 @@
},
{
"cell_type": "code",
- "execution_count": 95,
+ "execution_count": 94,
"metadata": {},
"outputs": [
{
@@ -3270,7 +3259,7 @@
},
{
"cell_type": "code",
- "execution_count": 96,
+ "execution_count": 95,
"metadata": {},
"outputs": [
{
@@ -3289,7 +3278,7 @@
},
{
"cell_type": "code",
- "execution_count": 97,
+ "execution_count": 96,
"metadata": {},
"outputs": [
{
@@ -3332,7 +3321,7 @@
},
{
"cell_type": "code",
- "execution_count": 98,
+ "execution_count": 97,
"metadata": {},
"outputs": [],
"source": [
@@ -3354,7 +3343,7 @@
},
{
"cell_type": "code",
- "execution_count": 99,
+ "execution_count": 98,
"metadata": {},
"outputs": [],
"source": [
@@ -3375,15 +3364,15 @@
},
{
"cell_type": "code",
- "execution_count": 100,
+ "execution_count": 99,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
- "(f0 -- f1)\n",
- "(i0 -- i1)\n"
+ "(f1 -- f2)\n",
+ "(i1 -- i2)\n"
]
}
],
@@ -3394,14 +3383,14 @@
},
{
"cell_type": "code",
- "execution_count": 101,
+ "execution_count": 100,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
- "([n0* .0.] -- [n0* .0.] n0)\n"
+ "([n1* .1.] -- [n1* .1.] n1)\n"
]
}
],
@@ -3414,15 +3403,15 @@
},
{
"cell_type": "code",
- "execution_count": 102,
+ "execution_count": 101,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
- "(a0 [.0.] -- n0)\n",
- "(n0 [n0* .0.] -- n1)\n"
+ "(a1 [.1.] -- n1)\n",
+ "(n1 [n1* .1.] -- n2)\n"
]
}
],
@@ -3435,14 +3424,14 @@
},
{
"cell_type": "code",
- "execution_count": 103,
+ "execution_count": 102,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
- "(a1 [.1.] -- [a1 .1.]) ([n1 n1* .1.] -- n0) (n0 [n0* .0.] -- n1)\n"
+ "(a1 [.1.] -- [a1 .1.]) ([n1 n1* .1.] -- n0) (n1 [n1* .1.] -- n2)\n"
]
}
],
@@ -3478,7 +3467,7 @@
},
{
"cell_type": "code",
- "execution_count": 104,
+ "execution_count": 103,
"metadata": {},
"outputs": [
{
@@ -3487,7 +3476,7 @@
"(a4, (a1*, (a3, s1)))"
]
},
- "execution_count": 104,
+ "execution_count": 103,
"metadata": {},
"output_type": "execute_result"
}
@@ -3499,7 +3488,7 @@
},
{
"cell_type": "code",
- "execution_count": 105,
+ "execution_count": 104,
"metadata": {},
"outputs": [
{
@@ -3508,7 +3497,7 @@
"(a1, (a2, s2))"
]
},
- "execution_count": 105,
+ "execution_count": 104,
"metadata": {},
"output_type": "execute_result"
}
@@ -3520,7 +3509,7 @@
},
{
"cell_type": "code",
- "execution_count": 106,
+ "execution_count": 105,
"metadata": {},
"outputs": [
{
@@ -3539,7 +3528,7 @@
},
{
"cell_type": "code",
- "execution_count": 107,
+ "execution_count": 106,
"metadata": {},
"outputs": [
{
@@ -3625,7 +3614,34 @@
"cell_type": "markdown",
"metadata": {},
"source": [
- "I think there's a way forward. If we convert our list of terms we are composing into a stack structure we can use it as a *Joy expression*, then we can treat the *output half* of a function's stack effect comment as a Joy interpreter stack, and just execute combinators directly. We can hybridize the compostition function with an interpreter to evaluate combinators, compose non-combinator functions, and put type variables on the stack. For combinators like `branch` that can have more than one stack effect we have to \"split universes\" again and return both. (Note: bug! If one branch doesn't type check the currect code ignores it, so you can think things are okay but have a type error waiting in the faled branch, I think... D'oh! FIXME!!!)"
+ "I think there's a way forward. If we convert our list of terms we are composing into a stack structure we can use it as a *Joy expression*, then we can treat the *output half* of a function's stack effect comment as a Joy interpreter stack, and just execute combinators directly. We can hybridize the compostition function with an interpreter to evaluate combinators, compose non-combinator functions, and put type variables on the stack. For combinators like `branch` that can have more than one stack effect we have to \"split universes\" again and return both."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 107,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "class FunctionJoyType(AnyJoyType):\n",
+ "\n",
+ " def __init__(self, name, sec, number):\n",
+ " self.name = name\n",
+ " self.stack_effects = sec\n",
+ " self.number = number\n",
+ "\n",
+ " def __add__(self, other):\n",
+ " return self\n",
+ " __radd__ = __add__\n",
+ "\n",
+ " def __repr__(self):\n",
+ " return self.name\n",
+ "\n",
+ "\n",
+ "class SymbolJoyType(FunctionJoyType): prefix = 'F'\n",
+ "class CombinatorJoyType(FunctionJoyType): prefix = 'C'\n",
+ "\n",
+ "\n"
]
},
{
@@ -3634,7 +3650,36 @@
"metadata": {},
"outputs": [],
"source": [
- "stack_concat = lambda q, e: (q[0], stack_concat(q[1], e)) if q else e"
+ "def flatten(g):\n",
+ " return list(chain.from_iterable(g))\n",
+ "\n",
+ "\n",
+ "ID = S[0], S[0] # Identity function.\n",
+ "\n",
+ "\n",
+ "def infer(e, F=ID):\n",
+ " if not e:\n",
+ " return [F]\n",
+ "\n",
+ " n, e = e\n",
+ "\n",
+ " if isinstance(n, SymbolJoyType):\n",
+ " res = flatten(infer(e, Fn) for Fn in MC([F], n.stack_effects))\n",
+ "\n",
+ " elif isinstance(n, CombinatorJoyType):\n",
+ " res = []\n",
+ " for combinator in n.stack_effects:\n",
+ " fi, fo = F\n",
+ " new_fo, ee, _ = combinator(fo, e, {})\n",
+ " ee = update(FUNCTIONS, ee) # Fix Symbols.\n",
+ " new_F = fi, new_fo\n",
+ " res.extend(infer(ee, new_F))\n",
+ " else:\n",
+ " lit = s9, (n, s9)\n",
+ " res = flatten(infer(e, Fn) for Fn in MC([F], [lit]))\n",
+ "\n",
+ " return res\n",
+ "\n"
]
},
{
@@ -3643,65 +3688,10 @@
"metadata": {},
"outputs": [],
"source": [
- "class SymbolJoyType(AnyJoyType):\n",
- " prefix = 'F'\n",
- "\n",
- " def __init__(self, name, sec, number):\n",
- " self.name = name\n",
- " self.stack_effects = sec\n",
- " self.number = number\n",
- "\n",
- "class CombinatorJoyType(SymbolJoyType): prefix = 'C'\n",
- "\n",
- "def dip_t(stack, expression):\n",
- " (quote, (a1, stack)) = stack\n",
- " expression = stack_concat(quote, (a1, expression))\n",
- " return stack, expression\n",
- "\n",
- "CONS = SymbolJoyType('cons', [cons], 23)\n",
- "DIP = CombinatorJoyType('dip', [dip_t], 44)\n",
- "\n",
- "\n",
- "def kav(F, e):\n",
- " #i, stack = F\n",
- " if not e:\n",
- " return [(F, e)]\n",
- " n, e = e\n",
- " if isinstance(n, SymbolJoyType):\n",
- " Fs = []\n",
- " for sec in n.stack_effects:\n",
- " Fs.extend(MC([F], sec))\n",
- " return [kav(Fn, e) for Fn in Fs]\n",
- " if isinstance(n, CombinatorJoyType):\n",
- " res = []\n",
- " for f in n.stack_effects:\n",
- " s, e = f(F[1], e)\n",
- " new_F = F[0], s\n",
- " res.extend(kav(new_F, e))\n",
- " return res\n",
- " lit = S[0], (n, S[0])\n",
- " return [kav(Fn, e) for Fn in MC([F], [lit])]\n"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": []
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": []
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "compare, and be amazed:"
+ "f0, f1, f2, f3, f4, f5, f6, f7, f8, f9 = F = map(FloatJoyType, _R)\n",
+ "i0, i1, i2, i3, i4, i5, i6, i7, i8, i9 = I = map(IntJoyType, _R)\n",
+ "n0, n1, n2, n3, n4, n5, n6, n7, n8, n9 = N\n",
+ "s0, s1, s2, s3, s4, s5, s6, s7, s8, s9 = S"
]
},
{
@@ -3710,10 +3700,48 @@
"metadata": {},
"outputs": [],
"source": [
- "def dip_t(stack, expression):\n",
- " (quote, (a1, stack)) = stack\n",
- " expression = stack_concat(quote, (a1, expression))\n",
- " return stack, expression"
+ "import joy.library\n",
+ "\n",
+ "FNs = '''ccons cons divmod_ dup dupd first\n",
+ " over pm pop popd popdd popop pred\n",
+ " rest rolldown rollup rrest second\n",
+ " sqrt stack succ swaack swap swons\n",
+ " third tuck uncons'''\n",
+ "\n",
+ "FUNCTIONS = {\n",
+ " name: SymbolJoyType(name, [NEW_DEFS[name]], i)\n",
+ " for i, name in enumerate(FNs.strip().split())\n",
+ " }\n",
+ "FUNCTIONS['sum'] = SymbolJoyType('sum', [(((Ns[1], s1), s0), (n0, s0))], 100)\n",
+ "FUNCTIONS['mul'] = SymbolJoyType('mul', [\n",
+ " ((i2, (i1, s0)), (i3, s0)),\n",
+ " ((f2, (i1, s0)), (f3, s0)),\n",
+ " ((i2, (f1, s0)), (f3, s0)),\n",
+ " ((f2, (f1, s0)), (f3, s0)),\n",
+ "], 101)\n",
+ "FUNCTIONS.update({\n",
+ " combo.__name__: CombinatorJoyType(combo.__name__, [combo], i)\n",
+ " for i, combo in enumerate((\n",
+ " joy.library.i,\n",
+ " joy.library.dip,\n",
+ " joy.library.dipd,\n",
+ " joy.library.dipdd,\n",
+ " joy.library.dupdip,\n",
+ " joy.library.b,\n",
+ " joy.library.x,\n",
+ " joy.library.infra,\n",
+ " ))\n",
+ " })\n",
+ "\n",
+ "def branch_true(stack, expression, dictionary):\n",
+ " (then, (else_, (flag, stack))) = stack\n",
+ " return stack, CONCAT(then, expression), dictionary\n",
+ "\n",
+ "def branch_false(stack, expression, dictionary):\n",
+ " (then, (else_, (flag, stack))) = stack\n",
+ " return stack, CONCAT(else_, expression), dictionary\n",
+ "\n",
+ "FUNCTIONS['branch'] = CombinatorJoyType('branch', [branch_true, branch_false], 100)"
]
},
{
@@ -3722,10 +3750,181 @@
"metadata": {},
"outputs": [],
"source": [
- "def dip(stack, expression, dictionary):\n",
- " (quote, (x, stack)) = stack\n",
- " expression = (x, expression)\n",
- " return stack, concat(quote, expression), dictionary"
+ "globals().update(FUNCTIONS)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 112,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "from itertools import chain\n",
+ "from joy.utils.stack import list_to_stack as l2s"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 113,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "expression = l2s([n1, n2, (mul, s2), (stack, s3), dip, infra, first])"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 114,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "(n1, (n2, ((mul, s2), ((stack, s3), (dip, (infra, (first, ())))))))"
+ ]
+ },
+ "execution_count": 114,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "expression"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 115,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "expression = l2s([n1, n2, mul])"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 116,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "(n1, (n2, (mul, ())))"
+ ]
+ },
+ "execution_count": 116,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "expression"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 117,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "[(s1, (f1, s1)), (s1, (i1, s1))]"
+ ]
+ },
+ "execution_count": 117,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "infer(expression)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 118,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "[(s1, (f1, s1)), (s1, (i1, s1))]"
+ ]
+ },
+ "execution_count": 118,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "infer(expression)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 119,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "(-- f1)\n",
+ "(-- i1)\n"
+ ]
+ }
+ ],
+ "source": [
+ "for stack_effect_comment in infer(expression):\n",
+ " print doc_from_stack_effect(*stack_effect_comment)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 123,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "(n1, (n2, (mul, ())))"
+ ]
+ },
+ "execution_count": 123,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "expression"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 124,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "[(s1, (f1, s1)), (s1, (i1, s1))]"
+ ]
+ },
+ "execution_count": 124,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "infer(expression)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "And that brings us to current Work-In-Progress. I'm pretty hopeful that the mixed-mode inferencer/interpreter `infer()` function along with the ability to specify multiple implementations for the combinators will permit modelling of the stack effects of e.g. `ifte`. If I can keep up the pace I should be able to verify that conjecture by the end of June."
]
},
{
@@ -3855,10 +4054,26 @@
"source": []
},
{
- "cell_type": "markdown",
+ "cell_type": "code",
+ "execution_count": null,
"metadata": {},
+ "outputs": [],
+ "source": []
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": []
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 137,
+ "metadata": {},
+ "outputs": [],
"source": [
- "And that brings us to current Work-In-Progress. I'm pretty hopeful that the mixed-mode inferencer/interpreter `kav()` function along with the ability to specify multiple implementations for the combinators will permit modelling of the stack effects of e.g. `ifte`. If I can keep up the pace I should be able to verify that conjecture by the end of June."
+ "hasattr?"
]
},
{
@@ -3878,40 +4093,43 @@
},
{
"cell_type": "code",
- "execution_count": 112,
+ "execution_count": 173,
"metadata": {},
- "outputs": [
- {
- "ename": "ValueError",
- "evalue": "need more than 1 value to unpack",
- "output_type": "error",
- "traceback": [
- "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
- "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
- "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mF\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mreduce\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mC\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mpop\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mswap\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mroll_down\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mrest\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mrest\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcons\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcons\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0;32mprint\u001b[0m \u001b[0mdoc_from_stack_effect\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0mF\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
- "\u001b[0;32m\u001b[0m in \u001b[0;36mC\u001b[0;34m(f, g)\u001b[0m\n\u001b[1;32m 10\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mC\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mf\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mg\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 11\u001b[0m \u001b[0mf\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mg\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mrelabel\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mf\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mg\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 12\u001b[0;31m \u001b[0;32mfor\u001b[0m \u001b[0mfg\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mcompose\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mf\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mg\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 13\u001b[0m \u001b[0;32myield\u001b[0m \u001b[0mdelabel\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfg\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
- "\u001b[0;32m\u001b[0m in \u001b[0;36mcompose\u001b[0;34m(f, g)\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mcompose\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mf\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mg\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0;34m(\u001b[0m\u001b[0mf_in\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mf_out\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mg_in\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mg_out\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mf\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mg\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3\u001b[0m \u001b[0ms\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0munify\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mg_in\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mf_out\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0ms\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;32mraise\u001b[0m \u001b[0mTypeError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Cannot unify %r and %r.'\u001b[0m \u001b[0;34m%\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mf_out\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mg_in\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
- "\u001b[0;32m\u001b[0m in \u001b[0;36mC\u001b[0;34m(f, g)\u001b[0m\n\u001b[1;32m 10\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mC\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mf\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mg\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 11\u001b[0m \u001b[0mf\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mg\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mrelabel\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mf\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mg\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 12\u001b[0;31m \u001b[0;32mfor\u001b[0m \u001b[0mfg\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mcompose\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mf\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mg\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 13\u001b[0m \u001b[0;32myield\u001b[0m \u001b[0mdelabel\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfg\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
- "\u001b[0;32m\u001b[0m in \u001b[0;36mcompose\u001b[0;34m(f, g)\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mcompose\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mf\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mg\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0;34m(\u001b[0m\u001b[0mf_in\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mf_out\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mg_in\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mg_out\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mf\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mg\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3\u001b[0m \u001b[0ms\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0munify\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mg_in\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mf_out\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0ms\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;32mraise\u001b[0m \u001b[0mTypeError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Cannot unify %r and %r.'\u001b[0m \u001b[0;34m%\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mf_out\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mg_in\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
- "\u001b[0;32m\u001b[0m in \u001b[0;36mC\u001b[0;34m(f, g)\u001b[0m\n\u001b[1;32m 10\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mC\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mf\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mg\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 11\u001b[0m \u001b[0mf\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mg\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mrelabel\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mf\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mg\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 12\u001b[0;31m \u001b[0;32mfor\u001b[0m \u001b[0mfg\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mcompose\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mf\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mg\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 13\u001b[0m \u001b[0;32myield\u001b[0m \u001b[0mdelabel\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfg\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
- "\u001b[0;32m\u001b[0m in \u001b[0;36mcompose\u001b[0;34m(f, g)\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mcompose\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mf\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mg\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0;34m(\u001b[0m\u001b[0mf_in\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mf_out\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mg_in\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mg_out\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mf\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mg\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3\u001b[0m \u001b[0ms\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0munify\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mg_in\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mf_out\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0ms\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;32mraise\u001b[0m \u001b[0mTypeError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Cannot unify %r and %r.'\u001b[0m \u001b[0;34m%\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mf_out\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mg_in\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
- "\u001b[0;32m\u001b[0m in \u001b[0;36mC\u001b[0;34m(f, g)\u001b[0m\n\u001b[1;32m 10\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mC\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mf\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mg\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 11\u001b[0m \u001b[0mf\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mg\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mrelabel\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mf\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mg\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 12\u001b[0;31m \u001b[0;32mfor\u001b[0m \u001b[0mfg\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mcompose\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mf\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mg\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 13\u001b[0m \u001b[0;32myield\u001b[0m \u001b[0mdelabel\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfg\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
- "\u001b[0;32m\u001b[0m in \u001b[0;36mcompose\u001b[0;34m(f, g)\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mcompose\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mf\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mg\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0;34m(\u001b[0m\u001b[0mf_in\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mf_out\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mg_in\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mg_out\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mf\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mg\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3\u001b[0m \u001b[0ms\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0munify\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mg_in\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mf_out\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0ms\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;32mraise\u001b[0m \u001b[0mTypeError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Cannot unify %r and %r.'\u001b[0m \u001b[0;34m%\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mf_out\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mg_in\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
- "\u001b[0;32m\u001b[0m in \u001b[0;36mC\u001b[0;34m(f, g)\u001b[0m\n\u001b[1;32m 10\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mC\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mf\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mg\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 11\u001b[0m \u001b[0mf\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mg\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mrelabel\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mf\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mg\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 12\u001b[0;31m \u001b[0;32mfor\u001b[0m \u001b[0mfg\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mcompose\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mf\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mg\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 13\u001b[0m \u001b[0;32myield\u001b[0m \u001b[0mdelabel\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfg\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
- "\u001b[0;32m\u001b[0m in \u001b[0;36mcompose\u001b[0;34m(f, g)\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mcompose\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mf\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mg\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0;34m(\u001b[0m\u001b[0mf_in\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mf_out\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mg_in\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mg_out\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mf\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mg\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3\u001b[0m \u001b[0ms\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0munify\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mg_in\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mf_out\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0ms\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;32mraise\u001b[0m \u001b[0mTypeError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Cannot unify %r and %r.'\u001b[0m \u001b[0;34m%\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mf_out\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mg_in\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
- "\u001b[0;31mValueError\u001b[0m: need more than 1 value to unpack"
- ]
- }
- ],
+ "outputs": [],
"source": [
- "F = reduce(C, (pop, swap, roll_down, rest, rest, cons, cons))\n",
+ "def _ge(self, other):\n",
+ " return (issubclass(other.__class__, self.__class__)\n",
+ " or hasattr(self, 'accept')\n",
+ " and isinstance(other, self.accept))\n",
"\n",
- "print doc_from_stack_effect(*F)"
+ "AnyJoyType.__ge__ = _ge\n",
+ "AnyJoyType.accept = tuple, int, float, long, str, unicode, bool, Symbol\n",
+ "StackJoyType.accept = tuple"
]
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 174,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "([a4 a5 .1.] a3 a2 a1 -- [a2 a3 .1.])\n"
+ ]
+ }
+ ],
+ "source": [
+ "F = infer(l2s((pop, swap, rolldown, rest, rest, cons, cons)))\n",
+ "\n",
+ "for f in F:\n",
+ " print doc_from_stack_effect(*f)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 126,
"metadata": {},
"outputs": [],
"source": [
@@ -3923,18 +4141,94 @@
"execution_count": null,
"metadata": {},
"outputs": [],
- "source": [
- "s = text_to_expression('[3 4 ...] 2 1')\n",
- "s"
- ]
+ "source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
+ "source": []
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 166,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "(a3 a2 a1 --)\n"
+ ]
+ }
+ ],
"source": [
- "L = unify(F[1], s)\n",
+ "F = infer(l2s((pop, pop, pop)))\n",
+ "\n",
+ "for f in F:\n",
+ " print doc_from_stack_effect(*f)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 167,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "(0, (1, (2, ())))"
+ ]
+ },
+ "execution_count": 167,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "s = text_to_expression('0 1 2')\n",
+ "s"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 168,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "(a1, (a2, (a3, s1)))"
+ ]
+ },
+ "execution_count": 168,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "F[0][0]"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 171,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "()"
+ ]
+ },
+ "execution_count": 171,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "L = unify(s, F[0][0])\n",
"L"
]
},
@@ -3943,8 +4237,103 @@
"execution_count": null,
"metadata": {},
"outputs": [],
+ "source": []
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": []
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": []
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 161,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "(0, (1, (2, ((3, (4, ())), ()))))"
+ ]
+ },
+ "execution_count": 161,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
"source": [
- "F[1]"
+ "s = text_to_expression('0 1 2 [3 4]')\n",
+ "s"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 162,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "(a1, (a2, (a3, ((a4, (a5, s1)), s2))))"
+ ]
+ },
+ "execution_count": 162,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "F[0][0]"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 163,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "()"
+ ]
+ },
+ "execution_count": 163,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "L = unify(s, F[0][0])\n",
+ "L"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 164,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "()"
+ ]
+ },
+ "execution_count": 164,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "L = unify(F[0][0], s)\n",
+ "L"
]
},
{
@@ -3967,10 +4356,23 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 156,
"metadata": {},
- "outputs": [],
- "source": []
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "True"
+ ]
+ },
+ "execution_count": 156,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "A[1] >= 23"
+ ]
},
{
"cell_type": "code",
@@ -4412,6 +4814,125 @@
"metadata": {},
"outputs": [],
"source": []
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": []
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": []
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": []
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": []
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "class SymbolJoyType(AnyJoyType):\n",
+ " prefix = 'F'\n",
+ "\n",
+ " def __init__(self, name, sec, number):\n",
+ " self.name = name\n",
+ " self.stack_effects = sec\n",
+ " self.number = number\n",
+ "\n",
+ "class CombinatorJoyType(SymbolJoyType): prefix = 'C'\n",
+ "\n",
+ "def dip_t(stack, expression):\n",
+ " (quote, (a1, stack)) = stack\n",
+ " expression = stack_concat(quote, (a1, expression))\n",
+ " return stack, expression\n",
+ "\n",
+ "CONS = SymbolJoyType('cons', [cons], 23)\n",
+ "DIP = CombinatorJoyType('dip', [dip_t], 44)\n",
+ "\n",
+ "\n",
+ "def kav(F, e):\n",
+ " #i, stack = F\n",
+ " if not e:\n",
+ " return [(F, e)]\n",
+ " n, e = e\n",
+ " if isinstance(n, SymbolJoyType):\n",
+ " Fs = []\n",
+ " for sec in n.stack_effects:\n",
+ " Fs.extend(MC([F], sec))\n",
+ " return [kav(Fn, e) for Fn in Fs]\n",
+ " if isinstance(n, CombinatorJoyType):\n",
+ " res = []\n",
+ " for f in n.stack_effects:\n",
+ " s, e = f(F[1], e)\n",
+ " new_F = F[0], s\n",
+ " res.extend(kav(new_F, e))\n",
+ " return res\n",
+ " lit = S[0], (n, S[0])\n",
+ " return [kav(Fn, e) for Fn in MC([F], [lit])]\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": []
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": []
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "compare, and be amazed:"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "def dip_t(stack, expression):\n",
+ " (quote, (a1, stack)) = stack\n",
+ " expression = stack_concat(quote, (a1, expression))\n",
+ " return stack, expression"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "def dip(stack, expression, dictionary):\n",
+ " (quote, (x, stack)) = stack\n",
+ " expression = (x, expression)\n",
+ " return stack, concat(quote, expression), dictionary"
+ ]
}
],
"metadata": {
diff --git a/docs/Types.md b/docs/Types.md
index 395fcda..0261bbe 100644
--- a/docs/Types.md
+++ b/docs/Types.md
@@ -127,7 +127,7 @@ The simplest way to "compile" this function would be something like:
```python
def poswrd(s, e, d):
- return roll_down(*swap(*pop(s, e, d)))
+ return rolldown(*swap(*pop(s, e, d)))
```
However, internally this function would still be allocating tuples (stack cells) and doing other unnecesssary work.
@@ -829,9 +829,9 @@ We can use `compile_()` to generate many primitives in the library from their st
```python
def defs():
- roll_down = (1, 2, 3), (2, 3, 1)
+ rolldown = (1, 2, 3), (2, 3, 1)
- roll_up = (1, 2, 3), (3, 1, 2)
+ rollup = (1, 2, 3), (3, 1, 2)
pop = (1,), ()
@@ -877,13 +877,13 @@ for name, stack_effect_comment in sorted(defs().items()):
return (a1, stack)
- def roll_down(stack):
+ def rolldown(stack):
"""(1 2 3 -- 2 3 1)"""
(a2, (a1, (a0, stack))) = stack
return (a0, (a2, (a1, stack)))
- def roll_up(stack):
+ def rollup(stack):
"""(1 2 3 -- 3 1 2)"""
(a2, (a1, (a0, stack))) = stack
return (a1, (a0, (a2, stack)))
@@ -1218,9 +1218,9 @@ Rewrite the stack effect comments:
```python
def defs():
- roll_down = (A[1], A[2], A[3]), (A[2], A[3], A[1])
+ rolldown = (A[1], A[2], A[3]), (A[2], A[3], A[1])
- roll_up = (A[1], A[2], A[3]), (A[3], A[1], A[2])
+ rollup = (A[1], A[2], A[3]), (A[3], A[1], A[2])
pop = (A[1],), ()
@@ -1295,8 +1295,8 @@ for name, stack_effect_comment in sorted(DEFS.items()):
popop = (a2 a1 --)
pred = (n1 -- n2)
rest = ([a1 .1.] -- [.1.])
- roll_down = (a1 a2 a3 -- a2 a3 a1)
- roll_up = (a1 a2 a3 -- a3 a1 a2)
+ rolldown = (a1 a2 a3 -- a2 a3 a1)
+ rollup = (a1 a2 a3 -- a3 a1 a2)
rrest = ([a0 a1 .0.] -- [.0.])
second = ([a0 a1 .0.] -- a1)
sqrt = (n0 -- n1)
@@ -1331,7 +1331,7 @@ Revisit the `F` function, works fine.
```python
-F = reduce(C, (pop, swap, roll_down, rest, rest, cons, cons))
+F = reduce(C, (pop, swap, rolldown, rest, rest, cons, cons))
F
```
@@ -1361,7 +1361,7 @@ def neato(*funcs):
```python
# e.g. [swap] dip
-neato(roll_up, swap, roll_down)
+neato(rollup, swap, rolldown)
```
(a0 a1 a2 -- a1 a0 a2)
@@ -1370,7 +1370,7 @@ neato(roll_up, swap, roll_down)
```python
# e.g. [popop] dipd
-neato(popdd, roll_down, pop)
+neato(popdd, rolldown, pop)
```
(a0 a1 a2 a3 -- a2 a3)
@@ -1379,7 +1379,7 @@ neato(popdd, roll_down, pop)
```python
# Reverse the order of the top three items.
-neato(roll_up, swap)
+neato(rollup, swap)
```
(a0 a1 a2 -- a2 a1 a0)
@@ -1461,8 +1461,8 @@ for name, stack_effect_comment in sorted(defs().items()):
popdd = (a3 a2 a1 -- a2 a1)
popop = (a2 a1 --)
rest = ([a1 .1.] -- [.1.])
- roll_down = (a1 a2 a3 -- a2 a3 a1)
- roll_up = (a1 a2 a3 -- a3 a1 a2)
+ rolldown = (a1 a2 a3 -- a2 a3 a1)
+ rollup = (a1 a2 a3 -- a3 a1 a2)
rrest = ([a0 a1 .0.] -- [.0.])
second = ([a0 a1 .0.] -- a1)
swap = (a1 a2 -- a2 a1)
@@ -1554,16 +1554,12 @@ NEW_DEFS = {
name: (sequence_to_stack(i), sequence_to_stack(o))
for name, (i, o) in DEFS.iteritems()
}
-
+NEW_DEFS['stack'] = S[0], (S[0], S[0])
+NEW_DEFS['swaack'] = (S[1], S[0]), (S[0], S[1])
globals().update(NEW_DEFS)
```
-```python
-stack = S[0], (S[0], S[0])
-```
-
-
```python
C(stack, uncons)
```
@@ -1662,12 +1658,14 @@ for name, stack_effect_comment in sorted(NEW_DEFS.items()):
popop = (a2 a1 --)
pred = (n1 -- n2)
rest = ([a1 .1.] -- [.1.])
- roll_down = (a1 a2 a3 -- a2 a3 a1)
- roll_up = (a1 a2 a3 -- a3 a1 a2)
+ rolldown = (a1 a2 a3 -- a2 a3 a1)
+ rollup = (a1 a2 a3 -- a3 a1 a2)
rrest = ([a0 a1 .0.] -- [.0.])
second = ([a0 a1 .0.] -- a1)
sqrt = (n0 -- n1)
+ stack = (... -- ... [...])
succ = (n1 -- n2)
+ swaack = ([.1.] -- [.0.])
swap = (a1 a2 -- a2 a1)
swons = ([.0.] a0 -- [a0 .0.])
third = ([a0 a1 a2 .0.] -- a2)
@@ -2268,14 +2266,167 @@ Obviously it would be:
Without any information about the contents of the quote we can't say much about the result.
-I think there's a way forward. If we convert our list of terms we are composing into a stack structure we can use it as a *Joy expression*, then we can treat the *output half* of a function's stack effect comment as a Joy interpreter stack, and just execute combinators directly. We can hybridize the compostition function with an interpreter to evaluate combinators, compose non-combinator functions, and put type variables on the stack. For combinators like `branch` that can have more than one stack effect we have to "split universes" again and return both. (Note: bug! If one branch doesn't type check the currect code ignores it, so you can think things are okay but have a type error waiting in the faled branch, I think... D'oh! FIXME!!!)
+I think there's a way forward. If we convert our list of terms we are composing into a stack structure we can use it as a *Joy expression*, then we can treat the *output half* of a function's stack effect comment as a Joy interpreter stack, and just execute combinators directly. We can hybridize the compostition function with an interpreter to evaluate combinators, compose non-combinator functions, and put type variables on the stack. For combinators like `branch` that can have more than one stack effect we have to "split universes" again and return both.
```python
-stack_concat = lambda q, e: (q[0], stack_concat(q[1], e)) if q else e
+class FunctionJoyType(AnyJoyType):
+
+ def __init__(self, name, sec, number):
+ self.name = name
+ self.stack_effects = sec
+ self.number = number
+
+ def __add__(self, other):
+ return self
+ __radd__ = __add__
+
+ def __repr__(self):
+ return self.name
+
+
+class SymbolJoyType(FunctionJoyType): prefix = 'F'
+class CombinatorJoyType(FunctionJoyType): prefix = 'C'
+
+
+
```
+```python
+def flatten(g):
+ return list(chain.from_iterable(g))
+
+
+ID = S[0], S[0] # Identity function.
+
+
+def infer(e, F=ID):
+ if not e:
+ return [F]
+
+ n, e = e
+
+ if isinstance(n, SymbolJoyType):
+ res = flatten(infer(e, Fn) for Fn in MC([F], n.stack_effects))
+
+ elif isinstance(n, CombinatorJoyType):
+ res = []
+ for combinator in n.stack_effects:
+ fi, fo = F
+ new_fo, ee, _ = combinator(fo, e, {})
+ ee = update(FUNCTIONS, ee) # Fix Symbols.
+ new_F = fi, new_fo
+ res.extend(infer(ee, new_F))
+ else:
+ lit = s9, (n, s9)
+ res = flatten(infer(e, Fn) for Fn in MC([F], [lit]))
+
+ return res
+
+
+```
+
+
+```python
+f0, f1, f2, f3, f4, f5, f6, f7, f8, f9 = F = map(FloatJoyType, _R)
+i0, i1, i2, i3, i4, i5, i6, i7, i8, i9 = I = map(IntJoyType, _R)
+n0, n1, n2, n3, n4, n5, n6, n7, n8, n9 = N
+s0, s1, s2, s3, s4, s5, s6, s7, s8, s9 = S
+```
+
+
+```python
+import joy.library
+
+FNs = '''ccons cons divmod_ dup dupd first
+ over pm pop popd popdd popop pred
+ rest rolldown rollup rrest second
+ sqrt stack succ swaack swap swons
+ third tuck uncons'''
+
+FUNCTIONS = {
+ name: SymbolJoyType(name, [NEW_DEFS[name]], i)
+ for i, name in enumerate(FNs.strip().split())
+ }
+FUNCTIONS['sum'] = SymbolJoyType('sum', [(((Ns[1], s1), s0), (n0, s0))], 100)
+FUNCTIONS['mul'] = SymbolJoyType('mul', [
+ ((i2, (i1, s0)), (i3, s0)),
+ ((f2, (i1, s0)), (f3, s0)),
+ ((i2, (f1, s0)), (f3, s0)),
+ ((f2, (f1, s0)), (f3, s0)),
+], 101)
+FUNCTIONS.update({
+ combo.__name__: CombinatorJoyType(combo.__name__, [combo], i)
+ for i, combo in enumerate((
+ joy.library.i,
+ joy.library.dip,
+ joy.library.dipd,
+ joy.library.dipdd,
+ joy.library.dupdip,
+ joy.library.b,
+ joy.library.x,
+ joy.library.infra,
+ ))
+ })
+
+def branch_true(stack, expression, dictionary):
+ (then, (else_, (flag, stack))) = stack
+ return stack, CONCAT(then, expression), dictionary
+
+def branch_false(stack, expression, dictionary):
+ (then, (else_, (flag, stack))) = stack
+ return stack, CONCAT(else_, expression), dictionary
+
+FUNCTIONS['branch'] = CombinatorJoyType('branch', [branch_true, branch_false], 100)
+```
+
+
+```python
+globals().update(FUNCTIONS)
+```
+
+
+```python
+from itertools import chain
+from joy.utils.stack import list_to_stack as l2s
+```
+
+
+```python
+expression = l2s([n1, n2, (mul, s2), (stack, s3), dip, infra, first])
+```
+
+
+```python
+expression
+```
+
+
+
+
+ (n1, (n2, ((mul, s2), ((stack, s3), (dip, (infra, (first, ())))))))
+
+
+
+
+```python
+expression = l2s([n1, n2, mul])
+```
+
+
+```python
+infer(expression)
+```
+
+
+
+
+ []
+
+
+
+
```python
class SymbolJoyType(AnyJoyType):
prefix = 'F'
@@ -2345,7 +2496,7 @@ For this to work the type label classes have to be modified to let `T >= t` succ
```python
-F = reduce(C, (pop, swap, roll_down, rest, rest, cons, cons))
+F = reduce(C, (pop, swap, rolldown, rest, rest, cons, cons))
print doc_from_stack_effect(*F)
```
@@ -2353,22 +2504,22 @@ print doc_from_stack_effect(*F)
---------------------------------------------------------------------------
- ValueError Traceback (most recent call last)
+ TypeError Traceback (most recent call last)
- in ()
- 1 F = reduce(C, (pop, swap, roll_down, rest, rest, cons, cons))
+ in ()
+ 1 F = reduce(C, (pop, swap, rolldown, rest, rest, cons, cons))
2
----> 3 print doc_from_stack_effect(*F)
- in C(f, g)
+ in C(f, g)
10 def C(f, g):
11 f, g = relabel(f, g)
---> 12 for fg in compose(f, g):
13 yield delabel(fg)
- in compose(f, g)
+ in compose(f, g)
1 def compose(f, g):
----> 2 (f_in, f_out), (g_in, g_out) = f, g
3 s = unify(g_in, f_out)
@@ -2376,14 +2527,14 @@ print doc_from_stack_effect(*F)
5 raise TypeError('Cannot unify %r and %r.' % (f_out, g_in))
- in C(f, g)
+ in C(f, g)
10 def C(f, g):
11 f, g = relabel(f, g)
---> 12 for fg in compose(f, g):
13 yield delabel(fg)
- in compose(f, g)
+ in compose(f, g)
1 def compose(f, g):
----> 2 (f_in, f_out), (g_in, g_out) = f, g
3 s = unify(g_in, f_out)
@@ -2391,14 +2542,14 @@ print doc_from_stack_effect(*F)
5 raise TypeError('Cannot unify %r and %r.' % (f_out, g_in))
- in C(f, g)
+ in C(f, g)
10 def C(f, g):
11 f, g = relabel(f, g)
---> 12 for fg in compose(f, g):
13 yield delabel(fg)
- in compose(f, g)
+ in compose(f, g)
1 def compose(f, g):
----> 2 (f_in, f_out), (g_in, g_out) = f, g
3 s = unify(g_in, f_out)
@@ -2406,14 +2557,14 @@ print doc_from_stack_effect(*F)
5 raise TypeError('Cannot unify %r and %r.' % (f_out, g_in))
- in C(f, g)
+ in C(f, g)
10 def C(f, g):
11 f, g = relabel(f, g)
---> 12 for fg in compose(f, g):
13 yield delabel(fg)
- in compose(f, g)
+ in compose(f, g)
1 def compose(f, g):
----> 2 (f_in, f_out), (g_in, g_out) = f, g
3 s = unify(g_in, f_out)
@@ -2421,14 +2572,14 @@ print doc_from_stack_effect(*F)
5 raise TypeError('Cannot unify %r and %r.' % (f_out, g_in))
- in C(f, g)
+ in C(f, g)
10 def C(f, g):
11 f, g = relabel(f, g)
---> 12 for fg in compose(f, g):
13 yield delabel(fg)
- in compose(f, g)
+ in compose(f, g)
1 def compose(f, g):
----> 2 (f_in, f_out), (g_in, g_out) = f, g
3 s = unify(g_in, f_out)
@@ -2436,7 +2587,22 @@ print doc_from_stack_effect(*F)
5 raise TypeError('Cannot unify %r and %r.' % (f_out, g_in))
- ValueError: need more than 1 value to unpack
+ in C(f, g)
+ 10 def C(f, g):
+ 11 f, g = relabel(f, g)
+ ---> 12 for fg in compose(f, g):
+ 13 yield delabel(fg)
+
+
+ in compose(f, g)
+ 1 def compose(f, g):
+ ----> 2 (f_in, f_out), (g_in, g_out) = f, g
+ 3 s = unify(g_in, f_out)
+ 4 if not s:
+ 5 raise TypeError('Cannot unify %r and %r.' % (f_out, g_in))
+
+
+ TypeError: 'SymbolJoyType' object is not iterable
diff --git a/docs/Types.rst b/docs/Types.rst
index 4a1a80d..216ce90 100644
--- a/docs/Types.rst
+++ b/docs/Types.rst
@@ -211,7 +211,7 @@ The simplest way to "compile" this function would be something like:
.. code:: ipython2
def poswrd(s, e, d):
- return roll_down(*swap(*pop(s, e, d)))
+ return rolldown(*swap(*pop(s, e, d)))
However, internally this function would still be allocating tuples
(stack cells) and doing other unnecesssary work.
@@ -1045,9 +1045,9 @@ from their stack effect comments:
def defs():
- roll_down = (1, 2, 3), (2, 3, 1)
+ rolldown = (1, 2, 3), (2, 3, 1)
- roll_up = (1, 2, 3), (3, 1, 2)
+ rollup = (1, 2, 3), (3, 1, 2)
pop = (1,), ()
@@ -1094,13 +1094,13 @@ from their stack effect comments:
return (a1, stack)
- def roll_down(stack):
+ def rolldown(stack):
"""(1 2 3 -- 2 3 1)"""
(a2, (a1, (a0, stack))) = stack
return (a0, (a2, (a1, stack)))
- def roll_up(stack):
+ def rollup(stack):
"""(1 2 3 -- 3 1 2)"""
(a2, (a1, (a0, stack))) = stack
return (a1, (a0, (a2, stack)))
@@ -1482,9 +1482,9 @@ Rewrite the stack effect comments:
def defs():
- roll_down = (A[1], A[2], A[3]), (A[2], A[3], A[1])
+ rolldown = (A[1], A[2], A[3]), (A[2], A[3], A[1])
- roll_up = (A[1], A[2], A[3]), (A[3], A[1], A[2])
+ rollup = (A[1], A[2], A[3]), (A[3], A[1], A[2])
pop = (A[1],), ()
@@ -1559,8 +1559,8 @@ Rewrite the stack effect comments:
popop = (a2 a1 --)
pred = (n1 -- n2)
rest = ([a1 .1.] -- [.1.])
- roll_down = (a1 a2 a3 -- a2 a3 a1)
- roll_up = (a1 a2 a3 -- a3 a1 a2)
+ rolldown = (a1 a2 a3 -- a2 a3 a1)
+ rollup = (a1 a2 a3 -- a3 a1 a2)
rrest = ([a0 a1 .0.] -- [.0.])
second = ([a0 a1 .0.] -- a1)
sqrt = (n0 -- n1)
@@ -1596,7 +1596,7 @@ Revisit the ``F`` function, works fine.
.. code:: ipython2
- F = reduce(C, (pop, swap, roll_down, rest, rest, cons, cons))
+ F = reduce(C, (pop, swap, rolldown, rest, rest, cons, cons))
F
@@ -1629,7 +1629,7 @@ also get the effect of combinators in some limited cases.
.. code:: ipython2
# e.g. [swap] dip
- neato(roll_up, swap, roll_down)
+ neato(rollup, swap, rolldown)
.. parsed-literal::
@@ -1640,7 +1640,7 @@ also get the effect of combinators in some limited cases.
.. code:: ipython2
# e.g. [popop] dipd
- neato(popdd, roll_down, pop)
+ neato(popdd, rolldown, pop)
.. parsed-literal::
@@ -1651,7 +1651,7 @@ also get the effect of combinators in some limited cases.
.. code:: ipython2
# Reverse the order of the top three items.
- neato(roll_up, swap)
+ neato(rollup, swap)
.. parsed-literal::
@@ -1753,8 +1753,8 @@ comments. We can write a function to check that:
popdd = (a3 a2 a1 -- a2 a1)
popop = (a2 a1 --)
rest = ([a1 .1.] -- [.1.])
- roll_down = (a1 a2 a3 -- a2 a3 a1)
- roll_up = (a1 a2 a3 -- a3 a1 a2)
+ rolldown = (a1 a2 a3 -- a2 a3 a1)
+ rollup = (a1 a2 a3 -- a3 a1 a2)
rrest = ([a0 a1 .0.] -- [.0.])
second = ([a0 a1 .0.] -- a1)
swap = (a1 a2 -- a2 a1)
@@ -1873,13 +1873,10 @@ conversion function instead. This is programmer's laziness.
name: (sequence_to_stack(i), sequence_to_stack(o))
for name, (i, o) in DEFS.iteritems()
}
-
+ NEW_DEFS['stack'] = S[0], (S[0], S[0])
+ NEW_DEFS['swaack'] = (S[1], S[0]), (S[0], S[1])
globals().update(NEW_DEFS)
-.. code:: ipython2
-
- stack = S[0], (S[0], S[0])
-
.. code:: ipython2
C(stack, uncons)
@@ -1984,12 +1981,14 @@ Clunky junk, but it will suffice for now.
popop = (a2 a1 --)
pred = (n1 -- n2)
rest = ([a1 .1.] -- [.1.])
- roll_down = (a1 a2 a3 -- a2 a3 a1)
- roll_up = (a1 a2 a3 -- a3 a1 a2)
+ rolldown = (a1 a2 a3 -- a2 a3 a1)
+ rollup = (a1 a2 a3 -- a3 a1 a2)
rrest = ([a0 a1 .0.] -- [.0.])
second = ([a0 a1 .0.] -- a1)
sqrt = (n0 -- n1)
+ stack = (... -- ... [...])
succ = (n1 -- n2)
+ swaack = ([.1.] -- [.0.])
swap = (a1 a2 -- a2 a1)
swons = ([.0.] a0 -- [a0 .0.])
third = ([a0 a1 a2 .0.] -- a2)
@@ -2686,14 +2685,159 @@ as a Joy interpreter stack, and just execute combinators directly. We
can hybridize the compostition function with an interpreter to evaluate
combinators, compose non-combinator functions, and put type variables on
the stack. For combinators like ``branch`` that can have more than one
-stack effect we have to "split universes" again and return both. (Note:
-bug! If one branch doesn't type check the currect code ignores it, so
-you can think things are okay but have a type error waiting in the faled
-branch, I think... D'oh! FIXME!!!)
+stack effect we have to "split universes" again and return both.
.. code:: ipython2
- stack_concat = lambda q, e: (q[0], stack_concat(q[1], e)) if q else e
+ class FunctionJoyType(AnyJoyType):
+
+ def __init__(self, name, sec, number):
+ self.name = name
+ self.stack_effects = sec
+ self.number = number
+
+ def __add__(self, other):
+ return self
+ __radd__ = __add__
+
+ def __repr__(self):
+ return self.name
+
+
+ class SymbolJoyType(FunctionJoyType): prefix = 'F'
+ class CombinatorJoyType(FunctionJoyType): prefix = 'C'
+
+
+
+
+.. code:: ipython2
+
+ def flatten(g):
+ return list(chain.from_iterable(g))
+
+
+ ID = S[0], S[0] # Identity function.
+
+
+ def infer(e, F=ID):
+ if not e:
+ return [F]
+
+ n, e = e
+
+ if isinstance(n, SymbolJoyType):
+ res = flatten(infer(e, Fn) for Fn in MC([F], n.stack_effects))
+
+ elif isinstance(n, CombinatorJoyType):
+ res = []
+ for combinator in n.stack_effects:
+ fi, fo = F
+ new_fo, ee, _ = combinator(fo, e, {})
+ ee = update(FUNCTIONS, ee) # Fix Symbols.
+ new_F = fi, new_fo
+ res.extend(infer(ee, new_F))
+ else:
+ lit = s9, (n, s9)
+ res = flatten(infer(e, Fn) for Fn in MC([F], [lit]))
+
+ return res
+
+
+
+.. code:: ipython2
+
+ f0, f1, f2, f3, f4, f5, f6, f7, f8, f9 = F = map(FloatJoyType, _R)
+ i0, i1, i2, i3, i4, i5, i6, i7, i8, i9 = I = map(IntJoyType, _R)
+ n0, n1, n2, n3, n4, n5, n6, n7, n8, n9 = N
+ s0, s1, s2, s3, s4, s5, s6, s7, s8, s9 = S
+
+.. code:: ipython2
+
+ import joy.library
+
+ FNs = '''ccons cons divmod_ dup dupd first
+ over pm pop popd popdd popop pred
+ rest rolldown rollup rrest second
+ sqrt stack succ swaack swap swons
+ third tuck uncons'''
+
+ FUNCTIONS = {
+ name: SymbolJoyType(name, [NEW_DEFS[name]], i)
+ for i, name in enumerate(FNs.strip().split())
+ }
+ FUNCTIONS['sum'] = SymbolJoyType('sum', [(((Ns[1], s1), s0), (n0, s0))], 100)
+ FUNCTIONS['mul'] = SymbolJoyType('mul', [
+ ((i2, (i1, s0)), (i3, s0)),
+ ((f2, (i1, s0)), (f3, s0)),
+ ((i2, (f1, s0)), (f3, s0)),
+ ((f2, (f1, s0)), (f3, s0)),
+ ], 101)
+ FUNCTIONS.update({
+ combo.__name__: CombinatorJoyType(combo.__name__, [combo], i)
+ for i, combo in enumerate((
+ joy.library.i,
+ joy.library.dip,
+ joy.library.dipd,
+ joy.library.dipdd,
+ joy.library.dupdip,
+ joy.library.b,
+ joy.library.x,
+ joy.library.infra,
+ ))
+ })
+
+ def branch_true(stack, expression, dictionary):
+ (then, (else_, (flag, stack))) = stack
+ return stack, CONCAT(then, expression), dictionary
+
+ def branch_false(stack, expression, dictionary):
+ (then, (else_, (flag, stack))) = stack
+ return stack, CONCAT(else_, expression), dictionary
+
+ FUNCTIONS['branch'] = CombinatorJoyType('branch', [branch_true, branch_false], 100)
+
+.. code:: ipython2
+
+ globals().update(FUNCTIONS)
+
+.. code:: ipython2
+
+ from itertools import chain
+ from joy.utils.stack import list_to_stack as l2s
+
+.. code:: ipython2
+
+ expression = l2s([n1, n2, (mul, s2), (stack, s3), dip, infra, first])
+
+.. code:: ipython2
+
+ expression
+
+
+
+
+.. parsed-literal::
+
+ (n1, (n2, ((mul, s2), ((stack, s3), (dip, (infra, (first, ())))))))
+
+
+
+.. code:: ipython2
+
+ expression = l2s([n1, n2, mul])
+
+.. code:: ipython2
+
+ infer(expression)
+
+
+
+
+.. parsed-literal::
+
+ []
+
+
.. code:: ipython2
@@ -2770,7 +2914,7 @@ For this to work the type label classes have to be modified to let
.. code:: ipython2
- F = reduce(C, (pop, swap, roll_down, rest, rest, cons, cons))
+ F = reduce(C, (pop, swap, rolldown, rest, rest, cons, cons))
print doc_from_stack_effect(*F)
@@ -2780,22 +2924,22 @@ For this to work the type label classes have to be modified to let
---------------------------------------------------------------------------
- ValueError Traceback (most recent call last)
+ TypeError Traceback (most recent call last)
- in ()
- 1 F = reduce(C, (pop, swap, roll_down, rest, rest, cons, cons))
+ in ()
+ 1 F = reduce(C, (pop, swap, rolldown, rest, rest, cons, cons))
2
----> 3 print doc_from_stack_effect(*F)
- in C(f, g)
+ in C(f, g)
10 def C(f, g):
11 f, g = relabel(f, g)
---> 12 for fg in compose(f, g):
13 yield delabel(fg)
- in compose(f, g)
+ in compose(f, g)
1 def compose(f, g):
----> 2 (f_in, f_out), (g_in, g_out) = f, g
3 s = unify(g_in, f_out)
@@ -2803,14 +2947,14 @@ For this to work the type label classes have to be modified to let
5 raise TypeError('Cannot unify %r and %r.' % (f_out, g_in))
- in C(f, g)
+ in C(f, g)
10 def C(f, g):
11 f, g = relabel(f, g)
---> 12 for fg in compose(f, g):
13 yield delabel(fg)
- in compose(f, g)
+ in compose(f, g)
1 def compose(f, g):
----> 2 (f_in, f_out), (g_in, g_out) = f, g
3 s = unify(g_in, f_out)
@@ -2818,14 +2962,14 @@ For this to work the type label classes have to be modified to let
5 raise TypeError('Cannot unify %r and %r.' % (f_out, g_in))
- in C(f, g)
+ in C(f, g)
10 def C(f, g):
11 f, g = relabel(f, g)
---> 12 for fg in compose(f, g):
13 yield delabel(fg)
- in compose(f, g)
+ in compose(f, g)
1 def compose(f, g):
----> 2 (f_in, f_out), (g_in, g_out) = f, g
3 s = unify(g_in, f_out)
@@ -2833,14 +2977,14 @@ For this to work the type label classes have to be modified to let
5 raise TypeError('Cannot unify %r and %r.' % (f_out, g_in))
- in C(f, g)
+ in C(f, g)
10 def C(f, g):
11 f, g = relabel(f, g)
---> 12 for fg in compose(f, g):
13 yield delabel(fg)
- in compose(f, g)
+