11module
22
3- def monotonic : Unit :=
4- ()
3+ /-! ## Implementation -/
4+
5+ def monotonic (xs : List Int) : Bool :=
6+ xs.Pairwise (· ≤ ·) || xs.Pairwise (· ≥ ·)
7+
8+ /-! ## Tests -/
9+
10+ set_option cbv.warning false
11+
12+ example : monotonic [1 , 2 , 4 , 10 ] = true := by cbv
13+ example : monotonic [1 , 2 , 4 , 20 ] = true := by cbv
14+ example : monotonic [1 , 20 , 4 , 10 ] = false := by cbv
15+ example : monotonic [4 , 1 , 0 , -10 ] = true := by cbv
16+ example : monotonic [4 , 1 , 1 , 0 ] = true := by cbv
17+ example : monotonic [1 , 2 , 3 , 2 , 5 , 60 ] = false := by cbv
18+ example : monotonic [1 , 2 , 3 , 4 , 5 , 60 ] = true := by cbv
19+ example : monotonic [9 , 9 , 9 , 9 ] = true := by cbv
20+
21+ /-! ## Verification -/
22+
23+ theorem monotonic_iff :
24+ monotonic xs ↔
25+ (∀ (i j : Nat) (hi : i < j) (hj : j < xs.length), xs[i] ≤ xs[j]) ∨
26+ (∀ (i j : Nat) (hi : i < j) (hj : j < xs.length), xs[i] ≥ xs[j]) := by
27+ grind [monotonic, List.pairwise_iff_getElem]
528
629/-!
730## Prompt
@@ -47,4 +70,4 @@ def check(candidate):
4770 assert candidate([9, 9, 9, 9]) == True
4871
4972```
50- -/
73+ -/
0 commit comments