Skip to content

Commit 3c2cf44

Browse files
committed
Revise λ−term signature symbols
1 parent d292d33 commit 3c2cf44

10 files changed

Lines changed: 39 additions & 18 deletions

File tree

src/notebook/math/lambda_/algebraic_types/alpha.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from ....support.schemas import SchemaInferenceError
55
from ..assertions import VariableTypeAssertion
66
from ..instantiation import AtomicLambdaSchemaInstantiation, infer_instantiation_from_term, instantiate_term_schema
7+
from ..signature import ConstantTermSymbol
78
from ..terms import Constant, TypedAbstraction, TypedApplication, TypedTerm
89
from ..type_derivation import (
910
AssumptionTree,
@@ -109,7 +110,7 @@ def visit_sum_elim(
109110
if not isinstance(self.other, TypedApplication) or \
110111
not isinstance(self.other.left, TypedApplication) or \
111112
not isinstance(self.other.left.left, TypedApplication) or \
112-
self.other.left.left.left != Constant('S₋'):
113+
self.other.left.left.left != Constant(ConstantTermSymbol('S₋')):
113114
raise NotAlphaEquivalent
114115

115116
other_sum_term = self.other.right

src/notebook/math/lambda_/algebraic_types/test_alpha.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
parse_variable,
1616
parse_variable_assertion,
1717
)
18+
from ..signature import BaseTypeSymbol
1819
from ..type_derivation import RuleApplicationTree, TypeDerivationError, apply, assume, premise
1920
from ..types import BaseType
2021
from .alpha import alpha_convert_derivation
@@ -85,8 +86,8 @@ def factory(x: str, y: str) -> RuleApplicationTree:
8586
),
8687

8788
premise(
88-
tree=assume(VariableTypeAssertion(parse_variable(x), BaseType('𝟙'))),
89-
discharge=VariableTypeAssertion(parse_variable(x), BaseType('𝟙'))
89+
tree=assume(VariableTypeAssertion(parse_variable(x), BaseType(BaseTypeSymbol('𝟙')))),
90+
discharge=VariableTypeAssertion(parse_variable(x), BaseType(BaseTypeSymbol('𝟙')))
9091
),
9192

