Skip to content

Commit 96b06d8

Browse files
committed
add lambda terms for pairs
1 parent 8d6776a commit 96b06d8

File tree

5 files changed

+107
-3
lines changed

5 files changed

+107
-3
lines changed

lambda_calculus/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from .terms import Variable, Abstraction, Application
66

7-
__version__ = "1.9.0"
7+
__version__ = "1.10.0"
88
__author__ = "Eric Niklas Wolf"
99
__email__ = "[email protected]"
1010
__all__ = (

lambda_calculus/terms/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
"Abstraction",
1818
"Application",
1919
"arithmetic",
20-
"logic"
20+
"logic",
21+
"pairs"
2122
)
2223

2324
T = TypeVar("T")

lambda_calculus/terms/pairs.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/python3
2+
3+
"""Implementation of pairs"""
4+
5+
from . import Variable, Abstraction, Application
6+
from .logic import TRUE, FALSE
7+
8+
__all__ = (
9+
"PAIR",
10+
"FIRST",
11+
"SECOND",
12+
"NIL",
13+
"NULL"
14+
)
15+
16+
PAIR = Abstraction.curried(
17+
("x", "y", "f"),
18+
Application.with_arguments(
19+
Variable("f"),
20+
(Variable("x"), Variable("y"))
21+
)
22+
)
23+
24+
FIRST = Abstraction("p", Application(Variable("p"), TRUE))
25+
26+
SECOND = Abstraction("p", Application(Variable("p"), FALSE))
27+
28+
NIL = Abstraction("x", TRUE)
29+
30+
NULL = Abstraction(
31+
"p",
32+
Application(
33+
Variable("p"),
34+
Abstraction.curried(
35+
("x", "y"),
36+
FALSE
37+
)
38+
)
39+
)

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "lambda_calculus"
3-
version = "1.9.0"
3+
version = "1.10.0"
44
description = "Implementation of the Lambda calculus"
55
requires-python = ">=3.10"
66
keywords = []

tests/terms/test_pairs.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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

Comments
 (0)