|
| 1 | +/- |
| 2 | +Copyright (c) 2026 Lean FRO, LLC. All rights reserved. |
| 3 | +Released under Apache 2.0 license as described in the file LICENSE. |
| 4 | +Authors: Kim Morrison |
| 5 | +-/ |
| 6 | + |
| 7 | +module |
| 8 | + |
| 9 | +public import HexInterval.Experiment.ProofEmitter |
| 10 | + |
| 11 | +@[expose] public section |
| 12 | + |
| 13 | +/-! |
| 14 | +# Joint semantic and proof-emitter package assembly |
| 15 | +
|
| 16 | +An executable propagator package, its semantic replay schemas, and its tactic |
| 17 | +emission handles are three views of one extension point. This module keeps |
| 18 | +the latter two views in one descriptor and checks them against the executable |
| 19 | +registry in the same package order. |
| 20 | +
|
| 21 | +The full replay key includes the rule, event role, and payload schema. Joint |
| 22 | +assembly requires exact package-local equality of those keys: an emitter may |
| 23 | +neither omit a semantic schema nor borrow one from another package. The |
| 24 | +frontend therefore remains function-agnostic without maintaining a second, |
| 25 | +potentially divergent registry by hand. |
| 26 | +-/ |
| 27 | + |
| 28 | +namespace Hex.Interval.Experiment.ProofRegistry |
| 29 | + |
| 30 | +open Propagator PayloadArena SemanticReplay ProofEmitter |
| 31 | + |
| 32 | +/-- The proof-facing declarations contributed by one executable package. |
| 33 | +
|
| 34 | +`semantic` contains the package-owned checkers and the corresponding `emit` |
| 35 | +fragment contains exactly the handles a tactic may use to apply them. -/ |
| 36 | +structure Package (semantics : Semantics Fact) (Handle : Type) where |
| 37 | + semantic : SemanticReplay.Package semantics |
| 38 | + emit : EmitPackage Handle |
| 39 | + |
| 40 | +/-- A semantic registry and emitter table assembled from the same packages. -/ |
| 41 | +structure Registry (semantics : Semantics Fact) (Handle : Type) where |
| 42 | + private mk :: |
| 43 | + semantic : SemanticReplay.Registry semantics |
| 44 | + emit : SchemaTable Handle |
| 45 | + |
| 46 | +private def make (semantic : SemanticReplay.Registry semantics) |
| 47 | + (emit : SchemaTable Handle) : Registry semantics Handle := |
| 48 | + { semantic, emit } |
| 49 | + |
| 50 | +/-- Failures specific to joint proof-package assembly. |
| 51 | +
|
| 52 | +Semantic failures retain the executable/semantic registry diagnostic. The |
| 53 | +remaining cases say which package failed exact emitter coverage. -/ |
| 54 | +inductive BuildError where |
| 55 | + | semantic (error : SemanticReplay.BuildError) |
| 56 | + | duplicateEmit (key : ReplayKey) |
| 57 | + | missingEmit (package : Nat) (key : ReplayKey) |
| 58 | + | extraEmit (package : Nat) (key : ReplayKey) |
| 59 | + | invalidEmit |
| 60 | + deriving DecidableEq, Repr |
| 61 | + |
| 62 | +namespace Package |
| 63 | + |
| 64 | +/-- Exact semantic replay addresses owned by this package. -/ |
| 65 | +def semanticKeys (package : Package semantics Handle) : List ReplayKey := |
| 66 | + package.semantic.factSchemas.toList.map PackedFactSchema.key ++ |
| 67 | + package.semantic.instanceSchemas.toList.map PackedInstanceSchema.key ++ |
| 68 | + package.semantic.equalitySchemas.toList.map PackedEqualitySchema.key |
| 69 | + |
| 70 | +/-- Exact tactic-emission addresses claimed by this package. -/ |
| 71 | +def emitKeys (package : Package semantics Handle) : List ReplayKey := |
| 72 | + package.emit.schemas.map (fun schema => schema.key) |
| 73 | + |
| 74 | +end Package |
| 75 | + |
| 76 | +def firstDuplicate (seen : List ReplayKey) : |
| 77 | + List (SchemaName Handle) -> Option ReplayKey |
| 78 | + | [] => none |
| 79 | + | schema :: rest => |
| 80 | + if seen.contains schema.key then some schema.key |
| 81 | + else firstDuplicate (schema.key :: seen) rest |
| 82 | + |
| 83 | +def checkMissing (package : Nat) (emit : List ReplayKey) : |
| 84 | + List ReplayKey -> Except BuildError Unit |
| 85 | + | [] => pure () |
| 86 | + | key :: rest => |
| 87 | + if emit.contains key then checkMissing package emit rest |
| 88 | + else throw (.missingEmit package key) |
| 89 | + |
| 90 | +def checkExtra (package : Nat) (semantic : List ReplayKey) : |
| 91 | + List ReplayKey -> Except BuildError Unit |
| 92 | + | [] => pure () |
| 93 | + | key :: rest => |
| 94 | + if semantic.contains key then checkExtra package semantic rest |
| 95 | + else throw (.extraEmit package key) |
| 96 | + |
| 97 | +def checkPackages (index : Nat) : |
| 98 | + List (Package semantics Handle) -> Except BuildError Unit |
| 99 | + | [] => pure () |
| 100 | + | package :: rest => do |
| 101 | + let semantic := package.semanticKeys |
| 102 | + let emit := package.emitKeys |
| 103 | + checkMissing index emit semantic |
| 104 | + checkExtra index semantic emit |
| 105 | + checkPackages (index + 1) rest |
| 106 | + |
| 107 | +/-- Build the semantic checker and tactic schema table from one package list. |
| 108 | +
|
| 109 | +The existing semantic builder first checks exact executable ownership and |
| 110 | +coverage. This layer additionally checks exact package-local correspondence |
| 111 | +between semantic schemas and emitter handles, plus global emitter uniqueness. |
| 112 | +The resulting table is selection data only: emitted theorem applications are |
| 113 | +still checked by Lean and by the transparent replay transitions. -/ |
| 114 | +opaque build (executable : Propagator.Registry Fact) |
| 115 | + (packages : Array (Package semantics Handle)) : |
| 116 | + Except BuildError (Registry semantics Handle) := do |
| 117 | + let semanticPackages := packages.map (fun package => package.semantic) |
| 118 | + let semantic ← |
| 119 | + match SemanticReplay.Registry.build executable semanticPackages with |
| 120 | + | .ok registry => pure registry |
| 121 | + | .error error => throw (BuildError.semantic error) |
| 122 | + let emitPackages := packages.toList.map (fun package => package.emit) |
| 123 | + let entries := emitPackages.flatMap (fun package => package.schemas) |
| 124 | + if let some key := firstDuplicate [] entries then |
| 125 | + throw (BuildError.duplicateEmit key) |
| 126 | + match checkPackages 0 packages.toList with |
| 127 | + | .error error => throw error |
| 128 | + | .ok _ => |
| 129 | + match SchemaTable.build emitPackages with |
| 130 | + | none => throw BuildError.invalidEmit |
| 131 | + | some emit => pure (make semantic emit) |
| 132 | + |
| 133 | +end Hex.Interval.Experiment.ProofRegistry |
0 commit comments