Built with doc-gen4, running Lean4. Bubbles () indicate interactive fragments: hover for details, tap to reveal contents. Use Ctrl+โ†‘Ctrl+โ†“to navigate, Ctrl+๐Ÿ–ฑ๏ธto focus. On Mac, use Cmdinstead of Ctrl.
import Mathlib

/-!
# Minimum of a list of natural numbers

Our next example is an algorithm to find the minimum of a non-empty list of natural numbers, with a proof of correctness. The proof of correctness consists of two statements about the algorithm: (1) the minimum is in the list, and (2) the minimum is less than or equal to each element in the list.

To avoid name collisions we will use the name `smallest`
-/

/-- The smallest element in a non-empty list of natural numbers  -/
def 
smallest: (l : List โ„•) โ†’ l โ‰  [] โ†’ โ„•
smallest
(l :
List: Type ?u.2 โ†’ Type ?u.2
List
โ„•: Type
โ„•
)(
_: l โ‰  []
_
: l โ‰ 
[]: List ?m.9
[]
) :
โ„•: Type
โ„•
:= match l with |
head: โ„•
head
:: tail => if
c: ?m.130
c
:tail =
[]: List ?m.57
[]
then
head: โ„•
head
else
Nat.min: โ„• โ†’ โ„• โ†’ โ„•
Nat.min
head: โ„•
head
(
smallest: (l : List โ„•) โ†’ l โ‰  [] โ†’ โ„•
smallest
tail
c: ?m.130
c
) /-! An example ```lean #eval smallest [7, 8, 1,2,3,4,5] (by decide) ``` -/
1
smallest: (l : List โ„•) โ†’ l โ‰  [] โ†’ โ„•
smallest
[
7: ?m.491
7
,
8: ?m.503
8
,
1: ?m.511
1
,
2: ?m.520
2
,
3: ?m.528
3
,
4: ?m.536
4
,
5: ?m.544
5
] (

Goals accomplished! ๐Ÿ™

[7, 8, 1, 2, 3, 4, 5] โ‰  []

Goals accomplished! ๐Ÿ™
) /-- the result of `smallest` applied to a list is a member of that list -/ theorem
smallest_in_list: โˆ€ (l : List โ„•) (hyp : l โ‰  []), smallest l hyp โˆˆ l
smallest_in_list
(l :
List: Type ?u.1081 โ†’ Type ?u.1081
List
โ„•: Type
โ„•
)(
hyp: l โ‰  []
hyp
: l โ‰ 
[]: List ?m.1088
[]
) :
smallest: (l : List โ„•) โ†’ l โ‰  [] โ†’ โ„•
smallest
l
hyp: l โ‰  []
hyp
โˆˆ l :=

Goals accomplished! ๐Ÿ™
l: List โ„•

hyp: l โ‰  []


l: List โ„•

hyp: l โ‰  []


l: List โ„•

head: โ„•

tail: List โ„•

hyp: head :: tail โ‰  []


smallest (head :: tail) hyp โˆˆ head :: tail
l: List โ„•

hyp: l โ‰  []


l: List โ„•

head: โ„•

tail: List โ„•

hyp: head :: tail โ‰  []

c: tail = []


pos
smallest (head :: tail) hyp โˆˆ head :: tail
l: List โ„•

head: โ„•

tail: List โ„•

hyp: head :: tail โ‰  []

c: ยฌtail = []


neg
smallest (head :: tail) hyp โˆˆ head :: tail
l: List โ„•

head: โ„•

tail: List โ„•

hyp: head :: tail โ‰  []


smallest (head :: tail) hyp โˆˆ head :: tail
l: List โ„•

head: โ„•

tail: List โ„•

hyp: head :: tail โ‰  []

c: tail = []


pos
smallest (head :: tail) hyp โˆˆ head :: tail
l: List โ„•

head: โ„•

tail: List โ„•

hyp: head :: tail โ‰  []

c: ยฌtail = []


neg
smallest (head :: tail) hyp โˆˆ head :: tail

