11module
22
3- def filter_integers : Unit :=
4- ()
3+ public import Std
4+ open Std
5+
6+ public section
7+
8+ /-!
9+ ## Implementation
10+
11+ This Problem assumes a language with dynamic typing. We approximate this by defining an inductive
12+ `Any` type.
13+ -/
14+
15+ inductive Any where
16+ | int : Int → Any
17+ | float : Float → Any
18+ | string : String → Any
19+
20+ @ [grind =]
21+ def Any.int? (x : Any) : Option Int :=
22+ if let .int a := x then
23+ some a
24+ else
25+ none
26+
27+ @ [grind =]
28+ def filterIntegers (xs : Array Any): Array Int :=
29+ xs.filterMap Any.int?
30+
31+ /-! ## Tests -/
32+
33+ example : filterIntegers #[] = #[] := by native_decide
34+ example : filterIntegers #[.int 4 , .float 23 .2 , .int 9 , .string "adasd" ] = #[4 , 9 ] := by native_decide
35+ example : filterIntegers #[.int 3 , .string "c" , .int 3 , .int 3 , .string "a" , .string "b" ] = #[3 , 3 , 3 ] := by native_decide
36+
37+ /-! ## Verification -/
38+
39+ section Verification
40+
41+ variable {xs ys : Array Any}
42+
43+ theorem filterIntegers_empty : filterIntegers #[] = #[] := by grind
44+ theorem filterIntegers_singleton_int : filterIntegers #[.int x] = #[x] := by grind
45+ theorem filterIntegers_singleton_float : filterIntegers #[.float x] = #[] := by grind
46+ theorem filterIntegers_singleton_string : filterIntegers #[.string x] = #[] := by grind
47+ theorem filterIntegers_push_int : filterIntegers (xs.push (.int x)) = (filterIntegers xs).push x := by grind
48+ theorem filterIntegers_push_float : filterIntegers (xs.push (.float x)) = filterIntegers xs := by grind
49+ theorem filterIntegers_push_string : filterIntegers (xs.push (.string x)) = filterIntegers xs := by grind
50+ theorem filterIntegers_append : filterIntegers (x ++ y) = filterIntegers x ++ filterIntegers y := by grind
51+
52+ end Verification
553
654/-!
755## Prompt
@@ -41,4 +89,4 @@ def check(candidate):
4189 assert candidate([4, {}, [], 23.2, 9, 'adasd']) == [4, 9]
4290 assert candidate([3, 'c', 3, 3, 'a', 'b']) == [3, 3, 3]
4391```
44- -/
92+ -/
0 commit comments