module public import LeanExport.Parse public import Lean.Declaration public import Lean.Expr import Lean.Util.FoldConsts import Lean.Data.Json /-! # `marqdo-replay`: independent statement/axiom verifier for proof payloads This executable is part of the *verifier* side of `marqdo-prove`. It consumes two Lean NDJSON export streams (the toolchain's own proof export format, written by the stock `leanexport` tool): * the **payload** produced by the proof search process, which must contain the project theorem declaration and its full dependency closure; * the **expected** proposition reconstructed *by the trusted frontend from the current sources* (it never comes from the proof search process). It then checks, without importing or trusting any `.olean`: 1. the payload contains exactly one declaration for the target name, and it is a theorem (not an axiom, definition, opaque constant or a second copy); 2. its type is alpha-equivalent to the expected proposition expression; 3. the target proof term contains no metavariables; 4. the transitive axiom closure of the exported payload is a subset of the fixed allowed base axioms (`propext`, `Classical.choice`, `Quot.sound`), with a specific diagnostic for `sorryAx` and native/compiler axioms. The actual kernel replay of the proof terms is performed by the bundled independent checker (`con-leche --verified`); this program deliberately does not implement a second kernel. -/ open Lean namespace MarqdoProof.Replay /-- The only base axioms accepted in a payload. `sorryAx` and any native / compiler trust axiom are rejected by omission and named in diagnostics. -/ def allowedAxioms : Array Name := #[``propext, ``Classical.choice, ``Quot.sound] def isAllowedAxiom (name : Name) : Bool := allowedAxioms.contains name private def escape (text : String) : String := Id.run do let mut out := String.mk [] for c in text.toList do match c with | '"' => out := out.push '\\' |>.push '"' | '\\' => out := out.push '\\' |>.push '\\' | '\n' => out := out.push '\\' |>.push 'n' | '\t' => out := out.push '\\' |>.push 't' | c => out := out.push c return out def renderArray (items : Array String) : String := let items := items.toList.map (fun item : String => "\"" ++ escape item ++ "\"") "[" ++ String.intercalate ", " items ++ "]" def render (ok : Bool) (target : String) (expectedType : String) (axioms : Array String) (reason : String) (used : Array String := #[]) (projectDecls : Array String := #[]) : String := "{\"schema\": \"marqdo.replay\", \"ok\": " ++ (if ok then "true" else "false") ++ ", \"target\": \"" ++ escape target ++ "\"" ++ ", \"expected_type\": \"" ++ escape expectedType ++ "\"" ++ ", \"axioms\": " ++ renderArray axioms ++ ", \"used\": " ++ renderArray used ++ ", \"project_decls\": " ++ renderArray projectDecls ++ ", \"reason\": \"" ++ escape reason ++ "\"}" private def fail (target : String) (reason : String) : IO UInt32 := do IO.println (render false target "" #[] reason) return 1 /-- Internal name prefixes of project declarations in this tool. -/ def isProjectConstant (name : Name) : Bool := let text := name.toString text.startsWith "marqdoTarget_" || text.startsWith "marqdoModel_" private def loadExport (path : String) : IO LeanExport.ExportedEnv := do let handle ← IO.FS.Handle.mk path IO.FS.Mode.read let stream := IO.FS.Stream.ofHandle handle LeanExport.parseStream stream private partial def hasMetavariable (expr : Expr) : Bool := if expr.isMVar then true else expr.getAppArgs.any hasMetavariable || (match expr with | .lam _ type body _ => hasMetavariable type || hasMetavariable body | .forallE _ type body _ => hasMetavariable type || hasMetavariable body | .letE _ type value body _ => hasMetavariable type || hasMetavariable value || hasMetavariable body | .mdata _ inner => hasMetavariable inner | .proj _ _ inner => hasMetavariable inner | _ => false) private def kindName : ConstantInfo → String | .axiomInfo _ => "axiom" | .defnInfo _ => "definition" | .thmInfo _ => "theorem" | .opaqueInfo _ => "opaque" | .quotInfo _ => "quotient" | .inductInfo _ => "inductive" | .ctorInfo _ => "constructor" | .recInfo _ => "recursor" /-- Run the verifier. Exit 0 only when every check passed. -/ def run (args : List String) : IO UInt32 := do let (payloadPath, expectedPath, targetLiteral, expectedLiteral) ← match args with | [payload, expected, target, expectedName] => pure (payload, expected, target, expectedName) | _ => IO.eprintln "usage: marqdo-replay PAYLOAD.ndjson EXPECTED.ndjson TARGET_NAME EXPECTED_NAME" return 2 let target := Name.mkSimple targetLiteral let expectedName := Name.mkSimple expectedLiteral let payload ← loadExport payloadPath let expected ← loadExport expectedPath if payload.constOrder.size != payload.constMap.size then return ← fail targetLiteral "payload export contains duplicate declarations" if expected.constOrder.size != expected.constMap.size then return ← fail targetLiteral "expected export contains duplicate declarations" let some targetInfo := payload.constMap.get? target | return ← fail targetLiteral s!"payload does not declare `{target}`" let some expectedInfo := expected.constMap.get? expectedName | return ← fail targetLiteral s!"expected module does not declare `{expectedName}`" let expectedExpr : Expr ← match expectedInfo with | .defnInfo value => pure value.value | other => return ← fail targetLiteral s!"expected declaration has unexpected kind `{kindName other}`" -- Axiom closure of the payload (the stream is closed under the target's -- dependencies by construction of `leanexport`). let mut axioms : Array String := #[] let mut projectDecls : Array String := #[] for (name, info) in payload.constMap.toList do if isProjectConstant name then projectDecls := projectDecls.push name.toString if let .axiomInfo _ := info then axioms := axioms.push name.toString if !isAllowedAxiom name then let reason := if name == ``sorryAx then "payload depends on `sorryAx`" else if name.toString.contains "trustCompiler" || name.toString.endsWith "_native" then s!"payload depends on native/compiler trust axiom `{name}`" else s!"payload depends on untrusted axiom `{name}`" return ← fail targetLiteral reason match targetInfo with | .thmInfo thm => if thm.type.hasMVar then return ← fail targetLiteral "target theorem type contains metavariables" if hasMetavariable thm.value then return ← fail targetLiteral "target proof term contains metavariables" if !thm.type.eqv expectedExpr then return ← fail targetLiteral s!"target proposition does not match the expected proposition" let used := (thm.type.getUsedConstants ++ thm.value.getUsedConstants).filterMap fun name => if isProjectConstant name then some name.toString else none IO.println (render true targetLiteral (toString expectedExpr) axioms "" used projectDecls) return 0 | other => return ← fail targetLiteral s!"payload target is not a theorem (kind `{kindName other}`)" end MarqdoProof.Replay /-- Executable entry point (must be in the root namespace for `lake exe`). -/ public def main (args : List String) : IO UInt32 := MarqdoProof.Replay.run args