tinyprove 1.0.0__tar.gz → 1.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-1.0.0 → tinyprove-1.1.0}/PKG-INFO +6 -6
- {tinyprove-1.0.0 → tinyprove-1.1.0}/README.md +5 -5
- {tinyprove-1.0.0 → tinyprove-1.1.0}/pyproject.toml +1 -1
- {tinyprove-1.0.0 → tinyprove-1.1.0}/src/tinyprove/inductive.py +10 -2
- {tinyprove-1.0.0 → tinyprove-1.1.0}/src/tinyprove/ir.py +30 -6
- {tinyprove-1.0.0 → tinyprove-1.1.0}/src/tinyprove/parser.py +44 -33
- {tinyprove-1.0.0 → tinyprove-1.1.0}/src/tinyprove.egg-info/PKG-INFO +6 -6
- {tinyprove-1.0.0 → tinyprove-1.1.0}/setup.cfg +0 -0
- {tinyprove-1.0.0 → tinyprove-1.1.0}/src/tinyprove/__init__.py +0 -0
- {tinyprove-1.0.0 → tinyprove-1.1.0}/src/tinyprove/core.py +0 -0
- {tinyprove-1.0.0 → tinyprove-1.1.0}/src/tinyprove/defs.py +0 -0
- {tinyprove-1.0.0 → tinyprove-1.1.0}/src/tinyprove/nat_fns.py +0 -0
- {tinyprove-1.0.0 → tinyprove-1.1.0}/src/tinyprove.egg-info/SOURCES.txt +0 -0
- {tinyprove-1.0.0 → tinyprove-1.1.0}/src/tinyprove.egg-info/dependency_links.txt +0 -0
- {tinyprove-1.0.0 → tinyprove-1.1.0}/src/tinyprove.egg-info/top_level.txt +0 -0
- {tinyprove-1.0.0 → tinyprove-1.1.0}/test/test.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: tinyprove
|
|
3
|
-
Version: 1.
|
|
3
|
+
Version: 1.1.0
|
|
4
4
|
Summary: Tiny CoC-based theorem prover with inductive types
|
|
5
5
|
Project-URL: Homepage, https://github.com/pb1729/tinyprove
|
|
6
6
|
Project-URL: Repository, https://github.com/pb1729/tinyprove
|
|
@@ -199,7 +199,7 @@ In a similar way, while the type of a function will be a function type, the type
|
|
|
199
199
|
|
|
200
200
|
### Basic definitions from `get_usual_axioms`
|
|
201
201
|
|
|
202
|
-
Calling `get_usual_axioms()` produces a module containing some definitions that are expected to be widely useful. Most of them are not axioms in the true sense: They are ordinary tinyprove definitions
|
|
202
|
+
Calling `get_usual_axioms()` produces a module containing some definitions that are expected to be widely useful. Most of them are not axioms in the true sense: They are ordinary tinyprove definitions:
|
|
203
203
|
|
|
204
204
|
```
|
|
205
205
|
# false
|
|
@@ -303,7 +303,7 @@ contrapos: Π A: Type0 => Π B: Type0 => Π _: (Π _: A => B) => (Π _: ¬ B =>
|
|
|
303
303
|
|
|
304
304
|
First we obtain `(a_to_b a): B` from `a: A`. Then we obtain a contradiction by feeding it to `nb`. This completes our first theorem. Checking it in python:
|
|
305
305
|
|
|
306
|
-
```
|
|
306
|
+
```python3
|
|
307
307
|
base = get_usual_axioms()
|
|
308
308
|
|
|
309
309
|
my_module = def_module("""
|
|
@@ -316,7 +316,7 @@ contrapos: Π A: Type0 => Π B: Type0 => Π _: (Π _: A => B) => (Π _: ¬ B =>
|
|
|
316
316
|
|
|
317
317
|
### Inductive Types
|
|
318
318
|
|
|
319
|
-
One thing we need
|
|
319
|
+
One thing we need before we can do most of modern mathematics is the ability to define inductive types. `⊥`, `⊤`, `&`, `|`, `∃`, and even `≡` are all in fact defined as inductive types! You define an inductive type by defining its paramters, indices, and constructors. The `⊤` type does not have any parameters or indices, and only has one constructor, so it makes a nice starting example.
|
|
320
320
|
|
|
321
321
|
```
|
|
322
322
|
ι ⊤ () [] : Type0 {
|
|
@@ -355,7 +355,7 @@ xor: Π x: Bit => Π y: Bit => Bit
|
|
|
355
355
|
|
|
356
356
|
The new thing used here is `Bit.ind0`, the inductive principle for the `Bit` datatype. Here are the arguments that an inductive principal takes, in order:
|
|
357
357
|
* The **parameters** of the inductive type.
|
|
358
|
-
* The **motive**. For an inductive datatype `T` with indices `i_1...i_k` of types `I_1...I_k`, the motive is a function that accepts arguments `(i_1: I_1, ... i_k: I_k, instance: T)` and returns a type. If we use `T.ind0`, that type must be of type `Type0`, if we use `T.ind1`, that type must be of type `Type1`, etc. When the motive is applied to the scrutinee (see later in the list) and its indices, the result is the type returned by the entire inductive principle call.
|
|
358
|
+
* The **motive**. For an inductive datatype `T` with indices `i_1...i_k` of types `I_1...I_k`, the motive is a function that accepts arguments `(i_1: I_1, ... i_k: I_k, instance: T i_1 ... i_k)` and returns a type. If we use `T.ind0`, that type must be of type `Type0`, if we use `T.ind1`, that type must be of type `Type1`, etc. When the motive is applied to the scrutinee (see later in the list) and its indices, the result is the type returned by the entire inductive principle call.
|
|
359
359
|
* The **cases**. There is one of these for every constructor. These are values (if the corresponding constructor has no fields) or functions (if the corresponding constructor has fields). When fully applied, the type of the case for the constructor `cons` must match the type returned by the motive when passed `instance: T = (cons ...)`.
|
|
360
360
|
* Self-reference: The thing that makes inductive principles incredibly powerful is that inductive types can reference themselves (i.e. a `BinaryTree` type has a `branch` constructor with left and right subtrees as fields, where each subtree is a `BinaryTree` itself). For fields that reference the original type, the inductive principle becomes recursive. We can obtain a result for that field that we are then able to use in our case function. This is how induction on the natural numbers, or any inductively defined type such as a tree type or a list type, works.
|
|
361
361
|
* The **indices** of the **scrutinee**. Because things have types, we need to specify the indices so that the next argument has a known expected type.
|
|
@@ -472,7 +472,7 @@ The `Vector` type is parametrized by `A`. It's not parametrized by `n`, though.
|
|
|
472
472
|
|
|
473
473
|
The problem with making `n` a type parameter is the following: To make, say, a `Vector A 3` given `a: A`, we need to use `v_cons` to append `a` onto a `Vector A 2`. But if `n` was a parameter, then `Vector A 2` would be impossible to make into a field. Self-references are assumed by the machinery for `ι` definitions to share the same parameters. So we could only accept other `Vector A 3` instances, which is hardly useful. However, there's no requirement that self-references must share the same indices. Indeed, you may have noticed that self references were distinguished because they required us to put `[]` after the inductive type name. These brackets hold the indices of that type, though up until now they were empty because our types did not have any indices. By making `n` an index, we allow ourselves to use a `Vector A 2` to build a `Vector A 3`, even though the value of `n` varies between the two.
|
|
474
474
|
|
|
475
|
-
Indices must be computable from the constructor arguments in some way. For example, `v_empty` doesn't have constructor arguments, but the index produced is always just the constant `0`, so that's fine. `v_cons` does have arguments, and the produced index `+ 1 len` can be easily computed from them. We also see that the index for the self-reference `rest` is computable from the previous constructor parameters (
|
|
475
|
+
Indices must be computable from the constructor arguments in some way. For example, `v_empty` doesn't have constructor arguments, but the index produced is always just the constant `0`, so that's fine. `v_cons` does have arguments, and the produced index `+ 1 len` can be easily computed from them. We also see that the index for the self-reference `rest` is computable from the previous constructor parameters (it just takes `len`).
|
|
476
476
|
|
|
477
477
|
```
|
|
478
478
|
vec_map: Π A: Type0 => Π B: Type0 => Π f: (Π _: A => B) => Π len: ℕ => Π v_a: Vector A len => Vector B len
|
|
@@ -189,7 +189,7 @@ In a similar way, while the type of a function will be a function type, the type
|
|
|
189
189
|
|
|
190
190
|
### Basic definitions from `get_usual_axioms`
|
|
191
191
|
|
|
192
|
-
Calling `get_usual_axioms()` produces a module containing some definitions that are expected to be widely useful. Most of them are not axioms in the true sense: They are ordinary tinyprove definitions
|
|
192
|
+
Calling `get_usual_axioms()` produces a module containing some definitions that are expected to be widely useful. Most of them are not axioms in the true sense: They are ordinary tinyprove definitions:
|
|
193
193
|
|
|
194
194
|
```
|
|
195
195
|
# false
|
|
@@ -293,7 +293,7 @@ contrapos: Π A: Type0 => Π B: Type0 => Π _: (Π _: A => B) => (Π _: ¬ B =>
|
|
|
293
293
|
|
|
294
294
|
First we obtain `(a_to_b a): B` from `a: A`. Then we obtain a contradiction by feeding it to `nb`. This completes our first theorem. Checking it in python:
|
|
295
295
|
|
|
296
|
-
```
|
|
296
|
+
```python3
|
|
297
297
|
base = get_usual_axioms()
|
|
298
298
|
|
|
299
299
|
my_module = def_module("""
|
|
@@ -306,7 +306,7 @@ contrapos: Π A: Type0 => Π B: Type0 => Π _: (Π _: A => B) => (Π _: ¬ B =>
|
|
|
306
306
|
|
|
307
307
|
### Inductive Types
|
|
308
308
|
|
|
309
|
-
One thing we need
|
|
309
|
+
One thing we need before we can do most of modern mathematics is the ability to define inductive types. `⊥`, `⊤`, `&`, `|`, `∃`, and even `≡` are all in fact defined as inductive types! You define an inductive type by defining its paramters, indices, and constructors. The `⊤` type does not have any parameters or indices, and only has one constructor, so it makes a nice starting example.
|
|
310
310
|
|
|
311
311
|
```
|
|
312
312
|
ι ⊤ () [] : Type0 {
|
|
@@ -345,7 +345,7 @@ xor: Π x: Bit => Π y: Bit => Bit
|
|
|
345
345
|
|
|
346
346
|
The new thing used here is `Bit.ind0`, the inductive principle for the `Bit` datatype. Here are the arguments that an inductive principal takes, in order:
|
|
347
347
|
* The **parameters** of the inductive type.
|
|
348
|
-
* The **motive**. For an inductive datatype `T` with indices `i_1...i_k` of types `I_1...I_k`, the motive is a function that accepts arguments `(i_1: I_1, ... i_k: I_k, instance: T)` and returns a type. If we use `T.ind0`, that type must be of type `Type0`, if we use `T.ind1`, that type must be of type `Type1`, etc. When the motive is applied to the scrutinee (see later in the list) and its indices, the result is the type returned by the entire inductive principle call.
|
|
348
|
+
* The **motive**. For an inductive datatype `T` with indices `i_1...i_k` of types `I_1...I_k`, the motive is a function that accepts arguments `(i_1: I_1, ... i_k: I_k, instance: T i_1 ... i_k)` and returns a type. If we use `T.ind0`, that type must be of type `Type0`, if we use `T.ind1`, that type must be of type `Type1`, etc. When the motive is applied to the scrutinee (see later in the list) and its indices, the result is the type returned by the entire inductive principle call.
|
|
349
349
|
* The **cases**. There is one of these for every constructor. These are values (if the corresponding constructor has no fields) or functions (if the corresponding constructor has fields). When fully applied, the type of the case for the constructor `cons` must match the type returned by the motive when passed `instance: T = (cons ...)`.
|
|
350
350
|
* Self-reference: The thing that makes inductive principles incredibly powerful is that inductive types can reference themselves (i.e. a `BinaryTree` type has a `branch` constructor with left and right subtrees as fields, where each subtree is a `BinaryTree` itself). For fields that reference the original type, the inductive principle becomes recursive. We can obtain a result for that field that we are then able to use in our case function. This is how induction on the natural numbers, or any inductively defined type such as a tree type or a list type, works.
|
|
351
351
|
* The **indices** of the **scrutinee**. Because things have types, we need to specify the indices so that the next argument has a known expected type.
|
|
@@ -462,7 +462,7 @@ The `Vector` type is parametrized by `A`. It's not parametrized by `n`, though.
|
|
|
462
462
|
|
|
463
463
|
The problem with making `n` a type parameter is the following: To make, say, a `Vector A 3` given `a: A`, we need to use `v_cons` to append `a` onto a `Vector A 2`. But if `n` was a parameter, then `Vector A 2` would be impossible to make into a field. Self-references are assumed by the machinery for `ι` definitions to share the same parameters. So we could only accept other `Vector A 3` instances, which is hardly useful. However, there's no requirement that self-references must share the same indices. Indeed, you may have noticed that self references were distinguished because they required us to put `[]` after the inductive type name. These brackets hold the indices of that type, though up until now they were empty because our types did not have any indices. By making `n` an index, we allow ourselves to use a `Vector A 2` to build a `Vector A 3`, even though the value of `n` varies between the two.
|
|
464
464
|
|
|
465
|
-
Indices must be computable from the constructor arguments in some way. For example, `v_empty` doesn't have constructor arguments, but the index produced is always just the constant `0`, so that's fine. `v_cons` does have arguments, and the produced index `+ 1 len` can be easily computed from them. We also see that the index for the self-reference `rest` is computable from the previous constructor parameters (
|
|
465
|
+
Indices must be computable from the constructor arguments in some way. For example, `v_empty` doesn't have constructor arguments, but the index produced is always just the constant `0`, so that's fine. `v_cons` does have arguments, and the produced index `+ 1 len` can be easily computed from them. We also see that the index for the self-reference `rest` is computable from the previous constructor parameters (it just takes `len`).
|
|
466
466
|
|
|
467
467
|
```
|
|
468
468
|
vec_map: Π A: Type0 => Π B: Type0 => Π f: (Π _: A => B) => Π len: ℕ => Π v_a: Vector A len => Vector B len
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
2
|
from dataclasses import dataclass, field
|
|
3
3
|
|
|
4
|
-
from .ir import Binder, IrNode, IrSort, IrConst, IrVar, IrNatLit, IrNat, IrLam, IrPi, IrApp, IrLet
|
|
4
|
+
from .ir import LineInfo, Binder, IrNode, IrSort, IrConst, IrVar, IrNatLit, IrNat, IrLam, IrPi, IrApp, IrLet
|
|
5
5
|
from .core import (
|
|
6
6
|
Binding, Value, Term, Module, Env,
|
|
7
7
|
LookupError, TypecheckError, DefinitionError, EvalError,
|
|
@@ -120,8 +120,16 @@ class IrInductiveDefinition(IrNode):
|
|
|
120
120
|
params: list[tuple[Binder, IrNode]]
|
|
121
121
|
indices: list[tuple[Binder, IrNode]]
|
|
122
122
|
constructors: list[IrConstructorDefinition]
|
|
123
|
+
line_ref: tuple[int, LineInfo]
|
|
123
124
|
def apply(self, module:Module):
|
|
124
|
-
|
|
125
|
+
try:
|
|
126
|
+
define_inductive(module, self)
|
|
127
|
+
except Exception as e:
|
|
128
|
+
pos, line_info = self.line_ref
|
|
129
|
+
locator_string = line_info.get_locator_string_for_line(pos)
|
|
130
|
+
new_err = DefinitionError(f"Failed inductive definition {self.name} starting {locator_string[1:]}\n"
|
|
131
|
+
"See the exception that caused this one for details.")
|
|
132
|
+
raise new_err from e
|
|
125
133
|
|
|
126
134
|
|
|
127
135
|
# IR manipulation:
|
|
@@ -100,18 +100,42 @@ class IrApp(IrNode):
|
|
|
100
100
|
self.arg.to_term(module, binders))
|
|
101
101
|
|
|
102
102
|
|
|
103
|
+
# Class for tracking line number data
|
|
104
|
+
class LineInfo:
|
|
105
|
+
def __init__(self, initial_num:int, lines:list[str], line_nums:list[int], tok_start_offsets:list[str]):
|
|
106
|
+
self.initial_num = initial_num
|
|
107
|
+
self.lines = lines
|
|
108
|
+
self.line_nums = line_nums
|
|
109
|
+
self.tok_start_offsets = tok_start_offsets
|
|
110
|
+
def get_locator_string_for_line(self, pos:int) -> str:
|
|
111
|
+
line_num = self.line_nums[pos]
|
|
112
|
+
return "\n".join(["",
|
|
113
|
+
f"on line {line_num}:",
|
|
114
|
+
self.lines[line_num - self.initial_num],
|
|
115
|
+
" "*self.tok_start_offsets[pos] + "^"
|
|
116
|
+
])
|
|
117
|
+
|
|
118
|
+
|
|
103
119
|
# IR node for constant definitions:
|
|
104
120
|
@dataclass(frozen=True)
|
|
105
121
|
class IrConstantDefinition(IrNode):
|
|
106
122
|
name: str
|
|
107
123
|
value: IrNode
|
|
108
124
|
expected_ty: Optional[IrNode]
|
|
125
|
+
line_ref: tuple[int, LineInfo]
|
|
109
126
|
def apply(self, module:Module):
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
127
|
+
try:
|
|
128
|
+
value = self.value.to_term(module, EmptyEnv())
|
|
129
|
+
expected_ty = (
|
|
130
|
+
None if self.expected_ty is None else
|
|
131
|
+
self.expected_ty.to_term(module, EmptyEnv())
|
|
132
|
+
)
|
|
133
|
+
define(module, self.name, value, expected_ty)
|
|
134
|
+
except Exception as e:
|
|
135
|
+
pos, line_info = self.line_ref
|
|
136
|
+
locator_string = line_info.get_locator_string_for_line(pos)
|
|
137
|
+
new_err = DefinitionError(f"Failed definition of {self.name} starting {locator_string[1:]}\n"
|
|
138
|
+
"See the exception that caused this one for details.")
|
|
139
|
+
raise new_err from e
|
|
116
140
|
|
|
117
141
|
|
|
@@ -3,6 +3,7 @@ from typing import List, Tuple
|
|
|
3
3
|
|
|
4
4
|
from .core import Env, EmptyEnv, EnvEntry
|
|
5
5
|
from .ir import (
|
|
6
|
+
LineInfo,
|
|
6
7
|
IrNode, IrSort, IrConst, IrNatLit, IrNat, IrVar, IrPi, IrLam, IrLet, IrApp,
|
|
7
8
|
IrConstantDefinition, Binder )
|
|
8
9
|
from .inductive import IrInductiveSelfRef, IrConstructorDefinition, IrInductiveDefinition
|
|
@@ -15,12 +16,12 @@ _first_symb_re = r"[A-Za-z_\|\&\+\*−≡¬⊥⊤<>∃]"
|
|
|
15
16
|
_rest_symb_re = r"(" + _first_symb_re + r"|[0-9])"
|
|
16
17
|
_name_re = _first_symb_re + _rest_symb_re + r"*"
|
|
17
18
|
_ident_re = r"(" + _name_re + f"\.)*" + _name_re
|
|
19
|
+
_whitespace_re = re.compile("\s*")
|
|
18
20
|
_token_re = re.compile(
|
|
19
21
|
r"""
|
|
20
|
-
\s* # skip leading whitespace
|
|
21
22
|
( # group captures only non-whitespace
|
|
22
23
|
=>|->| # separators
|
|
23
|
-
[\[\]()\{\},:=λΠι]|
|
|
24
|
+
[\[\]\(\)\{\},:=λΠι]| # punctuation (parens, lambdas, etc.)
|
|
24
25
|
""" + _ident_re + """| # identifiers
|
|
25
26
|
""" + _nat_re + """ | # natural number datatype
|
|
26
27
|
""" + _nat_lit_re + """ # natural number literals
|
|
@@ -30,29 +31,32 @@ _token_re = re.compile(
|
|
|
30
31
|
re.VERBOSE,
|
|
31
32
|
)
|
|
32
33
|
|
|
34
|
+
|
|
35
|
+
|
|
33
36
|
def split_path(ident:str) -> Tuple[str]:
|
|
34
37
|
return tuple(ident.split("."))
|
|
35
38
|
|
|
36
|
-
|
|
39
|
+
|
|
40
|
+
def _tokenise(src: str, initial_line_num:int=1) -> List[str]:
|
|
37
41
|
""" Return a list of string tokens (whitespace already stripped). """
|
|
38
|
-
|
|
42
|
+
lines = src.split("\n")
|
|
39
43
|
ans = []
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
return
|
|
44
|
+
line_nums = []
|
|
45
|
+
tok_start_offsets = []
|
|
46
|
+
for line_num, line in enumerate(lines, start=initial_line_num):
|
|
47
|
+
comment_start = line.find("#")
|
|
48
|
+
if comment_start >= 0:
|
|
49
|
+
line = line[:comment_start]
|
|
50
|
+
pos = _whitespace_re.match(line, 0).end() # jump past initial whitespace
|
|
51
|
+
while pos < len(line):
|
|
52
|
+
m = _token_re.match(line, pos)
|
|
53
|
+
if not m:
|
|
54
|
+
raise SyntaxError(f"Unexpected character on line {line_num}: {repr(line[pos])}")
|
|
55
|
+
ans.append(m.group(1))
|
|
56
|
+
line_nums.append(line_num)
|
|
57
|
+
tok_start_offsets.append(pos)
|
|
58
|
+
pos = m.end()
|
|
59
|
+
return ans, LineInfo(initial_line_num, lines, line_nums, tok_start_offsets)
|
|
56
60
|
|
|
57
61
|
|
|
58
62
|
def to_positive_int(digits:str) -> int | None:
|
|
@@ -75,19 +79,23 @@ def find_ref(binders:Env, name:str) -> IrNode:
|
|
|
75
79
|
|
|
76
80
|
# Recursive‑descent parser:
|
|
77
81
|
class Parser:
|
|
78
|
-
def __init__(self, tokens: List[str],
|
|
82
|
+
def __init__(self, tokens: List[str], line_info:LineInfo):
|
|
79
83
|
self.toks: List[str] = tokens
|
|
84
|
+
self.line_info = line_info
|
|
80
85
|
self.pos: int = 0
|
|
81
86
|
self.indty_name = None
|
|
82
87
|
self.env:Env = EmptyEnv() # maintain environment of what is in our lexical scope
|
|
88
|
+
def _err(self, msg:str, tok_offset:int=0):
|
|
89
|
+
pos = max(0, self.pos - tok_offset) # some errors should point to previous token
|
|
90
|
+
raise SyntaxError(msg + self.line_info.get_locator_string_for_line(pos))
|
|
83
91
|
def _peek(self) -> str | None:
|
|
84
92
|
return self.toks[self.pos] if self.pos < len(self.toks) else None
|
|
85
93
|
def _eat(self, expected: str | None = None) -> str:
|
|
86
94
|
tok = self._peek()
|
|
87
95
|
if tok is None:
|
|
88
|
-
|
|
96
|
+
self._err("unexpected end‑of‑input", 1)
|
|
89
97
|
if expected is not None and tok != expected:
|
|
90
|
-
|
|
98
|
+
self._err(f"expected '{expected}', got '{tok}'")
|
|
91
99
|
self.pos += 1
|
|
92
100
|
return tok
|
|
93
101
|
def _push(self, binder:Binder):
|
|
@@ -119,19 +127,21 @@ class Parser:
|
|
|
119
127
|
self._eat(",")
|
|
120
128
|
def _check_name(self, name, category:str):
|
|
121
129
|
if name == self.indty_name:
|
|
122
|
-
|
|
130
|
+
self._err(f"Can't shadow inductive type name f{self.indty_name}", 1)
|
|
123
131
|
if not re.match(_name_re, name):
|
|
124
|
-
|
|
132
|
+
self._err(f"Expected a valid {category} name instead of {name}", 1)
|
|
125
133
|
def parse(self) -> List[IrNode]:
|
|
126
134
|
return self._parse_comma_separated(None, self._parse_defn)
|
|
127
135
|
def _parse_defn(self) -> IrNode:
|
|
128
136
|
tok = self._peek()
|
|
129
137
|
if tok == "ι": # define inductive type
|
|
130
138
|
return self._parse_iota()
|
|
139
|
+
line_ref = (self.pos, self.line_info) # save for creating the Definition
|
|
131
140
|
# other definition
|
|
132
141
|
binder, A, rhs = self._parse_assignment()
|
|
133
|
-
return IrConstantDefinition(binder.name, rhs, A) # we just want the name as a string
|
|
142
|
+
return IrConstantDefinition(binder.name, rhs, A, line_ref) # we just want the name as a string
|
|
134
143
|
def _parse_iota(self) -> IrNode:
|
|
144
|
+
line_ref = (self.pos, self.line_info) # save for creating the Definition
|
|
135
145
|
self._eat("ι")
|
|
136
146
|
name = self._eat()
|
|
137
147
|
self._check_name(name, "inductive type")
|
|
@@ -161,7 +171,7 @@ class Parser:
|
|
|
161
171
|
return IrInductiveDefinition(
|
|
162
172
|
name, sort,
|
|
163
173
|
params, indices,
|
|
164
|
-
constructors)
|
|
174
|
+
constructors, line_ref)
|
|
165
175
|
def _parse_constructor_def(self) -> IrNode:
|
|
166
176
|
name = self._eat()
|
|
167
177
|
self._check_name(name, "constructor")
|
|
@@ -253,8 +263,10 @@ class Parser:
|
|
|
253
263
|
def _parse_atom(self) -> IrNode:
|
|
254
264
|
tok = self._peek()
|
|
255
265
|
if tok is None:
|
|
256
|
-
|
|
266
|
+
self._err("unexpected end‑of‑input while parsing atom")
|
|
257
267
|
# Parenthesised expression
|
|
268
|
+
if tok == "{": # braces of the let-expr make this unambiguous
|
|
269
|
+
return self._parse_let()
|
|
258
270
|
if tok == "(":
|
|
259
271
|
self._eat("(")
|
|
260
272
|
expr = self._parse_expr()
|
|
@@ -283,15 +295,14 @@ class Parser:
|
|
|
283
295
|
return IrInductiveSelfRef(index_vals)
|
|
284
296
|
return find_ref(self.env, tok)
|
|
285
297
|
# catchall
|
|
286
|
-
|
|
298
|
+
self._err(f"unexpected token '{tok}'")
|
|
287
299
|
|
|
288
300
|
|
|
289
301
|
# Parsing functions:
|
|
290
302
|
|
|
291
|
-
def parse(src: str
|
|
292
|
-
|
|
293
|
-
tokens
|
|
294
|
-
return Parser(tokens, ctx=ctx).parse()
|
|
303
|
+
def parse(src: str) -> List[IrNode]:
|
|
304
|
+
tokens, line_info = _tokenise(src)
|
|
305
|
+
return Parser(tokens, line_info).parse()
|
|
295
306
|
|
|
296
307
|
|
|
297
308
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: tinyprove
|
|
3
|
-
Version: 1.
|
|
3
|
+
Version: 1.1.0
|
|
4
4
|
Summary: Tiny CoC-based theorem prover with inductive types
|
|
5
5
|
Project-URL: Homepage, https://github.com/pb1729/tinyprove
|
|
6
6
|
Project-URL: Repository, https://github.com/pb1729/tinyprove
|
|
@@ -199,7 +199,7 @@ In a similar way, while the type of a function will be a function type, the type
|
|
|
199
199
|
|
|
200
200
|
### Basic definitions from `get_usual_axioms`
|
|
201
201
|
|
|
202
|
-
Calling `get_usual_axioms()` produces a module containing some definitions that are expected to be widely useful. Most of them are not axioms in the true sense: They are ordinary tinyprove definitions
|
|
202
|
+
Calling `get_usual_axioms()` produces a module containing some definitions that are expected to be widely useful. Most of them are not axioms in the true sense: They are ordinary tinyprove definitions:
|
|
203
203
|
|
|
204
204
|
```
|
|
205
205
|
# false
|
|
@@ -303,7 +303,7 @@ contrapos: Π A: Type0 => Π B: Type0 => Π _: (Π _: A => B) => (Π _: ¬ B =>
|
|
|
303
303
|
|
|
304
304
|
First we obtain `(a_to_b a): B` from `a: A`. Then we obtain a contradiction by feeding it to `nb`. This completes our first theorem. Checking it in python:
|
|
305
305
|
|
|
306
|
-
```
|
|
306
|
+
```python3
|
|
307
307
|
base = get_usual_axioms()
|
|
308
308
|
|
|
309
309
|
my_module = def_module("""
|
|
@@ -316,7 +316,7 @@ contrapos: Π A: Type0 => Π B: Type0 => Π _: (Π _: A => B) => (Π _: ¬ B =>
|
|
|
316
316
|
|
|
317
317
|
### Inductive Types
|
|
318
318
|
|
|
319
|
-
One thing we need
|
|
319
|
+
One thing we need before we can do most of modern mathematics is the ability to define inductive types. `⊥`, `⊤`, `&`, `|`, `∃`, and even `≡` are all in fact defined as inductive types! You define an inductive type by defining its paramters, indices, and constructors. The `⊤` type does not have any parameters or indices, and only has one constructor, so it makes a nice starting example.
|
|
320
320
|
|
|
321
321
|
```
|
|
322
322
|
ι ⊤ () [] : Type0 {
|
|
@@ -355,7 +355,7 @@ xor: Π x: Bit => Π y: Bit => Bit
|
|
|
355
355
|
|
|
356
356
|
The new thing used here is `Bit.ind0`, the inductive principle for the `Bit` datatype. Here are the arguments that an inductive principal takes, in order:
|
|
357
357
|
* The **parameters** of the inductive type.
|
|
358
|
-
* The **motive**. For an inductive datatype `T` with indices `i_1...i_k` of types `I_1...I_k`, the motive is a function that accepts arguments `(i_1: I_1, ... i_k: I_k, instance: T)` and returns a type. If we use `T.ind0`, that type must be of type `Type0`, if we use `T.ind1`, that type must be of type `Type1`, etc. When the motive is applied to the scrutinee (see later in the list) and its indices, the result is the type returned by the entire inductive principle call.
|
|
358
|
+
* The **motive**. For an inductive datatype `T` with indices `i_1...i_k` of types `I_1...I_k`, the motive is a function that accepts arguments `(i_1: I_1, ... i_k: I_k, instance: T i_1 ... i_k)` and returns a type. If we use `T.ind0`, that type must be of type `Type0`, if we use `T.ind1`, that type must be of type `Type1`, etc. When the motive is applied to the scrutinee (see later in the list) and its indices, the result is the type returned by the entire inductive principle call.
|
|
359
359
|
* The **cases**. There is one of these for every constructor. These are values (if the corresponding constructor has no fields) or functions (if the corresponding constructor has fields). When fully applied, the type of the case for the constructor `cons` must match the type returned by the motive when passed `instance: T = (cons ...)`.
|
|
360
360
|
* Self-reference: The thing that makes inductive principles incredibly powerful is that inductive types can reference themselves (i.e. a `BinaryTree` type has a `branch` constructor with left and right subtrees as fields, where each subtree is a `BinaryTree` itself). For fields that reference the original type, the inductive principle becomes recursive. We can obtain a result for that field that we are then able to use in our case function. This is how induction on the natural numbers, or any inductively defined type such as a tree type or a list type, works.
|
|
361
361
|
* The **indices** of the **scrutinee**. Because things have types, we need to specify the indices so that the next argument has a known expected type.
|
|
@@ -472,7 +472,7 @@ The `Vector` type is parametrized by `A`. It's not parametrized by `n`, though.
|
|
|
472
472
|
|
|
473
473
|
The problem with making `n` a type parameter is the following: To make, say, a `Vector A 3` given `a: A`, we need to use `v_cons` to append `a` onto a `Vector A 2`. But if `n` was a parameter, then `Vector A 2` would be impossible to make into a field. Self-references are assumed by the machinery for `ι` definitions to share the same parameters. So we could only accept other `Vector A 3` instances, which is hardly useful. However, there's no requirement that self-references must share the same indices. Indeed, you may have noticed that self references were distinguished because they required us to put `[]` after the inductive type name. These brackets hold the indices of that type, though up until now they were empty because our types did not have any indices. By making `n` an index, we allow ourselves to use a `Vector A 2` to build a `Vector A 3`, even though the value of `n` varies between the two.
|
|
474
474
|
|
|
475
|
-
Indices must be computable from the constructor arguments in some way. For example, `v_empty` doesn't have constructor arguments, but the index produced is always just the constant `0`, so that's fine. `v_cons` does have arguments, and the produced index `+ 1 len` can be easily computed from them. We also see that the index for the self-reference `rest` is computable from the previous constructor parameters (
|
|
475
|
+
Indices must be computable from the constructor arguments in some way. For example, `v_empty` doesn't have constructor arguments, but the index produced is always just the constant `0`, so that's fine. `v_cons` does have arguments, and the produced index `+ 1 len` can be easily computed from them. We also see that the index for the self-reference `rest` is computable from the previous constructor parameters (it just takes `len`).
|
|
476
476
|
|
|
477
477
|
```
|
|
478
478
|
vec_map: Π A: Type0 => Π B: Type0 => Π f: (Π _: A => B) => Π len: ℕ => Π v_a: Vector A len => Vector B len
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|