| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Core lemmas needed to make list argmin/max functions work |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 8 |
|
| 9 |
open import Relation.Binary.Bundles using (TotalOrder; Setoid) |
| 10 |
|
| 11 |
module Data.List.Extrema.Core |
| 12 |
{b ℓ₁ ℓ₂} (totalOrder : TotalOrder b ℓ₁ ℓ₂) where |
| 13 |
|
| 14 |
open import Algebra.Construct.LiftedChoice |
| 15 |
using (Lift; sel-≡; preservesᵒ; preservesᵇ ; forcesᵇ) |
| 16 |
open import Algebra.Core using (Op₂) |
| 17 |
open import Algebra.Definitions using (Selective) |
| 18 |
import Algebra.Construct.NaturalChoice.Min as Min |
| 19 |
using (_⊓_; x⊓y≈y⇒y≤x; x⊓y≈x⇒x≤y; ⊓-sel; ⊓-isSelectiveMagma) |
| 20 |
import Algebra.Construct.NaturalChoice.Max as Max |
| 21 |
using (_⊔_; x⊔y≈y⇒x≤y; x⊔y≈x⇒y≤x; ⊔-sel; ⊔-isSelectiveMagma) |
| 22 |
open import Data.Product.Base using (_×_; _,_) |
| 23 |
open import Data.Sum.Base using (_⊎_; inj₁; inj₂) |
| 24 |
open import Level using (Level) |
| 25 |
open import Relation.Binary.PropositionalEquality.Core using (_≡_) |
| 26 |
open import Relation.Binary.Definitions using (Trans) |
| 27 |
|
| 28 |
|
| 29 |
open TotalOrder totalOrder renaming (Carrier to B) |
| 30 |
open import Relation.Binary.Construct.NonStrictToStrict _≈_ _≤_ |
| 31 |
using (_<_; <-≤-trans; ≤-<-trans) |
| 32 |
|
| 33 |
------------------------------------------------------------------------ |
| 34 |
-- Setup |
| 35 |
|
| 36 |
-- open NonStrictToStrict totalOrder using (_<_; ≤-<-trans; <-≤-trans) |
| 37 |
|
| 38 |
open Max totalOrder |
| 39 |
open Min totalOrder |
| 40 |
|
| 41 |
private |
| 42 |
|
| 43 |
variable |
| 44 |
a : Level |
| 45 |
A : Set a |
| 46 |
|
| 47 |
<-transʳ : Trans _≤_ _<_ _<_ |
| 48 |
<-transʳ = ≤-<-trans trans antisym ≤-respˡ-≈ |
| 49 |
|
| 50 |
<-transˡ : Trans _<_ _≤_ _<_ |
| 51 |
<-transˡ = <-≤-trans Eq.sym trans antisym ≤-respʳ-≈ |
| 52 |
|
| 53 |
module _ (f : A → B) where |
| 54 |
|
| 55 |
lemma₁ : ∀ {x y v} → f x ≤ v → f x ⊓ f y ≈ f y → f y ≤ v |
| 56 |
lemma₁ fx≤v fx⊓fy≈fy = trans (x⊓y≈y⇒y≤x fx⊓fy≈fy) fx≤v |
| 57 |
|
| 58 |
lemma₂ : ∀ {x y v} → f y ≤ v → f x ⊓ f y ≈ f x → f x ≤ v |
| 59 |
lemma₂ fy≤v fx⊓fy≈fx = trans (x⊓y≈x⇒x≤y fx⊓fy≈fx) fy≤v |
| 60 |
|
| 61 |
lemma₃ : ∀ {x y v} → f x < v → f x ⊓ f y ≈ f y → f y < v |
| 62 |
lemma₃ fx<v fx⊓fy≈fy = <-transʳ (x⊓y≈y⇒y≤x fx⊓fy≈fy) fx<v |
| 63 |
|
| 64 |
lemma₄ : ∀ {x y v} → f y < v → f x ⊓ f y ≈ f x → f x < v |
| 65 |
lemma₄ fx<v fx⊓fy≈fy = <-transʳ (x⊓y≈x⇒x≤y fx⊓fy≈fy) fx<v |
| 66 |
|
| 67 |
------------------------------------------------------------------------ |
| 68 |
-- Definition of lifted max and min |
| 69 |
|
| 70 |
⊓ᴸ : (A → B) → Op₂ A |
| 71 |
⊓ᴸ = Lift _≈_ _⊓_ ⊓-sel |
| 72 |
|
| 73 |
⊔ᴸ : (A → B) → Op₂ A |
| 74 |
⊔ᴸ = Lift _≈_ _⊔_ ⊔-sel |
| 75 |
|
| 76 |
------------------------------------------------------------------------ |
| 77 |
-- Properties of ⊓ᴸ |
| 78 |
|
| 79 |
⊓ᴸ-sel : ∀ f → Selective {A = A} _≡_ (⊓ᴸ f) |
| 80 |
⊓ᴸ-sel f = sel-≡ ⊓-isSelectiveMagma f |
| 81 |
|
| 82 |
⊓ᴸ-presᵒ-≤v : ∀ f {v} (x y : A) → f x ≤ v ⊎ f y ≤ v → f (⊓ᴸ f x y) ≤ v |
| 83 |
⊓ᴸ-presᵒ-≤v f = preservesᵒ ⊓-isSelectiveMagma f (lemma₁ f) (lemma₂ f) |
| 84 |
|
| 85 |
⊓ᴸ-presᵒ-<v : ∀ f {v} (x y : A) → f x < v ⊎ f y < v → f (⊓ᴸ f x y) < v |
| 86 |
⊓ᴸ-presᵒ-<v f = preservesᵒ ⊓-isSelectiveMagma f (lemma₃ f) (lemma₄ f) |
| 87 |
|
| 88 |
⊓ᴸ-presᵇ-v≤ : ∀ f {v} {x y : A} → v ≤ f x → v ≤ f y → v ≤ f (⊓ᴸ f x y) |
| 89 |
⊓ᴸ-presᵇ-v≤ f {v} = preservesᵇ ⊓-isSelectiveMagma {P = λ x → v ≤ f x} f |
| 90 |
|
| 91 |
⊓ᴸ-presᵇ-v< : ∀ f {v} {x y : A} → v < f x → v < f y → v < f (⊓ᴸ f x y) |
| 92 |
⊓ᴸ-presᵇ-v< f {v} = preservesᵇ ⊓-isSelectiveMagma {P = λ x → v < f x} f |
| 93 |
|
| 94 |
⊓ᴸ-forcesᵇ-v≤ : ∀ f {v} (x y : A) → v ≤ f (⊓ᴸ f x y) → v ≤ f x × v ≤ f y |
| 95 |
⊓ᴸ-forcesᵇ-v≤ f {v} = forcesᵇ ⊓-isSelectiveMagma f |
| 96 |
(λ v≤fx fx⊓fy≈fx → trans v≤fx (x⊓y≈x⇒x≤y fx⊓fy≈fx)) |
| 97 |
(λ v≤fy fx⊓fy≈fy → trans v≤fy (x⊓y≈y⇒y≤x fx⊓fy≈fy)) |
| 98 |
|
| 99 |
------------------------------------------------------------------------ |
| 100 |
-- Properties of ⊔ᴸ |
| 101 |
|
| 102 |
⊔ᴸ-sel : ∀ f → Selective {A = A} _≡_ (⊔ᴸ f) |
| 103 |
⊔ᴸ-sel f = sel-≡ ⊔-isSelectiveMagma f |
| 104 |
|
| 105 |
⊔ᴸ-presᵒ-v≤ : ∀ f {v} (x y : A) → v ≤ f x ⊎ v ≤ f y → v ≤ f (⊔ᴸ f x y) |
| 106 |
⊔ᴸ-presᵒ-v≤ f {v} = preservesᵒ ⊔-isSelectiveMagma f |
| 107 |
(λ v≤fx fx⊔fy≈fy → trans v≤fx (x⊔y≈y⇒x≤y fx⊔fy≈fy)) |
| 108 |
(λ v≤fy fx⊔fy≈fx → trans v≤fy (x⊔y≈x⇒y≤x fx⊔fy≈fx)) |
| 109 |
|
| 110 |
⊔ᴸ-presᵒ-v< : ∀ f {v} (x y : A) → v < f x ⊎ v < f y → v < f (⊔ᴸ f x y) |
| 111 |
⊔ᴸ-presᵒ-v< f {v} = preservesᵒ ⊔-isSelectiveMagma f |
| 112 |
(λ v<fx fx⊔fy≈fy → <-transˡ v<fx (x⊔y≈y⇒x≤y fx⊔fy≈fy)) |
| 113 |
(λ v<fy fx⊔fy≈fx → <-transˡ v<fy (x⊔y≈x⇒y≤x fx⊔fy≈fx)) |
| 114 |
|
| 115 |
⊔ᴸ-presᵇ-≤v : ∀ f {v} {x y : A} → f x ≤ v → f y ≤ v → f (⊔ᴸ f x y) ≤ v |
| 116 |
⊔ᴸ-presᵇ-≤v f {v} = preservesᵇ ⊔-isSelectiveMagma {P = λ x → f x ≤ v} f |
| 117 |
|
| 118 |
⊔ᴸ-presᵇ-<v : ∀ f {v} {x y : A} → f x < v → f y < v → f (⊔ᴸ f x y) < v |
| 119 |
⊔ᴸ-presᵇ-<v f {v} = preservesᵇ ⊔-isSelectiveMagma {P = λ x → f x < v} f |
| 120 |
|
| 121 |
⊔ᴸ-forcesᵇ-≤v : ∀ f {v} (x y : A) → f (⊔ᴸ f x y) ≤ v → f x ≤ v × f y ≤ v |
| 122 |
⊔ᴸ-forcesᵇ-≤v f {v} = forcesᵇ ⊔-isSelectiveMagma f |
| 123 |
(λ fx≤v fx⊔fy≈fx → trans (x⊔y≈x⇒y≤x fx⊔fy≈fx) fx≤v) |
| 124 |
(λ fy≤v fx⊔fy≈fy → trans (x⊔y≈y⇒x≤y fx⊔fy≈fy) fy≤v) |