Type checking e.g. sum or product.
Any type accepts complex numbers.
Lots of the math functions now just use Number rather than more specific
poly-types.
This commit is contained in:
Simon Forman 2018-07-15 20:37:52 -07:00
parent c4af5682c4
commit cf12b9ce17
2 changed files with 40 additions and 20 deletions

View File

@ -185,7 +185,10 @@ def unify(u, v, s=None):
elif isinstance(u, tuple) and isinstance(v, tuple): elif isinstance(u, tuple) and isinstance(v, tuple):
if len(u) != 2 or len(v) != 2: if len(u) != 2 or len(v) != 2:
if _that_one_special_case(u, v):
return s,
raise ValueError(repr((u, v))) # Bad input. raise ValueError(repr((u, v))) # Bad input.
(a, b), (c, d) = v, u (a, b), (c, d) = v, u
if isinstance(a, KleeneStar): if isinstance(a, KleeneStar):
@ -231,6 +234,18 @@ def unify(u, v, s=None):
return res return res
def _that_one_special_case(u, v):
'''
Handle e.g. ((), (n1*, s1)) when type-checking sum, product, etc...
'''
return (
u == ()
and len(v) == 2
and isinstance(v[0], KleeneStar)
and isinstance(v[1], StackJoyType)
)
def _lil_uni(u, v, s): def _lil_uni(u, v, s):
if u >= v: if u >= v:
s[u] = v s[u] = v
@ -387,6 +402,8 @@ FUNCTIONS = {
stack swaack swap swons third tuck uncons unswons stuncons stack swaack swap swons third tuck uncons unswons stuncons
stununcons unit eq ge gt le lt ne and bool not stununcons unit eq ge gt le lt ne and bool not
_Tree_add_Ee _Tree_delete_R0 _Tree_delete_clear_stuff _Tree_get_E _Tree_add_Ee _Tree_delete_R0 _Tree_delete_clear_stuff _Tree_get_E
add mul sub floordiv modulus div truediv pow
neg pred succ
'''.strip().split()) '''.strip().split())
} }
'''Docstring for functions in Sphinx?''' '''Docstring for functions in Sphinx?'''
@ -401,32 +418,32 @@ def defs():
clear = [(s0, s1)] clear = [(s0, s1)]
add = mul = sub = floordiv = modulus = [ ## add = mul = sub = floordiv = modulus = [
((i2, (i1, s0)), (i3, s0)), ## ((i2, (i1, s0)), (i3, s0)),
((f2, (i1, s0)), (f3, s0)), ## ((f2, (i1, s0)), (f3, s0)),
((i2, (f1, s0)), (f3, s0)), ## ((i2, (f1, s0)), (f3, s0)),
((f2, (f1, s0)), (f3, s0)), ## ((f2, (f1, s0)), (f3, s0)),
] ## ]
div = truediv = pow_ = [ ## div = truediv = pow_ = [
((i2, (i1, s0)), (f3, s0)), ## ((i2, (i1, s0)), (f3, s0)),
((f2, (i1, s0)), (f3, s0)), ## ((f2, (i1, s0)), (f3, s0)),
((i2, (f1, s0)), (f3, s0)), ## ((i2, (f1, s0)), (f3, s0)),
((f2, (f1, s0)), (f3, s0)), ## ((f2, (f1, s0)), (f3, s0)),
] ## ]
lshift = rshift = [((i2, (i1, s0)), (i3, s0))] lshift = rshift = [((i2, (i1, s0)), (i3, s0))]
neg = pred = succ = [((n1, s0), (n2, s0))] ## neg = pred = succ = [((n1, s0), (n2, s0))]
sqrt = [((n1, s0), (f2, s0))] sqrt = [((n1, s0), (f2, s0))]
pm = divmod_ = [ ## pm = divmod_ = [
((i2, (i1, s0)), (i3, (i4, s0))), ## ((i2, (i1, s0)), (i3, (i4, s0))),
((f2, (i1, s0)), (f3, (f4, s0))), ## ((f2, (i1, s0)), (f3, (f4, s0))),
((i2, (f1, s0)), (f3, (f4, s0))), ## ((i2, (f1, s0)), (f3, (f4, s0))),
((f2, (f1, s0)), (f3, (f4, s0))), ## ((f2, (f1, s0)), (f3, (f4, s0))),
] ## ]
return { return {
name.rstrip('_'): stack_effect name.rstrip('_'): stack_effect
@ -480,6 +497,9 @@ def loop_false(stack, expression, dictionary):
FUNCTIONS['loop'] = CombinatorJoyType('loop', [loop_two_true, loop_true, loop_false], 101) FUNCTIONS['loop'] = CombinatorJoyType('loop', [loop_two_true, loop_true, loop_false], 101)
joy.library.add_aliases(FUNCTIONS, joy.library.ALIASES)
def set_expectations(): def set_expectations():
branch.expect = s7, (s6, (b1, s5)) branch.expect = s7, (s6, (b1, s5))
loop.expect = s6, (b1, s5) loop.expect = s6, (b1, s5)

View File

@ -9,7 +9,7 @@ class AnyJoyType(object):
Joy type variable. Represents any Joy value. Joy type variable. Represents any Joy value.
''' '''
accept = tuple, int, float, long, str, unicode, bool, Symbol accept = tuple, int, float, long, complex, str, unicode, bool, Symbol
prefix = 'a' prefix = 'a'
def __init__(self, number): def __init__(self, number):