Goals accomplished! ๐Ÿ™
l: List โ„•

head: โ„•

tail: List โ„•

hyp: head :: tail โ‰  []


smallest (head :: tail) hyp โˆˆ head :: tail
l: List โ„•

head: โ„•

tail: List โ„•

hyp: head :: tail โ‰  []

c: ยฌtail = []


neg
smallest (head :: tail) hyp โˆˆ head :: tail
l: List โ„•

head: โ„•

tail: List โ„•

hyp: head :: tail โ‰  []

c: ยฌtail = []


neg
head โ‰ค smallest tail (_ : ยฌtail = []) โˆจ Nat.min head (smallest tail (_ : ยฌtail = [])) โˆˆ tail
l: List โ„•

head: โ„•

tail: List โ„•

hyp: head :: tail โ‰  []

c: ยฌtail = []


neg
smallest (head :: tail) hyp โˆˆ head :: tail
l: List โ„•

head: โ„•

tail: List โ„•

hyp: head :: tail โ‰  []

c: ยฌtail = []


neg
head โ‰ค smallest tail (_ : ยฌtail = []) โˆจ Nat.min head (smallest tail (_ : ยฌtail = [])) โˆˆ tail

Goals accomplished! ๐Ÿ™
l: List โ„•

head: โ„•

tail: List โ„•

hyp: head :: tail โ‰  []

c: ยฌtail = []


tail โ‰  []

Goals accomplished! ๐Ÿ™
l: List โ„•

head: โ„•

tail: List โ„•

hyp: head :: tail โ‰  []

c: ยฌtail = []

c': head โ‰ค smallest tail (_ : ยฌtail = [])


pos
head โ‰ค smallest tail (_ : ยฌtail = []) โˆจ Nat.min head (smallest tail (_ : ยฌtail = [])) โˆˆ tail
l: List โ„•

head: โ„•

tail: List โ„•

hyp: head :: tail โ‰  []

c: ยฌtail = []

c': ยฌhead โ‰ค smallest tail (_ : ยฌtail = [])


neg
head โ‰ค smallest tail (_ : ยฌtail = []) โˆจ Nat.min head (smallest tail (_ : ยฌtail = [])) โˆˆ tail
l: List โ„•

head: โ„•

tail: List โ„•

hyp: head :: tail โ‰  []

c: ยฌtail = []


neg
smallest (head :: tail) hyp โˆˆ head :: tail
l: List โ„•

head: โ„•

tail: List โ„•

hyp: head :: tail โ‰  []

c: ยฌtail = []

c': head โ‰ค smallest tail (_ : ยฌtail = [])


pos
head โ‰ค smallest tail (_ : ยฌtail = []) โˆจ Nat.min head (smallest tail (_ : ยฌtail = [])) โˆˆ tail
l: List โ„•

head: โ„•

tail: List โ„•

hyp: head :: tail โ‰  []

c: ยฌtail = []

c': ยฌhead โ‰ค smallest tail (_ : ยฌtail = [])


neg
head โ‰ค smallest tail (_ : ยฌtail = []) โˆจ Nat.min head (smallest tail (_ : ยฌtail = [])) โˆˆ tail
l: List โ„•

head: โ„•

tail: List โ„•

hyp: head :: tail โ‰  []

c: ยฌtail = []

c': head โ‰ค smallest tail (_ : ยฌtail = [])


pos.h
head โ‰ค smallest tail (_ : ยฌtail = [])
l: List โ„•

head: โ„•

tail: List โ„•

hyp: head :: tail โ‰  []

c: ยฌtail = []

c': head โ‰ค smallest tail (_ : ยฌtail = [])


pos
head โ‰ค smallest tail (_ : ยฌtail = []) โˆจ Nat.min head (smallest tail (_ : ยฌtail = [])) โˆˆ tail
l: List โ„•

head: โ„•

tail: List โ„•

hyp: head :: tail โ‰  []

c: ยฌtail = []

c': ยฌhead โ‰ค smallest tail (_ : ยฌtail = [])


