Martin Escardo and Paulo Oliva, October 2021, with later additions.
We have various versions of argmin and argmax with different assumptions.
\begin{code}
{-# OPTIONS --safe --without-K #-}
module Games.ArgMinMax where
open import MLTT.Spartan hiding (_+_ ; J)
\end{code}
In this version of argmin and argmax we a assume a finite domain with
a finite type of outcomes.
\begin{code}
module ArgMinMax-Fin where
open import Fin.Embeddings
open import Fin.Order
open import Fin.Topology
open import Fin.Type
open import MLTT.SpartanList
open import Naturals.Order
open import Notation.Order
open import NotionsOfDecidability.Complemented
\end{code}
Every inhabited complemented subset of Fin n has a least and a
greatest element.
\begin{code}
Fin-wf : {n : β} (A : Fin n β π€ Μ ) (rβ : Fin n)
β is-complemented A
β A rβ
β Ξ£ r κ Fin n , A r Γ ((s : Fin n) β A s β r β€ s)
Fin-wf {π€} {succ n} A π d a = π , a , Ξ» s a' β β¨β©
Fin-wf {π€} {succ n} A (suc rβ) d a = Ξ³
where
IH : Ξ£ r κ Fin n , A (suc r) Γ ((s : Fin n) β A (suc s) β r β€ s)
IH = Fin-wf {π€} {n} (Ξ» x β A (suc x)) rβ (Ξ» x β d (suc x)) a
r : Fin n
r = prβ IH
b : A (suc r)
b = prβ (prβ IH)
c : (s : Fin n) β A (suc s) β r β€ s
c = prβ (prβ IH)
l : Β¬ A π β (s : Fin (succ n)) β A s β suc r β€ s
l Ξ½ π a = π-elim (Ξ½ a)
l Ξ½ (suc x) a = c x a
Ξ³ : Ξ£ r κ Fin (succ n) , A r Γ ((s : Fin (succ n)) β A s β r β€ s)
Ξ³ = Cases (d π)
(Ξ» aβ β π , aβ , Ξ» s a' β β¨β©)
(Ξ» (Ξ½ : Β¬ A π) β suc r , b , l Ξ½)
Fin-co-wf : {n : β} (A : Fin n β π€ Μ ) (rβ : Fin n)
β is-complemented A
β A rβ
β Ξ£ r κ Fin n , A r Γ ((s : Fin n) β A s β s β€ r)
Fin-co-wf {π€} {succ n} A π d a = Ξ³
where
Ξ΄ : is-decidable (Ξ£ i κ Fin n , A (suc i))
Ξ΄ = Fin-Compact (A β suc) (d β suc)
Ξ = Ξ£ r κ Fin (succ n) , A r Γ ((s : Fin (succ n)) β A s β s β€ r)
Ξ³ : Ξ
Ξ³ = Cases Ξ΄ f g
where
f : Ξ£ i κ Fin n , A (suc i) β Ξ
f (i , b) = suc r' , a' , h
where
IH : Ξ£ r' κ Fin n , A (suc r') Γ ((s' : Fin n) β A (suc s') β s' β€ r')
IH = Fin-co-wf {π€} {n} (A β suc) i (d β suc) b
r' : Fin n
r' = prβ IH
a' : A (suc r')
a' = prβ (prβ IH)
Ο : (s' : Fin n) β A (suc s') β s' β€ r'
Ο = prβ (prβ IH)
h : (s : Fin (succ n)) β A s β s β€ suc r'
h π c = β
h (suc x) c = Ο x c
g : Β¬ (Ξ£ i κ Fin n , A (suc i)) β Ξ
g Ξ½ = π , a , h
where
h : (s : Fin (succ n)) β A s β s β€ π
h (suc x) c = π-elim (Ξ½ (x , c))
h π c = β
Fin-co-wf {π€} {succ n} A (suc x) d a = suc (prβ IH) , prβ (prβ IH) , h
where
IH : Ξ£ r κ Fin n , A (suc r) Γ ((s : Fin n) β A (suc s) β s β€ r)
IH = Fin-co-wf {π€} {n} (A β suc) x (d β suc) a
h : (s : Fin (succ n)) β A s β s β€ suc (prβ IH)
h π b = β
h (suc x) b = prβ (prβ IH) x b
Fin-argmin : {a r : β} (p : Fin (succ a) β Fin r)
β Ξ£ x κ Fin (succ a) , ((y : Fin (succ a)) β p x β€ p y)
Fin-argmin {0} p = π , Ξ±
where
Ξ± : (y : Fin 1) β p π β€ p y
Ξ± π = β€-refl β¦ p π β§
Fin-argmin {succ a} p = Ξ³
where
IH : Ξ£ x κ Fin (succ a) , ((y : Fin (succ a)) β p (suc x) β€ p (suc y))
IH = Fin-argmin {a} (p β suc)
x = prβ IH
Ο = prβ IH
Ξ³ : Ξ£ x' κ Fin (succ (succ a)) , ((y : Fin (succ (succ a))) β p x' β€ p y)
Ξ³ = h (β€-decidable β¦ p π β§ β¦ p (suc x) β§)
where
h : is-decidable (p π β€ p (suc x)) β type-of Ξ³
h (inl l) = π , Ξ±
where
Ξ± : (y : (Fin (succ (succ a)))) β p π β€ p y
Ξ± π = β€-refl β¦ p π β§
Ξ± (suc y) = β€-trans β¦ p π β§ β¦ p (suc x) β§ β¦ p (suc y) β§ l (Ο y)
h (inr Ξ½) = suc x , Ξ±
where
Ξ± : (y : (Fin (succ (succ a)))) β p (suc x) β€ p y
Ξ± π = not-less-bigger-or-equal β¦ p (suc x) β§ β¦ p π β§
(contrapositive (<-coarser-than-β€ β¦ p π β§ β¦ p (suc x) β§) Ξ½)
Ξ± (suc y) = Ο y
argmin : {a r : β} β (Fin (succ a) β Fin r) β Fin (succ a)
argmin p = prβ (Fin-argmin p)
argmin-correct : {a r : β} (p : Fin (succ a) β Fin r)
β (y : Fin (succ a)) β p (argmin p) β€ p y
argmin-correct p = prβ (Fin-argmin p)
Fin-argmax : {a r : β} (p : Fin (succ a) β Fin r)
β Ξ£ x κ Fin (succ a) , ((y : Fin (succ a)) β p y β€ p x)
Fin-argmax {0} p = π , Ξ±
where
Ξ± : (y : Fin 1) β p y β€ p π
Ξ± π = β€-refl β¦ p π β§
Fin-argmax {succ a} p = Ξ³
where
IH : Ξ£ x κ Fin (succ a) , ((y : Fin (succ a)) β p (suc y) β€ p (suc x))
IH = Fin-argmax {a} (p β suc)
x = prβ IH
Ο = prβ IH
Ξ³ : Ξ£ x' κ Fin (succ (succ a)) , ((y : Fin (succ (succ a))) β p y β€ p x')
Ξ³ = h (β€-decidable β¦ p (suc x) β§ β¦ p π β§)
where
h : is-decidable (p (suc x) β€ p π) β type-of Ξ³
h (inl l) = π , Ξ±
where
Ξ± : (y : (Fin (succ (succ a)))) β p y β€ p π
Ξ± π = β€-refl β¦ p π β§
Ξ± (suc y) = β€-trans β¦ p (suc y) β§ β¦ p (suc x) β§ β¦ p π β§ (Ο y) l
h (inr Ξ½) = suc x , Ξ±
where
Ξ± : (y : (Fin (succ (succ a)))) β p y β€ p (suc x)
Ξ± π = not-less-bigger-or-equal β¦ p π β§ β¦ p (suc x) β§
(contrapositive (<-coarser-than-β€ β¦ p (suc x) β§ β¦ p π β§) Ξ½)
Ξ± (suc y) = Ο y
\end{code}
We could define argmin and argmax independently of their
specification, and then prove their specification:
\begin{code}
argmin' : {a r : β} β (Fin (succ a) β Fin r) β Fin (succ a)
argmin' {0} p = π
argmin' {succ a} p = Ξ³
where
m : Fin (succ a)
m = argmin' {a} (p β suc)
Ξ³ : Fin (succ (succ a))
Ξ³ = Cases (β€-decidable β¦ p π β§ β¦ p (suc m) β§)
(Ξ» (l : p π β€ p (suc m)) β π)
(Ξ» otherwise β suc m)
argmax' : {a r : β} β (Fin (succ a) β Fin r) β Fin (succ a)
argmax' {0} p = π
argmax' {succ a} p = Ξ³
where
m : Fin (succ a)
m = argmax' {a} (p β suc)
Ξ³ : Fin (succ (succ a))
Ξ³ = Cases (β€-decidable β¦ p π β§ β¦ p (suc m) β§)
(Ξ» (l : p π β€ p (suc m)) β suc m)
(Ξ» otherwise β π)
\end{code}
TODO. Complete the following.
\begin{code}
\end{code}
This version of argmin and argmax assumes a compact domain and a
finite type of outcomes.
\begin{code}
module ArgMinMax-Compact-Fin where
open import Fin.Order
open import Fin.Topology
open import Fin.Type
open import Notation.Order
open import NotionsOfDecidability.Complemented
open import TypeTopology.CompactTypes
open ArgMinMax-Fin
compact-argmax : {X : π€ Μ } {n : β} (p : X β Fin n)
β is-Compact X
β X
β Ξ£ x κ X , ((y : X) β p y β€ p x)
compact-argmax {π€} {X} {n} p ΞΊ xβ = II I
where
A : Fin n β π€ Μ
A r = Ξ£ x κ X , p x οΌ r
aβ : A (p xβ)
aβ = xβ , refl
Ξ΄ : is-complemented A
Ξ΄ r = ΞΊ (Ξ» x β p x οΌ r) (Ξ» x β Fin-is-discrete (p x) r)
I : Ξ£ r κ Fin n , A r Γ ((s : Fin n) β A s β s β€ r)
I = Fin-co-wf A (p xβ) Ξ΄ aβ
II : type-of I β Ξ£ x κ X , ((y : X) β p y β€ p x)
II (.(p y) , ((y , refl) , Ο)) = y , (Ξ» y β Ο (p y) (y , refl))
compact-argmin : {X : π€ Μ } {n : β} (p : X β Fin n)
β is-Compact X
β X
β Ξ£ x κ X , ((y : X) β p x β€ p y)
compact-argmin {π€} {X} {n} p ΞΊ xβ = II I
where
A : Fin n β π€ Μ
A r = Ξ£ x κ X , p x οΌ r
aβ : A (p xβ)
aβ = xβ , refl
Ξ΄ : is-complemented A
Ξ΄ r = ΞΊ (Ξ» x β p x οΌ r) (Ξ» x β Fin-is-discrete (p x) r)
I : Ξ£ r κ Fin n , A r Γ ((s : Fin n) β A s β r β€ s)
I = Fin-wf A (p xβ) Ξ΄ aβ
II : type-of I β Ξ£ x κ X , ((y : X) β p x β€ p y)
II (.(p y) , ((y , refl) , Ο)) = y , (Ξ» y β Ο (p y) (y , refl))
\end{code}
Added 11th September 2024. Simplified and more efficient version for
the boolean-valued case.
\begin{code}
module ArgMinMax-Fin-π where
open import Fin.Type
open import MLTT.Two-Properties
open import Naturals.Addition
Minβ : (i : β) β (Fin (i + 1) β π) β π
Minβ 0 p = p π
Minβ (succ i) p = minπ (p π) (Minβ i (p β suc))
Maxβ : (i : β) β (Fin (i + 1) β π) β π
Maxβ 0 p = p π
Maxβ (succ i) p = maxπ (p π) (Maxβ i (p β suc))
argMinβ : (i : β) β (Fin (i + 1) β π) β Fin (i + 1)
argMinβ 0 p = π
argMinβ (succ i) p =
π-equality-cases
(Ξ» (_ : p π οΌ β) β π)
(Ξ» (_ : p π οΌ β) β suc (argMinβ i (p β suc)))
argMaxβ : (i : β) β (Fin (i + 1) β π) β Fin (i + 1)
argMaxβ 0 p = π
argMaxβ (succ i) p =
π-equality-cases
(Ξ» (_ : p π οΌ β) β suc (argMaxβ i (p β suc)))
(Ξ» (_ : p π οΌ β) β π)
argMinβ-is-selection-for-Minβ : (i : β)
(p : Fin (i + 1) β π)
β p (argMinβ i p) οΌ Minβ i p
argMinβ-is-selection-for-Minβ 0 p = refl
argMinβ-is-selection-for-Minβ (succ i) p =
π-equality-cases
(Ξ» (e : p π οΌ β)
β p (argMinβ (succ i) p) οΌβ¨ ap p (π-equality-casesβ e) β©
p π οΌβ¨ e β©
β οΌβ¨reflβ©
minπ β (Minβ i (p β suc)) οΌβ¨ ap (Ξ» - β minπ - (Minβ i (p β suc))) (e β»ΒΉ) β©
minπ (p π) (Minβ i (p β suc)) οΌβ¨reflβ©
Minβ (succ i) p β)
(Ξ» (e : p π οΌ β)
β p (argMinβ (succ i) p) οΌβ¨ ap p (π-equality-casesβ e) β©
p (suc (argMinβ i (p β suc))) οΌβ¨ argMinβ-is-selection-for-Minβ i (p β suc) β©
Minβ i (p β suc) οΌβ¨reflβ©
minπ β (Minβ i (p β suc)) οΌβ¨ ap (Ξ» - β minπ - (Minβ i (p β suc))) (e β»ΒΉ) β©
minπ (p π) (Minβ i (p β suc)) οΌβ¨reflβ©
Minβ (succ i) p β)
argMaxβ-is-selection-for-Maxβ : (i : β)
(p : Fin (i + 1) β π)
β p (argMaxβ i p) οΌ Maxβ i p
argMaxβ-is-selection-for-Maxβ 0 p = refl
argMaxβ-is-selection-for-Maxβ (succ i) p =
π-equality-cases
(Ξ» (e : p π οΌ β)
β p (argMaxβ (succ i) p) οΌβ¨ ap p (π-equality-casesβ e) β©
p (suc (argMaxβ i (p β suc))) οΌβ¨ argMaxβ-is-selection-for-Maxβ i (p β suc) β©
Maxβ i (p β suc) οΌβ¨reflβ©
maxπ β (Maxβ i (p β suc)) οΌβ¨ ap (Ξ» - β maxπ - (Maxβ i (p β suc))) (e β»ΒΉ) β©
maxπ (p π) (Maxβ i (p β suc)) οΌβ¨reflβ©
Maxβ (succ i) p β)
(Ξ» (e : p π οΌ β)
β p (argMaxβ (succ i) p) οΌβ¨ ap p (π-equality-casesβ e) β©
p π οΌβ¨ e β©
β οΌβ¨reflβ©
maxπ β (Maxβ i (p β suc)) οΌβ¨ ap (Ξ» - β maxπ - (Maxβ i (p β suc))) (e β»ΒΉ) β©
maxπ (p π) (Maxβ i (p β suc)) οΌβ¨reflβ©
Maxβ (succ i) p β)
\end{code}
Added 3rd March 2026. Moved and refined from the alpha-beta file.
This version of argmin and argmax assumes a listed domain and
any type of outcomes that has a decidable order.
\begin{code}
module ArgMinMax-Listed
{π€ π₯ : Universe}
(R : π€ Μ )
(_<_ : R β R β π₯ Μ )
(Ξ΄ : (r s : R) β is-decidable (r < s))
where
open import MLTT.List
_β₯_ _β€_ : R β R β π₯ Μ
r β₯ s = Β¬ (r < s)
s β€ r = r β₯ s
private
min' max' : (r s : R) β is-decidable (r < s) β R
min' r s (inl lt) = r
min' r s (inr ge) = s
max' r s (inl lt) = s
max' r s (inr ge) = r
min max : R β R β R
min r s = min' r s (Ξ΄ r s)
max r s = max' r s (Ξ΄ r s)
open import MonadOnTypes.K
open K-definitions R
Min Max : {X : π€ Μ } β listedβΊ X β K X
Min (xβ , xs , _) p = foldr (Ξ» x β min (p x)) (p xβ) xs
Max (xβ , xs , _) p = foldr (Ξ» x β max (p x)) (p xβ) xs
private
argmin' argmax' : {X : π€ Μ } (p : X β R) (x y : X) β is-decidable (p x < p y) β X
argmin' p x y (inl le) = x
argmin' p x y (inr ge) = y
argmax' p x y (inl le) = y
argmax' p x y (inr ge) = x
argmin'-spec : {X : π€ Μ } (p : X β R) (x y : X) (d : is-decidable (p x < p y))
β p (argmin' p x y d) οΌ min' (p x) (p y) d
argmin'-spec p x y (inl lt) = refl
argmin'-spec p x y (inr ge) = refl
argmax'-spec : {X : π€ Μ } (p : X β R) (x y : X) (d : is-decidable (p x < p y))
β p (argmax' p x y d) οΌ max' (p x) (p y) d
argmax'-spec p x y (inl lt) = refl
argmax'-spec p x y (inr ge) = refl
argmin argmax : {X : π€ Μ } β (X β R) β X β X β X
argmin p x y = argmin' p x y (Ξ΄ (p x) (p y))
argmax p x y = argmax' p x y (Ξ΄ (p x) (p y))
argmin-spec : {X : π€ Μ } (p : X β R) (x y : X)
β p (argmin p x y) οΌ min (p x) (p y)
argmin-spec p x y = argmin'-spec p x y (Ξ΄ (p x) (p y))
argmax-spec : {X : π€ Μ } (p : X β R) (x y : X)
β p (argmax p x y) οΌ max (p x) (p y)
argmax-spec p x y = argmax'-spec p x y (Ξ΄ (p x) (p y))
open import MonadOnTypes.J
open J-definitions R
ArgMin ArgMax : {X : π€ Μ } β listedβΊ X β J X
ArgMin (xβ , xs , _) p = foldr (argmin p) xβ xs
ArgMax (xβ , xs , _) p = foldr (argmax p) xβ xs
open import MonadOnTypes.JK R
ArgMin-spec : {X : π€ Μ } (β : listedβΊ X) β (ArgMin β) attains (Min β)
ArgMin-spec {X} (xβ , xs , m) p = I xs
where
I : (xs : List X)
β p (foldr (argmin p) xβ xs) οΌ foldr (Ξ» x β min (p x)) (p xβ) xs
I [] = refl
I (x β· xs) = Iβ
where
IH : p (foldr (argmin p) xβ xs) οΌ foldr (Ξ» x β min (p x)) (p xβ) xs
IH = I xs
Iβ = p (argmin p x (foldr (argmin p) xβ xs)) οΌβ¨ Iβ β©
min (p x) (p (foldr (argmin p) xβ xs)) οΌβ¨ Iβ β©
min (p x) (foldr (Ξ» xβ β min (p xβ)) (p xβ) xs) β
where
Iβ = argmin-spec p x (foldr (argmin p) xβ xs)
Iβ = ap (min (p x)) IH
ArgMax-spec : {X : π€ Μ } (β : listedβΊ X) β (ArgMax β) attains (Max β)
ArgMax-spec {X} (xβ , xs , m) p = I xs
where
I : (xs : List X)
β p (foldr (argmax p) xβ xs) οΌ foldr (Ξ» x β max (p x)) (p xβ) xs
I [] = refl
I (x β· xs) = Iβ
where
IH : p (foldr (argmax p) xβ xs) οΌ foldr (Ξ» x β max (p x)) (p xβ) xs
IH = I xs
Iβ = p (argmax p x (foldr (argmax p) xβ xs)) οΌβ¨ Iβ β©
max (p x) (p (foldr (argmax p) xβ xs)) οΌβ¨ Iβ β©
max (p x) (foldr (Ξ» xβ β max (p xβ)) (p xβ) xs) β
where
Iβ = argmax-spec p x (foldr (argmax p) xβ xs)
Iβ = ap (max (p x)) IH
\end{code}