~ubuntu-branches/ubuntu/maverick/agda-stdlib/maverick

« back to all changes in this revision

Viewing changes to src/Data/Star/Decoration.agda

  • Committer: Bazaar Package Importer
  • Author(s): Iain Lane
  • Date: 2010-01-08 23:35:09 UTC
  • Revision ID: james.westby@ubuntu.com-20100108233509-l6ejt9xji64xysfb
Import upstream version 0.3

Show diffs side-by-side

added added

removed removed

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