neg
head โ‰ค smallest tail (_ : ยฌtail = []) โˆจ Nat.min head (smallest tail (_ : ยฌtail = [])) โˆˆ tail

Goals accomplished! ๐Ÿ™
l: List โ„•

head: โ„•

tail: List โ„•

hyp: head :: tail โ‰  []

c: ยฌtail = []


neg
smallest (head :: tail) hyp โˆˆ head :: tail
l: List โ„•

head: โ„•

tail: List โ„•

hyp: head :: tail โ‰  []

c: ยฌtail = []

c': ยฌhead โ‰ค smallest tail (_ : ยฌtail = [])


neg
head โ‰ค smallest tail (_ : ยฌtail = []) โˆจ Nat.min head (smallest tail (_ : ยฌtail = [])) โˆˆ tail
l: List โ„•

head: โ„•

tail: List โ„•

hyp: head :: tail โ‰  []

c: ยฌtail = []

c': ยฌhead โ‰ค smallest tail (_ : ยฌtail = [])


neg.h
Nat.min head (smallest tail (_ : ยฌtail = [])) โˆˆ tail
l: List โ„•

head: โ„•

tail: List โ„•

hyp: head :: tail โ‰  []

c: ยฌtail = []

c': ยฌhead โ‰ค smallest tail (_ : ยฌtail = [])


neg
head โ‰ค smallest tail (_ : ยฌtail = []) โˆจ Nat.min head (smallest tail (_ : ยฌtail = [])) โˆˆ tail
l: List โ„•

head: โ„•

tail: List โ„•

hyp: head :: tail โ‰  []

c: ยฌtail = []

c': ยฌhead โ‰ค smallest tail (_ : ยฌtail = [])


neg.h
Nat.min head (smallest tail (_ : ยฌtail = [])) โˆˆ tail

Goals accomplished! ๐Ÿ™
l: List โ„•

head: โ„•

tail: List โ„•

hyp: head :: tail โ‰  []

c: ยฌtail = []

c': ยฌhead โ‰ค smallest tail (_ : ยฌtail = [])


tail โ‰  []

Goals accomplished! ๐Ÿ™
l: List โ„•

head: โ„•

tail: List โ„•

hyp: head :: tail โ‰  []

c: ยฌtail = []

c': ยฌhead โ‰ค smallest tail (_ : ยฌtail = [])


neg.h
Nat.min head (smallest tail (_ : ยฌtail = [])) โˆˆ tail

Goals accomplished! ๐Ÿ™
l: List โ„•

head: โ„•

tail: List โ„•

hyp: head :: tail โ‰  []

c: ยฌtail = []

c': ยฌhead โ‰ค smallest tail (_ : ยฌtail = [])


tail โ‰  []

Goals accomplished! ๐Ÿ™
l: List โ„•

head: โ„•

tail: List โ„•

hyp: head :: tail โ‰  []

c: ยฌtail = []

c': ยฌhead โ‰ค smallest tail (_ : ยฌtail = [])


neg.h
Nat.min head (smallest tail (_ : ยฌtail = [])) โˆˆ tail

Goals accomplished! ๐Ÿ™
l: List โ„•

head: โ„•

tail: List โ„•

hyp: head :: tail โ‰  []

c: ยฌtail = []

c': ยฌhead โ‰ค smallest tail (_ : ยฌtail = [])


Nat.min head (smallest tail (_ : ยฌtail = [])) = smallest tail (_ : ยฌtail = [])
l: List โ„•

head: โ„•

tail: List โ„•

hyp: head :: tail โ‰  []

c: ยฌtail = []

c': ยฌhead โ‰ค smallest tail (_ : ยฌtail = [])


h
smallest tail (_ : ยฌtail = []) โ‰ค head
l: List โ„•

head: โ„•

tail: List โ„•

hyp: head :: tail โ‰  []

c: ยฌtail = []

c': ยฌhead โ‰ค smallest tail (_ : ยฌtail = [])


