tinyprove 0.1.4__tar.gz → 0.1.6__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: tinyprove
3
- Version: 0.1.4
3
+ Version: 0.1.6
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
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "tinyprove"
7
- version = "0.1.4"
7
+ version = "0.1.6"
8
8
  description = "Tiny CoC-based theorem prover with inductive types"
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.10"
@@ -52,28 +52,33 @@ class DefsExtend:
52
52
  return self.base_defns.match_reduce(key, argchain, defns)
53
53
 
54
54
 
55
- def type_level(ty:Term, ctx:List[Tuple[str, Term]], defns:Definitions):
55
+ def type_level(ty_whnf:Term, ctx:List[Tuple[str, Term]], defns:Definitions):
56
56
  """ Compute the effective universe level of a type. """
57
- ty_whnf = whnf(ty, defns)
58
57
  if isinstance(ty_whnf, Sort):
59
58
  return ty_whnf.level
60
59
  else:
61
60
  ty_ty = whnf(infer(ty_whnf, ctx, defns), defns)
62
61
  if not isinstance(ty_ty, Sort):
63
- raise TypecheckError(f"Expected {ty.str(ctx)} to be a Sort but found type {ty_ty.str(ctx)}.")
62
+ raise TypecheckError(f"Expected {ty_whnf.str(ctx)} to be a Sort but found type {ty_ty.str(ctx)}.")
64
63
  return ty_ty.level
65
64
 
66
65
 
67
- def typecheck_args(args:List[Tuple[str, IrNode]], ctx:List[Tuple[str, Term]], selfref_ir:IrNode, defns:Definitions, max_level:int|None=None):
66
+ def typecheck_args(args:List[Tuple[str, IrNode]], ctx:List[Tuple[str, Term]], selfref_ir:IrNode, defns:Definitions,
67
+ max_level:int|None=None, positive_typenm:str|None=None):
68
68
  """ Given a list of args that might be type parameters or constructor args,
69
69
  check that they are all some kind of Sort. Returns a full ctx containing all args. """
70
70
  if len(args) == 0: return ctx
71
71
  (arg_nm, arg_ty_ir), *args_rest = args
72
72
  arg_ty = selfref_sub(arg_ty_ir, selfref_ir).to_term(ctx_to_names(ctx))
73
- ty_level = type_level(arg_ty, ctx, defns) # typecheck and get the type's level
74
- if max_level is not None and ty_level > max_level:
73
+ arg_ty_whnf = whnf(arg_ty, defns)
74
+ ty_level = type_level(arg_ty_whnf, ctx, defns) # typecheck and get the type's level
75
+ if max_level is not None and ty_level > max_level: # ensure constructor arguments lie within the correct universe
75
76
  raise TypecheckError(f"Argument {arg_nm} is at the Type{ty_level} level but at most Type{max_level} would be sound.")
76
- return typecheck_args(args_rest, [(arg_nm, arg_ty)] + ctx, selfref_ir, defns, max_level)
77
+ if positive_typenm is not None: # do a positivity check if a typename that must be positive was specificed
78
+ if not all(walk(positive_typenm, arg_ty_whnf, defns)):
79
+ raise DefinitionError(f"Constructor arg {arg_nm} fails positivity check.")
80
+ return typecheck_args(args_rest, [(arg_nm, arg_ty)] + ctx, selfref_ir, defns,
81
+ max_level, positive_typenm)
77
82
 
78
83
 
79
84
  @dataclass(frozen=True)
@@ -122,28 +127,31 @@ def selfref_sub(node:IrNode, selfref_ir:IrNode) -> IrNode:
122
127
  raise DefinitionError("Unknown node type.")
123
128
 
124
129
 
