File size: 14,812 Bytes
0115dcf | 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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 | import Pantograph.Frontend
import Test.Common
open Lean Pantograph Frontend
namespace Pantograph.Test.Frontend.Distil
abbrev TestM := TestT MetaM
abbrev Test := TestM Unit
private def collectSorrysFromSource (source: String) (options : Frontend.GoalCollectionOptions := {})
: CoreM (List GoalState) := do
let (context, state) β do Frontend.createContextStateFromFile source (env? := β getEnv)
let m := show FrontendM _ from Frontend.mapCompilationSteps Ξ» step => do
return (step.before, β Frontend.collectSorrys step options)
let li β m.run {} |>.run context |>.run' state
let goalStates β li.filterMapM Ξ» (env, sorrys) => withEnv env do
if sorrys.isEmpty then
return .none
let { state, .. } β (Frontend.sorrysToGoalState sorrys).run'
return .some state
return goalStates
private def test_sorry_in_middle: Test := do
let sketch := "
example : β (n m: Nat), n + m = m + n := by
intros n m
sorry
"
let goalStates β collectSorrysFromSource sketch
let [goalState] := goalStates | panic! s!"Incorrect number of states: {goalStates.length}"
checkEq "goals" ((β goalState.serializeGoals (options := {})).map (Β·.devolatilize)) #[
{
target := { pp? := "n + m = m + n" },
vars := #[{
userName := `n,
type? := .some { pp? := "Nat" },
}, {
userName := `m,
type? := .some { pp? := "Nat" },
}
],
}
]
let .success st _ β runTermElabMInMeta $ goalState.tryDraft .unfocus "have : 1 + 1 = 2 := by sorry\nsorry"
| fail "Draft tactic failed"
checkEq "goals" st.goals.length 2
private def test_sorry_in_coupled: Test := do
let sketch := "
example : β (y: Nat), β (x: Nat), y + 1 = x := by
intro y
apply Exists.intro
case h => sorry
case w => sorry
"
let goalStates β collectSorrysFromSource sketch
let [goalState] := goalStates | panic! s!"Incorrect number of states: {goalStates.length}"
checkEq "goals" ((β goalState.serializeGoals (options := {})).map (Β·.devolatilize)) #[
{
target := { pp? := "y + 1 = ?w" },
vars := #[{
userName := `y,
type? := .some { pp? := "Nat" },
}
],
},
{
userName? := .some `w,
target := { pp? := "Nat" },
vars := #[{
userName := `y,
type? := .some { pp? := "Nat" },
}
],
}
]
private def test_sorry_with_local_instance (tacticMode : Bool) : Test := do
let placeholder := if tacticMode then "by sorry" else "sorry"
let sketch := s!"
def test (Ξ± : Type) [s : Inhabited Ξ±] : Ξ± := @Inhabited.default Ξ± s
example (Ξ± : Type) [Inhabited Ξ±] : Ξ± := {placeholder}
"
let goalStates β collectSorrysFromSource sketch
let [goalState] := goalStates | panic! s!"Incorrect number of states: {goalStates.length}"
let result β runTermElabMInMeta $ goalState.tryTactic .unfocus "exact test Ξ±"
checkTrue "success" $ result matches .success ..
match result with
| .success .. => return ()
| .failure messages =>
let messages β messages.mapM (Β·.toString)
fail s!"Could not execute tactic {messages}"
| .parseError e =>
fail s!"Parse error: {e}"
| .invalidAction e =>
fail s!"Invalid action: {e}"
private def test_sorry_circular : Test := do
let sketch := "
theorem test (p q : Prop) (hp : p) (hq : q) : p β§ q β§ p := by sorry
"
let goalStates β collectSorrysFromSource sketch
let [goalState] := goalStates | panic! s!"Incorrect number of states: {goalStates.length}"
let result β runTermElabMInMeta $ goalState.tryTactic .unfocus "exact test"
checkTrue "failure" $ result matches .failure ..
match result with
| .success .. =>
fail s!"This should not succeed"
| .failure .. =>
return ()
| .parseError e =>
fail s!"Parse error: {e}"
| .invalidAction e =>
fail s!"Invalid action: {e}"
private def test_environment_capture: Test := do
let sketch := "
def mystery (n: Nat) := n + 1
example (n: Nat) : mystery n + 1 = n + 2 := sorry
"
let goalStates β collectSorrysFromSource sketch
let [goalState] := goalStates | panic! s!"Incorrect number of states: {goalStates.length}"
checkEq "goals" ((β goalState.serializeGoals (options := {})).map (Β·.devolatilize)) #[
{
target := { pp? := "mystery n + 1 = n + 2" },
vars := #[{
userName := `n,
type? := .some { pp? := "Nat" },
}],
}
]
private def test_capture_type_mismatch : Test := do
let input := "
def mystery (k: Nat) : Nat := true
"
let options := { collectTypeErrors := true }
let goalStates β collectSorrysFromSource input options
let [goalState] := goalStates | panic! s!"Incorrect number of states: {goalStates.length}"
checkEq "goals" ((β goalState.serializeGoals).map (Β·.devolatilize)) #[
{
target := { pp? := "Nat" },
vars := #[{
userName := `k,
type? := .some { pp? := "Nat" },
}],
}
]
def test_capture_type_mismatch_in_binder : Test := do
let input := "
example (p: Prop) (h: (β (x: Prop), Nat) β p): p := h (Ξ» (y: Nat) => 5)
"
let options := { collectTypeErrors := true }
let goalStates β collectSorrysFromSource input options
let [goalState] := goalStates | panic! s!"Incorrect number of states: {goalStates.length}"
checkEq "goals" ((β goalState.serializeGoals (options := {})).map (Β·.devolatilize)) #[]
private def test_distil_simple : Test := do
let input := "
set_option pp.analyze true
theorem mystery : β (p q : Prop), p β¨ q β q β¨ p := sorry
"
let [_dst@{ goalState := state }] β distilSearchTargets (β getEnv) input
| fail "Incorrect number of search states"
let .success state _ β (state.tryTactic .unfocus "intro p q").run' (ctx := defaultElabContext)
| fail "`intro` failed"
checkEq "goals" state.goals.length 1
private def test_distil_tail : Test := do
let input := "
theorem mystery : β (n m: Nat), n + m = m + n := by
intros n m
sorry
"
let [_dst@{ goalState := state }] β distilSearchTargets (β getEnv) input { ignoreValues := false }
| fail "Incorrect number of search states"
checkEq "start" ((β state.serializeGoals {}).map (Β·.devolatilize))
#[{
target := { pp? := "n + m = m + n" },
vars := #[{
userName := `n,
type? := .some { pp? := "Nat" },
}, {
userName := `m,
type? := .some { pp? := "Nat" },
}
],
}]
private def test_distil_induction : Test := do
let input := "
theorem mystery : β (n m: Nat), n + m = m + n := by
intros n m
induction n with
| zero =>
have h1 : 0 + m = m := sorry
sorry
| succ n ih =>
have h2 : n + m = m := sorry
sorry
"
let [_dst@{ goalState := state }] β distilSearchTargets (β getEnv) input { ignoreValues := false }
| fail "Incorrect number of search states"
let n' := .mkSimple "nβ"
checkEq "start" ((β state.serializeGoals {}).map (Β·.devolatilize)) #[
{
target := { pp? := "n + 1 + m = m + (n + 1)" },
vars := #[
{ var n' "Nat" with isInaccessible := true },
var `m "Nat",
var `n "Nat",
var `ih "n + m = m + n",
{ var `h2 "n + m = m" with value? := .some { pp? := "?m.5" }},
],
},
{
target := { pp? := "n + m = m" },
vars := #[
{ var n' "Nat" with isInaccessible := true },
var `m "Nat",
var `n "Nat",
var `ih "n + m = m + n",
],
},
{
target := { pp? := "0 + m = m + 0" },
vars := #[
var `n "Nat",
var `m "Nat",
{ var `h1 "0 + m = m" with value? := .some { pp? := "?m.2" }},
],
},
{
target := { pp? := "0 + m = m" },
vars := #[
var `n "Nat",
var `m "Nat",
],
},
]
where
var (userName : Name) (type : String) : Protocol.Variable := {
userName,
type? := .some { pp? := type },
}
private def test_distil_instance (tacticMode : Bool) : Test := do
let placeholder := if tacticMode then "by sorry" else "sorry"
let input := s!"
def test (Ξ± : Type) [s : Inhabited Ξ±] : Ξ± := @Inhabited.default Ξ± s
def mystery (Ξ± : Type) [Inhabited Ξ±] : Ξ± := {placeholder}
"
let [_dst@{ goalState := state }] β distilSearchTargets (β getEnv) input { ignoreValues := false }
| fail "Incorrect number of search states"
checkEq "start" ((β state.serializeGoals {}).map (Β·.devolatilize))
#[{
target := { pp? := .some "Ξ±" },
vars := #[
{
userName := `Ξ±,
type? := .some { pp? := .some "Type" }
},
{
userName := .mkSimple "instβ",
isInaccessible := true,
type? := .some { pp? := .some "Inhabited Ξ±" }
},
]
}]
let state? β (state.tryTactic .unfocus "exact test Ξ±").run' (ctx := defaultElabContext)
match state? with
| .success state _ =>
checkEq "goals" state.goals.length 0
checkTrue "root" state.isSolved
| .failure messages =>
let messages β messages.mapM (Β·.toString)
checkEq "messages" messages #[];
fail "failed"
| .parseError e =>
fail s!"Parse error: {e}"
| .invalidAction e =>
fail s!"Invalid action: {e}"
private def test_distil_environment_capture : Test := do
let input := "
def mystery (n: Nat) := n + 1
theorem property (n: Nat) : mystery n + 1 = n + 2 := sorry
"
let [_dst@{ goalState := state }] β distilSearchTargets (β getEnv) input { ignoreValues := false }
| fail "Incorrect number of search states"
let goals := (β state.serializeGoals).map (Β·.devolatilize)
checkEq "goals" goals #[
{
target := { pp? := "mystery n + 1 = n + 2" },
vars := #[{
userName := `n,
type? := .some { pp? := "Nat" },
}],
}
]
checkFalse "root" state.isSolved
let .success state _ β runTermElabMInMeta do state.tryTactic .unfocus "rfl"
| fail "Tactic block failed"
checkTrue "root" state.isSolved
private def test_distil_circular : Test := do
let input := "
theorem test (p q : Prop) (hp : p) (hq : q) : p β§ q β§ p := by sorry
"
let id := "test"
let [_dst@{ goalState := state }] β distilSearchTargets (β getEnv) input
| fail "Incorrect number of search states"
let result β (state.tryTactic .unfocus s!"exact {id} p q hp hq").run' (ctx := defaultElabContext)
match result with
| .success .. =>
fail s!"This should not succeed"
| .failure messages =>
let messages β messages.mapM (Β·.toString)
checkEq "failure" messages #[s!"{β getFileName}:0:0: error(lean.unknownIdentifier): Unknown identifier `{id}`\n"]
| .parseError e =>
fail s!"Parse error: {e}"
| .invalidAction e =>
fail s!"Invalid action: {e}"
private def test_distil_companion : Test := do
let input := "
def f : Nat β Nat := sorry
def g : Nat β Nat := Ξ» x => x + 1
theorem mystery (n : Nat) : f n = g n := sorry
"
let [_dst@{ goalState := state }] β distilSearchTargets (β getEnv) input
| fail "Incorrect number of search states"
checkEq "start" ((β state.serializeGoals {}).map (Β·.devolatilize))
#[{
target := { pp? := .some "{ f // β (n : Nat), f n = g n }" },
}]
private def test_distil_multiple_cond : Test := do
let input := "
def f : Nat β Nat := sorry
theorem mystery1 : f 1 = 1 := sorry
theorem mystery2 : f 2 = 2 := sorry
"
let [_dst@{ goalState := state }] β distilSearchTargets (β getEnv) input
| fail "Incorrect number of search states"
checkEq "start" ((β state.serializeGoals {}).map (Β·.devolatilize))
#[{
target := { pp? := .some "{ f // f 1 = 1 β§ f 2 = 2 }" },
}]
private def test_distil_existing_value : Test := do
let input := "
def f : Nat β Nat := Ξ» x => x + sorry
theorem mystery1 : f 1 = 2 := sorry
theorem mystery2 : f 2 = 4 := sorry
"
let [_dst@{ goalState := state }] β distilSearchTargets (β getEnv) input { ignoreValues := false }
| fail "Incorrect number of search states"
checkEq "start" ((β state.serializeGoals {}).map (Β·.devolatilize))
#[
{
userName? := `f,
vars := #[{ userName := `x, type? := .some { pp? := .some "Nat" } }]
target := { pp? := .some "Nat" },
},
{
target := { pp? := .some "(fun x => x + ?f) 1 = 2" },
},
{
target := { pp? := .some "(fun x => x + ?f) 2 = 4" },
},
]
checkFalse "root" state.isSolved
let .success state _ β runTermElabMInMeta do state.tryTactic .unfocus "exact x; rfl; rfl"
| fail "Tactic block failed"
checkEq "goals" state.goals.length 0
checkTrue "root" state.isSolved
/-- Tests handling of newline chars -/
private def test_distil_predicate : Test := do
let input := "
structure Command where
prog : String
args : List String
deriving Repr, DecidableEq
def p (s : String) : Prop := s = \"ls\"
theorem mystery (s : String) : p s := sorry
"
let [_dst@{ goalState := state }] β distilSearchTargets (β getEnv) input
| fail "Incorrect number of search states"
checkTrue "has `p" <| (state.env.find? `p).isSome
let state? β (state.tryDraft .unfocus "by\n unfold p\n sorry").run' (ctx := defaultElabContext)
match state? with
| .success state _ =>
checkEq "goals" state.goals.length 1
| .failure messages =>
let messages β messages.mapM (Β·.toString)
checkEq "messages" messages #[];
fail "failed"
| .parseError e =>
fail s!"Parse error: {e}"
| .invalidAction e =>
fail s!"Invalid action: {e}"
def suite (env : Environment): List (String Γ IO LSpec.TestSeq) :=
let tests := [
("sorry in middle", test_sorry_in_middle),
("sorry in coupled", test_sorry_in_coupled),
("sorry with local instances (term)", test_sorry_with_local_instance false),
("sorry with local instances (tactic)", test_sorry_with_local_instance true),
("sorry circular", test_sorry_circular),
("environment_capture", test_environment_capture),
("capture_type_mismatch", test_capture_type_mismatch),
--("capture_type_mismatch_in_binder", test_capture_type_mismatch_in_binder),
("distil simple", test_distil_simple),
("distil tail", test_distil_tail),
("distil induction", test_distil_induction),
("distil instance (term)", test_distil_instance false),
("distil instance (true)", test_distil_instance true),
("distil environment capture", test_distil_environment_capture),
("distil circular", test_distil_circular),
("distil companion", test_distil_companion),
("distil multiple conditions", test_distil_multiple_cond),
("distil existing value", test_distil_existing_value),
("distil predicate", test_distil_predicate),
]
tests.map (fun (name, test) => (name, runMetaMSeq env $ runTest test))
|