forked from thanhnguyen-aws/plausible
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathBitVecStructureTest.lean
More file actions
92 lines (80 loc) · 3.61 KB
/
BitVecStructureTest.lean
File metadata and controls
92 lines (80 loc) · 3.61 KB
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
import Plausible.Arbitrary
import Plausible.DeriveArbitrary
import Plausible.Attr
import Plausible.Testable
open Plausible Gen
set_option guard_msgs.diff true
/-- Dummy `inductive` where a constructor has a dependently-typed argument (`BitVec n`)
whose index does not appear in the overall type (`DummyInductive`) -/
inductive DummyInductive where
| FromBitVec : ∀ (n : Nat), BitVec n → String → DummyInductive
deriving Repr
set_option trace.plausible.deriving.arbitrary true in
/--
trace: [plausible.deriving.arbitrary] ⏎
[mutual
def arbitraryDummyInductive✝ : Nat → Plausible.Gen (@DummyInductive✝) :=
let rec aux_arb (fuel✝ : Nat) : Plausible.Gen (@DummyInductive✝) :=
match fuel✝ with
| Nat.zero =>
Plausible.Gen.oneOfWithDefault
(do
let a✝ ← Plausible.Arbitrary.arbitrary
let a✝¹ ← Plausible.Arbitrary.arbitrary
let a✝² ← Plausible.Arbitrary.arbitrary
return DummyInductive.FromBitVec a✝ a✝¹ a✝²)
[(do
let a✝ ← Plausible.Arbitrary.arbitrary
let a✝¹ ← Plausible.Arbitrary.arbitrary
let a✝² ← Plausible.Arbitrary.arbitrary
return DummyInductive.FromBitVec a✝ a✝¹ a✝²)]
| fuel'✝ + 1 =>
Plausible.Gen.frequency
(do
let a✝ ← Plausible.Arbitrary.arbitrary
let a✝¹ ← Plausible.Arbitrary.arbitrary
let a✝² ← Plausible.Arbitrary.arbitrary
return DummyInductive.FromBitVec a✝ a✝¹ a✝²)
[(1,
(do
let a✝ ← Plausible.Arbitrary.arbitrary
let a✝¹ ← Plausible.Arbitrary.arbitrary
let a✝² ← Plausible.Arbitrary.arbitrary
return DummyInductive.FromBitVec a✝ a✝¹ a✝²)),
]
fun fuel✝ => aux_arb fuel✝
end,
instance : Plausible.ArbitraryFueled✝ (@DummyInductive✝) :=
⟨arbitraryDummyInductive✝⟩]
-/
#guard_msgs in
deriving instance Arbitrary for DummyInductive
-- Test that we can successfully synthesize instances of `Arbitrary` & `ArbitraryFueled`
/-- info: instArbitraryFueledDummyInductive -/
#guard_msgs in
#synth ArbitraryFueled DummyInductive
/-- info: instArbitraryOfArbitraryFueled -/
#guard_msgs in
#synth Arbitrary DummyInductive
/-- Shrinker for `DummyInductive` -/
def shrinkDummyInductive : DummyInductive → List DummyInductive
| .FromBitVec n bitVec str =>
let shrunkenBitVecs := Shrinkable.shrink bitVec
let shrunkenStrs := Shrinkable.shrink str
(fun (bv, s) => .FromBitVec n bv s) <$> List.zip shrunkenBitVecs shrunkenStrs
/-- `Shrinkable` instance for `DummyInductive` -/
instance : Shrinkable DummyInductive where
shrink := shrinkDummyInductive
/-- `SampleableExt` instance for `DummyInductive` -/
instance : SampleableExt DummyInductive :=
SampleableExt.mkSelfContained Arbitrary.arbitrary
/-- To test whether the derived generator can generate counterexamples,
we state an (erroneous) property that states that all `BitVec` arguments
to `DummyInductive.FromBitVec` represent the `Nat` 2, and see
if the derived generator can refute this property. -/
def BitVecEqualsTwo : DummyInductive → Bool
| .FromBitVec _ bitVec _ => bitVec.toNat == 2
/-- error: Found a counter-example! -/
#guard_msgs in
#eval Testable.check (∀ ind : DummyInductive, BitVecEqualsTwo ind)
(cfg := {numInst := 10, maxSize := 5, quiet := true})