diff --git a/abstract_syntax/ops.py b/abstract_syntax/ops.py index f850fc21..9992c443 100644 --- a/abstract_syntax/ops.py +++ b/abstract_syntax/ops.py @@ -291,6 +291,19 @@ def loc_prefix(loc: Meta | None) -> str: 'Post-typecheck AST sanity check failed: pre-uniquify `Var` ' 'nodes still present.\n' + '\n'.join(msgs)) +def _statement_scope(stmt: Statement, seen: dict[str, int]) -> str: + """The scope segment of a top-level statement: its name (or, if it + has none, its keyword, e.g. ``assert``), then how many earlier + statements had the same, so ``theorem foo`` is ``3foo_`` and a second + ``foo`` is ``3foo1_``. The length prefix keeps segments, and the + scopes made by nesting them (an import's module inside the import), + distinct from each other.""" + name = getattr(stmt, 'name', None) + key = name if isinstance(name, str) else type(stmt).__name__.lower() + k = seen.get(key, 0) + seen[key] = k + 1 + return str(len(key)) + key + (str(k) if k else '') + '_' + def uniquify_deduce(ast: Sequence[Statement], ctx: UniquifyContext) -> list[Statement]: """Uniquify ``ast`` against ``ctx``'s counter, returning the post-uniquify AST. @@ -306,13 +319,14 @@ def uniquify_deduce(ast: Sequence[Statement], ctx: UniquifyContext) -> list[Stat prelude names. Tests typically pass a fresh context for reproducibility. - Each top-level statement gets its own scope segment (``"sN_"`` - appended to the caller's scope, ``name_id`` reset to ``0``) so - edits confined to statement *N* don't perturb the bound-name - suffixes in statement *M > N*. That edit-invariance is what - Phase-3 Step 14's dependency-aware cache invalidation relies on - -- without it, the structural hash of every downstream statement - drifts on every edit and the cache is useless. + Each top-level statement gets its own scope segment (see + ``_statement_scope``, appended to the caller's scope, ``name_id`` + reset to ``0``), named after the statement rather than its position, + so neither an edit inside one statement nor inserting or deleting + one perturbs the names in the others. That is what the + per-statement caches (``checker_cache``) rely on -- without it, the + structural hash of every downstream statement drifts and the caches + miss (#1303). """ env: dict[str, object] = {} env['≠'] = ['≠'] @@ -322,8 +336,9 @@ def uniquify_deduce(ast: Sequence[Statement], ctx: UniquifyContext) -> list[Stat outer_scope = ctx.scope outer_name_id = ctx.name_id result: list[Statement] = [] - for i, stmt in enumerate(ast): - ctx.scope = outer_scope + 's' + str(i) + '_' + seen: dict[str, int] = {} + for stmt in ast: + ctx.scope = outer_scope + _statement_scope(stmt, seen) ctx.name_id = 0 result.append(stmt.uniquify(env, ctx)) # Restore the caller's scope/counter -- the per-statement reset is diff --git a/checker_cache.py b/checker_cache.py index 5c75fca0..7f5047c7 100644 --- a/checker_cache.py +++ b/checker_cache.py @@ -243,17 +243,28 @@ def _collect_defined_names(stmt: Statement) -> set[str]: return names -def _is_global_barrier(stmt: Statement) -> bool: - """Statements with global side-effects on later checking. +# Statements whose effect on checking other statements goes only +# through the names they define. Every other kind is a barrier: views +# and models are found by type, associativity by operator and type, +# imports bring in whole modules, and so on. +_NAME_ONLY_STATEMENTS = ( + Assert, Define, GenRecFun, Postulate, PostulateFun, PostulateType, + Predicate, Print, RecFun, Theorem, TypeAlias, Union, ViewRecFun, +) - ``Import`` brings a module's exports into the environment; - ``Auto`` registers a rewrite rule consulted by every later - proof. Conservatively treat both as a barrier: every later - statement implicitly depends on them, so editing or removing - one invalidates the cache for everything after. So are concepts - and models (#1264): a where formula names its concepts by string, - and a model is found by its concept and type, never by name.""" - return isinstance(stmt, (Import, Auto, ConceptDecl, ModelDecl)) + +def _is_global_barrier(stmt: Statement) -> bool: + """Statements with global side-effects on later checking: every + statement except those whose effect goes only through the names they + define (``_NAME_ONLY_STATEMENTS``). ``Import`` brings in a module's + exports, ``Auto`` registers a rewrite rule consulted by every later + proof, ``inductive`` an induction principle, concepts and models are + found by concept and type (#1264), views by type, and so on. Every + later statement implicitly depends on them, so editing or removing + one invalidates the cache for everything after. (Since #1303 removing + a statement no longer renames the ones after it, so this is what + invalidates them.)""" + return not isinstance(stmt, _NAME_ONLY_STATEMENTS) # --------------------------------------------------------------------------- @@ -280,14 +291,6 @@ def _is_global_barrier(stmt: Statement) -> bool: _TypeCheckEntry = tuple[int, tuple[int, int], Statement] _type_check_cache: dict[str, dict[tuple[int, int], _TypeCheckEntry]] = {} -# Statements whose effect on type checking other statements goes only -# through the names they define. Every other kind is a barrier: views -# and models are found by type, associativity by operator and type, -# imports bring in whole modules, and so on. -_NAME_ONLY_STATEMENTS = ( - Assert, Define, GenRecFun, Postulate, PostulateFun, PostulateType, - Predicate, Print, RecFun, Theorem, TypeAlias, Union, ViewRecFun, -) # The statements whose type check is cached: the ones that do real # work in ``type_check_stmt`` and have no side effect there. @@ -308,7 +311,7 @@ def type_check_fingerprints(stmts: list[Statement], for n in _collect_defined_names(s): defined_to_idx.setdefault(n, i) barriers = hash(tuple(h for s, h in zip(stmts, hashes) - if not isinstance(s, _NAME_ONLY_STATEMENTS))) + if _is_global_barrier(s))) deps_fps: list[int] = [] full_fps: list[int] = [] # a statement's hash folded with its deps for i, s in enumerate(stmts): diff --git a/docs/lsp-plan.md b/docs/lsp-plan.md index 373933f5..47bbf0f7 100644 --- a/docs/lsp-plan.md +++ b/docs/lsp-plan.md @@ -100,7 +100,7 @@ Originally framed as optional ("only if 3s/check turns out to be too slow") but - [x] **Step 14: Dependency-aware invalidation.** Walk each statement's post-uniquify AST to collect referenced names; invalidate dependents on edit. - *Acceptance:* edit theorem `T1`; assert `T2` (which uses `T1`) is invalidated and rechecked. - - *Implementation:* `_collect_referenced_names` walks the post-uniquify AST gathering every `VarRef` / `PVar` name; `_collect_defined_names` returns the names a top-level statement introduces (its own name plus union constructors / predicate rules / synthesised induction lemmas). `check_deduce`'s third loop maintains a `defined_to_idx` map and replaces Step 13's `chain_hash` with `deps_fingerprint = hash(tuple(stmt_hashes_so_far[j] for j in sorted(dep_indices)))` — only the prior statements *this* one references contribute. `Import` and `Auto` are treated as global barriers (every later statement implicitly depends on them) since they have effects observable everywhere downstream. `Auto` additionally propagates its referenced names into every later statement's dependency set, so editing an `auto`'d theorem invalidates downstream proofs that rely on the implicit rewrite without textually mentioning it. Required a foundational fix in `uniquify_deduce`: each top-level statement now gets its own `s_` scope segment with `name_id` reset, so an edit confined to statement *N* no longer shifts the bound-name suffixes of statement *M > N* — without that, every downstream statement's stmt_hash drifts on every edit and the cache is useless. Acceptance test in `test/lsp/test_check_proofs_cache.py::test_editing_T1_invalidates_T2_that_uses_it`; complement test pins the negative direction (unrelated downstream stays a hit), plus an `auto`-aware test for the implicit-dep case. Known limitation tracked in [#368](https://github.com/jsiek/deduce/issues/368): `proof_checker.name_id` is still process-monotonic and can perturb stmt hashes for modules that exercise predicate desugaring / induction translation — fold it into a context object or snapshot it via the LSP state machinery. + - *Implementation:* `_collect_referenced_names` walks the post-uniquify AST gathering every `VarRef` / `PVar` name; `_collect_defined_names` returns the names a top-level statement introduces (its own name plus union constructors / predicate rules / synthesised induction lemmas). `check_deduce`'s third loop maintains a `defined_to_idx` map and replaces Step 13's `chain_hash` with `deps_fingerprint = hash(tuple(stmt_hashes_so_far[j] for j in sorted(dep_indices)))` — only the prior statements *this* one references contribute. `Import` and `Auto` are treated as global barriers (every later statement implicitly depends on them) since they have effects observable everywhere downstream. `Auto` additionally propagates its referenced names into every later statement's dependency set, so editing an `auto`'d theorem invalidates downstream proofs that rely on the implicit rewrite without textually mentioning it. Required a foundational fix in `uniquify_deduce`: each top-level statement now gets its own `s_` scope segment with `name_id` reset, so an edit confined to statement *N* no longer shifts the bound-name suffixes of statement *M > N* (since #1303 the segment is named after the statement, `_`, not its index *N*, so inserting or deleting a statement doesn't rename the others either; with that, every statement that is not a plain named definition became a barrier, since removing one no longer happens to invalidate everything after it) — without that, every downstream statement's stmt_hash drifts on every edit and the cache is useless. Acceptance test in `test/lsp/test_check_proofs_cache.py::test_editing_T1_invalidates_T2_that_uses_it`; complement test pins the negative direction (unrelated downstream stays a hit), plus an `auto`-aware test for the implicit-dep case. Known limitation tracked in [#368](https://github.com/jsiek/deduce/issues/368): `proof_checker.name_id` is still process-monotonic and can perturb stmt hashes for modules that exercise predicate desugaring / induction translation — fold it into a context object or snapshot it via the LSP state machinery. ## Phase 4 — Structured proof-editing operations (Agda-like) diff --git a/test/compile/lower/array.cir b/test/compile/lower/array.cir index f021b537..0c126765 100644 --- a/test/compile/lower/array.cir +++ b/test/compile/lower/array.cir @@ -1,8 +1,8 @@ -union MyNat.s0_0 {zero.s0_1/0, suc.s0_2/1} -union List.s1_0 {empty.s1_2/0, node.s1_3/2} -global ns.s2_0 = node.s1_3(zero.s0_1, node.s1_3(suc.s0_2(zero.s0_1), node.s1_3(suc.s0_2(suc.s0_2(zero.s0_1)), empty.s1_2))) -global A.s3_0 = array(ns.s2_0) -print A.s3_0 -print A.s3_0[zero.s0_1] -print A.s3_0[suc.s0_2(zero.s0_1)] -print A.s3_0[suc.s0_2(suc.s0_2(zero.s0_1))] +union MyNat.5MyNat_0 {zero.5MyNat_1/0, suc.5MyNat_2/1} +union List.4List_0 {empty.4List_2/0, node.4List_3/2} +global ns.2ns_0 = node.4List_3(zero.5MyNat_1, node.4List_3(suc.5MyNat_2(zero.5MyNat_1), node.4List_3(suc.5MyNat_2(suc.5MyNat_2(zero.5MyNat_1)), empty.4List_2))) +global A.1A_0 = array(ns.2ns_0) +print A.1A_0 +print A.1A_0[zero.5MyNat_1] +print A.1A_0[suc.5MyNat_2(zero.5MyNat_1)] +print A.1A_0[suc.5MyNat_2(suc.5MyNat_2(zero.5MyNat_1))] diff --git a/test/compile/lower/array.ir b/test/compile/lower/array.ir index f021b537..0c126765 100644 --- a/test/compile/lower/array.ir +++ b/test/compile/lower/array.ir @@ -1,8 +1,8 @@ -union MyNat.s0_0 {zero.s0_1/0, suc.s0_2/1} -union List.s1_0 {empty.s1_2/0, node.s1_3/2} -global ns.s2_0 = node.s1_3(zero.s0_1, node.s1_3(suc.s0_2(zero.s0_1), node.s1_3(suc.s0_2(suc.s0_2(zero.s0_1)), empty.s1_2))) -global A.s3_0 = array(ns.s2_0) -print A.s3_0 -print A.s3_0[zero.s0_1] -print A.s3_0[suc.s0_2(zero.s0_1)] -print A.s3_0[suc.s0_2(suc.s0_2(zero.s0_1))] +union MyNat.5MyNat_0 {zero.5MyNat_1/0, suc.5MyNat_2/1} +union List.4List_0 {empty.4List_2/0, node.4List_3/2} +global ns.2ns_0 = node.4List_3(zero.5MyNat_1, node.4List_3(suc.5MyNat_2(zero.5MyNat_1), node.4List_3(suc.5MyNat_2(suc.5MyNat_2(zero.5MyNat_1)), empty.4List_2))) +global A.1A_0 = array(ns.2ns_0) +print A.1A_0 +print A.1A_0[zero.5MyNat_1] +print A.1A_0[suc.5MyNat_2(zero.5MyNat_1)] +print A.1A_0[suc.5MyNat_2(suc.5MyNat_2(zero.5MyNat_1))] diff --git a/test/compile/lower/array.pir b/test/compile/lower/array.pir index f021b537..0c126765 100644 --- a/test/compile/lower/array.pir +++ b/test/compile/lower/array.pir @@ -1,8 +1,8 @@ -union MyNat.s0_0 {zero.s0_1/0, suc.s0_2/1} -union List.s1_0 {empty.s1_2/0, node.s1_3/2} -global ns.s2_0 = node.s1_3(zero.s0_1, node.s1_3(suc.s0_2(zero.s0_1), node.s1_3(suc.s0_2(suc.s0_2(zero.s0_1)), empty.s1_2))) -global A.s3_0 = array(ns.s2_0) -print A.s3_0 -print A.s3_0[zero.s0_1] -print A.s3_0[suc.s0_2(zero.s0_1)] -print A.s3_0[suc.s0_2(suc.s0_2(zero.s0_1))] +union MyNat.5MyNat_0 {zero.5MyNat_1/0, suc.5MyNat_2/1} +union List.4List_0 {empty.4List_2/0, node.4List_3/2} +global ns.2ns_0 = node.4List_3(zero.5MyNat_1, node.4List_3(suc.5MyNat_2(zero.5MyNat_1), node.4List_3(suc.5MyNat_2(suc.5MyNat_2(zero.5MyNat_1)), empty.4List_2))) +global A.1A_0 = array(ns.2ns_0) +print A.1A_0 +print A.1A_0[zero.5MyNat_1] +print A.1A_0[suc.5MyNat_2(zero.5MyNat_1)] +print A.1A_0[suc.5MyNat_2(suc.5MyNat_2(zero.5MyNat_1))] diff --git a/test/compile/lower/basic.cir b/test/compile/lower/basic.cir index c42b88ae..bf39d43b 100644 --- a/test/compile/lower/basic.cir +++ b/test/compile/lower/basic.cir @@ -1,7 +1,7 @@ -union MyNat.s0_0 {zero.s0_1/0, suc.s0_2/1} -fn add.s1_0($scr0, m.s1_1) = match $scr0 { | zero.s0_1 -> m.s1_1 | suc.s0_2(n.s1_2) -> suc.s0_2(add.s1_0(n.s1_2, m.s1_1)) } -global two.s2_0 = suc.s0_2(suc.s0_2(zero.s0_1)) -fn add_two.s3_1(x.s3_0) = add.s1_0(two.s2_0, x.s3_0) -fn pick.s4_3(b.s4_0, x.s4_1, y.s4_2) = if b.s4_0 then x.s4_1 else y.s4_2 -print add_two.s3_1(suc.s0_2(zero.s0_1)) -print pick.s4_3(true, two.s2_0, zero.s0_1) +union MyNat.5MyNat_0 {zero.5MyNat_1/0, suc.5MyNat_2/1} +fn add.3add_0($scr0, m.3add_1) = match $scr0 { | zero.5MyNat_1 -> m.3add_1 | suc.5MyNat_2(n.3add_2) -> suc.5MyNat_2(add.3add_0(n.3add_2, m.3add_1)) } +global two.3two_0 = suc.5MyNat_2(suc.5MyNat_2(zero.5MyNat_1)) +fn add_two.7add_two_1(x.7add_two_0) = add.3add_0(two.3two_0, x.7add_two_0) +fn pick.4pick_3(b.4pick_0, x.4pick_1, y.4pick_2) = if b.4pick_0 then x.4pick_1 else y.4pick_2 +print add_two.7add_two_1(suc.5MyNat_2(zero.5MyNat_1)) +print pick.4pick_3(true, two.3two_0, zero.5MyNat_1) diff --git a/test/compile/lower/basic.ir b/test/compile/lower/basic.ir index c42b88ae..bf39d43b 100644 --- a/test/compile/lower/basic.ir +++ b/test/compile/lower/basic.ir @@ -1,7 +1,7 @@ -union MyNat.s0_0 {zero.s0_1/0, suc.s0_2/1} -fn add.s1_0($scr0, m.s1_1) = match $scr0 { | zero.s0_1 -> m.s1_1 | suc.s0_2(n.s1_2) -> suc.s0_2(add.s1_0(n.s1_2, m.s1_1)) } -global two.s2_0 = suc.s0_2(suc.s0_2(zero.s0_1)) -fn add_two.s3_1(x.s3_0) = add.s1_0(two.s2_0, x.s3_0) -fn pick.s4_3(b.s4_0, x.s4_1, y.s4_2) = if b.s4_0 then x.s4_1 else y.s4_2 -print add_two.s3_1(suc.s0_2(zero.s0_1)) -print pick.s4_3(true, two.s2_0, zero.s0_1) +union MyNat.5MyNat_0 {zero.5MyNat_1/0, suc.5MyNat_2/1} +fn add.3add_0($scr0, m.3add_1) = match $scr0 { | zero.5MyNat_1 -> m.3add_1 | suc.5MyNat_2(n.3add_2) -> suc.5MyNat_2(add.3add_0(n.3add_2, m.3add_1)) } +global two.3two_0 = suc.5MyNat_2(suc.5MyNat_2(zero.5MyNat_1)) +fn add_two.7add_two_1(x.7add_two_0) = add.3add_0(two.3two_0, x.7add_two_0) +fn pick.4pick_3(b.4pick_0, x.4pick_1, y.4pick_2) = if b.4pick_0 then x.4pick_1 else y.4pick_2 +print add_two.7add_two_1(suc.5MyNat_2(zero.5MyNat_1)) +print pick.4pick_3(true, two.3two_0, zero.5MyNat_1) diff --git a/test/compile/lower/basic.pir b/test/compile/lower/basic.pir index c42b88ae..bf39d43b 100644 --- a/test/compile/lower/basic.pir +++ b/test/compile/lower/basic.pir @@ -1,7 +1,7 @@ -union MyNat.s0_0 {zero.s0_1/0, suc.s0_2/1} -fn add.s1_0($scr0, m.s1_1) = match $scr0 { | zero.s0_1 -> m.s1_1 | suc.s0_2(n.s1_2) -> suc.s0_2(add.s1_0(n.s1_2, m.s1_1)) } -global two.s2_0 = suc.s0_2(suc.s0_2(zero.s0_1)) -fn add_two.s3_1(x.s3_0) = add.s1_0(two.s2_0, x.s3_0) -fn pick.s4_3(b.s4_0, x.s4_1, y.s4_2) = if b.s4_0 then x.s4_1 else y.s4_2 -print add_two.s3_1(suc.s0_2(zero.s0_1)) -print pick.s4_3(true, two.s2_0, zero.s0_1) +union MyNat.5MyNat_0 {zero.5MyNat_1/0, suc.5MyNat_2/1} +fn add.3add_0($scr0, m.3add_1) = match $scr0 { | zero.5MyNat_1 -> m.3add_1 | suc.5MyNat_2(n.3add_2) -> suc.5MyNat_2(add.3add_0(n.3add_2, m.3add_1)) } +global two.3two_0 = suc.5MyNat_2(suc.5MyNat_2(zero.5MyNat_1)) +fn add_two.7add_two_1(x.7add_two_0) = add.3add_0(two.3two_0, x.7add_two_0) +fn pick.4pick_3(b.4pick_0, x.4pick_1, y.4pick_2) = if b.4pick_0 then x.4pick_1 else y.4pick_2 +print add_two.7add_two_1(suc.5MyNat_2(zero.5MyNat_1)) +print pick.4pick_3(true, two.3two_0, zero.5MyNat_1) diff --git a/test/compile/lower/closure.cir b/test/compile/lower/closure.cir index dcee5928..a83bf204 100644 --- a/test/compile/lower/closure.cir +++ b/test/compile/lower/closure.cir @@ -1,5 +1,5 @@ -union MyNat.s0_0 {zero.s0_1/0, suc.s0_2/1} -fn add.s1_0($scr0, m.s1_1) = match $scr0 { | zero.s0_1 -> m.s1_1 | suc.s0_2(n.s1_2) -> suc.s0_2(add.s1_0(n.s1_2, m.s1_1)) } -fn adder.s2_2(x.s2_0) = closure[closure$lam1](x.s2_0) -print adder.s2_2(suc.s0_2(zero.s0_1))(suc.s0_2(suc.s0_2(zero.s0_1))) -fn closure$lam1[x.s2_0](y.s2_1) = add.s1_0(x.s2_0, y.s2_1) +union MyNat.5MyNat_0 {zero.5MyNat_1/0, suc.5MyNat_2/1} +fn add.3add_0($scr0, m.3add_1) = match $scr0 { | zero.5MyNat_1 -> m.3add_1 | suc.5MyNat_2(n.3add_2) -> suc.5MyNat_2(add.3add_0(n.3add_2, m.3add_1)) } +fn adder.5adder_2(x.5adder_0) = closure[closure$lam1](x.5adder_0) +print adder.5adder_2(suc.5MyNat_2(zero.5MyNat_1))(suc.5MyNat_2(suc.5MyNat_2(zero.5MyNat_1))) +fn closure$lam1[x.5adder_0](y.5adder_1) = add.3add_0(x.5adder_0, y.5adder_1) diff --git a/test/compile/lower/closure.ir b/test/compile/lower/closure.ir index ad8842ff..16bd8cc2 100644 --- a/test/compile/lower/closure.ir +++ b/test/compile/lower/closure.ir @@ -1,4 +1,4 @@ -union MyNat.s0_0 {zero.s0_1/0, suc.s0_2/1} -fn add.s1_0($scr0, m.s1_1) = match $scr0 { | zero.s0_1 -> m.s1_1 | suc.s0_2(n.s1_2) -> suc.s0_2(add.s1_0(n.s1_2, m.s1_1)) } -fn adder.s2_2(x.s2_0) = λ(y.s2_1). add.s1_0(x.s2_0, y.s2_1) -print adder.s2_2(suc.s0_2(zero.s0_1))(suc.s0_2(suc.s0_2(zero.s0_1))) +union MyNat.5MyNat_0 {zero.5MyNat_1/0, suc.5MyNat_2/1} +fn add.3add_0($scr0, m.3add_1) = match $scr0 { | zero.5MyNat_1 -> m.3add_1 | suc.5MyNat_2(n.3add_2) -> suc.5MyNat_2(add.3add_0(n.3add_2, m.3add_1)) } +fn adder.5adder_2(x.5adder_0) = λ(y.5adder_1). add.3add_0(x.5adder_0, y.5adder_1) +print adder.5adder_2(suc.5MyNat_2(zero.5MyNat_1))(suc.5MyNat_2(suc.5MyNat_2(zero.5MyNat_1))) diff --git a/test/compile/lower/closure.pir b/test/compile/lower/closure.pir index dcee5928..a83bf204 100644 --- a/test/compile/lower/closure.pir +++ b/test/compile/lower/closure.pir @@ -1,5 +1,5 @@ -union MyNat.s0_0 {zero.s0_1/0, suc.s0_2/1} -fn add.s1_0($scr0, m.s1_1) = match $scr0 { | zero.s0_1 -> m.s1_1 | suc.s0_2(n.s1_2) -> suc.s0_2(add.s1_0(n.s1_2, m.s1_1)) } -fn adder.s2_2(x.s2_0) = closure[closure$lam1](x.s2_0) -print adder.s2_2(suc.s0_2(zero.s0_1))(suc.s0_2(suc.s0_2(zero.s0_1))) -fn closure$lam1[x.s2_0](y.s2_1) = add.s1_0(x.s2_0, y.s2_1) +union MyNat.5MyNat_0 {zero.5MyNat_1/0, suc.5MyNat_2/1} +fn add.3add_0($scr0, m.3add_1) = match $scr0 { | zero.5MyNat_1 -> m.3add_1 | suc.5MyNat_2(n.3add_2) -> suc.5MyNat_2(add.3add_0(n.3add_2, m.3add_1)) } +fn adder.5adder_2(x.5adder_0) = closure[closure$lam1](x.5adder_0) +print adder.5adder_2(suc.5MyNat_2(zero.5MyNat_1))(suc.5MyNat_2(suc.5MyNat_2(zero.5MyNat_1))) +fn closure$lam1[x.5adder_0](y.5adder_1) = add.3add_0(x.5adder_0, y.5adder_1) diff --git a/test/compile/lower/generic.cir b/test/compile/lower/generic.cir index ad95b11a..a1f03bcd 100644 --- a/test/compile/lower/generic.cir +++ b/test/compile/lower/generic.cir @@ -1,6 +1,6 @@ -union Box.s0_0 {box.s0_2/1} -fn unbox.s1_3(b.s1_1) = match b.s1_1 { | box.s0_2(x.s1_2) -> x.s1_2 } -fn id.s2_3(x.s2_2) = x.s2_2 -union MyNat.s3_0 {zero.s3_1/0, suc.s3_2/1} -print id.s2_3(suc.s3_2(zero.s3_1)) -print unbox.s1_3(box.s0_2(suc.s3_2(suc.s3_2(zero.s3_1)))) +union Box.3Box_0 {box.3Box_2/1} +fn unbox.5unbox_3(b.5unbox_1) = match b.5unbox_1 { | box.3Box_2(x.5unbox_2) -> x.5unbox_2 } +fn id.2id_3(x.2id_2) = x.2id_2 +union MyNat.5MyNat_0 {zero.5MyNat_1/0, suc.5MyNat_2/1} +print id.2id_3(suc.5MyNat_2(zero.5MyNat_1)) +print unbox.5unbox_3(box.3Box_2(suc.5MyNat_2(suc.5MyNat_2(zero.5MyNat_1)))) diff --git a/test/compile/lower/generic.ir b/test/compile/lower/generic.ir index ad95b11a..a1f03bcd 100644 --- a/test/compile/lower/generic.ir +++ b/test/compile/lower/generic.ir @@ -1,6 +1,6 @@ -union Box.s0_0 {box.s0_2/1} -fn unbox.s1_3(b.s1_1) = match b.s1_1 { | box.s0_2(x.s1_2) -> x.s1_2 } -fn id.s2_3(x.s2_2) = x.s2_2 -union MyNat.s3_0 {zero.s3_1/0, suc.s3_2/1} -print id.s2_3(suc.s3_2(zero.s3_1)) -print unbox.s1_3(box.s0_2(suc.s3_2(suc.s3_2(zero.s3_1)))) +union Box.3Box_0 {box.3Box_2/1} +fn unbox.5unbox_3(b.5unbox_1) = match b.5unbox_1 { | box.3Box_2(x.5unbox_2) -> x.5unbox_2 } +fn id.2id_3(x.2id_2) = x.2id_2 +union MyNat.5MyNat_0 {zero.5MyNat_1/0, suc.5MyNat_2/1} +print id.2id_3(suc.5MyNat_2(zero.5MyNat_1)) +print unbox.5unbox_3(box.3Box_2(suc.5MyNat_2(suc.5MyNat_2(zero.5MyNat_1)))) diff --git a/test/compile/lower/generic.pir b/test/compile/lower/generic.pir index ad95b11a..a1f03bcd 100644 --- a/test/compile/lower/generic.pir +++ b/test/compile/lower/generic.pir @@ -1,6 +1,6 @@ -union Box.s0_0 {box.s0_2/1} -fn unbox.s1_3(b.s1_1) = match b.s1_1 { | box.s0_2(x.s1_2) -> x.s1_2 } -fn id.s2_3(x.s2_2) = x.s2_2 -union MyNat.s3_0 {zero.s3_1/0, suc.s3_2/1} -print id.s2_3(suc.s3_2(zero.s3_1)) -print unbox.s1_3(box.s0_2(suc.s3_2(suc.s3_2(zero.s3_1)))) +union Box.3Box_0 {box.3Box_2/1} +fn unbox.5unbox_3(b.5unbox_1) = match b.5unbox_1 { | box.3Box_2(x.5unbox_2) -> x.5unbox_2 } +fn id.2id_3(x.2id_2) = x.2id_2 +union MyNat.5MyNat_0 {zero.5MyNat_1/0, suc.5MyNat_2/1} +print id.2id_3(suc.5MyNat_2(zero.5MyNat_1)) +print unbox.5unbox_3(box.3Box_2(suc.5MyNat_2(suc.5MyNat_2(zero.5MyNat_1)))) diff --git a/test/compile/lower/genrecfun.cir b/test/compile/lower/genrecfun.cir index ad41257c..104ada7a 100644 --- a/test/compile/lower/genrecfun.cir +++ b/test/compile/lower/genrecfun.cir @@ -1,6 +1,6 @@ -union MyNat.s0_0 {zero.s0_1/0, suc.s0_2/1} -fn iszero.s1_2(n.s1_0) = match n.s1_0 { | zero.s0_1 -> true | suc.s0_2(n'.s1_1) -> false } -fn pred.s2_2(n.s2_0) = match n.s2_0 { | zero.s0_1 -> zero.s0_1 | suc.s0_2(n'.s2_1) -> n'.s2_1 } -fn <.s3_0($scr0, m.s3_1) = match $scr0 { | zero.s0_1 -> match m.s3_1 { | zero.s0_1 -> false | suc.s0_2(m'.s3_2) -> true } | suc.s0_2(n'.s3_3) -> match m.s3_1 { | zero.s0_1 -> false | suc.s0_2(m'.s3_5) -> <.s3_0(n'.s3_3, m'.s3_5) } } -fn count_down.s6_0(n.s6_1) = if iszero.s1_2(n.s6_1) then zero.s0_1 else count_down.s6_0(pred.s2_2(n.s6_1)) -print count_down.s6_0(suc.s0_2(suc.s0_2(suc.s0_2(zero.s0_1)))) +union MyNat.5MyNat_0 {zero.5MyNat_1/0, suc.5MyNat_2/1} +fn iszero.6iszero_2(n.6iszero_0) = match n.6iszero_0 { | zero.5MyNat_1 -> true | suc.5MyNat_2(n'.6iszero_1) -> false } +fn pred.4pred_2(n.4pred_0) = match n.4pred_0 { | zero.5MyNat_1 -> zero.5MyNat_1 | suc.5MyNat_2(n'.4pred_1) -> n'.4pred_1 } +fn <.1<_0($scr0, m.1<_1) = match $scr0 { | zero.5MyNat_1 -> match m.1<_1 { | zero.5MyNat_1 -> false | suc.5MyNat_2(m'.1<_2) -> true } | suc.5MyNat_2(n'.1<_3) -> match m.1<_1 { | zero.5MyNat_1 -> false | suc.5MyNat_2(m'.1<_5) -> <.1<_0(n'.1<_3, m'.1<_5) } } +fn count_down.10count_down_0(n.10count_down_1) = if iszero.6iszero_2(n.10count_down_1) then zero.5MyNat_1 else count_down.10count_down_0(pred.4pred_2(n.10count_down_1)) +print count_down.10count_down_0(suc.5MyNat_2(suc.5MyNat_2(suc.5MyNat_2(zero.5MyNat_1)))) diff --git a/test/compile/lower/genrecfun.ir b/test/compile/lower/genrecfun.ir index ad41257c..104ada7a 100644 --- a/test/compile/lower/genrecfun.ir +++ b/test/compile/lower/genrecfun.ir @@ -1,6 +1,6 @@ -union MyNat.s0_0 {zero.s0_1/0, suc.s0_2/1} -fn iszero.s1_2(n.s1_0) = match n.s1_0 { | zero.s0_1 -> true | suc.s0_2(n'.s1_1) -> false } -fn pred.s2_2(n.s2_0) = match n.s2_0 { | zero.s0_1 -> zero.s0_1 | suc.s0_2(n'.s2_1) -> n'.s2_1 } -fn <.s3_0($scr0, m.s3_1) = match $scr0 { | zero.s0_1 -> match m.s3_1 { | zero.s0_1 -> false | suc.s0_2(m'.s3_2) -> true } | suc.s0_2(n'.s3_3) -> match m.s3_1 { | zero.s0_1 -> false | suc.s0_2(m'.s3_5) -> <.s3_0(n'.s3_3, m'.s3_5) } } -fn count_down.s6_0(n.s6_1) = if iszero.s1_2(n.s6_1) then zero.s0_1 else count_down.s6_0(pred.s2_2(n.s6_1)) -print count_down.s6_0(suc.s0_2(suc.s0_2(suc.s0_2(zero.s0_1)))) +union MyNat.5MyNat_0 {zero.5MyNat_1/0, suc.5MyNat_2/1} +fn iszero.6iszero_2(n.6iszero_0) = match n.6iszero_0 { | zero.5MyNat_1 -> true | suc.5MyNat_2(n'.6iszero_1) -> false } +fn pred.4pred_2(n.4pred_0) = match n.4pred_0 { | zero.5MyNat_1 -> zero.5MyNat_1 | suc.5MyNat_2(n'.4pred_1) -> n'.4pred_1 } +fn <.1<_0($scr0, m.1<_1) = match $scr0 { | zero.5MyNat_1 -> match m.1<_1 { | zero.5MyNat_1 -> false | suc.5MyNat_2(m'.1<_2) -> true } | suc.5MyNat_2(n'.1<_3) -> match m.1<_1 { | zero.5MyNat_1 -> false | suc.5MyNat_2(m'.1<_5) -> <.1<_0(n'.1<_3, m'.1<_5) } } +fn count_down.10count_down_0(n.10count_down_1) = if iszero.6iszero_2(n.10count_down_1) then zero.5MyNat_1 else count_down.10count_down_0(pred.4pred_2(n.10count_down_1)) +print count_down.10count_down_0(suc.5MyNat_2(suc.5MyNat_2(suc.5MyNat_2(zero.5MyNat_1)))) diff --git a/test/compile/lower/genrecfun.pir b/test/compile/lower/genrecfun.pir index c60f0445..ba05779d 100644 --- a/test/compile/lower/genrecfun.pir +++ b/test/compile/lower/genrecfun.pir @@ -1,5 +1,5 @@ -union MyNat.s0_0 {zero.s0_1/0, suc.s0_2/1} -fn iszero.s1_2(n.s1_0) = match n.s1_0 { | zero.s0_1 -> true | suc.s0_2(n'.s1_1) -> false } -fn pred.s2_2(n.s2_0) = match n.s2_0 { | zero.s0_1 -> zero.s0_1 | suc.s0_2(n'.s2_1) -> n'.s2_1 } -fn count_down.s6_0(n.s6_1) = if iszero.s1_2(n.s6_1) then zero.s0_1 else count_down.s6_0(pred.s2_2(n.s6_1)) -print count_down.s6_0(suc.s0_2(suc.s0_2(suc.s0_2(zero.s0_1)))) +union MyNat.5MyNat_0 {zero.5MyNat_1/0, suc.5MyNat_2/1} +fn iszero.6iszero_2(n.6iszero_0) = match n.6iszero_0 { | zero.5MyNat_1 -> true | suc.5MyNat_2(n'.6iszero_1) -> false } +fn pred.4pred_2(n.4pred_0) = match n.4pred_0 { | zero.5MyNat_1 -> zero.5MyNat_1 | suc.5MyNat_2(n'.4pred_1) -> n'.4pred_1 } +fn count_down.10count_down_0(n.10count_down_1) = if iszero.6iszero_2(n.10count_down_1) then zero.5MyNat_1 else count_down.10count_down_0(pred.4pred_2(n.10count_down_1)) +print count_down.10count_down_0(suc.5MyNat_2(suc.5MyNat_2(suc.5MyNat_2(zero.5MyNat_1)))) diff --git a/test/compile/lower/postulate.cir b/test/compile/lower/postulate.cir index 2b7a5484..18fb0c64 100644 --- a/test/compile/lower/postulate.cir +++ b/test/compile/lower/postulate.cir @@ -1,2 +1,2 @@ -union N.s0_0 {z.s0_1/0, s.s0_2/1} -print s.s0_2(z.s0_1) +union N.1N_0 {z.1N_1/0, s.1N_2/1} +print s.1N_2(z.1N_1) diff --git a/test/compile/lower/postulate.ir b/test/compile/lower/postulate.ir index 2b7a5484..18fb0c64 100644 --- a/test/compile/lower/postulate.ir +++ b/test/compile/lower/postulate.ir @@ -1,2 +1,2 @@ -union N.s0_0 {z.s0_1/0, s.s0_2/1} -print s.s0_2(z.s0_1) +union N.1N_0 {z.1N_1/0, s.1N_2/1} +print s.1N_2(z.1N_1) diff --git a/test/compile/lower/postulate.pir b/test/compile/lower/postulate.pir index 2b7a5484..18fb0c64 100644 --- a/test/compile/lower/postulate.pir +++ b/test/compile/lower/postulate.pir @@ -1,2 +1,2 @@ -union N.s0_0 {z.s0_1/0, s.s0_2/1} -print s.s0_2(z.s0_1) +union N.1N_0 {z.1N_1/0, s.1N_2/1} +print s.1N_2(z.1N_1) diff --git a/test/compile/lower/pruning.cir b/test/compile/lower/pruning.cir index 4ae54d51..ea93a0b3 100644 --- a/test/compile/lower/pruning.cir +++ b/test/compile/lower/pruning.cir @@ -1,5 +1,5 @@ -union MyNat.s0_0 {zero.s0_1/0, suc.s0_2/1} -fn used.s1_0($scr0, m.s1_1) = match $scr0 { | zero.s0_1 -> m.s1_1 | suc.s0_2(n.s1_2) -> suc.s0_2(used.s1_0(n.s1_2, m.s1_1)) } -fn unused.s2_0($scr1, m.s2_1) = match $scr1 { | zero.s0_1 -> m.s2_1 | suc.s0_2(n.s2_2) -> suc.s0_2(unused.s2_0(n.s2_2, m.s2_1)) } -global also_unused.s3_0 = suc.s0_2(suc.s0_2(suc.s0_2(zero.s0_1))) -print used.s1_0(suc.s0_2(zero.s0_1), suc.s0_2(suc.s0_2(zero.s0_1))) +union MyNat.5MyNat_0 {zero.5MyNat_1/0, suc.5MyNat_2/1} +fn used.4used_0($scr0, m.4used_1) = match $scr0 { | zero.5MyNat_1 -> m.4used_1 | suc.5MyNat_2(n.4used_2) -> suc.5MyNat_2(used.4used_0(n.4used_2, m.4used_1)) } +fn unused.6unused_0($scr1, m.6unused_1) = match $scr1 { | zero.5MyNat_1 -> m.6unused_1 | suc.5MyNat_2(n.6unused_2) -> suc.5MyNat_2(unused.6unused_0(n.6unused_2, m.6unused_1)) } +global also_unused.11also_unused_0 = suc.5MyNat_2(suc.5MyNat_2(suc.5MyNat_2(zero.5MyNat_1))) +print used.4used_0(suc.5MyNat_2(zero.5MyNat_1), suc.5MyNat_2(suc.5MyNat_2(zero.5MyNat_1))) diff --git a/test/compile/lower/pruning.ir b/test/compile/lower/pruning.ir index 4ae54d51..ea93a0b3 100644 --- a/test/compile/lower/pruning.ir +++ b/test/compile/lower/pruning.ir @@ -1,5 +1,5 @@ -union MyNat.s0_0 {zero.s0_1/0, suc.s0_2/1} -fn used.s1_0($scr0, m.s1_1) = match $scr0 { | zero.s0_1 -> m.s1_1 | suc.s0_2(n.s1_2) -> suc.s0_2(used.s1_0(n.s1_2, m.s1_1)) } -fn unused.s2_0($scr1, m.s2_1) = match $scr1 { | zero.s0_1 -> m.s2_1 | suc.s0_2(n.s2_2) -> suc.s0_2(unused.s2_0(n.s2_2, m.s2_1)) } -global also_unused.s3_0 = suc.s0_2(suc.s0_2(suc.s0_2(zero.s0_1))) -print used.s1_0(suc.s0_2(zero.s0_1), suc.s0_2(suc.s0_2(zero.s0_1))) +union MyNat.5MyNat_0 {zero.5MyNat_1/0, suc.5MyNat_2/1} +fn used.4used_0($scr0, m.4used_1) = match $scr0 { | zero.5MyNat_1 -> m.4used_1 | suc.5MyNat_2(n.4used_2) -> suc.5MyNat_2(used.4used_0(n.4used_2, m.4used_1)) } +fn unused.6unused_0($scr1, m.6unused_1) = match $scr1 { | zero.5MyNat_1 -> m.6unused_1 | suc.5MyNat_2(n.6unused_2) -> suc.5MyNat_2(unused.6unused_0(n.6unused_2, m.6unused_1)) } +global also_unused.11also_unused_0 = suc.5MyNat_2(suc.5MyNat_2(suc.5MyNat_2(zero.5MyNat_1))) +print used.4used_0(suc.5MyNat_2(zero.5MyNat_1), suc.5MyNat_2(suc.5MyNat_2(zero.5MyNat_1))) diff --git a/test/compile/lower/pruning.pir b/test/compile/lower/pruning.pir index caf4e6a6..aea2ca1d 100644 --- a/test/compile/lower/pruning.pir +++ b/test/compile/lower/pruning.pir @@ -1,3 +1,3 @@ -union MyNat.s0_0 {zero.s0_1/0, suc.s0_2/1} -fn used.s1_0($scr0, m.s1_1) = match $scr0 { | zero.s0_1 -> m.s1_1 | suc.s0_2(n.s1_2) -> suc.s0_2(used.s1_0(n.s1_2, m.s1_1)) } -print used.s1_0(suc.s0_2(zero.s0_1), suc.s0_2(suc.s0_2(zero.s0_1))) +union MyNat.5MyNat_0 {zero.5MyNat_1/0, suc.5MyNat_2/1} +fn used.4used_0($scr0, m.4used_1) = match $scr0 { | zero.5MyNat_1 -> m.4used_1 | suc.5MyNat_2(n.4used_2) -> suc.5MyNat_2(used.4used_0(n.4used_2, m.4used_1)) } +print used.4used_0(suc.5MyNat_2(zero.5MyNat_1), suc.5MyNat_2(suc.5MyNat_2(zero.5MyNat_1))) diff --git a/test/compile/lower/unbox.cir b/test/compile/lower/unbox.cir index 1e43b83d..0dcf2e52 100644 --- a/test/compile/lower/unbox.cir +++ b/test/compile/lower/unbox.cir @@ -1,6 +1,6 @@ -union MyNat.s0_0 {zero.s0_1/0, suc.s0_2/1} -print zero.s0_1 -print zero.s0_1 -print zero.s0_1 -print zero.s0_1 -print zero.s0_1 +union MyNat.5MyNat_0 {zero.5MyNat_1/0, suc.5MyNat_2/1} +print zero.5MyNat_1 +print zero.5MyNat_1 +print zero.5MyNat_1 +print zero.5MyNat_1 +print zero.5MyNat_1 diff --git a/test/compile/lower/unbox.ir b/test/compile/lower/unbox.ir index 1e43b83d..0dcf2e52 100644 --- a/test/compile/lower/unbox.ir +++ b/test/compile/lower/unbox.ir @@ -1,6 +1,6 @@ -union MyNat.s0_0 {zero.s0_1/0, suc.s0_2/1} -print zero.s0_1 -print zero.s0_1 -print zero.s0_1 -print zero.s0_1 -print zero.s0_1 +union MyNat.5MyNat_0 {zero.5MyNat_1/0, suc.5MyNat_2/1} +print zero.5MyNat_1 +print zero.5MyNat_1 +print zero.5MyNat_1 +print zero.5MyNat_1 +print zero.5MyNat_1 diff --git a/test/compile/lower/unbox.pir b/test/compile/lower/unbox.pir index 1e43b83d..0dcf2e52 100644 --- a/test/compile/lower/unbox.pir +++ b/test/compile/lower/unbox.pir @@ -1,6 +1,6 @@ -union MyNat.s0_0 {zero.s0_1/0, suc.s0_2/1} -print zero.s0_1 -print zero.s0_1 -print zero.s0_1 -print zero.s0_1 -print zero.s0_1 +union MyNat.5MyNat_0 {zero.5MyNat_1/0, suc.5MyNat_2/1} +print zero.5MyNat_1 +print zero.5MyNat_1 +print zero.5MyNat_1 +print zero.5MyNat_1 +print zero.5MyNat_1 diff --git a/test/lsp/test_check_proofs_cache.py b/test/lsp/test_check_proofs_cache.py index 9f33b5eb..75c28422 100644 --- a/test/lsp/test_check_proofs_cache.py +++ b/test/lsp/test_check_proofs_cache.py @@ -489,3 +489,19 @@ def stub(s, env): f"post-decl AST; called {len(calls)} times: " f"{[type(c).__name__ for c in calls]}" ) + + +@pytest.mark.parametrize("edit", ["insert", "delete"]) +def test_inserting_or_deleting_a_statement_keeps_the_others_hits(edit: str) -> None: + """A statement's uniquified names don't depend on its position + (#1303), so adding a theorem in front of the others, or deleting + one, leaves the rest cache hits.""" + new = "theorem t0: true\nproof\n .\nend\n\n" + before, after = (_THREE_THEOREMS, new + _THREE_THEOREMS) if edit == "insert" \ + else (new + _THREE_THEOREMS, _THREE_THEOREMS) + check("test.pf", before) + proof_checker._cache_stats["hits"].clear() + proof_checker._cache_stats["misses"].clear() + assert check("test.pf", after) == [] + assert _hits() == 3 + assert _misses() == (1 if edit == "insert" else 0) diff --git a/test/lsp/test_type_check_cache.py b/test/lsp/test_type_check_cache.py index fff4dba1..69a8269e 100644 --- a/test/lsp/test_type_check_cache.py +++ b/test/lsp/test_type_check_cache.py @@ -69,8 +69,7 @@ def _recheck(content: str, path: str = "test.pf") -> tuple[int, int]: auto uses_g """ # The statements whose type check is cached: f, g, uses_f, uses_g, and -# the assert. (Edits below keep the number of statements: inserting one -# renames every statement after it, #1303.) +# the assert. def test_unchanged_recheck_type_checks_nothing() -> None: diff --git a/test/lsp/test_uniquify_determinism.py b/test/lsp/test_uniquify_determinism.py index 0e20eff0..ef084802 100644 --- a/test/lsp/test_uniquify_determinism.py +++ b/test/lsp/test_uniquify_determinism.py @@ -57,28 +57,21 @@ def _collect_unique_names(ast) -> list: out: list = [] def looks_uniquified(s) -> bool: - # uniquified names contain a literal `.` followed by either a - # plain digit run (legacy global-counter form) or one or more - # ``s_`` segments (per-statement scoping, Step 14). - if not isinstance(s, str): + # uniquified names contain a literal `.` followed by zero or + # more per-statement scope segments ``_`` (see + # ``ops._statement_scope``) and a trailing decimal run. + if not isinstance(s, str) or "." not in s: return False - if "." not in s: - return False - suffix = s.rsplit(".", 1)[1] - if suffix.isdigit(): - return True - # Per-statement scoped form: zero or more ``s_`` prefixes - # followed by a trailing decimal run. - rest = suffix - while True: - if not rest.startswith("s"): - break - tail = rest[1:] - digits, sep, after = tail.partition("_") - if not sep or not digits.isdigit(): + rest = s.split(".", 1)[1] + while not rest.isdigit(): + n = len(rest) - len(rest.lstrip("0123456789")) + if n == 0: + return False + length = int(rest[:n]) + count, sep, rest = rest[n + length:].partition("_") + if not sep or not (count == "" or count.isdigit()): return False - rest = after - return rest.isdigit() + return True def visit(node, seen): if id(node) in seen: @@ -190,11 +183,11 @@ def test_uniquify_deduce_restores_caller_state() -> None: def test_uniquify_deduce_uses_per_statement_scopes() -> None: """Two top-level statements that share a base name (here, both bind the proof variable ``pP``) get *disjoint* uniquified suffixes - because ``uniquify_deduce`` pushes a fresh ``s_`` segment onto - ``ctx.scope`` per top-level statement. This is what makes - structural hashing edit-invariant -- the dependency-tracking cache - (Step 14) needs unrelated downstream statements' hashes to stay - stable when an earlier statement's body changes size.""" + because ``uniquify_deduce`` pushes a fresh segment, named after the + statement, onto ``ctx.scope`` per top-level statement. This is + what makes structural hashing edit-invariant -- the per-statement + caches need unrelated statements' hashes to stay stable when + another statement changes size.""" src = ( "theorem t1: all P:bool. if P then P\n" "proof arbitrary P:bool suppose pP: P pP end\n" @@ -203,12 +196,32 @@ def test_uniquify_deduce_uses_per_statement_scopes() -> None: ) out = uniquify_deduce(_parse(src), UniquifyContext()) names = _collect_unique_names(out) - s0_names = [n for n in names if ".s0_" in n] - s1_names = [n for n in names if ".s1_" in n] - assert s0_names, f"no s0_-scoped names found in {names}" - assert s1_names, f"no s1_-scoped names found in {names}" + t1_names = [n for n in names if ".2t1_" in n] + t2_names = [n for n in names if ".2t2_" in n] + assert t1_names, f"no t1-scoped names found in {names}" + assert t2_names, f"no t2-scoped names found in {names}" # Each top-level statement's bound names live in its own scope. - assert set(s0_names).isdisjoint(set(s1_names)) + assert set(t1_names).isdisjoint(set(t2_names)) + + +def test_inserting_a_statement_keeps_the_others_names() -> None: + """A statement's names don't depend on its position (#1303), so + inserting a statement in front leaves the others' names, and so + their structural hashes, unchanged. Statements with the same name, + or unnamed ones of the same kind, are told apart by a count.""" + t = ( + "theorem t1: all P:bool. if P then P\n" + "proof arbitrary P:bool suppose pP: P pP end\n" + "assert true\n" + ) + before = _collect_unique_names(uniquify_deduce(_parse(t), UniquifyContext())) + after = _collect_unique_names(uniquify_deduce( + _parse("theorem t0: true proof . end\n" + t), UniquifyContext())) + assert set(before) <= set(after) + a = "assert all P:bool. P or not P\n" + names = set(_collect_unique_names(uniquify_deduce(_parse(a + a), + UniquifyContext()))) + assert {n for n in names if n.startswith("P.")} == {"P.6assert_0", "P.6assert1_0"} def test_independent_ctxs_do_not_leak() -> None: diff --git a/test/should-error/givens_assume_no_prem_not_ifthen.pf.err b/test/should-error/givens_assume_no_prem_not_ifthen.pf.err index 5342d58f..88185731 100644 --- a/test/should-error/givens_assume_no_prem_not_ifthen.pf.err +++ b/test/should-error/givens_assume_no_prem_not_ifthen.pf.err @@ -1,4 +1,4 @@ ./test/should-error/givens_assume_no_prem_not_ifthen.pf:5.3-5.11: expected proof of P - not a proof of if-then: assume q.s0_3{?} + not a proof of if-then: assume q.1T_3{?} Givens: p: P