Minor cleanup.

Renamed update() to reify() and reinstated recursive substitution.
This commit is contained in:
Simon Forman
2018-07-04 10:37:43 -07:00
parent 4fff827338
commit ab8ec6c95d
4 changed files with 46 additions and 44 deletions
+10 -7
View File
@@ -34,7 +34,7 @@ from joy.utils.types import (
_stacky,
_R,
relabel, delabel,
update,
reify,
)
@@ -155,12 +155,12 @@ class CombinatorJoyType(FunctionJoyType):
def _log_uni(U):
def inner(u, v, s=None):
_log.info(
_log.debug(
'%3i %s U %s w/ %s',
len(inspect_stack()), u, v, s,
)
res = U(u, v, s)
_log.info(
_log.debug(
'%3i %s U %s w/ %s => %s',
len(inspect_stack()), u, v, s, res,
)
@@ -176,8 +176,8 @@ def unify(u, v, s=None):
if s is None:
s = {}
elif s:
u = update(s, u)
v = update(s, v)
u = reify(s, u)
v = reify(s, v)
if u == v:
res = s,
@@ -243,7 +243,7 @@ def _lil_uni(u, v, s):
def _compose(f, g, e):
(f_in, f_out), (g_in, g_out) = f, g
for s in unify(g_in, f_out):
yield update(s, (e, (f_in, g_out)))
yield reify(s, (e, (f_in, g_out)))
def compose(f, g, e):
@@ -311,7 +311,7 @@ def _infer(e, F=ID):
def _interpret(f, fi, fo, e):
new_fo, ee, _ = f(fo, e, {})
ee = update(FUNCTIONS, ee) # Fix Symbols.
ee = reify(FUNCTIONS, ee) # Fix Symbols.
new_F = fi, new_fo
return _infer(ee, new_F)
@@ -376,6 +376,8 @@ def defs():
sum_ = product = [(((Ns[1], s1), s0), (n0, s0))]
clear = [((As[1], s0), s1)]
add = mul = sub = floordiv = modulus = [
((i2, (i1, s0)), (i3, s0)),
((f2, (i1, s0)), (f3, s0)),
@@ -466,6 +468,7 @@ def set_expectations():
scope = globals().copy()
scope.update(FUNCTIONS)
eval(set_expectations.func_code, scope)
del scope
# Type Checking...
+20 -24
View File
@@ -1,6 +1,7 @@
from collections import Counter
from itertools import imap
from joy.utils.stack import concat
from joy.parser import Symbol
class AnyJoyType(object):
@@ -53,27 +54,19 @@ class StackJoyType(AnyJoyType):
class JoyTypeError(Exception): pass
def update(s, term):
'''
Apply substitution dict to term, returning new term.
'''
t = _update(s, term)
n = 10
while t != term:
n -= 1
if not n:
print t
print term
print 'lalala'
1/0
term = t
t = _update(s, term)
return term
def _update(s, term):
if not isinstance(term, tuple):
return s.get(term, term)
return tuple(_update(s, inner) for inner in term)
def reify(meaning, name, seen=None):
'''
Apply substitution dict to term, returning new term.
'''
if isinstance(name, tuple):
return tuple(reify(meaning, inner) for inner in name)
safety = 101
while name in meaning and safety:
safety -= 1
name = meaning[name]
if not safety:
raise ValueError('Cycle in substitution dict: %s' % (meaning,))
return name
def relabel(left, right):
@@ -121,8 +114,8 @@ def unify(u, v, s=None):
if s is None:
s = {}
elif s:
u = update(s, u)
v = update(s, v)
u = reify(s, u)
v = reify(s, v)
if isinstance(u, AnyJoyType) and isinstance(v, AnyJoyType):
if u >= v:
@@ -164,7 +157,7 @@ def _compose(f, g):
'''
# Relabel, unify, update, delabel.
(f_in, f_out), (g_in, g_out) = relabel(f, g)
fg = update(unify(g_in, f_out), (f_in, g_out))
fg = reify(unify(g_in, f_out), (f_in, g_out))
return delabel(fg)
@@ -273,6 +266,8 @@ def defs():
'''
Return a dict of named stack effects.
'''
at = __(s0, i1), __(a1)
drop = take = __(s0, i1), __(s1)
cons = __(a1, s0), __((a1, s0),)
ccons = compose(cons, cons)
dup = __(a1,), __(a1, a1)
@@ -316,6 +311,7 @@ def defs():
first_two = compose(uncons, uncons, pop)
fourth = compose(rest, third)
of = compose(swap, at)
_Tree_add_Ee = compose(pop, swap, rolldown, rrest, ccons)
_Tree_get_E = compose(popop, second)