Nat.min head (smallest tail (_ : ยฌtail = [])) = smallest tail (_ : ยฌtail = [])
l: List โ„•

head: โ„•

tail: List โ„•

hyp: head :: tail โ‰  []

c: ยฌtail = []

c': ยฌhead โ‰ค smallest tail (_ : ยฌtail = [])


h.a
ยฌhead โ‰ค smallest tail (_ : ยฌtail = [])
l: List โ„•

head: โ„•

tail: List โ„•

hyp: head :: tail โ‰  []

c: ยฌtail = []

c': ยฌhead โ‰ค smallest tail (_ : ยฌtail = [])


Nat.min head (smallest tail (_ : ยฌtail = [])) = smallest tail (_ : ยฌtail = [])

Goals accomplished! ๐Ÿ™
l: List โ„•

head: โ„•

tail: List โ„•

hyp: head :: tail โ‰  []

c: ยฌtail = []

c': ยฌhead โ‰ค smallest tail (_ : ยฌtail = [])


neg
head โ‰ค smallest tail (_ : ยฌtail = []) โˆจ Nat.min head (smallest tail (_ : ยฌtail = [])) โˆˆ tail
l: List โ„•

head: โ„•

tail: List โ„•

hyp: head :: tail โ‰  []

c: ยฌtail = []

c': ยฌhead โ‰ค smallest tail (_ : ยฌtail = [])

lem: Nat.min head (smallest tail (_ : ยฌtail = [])) = smallest tail (_ : ยฌtail = [])


neg.h
Nat.min head (smallest tail (_ : ยฌtail = [])) โˆˆ tail
l: List โ„•

head: โ„•

tail: List โ„•

hyp: head :: tail โ‰  []

c: ยฌtail = []

c': ยฌhead โ‰ค smallest tail (_ : ยฌtail = [])

lem: Nat.min head (smallest tail (_ : ยฌtail = [])) = smallest tail (_ : ยฌtail = [])


neg.h
smallest tail (_ : ยฌtail = []) โˆˆ tail
l: List โ„•

head: โ„•

tail: List โ„•

hyp: head :: tail โ‰  []

c: ยฌtail = []

c': ยฌhead โ‰ค smallest tail (_ : ยฌtail = [])

lem: Nat.min head (smallest tail (_ : ยฌtail = [])) = smallest tail (_ : ยฌtail = [])


neg.h
smallest tail (_ : ยฌtail = []) โˆˆ tail
l: List โ„•

head: โ„•

tail: List โ„•

hyp: head :: tail โ‰  []

c: ยฌtail = []

c': ยฌhead โ‰ค smallest tail (_ : ยฌtail = [])


neg
head โ‰ค smallest tail (_ : ยฌtail = []) โˆจ Nat.min head (smallest tail (_ : ยฌtail = [])) โˆˆ tail

Goals accomplished! ๐Ÿ™
Nat.min_eq_right {a b : โ„•} (h : b โ‰ค a) : min a b = b
Nat.min_eq_right: โˆ€ {a b : โ„•}, b โ‰ค a โ†’ min a b = b
Nat.min_eq_right
-- Nat.min_eq_right {a b : โ„•} (h : b โ‰ค a) : min a b = b /-- An example of using `left` and `right` tactics. -/ def
egNat: โ„•
egNat
:
โ„•: Type
โ„•
:=

Goals accomplished! ๐Ÿ™

Goals accomplished! ๐Ÿ™
1
egNat: โ„•
egNat
-- 1 /-- `smallest` is less than or equal to each element in the list -/ theorem
smallest_le: โˆ€ (l : List โ„•) (hyp : l โ‰  []) (m : โ„•), m โˆˆ l โ†’ smallest l hyp โ‰ค m
smallest_le
(l :
List: Type ?u.21976 โ†’ Type ?u.21976
List
โ„•: Type
โ„•
) (
hyp: l โ‰  []
hyp
: l โ‰ 
[]: List ?m.21983
[]
) : โˆ€ m :
โ„•: Type
โ„•
, m โˆˆ l โ†’
smallest: (l : List โ„•) โ†’ l โ‰  [] โ†’ โ„•
smallest
l
hyp: l โ‰  []
hyp
โ‰ค m := match l with |
head: โ„•
head
:: tail =>

