| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- First generalizes the idea that an element is the first in a list to |
| 5 |
-- satisfy a predicate. |
| 6 |
------------------------------------------------------------------------ |
| 7 |
|
| 8 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 9 |
|
| 10 |
module Data.List.Relation.Unary.First {a} {A : Set a} where |
| 11 |
|
| 12 |
open import Level using (_⊔_) |
| 13 |
open import Data.Fin.Base as Fin using (Fin; zero; suc) |
| 14 |
open import Data.List.Base as List using (List; []; _∷_) |
| 15 |
open import Data.List.Relation.Unary.All as All using (All; []; _∷_) |
| 16 |
open import Data.List.Relation.Unary.Any as Any using (Any; here; there) |
| 17 |
open import Data.Product.Base using (∃; -,_; _,_) |
| 18 |
open import Data.Sum.Base as Sum using (_⊎_; inj₁; inj₂) |
| 19 |
open import Function.Base using (id; _∘′_) |
| 20 |
open import Relation.Unary |
| 21 |
open import Relation.Nullary.Negation.Core using (¬_; contradiction) |
| 22 |
|
| 23 |
------------------------------------------------------------------------ |
| 24 |
-- Basic type. |
| 25 |
|
| 26 |
module _ {p q} (P : Pred A p) (Q : Pred A q) where |
| 27 |
|
| 28 |
infix 1 _++_∷_ |
| 29 |
infixr 5 _∷_ |
| 30 |
|
| 31 |
data First : Pred (List A) (a ⊔ p ⊔ q) where |
| 32 |
[_] : ∀ {x xs} → Q x → First (x ∷ xs) |
| 33 |
_∷_ : ∀ {x xs} → P x → First xs → First (x ∷ xs) |
| 34 |
|
| 35 |
data FirstView : Pred (List A) (a ⊔ p ⊔ q) where |
| 36 |
_++_∷_ : ∀ {xs y} → All P xs → Q y → ∀ ys → FirstView (xs List.++ y ∷ ys) |
| 37 |
|
| 38 |
------------------------------------------------------------------------ |
| 39 |
-- map |
| 40 |
|
| 41 |
module _ {p q r s} {P : Pred A p} {Q : Pred A q} {R : Pred A r} {S : Pred A s} where |
| 42 |
|
| 43 |
map : P ⊆ R → Q ⊆ S → First P Q ⊆ First R S |
| 44 |
map p⇒r q⇒r [ qx ] = [ q⇒r qx ] |
| 45 |
map p⇒r q⇒r (px ∷ pqxs) = p⇒r px ∷ map p⇒r q⇒r pqxs |
| 46 |
|
| 47 |
module _ {p q r} {P : Pred A p} {Q : Pred A q} {R : Pred A r} where |
| 48 |
|
| 49 |
map₁ : P ⊆ R → First P Q ⊆ First R Q |
| 50 |
map₁ p⇒r = map p⇒r id |
| 51 |
|
| 52 |
map₂ : Q ⊆ R → First P Q ⊆ First P R |
| 53 |
map₂ = map id |
| 54 |
|
| 55 |
refine : P ⊆ Q ∪ R → First P Q ⊆ First R Q |
| 56 |
refine f [ qx ] = [ qx ] |
| 57 |
refine f (px ∷ pqxs) with f px |
| 58 |
... | inj₁ qx = [ qx ] |
| 59 |
... | inj₂ rx = rx ∷ refine f pqxs |
| 60 |
|
| 61 |
module _ {p q} {P : Pred A p} {Q : Pred A q} where |
| 62 |
|
| 63 |
------------------------------------------------------------------------ |
| 64 |
-- Operations |
| 65 |
|
| 66 |
empty : ¬ First P Q [] |
| 67 |
empty () |
| 68 |
|
| 69 |
tail : ∀ {x xs} → ¬ Q x → First P Q (x ∷ xs) → First P Q xs |
| 70 |
tail ¬qx [ qx ] = contradiction qx ¬qx |
| 71 |
tail ¬qx (px ∷ pqxs) = pqxs |
| 72 |
|
| 73 |
index : First P Q ⊆ (Fin ∘′ List.length) |
| 74 |
index [ qx ] = zero |
| 75 |
index (_ ∷ pqxs) = suc (index pqxs) |
| 76 |
|
| 77 |
index-satisfied : ∀ {xs} (pqxs : First P Q xs) → Q (List.lookup xs (index pqxs)) |
| 78 |
index-satisfied [ qx ] = qx |
| 79 |
index-satisfied (_ ∷ pqxs) = index-satisfied pqxs |
| 80 |
|
| 81 |
satisfied : ∀ {xs} → First P Q xs → ∃ Q |
| 82 |
satisfied pqxs = -, index-satisfied pqxs |
| 83 |
|
| 84 |
satisfiable : Satisfiable Q → Satisfiable (First P Q) |
| 85 |
satisfiable (x , qx) = List.[ x ] , [ qx ] |
| 86 |
|
| 87 |
------------------------------------------------------------------------ |
| 88 |
-- Decidability results |
| 89 |
|
| 90 |
first : Π[ P ∪ Q ] → Π[ First P Q ∪ All P ] |
| 91 |
first p⊎q [] = inj₂ [] |
| 92 |
first p⊎q (x ∷ xs) with p⊎q x |
| 93 |
... | inj₁ px = Sum.map (px ∷_) (px ∷_) (first p⊎q xs) |
| 94 |
... | inj₂ qx = inj₁ [ qx ] |
| 95 |
|
| 96 |
------------------------------------------------------------------------ |
| 97 |
-- Relationship with Any |
| 98 |
|
| 99 |
module _ {q} {Q : Pred A q} where |
| 100 |
|
| 101 |
fromAny : Any Q ⊆ First U Q |
| 102 |
fromAny (here qx) = [ qx ] |
| 103 |
fromAny (there any) = _ ∷ fromAny any |
| 104 |
|
| 105 |
toAny : ∀ {p} {P : Pred A p} → First P Q ⊆ Any Q |
| 106 |
toAny [ qx ] = here qx |
| 107 |
toAny (_ ∷ pqxs) = there (toAny pqxs) |