SNAPKITTYWEST commited on
Commit
8882e55
·
verified ·
1 Parent(s): dc588fd

add lean4/src/SovereignCorpus/Bridge/Granite4Parser.lean

Browse files
lean4/src/SovereignCorpus/Bridge/Granite4Parser.lean ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import Lean.Data.Json
2
+ import SovereignCorpus.Bridge.Granite4Schema
3
+
4
+ namespace SovereignCorpus.Bridge.Granite4Parser
5
+
6
+ open Lean
7
+ open SovereignCorpus.Bridge
8
+
9
+ inductive ParseError where
10
+ | jsonDecode (msg : String) (line : Nat)
11
+ | schemaViolation (field : String) (reason : String)
12
+ | lean3SyntaxDetected (pattern : String) (line : Nat)
13
+ | tacticForbidden (tactic : String) (line : Nat)
14
+ deriving Repr, Inhabited
15
+
16
+ def forbiddenPatterns : List String :=
17
+ [
18
+ "meta def",
19
+ "tactic.block",
20
+ "refine_struct",
21
+ "classical.by_contradiction",
22
+ "by_contra!",
23
+ "have! := by",
24
+ "sorryAx",
25
+ "prop_decidable",
26
+ "Classical.propDecidable"
27
+ ]
28
+
29
+ def defaultAllowedTactics : List String :=
30
+ ["rw", "simp_all", "norm_num", "linarith", "nlinarith", "aesop", "apply", "exact", "intro", "obtain", "cases", "induction", "constructor", "split_ifs", "field_simp", "ring_nf", "norm_cast", "omega"]
31
+
32
+ def containsText (haystack needle : String) : Bool :=
33
+ if needle.isEmpty then
34
+ true
35
+ else
36
+ (haystack.splitOn needle).length > 1
37
+
38
+ def containsForbiddenPattern (text : String) : Option String :=
39
+ forbiddenPatterns.find? (fun pattern => containsText text pattern)
40
+
41
+ def guardLean4Syntax (p : GraniteProblem) (line : Nat) : Except ParseError Unit := do
42
+ let haystack := p.statement ++ "\n" ++ String.intercalate "\n" p.context ++ "\n" ++ p.tacticHint.getD ""
43
+ match containsForbiddenPattern haystack with
44
+ | some pattern => throw <| ParseError.lean3SyntaxDetected pattern line
45
+ | none => pure ()
46
+
47
+ def guardTactics (p : GraniteProblem) (line : Nat) : Except ParseError Unit := do
48
+ match p.tacticHint with
49
+ | some hint =>
50
+ if p.meta.allowedTactics.contains hint || defaultAllowedTactics.contains hint then
51
+ pure ()
52
+ else
53
+ throw <| ParseError.tacticForbidden hint line
54
+ | none => pure ()
55
+
56
+ def guardStatement (p : GraniteProblem) : Except ParseError Unit := do
57
+ if containsText p.statement "sorry" then
58
+ throw <| ParseError.schemaViolation "statement" "target theorem statement must not contain sorry"
59
+ else
60
+ pure ()
61
+
62
+ def parseLine (line : String) (lineNum : Nat) : Except ParseError GraniteProblem := do
63
+ if line.trim.isEmpty then
64
+ throw <| ParseError.jsonDecode "empty line" lineNum
65
+ let json <- Json.parse line |>.mapError (fun err => ParseError.jsonDecode err lineNum)
66
+ let problem <- fromJson? json |>.mapError (fun err => ParseError.jsonDecode err lineNum)
67
+ guardLean4Syntax problem lineNum
68
+ guardTactics problem lineNum
69
+ guardStatement problem
70
+ pure problem
71
+
72
+ def loadProblems (path : String) : IO (Except String (List GraniteProblem)) := do
73
+ let content ← IO.FS.readFile path
74
+ let lines := content.splitOn "\n"
75
+ let parsed := lines.enum.filterMap (fun ⟨idx, line⟩ =>
76
+ if line.trim.isEmpty then
77
+ none
78
+ else
79
+ some <| parseLine line (idx + 1)
80
+ )
81
+ let errors := parsed.filterMap (fun r => match r with | .error e => some e | .ok _ => none)
82
+ if !errors.isEmpty then
83
+ pure <| Except.error s!"parse failures: {repr errors}"
84
+ else
85
+ pure <| Except.ok <| parsed.filterMap (fun r => match r with | .ok p => some p | .error _ => none)
86
+
87
+ end SovereignCorpus.Bridge.Granite4Parser