Goals accomplished! ๐Ÿ™
l: List โ„•

head: โ„•

tail: List โ„•

hyp: head :: tail โ‰  []


โˆ€ (m : โ„•), m โˆˆ head :: tail โ†’ smallest (head :: tail) hyp โ‰ค m
l: List โ„•

head: โ„•

tail: List โ„•

hyp: head :: tail โ‰  []


(if c : tail = [] then head else Nat.min head (smallest tail c)) โ‰ค head โˆง โˆ€ (a : โ„•), a โˆˆ tail โ†’ (if c : tail = [] then head else Nat.min head (smallest tail c)) โ‰ค a
l: List โ„•

head: โ„•

tail: List โ„•

hyp: head :: tail โ‰  []


โˆ€ (m : โ„•), m โˆˆ head :: tail โ†’ smallest (head :: tail) hyp โ‰ค m
l: List โ„•

head: โ„•

tail: List โ„•

hyp: head :: tail โ‰  []


left
(if c : tail = [] then head else Nat.min head (smallest tail c)) โ‰ค head
l: List โ„•

head: โ„•

tail: List โ„•

hyp: head :: tail โ‰  []


right
โˆ€ (a : โ„•), a โˆˆ tail โ†’ (if c : tail = [] then head else Nat.min head (smallest tail c)) โ‰ค a
l: List โ„•

head: โ„•

tail: List โ„•

hyp: head :: tail โ‰  []


โˆ€ (m : โ„•), m โˆˆ head :: tail โ†’ smallest (head :: tail) hyp โ‰ค m
l: List โ„•

head: โ„•

tail: List โ„•

hyp: head :: tail โ‰  []


left
(if c : tail = [] then head else Nat.min head (smallest tail c)) โ‰ค head
l: List โ„•

head: โ„•

tail: List โ„•

hyp: head :: tail โ‰  []


right
โˆ€ (a : โ„•), a โˆˆ tail โ†’ (if c : tail = [] then head else Nat.min head (smallest tail c)) โ‰ค a
l: List โ„•

head: โ„•

tail: List โ„•

hyp: head :: tail โ‰  []

hโœ: tail = []


left.inl
head โ‰ค head
l: List โ„•

head: โ„•

tail: List โ„•

hyp: head :: tail โ‰  []

hโœ: ยฌtail = []


left.inr
Nat.min head (smallest tail hโœ) โ‰ค head
l: List โ„•

head: โ„•

tail: List โ„•

hyp: head :: tail โ‰  []


left
(if c : tail = [] then head else Nat.min head (smallest tail c)) โ‰ค head
l: List โ„•

head: โ„•

tail: List โ„•

hyp: head :: tail โ‰  []

hโœ: tail = []


left.inl
head โ‰ค head
l: List โ„•

head: โ„•

tail: List โ„•

hyp: head :: tail โ‰  []

hโœ: ยฌtail = []


left.inr
Nat.min head (smallest tail hโœ) โ‰ค head
l: List โ„•

head: โ„•

tail: List โ„•

hyp: head :: tail โ‰  []


left
(if c : tail = [] then head else Nat.min head (smallest tail c)) โ‰ค head

Goals accomplished! ๐Ÿ™
l: List โ„•

head: โ„•

tail: List โ„•

hyp: head :: tail โ‰  []


โˆ€ (m : โ„•), m โˆˆ head :: tail โ†’ smallest (head :: tail) hyp โ‰ค m
l: List โ„•

head: โ„•

tail: List โ„•

hyp: head :: tail โ‰  []


right
โˆ€ (a : โ„•), a โˆˆ tail โ†’ (if c : tail = [] then head else Nat.min head (smallest tail c)) โ‰ค a
l: List โ„•

head: โ„•

tail: List โ„•

