Skip to content

Commit

Permalink
feat: make Fact testable for slim_check (leanprover-community#12566)
Browse files Browse the repository at this point in the history
Closes leanprover-community#12565

Also adds a sort-of-missing `g.withContext`. I don't think its absence causes any errors.
  • Loading branch information
kmill committed May 1, 2024
1 parent dc5d5b4 commit 59710fc
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Mathlib/Logic/Basic.lean
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ theorem fact_iff {p : Prop} : Fact p ↔ p := ⟨fun h ↦ h.1, fun h ↦ ⟨h
#align fact_iff fact_iff
#align fact.elim Fact.elim

instance {p : Prop} [Decidable p] : Decidable (Fact p) :=
decidable_of_iff _ fact_iff.symm

/-- Swaps two pairs of arguments to a function. -/
@[reducible] def Function.swap₂ {ι₁ ι₂ : Sort*} {κ₁ : ι₁ → Sort*} {κ₂ : ι₂ → Sort*}
{φ : ∀ i₁, κ₁ i₁ → ∀ i₂, κ₂ i₂ → Sort*} (f : ∀ i₁ j₁ i₂ j₂, φ i₁ j₁ i₂ j₂)
Expand Down
1 change: 1 addition & 0 deletions Mathlib/Tactic/SlimCheck.lean
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ syntax (name := slimCheckSyntax) "slim_check" (config)? : tactic
elab_rules : tactic | `(tactic| slim_check $[$cfg]?) => withMainContext do
let cfg ← elabConfig (mkOptionalNode cfg)
let (_, g) ← (← getMainGoal).revert ((← getLocalHyps).map (Expr.fvarId!))
g.withContext do
let tgt ← g.getType
let tgt' ← addDecorations tgt
let cfg := { cfg with
Expand Down
8 changes: 8 additions & 0 deletions Mathlib/Testing/SlimCheck/Testable.lean
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,11 @@ instance forallTypesTestable {f : Type → Prop} [Testable (f Int)] :
let r ← runProp (f Int) cfg min
pure <| addVarInfo var "ℤ" (· <| Int) r

instance factTestable [Testable p] : Testable (Fact p) where
run cfg min := do
let h ← runProp p cfg min
pure <| iff fact_iff h

/--
Format the counter-examples found in a test failure.
-/
Expand Down Expand Up @@ -451,6 +456,9 @@ instance False.printableProp : PrintableProp False where
instance Bool.printableProp {b : Bool} : PrintableProp b where
printProp := if b then "true" else "false"

instance Fact.printableProp [PrintableProp p] : PrintableProp (Fact p) where
printProp := printProp p

end PrintableProp

section IO
Expand Down
18 changes: 18 additions & 0 deletions test/slim_check.lean
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Mathlib.Tactic.SuccessIfFailWithMsg
import Mathlib.Data.Finsupp.Notation
import Mathlib.Testing.SlimCheck.Functions
import Mathlib.Tactic.Have
import Mathlib.Data.Nat.Prime

private axiom test_sorry : ∀ {α}, α

Expand Down Expand Up @@ -452,3 +453,20 @@ theorem testBit_pred :
testBit (pred x) i = (decide (0 < x) &&
(Bool.xor ((List.range i).all fun j => ! testBit x j) (testBit x i))) := by
slim_check

-- https://github.com/leanprover-community/mathlib4/issues/12565
-- Make `slim_check` handle `Fact` instances.
/--
error:
===================
Found problems!
a := 7
guard: ⋯
issue: ⋯ does not hold
issue: ⋯ does not hold
(0 shrinks)
-------------------
-/
#guard_msgs in
example {a : ℕ} [Fact a.Prime] : (a + 1).Prime ∨ (a + 2).Prime := by
slim_check (config := { randomSeed := some 257 })

0 comments on commit 59710fc

Please sign in to comment.