| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Decorated star-lists |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --with-K --safe #-} |
| 8 |
|
| 9 |
module Data.Star.Decoration where |
| 10 |
|
| 11 |
open import Data.Unit.Base using (⊤; tt) |
| 12 |
open import Function.Base using (flip) |
| 13 |
open import Level using (Level; suc; _⊔_) |
| 14 |
open import Relation.Binary.Core using (Rel; _=[_]⇒_; _⇒_) |
| 15 |
open import Relation.Binary.Definitions using (NonEmpty; nonEmpty) |
| 16 |
open import Relation.Binary.Construct.Closure.ReflexiveTransitive |
| 17 |
using (Star; ε; _◅_; gmap; map; _◅◅_; _▻▻_) |
| 18 |
|
| 19 |
-- A predicate on relation "edges" (think of the relation as a graph). |
| 20 |
|
| 21 |
EdgePred : {ℓ r : Level} (p : Level) {I : Set ℓ} → Rel I r → Set (suc p ⊔ ℓ ⊔ r) |
| 22 |
EdgePred p T = ∀ {i j} → T i j → Set p |
| 23 |
|
| 24 |
data NonEmptyEdgePred {ℓ r p : Level} {I : Set ℓ} (T : Rel I r) |
| 25 |
(P : EdgePred p T) : Set (ℓ ⊔ r ⊔ p) where |
| 26 |
nonEmptyEdgePred : ∀ {i j} {x : T i j} |
| 27 |
(p : P x) → NonEmptyEdgePred T P |
| 28 |
|
| 29 |
-- Decorating an edge with more information. |
| 30 |
|
| 31 |
data DecoratedWith {ℓ r p : Level} {I : Set ℓ} {T : Rel I r} (P : EdgePred p T) |
| 32 |
: Rel (NonEmpty (Star T)) (ℓ ⊔ r ⊔ p) where |
| 33 |
↦ : ∀ {i j k} {x : T i j} {xs : Star T j k} |
| 34 |
(p : P x) → DecoratedWith P (nonEmpty (x ◅ xs)) (nonEmpty xs) |
| 35 |
|
| 36 |
module _ {ℓ r p : Level} {I : Set ℓ} {T : Rel I r} {P : EdgePred p T} where |
| 37 |
|
| 38 |
edge : ∀ {i j} → DecoratedWith {T = T} P i j → NonEmpty T |
| 39 |
edge (↦ {x = x} p) = nonEmpty x |
| 40 |
|
| 41 |
decoration : ∀ {i j} → (d : DecoratedWith {T = T} P i j) → |
| 42 |
P (NonEmpty.proof (edge d)) |
| 43 |
decoration (↦ p) = p |
| 44 |
|
| 45 |
-- Star-lists decorated with extra information. All P xs means that |
| 46 |
-- all edges in xs satisfy P. |
| 47 |
|
| 48 |
All : ∀ {ℓ r p} {I : Set ℓ} {T : Rel I r} → EdgePred p T → EdgePred (ℓ ⊔ (r ⊔ p)) (Star T) |
| 49 |
All P {j = j} xs = |
| 50 |
Star (DecoratedWith P) (nonEmpty xs) (nonEmpty {y = j} ε) |
| 51 |
|
| 52 |
-- We can map over decorated vectors. |
| 53 |
|
| 54 |
gmapAll : ∀ {ℓ ℓ′ r p q} {I : Set ℓ} {T : Rel I r} {P : EdgePred p T} |
| 55 |
{J : Set ℓ′} {U : Rel J r} {Q : EdgePred q U} |
| 56 |
{i j} {xs : Star T i j} |
| 57 |
(f : I → J) (g : T =[ f ]⇒ U) → |
| 58 |
(∀ {i j} {x : T i j} → P x → Q (g x)) → |
| 59 |
All P xs → All {T = U} Q (gmap f g xs) |
| 60 |
gmapAll f g h ε = ε |
| 61 |
gmapAll f g h (↦ x ◅ xs) = ↦ (h x) ◅ gmapAll f g h xs |
| 62 |
|
| 63 |
-- Since we don't automatically have gmap id id xs ≡ xs it is easier |
| 64 |
-- to implement mapAll in terms of map than in terms of gmapAll. |
| 65 |
|
| 66 |
mapAll : ∀ {ℓ r p q} {I : Set ℓ} {T : Rel I r} |
| 67 |
{P : EdgePred p T} {Q : EdgePred q T} {i j} {xs : Star T i j} → |
| 68 |
(∀ {i j} {x : T i j} → P x → Q x) → |
| 69 |
All P xs → All Q xs |
| 70 |
mapAll {P = P} {Q} f ps = map F ps |
| 71 |
where |
| 72 |
F : DecoratedWith P ⇒ DecoratedWith Q |
| 73 |
F (↦ x) = ↦ (f x) |
| 74 |
|
| 75 |
-- We can decorate star-lists with universally true predicates. |
| 76 |
|
| 77 |
decorate : ∀ {ℓ r p} {I : Set ℓ} {T : Rel I r} {P : EdgePred p T} {i j} → |
| 78 |
(∀ {i j} (x : T i j) → P x) → |
| 79 |
(xs : Star T i j) → All P xs |
| 80 |
decorate f ε = ε |
| 81 |
decorate f (x ◅ xs) = ↦ (f x) ◅ decorate f xs |
| 82 |
|
| 83 |
-- We can append Alls. Unfortunately _◅◅_ does not quite work. |
| 84 |
|
| 85 |
infixr 5 _◅◅◅_ _▻▻▻_ |
| 86 |
|
| 87 |
_◅◅◅_ : ∀ {ℓ r p} {I : Set ℓ} {T : Rel I r} {P : EdgePred p T} |
| 88 |
{i j k} {xs : Star T i j} {ys : Star T j k} → |
| 89 |
All P xs → All P ys → All P (xs ◅◅ ys) |
| 90 |
ε ◅◅◅ ys = ys |
| 91 |
(↦ x ◅ xs) ◅◅◅ ys = ↦ x ◅ xs ◅◅◅ ys |
| 92 |
|
| 93 |
_▻▻▻_ : ∀ {ℓ r p} {I : Set ℓ} {T : Rel I r} {P : EdgePred p T} |
| 94 |
{i j k} {xs : Star T j k} {ys : Star T i j} → |
| 95 |
All P xs → All P ys → All P (xs ▻▻ ys) |
| 96 |
_▻▻▻_ = flip _◅◅◅_ |