-
Notifications
You must be signed in to change notification settings - Fork 447
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: occurs check at metavariable types (#4420)
closes #4405
- Loading branch information
1 parent
a1c8a94
commit c8e668a
Showing
5 changed files
with
231 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import Lean.Elab.Command | ||
|
||
/-- | ||
error: application type mismatch | ||
⟨Nat.lt_irrefl ↑(?m.58 n), Fin.is_lt (?m.58 n)⟩ | ||
argument | ||
Fin.is_lt (?m.58 n) | ||
has type | ||
↑(?m.58 n) < ?m.57 n : Prop | ||
but is expected to have type | ||
↑(?m.58 n) < ↑(?m.58 n) : Prop | ||
-/ | ||
#guard_msgs in | ||
def foo := fun n => (not_and_self_iff _).mp ⟨Nat.lt_irrefl _, Fin.is_lt _⟩ | ||
|
||
/-- | ||
error: type mismatch | ||
Fin.is_lt ?m.185 | ||
has type | ||
↑?m.185 < ?m.184 : Prop | ||
but is expected to have type | ||
?a < ?a : Prop | ||
--- | ||
error: unsolved goals | ||
case a | ||
⊢ Nat | ||
this : ?a < ?a | ||
⊢ True | ||
-/ | ||
#guard_msgs in | ||
def test : True := by | ||
have : ((?a : Nat) < ?a : Prop) := by | ||
refine Fin.is_lt ?_ | ||
done | ||
done | ||
|
||
open Lean Meta | ||
/-- | ||
info: Defeq?: false | ||
--- | ||
info: fun x_0 x_1 => x_1 | ||
-/ | ||
#guard_msgs in | ||
run_meta do | ||
let mvarIdNat ← mkFreshExprMVar (.some (.const ``Nat [])) | ||
let mvarIdFin ← mkFreshExprMVar (.some (.app (.const `Fin []) mvarIdNat)) | ||
-- mvarIdNat.assign (.app (.const ``Fin.val []) mvaridFin)) | ||
let b ← isDefEq mvarIdNat (mkApp2 (.const ``Fin.val []) mvarIdNat mvarIdFin) | ||
logInfo m!"Defeq?: {b}" -- prints true | ||
-- Now mvaridNat occurs in its own type | ||
-- This will stack overflow | ||
let r ← abstractMVars mvarIdFin (levels := false) | ||
logInfo m!"{r.expr}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
namespace SlimCheck | ||
|
||
inductive TestResult (p : Prop) where | ||
| success : PSum Unit p → TestResult p | ||
| gaveUp : Nat → TestResult p | ||
| failure : ¬ p → List String → Nat → TestResult p | ||
deriving Inhabited | ||
|
||
/-- Configuration for testing a property. -/ | ||
structure Configuration where | ||
numInst : Nat := 100 | ||
maxSize : Nat := 100 | ||
numRetries : Nat := 10 | ||
traceDiscarded : Bool := false | ||
traceSuccesses : Bool := false | ||
traceShrink : Bool := false | ||
traceShrinkCandidates : Bool := false | ||
randomSeed : Option Nat := none | ||
quiet : Bool := false | ||
deriving Inhabited | ||
|
||
abbrev Rand := Id | ||
|
||
abbrev Gen (α : Type u) := ReaderT (ULift Nat) Rand α | ||
|
||
/-- `Testable p` uses random examples to try to disprove `p`. -/ | ||
class Testable (p : Prop) where | ||
run (cfg : Configuration) (minimize : Bool) : Gen (TestResult p) | ||
|
||
def NamedBinder (_n : String) (p : Prop) : Prop := p | ||
|
||
namespace TestResult | ||
|
||
def isFailure : TestResult p → Bool | ||
| failure _ _ _ => true | ||
| _ => false | ||
|
||
end TestResult | ||
|
||
namespace Testable | ||
|
||
open TestResult | ||
|
||
def runProp (p : Prop) [Testable p] : Configuration → Bool → Gen (TestResult p) := Testable.run | ||
|
||
variable {var : String} | ||
|
||
def addShrinks (n : Nat) : TestResult p → TestResult p | ||
| TestResult.failure p xs m => TestResult.failure p xs (m + n) | ||
| p => p | ||
|
||
instance [Pure m] : Inhabited (OptionT m α) := ⟨(pure none : m (Option α))⟩ | ||
|
||
class Shrinkable (α : Type u) where | ||
shrink : (x : α) → List α := fun _ ↦ [] | ||
|
||
class SampleableExt (α : Sort u) where | ||
proxy : Type v | ||
[proxyRepr : Repr proxy] | ||
[shrink : Shrinkable proxy] | ||
sample : Gen proxy | ||
interp : proxy → α | ||
|
||
partial def minimizeAux [SampleableExt α] {β : α → Prop} [∀ x, Testable (β x)] (cfg : Configuration) | ||
(var : String) (x : SampleableExt.proxy α) (n : Nat) : | ||
OptionT Gen (Σ x, TestResult (β (SampleableExt.interp x))) := do | ||
let candidates := SampleableExt.shrink.shrink x | ||
for candidate in candidates do | ||
let res ← OptionT.lift <| Testable.runProp (β (SampleableExt.interp candidate)) cfg true | ||
if res.isFailure then | ||
if cfg.traceShrink then | ||
pure () -- slimTrace s!"{var} shrunk to {repr candidate} from {repr x}" | ||
let currentStep := OptionT.lift <| pure <| Sigma.mk candidate (addShrinks (n + 1) res) | ||
let nextStep := minimizeAux cfg var candidate (n + 1) | ||
return ← (nextStep <|> currentStep) | ||
failure |