diff options
author | Ian Johnson <ian.johnson@appliedlanguage.com> | 2013-08-27 18:03:11 +0400 |
---|---|---|
committer | Ian Johnson <ian.johnson@appliedlanguage.com> | 2013-08-27 18:03:11 +0400 |
commit | 7d13508d6f2e730d65b6ae8708a0684dd1617b0c (patch) | |
tree | b00e944559deaa365722f61e9b06930092918fde | |
parent | 8e366c3579c6e8a1d749b5a6dd7fa5afd4bb452e (diff) |
Continuation monad implemented
-rw-r--r-- | src/pypeline/core/arrows/tests/function_arrow_tests.py | 95 | ||||
-rw-r--r-- | src/pypeline/core/types/cont.py | 8 | ||||
-rw-r--r-- | src/pypeline/core/types/tests/cont_tests.py | 7 |
3 files changed, 73 insertions, 37 deletions
diff --git a/src/pypeline/core/arrows/tests/function_arrow_tests.py b/src/pypeline/core/arrows/tests/function_arrow_tests.py index c5184ba..b38e33c 100644 --- a/src/pypeline/core/arrows/tests/function_arrow_tests.py +++ b/src/pypeline/core/arrows/tests/function_arrow_tests.py @@ -26,7 +26,7 @@ class ArrowUnitTests(unittest.TestCase): # Arrow law tests # - # Identity + # arr id = id def test_identity(self): iden = lambda x: x arrow = FunctionArrow(iden) @@ -48,6 +48,69 @@ class ArrowUnitTests(unittest.TestCase): self.assertEquals(arrow_func_comp(value), arrow_comp(value)) + # first (arr f) = arr (first f) + def first_arr_f_is_arr_first_f(self): + f = lambda x: x + 1 + fst = lambda x: x[0] + 1 + + arrow_one = FunctionArrow(f).first() + arrow_two = FunctionArrow(fst) + + value = (8, -10) + self.assertEquals(arrow_one(value), arrow_two(value)) + + + # first (a >>> b) = first a >>> first b + def test_first_a_b_is_first_a_first_b(self): + a = lambda x: x * 9 + b = lambda x: x - 7 + c = lambda x: b(a(x)) + + arrow_one = FunctionArrow(c).first() + arrow_two = FunctionArrow(a).first() >> FunctionArrow(b).first() + + value = (7, 21) + self.assertEquals(arrow_one(value), arrow_two(value)) + + + # first f >>> arr fst = arr fst >>> f + def test_first_arrow_fst_is_arrow_fst_func(self): + fst = lambda x: x[0] + f = lambda x: x * -9 + + arrow_one = FunctionArrow(f).first() >> FunctionArrow(fst) + arrow_two = FunctionArrow(fst) >> FunctionArrow(f) + + value = (-3, 19) + self.assertEquals(arrow_one(value), arrow_two(value)) + + + # first f >>> arr (id *** g) = arr (id *** g) >>> first f + def first_f_arr_id_g_is_arr_id_g_first_f(self): + id = lambda x: x + f = lambda x: x * -3 + g = lambda x: x - 9 + + arrow_one = FunctionArrow(f).first() >> (FunctionArrow(id) * FunctionArrow(g)) + arrow_two = (FunctionArrow(id) * FunctionArrow(g)) >> FunctionArrow(f).first() + + value = (-9, 8) + self.assertEquals(arrow_one(value), arrow_two(value)) + + + # first (first f) >>> arr assoc = arr assoc >>> first f + # where assoc((a, b), c) = (a, (b, c)) + def first_first_f_arr_assoc_is_arr_assoc_first_f(self): + assoc = lambda ab, c: (ab[0], (ab[1], c)) + f = lambda x: x + 3 + + arrow_one = FunctionArrow(f).first().first() >> FunctionArrow(assoc) + arrow_two = FunctionArrow(assoc) >> FunctionArrow(f).first() + + value = ((1, 2), 3) + self.assertEquals(arrow_one(value), arrow_two(value)) + + # arr id >>> a = a = a >>> arr id def test_arrow_id_func_is_func_is_func_comp_arrow(self): a = lambda x: x + 1 @@ -61,12 +124,6 @@ class ArrowUnitTests(unittest.TestCase): self.assertEquals(arrow_two(value), a(value)) - # first a >>> arr pi_1 = arr pi_1 >>> a - def test_first_arrow_pi_is_arrow_pi_func(self): - # Not sure what pi_1 is in this law - pass - - # first a >>> arr (id x f) = arr (id x f) >>> first a # first a >>> second f = second f >>> first a def test_first_arr_id_f_is_arr_id_f_first(self): @@ -80,30 +137,6 @@ class ArrowUnitTests(unittest.TestCase): self.assertEquals(arrow_one(value), arrow_two(value)) - # first a >>> arr alpha = arr alpha >>> first (first a) - def test_first_a_comp_arr_alpha_is_arr_alpha_first_first_a(self): - # not sure what alpha is in this law - pass - - - # first (arr f) = arr (f x id) - # first f = arr (first f) - # first f = first f - - - # first (a >>> b) = first a >>> first b - def test_first_a_b_is_first_a_first_b(self): - a = lambda x: x * 9 - b = lambda x: x - 7 - c = lambda x: b(a(x)) - - arrow_one = FunctionArrow(c).first() - arrow_two = FunctionArrow(a).first() >> FunctionArrow(b).first() - - value = (7, 21) - self.assertEquals(arrow_one(value), arrow_two(value)) - - # # Ad hoc tests # diff --git a/src/pypeline/core/types/cont.py b/src/pypeline/core/types/cont.py index 056414d..91614f5 100644 --- a/src/pypeline/core/types/cont.py +++ b/src/pypeline/core/types/cont.py @@ -63,5 +63,9 @@ def callCC(f): type(f) is not types.MethodType: raise ValueError("Must be a function or method") - cont = lambda k: Cont.runCont(f(lambda a: Cont(lambda: k(a)), k)) - return Cont(cont) + def function(k): + def function_arg(a): + return Cont(lambda x: k(a)) + return Cont.runCont(f(function_arg), k) + + return Cont(function) diff --git a/src/pypeline/core/types/tests/cont_tests.py b/src/pypeline/core/types/tests/cont_tests.py index 48edfe6..13ad05f 100644 --- a/src/pypeline/core/types/tests/cont_tests.py +++ b/src/pypeline/core/types/tests/cont_tests.py @@ -59,8 +59,7 @@ class ContMonadUnitTest(unittest.TestCase): return callCC(lambda ok: callCC(lambda not_ok: not_ok("Divide by zero error") if y is 0 else ok(x / y)) >= (lambda err: k(err))) - error = lambda err: sys.stderr.write(err + os.linesep) + error = lambda err: Cont(lambda _: sys.stderr.write(err + os.linesep)) - print Cont.runCont(divide_cps(10, 2, error), lambda x: x) - print Cont.runCont(divide_cps(10, 0, error), lambda x: x) - self.assertTrue(False) + self.assertEquals(3, Cont.runCont(divide_cps(10, 3, error), lambda x: x)) + self.assertEquals(None, Cont.runCont(divide_cps(10, 0, error), lambda x: x)) |