1+ import Std.Do.Triple
2+ import Std.Tactic.Do
3+
4+ open Std.Do
5+
6+ /-- Min: Find the minimum element in a non-empty list.
7+
8+ Recursively finds the minimum element by comparing the last element
9+ with the minimum of the prefix.
10+ -/
11+ def min : List Int → Int
12+ | [] => 0 -- Should not happen given precondition
13+ | [a] => a
14+ | l =>
15+ let minPrefix := min (l.take (l.length - 1 ))
16+ let lastElem := l.getLast!
17+ if lastElem ≤ minPrefix then lastElem else minPrefix
18+ termination_by l => l.length
19+ decreasing_by sorry
20+
21+ /-- Max: Find the maximum element in a non-empty list.
22+
23+ Recursively finds the maximum element by comparing the last element
24+ with the maximum of the prefix.
25+ -/
26+ def max : List Int → Int
27+ | [] => 0 -- Should not happen given precondition
28+ | [a] => a
29+ | l =>
30+ let maxPrefix := max (l.take (l.length - 1 ))
31+ let lastElem := l.getLast!
32+ if lastElem ≥ maxPrefix then lastElem else maxPrefix
33+ termination_by l => l.length
34+ decreasing_by sorry
35+
36+ /-- SumMinMax: Return the sum of the minimum and maximum elements in an array.
37+
38+ Given a non-empty array, returns the sum of its minimum and maximum elements.
39+
40+ Example: sumMinMax([3, 1, 4, 1, 5]) = 1 + 5 = 6
41+ -/
42+ def sumMinMax (a : Array Int) : Id Int :=
43+ sorry
44+
45+ /-- Specification: sumMinMax returns the sum of the minimum and maximum elements.
46+
47+ Precondition: The array is non-empty
48+ Postcondition: The result equals max(a.toList) + min(a.toList)
49+ -/
50+ theorem sumMinMax_spec (a : Array Int) :
51+ ⦃⌜a.size > 0 ⌝⦄
52+ sumMinMax a
53+ ⦃⇓sum => ⌜sum = max a.toList + min a.toList⌝⦄ := by
54+ sorry
0 commit comments