|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +"""Test the Pytkell dialect.""" |
| 3 | + |
| 4 | +# from mcpyrate.debug import dialects, StepExpansion |
| 5 | +from ...dialects import dialects, Pytkell # noqa: F401 |
| 6 | + |
| 7 | +from ...syntax import macros, continuations, call_cc, tco # noqa: F401 |
| 8 | +from ...misc import timer |
| 9 | + |
| 10 | +from types import FunctionType |
| 11 | +from operator import add, mul |
| 12 | + |
| 13 | +# TODO: use the test framework |
| 14 | + |
| 15 | +def runtests(): |
| 16 | + print(f"Hello from {__lang__}!") # noqa: F821, the dialect template defines it. |
| 17 | + |
| 18 | + # function definitions (both def and lambda) and calls are auto-curried |
| 19 | + def add3(a, b, c): |
| 20 | + return a + b + c |
| 21 | + |
| 22 | + a = add3(1) |
| 23 | + assert isinstance(a, FunctionType) |
| 24 | + a = a(2) |
| 25 | + assert isinstance(a, FunctionType) |
| 26 | + a = a(3) |
| 27 | + assert isinstance(a, int) |
| 28 | + |
| 29 | + # actually partial evaluation so any of these works |
| 30 | + assert add3(1)(2)(3) == 6 |
| 31 | + assert add3(1, 2)(3) == 6 |
| 32 | + assert add3(1)(2, 3) == 6 |
| 33 | + assert add3(1, 2, 3) == 6 |
| 34 | + |
| 35 | + # arguments of a function call are auto-lazified (converted to promises, MacroPy lazy[]) |
| 36 | + def addfirst2(a, b, c): |
| 37 | + # a and b are read, so their promises are forced |
| 38 | + # c is not used, so not evaluated either |
| 39 | + return a + b |
| 40 | + assert addfirst2(1)(2)(1 / 0) == 3 |
| 41 | + |
| 42 | + # let-bindings are auto-lazified |
| 43 | + x = let[((x, 42), # noqa: F821 |
| 44 | + (y, 1 / 0)) in x] # noqa: F821 |
| 45 | + assert x == 42 |
| 46 | + |
| 47 | + # assignments are not (because they can imperatively update existing names) |
| 48 | + try: |
| 49 | + a = 1 / 0 |
| 50 | + except ZeroDivisionError: |
| 51 | + pass |
| 52 | + else: |
| 53 | + assert False, "expected a zero division error" |
| 54 | + |
| 55 | + # so if you want that, use lazy[] manually (it's a builtin in Pytkell) |
| 56 | + a = lazy[1 / 0] # this blows up only when the value is read (name 'a' in Load context) # noqa: F821 |
| 57 | + |
| 58 | + # manually lazify items in a data structure literal, recursively (see unpythonic.syntax.lazyrec): |
| 59 | + a = lazyrec[(1, 2, 3 / 0)] # noqa: F821 |
| 60 | + assert a[:-1] == (1, 2) # reading a slice forces only that slice |
| 61 | + |
| 62 | + # laziness passes through |
| 63 | + def g(a, b): |
| 64 | + return a # b not used |
| 65 | + def f(a, b): |
| 66 | + return g(a, b) # b is passed along, but its value is not used |
| 67 | + assert f(42, 1 / 0) == 42 |
| 68 | + |
| 69 | + def f(a, b): |
| 70 | + return (a, b) |
| 71 | + assert f(1, 2) == (1, 2) |
| 72 | + assert (flip(f))(1, 2) == (2, 1) # NOTE flip reverses all (doesn't just flip the first two) # noqa: F821 |
| 73 | + |
| 74 | +# # TODO: this doesn't work, because curry sees f's arities as (2, 2) (kwarg handling!) |
| 75 | +# assert (flip(f))(1, b=2) == (1, 2) # b -> kwargs |
| 76 | + |
| 77 | + # http://www.cse.chalmers.se/~rjmh/Papers/whyfp.html |
| 78 | + my_sum = foldl(add, 0) # noqa: F821 |
| 79 | + my_prod = foldl(mul, 1) # noqa: F821 |
| 80 | + my_map = lambda f: foldr(compose(cons, f), nil) # compose is unpythonic.fun.composerc # noqa: F821 |
| 81 | + |
| 82 | + assert my_sum(range(1, 5)) == 10 |
| 83 | + assert my_prod(range(1, 5)) == 24 |
| 84 | + assert tuple(my_map((lambda x: 2 * x), (1, 2, 3))) == (2, 4, 6) |
| 85 | + |
| 86 | + assert tuple(scanl(add, 0, (1, 2, 3))) == (0, 1, 3, 6) # noqa: F821 |
| 87 | + assert tuple(scanr(add, 0, (1, 2, 3))) == (0, 3, 5, 6) # NOTE output ordering different from Haskell # noqa: F821 |
| 88 | + |
| 89 | + # let-in |
| 90 | + x = let[(a, 21) in 2 * a] # noqa: F821 |
| 91 | + assert x == 42 |
| 92 | + |
| 93 | + x = let[((a, 21), # noqa: F821 |
| 94 | + (b, 17)) in # noqa: F821 |
| 95 | + 2 * a + b] # noqa: F821 |
| 96 | + assert x == 59 |
| 97 | + |
| 98 | + # let-where |
| 99 | + x = let[2 * a, where(a, 21)] # noqa: F821 |
| 100 | + assert x == 42 |
| 101 | + |
| 102 | + x = let[2 * a + b, # noqa: F821 |
| 103 | + where((a, 21), # noqa: F821 |
| 104 | + (b, 17))] # noqa: F821 |
| 105 | + assert x == 59 |
| 106 | + |
| 107 | + # nondeterministic evaluation (essentially do-notation in the List monad) |
| 108 | + # |
| 109 | + # pythagorean triples |
| 110 | + pt = forall[z << range(1, 21), # hypotenuse # noqa: F821 |
| 111 | + x << range(1, z + 1), # shorter leg # noqa: F821 |
| 112 | + y << range(x, z + 1), # longer leg # noqa: F821 |
| 113 | + insist(x * x + y * y == z * z), # see also deny() # noqa: F821 |
| 114 | + (x, y, z)] # noqa: F821 |
| 115 | + assert tuple(sorted(pt)) == ((3, 4, 5), (5, 12, 13), (6, 8, 10), |
| 116 | + (8, 15, 17), (9, 12, 15), (12, 16, 20)) |
| 117 | + |
| 118 | + # functional update for sequences |
| 119 | + # |
| 120 | + tup1 = (1, 2, 3, 4, 5) |
| 121 | + tup2 = fup(tup1)[2:] << (10, 20, 30) # fup(sequence)[idx_or_slice] << sequence_of_values # noqa: F821 |
| 122 | + assert tup2 == (1, 2, 10, 20, 30) |
| 123 | + assert tup1 == (1, 2, 3, 4, 5) |
| 124 | + |
| 125 | + # immutable dict, with functional update |
| 126 | + # |
| 127 | + d1 = frozendict(foo='bar', bar='tavern') # noqa: F821 |
| 128 | + d2 = frozendict(d1, bar='pub') # noqa: F821 |
| 129 | + assert tuple(sorted(d1.items())) == (('bar', 'tavern'), ('foo', 'bar')) |
| 130 | + assert tuple(sorted(d2.items())) == (('bar', 'pub'), ('foo', 'bar')) |
| 131 | + |
| 132 | + # s = mathematical Sequence (const, arithmetic, geometric, power) |
| 133 | + # |
| 134 | + assert last(take(10000, s(1, ...))) == 1 # noqa: F821 |
| 135 | + assert last(take(5, s(0, 1, ...))) == 4 # noqa: F821 |
| 136 | + assert last(take(5, s(1, 2, 4, ...))) == (1 * 2 * 2 * 2 * 2) # 16 # noqa: F821 |
| 137 | + assert last(take(5, s(2, 4, 16, ...))) == (((((2)**2)**2)**2)**2) # 65536 # noqa: F821 |
| 138 | + |
| 139 | + # s() takes care to avoid roundoff |
| 140 | + assert last(take(1001, s(0, 0.001, ...))) == 1 # noqa: F821 |
| 141 | + |
| 142 | + # iterables returned by s() support infix math |
| 143 | + # (to add infix math support to some other iterable, m(iterable)) |
| 144 | + c = s(1, 3, ...) + s(2, 4, ...) # noqa: F821 |
| 145 | + assert tuple(take(5, c)) == (3, 7, 11, 15, 19) # noqa: F821 |
| 146 | + assert tuple(take(5, c)) == (23, 27, 31, 35, 39) # consumed! # noqa: F821 |
| 147 | + |
| 148 | + # imemoize = memoize Iterable (makes a gfunc, drops math support) |
| 149 | + # gmathify returns a new gfunc that adds infix math support |
| 150 | + # to generators the original gfunc makes. |
| 151 | + # |
| 152 | + # see also gmemoize, fimemoize in unpythonic |
| 153 | + # |
| 154 | + mi = lambda x: gmathify(imemoize(x)) # noqa: F821 |
| 155 | + a = mi(s(1, 3, ...)) # noqa: F821 |
| 156 | + b = mi(s(2, 4, ...)) # noqa: F821 |
| 157 | + c = lambda: a() + b() |
| 158 | + assert tuple(take(5, c())) == (3, 7, 11, 15, 19) # noqa: F821 |
| 159 | + assert tuple(take(5, c())) == (3, 7, 11, 15, 19) # now it's a new instance; no recomputation # noqa: F821 |
| 160 | + |
| 161 | + factorials = mi(scanl(mul, 1, s(1, 2, ...))) # 0!, 1!, 2!, ... # noqa: F821 |
| 162 | + assert last(take(6, factorials())) == 120 # noqa: F821 |
| 163 | + assert first(drop(5, factorials())) == 120 # noqa: F821 |
| 164 | + |
| 165 | + squares = s(1, 2, ...)**2 # noqa: F821 |
| 166 | + assert last(take(10, squares)) == 100 # noqa: F821 |
| 167 | + |
| 168 | + harmonic = 1 / s(1, 2, ...) # noqa: F821 |
| 169 | + assert last(take(10, harmonic)) == 1 / 10 # noqa: F821 |
| 170 | + |
| 171 | + # unpythonic's continuations are supported |
| 172 | + with continuations: |
| 173 | + k = None # kontinuation |
| 174 | + def setk(*args, cc): |
| 175 | + nonlocal k |
| 176 | + k = cc # current continuation, i.e. where to go after setk() finishes |
| 177 | + return args # tuple means multiple-return-values |
| 178 | + def doit(): |
| 179 | + lst = ['the call returned'] |
| 180 | + *more, = call_cc[setk('A')] |
| 181 | + return lst + list(more) |
| 182 | + assert doit() == ['the call returned', 'A'] |
| 183 | + # We can now send stuff into k, as long as it conforms to the |
| 184 | + # signature of the assignment targets of the "call_cc". |
| 185 | + assert k('again') == ['the call returned', 'again'] |
| 186 | + assert k('thrice', '!') == ['the call returned', 'thrice', '!'] |
| 187 | + |
| 188 | + # as is unpythonic's tco |
| 189 | + with tco: |
| 190 | + def fact(n): |
| 191 | + def f(k, acc): |
| 192 | + if k == 1: |
| 193 | + return acc |
| 194 | + return f(k - 1, k * acc) |
| 195 | + return f(n, 1) # TODO: doesn't work as f(n, acc=1) due to curry's kwarg handling |
| 196 | + assert fact(4) == 24 |
| 197 | + print("Performance...") |
| 198 | + with timer() as tictoc: |
| 199 | + fact(5000) # no crash, but Pytkell is a bit slow |
| 200 | + print(" Time taken for factorial of 5000: {:g}s".format(tictoc.dt)) |
| 201 | + |
| 202 | + print("All tests PASSED") |
| 203 | + |
| 204 | +if __name__ == '__main__': |
| 205 | + runtests() |
0 commit comments