| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Properties of First |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 8 |
|
| 9 |
module Data.List.Relation.Unary.First.Properties where |
| 10 |
|
| 11 |
open import Data.Bool.Base using (true; false) |
| 12 |
open import Data.Fin.Base using (suc) |
| 13 |
open import Data.List.Base as List using (List; []; _∷_) |
| 14 |
open import Data.List.Relation.Unary.All as All using (All; []; _∷_) |
| 15 |
open import Data.List.Relation.Unary.Any as Any using (here; there) |
| 16 |
open import Data.List.Relation.Unary.First |
| 17 |
import Data.Sum.Base as Sum |
| 18 |
open import Function.Base using (_∘′_; _∘_; id) |
| 19 |
open import Relation.Binary.PropositionalEquality.Core as ≡ using (_≡_; refl; _≗_) |
| 20 |
open import Relation.Nullary.Decidable.Core as Dec |
| 21 |
open import Relation.Nullary.Negation.Core using (contradiction) |
| 22 |
open import Relation.Nullary.Reflects using (invert) |
| 23 |
open import Relation.Unary using (Pred; _⊆_; ∁; Irrelevant; Decidable) |
| 24 |
|
| 25 |
------------------------------------------------------------------------ |
| 26 |
-- map |
| 27 |
|
| 28 |
module _ {a b p q} {A : Set a} {B : Set b} {P : Pred B p} {Q : Pred B q} where |
| 29 |
|
| 30 |
map⁺ : {f : A → B} → First (P ∘′ f) (Q ∘′ f) ⊆ First P Q ∘′ List.map f |
| 31 |
map⁺ [ qfx ] = [ qfx ] |
| 32 |
map⁺ (pfxs ∷ pqfxs) = pfxs ∷ map⁺ pqfxs |
| 33 |
|
| 34 |
map⁻ : {f : A → B} → First P Q ∘′ List.map f ⊆ First (P ∘′ f) (Q ∘′ f) |
| 35 |
map⁻ {f} {x ∷ xs} [ qfx ] = [ qfx ] |
| 36 |
map⁻ {f} {x ∷ xs} (pfx ∷ pqfxs) = pfx ∷ map⁻ pqfxs |
| 37 |
|
| 38 |
------------------------------------------------------------------------ |
| 39 |
-- (++) |
| 40 |
|
| 41 |
module _ {a p q} {A : Set a} {P : Pred A p} {Q : Pred A q} where |
| 42 |
|
| 43 |
++⁺ : ∀ {xs ys} → All P xs → First P Q ys → First P Q (xs List.++ ys) |
| 44 |
++⁺ [] pqys = pqys |
| 45 |
++⁺ (px ∷ pxs) pqys = px ∷ ++⁺ pxs pqys |
| 46 |
|
| 47 |
⁺++ : ∀ {xs} → First P Q xs → ∀ ys → First P Q (xs List.++ ys) |
| 48 |
⁺++ [ qx ] ys = [ qx ] |
| 49 |
⁺++ (px ∷ pqxs) ys = px ∷ ⁺++ pqxs ys |
| 50 |
|
| 51 |
------------------------------------------------------------------------ |
| 52 |
-- Relationship to All |
| 53 |
|
| 54 |
module _ {a p q} {A : Set a} {P : Pred A p} {Q : Pred A q} where |
| 55 |
|
| 56 |
All⇒¬First : P ⊆ ∁ Q → All P ⊆ ∁ (First P Q) |
| 57 |
All⇒¬First p⇒¬q (px ∷ pxs) [ qx ] = contradiction qx (p⇒¬q px) |
| 58 |
All⇒¬First p⇒¬q (_ ∷ pxs) (_ ∷ hf) = All⇒¬First p⇒¬q pxs hf |
| 59 |
|
| 60 |
First⇒¬All : Q ⊆ ∁ P → First P Q ⊆ ∁ (All P) |
| 61 |
First⇒¬All q⇒¬p [ qx ] (px ∷ pxs) = q⇒¬p qx px |
| 62 |
First⇒¬All q⇒¬p (_ ∷ pqxs) (_ ∷ pxs) = First⇒¬All q⇒¬p pqxs pxs |
| 63 |
|
| 64 |
¬First⇒All : ∁ Q ⊆ P → ∁ (First P Q) ⊆ All P |
| 65 |
¬First⇒All ¬q⇒p {[]} _ = [] |
| 66 |
¬First⇒All ¬q⇒p {x ∷ xs} ¬pqxxs = |
| 67 |
let px = ¬q⇒p (¬pqxxs ∘ [_]) in |
| 68 |
px ∷ ¬First⇒All ¬q⇒p (¬pqxxs ∘ (px ∷_)) |
| 69 |
|
| 70 |
¬All⇒First : Decidable P → ∁ P ⊆ Q → ∁ (All P) ⊆ First P Q |
| 71 |
¬All⇒First P? ¬p⇒q {x = []} ¬⊤ = contradiction [] ¬⊤ |
| 72 |
¬All⇒First P? ¬p⇒q {x = x ∷ xs} ¬∀ with P? x |
| 73 |
... | true because [px] = let px = invert [px] in |
| 74 |
px ∷ ¬All⇒First P? ¬p⇒q (¬∀ ∘ (px ∷_)) |
| 75 |
... | false because [¬px] = [ ¬p⇒q (invert [¬px]) ] |
| 76 |
|
| 77 |
------------------------------------------------------------------------ |
| 78 |
-- Irrelevance |
| 79 |
|
| 80 |
unique-index : ∀ {xs} → P ⊆ ∁ Q → (f₁ f₂ : First P Q xs) → index f₁ ≡ index f₂ |
| 81 |
unique-index p⇒¬q [ _ ] [ _ ] = refl |
| 82 |
unique-index p⇒¬q [ qx ] (px ∷ _) = contradiction qx (p⇒¬q px) |
| 83 |
unique-index p⇒¬q (px ∷ _) [ qx ] = contradiction qx (p⇒¬q px) |
| 84 |
unique-index p⇒¬q (_ ∷ f₁) (_ ∷ f₂) = ≡.cong suc (unique-index p⇒¬q f₁ f₂) |
| 85 |
|
| 86 |
irrelevant : P ⊆ ∁ Q → Irrelevant P → Irrelevant Q → Irrelevant (First P Q) |
| 87 |
irrelevant p⇒¬q p-irr q-irr [ px ] [ qx ] = ≡.cong [_] (q-irr px qx) |
| 88 |
irrelevant p⇒¬q p-irr q-irr [ qx ] (px ∷ _) = contradiction qx (p⇒¬q px) |
| 89 |
irrelevant p⇒¬q p-irr q-irr (px ∷ _) [ qx ] = contradiction qx (p⇒¬q px) |
| 90 |
irrelevant p⇒¬q p-irr q-irr (px ∷ f) (qx ∷ g) = |
| 91 |
≡.cong₂ _∷_ (p-irr px qx) (irrelevant p⇒¬q p-irr q-irr f g) |
| 92 |
|
| 93 |
------------------------------------------------------------------------ |
| 94 |
-- Decidability |
| 95 |
|
| 96 |
module _ {a p} {A : Set a} {P : Pred A p} where |
| 97 |
|
| 98 |
first? : Decidable P → Decidable (First P (∁ P)) |
| 99 |
first? P? = Dec.fromSum |
| 100 |
∘ Sum.map₂ (All⇒¬First contradiction) |
| 101 |
∘ first (Dec.toSum ∘ P?) |
| 102 |
|
| 103 |
cofirst? : Decidable P → Decidable (First (∁ P) P) |
| 104 |
cofirst? P? = Dec.fromSum |
| 105 |
∘ Sum.map₂ (All⇒¬First id) |
| 106 |
∘ first (Sum.swap ∘ Dec.toSum ∘ P?) |
| 107 |
|
| 108 |
------------------------------------------------------------------------ |
| 109 |
-- Conversion to Any |
| 110 |
|
| 111 |
module _ {a p} {A : Set a} {P : Pred A p} where |
| 112 |
|
| 113 |
fromAny∘toAny≗id : ∀ {xs} → fromAny {Q = P} {x = xs} ∘′ toAny ≗ id |
| 114 |
fromAny∘toAny≗id [ qx ] = refl |
| 115 |
fromAny∘toAny≗id (px ∷ pqxs) = ≡.cong (_ ∷_) (fromAny∘toAny≗id pqxs) |
| 116 |
|
| 117 |
toAny∘fromAny≗id : ∀ {xs} → toAny {Q = P} ∘′ fromAny {x = xs} ≗ id |
| 118 |
toAny∘fromAny≗id (here px) = refl |
| 119 |
toAny∘fromAny≗id (there v) = ≡.cong there (toAny∘fromAny≗id v) |
| 120 |
|
| 121 |
------------------------------------------------------------------------ |
| 122 |
-- Equivalence between the inductive definition and the view |
| 123 |
|
| 124 |
module _ {a p q} {A : Set a} {P : Pred A p} {Q : Pred A q} where |
| 125 |
|
| 126 |
toView : ∀ {as} → First P Q as → FirstView P Q as |
| 127 |
toView [ qx ] = [] ++ qx ∷ _ |
| 128 |
toView (px ∷ pqxs) with toView pqxs |
| 129 |
... | pxs ++ qy ∷ ys = (px ∷ pxs) ++ qy ∷ ys |
| 130 |
|
| 131 |
fromView : ∀ {as} → FirstView P Q as → First P Q as |
| 132 |
fromView (pxs ++ qy ∷ ys) = ++⁺ pxs [ qy ] |