Debian Codegraph @

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

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