lean-pkg 0.25.0__py3-none-any.whl
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.
- lean_pkg/__init__.py +3 -0
- lean_pkg/adapters/__init__.py +148 -0
- lean_pkg/adapters/github.py +65 -0
- lean_pkg/adapters/leansearch.py +68 -0
- lean_pkg/adapters/loogle.py +45 -0
- lean_pkg/adapters/palomar.py +170 -0
- lean_pkg/adapters/reservoir.py +127 -0
- lean_pkg/anchor.py +678 -0
- lean_pkg/audit.py +716 -0
- lean_pkg/binding_file.py +446 -0
- lean_pkg/blueprint.py +213 -0
- lean_pkg/build_scope.py +282 -0
- lean_pkg/ci_github.py +917 -0
- lean_pkg/claim_axioms.py +421 -0
- lean_pkg/claims.py +1155 -0
- lean_pkg/claims_mark.py +197 -0
- lean_pkg/cli.py +5208 -0
- lean_pkg/cli_parser.py +768 -0
- lean_pkg/command_tree.py +615 -0
- lean_pkg/comparator_config.py +990 -0
- lean_pkg/comparator_contracts.py +382 -0
- lean_pkg/comparator_receipt.py +2854 -0
- lean_pkg/comparator_stage.py +623 -0
- lean_pkg/comparator_types.py +830 -0
- lean_pkg/container.py +172 -0
- lean_pkg/core.py +1570 -0
- lean_pkg/coverage.py +1278 -0
- lean_pkg/data/stacks/lts-2026.10.yaml +73 -0
- lean_pkg/data/toolchain-advisories.yaml +77 -0
- lean_pkg/data/upgrade-breaking.yaml +20 -0
- lean_pkg/data/verify-workflow-pins.yaml +14 -0
- lean_pkg/deps_closure.py +512 -0
- lean_pkg/deps_fill.py +445 -0
- lean_pkg/dup_constants.py +448 -0
- lean_pkg/exercise.py +828 -0
- lean_pkg/exercise_forbid.py +288 -0
- lean_pkg/export_paths.py +77 -0
- lean_pkg/extract.py +1172 -0
- lean_pkg/extract_decls.py +501 -0
- lean_pkg/extract_ledger.py +1408 -0
- lean_pkg/extract_summary.py +132 -0
- lean_pkg/extract_types.py +502 -0
- lean_pkg/extract_write.py +138 -0
- lean_pkg/formalization.py +1078 -0
- lean_pkg/formalization_kept.py +182 -0
- lean_pkg/fresh_repo.py +709 -0
- lean_pkg/gate.py +2141 -0
- lean_pkg/headline.py +211 -0
- lean_pkg/help_topics.py +442 -0
- lean_pkg/holes.py +289 -0
- lean_pkg/host_routes.py +376 -0
- lean_pkg/hypotheses.py +788 -0
- lean_pkg/index.py +1749 -0
- lean_pkg/json_stdout.py +48 -0
- lean_pkg/lake_hygiene.py +289 -0
- lean_pkg/lean_diag.py +280 -0
- lean_pkg/lean_scan.py +1300 -0
- lean_pkg/ledger.py +582 -0
- lean_pkg/next_step.py +510 -0
- lean_pkg/page.py +1831 -0
- lean_pkg/page_html.py +153 -0
- lean_pkg/palomar.py +316 -0
- lean_pkg/palomar_check.py +1027 -0
- lean_pkg/port_modules.py +677 -0
- lean_pkg/prior_art.py +286 -0
- lean_pkg/producer.py +212 -0
- lean_pkg/producer_skew.py +208 -0
- lean_pkg/public_safety.py +1487 -0
- lean_pkg/publish.py +402 -0
- lean_pkg/rebase.py +984 -0
- lean_pkg/receipt.py +2385 -0
- lean_pkg/receipt_check.py +1064 -0
- lean_pkg/receipt_ci.py +660 -0
- lean_pkg/receipt_systemd.py +890 -0
- lean_pkg/recommended.py +232 -0
- lean_pkg/report.py +727 -0
- lean_pkg/reservoir.py +203 -0
- lean_pkg/scaffold.py +588 -0
- lean_pkg/schemas/formalization-0.4.schema.json +344 -0
- lean_pkg/schemas/formalization-0.4.schema.source.txt +15 -0
- lean_pkg/scripts/ClaimAxioms.lean +47 -0
- lean_pkg/scripts/ContractsOnly.lean +300 -0
- lean_pkg/scripts/DupConstants.lean +149 -0
- lean_pkg/scripts/HypProbe.lean +257 -0
- lean_pkg/scripts/comparator-check.sh +396 -0
- lean_pkg/scripts/verify-comparator.sh +135 -0
- lean_pkg/seal_bundle.py +647 -0
- lean_pkg/search.py +865 -0
- lean_pkg/site_profiles/easley.yaml +10 -0
- lean_pkg/site_profiles/github-actions.yaml +12 -0
- lean_pkg/sites.py +225 -0
- lean_pkg/stacks.py +373 -0
- lean_pkg/statements_module.py +217 -0
- lean_pkg/templates/Apache-2.0.txt +202 -0
- lean_pkg/toolchain_advisories.py +225 -0
- lean_pkg/upgrade.py +706 -0
- lean_pkg/verify.py +495 -0
- lean_pkg-0.25.0.dist-info/METADATA +93 -0
- lean_pkg-0.25.0.dist-info/RECORD +103 -0
- lean_pkg-0.25.0.dist-info/WHEEL +5 -0
- lean_pkg-0.25.0.dist-info/entry_points.txt +5 -0
- lean_pkg-0.25.0.dist-info/licenses/LICENSE +202 -0
- lean_pkg-0.25.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,300 @@
|
|
|
1
|
+
/-
|
|
2
|
+
Copyright 2026 Le Chen. SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
-/
|
|
4
|
+
-- lean_pkg.comparator_contracts: the Lean half. A contracts-only diagnostic over export files.
|
|
5
|
+
--
|
|
6
|
+
-- Usage: lean --run ContractsOnly.lean PLAN.json
|
|
7
|
+
--
|
|
8
|
+
-- PLAN.json: {"slots": [{"config", "challenge", "solution", "targets", "theorems", "definitions",
|
|
9
|
+
-- "axioms"}]}, where "challenge" and "solution" are export files written by the toolchain's `leanexport`
|
|
10
|
+
-- (several slots may name the same file: each file is parsed once) and "targets" is the list Lake's
|
|
11
|
+
-- `compareIt` exports; a slot whose targets are not all in both exports is reported as an error with
|
|
12
|
+
-- the missing names (Lake's own export would fail), never compared. Otherwise this program runs exactly what
|
|
13
|
+
-- `lake comparator`'s compare step runs on the two exports (Lake v4.35.0, `Lake/CLI/Check.lean`,
|
|
14
|
+
-- `verifyMatch.verifyCompare`): Lake's own public `Lake.Check.compareAt` with the theorem names and the
|
|
15
|
+
-- permitted axioms as theorem targets, the definition holes and the kernel's primitive constants, then
|
|
16
|
+
-- `Lake.Check.checkAxioms`. Their result is the verdict; nothing here re-implements it.
|
|
17
|
+
--
|
|
18
|
+
-- A failing slot then gets a diagnosis, which is display only: a walk that mirrors `Compare.loop`
|
|
19
|
+
-- (same worklist order, same edges: `Lake.Check.runForUsedConsts` of the Solution's constant, the type
|
|
20
|
+
-- alone for a target) records which constant pushed which, so it ends at the constant Lake refused and
|
|
21
|
+
-- yields the chain from the target to it (the breadcrumb), the first ConstantInfo field in which the two
|
|
22
|
+
-- copies differ (compared with `==`, Expr's BEq, which is what Lake compares: binder names and binder
|
|
23
|
+
-- annotations are ignored), and, for an Expr field, the first differing subterm as a short descriptor
|
|
24
|
+
-- (a constant's name, or the node kind), never a whole expression. Whether that walk ended at the name
|
|
25
|
+
-- Lake's message names (its quoted last word) is reported as `consistent`.
|
|
26
|
+
--
|
|
27
|
+
-- No kernel runs and nothing is written: one JSON line per slot on stdout and a final
|
|
28
|
+
-- `{"done": true, "slots": N}`. A run without that line is incomplete; the Python side refuses it.
|
|
29
|
+
import Lake.Check.Compare
|
|
30
|
+
import Lake.Check.Axioms
|
|
31
|
+
import Lake.Check.Util
|
|
32
|
+
import LeanExport.Parse
|
|
33
|
+
import Lean.Data.Json
|
|
34
|
+
|
|
35
|
+
open Lean
|
|
36
|
+
|
|
37
|
+
namespace ContractsOnly
|
|
38
|
+
|
|
39
|
+
/-- The kernel's primitive constants, as `Lake.CLI.Check.primitiveTargets` lists them in Lean v4.35.0
|
|
40
|
+
(a private definition there, so copied; tests/test_comparator_contracts.py compares this list with the
|
|
41
|
+
toolchain's source when it is installed). -/
|
|
42
|
+
def primitiveTargets : Array Name := #[
|
|
43
|
+
``Nat.add, ``Nat.sub, ``Nat.mul, ``Nat.pow, ``Nat.gcd, ``Nat.div, ``Nat.mod, ``Nat.beq, ``Nat.ble,
|
|
44
|
+
``Nat.land, ``Nat.lor, ``Nat.xor, ``Nat.shiftLeft, ``Nat.shiftRight, ``String.ofList, ``Char.ofNat,
|
|
45
|
+
``List, ``eagerReduce, ``Nat, ``String, ``String.mk, ``Char, ``optParam, ``autoParam, ``semiOutParam,
|
|
46
|
+
``outParam]
|
|
47
|
+
|
|
48
|
+
def kindOf : ConstantInfo → String
|
|
49
|
+
| .axiomInfo _ => "axiom" | .defnInfo _ => "def" | .thmInfo _ => "theorem"
|
|
50
|
+
| .opaqueInfo _ => "opaque" | .quotInfo _ => "quot" | .inductInfo _ => "inductive"
|
|
51
|
+
| .ctorInfo _ => "constructor" | .recInfo _ => "recursor"
|
|
52
|
+
|
|
53
|
+
/-- A short, bounded description of one Expr node: never the whole term (a shared proof DAG printed
|
|
54
|
+
as a tree can run to gigabytes). -/
|
|
55
|
+
def nodeDesc : Expr → String
|
|
56
|
+
| .bvar i => s!"bvar {i}"
|
|
57
|
+
| .fvar _ => "fvar"
|
|
58
|
+
| .mvar _ => "mvar"
|
|
59
|
+
| .sort u => s!"sort {u}"
|
|
60
|
+
| .const n us => if us.isEmpty then s!"const {n}" else s!"const {n}.\{{", ".intercalate (us.map toString)}}"
|
|
61
|
+
| e@(.app ..) => match e.getAppFn with
|
|
62
|
+
| .const n _ => s!"app of {n} ({e.getAppNumArgs} args)"
|
|
63
|
+
| _ => s!"app ({e.getAppNumArgs} args)"
|
|
64
|
+
| .lam .. => "lam"
|
|
65
|
+
| .forallE .. => "forall"
|
|
66
|
+
| .letE .. => "let"
|
|
67
|
+
| .lit (.natVal n) => let s := toString n; if s.length > 40 then "nat literal" else s!"nat {s}"
|
|
68
|
+
| .lit (.strVal s) => if s.length > 40 then "string literal" else s!"string {s.quote}"
|
|
69
|
+
| .mdata .. => "mdata"
|
|
70
|
+
| .proj s i _ => s!"proj {s} {i}"
|
|
71
|
+
|
|
72
|
+
/-- The first position (a path of child labels) where `a` and `b` differ under `==`, with the two
|
|
73
|
+
nodes there. It descends only into the first child that differs, so it costs a few `==` tests per
|
|
74
|
+
level, however shared the terms are. -/
|
|
75
|
+
partial def firstDiff (a b : Expr) (path : Array String := #[]) : Array String × Expr × Expr :=
|
|
76
|
+
let stop := (path, a, b)
|
|
77
|
+
match a, b with
|
|
78
|
+
| .app f x, .app g y =>
|
|
79
|
+
if f != g then firstDiff f g (path.push "fn") else if x != y then firstDiff x y (path.push "arg") else stop
|
|
80
|
+
| .lam _ t x _, .lam _ u y _ | .forallE _ t x _, .forallE _ u y _ =>
|
|
81
|
+
if t != u then firstDiff t u (path.push "domain") else if x != y then firstDiff x y (path.push "body")
|
|
82
|
+
else stop
|
|
83
|
+
| .letE _ t v x _, .letE _ u w y _ =>
|
|
84
|
+
if t != u then firstDiff t u (path.push "type") else if v != w then firstDiff v w (path.push "value")
|
|
85
|
+
else if x != y then firstDiff x y (path.push "body") else stop
|
|
86
|
+
| .mdata _ x, .mdata _ y => if x != y then firstDiff x y (path.push "mdata") else stop
|
|
87
|
+
| .proj s i x, .proj t j y => if s == t && i == j && x != y then firstDiff x y (path.push "struct") else stop
|
|
88
|
+
| _, _ => stop
|
|
89
|
+
|
|
90
|
+
def exprField (name : String) (a b : Expr) : Option Json :=
|
|
91
|
+
if a == b then none else
|
|
92
|
+
let (path, x, y) := firstDiff a b
|
|
93
|
+
some <| Json.mkObj [("field", name), ("path", toJson path), ("challenge", nodeDesc x),
|
|
94
|
+
("solution", nodeDesc y)]
|
|
95
|
+
|
|
96
|
+
def plainField [BEq α] [ToString α] (name : String) (a b : α) : Option Json :=
|
|
97
|
+
if a == b then none else some <| Json.mkObj [("field", name), ("challenge", toString a), ("solution", toString b)]
|
|
98
|
+
|
|
99
|
+
def safetyOf : ConstantInfo → String
|
|
100
|
+
| .defnInfo v => match v.safety with | .safe => "safe" | .unsafe => "unsafe" | .partial => "partial"
|
|
101
|
+
| c => if c.isUnsafe then "unsafe" else "safe"
|
|
102
|
+
|
|
103
|
+
def hintsOf : ConstantInfo → String
|
|
104
|
+
| .defnInfo v => match v.hints with | .opaque => "opaque" | .abbrev => "abbrev" | .regular h => s!"regular {h}"
|
|
105
|
+
| _ => ""
|
|
106
|
+
|
|
107
|
+
def allOf : ConstantInfo → List Name
|
|
108
|
+
| .thmInfo v => v.all | .defnInfo v => v.all | .opaqueInfo v => v.all | .inductInfo v => v.all
|
|
109
|
+
| .recInfo v => v.all | _ => []
|
|
110
|
+
|
|
111
|
+
/-- The first field in which the Challenge's and the Solution's copies differ, in the order kind, name,
|
|
112
|
+
levelParams, type, value, safety, hints, all, then the inductive, constructor, recursor and quotient
|
|
113
|
+
fields; `none` when they are equal. `onlyStatement`: a target, of which Lake compares the
|
|
114
|
+
ConstantVal only (and, for a definition hole, the safety). -/
|
|
115
|
+
def firstField (c s : ConstantInfo) (onlyStatement : Bool := false) (withSafety : Bool := false) :
|
|
116
|
+
Option Json := Id.run do
|
|
117
|
+
let head := [plainField "kind" (kindOf c) (kindOf s), plainField "name" c.name s.name,
|
|
118
|
+
plainField "levelParams" c.levelParams s.levelParams, exprField "type" c.type s.type]
|
|
119
|
+
for f in head do if f.isSome then return f
|
|
120
|
+
if onlyStatement then
|
|
121
|
+
return if withSafety then plainField "safety" (safetyOf c) (safetyOf s) else none
|
|
122
|
+
if let (some x, some y) := (c.value? (allowOpaque := true), s.value? (allowOpaque := true)) then
|
|
123
|
+
if let some f := exprField "value" x y then return some f
|
|
124
|
+
let rest := [plainField "safety" (safetyOf c) (safetyOf s), plainField "hints" (hintsOf c) (hintsOf s),
|
|
125
|
+
plainField "all" (allOf c) (allOf s)]
|
|
126
|
+
for f in rest do if f.isSome then return f
|
|
127
|
+
match c, s with
|
|
128
|
+
| .inductInfo a, .inductInfo b =>
|
|
129
|
+
for f in [plainField "numParams" a.numParams b.numParams, plainField "numIndices" a.numIndices b.numIndices,
|
|
130
|
+
plainField "ctors" a.ctors b.ctors, plainField "numNested" a.numNested b.numNested,
|
|
131
|
+
plainField "isRec" a.isRec b.isRec, plainField "isReflexive" a.isReflexive b.isReflexive] do
|
|
132
|
+
if f.isSome then return f
|
|
133
|
+
| .ctorInfo a, .ctorInfo b =>
|
|
134
|
+
for f in [plainField "induct" a.induct b.induct, plainField "cidx" a.cidx b.cidx,
|
|
135
|
+
plainField "numParams" a.numParams b.numParams, plainField "numFields" a.numFields b.numFields] do
|
|
136
|
+
if f.isSome then return f
|
|
137
|
+
| .recInfo a, .recInfo b =>
|
|
138
|
+
for f in [plainField "numParams" a.numParams b.numParams, plainField "numIndices" a.numIndices b.numIndices,
|
|
139
|
+
plainField "numMotives" a.numMotives b.numMotives, plainField "numMinors" a.numMinors b.numMinors,
|
|
140
|
+
plainField "k" a.k b.k, plainField "rules" (a.rules.map (·.ctor)) (b.rules.map (·.ctor)),
|
|
141
|
+
plainField "rules.nfields" (a.rules.map (·.nfields)) (b.rules.map (·.nfields))] do
|
|
142
|
+
if f.isSome then return f
|
|
143
|
+
for (x, y) in a.rules.zip b.rules do
|
|
144
|
+
if let some f := exprField s!"rules[{x.ctor}].rhs" x.rhs y.rhs then return some f
|
|
145
|
+
| .quotInfo a, .quotInfo b =>
|
|
146
|
+
if a.kind != b.kind then return some <| Json.mkObj [("field", "quotKind")]
|
|
147
|
+
| _, _ => pure ()
|
|
148
|
+
return none
|
|
149
|
+
|
|
150
|
+
/-- The result of the mirrored walk: the constant it stopped at, why, the chain that reached it, and the
|
|
151
|
+
field diagnosis. -/
|
|
152
|
+
structure Stop where
|
|
153
|
+
name : Name
|
|
154
|
+
reason : String
|
|
155
|
+
chain : Array Name
|
|
156
|
+
field : Option Json
|
|
157
|
+
|
|
158
|
+
/-- Follow the parent links from `n` back to a root. -/
|
|
159
|
+
partial def chainOf (parent : Std.HashMap Name (Option Name)) (n : Name) (acc : Array Name := #[]) :
|
|
160
|
+
Array Name :=
|
|
161
|
+
let acc := acc.push n
|
|
162
|
+
match parent[n]? with
|
|
163
|
+
| some (some p) => if acc.contains p then acc.reverse else chainOf parent p acc
|
|
164
|
+
| _ => acc.reverse
|
|
165
|
+
|
|
166
|
+
/-- The walk of `Lake.Check.compareAt` with parent links. Each worklist entry carries the constant that
|
|
167
|
+
pushed it; the parent recorded is the one of the entry that is actually processed. -/
|
|
168
|
+
def mirrorCompare (ch so : LeanExport.ExportedEnv) (thms defs : Array Name) : Option Stop := Id.run do
|
|
169
|
+
let mut work : Array (Name × Option Name) := primitiveTargets.map (·, none)
|
|
170
|
+
for t in thms do
|
|
171
|
+
let (some c, some s) := (ch.constMap[t]?, so.constMap[t]?)
|
|
172
|
+
| return some ⟨t, "missing", #[t], none⟩
|
|
173
|
+
let kindOk := match c, s with | .thmInfo _, .thmInfo _ | .axiomInfo _, .axiomInfo _ => true | _, _ => false
|
|
174
|
+
if !kindOk then return some ⟨t, "kind", #[t], plainField "kind" (kindOf c) (kindOf s)⟩
|
|
175
|
+
if c.toConstantVal != s.toConstantVal then return some ⟨t, "statement", #[t], firstField c s true⟩
|
|
176
|
+
work := work ++ (c.type.getUsedConstants.map (·, some t))
|
|
177
|
+
for d in defs do
|
|
178
|
+
let (some c, some s) := (ch.constMap[d]?, so.constMap[d]?)
|
|
179
|
+
| return some ⟨d, "missing", #[d], none⟩
|
|
180
|
+
match c, s with
|
|
181
|
+
| .defnInfo cv, .defnInfo sv =>
|
|
182
|
+
if !Lake.Check.definitionHoleMatches cv sv then
|
|
183
|
+
return some ⟨d, "definition hole", #[d], firstField c s true true⟩
|
|
184
|
+
| _, _ => return some ⟨d, "kind", #[d], plainField "kind" (kindOf c) (kindOf s)⟩
|
|
185
|
+
work := work.push (d, none)
|
|
186
|
+
let targets : Std.HashSet Name := Std.HashSet.ofArray (thms ++ defs)
|
|
187
|
+
let mut checked : Std.HashSet Name := {}
|
|
188
|
+
let mut parent : Std.HashMap Name (Option Name) := {}
|
|
189
|
+
while h : work.size > 0 do
|
|
190
|
+
let (n, p) := work[work.size - 1]
|
|
191
|
+
work := work.pop
|
|
192
|
+
if checked.contains n then continue
|
|
193
|
+
parent := parent.insert n p
|
|
194
|
+
let (some c, some s) := (ch.constMap[n]?, so.constMap[n]?)
|
|
195
|
+
| return some ⟨n, "missing", chainOf parent n, none⟩
|
|
196
|
+
let next :=
|
|
197
|
+
if targets.contains s.name then s.type.getUsedConstants
|
|
198
|
+
else if c != s then #[]
|
|
199
|
+
else (Lake.Check.runForUsedConsts (m := StateM (Array Name)) s (fun x => modify (·.push x)) |>.run #[]).2
|
|
200
|
+
if !targets.contains s.name && c != s then
|
|
201
|
+
return some ⟨n, "not equal", chainOf parent n, firstField c s⟩
|
|
202
|
+
for x in next do
|
|
203
|
+
if !checked.contains x then work := work.push (x, some n)
|
|
204
|
+
checked := checked.insert n
|
|
205
|
+
return none
|
|
206
|
+
|
|
207
|
+
/-- The walk of `Lake.Check.checkAxioms` with parent links, ending at the axiom (or missing constant)
|
|
208
|
+
it refuses. -/
|
|
209
|
+
def mirrorAxioms (so : LeanExport.ExportedEnv) (thms defs legal : Array Name) : Option Stop := Id.run do
|
|
210
|
+
let legal : Std.HashSet Name := Std.HashSet.ofArray legal
|
|
211
|
+
for t in thms do
|
|
212
|
+
match so.constMap[t]? with
|
|
213
|
+
| some (.thmInfo _) => pure ()
|
|
214
|
+
| some s => return some ⟨t, "not a theorem", #[t], some <| Json.mkObj [("field", "kind"), ("solution", kindOf s)]⟩
|
|
215
|
+
| none => return some ⟨t, "missing", #[t], none⟩
|
|
216
|
+
for d in defs do
|
|
217
|
+
match so.constMap[d]? with
|
|
218
|
+
| some (.defnInfo _) => pure ()
|
|
219
|
+
| some s => return some ⟨d, "not a definition", #[d], some <| Json.mkObj [("field", "kind"), ("solution", kindOf s)]⟩
|
|
220
|
+
| none => return some ⟨d, "missing", #[d], none⟩
|
|
221
|
+
let mut work : Array (Name × Option Name) := (thms ++ defs).map (·, none)
|
|
222
|
+
let mut checked : Std.HashSet Name := {}
|
|
223
|
+
let mut parent : Std.HashMap Name (Option Name) := {}
|
|
224
|
+
while h : work.size > 0 do
|
|
225
|
+
let (n, p) := work[work.size - 1]
|
|
226
|
+
work := work.pop
|
|
227
|
+
if checked.contains n then continue
|
|
228
|
+
parent := parent.insert n p
|
|
229
|
+
let some s := so.constMap[n]? | return some ⟨n, "missing", chainOf parent n, none⟩
|
|
230
|
+
let used := (Lake.Check.runForUsedConsts (m := StateM (Array Name)) s (fun x => modify (·.push x)) |>.run #[]).2
|
|
231
|
+
for x in used do
|
|
232
|
+
match so.constMap[x]? with
|
|
233
|
+
| none => return some ⟨x, "missing", chainOf parent n |>.push x, none⟩
|
|
234
|
+
| some (.axiomInfo _) =>
|
|
235
|
+
if !legal.contains x then return some ⟨x, "illegal axiom", chainOf parent n |>.push x, none⟩
|
|
236
|
+
| some _ => pure ()
|
|
237
|
+
if !checked.contains x then work := work.push (x, some n)
|
|
238
|
+
checked := checked.insert n
|
|
239
|
+
return none
|
|
240
|
+
|
|
241
|
+
def names (j : Json) (key : String) : Array Name :=
|
|
242
|
+
match j.getObjValAs? (Array String) key with
|
|
243
|
+
| .ok xs => xs.map String.toName
|
|
244
|
+
| .error _ => #[]
|
|
245
|
+
|
|
246
|
+
end ContractsOnly
|
|
247
|
+
|
|
248
|
+
open ContractsOnly in
|
|
249
|
+
def main (args : List String) : IO UInt32 := do
|
|
250
|
+
let [planPath] := args | IO.eprintln "usage: lean --run ContractsOnly.lean PLAN.json"; return 2
|
|
251
|
+
let plan ← IO.ofExcept <| Json.parse (← IO.FS.readFile planPath)
|
|
252
|
+
let slots ← IO.ofExcept <| plan.getObjValAs? (Array Json) "slots"
|
|
253
|
+
let mut cache : Std.HashMap String LeanExport.ExportedEnv := {}
|
|
254
|
+
let out ← IO.getStdout
|
|
255
|
+
for slot in slots do
|
|
256
|
+
let config := (slot.getObjValAs? String "config").toOption.getD ""
|
|
257
|
+
let chPath ← IO.ofExcept <| slot.getObjValAs? String "challenge"
|
|
258
|
+
let soPath ← IO.ofExcept <| slot.getObjValAs? String "solution"
|
|
259
|
+
for p in [chPath, soPath] do
|
|
260
|
+
unless cache.contains p do
|
|
261
|
+
let env ← LeanExport.parseStream <| .ofHandle (← IO.FS.Handle.mk p .read)
|
|
262
|
+
cache := cache.insert p env
|
|
263
|
+
let ch := cache[chPath]!
|
|
264
|
+
let so := cache[soPath]!
|
|
265
|
+
-- every export target must be in both exports: `lake comparator` exports exactly these names and
|
|
266
|
+
-- its exporter fails on a missing one (the shared export here is made with --ignore-missing)
|
|
267
|
+
let targets := names slot "targets"
|
|
268
|
+
let missing := (targets.filter (!ch.constMap.contains ·)).map (s!"challenge: {·}")
|
|
269
|
+
++ (targets.filter (!so.constMap.contains ·)).map (s!"solution: {·}")
|
|
270
|
+
unless missing.isEmpty do
|
|
271
|
+
out.putStrLn (Json.mkObj [("config", config), ("verdict", "error"),
|
|
272
|
+
("missing", toJson missing)]).compress
|
|
273
|
+
continue
|
|
274
|
+
let thms := names slot "theorems"
|
|
275
|
+
let defs := names slot "definitions"
|
|
276
|
+
let axs := names slot "axioms"
|
|
277
|
+
-- exactly `verifyCompare`: the permitted axioms are theorem targets of the comparison
|
|
278
|
+
let (stage, verdict) :=
|
|
279
|
+
match Lake.Check.compareAt ch so (thms ++ axs) defs primitiveTargets with
|
|
280
|
+
| .error e => ("compare", Except.error e)
|
|
281
|
+
| .ok () => match Lake.Check.checkAxioms so thms defs axs with
|
|
282
|
+
| .error e => ("axioms", Except.error e)
|
|
283
|
+
| .ok () => ("", Except.ok ())
|
|
284
|
+
let line ← match verdict with
|
|
285
|
+
| .ok () => pure <| Json.mkObj [("config", config), ("verdict", "match")]
|
|
286
|
+
| .error msg =>
|
|
287
|
+
let stop := if stage == "compare" then mirrorCompare ch so (thms ++ axs) defs
|
|
288
|
+
else mirrorAxioms so thms defs axs
|
|
289
|
+
let base := [("config", toJson config), ("verdict", toJson "differs"), ("stage", toJson stage),
|
|
290
|
+
("lake_message", toJson msg)]
|
|
291
|
+
pure <| match stop with
|
|
292
|
+
| none => Json.mkObj (base ++ [("consistent", toJson false)])
|
|
293
|
+
| some st => Json.mkObj (base ++ [
|
|
294
|
+
("consistent", toJson (msg.endsWith s!"'{st.name}'")),
|
|
295
|
+
("constant", toJson (toString st.name)), ("reason", toJson st.reason),
|
|
296
|
+
("breadcrumb", toJson (st.chain.map toString)), ("first_field", st.field.getD Json.null)])
|
|
297
|
+
out.putStrLn line.compress
|
|
298
|
+
out.flush
|
|
299
|
+
out.putStrLn (Json.mkObj [("done", true), ("slots", slots.size)]).compress
|
|
300
|
+
return 0
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
/-
|
|
2
|
+
Copyright 2026 Le Chen. SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
-/
|
|
4
|
+
-- lean_pkg.dup_constants: the Lean half. Read-only probe of a compiled environment.
|
|
5
|
+
--
|
|
6
|
+
-- Usage: lake env lean --run DupConstants.lean MOD [MOD ...]
|
|
7
|
+
--
|
|
8
|
+
-- Imports MOD... exactly as `import MOD` would (the same `importModules`, so the same duplicate
|
|
9
|
+
-- tolerance Lean applies), then walks the constant tables of every module in the import closure
|
|
10
|
+
-- (`env.header.moduleData`, the very arrays `importModules` merged). Every name that occurs in the table
|
|
11
|
+
-- of more than one module is printed as one JSON line: each module's own `ConstantInfo` compared field by
|
|
12
|
+
-- field, strictly (`Expr.equal`: binder names and annotations included), against the one `env.find?`
|
|
13
|
+
-- returns (the winner), the module whose copy IS the winner (pointer identity), the module
|
|
14
|
+
-- `env.getModuleIdxFor?` records for that name (the first owner, not necessarily the winner's), and each
|
|
15
|
+
-- owner's `.olean` path.
|
|
16
|
+
-- A final line `{"done": ...}` carries the totals; a run without it is incomplete (the Python side
|
|
17
|
+
-- refuses it). Nothing is judged here: lean_pkg/dup_constants.py decides.
|
|
18
|
+
import Lean
|
|
19
|
+
|
|
20
|
+
open Lean
|
|
21
|
+
|
|
22
|
+
namespace DupConstants
|
|
23
|
+
|
|
24
|
+
def kindOf : ConstantInfo → String
|
|
25
|
+
| .axiomInfo _ => "axiom"
|
|
26
|
+
| .defnInfo _ => "def"
|
|
27
|
+
| .thmInfo _ => "theorem"
|
|
28
|
+
| .opaqueInfo _ => "opaque"
|
|
29
|
+
| .quotInfo _ => "quot"
|
|
30
|
+
| .inductInfo _ => "inductive"
|
|
31
|
+
| .ctorInfo _ => "constructor"
|
|
32
|
+
| .recInfo _ => "recursor"
|
|
33
|
+
|
|
34
|
+
def safetyOf : ConstantInfo → String
|
|
35
|
+
| .defnInfo v => match v.safety with
|
|
36
|
+
| .safe => "safe" | .unsafe => "unsafe" | .partial => "partial"
|
|
37
|
+
| c => if c.isUnsafe then "unsafe" else "safe"
|
|
38
|
+
|
|
39
|
+
def allOf : ConstantInfo → Option (List Name)
|
|
40
|
+
| .thmInfo v => some v.all
|
|
41
|
+
| .defnInfo v => some v.all
|
|
42
|
+
| .opaqueInfo v => some v.all
|
|
43
|
+
| .inductInfo v => some v.all
|
|
44
|
+
| .recInfo v => some v.all
|
|
45
|
+
| _ => none
|
|
46
|
+
|
|
47
|
+
def valueOf : ConstantInfo → Option Expr
|
|
48
|
+
| .thmInfo v => some v.value
|
|
49
|
+
| .defnInfo v => some v.value
|
|
50
|
+
| .opaqueInfo v => some v.value
|
|
51
|
+
| _ => none
|
|
52
|
+
|
|
53
|
+
def hintsOf : ConstantInfo → String
|
|
54
|
+
| .defnInfo v => match v.hints with
|
|
55
|
+
| .opaque => "opaque" | .abbrev => "abbrev" | .regular h => s!"regular {h}"
|
|
56
|
+
| _ => ""
|
|
57
|
+
|
|
58
|
+
/-- The remaining fields of inductive types, constructors, recursors and quotients, as text. -/
|
|
59
|
+
def shapeOf : ConstantInfo → String
|
|
60
|
+
| .inductInfo v => s!"params={v.numParams} indices={v.numIndices} ctors={v.ctors} nested={v.numNested} rec={v.isRec} reflexive={v.isReflexive}"
|
|
61
|
+
| .ctorInfo v => s!"induct={v.induct} cidx={v.cidx} params={v.numParams} fields={v.numFields}"
|
|
62
|
+
| .recInfo v => s!"params={v.numParams} indices={v.numIndices} motives={v.numMotives} minors={v.numMinors} k={v.k} rules={v.rules.map fun r => (r.ctor, r.nfields)}"
|
|
63
|
+
| .quotInfo v => match v.kind with
|
|
64
|
+
| .type => "type" | .ctor => "ctor" | .lift => "lift" | .ind => "ind"
|
|
65
|
+
| _ => ""
|
|
66
|
+
|
|
67
|
+
def optEq (a b : Option Expr) : Bool :=
|
|
68
|
+
match a, b with
|
|
69
|
+
| some x, some y => Expr.equal x y
|
|
70
|
+
| none, none => true
|
|
71
|
+
| _, _ => false
|
|
72
|
+
|
|
73
|
+
def rulesEq : ConstantInfo → ConstantInfo → Bool
|
|
74
|
+
| .recInfo a, .recInfo b =>
|
|
75
|
+
a.rules.length == b.rules.length && (a.rules.zip b.rules).all fun (x, y) => Expr.equal x.rhs y.rhs
|
|
76
|
+
| _, _ => true
|
|
77
|
+
|
|
78
|
+
/-- The fields in which `a` and `b` differ (empty: exactly equal). -/
|
|
79
|
+
def differences (a b : ConstantInfo) : Array String := Id.run do
|
|
80
|
+
let mut d := #[]
|
|
81
|
+
if kindOf a != kindOf b then d := d.push "kind"
|
|
82
|
+
if a.levelParams != b.levelParams then d := d.push "levelParams"
|
|
83
|
+
if !Expr.equal a.type b.type then d := d.push "type"
|
|
84
|
+
if !optEq (valueOf a) (valueOf b) then d := d.push "value"
|
|
85
|
+
if allOf a != allOf b then d := d.push "all"
|
|
86
|
+
if safetyOf a != safetyOf b then d := d.push "safety"
|
|
87
|
+
if hintsOf a != hintsOf b then d := d.push "hints"
|
|
88
|
+
if shapeOf a != shapeOf b || !rulesEq a b then d := d.push "shape"
|
|
89
|
+
return d
|
|
90
|
+
|
|
91
|
+
def summary (c : ConstantInfo) : Json :=
|
|
92
|
+
Json.mkObj [("kind", kindOf c), ("levelParams", toJson (c.levelParams.map toString)),
|
|
93
|
+
("safety", safetyOf c), ("all", toJson ((allOf c).map (·.map toString))),
|
|
94
|
+
("hasValue", (valueOf c).isSome), ("typeHash", toJson (c.type.hash.toNat)),
|
|
95
|
+
("valueHash", toJson ((valueOf c).map (·.hash.toNat)))]
|
|
96
|
+
|
|
97
|
+
unsafe def samePtrImpl (a b : ConstantInfo) : Bool := ptrAddrUnsafe a == ptrAddrUnsafe b
|
|
98
|
+
|
|
99
|
+
/-- `a` and `b` are the same object in memory: the copy `env.find?` actually returns. -/
|
|
100
|
+
@[implemented_by samePtrImpl] opaque samePtr (a b : ConstantInfo) : Bool
|
|
101
|
+
|
|
102
|
+
end DupConstants
|
|
103
|
+
|
|
104
|
+
open DupConstants in
|
|
105
|
+
def main (args : List String) : IO UInt32 := do
|
|
106
|
+
if args.isEmpty then
|
|
107
|
+
IO.eprintln "usage: lake env lean --run DupConstants.lean MOD [MOD ...]"
|
|
108
|
+
return 2
|
|
109
|
+
initSearchPath (← findSysroot)
|
|
110
|
+
let imports := args.toArray.map fun m => ({ module := m.toName } : Import)
|
|
111
|
+
let env ← importModules imports {} (loadExts := false)
|
|
112
|
+
let mods := env.header.moduleNames
|
|
113
|
+
let data := env.header.moduleData
|
|
114
|
+
-- name -> [(module index, its ConstantInfo)]
|
|
115
|
+
let mut seen : Std.HashMap Name (Array (Nat × ConstantInfo)) := {}
|
|
116
|
+
let mut total := 0
|
|
117
|
+
for h : i in [0:data.size] do
|
|
118
|
+
let md := data[i]
|
|
119
|
+
for c in md.constants do
|
|
120
|
+
total := total + 1
|
|
121
|
+
seen := seen.alter c.name fun
|
|
122
|
+
| some xs => some (xs.push (i, c))
|
|
123
|
+
| none => some #[(i, c)]
|
|
124
|
+
let mut dups := 0
|
|
125
|
+
for (n, copies) in seen.toArray.qsort (fun a b => a.1.lt b.1) do
|
|
126
|
+
if copies.size < 2 then continue
|
|
127
|
+
dups := dups + 1
|
|
128
|
+
let winner := env.find? n
|
|
129
|
+
let recorded := (env.getModuleIdxFor? n).bind fun i => mods[i.toNat]?
|
|
130
|
+
let mut ownerJson := #[]
|
|
131
|
+
let mut winnerModule : Option Name := none
|
|
132
|
+
for (i, c) in copies do
|
|
133
|
+
let m := mods[i]?.getD .anonymous
|
|
134
|
+
let olean ← try pure (some (← findOLean m).toString) catch _ => pure none
|
|
135
|
+
let same := match winner with | some w => samePtr c w | none => false
|
|
136
|
+
if same && winnerModule.isNone then winnerModule := some m
|
|
137
|
+
ownerJson := ownerJson.push <| Json.mkObj [("module", toString m), ("olean", toJson olean),
|
|
138
|
+
("constant", summary c), ("isWinner", same),
|
|
139
|
+
("vsWinner", match winner with
|
|
140
|
+
| some w => toJson (differences c w)
|
|
141
|
+
| none => Json.null)]
|
|
142
|
+
IO.println <| Json.compress <| Json.mkObj [("name", toString n),
|
|
143
|
+
("owners", toJson ownerJson),
|
|
144
|
+
("winner", match winner with | some w => summary w | none => Json.null),
|
|
145
|
+
("winnerModule", toJson (winnerModule.map toString)),
|
|
146
|
+
("recordedOwner", toJson (recorded.map toString))]
|
|
147
|
+
IO.println <| Json.compress <| Json.mkObj [("done", true), ("modules", toJson mods.size),
|
|
148
|
+
("constants", toJson total), ("duplicates", toJson dups)]
|
|
149
|
+
return 0
|