9293
premise(

src/notebook/math/lambda_/curry_howard/derivation_to_proof.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def type_connective_to_formula_connective(conn: BinaryTypeConnective) -> BinaryC
3333
class TypeToFormulaVisitor(TypeVisitor[Formula]):
3434
@override
3535
def visit_base(self, type_: BaseType) -> PropConstant:
36-
match type_.name:
36+
match type_.value.name:
3737
case '𝟙':
3838
return PropConstant(PropConstantSymbol.VERUM)
3939

src/notebook/math/lambda_/curry_howard/proof_to_derivation.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from ..assertions import VariableTypeAssertion
2020
from ..instantiation import AtomicLambdaSchemaInstantiation
2121
from ..parsing import parse_type_variable
22+
from ..signature import BaseTypeSymbol
2223
from ..terms import Variable
2324
from ..type_derivation import tree as dtree
2425
from ..type_system import TypingRule
@@ -52,10 +53,10 @@ class FormulaToTypeVisitor(FormulaVisitor[SimpleType]):
5253
def visit_prop_constant(self, formula: PropConstant) -> BaseType:
5354
match formula.value:
5455
case PropConstantSymbol.VERUM:
55-
return BaseType('𝟙')
56+
return BaseType(BaseTypeSymbol('𝟙'))
5657

5758
case PropConstantSymbol.FALSUM:
58-
return BaseType('𝟘')
59+
return BaseType(BaseTypeSymbol('𝟘'))
5960

6061
@override
6162
def visit_predicate(self, formula: PredicateApplication) -> TypeVariable:

src/notebook/math/lambda_/parsing/parser.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def __init__(self, signature: LambdaSignature, source: str, tokens: Sequence[Lam
5050
def _parse_base_type(self) -> BaseType:
5151
head = self.peek_unsafe()
5252
self.advance()
53-
return BaseType(head.value)
53+
return BaseType(self.signature.get_base_type_symbol(head.value))
5454

5555
def parse_type_variable(self) -> TypeVariable:
5656
identifier = self.parse_greek_identifier('SMALL_GREEK_IDENTIFIER')
@@ -137,7 +137,7 @@ def parse_type(self, *, parse_schema: bool) -> SimpleType | SimpleTypeSchema:
137137
def _parse_constant(self) -> Constant:
138138
head = self.peek_unsafe()
139139
self.advance()
140-
return Constant(head.value)
140+
return Constant(self.signature.get_constant_term_symbol(head.value))
141141

142142
def parse_term_placeholder(self) -> TermPlaceholder:
143143
identifier = self.parse_latin_identifier('CAPITAL_LATIN_IDENTIFIER')

src/notebook/math/lambda_/parsing/test_parser.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,13 @@ def test_parsing_invalid_variable_suffix() -> None:
8888
@pytest_parametrize_kwargs(
8989
dict(
9090
term='𝐐',
91-
expected=Constant('𝐐')
91+
expected=Constant(ConstantTermSymbol('𝐐'))
9292
),
9393
dict(
9494
term='(𝐐(𝐐x))',
9595
expected=UntypedApplication(
96-
Constant('𝐐'),
97-
UntypedApplication(Constant('𝐐'), Variable(LatinIdentifier('x')))
96+
Constant(ConstantTermSymbol('𝐐')),
97+
UntypedApplication(Constant(ConstantTermSymbol('𝐐')), Variable(LatinIdentifier('x')))
9898
)
9999
)
100100
)

src/notebook/math/lambda_/signature/signature.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from ....support.collections import MissingKeyError, TrieMapping
44
from .exceptions import LambdaSignatureError
5-
from .symbols import SignatureSymbol
5+
from .symbols import BaseTypeSymbol, ConstantTermSymbol, SignatureSymbol
66

77

88
class LambdaSignature:
@@ -25,6 +25,22 @@ def add_symbol(self, symbol: SignatureSymbol) -> None:
2525
def __getitem__(self, name: str) -> SignatureSymbol:
2626
return self.trie[name]
2727

28+
def get_constant_term_symbol(self, name: str) -> ConstantTermSymbol:
29+
sym = self[name]
30+
31+
if not isinstance(sym, ConstantTermSymbol):
32+
raise LambdaSignatureError(f'Expected {name} to be a constant term symbol, but got a {sym.get_kind_string()}')
33+
34+
return sym
35+
36+
def get_base_type_symbol(self, name: str) -> BaseTypeSymbol:
37+
sym = self[name]
38+
39+
if not isinstance(sym, BaseTypeSymbol):
40+
raise LambdaSignatureError(f'Expected {name} to be a constant term symbol, but got a {sym.get_kind_string()}')
41+
42+
return sym
43+
2844
def __contains__(self, symbol: SignatureSymbol) -> bool:
2945
try:
3046
existing = self.trie[symbol.name]

src/notebook/math/lambda_/terms/terms.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@
22

33
from ....parsing.identifiers import LatinIdentifier
44
from ..alphabet import BinderSymbol
5+
from ..signature import ConstantTermSymbol
56
from ..types import SimpleType
67

78

89
@dataclass(frozen=True)
910
class Constant:
10-
name: str
11+
value: ConstantTermSymbol
1112

1213
def __str__(self) -> str:
13-
return str(self.name)
14+
return str(self.value)
1415

1516
def __repr__(self) -> str:
1617
return f"parse_term('{self}')"

src/notebook/math/lambda_/types/types.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22

33
from ....parsing.identifiers import GreekIdentifier
44
from ..alphabet import BinaryTypeConnective
5+
from ..signature import BaseTypeSymbol
56

67

78
@dataclass(frozen=True)
89
class BaseType:
9-
name: str
10+
value: BaseTypeSymbol
1011

1112
def __str__(self) -> str:
12-
return str(self.name)
13+
return str(self.value)
1314

1415
def __repr__(self) -> str:
1516
return f"parse_type('{self}')"

src/notebook/math/lambda_/untyped/alpha.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ def are_terms_alpha_equivalent(m: UntypedTerm, n: UntypedTerm) -> bool:
1111
case (Constant(), Constant()):
1212
assert isinstance(m, Constant)
1313
assert isinstance(n, Constant)
14-
return m.name == n.name
14+
return m == n
1515

1616
case (Variable(), Variable()):
1717
assert isinstance(m, Variable)
1818
assert isinstance(n, Variable)
19-
return m.identifier == n.identifier
19+
return m == n
2020

2121
case (UntypedApplication(), UntypedApplication()):
2222
assert isinstance(m, UntypedApplication)

0 commit comments

Comments
 (0)