|
| 1 | +#!/usr/bin/python3 |
| 2 | + |
| 3 | +"""Tests for combinator terms""" |
| 4 | + |
| 5 | +from unittest import TestCase |
| 6 | +from lambda_calculus.terms import Variable, combinators |
| 7 | +from lambda_calculus.visitors.normalisation import BetaNormalisingVisitor |
| 8 | + |
| 9 | + |
| 10 | +class CombinatorTest(TestCase): |
| 11 | + """Tests for combinator terms""" |
| 12 | + |
| 13 | + def test_is_combinator(self) -> None: |
| 14 | + """test that all terms a combinators""" |
| 15 | + for name in combinators.__all__: |
| 16 | + self.assertTrue(getattr(combinators, name).is_combinator()) |
| 17 | + |
| 18 | + def test_y(self) -> None: |
| 19 | + """test Y combinator""" |
| 20 | + self.assertEqual( |
| 21 | + BetaNormalisingVisitor().skip_intermediate( |
| 22 | + combinators.Y.apply_to(combinators.K.apply_to(Variable("a"))) |
| 23 | + ), |
| 24 | + Variable("a") |
| 25 | + ) |
| 26 | + |
| 27 | + def test_s(self) -> None: |
| 28 | + """test S combinator""" |
| 29 | + term = Variable("a") |
| 30 | + self.assertEqual( |
| 31 | + BetaNormalisingVisitor().skip_intermediate( |
| 32 | + combinators.S.apply_to(combinators.K, Variable("b"), term) |
| 33 | + ), |
| 34 | + term |
| 35 | + ) |
| 36 | + |
| 37 | + def test_k(self) -> None: |
| 38 | + """test K combinator""" |
| 39 | + term = Variable("a") |
| 40 | + self.assertEqual( |
| 41 | + BetaNormalisingVisitor().skip_intermediate( |
| 42 | + combinators.K.apply_to(term, Variable("b")) |
| 43 | + ), |
| 44 | + term |
| 45 | + ) |
| 46 | + |
| 47 | + def test_i(self) -> None: |
| 48 | + """test I combinator""" |
| 49 | + term = Variable("a") |
| 50 | + self.assertEqual( |
| 51 | + BetaNormalisingVisitor().skip_intermediate( |
| 52 | + combinators.I.apply_to(term) |
| 53 | + ), |
| 54 | + term |
| 55 | + ) |
| 56 | + |
| 57 | + def test_b(self) -> None: |
| 58 | + """test B combinator""" |
| 59 | + self.assertEqual( |
| 60 | + BetaNormalisingVisitor().skip_intermediate( |
| 61 | + combinators.B.apply_to( |
| 62 | + combinators.K.apply_to(Variable("b")), |
| 63 | + combinators.K.apply_to(Variable("c")), |
| 64 | + Variable("a") |
| 65 | + ) |
| 66 | + ), |
| 67 | + Variable("b") |
| 68 | + ) |
| 69 | + |
| 70 | + def test_c(self) -> None: |
| 71 | + """test C combinator""" |
| 72 | + self.assertEqual( |
| 73 | + BetaNormalisingVisitor().skip_intermediate( |
| 74 | + combinators.C.apply_to(combinators.I, Variable("a"), Variable("b")) |
| 75 | + ), |
| 76 | + Variable("b").apply_to(Variable("a")) |
| 77 | + ) |
| 78 | + |
| 79 | + def test_w(self) -> None: |
| 80 | + """test W combinator""" |
| 81 | + self.assertEqual( |
| 82 | + BetaNormalisingVisitor().skip_intermediate( |
| 83 | + combinators.W.apply_to(combinators.I, Variable("a")) |
| 84 | + ), |
| 85 | + Variable("a").apply_to(Variable("a")) |
| 86 | + ) |
| 87 | + |
| 88 | + def test_omega(self) -> None: |
| 89 | + """test Omega combinator""" |
| 90 | + for index, (_, step) in enumerate(BetaNormalisingVisitor().visit(combinators.OMEGA)): |
| 91 | + if index < 10: |
| 92 | + self.assertEqual(step, combinators.OMEGA) |
| 93 | + else: |
| 94 | + break |
| 95 | + else: |
| 96 | + self.fail("reached beta normal form") |
0 commit comments