Debian Codegraph @

src/Data/List/Fresh/Relation/Unary/Any.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Any predicate transformer for fresh lists
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --cubical-compatible --safe #-}
8
9 module Data.List.Fresh.Relation.Unary.Any where
10
11 open import Level using (Level; _⊔_; Lift)
12 open import Data.List.Fresh using (List#; []; cons; _∷#_; _#_)
13 open import Data.Product.Base using (∃; _,_; -,_)
14 open import Data.Sum.Base using (_⊎_; [_,_]′; inj₁; inj₂)
15 open import Function.Bundles using (_⇔_; mk⇔)
16 open import Level using (Level; _⊔_; Lift)
17 open import Relation.Nullary.Negation using (¬_; contradiction)
18 open import Relation.Nullary.Decidable as Dec using (Dec; no; _⊎-dec_)
19 open import Relation.Unary as U using (Pred; IUniversal; Universal; Decidable; _⇒_; _∪_; _∩_)
20 open import Relation.Binary.Core using (Rel)
21
22 private
23 variable
24 a p q r : Level
25 A : Set a
26
27 module _ {A : Set a} {R : Rel A r} (P : Pred A p) where
28
29 data Any : List# A R → Set (p ⊔ a ⊔ r) where
30 here : ∀ {x xs pr} → P x → Any (cons x xs pr)
31 there : ∀ {x xs pr} → Any xs → Any (cons x xs pr)
32
33 module _ {R : Rel A r} {P : Pred A p} {x} {xs : List# A R} {pr} where
34
35 head : ¬ Any P xs → Any P (cons x xs pr) → P x
36 head ¬tail (here p) = p
37 head ¬tail (there ps) = contradiction ps ¬tail
38
39 tail : ¬ P x → Any P (cons x xs pr) → Any P xs
40 tail ¬head (here p) = contradiction p ¬head
41 tail ¬head (there ps) = ps
42
43 toSum : Any P (cons x xs pr) → P x ⊎ Any P xs
44 toSum (here p) = inj₁ p
45 toSum (there ps) = inj₂ ps
46
47 fromSum : P x ⊎ Any P xs → Any P (cons x xs pr)
48 fromSum = [ here , there ]′
49
50 ⊎⇔Any : (P x ⊎ Any P xs) ⇔ Any P (cons x xs pr)
51 ⊎⇔Any = mk⇔ fromSum toSum
52
53 module _ {R : Rel A r} {P : Pred A p} {Q : Pred A q} where
54
55 map : {xs : List# A R} → ∀[ P ⇒ Q ] → Any P xs → Any Q xs
56 map p⇒q (here p) = here (p⇒q p)
57 map p⇒q (there p) = there (map p⇒q p)
58
59 module _ {R : Rel A r} {P : Pred A p} where
60
61 witness : {xs : List# A R} → Any P xs → ∃ P
62 witness (here p) = -, p
63 witness (there ps) = witness ps
64
65 remove : (xs : List# A R) → Any P xs → List# A R
66 remove-# : ∀ {x} {xs : List# A R} p → x # xs → x # (remove xs p)
67
68 remove (_ ∷# xs) (here _) = xs
69 remove (cons x xs pr) (there k) = cons x (remove xs k) (remove-# k pr)
70
71 remove-# (here x) (p , ps) = ps
72 remove-# (there k) (p , ps) = p , remove-# k ps
73
74 infixl 4 _─_
75 _─_ = remove
76
77 module _ {R : Rel A r} {P : Pred A p} (P? : Decidable P) where
78
79 any? : (xs : List# A R) → Dec (Any P xs)
80 any? [] = no (λ ())
81 any? (x ∷# xs) = Dec.map ⊎⇔Any (P? x ⊎-dec any? xs)