|
| 1 | +#!/usr/bin/python3 |
| 2 | + |
| 3 | +"""Tests for pair terms""" |
| 4 | + |
| 5 | +from unittest import TestCase |
| 6 | +from lambda_calculus.visitors.normalisation import BetaNormalisingVisitor |
| 7 | +from lambda_calculus.terms import Term, Variable, Application, pairs |
| 8 | +from lambda_calculus.terms.logic import TRUE, FALSE |
| 9 | + |
| 10 | + |
| 11 | +class PairTest(TestCase): |
| 12 | + """Test for pair terms""" |
| 13 | + |
| 14 | + visitor: BetaNormalisingVisitor |
| 15 | + |
| 16 | + def setUp(self) -> None: |
| 17 | + """create a visitor""" |
| 18 | + self.visitor = BetaNormalisingVisitor() |
| 19 | + |
| 20 | + def make_pair(self, a: Term[str], b: Term[str]) -> Term[str]: |
| 21 | + """make a pair (a, b)""" |
| 22 | + return Application.with_arguments(pairs.PAIR, (a, b)) |
| 23 | + |
| 24 | + def test_first(self) -> None: |
| 25 | + """test accessing the first element""" |
| 26 | + self.assertEqual( |
| 27 | + self.visitor.skip_intermediate( |
| 28 | + Application( |
| 29 | + pairs.FIRST, |
| 30 | + self.make_pair(Variable("a"), Variable("b")) |
| 31 | + ) |
| 32 | + ), |
| 33 | + Variable("a") |
| 34 | + ) |
| 35 | + |
| 36 | + def test_second(self) -> None: |
| 37 | + """test accessing rhe second element""" |
| 38 | + self.assertEqual( |
| 39 | + self.visitor.skip_intermediate( |
| 40 | + Application( |
| 41 | + pairs.SECOND, |
| 42 | + self.make_pair(Variable("a"), Variable("b")) |
| 43 | + ) |
| 44 | + ), |
| 45 | + Variable("b") |
| 46 | + ) |
| 47 | + |
| 48 | + def test_null(self) -> None: |
| 49 | + """test check for NIL pair""" |
| 50 | + self.assertEqual( |
| 51 | + self.visitor.skip_intermediate( |
| 52 | + Application(pairs.NULL, pairs.NIL) |
| 53 | + ), |
| 54 | + TRUE |
| 55 | + ) |
| 56 | + self.assertEqual( |
| 57 | + self.visitor.skip_intermediate( |
| 58 | + Application( |
| 59 | + pairs.NULL, |
| 60 | + self.make_pair(Variable("a"), Variable("b")) |
| 61 | + ) |
| 62 | + ), |
| 63 | + FALSE |
| 64 | + ) |
0 commit comments