File size: 6,401 Bytes
d3791e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/-
Verify.lean: decides whether a submitted Lean module proves the theorems a task asked for.

  lean --run Verify.lean [--free a,b] [--native] <ReferenceModule> <SubmissionModule> <target>...

`--free a,b` names definitions the task asks the solver to write: their type must match the
reference, their value is the solver's own. `--native` additionally accepts the axioms that
`native_decide` introduces; it is used only for hidden test theorems the grader itself proves.

Both modules must already be compiled to .olean files on LEAN_PATH. This program imports only
`Lean`, never the submission, so nothing the submission declares (notation, macros, instances)
can change how the check itself is read. It reads the reference module's constants from its .olean, imports the
submission as an environment, and compares kernel terms:

  1. every non-target constant the reference module declares (the task's own definitions) must be
     present in the submission with the same kind, type and value;
  2. every target must be present with exactly the reference type;
  3. the proof of every target may depend only on the three standard axioms.

It prints one JSON object per target and exits 0. The caller turns that into a reward.
-/
import Lean
open Lean

def allowedAxioms : List Name := [``propext, ``Classical.choice, ``Quot.sound]

/-- All axioms a constant depends on, found by walking every constant it mentions. -/
partial def axiomsOf (env : Environment) (root : Name) : Array Name := Id.run do
  let mut seen : NameSet := {}
  let mut todo : Array Name := #[root]
  let mut out : Array Name := #[]
  while h : todo.size > 0 do
    let c := todo[todo.size - 1]
    todo := todo.pop
    if seen.contains c then continue
    seen := seen.insert c
    match env.find? c with
    | none => out := out.push (`missing ++ c)
    | some info =>
      let mut es : Array Expr := #[info.type]
      match info with
      | .axiomInfo _ => out := out.push c
      | .defnInfo v => es := es.push v.value
      | .thmInfo v => es := es.push v.value
      | .opaqueInfo v => es := es.push v.value
      | .ctorInfo v => todo := todo.push v.induct
      | .inductInfo v => for k in v.ctors do todo := todo.push k
      | .recInfo v => for r in v.rules do es := es.push r.rhs
      | .quotInfo _ => pure ()
      for e in es do
        for k in e.getUsedConstants do
          if !seen.contains k then todo := todo.push k
  return out

def kindOf : ConstantInfo → String
  | .axiomInfo _ => "axiom" | .defnInfo _ => "def" | .thmInfo _ => "theorem"
  | .opaqueInfo _ => "opaque" | .quotInfo _ => "quot" | .inductInfo _ => "inductive"
  | .ctorInfo _ => "constructor" | .recInfo _ => "recursor"

def valueOf : ConstantInfo → Option Expr
  | .defnInfo v => some v.value | .thmInfo v => some v.value | .opaqueInfo v => some v.value
  | _ => none

def sameDecl (a b : ConstantInfo) : Bool :=
  kindOf a == kindOf b && a.levelParams == b.levelParams && a.type == b.type && valueOf a == valueOf b

def jstr (s : String) : String := (Json.str s).compress

def isNativeAxiom (n : Name) : Bool :=
  n == ``Lean.ofReduceBool || n == ``Lean.trustCompiler ||
    n.components.any (fun c => c == `native_decide)

def isUnder (frees : List Name) (c : Name) : Bool := frees.any (fun f => f.isPrefixOf c)

partial def parseFlags (args : List String) (free : List Name) (native : Bool) :
    List Name × Bool × List String :=
  match args with
  | "--free" :: v :: rest => parseFlags rest (free ++ (v.splitOn ",").map String.toName) native
  | "--native" :: rest => parseFlags rest free true
  | rest => (free, native, rest)

def main (args : List String) : IO UInt32 := do
  let (free, native, args) := parseFlags args [] false
  match args with
  | refMod :: solMod :: targets =>
    initSearchPath (← findSysroot)
    let refName := refMod.toName
    let solName := solMod.toName
    let targetNames := targets.map String.toName
    -- The reference is read straight from its .olean: that file holds exactly the constants the
    -- reference module declares, which is all the comparison needs. Only the submission is imported
    -- as a full environment (Mathlib included), so memory stays at one copy of Mathlib, not two.
    let (refData, _) ← readModuleData (← findOLean refName)
    let mut refMap : Std.HashMap Name ConstantInfo := {}
    for i in [0:refData.constNames.size] do
      refMap := refMap.insert refData.constNames[i]! refData.constants[i]!
    let solEnv ← importModules #[{ module := solName }] {} (trustLevel := 0)
    -- 1. the task's own definitions are unchanged
    let mut tampered : Array Name := #[]
    for c in refData.constNames do
      if targetNames.contains c then continue
      let some r := refMap[c]? | continue
      if free.contains c then
        match solEnv.find? c with
        | some s =>
          if r.levelParams != s.levelParams || r.type != s.type then tampered := tampered.push c
        | none => tampered := tampered.push c
        continue
      if isUnder free c then continue
      match solEnv.find? c with
      | some s => if !sameDecl r s then tampered := tampered.push c
      | none => tampered := tampered.push c
    for t in targetNames do
      let mut ok := true
      let mut why := ""
      match refMap[t]?, solEnv.find? t with
      | none, _ => ok := false; why := "target missing from reference"
      | _, none => ok := false; why := "theorem not found in the submission"
      | some r, some s =>
        if !tampered.isEmpty then
          ok := false; why := s!"the task's definitions were changed or are missing: {tampered.toList}"
        else if kindOf s != "theorem" && kindOf s != "def" then
          ok := false; why := s!"declared as {kindOf s}, not proved"
        else if r.levelParams != s.levelParams || r.type != s.type then
          ok := false; why := "the statement differs from the one asked for"
        else
          let ax := axiomsOf solEnv t
          let bad := ax.filter (fun a => !allowedAxioms.contains a && !(native && isNativeAxiom a))
          if !bad.isEmpty then
            ok := false; why := s!"depends on axioms that are not allowed: {bad.toList}"
      IO.println s!"\{\"target\": {jstr t.toString}, \"ok\": {ok}, \"why\": {jstr why}}"
    return 0
  | _ =>
    IO.eprintln "usage: lean --run Verify.lean [--free a,b] [--native] <ReferenceModule> <SubmissionModule> <target>..."
    return 2