125
- def walk(indty_name: str, node:IrNode, polarity:bool=True) -> bool:
126
- """ Do a polarity check of node to ensure that self-references to our inductive type are positive only.
130
+ def walk(indty_name: str, arg_ty:Term, defns:Definitions, polarity:bool=True) -> List[bool]:
131
+ """ Do a polarity check of node and returns a list of polarities of self-references.
127
132
  polarity: True = positive, and False = negative """
128
- match node:
129
- case IrLam():
133
+ arg_ty = whnf(arg_ty, defns)
134
+ match arg_ty:
135
+ case Lam():
130
136
  raise DefinitionError("Lambdas not supported in constructor definition.")
131
- case IrPi(param, A, B):
132
- return walk(indty_name, A, not polarity) and walk(indty_name, B, polarity)
133
- case IrApp(fn, arg):
134
- return walk(indty_name, fn, polarity) and walk(indty_name, arg, polarity)
135
- case IrSort():
136
- return True
137
- case IrVar(nm):
138
- return True
139
- case IrConst(name):
137
+ case Pi(param, A, B):
138
+ return walk(indty_name, A, defns, not polarity) + walk(indty_name, B, defns, polarity)
139
+ case App(fn, arg):
140
+ arg_appearances = [
141
+ False # this application could produce a result where the inductive type appears negatively
142
+ for _ in walk(indty_name, arg, defns, polarity)
143
+ ]
144
+ return walk(indty_name, fn, defns, polarity) + arg_appearances
145
+ case Sort():
146
+ return []
147
+ case Var():
148
+ return []
149
+ case Const(name):
140
150
  if name == indty_name:
141
- raise DefinitionError("Found an IrConst that refers back to the original inductive type! This should be done solely with IrInductiveSelfRef.")
142
- return True
143
- case IrInductiveSelfRef(index_vals):
144
- return polarity
151
+ return [polarity]
152
+ return []
145
153
  case _:
146
- raise DefinitionError("Unknown node type.")
154
+ raise DefinitionError("Unknown term.")
147
155
 
148
156
 
149
157
  class ConstructorDef:
@@ -155,8 +163,8 @@ class ConstructorDef:
155
163
  self.args = args
156
164
  self.output_indices = output_indices
157
165
  # type & positivity checking:
158
- constructor_ctx = typecheck_args(self.args, self.indty.params_ctx, self.indty.selfref_ir, self.indty.defns_ext, self.indty.get_level())
159
- self.check_positive()
166
+ constructor_ctx = typecheck_args(self.args, self.indty.params_ctx, self.indty.selfref_ir, self.indty.defns_ext,
167
+ max_level=self.indty.get_level(), positive_typenm=self.indty.name)
160
168
  self.check_output_indices(constructor_ctx, self.indty.inds_ctx)
161
169
  # prep data that will be used by match-reduce
162
170
  self.matchred_sig = self.get_matchred_sig()
@@ -190,10 +198,6 @@ class ConstructorDef:
190
198
  self.output_indices),
191
199
  applied_constructor_ty),
192
200
  converted_args[::-1])
193
- def check_positive(self):
194
- for arg_nm, arg_ty in self.args:
195
- if not walk(self.indty.name, arg_ty):
196
- raise DefinitionError(f"Constructor {self.name} arg {arg_nm} fails positivity check.")
197
201
  def check_output_indices(self, constructor_ctx:List[Tuple[str, Term]], inds_ctx:List[Tuple[str, Term]]):
198
202
  num_inds = len(self.indty.indices)
199
203
  assert len(self.output_indices) == num_inds, f"Constructor {self.name} definition has incorrect number of output indices."
@@ -16,14 +16,23 @@ _token_re = re.compile(
16
16
  [\[\]()\|,:=λΠδι]| # punctuation (unicode lambdas, etc.)
17
17
  """ + _ident_re + """ # identifiers
18
18
  )
19
+ \s* # skip trailing whitespace
19
20
  """,
20
21
  re.VERBOSE,
21
22
  )
22
23
 
23
24
 
24
25
  def _tokenise(src: str) -> List[str]:
25
- """Return a *list* of string tokens (whitespace already stripped)."""
26
- return [m.group(1) for m in _token_re.finditer(src)]
26
+ """ Return a list of string tokens (whitespace already stripped). """
27
+ pos = 0
28
+ ans = []
29
+ while pos < len(src):
30
+ m = _token_re.match(src, pos)
31
+ if not m:
32
+ raise SyntaxError(f"Unexpected character at position {pos}: {repr(src[pos])}")
33
+ ans.append(m.group(1))
34
+ pos = m.end()
35
+ return ans
27
36
 
28
37
 
29
38
  def strip_comments(src:str) -> str:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: tinyprove
3
- Version: 0.1.4
3
+ Version: 0.1.6
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
@@ -288,7 +288,7 @@ test("1 != 0",
288
288
  (Eq.ind.0 Nat (Nat.S Nat.Z)
289
289
  (λ y: Nat -> λ instance: (Eq Nat (Nat.S Nat.Z) y) -> (isSucc y)) # motive
290
290
  Unit.in # case refl : isSucc 1, reduces to Unit
291
- Nat.Z" # y := 0
291
+ Nat.Z # y := 0
292
292
  p
293
293
  )
294
294
  """
File without changes
File without changes
File without changes