tinyprove 0.1.3__tar.gz → 0.1.4__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.3
3
+ Version: 0.1.4
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.3"
7
+ version = "0.1.4"
8
8
  description = "Tiny CoC-based theorem prover with inductive types"
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.10"
@@ -188,22 +188,30 @@ def whnf(term:Term, defns:Definitions) -> Term:
188
188
  return ans
189
189
 
190
190
 
191
- def conv(t1:Term, t2:Term, defns:Definitions) -> bool:
191
+ def conv(t1:Term, t2:Term, defns:Definitions, accept_assignable:bool=False) -> bool:
192
+ """ Check structural equality for two terms. The check is exact and symmetric by
193
+ default, if accept_assignable is set, we instead check if t1 is assignable to t2. """
192
194
  t1 = whnf(t1, defns)
193
195
  t2 = whnf(t2, defns)
194
196
  match t1, t2:
195
197
  case Sort(l1), Sort(l2):
196
- return l1 == l2
198
+ return l1 == l2 or (accept_assignable and l1 <= l2)
197
199
  case Var(depth1), Var(depth2):
198
200
  return depth1 == depth2
199
201
  case Const(name1), Const(name2):
200
202
  return name1 == name2
201
203
  case App(fn1, arg1), App(fn2, arg2):
202
- return conv(fn1, fn2, defns) and conv(arg1, arg2, defns)
204
+ return (
205
+ conv(fn1, fn2, defns) and
206
+ conv(arg1, arg2, defns))
203
207
  case Pi(_, A1, B1), Pi(_, A2, B2):
204
- return conv(A1, A2, defns) and conv(B1, B2, defns)
208
+ return ( # NOTE: we preserve the accept_assignable flag for Pi types
209
+ conv(A2, A1, defns, accept_assignable) and # swap argument order to ensure contravariance
210
+ conv(B1, B2, defns, accept_assignable)) # output has same order for covariance
205
211
  case Lam(_, A1, body1), Lam(_, A2, body2):
206
- return conv(A1, A2, defns) and conv(body1, body2, defns)
212
+ return (
213
+ conv(A2, A1, defns) and
214
+ conv(body1, body2, defns))
207
215
  case _:
208
216
  return False # mismatched shapes
209
217
 
@@ -249,10 +257,16 @@ def infer(term:Term, ctx, defns:Definitions) -> Term:
249
257
  case _:
250
258
  raise TypecheckError(f"Failed to recognize term {term}")
251
259
 
260
+
261
+
262
+ def is_assignable(actual:Term, expected:Term, defns:Definitions) -> bool:
263
+ return conv(actual, expected, defns, accept_assignable=True)
264
+
265
+
252
266
  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)}.")
267
+ actual = infer(term, ctx, defns)
268
+ if not is_assignable(actual, expected, defns):
269
+ raise TypecheckError(f"Term {term.str(ctx)} not assignable to type {expected.str(ctx)}.\nActual type was {actual.str(ctx)}.")
256
270
 
257
271
 
258
272
 
@@ -306,7 +320,7 @@ class ConstDefinition:
306
320
  self.value = whnf(value, defns)
307
321
  self.type = whnf(infer(self.value, [], defns), defns)
308
322
  if expected_ty is not None:
309
- if not conv(self.type, expected_ty, defns):
323
+ if not is_assignable(self.type, expected_ty, defns):
310
324
  raise TypecheckError(f"When defining constant {self.name}, expected a type of {expected_ty.str([])} but actually got {self.type.str([])}.")
311
325
  self.used = find_used_defs(self.value)
312
326
  def get_type(self, key:List[str]) -> Term:
@@ -47,19 +47,33 @@ class DefsExtend:
47
47
  def __contains__(self, key:str) -> bool:
48
48
  return key in self.new_defs_dict or key in self.base_defns
49
49
  def match_reduce(self, key:str, argchain: List[Term], defns:Definitions) -> Tuple[Term, List[Term]] | None:
50
+ if key in self.new_defs_dict:
51
+ return None # Adding new reduction rules is not supported here
50
52
  return self.base_defns.match_reduce(key, argchain, defns)
51
53
 
52
54
 
53
- def typecheck_args(args:List[Tuple[str, IrNode]], ctx:List[Tuple[str, Term]], selfref_ir:IrNode, defns:Definitions):
55
+ def type_level(ty:Term, ctx:List[Tuple[str, Term]], defns:Definitions):
56
+ """ Compute the effective universe level of a type. """
57
+ ty_whnf = whnf(ty, defns)
58
+ if isinstance(ty_whnf, Sort):
59
+ return ty_whnf.level
60
+ else:
61
+ ty_ty = whnf(infer(ty_whnf, ctx, defns), defns)
62
+ 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)}.")
64
+ return ty_ty.level
65
+
66
+
67
+ def typecheck_args(args:List[Tuple[str, IrNode]], ctx:List[Tuple[str, Term]], selfref_ir:IrNode, defns:Definitions, max_level:int|None=None):
54
68
  """ Given a list of args that might be type parameters or constructor args,
55
69
  check that they are all some kind of Sort. Returns a full ctx containing all args. """
56
70
  if len(args) == 0: return ctx
57
71
  (arg_nm, arg_ty_ir), *args_rest = args
58
72
  arg_ty = selfref_sub(arg_ty_ir, selfref_ir).to_term(ctx_to_names(ctx))
59
- arg_ty_ty = infer(arg_ty, ctx, defns)
60
- if not isinstance(arg_ty_ty, Sort):
61
- raise TypecheckError(f"Expected {arg_ty.str(ctx)} to be a Sort but found type {ty_ty.str(ctx)}.")
62
- return typecheck_args(args_rest, [(arg_nm, arg_ty)] + ctx, selfref_ir, defns)
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:
75
+ 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)
63
77
 
64
78
 
65
79
  @dataclass(frozen=True)
@@ -141,7 +155,7 @@ class ConstructorDef:
141
155
  self.args = args
142
156
  self.output_indices = output_indices
143
157
  # type & positivity checking:
144
- constructor_ctx = typecheck_args(self.args, self.indty.params_ctx, self.indty.selfref_ir, self.indty.defns_ext)
158
+ constructor_ctx = typecheck_args(self.args, self.indty.params_ctx, self.indty.selfref_ir, self.indty.defns_ext, self.indty.get_level())
145
159
  self.check_positive()
146
160
  self.check_output_indices(constructor_ctx, self.indty.inds_ctx)
147
161
  # prep data that will be used by match-reduce
@@ -252,6 +266,8 @@ class InductiveDef:
252
266
  IrConst(self.name),
253
267
  [IrVar(nm) for nm, _ in self.params]
254
268
  )
269
+ def get_level(self) -> int:
270
+ return self.sort.level
255
271
  def get_index_vars(self) -> List[IrNode]:
256
272
  """ Get a list of vars corresponding to this inductive's indices.
257
273
  Note: Assumes that the vars will be present somewhere in the context. """
@@ -348,5 +364,3 @@ class InductiveDef:
348
364
 
349
365
 
350
366
 
351
-
352
-
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: tinyprove
3
- Version: 0.1.3
3
+ Version: 0.1.4
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
File without changes
File without changes
File without changes
File without changes