Debian Codegraph @

src/Data/Star/Pointer.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Pointers into star-lists
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --with-K --safe #-}
8
9 module Data.Star.Pointer {ℓ} {I : Set ℓ} where
10
11 open import Data.Maybe.Base using (Maybe; nothing; just)
12 open import Data.Star.Decoration
13 open import Data.Unit.Base using (tt)
14 open import Function.Base using (const; case_of_)
15 open import Level using (Level; _⊔_; lift)
16 open import Relation.Binary.Core using (Rel)
17 open import Relation.Binary.Definitions using (NonEmpty; nonEmpty)
18 open import Relation.Binary.Construct.Closure.ReflexiveTransitive
19 using (Star; ε; _◅_; gmap; map; _◅◅_; _▻▻_; _⋆)
20
21 private
22 variable
23 r p q : Level
24
25 -- Pointers into star-lists. The edge pointed to is decorated with Q,
26 -- while other edges are decorated with P.
27
28 data Pointer {T : Rel I r}
29 (P : EdgePred p T) (Q : EdgePred q T)
30 : Rel (Maybe (NonEmpty (Star T))) (ℓ ⊔ r ⊔ p ⊔ q) where
31 step : ∀ {i j k} {x : T i j} {xs : Star T j k}
32 (p : P x) → Pointer P Q (just (nonEmpty (x ◅ xs)))
33 (just (nonEmpty xs))
34 done : ∀ {i j k} {x : T i j} {xs : Star T j k}
35 (q : Q x) → Pointer P Q (just (nonEmpty (x ◅ xs))) nothing
36
37 -- Any P Q xs means that some edge in xs satisfies Q, while all
38 -- preceding edges satisfy P. A star-list of type Any Always Always xs
39 -- is basically a prefix of xs; the existence of such a prefix
40 -- guarantees that xs is non-empty.
41
42 Any : {T : Rel I r} (P : EdgePred p T) (Q : EdgePred q T) →
43 EdgePred (ℓ ⊔ (r ⊔ (p ⊔ q))) (Star T)
44 Any P Q xs = Star (Pointer P Q) (just (nonEmpty xs)) nothing
45
46 module _ {T : Rel I r} {P : EdgePred p T} {Q : EdgePred q T} where
47
48 this : ∀ {i j k} {x : T i j} {xs : Star T j k} →
49 Q x → Any P Q (x ◅ xs)
50 this q = done q ◅ ε
51
52 that : ∀ {i j k} {x : T i j} {xs : Star T j k} →
53 P x → Any P Q xs → Any P Q (x ◅ xs)
54 that p = _◅_ (step p)
55
56 -- Safe lookup.
57
58 data Result (T : Rel I r)
59 (P : EdgePred p T) (Q : EdgePred q T) : Set (ℓ ⊔ r ⊔ p ⊔ q) where
60 result : ∀ {i j} {x : T i j} (p : P x) (q : Q x) → Result T P Q
61
62 -- The first argument points out which edge to extract. The edge is
63 -- returned, together with proofs that it satisfies Q and R.
64
65 module _ {T : Rel I r} {P : EdgePred p T} {Q : EdgePred q T} where
66
67 lookup : ∀ {r} {R : EdgePred r T} {i j} {xs : Star T i j} →
68 All R xs → Any P Q xs → Result T Q R
69 lookup (↦ r ◅ _) (done q ◅ ε) = result q r
70 lookup (↦ _ ◅ rs) (step p ◅ ps) = lookup rs ps
71
72 -- We can define something resembling init.
73
74 prefixIndex : ∀ {i j} {xs : Star T i j} → Any P Q xs → I
75 prefixIndex (done {i = i} q ◅ _) = i
76 prefixIndex (step p ◅ ps) = prefixIndex ps
77
78 prefix : ∀ {i j} {xs : Star T i j} →
79 (ps : Any P Q xs) → Star T i (prefixIndex ps)
80 prefix (done q ◅ _) = ε
81 prefix (step {x = x} p ◅ ps) = x ◅ prefix ps
82
83 -- Here we are taking the initial segment of ps (all elements but the
84 -- last, i.e. all edges satisfying P).
85
86 init : ∀ {i j} {xs : Star T i j} →
87 (ps : Any P Q xs) → All P (prefix ps)
88 init (done q ◅ _) = ε
89 init (step p ◅ ps) = ↦ p ◅ init ps
90
91 -- One can simplify the implementation by not carrying around the
92 -- indices in the type:
93
94 last : ∀ {i j} {xs : Star T i j} →
95 Any P Q xs → NonEmptyEdgePred T Q
96 last ps with result q _ ← lookup {r = p} (decorate (const (lift tt)) _) ps =
97 nonEmptyEdgePred q