hyp: head :: tail โ‰  []

a: โ„•

hyp': a โˆˆ tail


right
(if c : tail = [] then head else Nat.min head (smallest tail c)) โ‰ค a
l: List โ„•

head: โ„•

tail: List โ„•

hyp: head :: tail โ‰  []


right
โˆ€ (a : โ„•), a โˆˆ tail โ†’ (if c : tail = [] then head else Nat.min head (smallest tail c)) โ‰ค a
l: List โ„•

head: โ„•

tail: List โ„•

hyp: head :: tail โ‰  []

a: โ„•

hyp': a โˆˆ tail


right
(if c : tail = [] then head else Nat.min head (smallest tail c)) โ‰ค a

Goals accomplished! ๐Ÿ™
l: List โ„•

head: โ„•

tail: List โ„•

hyp: head :: tail โ‰  []

a: โ„•

hyp': a โˆˆ tail


tail โ‰  []
l: List โ„•

head: โ„•

tail: List โ„•

hyp: head :: tail โ‰  []

a: โ„•

hyp': a โˆˆ tail

contra: tail = []


l: List โ„•

head: โ„•

tail: List โ„•

hyp: head :: tail โ‰  []

a: โ„•

hyp': a โˆˆ tail


tail โ‰  []
l: List โ„•

head: โ„•

tail: List โ„•

hyp: head :: tail โ‰  []

a: โ„•

hyp': a โˆˆ tail

contra: tail = []


l: List โ„•

head: โ„•

tail: List โ„•

hyp: head :: tail โ‰  []

a: โ„•

hyp': a โˆˆ []

contra: tail = []


l: List โ„•

head: โ„•

tail: List โ„•

hyp: head :: tail โ‰  []

a: โ„•

hyp': a โˆˆ []

contra: tail = []


l: List โ„•

head: โ„•

tail: List โ„•

hyp: head :: tail โ‰  []

a: โ„•

hyp': a โˆˆ []

contra: tail = []


l: List โ„•

head: โ„•

tail: List โ„•

hyp: head :: tail โ‰  []

a: โ„•

hyp': a โˆˆ tail


tail โ‰  []

Goals accomplished! ๐Ÿ™
l: List โ„•

head: โ„•

tail: List โ„•

hyp: head :: tail โ‰  []


right
โˆ€ (a : โ„•), a โˆˆ tail โ†’ (if c : tail = [] then head else Nat.min head (smallest tail c)) โ‰ค a
l: List โ„•

head: โ„•

tail: List โ„•

hyp: head :: tail โ‰  []

a: โ„•

hyp': a โˆˆ tail

c'': tail โ‰  []


right
head โ‰ค a โˆจ smallest tail (_ : ยฌtail = []) โ‰ค a
l: List โ„•

head: โ„•

tail: List โ„•

hyp: head :: tail โ‰  []


right
โˆ€ (a : โ„•), a โˆˆ tail โ†’ (if c : tail = [] then head else Nat.min head (smallest tail c)) โ‰ค a
l: List โ„•

head: โ„•

tail: List โ„•

hyp: head :: tail โ‰  []

a: โ„•

hyp': a โˆˆ tail

c'': tail โ‰  []


right.h
smallest tail (_ : ยฌtail = []) โ‰ค a
l: List โ„•

head: โ„•

tail: List โ„•

hyp: head :: tail โ‰  []


right
โˆ€ (a : โ„•), a โˆˆ tail โ†’ (if c : tail = [] then head else Nat.min head (smallest tail c)) โ‰ค a

Goals accomplished! ๐Ÿ™
List.mem_cons.{u_1} {ฮฑโœ : Type u_1} {a b : ฮฑโœ} {l : List ฮฑโœ} : a โˆˆ b :: l โ†” a = b โˆจ a โˆˆ l
List.mem_cons: โˆ€ {ฮฑ : Type u_1} {a b : ฮฑ} {l : List ฮฑ}, a โˆˆ b :: l โ†” a = b โˆจ a โˆˆ l
List.mem_cons