| /- | |
| Copyright (c) 2025 Lean FRO, LLC. All rights reserved. | |
| Released under Apache 2.0 license as described in the file LICENSE. | |
| Authors: Henrik Böving | |
| -/ | |
| module | |
| prelude | |
| public import LeanExport.Parse | |
| import Lake.Check.Util | |
| import Init.Data.ToString.Macro | |
| import Std.Data.HashSet | |
| namespace Lake.Check | |
| namespace Compare | |
| structure Context where | |
| challenge : LeanExport.ExportedEnv | |
| solution : LeanExport.ExportedEnv | |
| definitionTargets : Std.HashSet Lean.Name | |
| theoremTargets : Std.HashSet Lean.Name | |
| structure State where | |
| worklist : Array Lean.Name | |
| checked : Std.HashSet Lean.Name | |
| abbrev CompareM := ReaderT Context <| StateT State <| Except String | |
| def addWorklist (n : Lean.Name) : CompareM Unit := do | |
| if !(← get).checked.contains n then | |
| modify fun s => { s with worklist := s.worklist.push n } | |
| def addRelevantConsts (info : Lean.ConstantInfo) : CompareM Unit := do | |
| runForUsedConsts info addWorklist | |
| partial def loop : CompareM Unit := do | |
| if (← get).worklist.isEmpty then | |
| return () | |
| let target ← modifyGet fun s => (s.worklist.back!, { s with worklist := s.worklist.pop }) | |
| if (← get).checked.contains target then | |
| loop | |
| else | |
| let some challengeConst := (← read).challenge.constMap[target]? | |
| | throw s!"Const not found in challenge '{target}'" | |
| let some solutionConst := (← read).solution.constMap[target]? | |
| | throw s!"Const not found in solution '{target}'" | |
| if (← read).definitionTargets.contains solutionConst.name | |
| || (← read).theoremTargets.contains solutionConst.name then | |
| solutionConst.type.getUsedConstants.forM addWorklist | |
| else | |
| if challengeConst != solutionConst then | |
| throw s!"Const does not match between challenge and target '{target}'" | |
| addRelevantConsts solutionConst | |
| modify fun s => { s with checked := s.checked.insert target } | |
| loop | |
| end Compare | |
| public def definitionHoleMatches (challengeHole solutionHole : Lean.DefinitionVal) : Bool := | |
| challengeHole.toConstantVal == solutionHole.toConstantVal | |
| && challengeHole.safety == solutionHole.safety | |
| public def compareAt (challenge solution : LeanExport.ExportedEnv) (theoremTargets : Array Lean.Name) | |
| (definitionTargets : Array Lean.Name) (primitive : Array Lean.Name) : Except String Unit := do | |
| let mut worklist := primitive | |
| for target in theoremTargets do | |
| let some challengeConst := challenge.constMap[target]? | |
| | throw s!"Const not found in challenge: '{target}'" | |
| let some solutionConst := solution.constMap[target]? | |
| | throw s!"Const not found in solution: '{target}'" | |
| let (challengeConst, solutionConst) ← | |
| match challengeConst, solutionConst with | |
| | .thmInfo cc, .thmInfo sc | |
| | .axiomInfo cc, .axiomInfo sc => pure (cc.toConstantVal, sc.toConstantVal) | |
| | _, _ => throw s!"Challenge and solution constant kind don't match: '{target}'" | |
| if challengeConst != solutionConst then | |
| throw s!"Challenge and solution theorem statement do not match: '{target}'" | |
| worklist := worklist ++ challengeConst.type.getUsedConstants | |
| for target in definitionTargets do | |
| let some challengeConst := challenge.constMap[target]? | |
| | throw s!"Const not found in challenge: '{target}'" | |
| let some solutionConst := solution.constMap[target]? | |
| | throw s!"Const not found in solution: '{target}'" | |
| let .defnInfo challengeConst := challengeConst | |
| | throw s!"Challenge constant is not a definition: '{target}'" | |
| let .defnInfo solutionConst := solutionConst | |
| | throw s!"Solution constant is not a definition: '{target}'" | |
| unless challengeConst.value.getLambdaBody.isAppOf ``sorryAx do | |
| throw s!"Challenge definition is not sorried: '{target}'" | |
| if !definitionHoleMatches challengeConst solutionConst then | |
| throw s!"Const does not match between challenge and target '{target}'" | |
| worklist := worklist.push solutionConst.name | |
| let definitionTargets := Std.HashSet.ofArray definitionTargets | |
| let theoremTargets := Std.HashSet.ofArray theoremTargets | |
| Compare.loop.run { challenge, solution, definitionTargets, theoremTargets } |>.run' { worklist, checked := {} } | |
| end Lake.Check |