tinyprove 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- tinyprove-0.1.0/PKG-INFO +50 -0
- tinyprove-0.1.0/README.md +43 -0
- tinyprove-0.1.0/pyproject.toml +18 -0
- tinyprove-0.1.0/setup.cfg +4 -0
- tinyprove-0.1.0/src/tinyprove/__init__.py +27 -0
- tinyprove-0.1.0/src/tinyprove/axiom_defs.py +62 -0
- tinyprove-0.1.0/src/tinyprove/core.py +320 -0
- tinyprove-0.1.0/src/tinyprove/inductive.py +340 -0
- tinyprove-0.1.0/src/tinyprove/ir.py +84 -0
- tinyprove-0.1.0/src/tinyprove/parser.py +141 -0
- tinyprove-0.1.0/src/tinyprove.egg-info/PKG-INFO +50 -0
- tinyprove-0.1.0/src/tinyprove.egg-info/SOURCES.txt +13 -0
- tinyprove-0.1.0/src/tinyprove.egg-info/dependency_links.txt +1 -0
- tinyprove-0.1.0/src/tinyprove.egg-info/top_level.txt +1 -0
- tinyprove-0.1.0/test/test.py +790 -0
tinyprove-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: tinyprove
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Tiny CoC-based theorem prover with inductive types
|
|
5
|
+
Requires-Python: >=3.10
|
|
6
|
+
Description-Content-Type: text/markdown
|
|
7
|
+
|
|
8
|
+
# Tinyprove
|
|
9
|
+
|
|
10
|
+
A small, minimal theorem prover.
|
|
11
|
+
|
|
12
|
+
[pytorch](https://pytorch.org/) : [tinygrad](https://github.com/tinygrad/tinygrad) :: [lean](https://lean-lang.org/) : **tinyprove**
|
|
13
|
+
|
|
14
|
+
Get started:
|
|
15
|
+
|
|
16
|
+
```
|
|
17
|
+
from parser import parse
|
|
18
|
+
from tinyprove import infer, check
|
|
19
|
+
from axiom_defs import DEFNS
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
Parser supports parsing with a context of existing variables specified.
|
|
23
|
+
|
|
24
|
+
```
|
|
25
|
+
ctx = [("A", parse("Type0"))]
|
|
26
|
+
identity = parse("λ x: A -> x", ctx)
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
Terms can be printed (need to pass context).
|
|
30
|
+
|
|
31
|
+
```
|
|
32
|
+
print(identity.str(ctx))
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
Type inference:
|
|
36
|
+
|
|
37
|
+
```
|
|
38
|
+
identity_ty = infer(identity, ctx)
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
DEFNS supplies some basic inductive types (`False`, `And`, `Or`, `Exists`, `Nat`), plus the classical excluded middle axiom (`.em`).
|
|
42
|
+
|
|
43
|
+
```
|
|
44
|
+
identity_ty = infer(identity, ctx, defns=DEFNS)
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
Since `identity` doesn't use any of these definitions, the result is the same.
|
|
48
|
+
|
|
49
|
+
To see how you can prove some basic theorems, check `test.py`.
|
|
50
|
+
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# Tinyprove
|
|
2
|
+
|
|
3
|
+
A small, minimal theorem prover.
|
|
4
|
+
|
|
5
|
+
[pytorch](https://pytorch.org/) : [tinygrad](https://github.com/tinygrad/tinygrad) :: [lean](https://lean-lang.org/) : **tinyprove**
|
|
6
|
+
|
|
7
|
+
Get started:
|
|
8
|
+
|
|
9
|
+
```
|
|
10
|
+
from parser import parse
|
|
11
|
+
from tinyprove import infer, check
|
|
12
|
+
from axiom_defs import DEFNS
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
Parser supports parsing with a context of existing variables specified.
|
|
16
|
+
|
|
17
|
+
```
|
|
18
|
+
ctx = [("A", parse("Type0"))]
|
|
19
|
+
identity = parse("λ x: A -> x", ctx)
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
Terms can be printed (need to pass context).
|
|
23
|
+
|
|
24
|
+
```
|
|
25
|
+
print(identity.str(ctx))
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
Type inference:
|
|
29
|
+
|
|
30
|
+
```
|
|
31
|
+
identity_ty = infer(identity, ctx)
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
DEFNS supplies some basic inductive types (`False`, `And`, `Or`, `Exists`, `Nat`), plus the classical excluded middle axiom (`.em`).
|
|
35
|
+
|
|
36
|
+
```
|
|
37
|
+
identity_ty = infer(identity, ctx, defns=DEFNS)
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
Since `identity` doesn't use any of these definitions, the result is the same.
|
|
41
|
+
|
|
42
|
+
To see how you can prove some basic theorems, check `test.py`.
|
|
43
|
+
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=68", "wheel"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "tinyprove"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Tiny CoC-based theorem prover with inductive types"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.10"
|
|
11
|
+
dependencies = []
|
|
12
|
+
|
|
13
|
+
[tool.setuptools]
|
|
14
|
+
package-dir = {"" = "src"}
|
|
15
|
+
|
|
16
|
+
[tool.setuptools.packages.find]
|
|
17
|
+
where = ["src"]
|
|
18
|
+
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
from .core import (
|
|
2
|
+
TypecheckError,
|
|
3
|
+
Term, Sort, Const, Var, Pi, Lam, App,
|
|
4
|
+
Definitions, AxiomDefinition, ConstDefinition,
|
|
5
|
+
infer, check,
|
|
6
|
+
)
|
|
7
|
+
from .ir import (
|
|
8
|
+
IrNode, IrSort, IrConst, IrVar, IrPi, IrLam, IrApp,
|
|
9
|
+
)
|
|
10
|
+
from .parser import (
|
|
11
|
+
parse_ir, parse,
|
|
12
|
+
)
|
|
13
|
+
from .inductive import (
|
|
14
|
+
InductiveDef, IrConstructorDefinition, IrInductiveSelfRef,
|
|
15
|
+
)
|
|
16
|
+
from .axiom_defs import (
|
|
17
|
+
get_usual_axioms,
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
try:
|
|
22
|
+
from importlib.metadata import version
|
|
23
|
+
__version__ = version("tinyprove")
|
|
24
|
+
except Exception:
|
|
25
|
+
__version__ = "0.0.0"
|
|
26
|
+
|
|
27
|
+
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
from .ir import *
|
|
2
|
+
from .parser import parse
|
|
3
|
+
from .core import Definitions, AxiomDefinition
|
|
4
|
+
from .inductive import InductiveDef, IrConstructorDefinition, IrInductiveSelfRef
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def get_usual_axioms():
|
|
9
|
+
ans = Definitions()
|
|
10
|
+
|
|
11
|
+
# Constructive:
|
|
12
|
+
|
|
13
|
+
ans.add(InductiveDef("False", IrSort(0), [], [], [], ans))
|
|
14
|
+
|
|
15
|
+
ans.add(InductiveDef("Unit", IrSort(0), [], [],
|
|
16
|
+
[
|
|
17
|
+
IrConstructorDefinition("in", [], []),
|
|
18
|
+
], ans))
|
|
19
|
+
|
|
20
|
+
ans.add(InductiveDef("And", IrSort(0), [("A", IrSort(0)), ("B", IrSort(0))], [],
|
|
21
|
+
[
|
|
22
|
+
IrConstructorDefinition("in", [("a", IrVar("A")), ("b", IrVar("B"))], []),
|
|
23
|
+
], ans))
|
|
24
|
+
|
|
25
|
+
ans.add(InductiveDef("Or", IrSort(0), [("A", IrSort(0)), ("B", IrSort(0))], [],
|
|
26
|
+
[
|
|
27
|
+
IrConstructorDefinition("inl", [("a", IrVar("A"))], []),
|
|
28
|
+
IrConstructorDefinition("inr", [("b", IrVar("B"))], []),
|
|
29
|
+
], ans))
|
|
30
|
+
|
|
31
|
+
ans.add(InductiveDef("Eq", IrSort(0), [("A", IrSort(0)), ("x", IrVar("A"))], [("y", IrVar("A"))],
|
|
32
|
+
[
|
|
33
|
+
IrConstructorDefinition("refl", [], [IrVar("x")]),
|
|
34
|
+
], ans))
|
|
35
|
+
|
|
36
|
+
ans.add(InductiveDef("Exists", IrSort(0), [("A", IrSort(0)), ("P", IrPi("a", IrVar("A"), IrSort(0)))], [],
|
|
37
|
+
[
|
|
38
|
+
IrConstructorDefinition("in", [("a", IrVar("A")), ("pa", IrApp(IrVar("P"), IrVar("a")))], []),
|
|
39
|
+
], ans))
|
|
40
|
+
|
|
41
|
+
ans.add(InductiveDef("Nat", IrSort(0), [], [],
|
|
42
|
+
[
|
|
43
|
+
IrConstructorDefinition("Z", [], []),
|
|
44
|
+
IrConstructorDefinition("S", [("n", IrInductiveSelfRef([]))], []),
|
|
45
|
+
], ans))
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
# Non-constructive:
|
|
49
|
+
|
|
50
|
+
ans.add(AxiomDefinition(
|
|
51
|
+
"",
|
|
52
|
+
{
|
|
53
|
+
"em": parse("Π A: Type0 => (Or A (Π a: A => False))")
|
|
54
|
+
},
|
|
55
|
+
ans))
|
|
56
|
+
|
|
57
|
+
return ans
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
|
|
@@ -0,0 +1,320 @@
|
|
|
1
|
+
from dataclasses import dataclass
|
|
2
|
+
from typing import List, Dict, Tuple, Set
|
|
3
|
+
from enum import Enum
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class TypecheckError(Exception):
|
|
8
|
+
pass
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
# ---- Term AST and DeBruijn Manipulations: ----
|
|
12
|
+
|
|
13
|
+
class Term:
|
|
14
|
+
""" Base class for tinyprove AST. """
|
|
15
|
+
def __str__(self):
|
|
16
|
+
return self.str([])
|
|
17
|
+
|
|
18
|
+
@dataclass(frozen=True)
|
|
19
|
+
class Sort(Term):
|
|
20
|
+
level: int
|
|
21
|
+
def str(self, ctx):
|
|
22
|
+
return f"Type{self.level}"
|
|
23
|
+
def shift(self, shift, keep=0):
|
|
24
|
+
return self
|
|
25
|
+
def subst(self, j, term):
|
|
26
|
+
return self
|
|
27
|
+
|
|
28
|
+
@dataclass(frozen=True)
|
|
29
|
+
class Const(Term):
|
|
30
|
+
name: str
|
|
31
|
+
def str(self, ctx):
|
|
32
|
+
return self.name
|
|
33
|
+
def shift(self, shift, keep=0):
|
|
34
|
+
return self
|
|
35
|
+
def subst(self, j, term):
|
|
36
|
+
return self
|
|
37
|
+
|
|
38
|
+
@dataclass(frozen=True)
|
|
39
|
+
class Var(Term):
|
|
40
|
+
depth: int
|
|
41
|
+
def str(self, ctx):
|
|
42
|
+
if self.depth < len(ctx):
|
|
43
|
+
return ctx[self.depth][0]
|
|
44
|
+
else:
|
|
45
|
+
return f"^{self.depth}"
|
|
46
|
+
def shift(self, shift, keep=0):
|
|
47
|
+
if self.depth < keep: return self
|
|
48
|
+
return Var(self.depth + shift)
|
|
49
|
+
def subst(self, j, term):
|
|
50
|
+
if self.depth == j:
|
|
51
|
+
return term
|
|
52
|
+
if self.depth > j:
|
|
53
|
+
return Var(self.depth - 1) # substitution removes a variable
|
|
54
|
+
return self
|
|
55
|
+
|
|
56
|
+
@dataclass(frozen=True)
|
|
57
|
+
class Pi(Term):
|
|
58
|
+
param: str
|
|
59
|
+
A: Term
|
|
60
|
+
B: Term
|
|
61
|
+
def str(self, ctx):
|
|
62
|
+
return f"(Π {self.param}: {self.A.str(ctx)} => {self.B.str([(self.param, None)] + ctx)})"
|
|
63
|
+
def shift(self, shift, keep=0):
|
|
64
|
+
return Pi(self.param, self.A.shift(shift, keep), self.B.shift(shift, keep + 1)) # keep param
|
|
65
|
+
def subst(self, j, term):
|
|
66
|
+
return Pi(self.param, self.A.subst(j, term), self.B.subst(j + 1, term.shift(1)))
|
|
67
|
+
|
|
68
|
+
@dataclass(frozen=True)
|
|
69
|
+
class Lam(Term):
|
|
70
|
+
param: str
|
|
71
|
+
A: Term
|
|
72
|
+
body: Term
|
|
73
|
+
def str(self, ctx):
|
|
74
|
+
return f"(λ {self.param}: {self.A.str(ctx)} -> {self.body.str([(self.param, None)] + ctx)})"
|
|
75
|
+
def shift(self, shift, keep=0):
|
|
76
|
+
return Lam(self.param, self.A.shift(shift, keep), self.body.shift(shift, keep + 1)) # keep param
|
|
77
|
+
def subst(self, j, term):
|
|
78
|
+
return Lam(self.param, self.A.subst(j, term), self.body.subst(j + 1, term.shift(1)))
|
|
79
|
+
|
|
80
|
+
'''
|
|
81
|
+
@dataclass(frozen=True)
|
|
82
|
+
class Let(Term):
|
|
83
|
+
param:str
|
|
84
|
+
val: Term
|
|
85
|
+
body: Term
|
|
86
|
+
def str(self, ctx):
|
|
87
|
+
return f"let {self.param} = {self.val.str(ctx)}; {self.body.str([(self.param, None)] + ctx)}"
|
|
88
|
+
def shift(self, shift, keep=0):
|
|
89
|
+
return Let(self.param, self.val.shift(shift, keep), self.body.shift(shift, keep + 1))
|
|
90
|
+
def subst(self, j, term):
|
|
91
|
+
return Let(self.param, self.val.subst(j, term), self.body.subst(j + 1, term.shift(1)))
|
|
92
|
+
'''
|
|
93
|
+
|
|
94
|
+
@dataclass(frozen=True)
|
|
95
|
+
class App(Term):
|
|
96
|
+
fn: Term
|
|
97
|
+
arg: Term
|
|
98
|
+
def str(self, ctx):
|
|
99
|
+
return f"({self.fn.str(ctx)} {self.arg.str(ctx)})"
|
|
100
|
+
def shift(self, shift, keep=0):
|
|
101
|
+
return App(self.fn.shift(shift, keep), self.arg.shift(shift, keep))
|
|
102
|
+
def subst(self, j, term):
|
|
103
|
+
return App(self.fn.subst(j, term), self.arg.subst(j, term))
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
# ---- Proof Environment: ----
|
|
107
|
+
|
|
108
|
+
class Definitions:
|
|
109
|
+
""" An environment class that holds various kinds of definitions that are referenced by Const terms. """
|
|
110
|
+
def __init__(self):
|
|
111
|
+
self.defs = {}
|
|
112
|
+
def add(self, definition):
|
|
113
|
+
def_name = definition.name
|
|
114
|
+
if "." in def_name:
|
|
115
|
+
raise RuntimeError(f"{def_name} is not a legal definition name, as these cannot contain the `.` separator.")
|
|
116
|
+
if def_name in self.defs:
|
|
117
|
+
raise RuntimeError(f"Can't add another definition with the same name ({def_name}) as an existing one.")
|
|
118
|
+
self.defs[def_name] = definition
|
|
119
|
+
def remove(self, def_name:str):
|
|
120
|
+
if def_name not in self.defs:
|
|
121
|
+
raise RuntimeError(f"No definition named {def_name} was found.")
|
|
122
|
+
is_being_used = any(
|
|
123
|
+
def_name in definition.used
|
|
124
|
+
for definition in self.defs.values()
|
|
125
|
+
)
|
|
126
|
+
if is_being_used:
|
|
127
|
+
raise RuntimeError(f"Definition {def_name} is currently in use by other definitions in this environment.")
|
|
128
|
+
del self.defs[def_name]
|
|
129
|
+
def __getitem__(self, key:str) -> Term:
|
|
130
|
+
assert isinstance(key, str)
|
|
131
|
+
name, *rest = key.split(".")
|
|
132
|
+
return self.defs[name].get_type(rest)
|
|
133
|
+
def __contains__(self, key:str) -> bool:
|
|
134
|
+
try:
|
|
135
|
+
self[key]
|
|
136
|
+
except KeyError:
|
|
137
|
+
return False
|
|
138
|
+
else:
|
|
139
|
+
return True
|
|
140
|
+
def match_reduce(self, key:str, argchain: List[Term], defns) -> Tuple[Term, List[Term]] | None:
|
|
141
|
+
name, *rest = key.split(".")
|
|
142
|
+
return self.defs[name].match_reduce(rest, argchain, defns)
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
# ---- WHNF Reduction and Type-checking / Inference: ----
|
|
146
|
+
|
|
147
|
+
def to_app_list(term:Term) -> Tuple[Term, List[Term]]:
|
|
148
|
+
match term:
|
|
149
|
+
case App(fn, arg):
|
|
150
|
+
head, args = to_app_list(fn)
|
|
151
|
+
args.append(arg)
|
|
152
|
+
return head, args
|
|
153
|
+
case other:
|
|
154
|
+
return other, []
|
|
155
|
+
|
|
156
|
+
def argchain_whnf(head:Term, args:List[Term], defns:Definitions) -> Tuple[Term, List[Term]]:
|
|
157
|
+
""" Workhorse function of whnf reduction. Terms are represented as a non-App head and a list of args.
|
|
158
|
+
beta reduction is built-in, and defns can supply other reductions through .match_reduce() """
|
|
159
|
+
# make head not an App by adding arguments to args
|
|
160
|
+
head, new_args = to_app_list(head)
|
|
161
|
+
args = new_args + args
|
|
162
|
+
# reduce if we can, otherwise return
|
|
163
|
+
match head:
|
|
164
|
+
case Lam(param, A, body):
|
|
165
|
+
if len(args) >= 1: # lambdas can be reduced if there is an arg to apply them to
|
|
166
|
+
head = body.subst(0, args[0])
|
|
167
|
+
args = args[1:] # first argument was used up
|
|
168
|
+
else:
|
|
169
|
+
return head, args
|
|
170
|
+
case Const(name):
|
|
171
|
+
subst = defns.match_reduce(name, args, defns)
|
|
172
|
+
if subst is None: # no reduction supplied
|
|
173
|
+
return head, args
|
|
174
|
+
else:
|
|
175
|
+
head, args = subst # we use the reduction supplied by defns
|
|
176
|
+
case _: # nothing else can be reduced
|
|
177
|
+
return head, args
|
|
178
|
+
# we've done a reduction so head could be an App now
|
|
179
|
+
return argchain_whnf(head, args, defns) # recursive call
|
|
180
|
+
|
|
181
|
+
def whnf(term:Term, defns:Definitions) -> Term:
|
|
182
|
+
""" Reduce term to weak head normal form. """
|
|
183
|
+
head, args = argchain_whnf(term, [], defns)
|
|
184
|
+
ans = head
|
|
185
|
+
while len(args) > 0:
|
|
186
|
+
ans = App(ans, args[0])
|
|
187
|
+
args = args[1:]
|
|
188
|
+
return ans
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
def conv(t1:Term, t2:Term, defns:Definitions) -> bool:
|
|
192
|
+
t1 = whnf(t1, defns)
|
|
193
|
+
t2 = whnf(t2, defns)
|
|
194
|
+
match t1, t2:
|
|
195
|
+
case Sort(l1), Sort(l2):
|
|
196
|
+
return l1 == l2
|
|
197
|
+
case Var(depth1), Var(depth2):
|
|
198
|
+
return depth1 == depth2
|
|
199
|
+
case Const(name1), Const(name2):
|
|
200
|
+
return name1 == name2
|
|
201
|
+
case App(fn1, arg1), App(fn2, arg2):
|
|
202
|
+
return conv(fn1, fn2, defns) and conv(arg1, arg2, defns)
|
|
203
|
+
case Pi(_, A1, B1), Pi(_, A2, B2):
|
|
204
|
+
return conv(A1, A2, defns) and conv(B1, B2, defns)
|
|
205
|
+
case Lam(_, A1, body1), Lam(_, A2, body2):
|
|
206
|
+
return conv(A1, A2, defns) and conv(body1, body2, defns)
|
|
207
|
+
case _:
|
|
208
|
+
return False # mismatched shapes
|
|
209
|
+
|
|
210
|
+
def infer(term:Term, ctx, defns:Definitions) -> Term:
|
|
211
|
+
match term:
|
|
212
|
+
case Sort(level):
|
|
213
|
+
return Sort(level + 1)
|
|
214
|
+
case Const(name):
|
|
215
|
+
if name not in defns:
|
|
216
|
+
raise TypecheckError(f"Unknown constant {name}.")
|
|
217
|
+
return defns[name]
|
|
218
|
+
case Var(depth):
|
|
219
|
+
if 0 <= depth < len(ctx):
|
|
220
|
+
name, var_ty = ctx[depth]
|
|
221
|
+
return var_ty.shift(depth + 1)
|
|
222
|
+
else:
|
|
223
|
+
raise TypecheckError(f"Tried to lookup a varible with depth {depth} in context of size {len(ctx)}")
|
|
224
|
+
case Pi(param, A, B):
|
|
225
|
+
A_ty = whnf(infer(A, ctx, defns), defns)
|
|
226
|
+
if not isinstance(A_ty, Sort):
|
|
227
|
+
raise TypecheckError(f"Expected A in Pi type {term.str(ctx)} to be a Sort, but found {A_ty.str(ctx)}.")
|
|
228
|
+
ctx_B = [(param, A)] + ctx
|
|
229
|
+
B_ty = whnf(infer(B, ctx_B, defns), defns)
|
|
230
|
+
if not isinstance(B_ty, Sort):
|
|
231
|
+
raise TypecheckError(f"Expected B in Pi type {term.str(ctx)} to be a Sort, but found {B_ty.str(ctx_B)}.")
|
|
232
|
+
return Sort(max(A_ty.level, B_ty.level))
|
|
233
|
+
case Lam(param, A, body):
|
|
234
|
+
A_ty = whnf(infer(A, ctx, defns), defns)
|
|
235
|
+
if not isinstance(A_ty, Sort):
|
|
236
|
+
raise TypecheckError(f"Expected A in Lam {term.str(ctx)} to be a Sort, but found {A_ty.str(ctx)}")
|
|
237
|
+
ctx_body = [(param, A)] + ctx
|
|
238
|
+
body_ty = infer(body, ctx_body, defns)
|
|
239
|
+
return Pi(param, A, body_ty)
|
|
240
|
+
case App(fn, arg):
|
|
241
|
+
fn_ty = whnf(infer(fn, ctx, defns), defns)
|
|
242
|
+
match fn_ty:
|
|
243
|
+
case Pi(param, A, B):
|
|
244
|
+
check(arg, A, ctx, defns)
|
|
245
|
+
ctx_B = [(param, A)] + ctx
|
|
246
|
+
return whnf(B.subst(0, arg), defns)
|
|
247
|
+
case _:
|
|
248
|
+
raise TypecheckError(f"Expected type of fn in application {term.str(ctx)} to be a Pi type, but found {fn_ty.str(ctx)}.")
|
|
249
|
+
case _:
|
|
250
|
+
raise TypecheckError(f"Failed to recognize term {term}")
|
|
251
|
+
|
|
252
|
+
def check(term, expected, ctx, defns:Definitions):
|
|
253
|
+
term_ty = infer(term, ctx, defns)
|
|
254
|
+
if not conv(term_ty, expected, defns):
|
|
255
|
+
raise TypecheckError(f"Expected term {term.str(ctx)} to have type {expected.str(ctx)} but found {term_ty.str(ctx)}.")
|
|
256
|
+
|
|
257
|
+
|
|
258
|
+
|
|
259
|
+
# ---- Definition Classes: ----
|
|
260
|
+
|
|
261
|
+
def find_used_defs(*terms:Tuple[Term]) -> Set[str]:
|
|
262
|
+
terms = list(terms)
|
|
263
|
+
ans = set()
|
|
264
|
+
i = 0 # do a BFS of the Term tree
|
|
265
|
+
while i < len(terms):
|
|
266
|
+
term_i = terms[i]
|
|
267
|
+
match term_i:
|
|
268
|
+
case Pi(param, A, B):
|
|
269
|
+
terms.append(A)
|
|
270
|
+
terms.append(B)
|
|
271
|
+
case Lam(param, A, body):
|
|
272
|
+
terms.append(A)
|
|
273
|
+
terms.append(body)
|
|
274
|
+
case App(fn, arg):
|
|
275
|
+
terms.append(fn)
|
|
276
|
+
terms.append(arg)
|
|
277
|
+
case Const(name): # if it's a Const, it uses other definitions!
|
|
278
|
+
ans.add(name.split(".")[0])
|
|
279
|
+
i += 1
|
|
280
|
+
return ans
|
|
281
|
+
|
|
282
|
+
class AxiomDefinition:
|
|
283
|
+
def __init__(self, name:str, axioms:Dict[str, Term], defns:Definitions):
|
|
284
|
+
self.name = name
|
|
285
|
+
self.axioms = axioms
|
|
286
|
+
axiom_terms = []
|
|
287
|
+
for axiom_name in self.axioms:
|
|
288
|
+
axiom = self.axioms[axiom_name]
|
|
289
|
+
axiom_ty = whnf(infer(axiom, [], defns), defns)
|
|
290
|
+
if not isinstance(axiom_ty, Sort):
|
|
291
|
+
raise TypecheckError(f"Expected type of axiom {self.name}.{axiom_name} to be a Sort.")
|
|
292
|
+
axiom_terms.append(axiom)
|
|
293
|
+
self.used = find_used_defs(*axiom_terms)
|
|
294
|
+
def get_type(self, key:List[str]) -> Term:
|
|
295
|
+
match key:
|
|
296
|
+
case [axiom_name]:
|
|
297
|
+
if axiom_name in self.axioms:
|
|
298
|
+
return self.axioms[axiom_name]
|
|
299
|
+
raise IndexError(f"Couldn't find key {self.name}.{'.'.join(key)}.")
|
|
300
|
+
def match_reduce(self, key:List[str], argchain: List[Term], defns:Definitions) -> Tuple[Term, List[Term]] | None:
|
|
301
|
+
return None
|
|
302
|
+
|
|
303
|
+
class ConstDefinition:
|
|
304
|
+
def __init__(self, name:str, value:Term, defns:Definitions):
|
|
305
|
+
self.name = name
|
|
306
|
+
self.value = whnf(value, defns)
|
|
307
|
+
self.type = whnf(infer(self.value, [], defns), defns)
|
|
308
|
+
self.used = find_used_defs(self.value)
|
|
309
|
+
def get_type(self, key:List[str]) -> Term:
|
|
310
|
+
match key:
|
|
311
|
+
case []:
|
|
312
|
+
return self.type
|
|
313
|
+
raise IndexError(f"Couldn't find key {self.name}.{'.'.join(key)}.")
|
|
314
|
+
def match_reduce(self, key:List[str], argchain: List[Term], defns:Definitions) -> Tuple[Term, List[Term]] | None:
|
|
315
|
+
match key:
|
|
316
|
+
case []:
|
|
317
|
+
return self.value, argchain
|
|
318
|
+
raise IndexError(f"Couldn't find key {self.name}.{'.'.join(key)}.")
|
|
319
|
+
|
|
320
|
+
|