aboutsummaryrefslogtreecommitdiff
path: root/Data/Hypergraph/Edge.agda
blob: 3c2a3a12e2092431475e1848d84f1a4f194227df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
{-# OPTIONS --without-K --safe #-}

open import Data.Hypergraph.Label using (HypergraphLabel)

module Data.Hypergraph.Edge (HL : HypergraphLabel) where


open import Relation.Binary using (Rel; IsStrictTotalOrder; Tri; Trichotomous; _Respects_)
open import Data.Castable using (IsCastable)
open import Data.Fin using (Fin)
open import Data.Fin.Show using () renaming (show to showFin)
open import Data.Nat.Base using (ℕ; _<_)
open import Data.Nat.Properties using (<-irrefl; <-trans; <-resp₂-≡; <-cmp)
open import Data.Product.Base using (_,_; proj₁; proj₂)
open import Data.String using (String; _<+>_)
open import Data.Vec.Relation.Binary.Pointwise.Inductive using (≡⇒Pointwise-≡; Pointwise-≡⇒≡)
open import Data.Vec.Show using () renaming (show to showVec)
open import Level using (0)
open import Relation.Binary.Bundles using (DecTotalOrder; StrictTotalOrder)
open import Relation.Binary.Structures using (IsEquivalence)
open import Relation.Nullary using (¬_)

import Data.Fin.Base as Fin
import Data.Fin.Properties as FinProp
import Data.Vec.Base as VecBase
import Data.Vec.Relation.Binary.Equality.Cast as VecCast
import Data.Vec.Relation.Binary.Lex.Strict as Lex
import Relation.Binary.PropositionalEquality as import Relation.Binary.Properties.DecTotalOrder as DTOP
import Relation.Binary.Properties.StrictTotalOrder as STOP

module HL = HypergraphLabel HL
open HL using (Label; cast; cast-is-id)
open VecBase using (Vec)

record Edge (v : ) : Set where
  field
    {arity} :     label : Label arity
    ports : Vec (Fin v) arity

open  using (_≡_)
open VecCast using (_≈[_]_)

record ≈-Edge {n : } (E E′ : Edge n) : Set where
  module E = Edge E
  module E = Edge E′
  field
    ≡arity : E.arity  E′.arity
    ≡label : cast ≡arity E.label  E′.label
    ≡ports : E.ports ≈[ ≡arity ] E′.ports

≈-Edge-refl : {v : } {x : Edge v}  ≈-Edge x x
≈-Edge-refl {_} {x} = record
    { ≡arity = ≡.refl
    ; ≡label = HL.≈-reflexive ≡.refl
    ; ≡ports = VecCast.≈-reflexive ≡.refl
    }
  where
    open Edge x using (arity; label)
    open DecTotalOrder (HL.decTotalOrder arity) using (module Eq)

≈-Edge-sym : {v : } {x y : Edge v}  ≈-Edge x y  ≈-Edge y x
≈-Edge-sym {_} {x} {y} x≈y = record
    { ≡arity = ≡.sym ≡arity
    ; ≡label = HL.≈-sym ≡label
    ; ≡ports = VecCast.≈-sym ≡ports
    }
  where
    open ≈-Edge x≈y
    open DecTotalOrder (HL.decTotalOrder E.arity) using (module Eq)

≈-Edge-trans : {v : } {i j k : Edge v}  ≈-Edge i j  ≈-Edge j k  ≈-Edge i k
≈-Edge-trans {_} {i} {j} {k} i≈j j≈k = record
    { ≡arity = ≡.trans i≈j.≡arity j≈k.≡arity
    ; ≡label = HL.≈-trans i≈j.≡label j≈k.≡label
    ; ≡ports = VecCast.≈-trans i≈j.≡ports j≈k.≡ports
    }
  where
    module i≈j = ≈-Edge i≈j
    module j≈k = ≈-Edge j≈k

open HL using (_[_<_])
_<<_ : {v a : }  Rel (Vec (Fin v) a) 0ℓ
_<<_ {v} = Lex.Lex-< _≡_ (Fin._<_ {v})
data <-Edge {v : } : Edge v  Edge v  Set where
  <-arity
      : {x y : Edge v}
       Edge.arity x < Edge.arity y
       <-Edge x y
  <-label
      : {x y : Edge v}
        (≡a : Edge.arity x  Edge.arity y)
       Edge.arity y [ cast ≡a (Edge.label x) < Edge.label y ]
       <-Edge x y
  <-ports
      : {x y : Edge v}
        (≡a : Edge.arity x  Edge.arity y)
        (≡l : Edge.label x HL.≈[ ≡a ] Edge.label y)
       VecBase.cast ≡a (Edge.ports x) << Edge.ports y
       <-Edge x y

<-Edge-irrefl : {v : } {x y : Edge v}  ≈-Edge x y  ¬ <-Edge x y
<-Edge-irrefl record { ≡arity = ≡a } (<-arity n<m) = <-irrefl ≡a n<m
<-Edge-irrefl record { ≡label = ≡l } (<-label _ (_ , x≉y)) = x≉y ≡l
<-Edge-irrefl record { ≡ports = ≡p } (<-ports ≡.refl ≡l x<y)
    = Lex.<-irrefl FinProp.<-irrefl (≡⇒Pointwise-≡ ≡p) x<y

<-Edge-trans : {v : } {i j k : Edge v}  <-Edge i j  <-Edge j k  <-Edge i k
<-Edge-trans (<-arity i<j) (<-arity j<k) = <-arity (<-trans i<j j<k)
<-Edge-trans (<-arity i<j) (<-label ≡.refl j<k) = <-arity i<j
<-Edge-trans (<-arity i<j) (<-ports ≡.refl _ j<k) = <-arity i<j
<-Edge-trans (<-label ≡.refl i<j) (<-arity j<k) = <-arity j<k
<-Edge-trans {_} {i} (<-label ≡.refl i<j) (<-label ≡.refl j<k)
    = <-label ≡.refl (<-label-trans i<j (<-respˡ-≈ (HL.≈-reflexive ≡.refl) j<k))
  where
    open DTOP (HL.decTotalOrder (Edge.arity i)) using (<-respˡ-≈) renaming (<-trans to <-label-trans)
<-Edge-trans {k = k} (<-label ≡.refl i<j) (<-ports ≡.refl ≡.refl _)
    = <-label ≡.refl (<-respʳ-≈ (≡.sym (HL.≈-reflexive ≡.refl)) i<j)
  where
    open DTOP (HL.decTotalOrder (Edge.arity k)) using (<-respʳ-≈)
<-Edge-trans (<-ports ≡.refl _ _) (<-arity j<k) = <-arity j<k
<-Edge-trans {k = k} (<-ports ≡.refl ≡.refl _) (<-label ≡.refl j<k)
    = <-label ≡.refl (<-respˡ-≈ (≡.cong (cast _) (HL.≈-reflexive ≡.refl)) j<k)
  where
    open DTOP (HL.decTotalOrder (Edge.arity k)) using (<-respˡ-≈)
<-Edge-trans {j = j} (<-ports ≡.refl ≡l₁ i<j) (<-ports ≡.refl ≡l₂ j<k)
  rewrite (VecCast.cast-is-id ≡.refl (Edge.ports j))
    = <-ports ≡.refl
        (HL.≈-trans ≡l₁ ≡l₂)
        (Lex.<-trans ≡-isPartialEquivalence FinProp.<-resp₂-≡ FinProp.<-trans i<j j<k)
  where
    open IsEquivalence ≡.isEquivalence using () renaming (isPartialEquivalence to ≡-isPartialEquivalence)

<-Edge-respˡ-≈ : {v : } {y : Edge v}  (λ x  <-Edge x y) Respects ≈-Edge
<-Edge-respˡ-≈ ≈x (<-arity x₁<y) = <-arity (proj₂ <-resp₂-≡ ≡arity x₁<y)
  where
    open ≈-Edge ≈x using (≡arity)
<-Edge-respˡ-≈ {_} {y} record { ≡arity = ≡.refl ; ≡label = ≡.refl } (<-label ≡.refl x₁<y)
    = <-label ≡.refl (<-respˡ-≈ (≡.sym (HL.≈-reflexive ≡.refl)) x₁<y)
  where
    module y = Edge y
    open DTOP (HL.decTotalOrder y.arity) using (<-respˡ-≈)
<-Edge-respˡ-≈ record { ≡arity = ≡.refl ; ≡label = ≡.refl; ≡ports = ≡.refl} (<-ports ≡.refl ≡.refl x₁<y)
    = <-ports
        ≡.refl
        (≡.cong (cast _) (HL.≈-reflexive ≡.refl))
        (Lex.<-respectsˡ
            ≡-isPartialEquivalence
            FinProp.<-respˡ-≡
            (≡⇒Pointwise-≡ (≡.sym (VecCast.≈-reflexive ≡.refl)))
            x₁<y)
  where
    open IsEquivalence ≡.isEquivalence using () renaming (isPartialEquivalence to ≡-isPartialEquivalence)

<-Edge-respʳ-≈ : {v : } {x : Edge v}  <-Edge x Respects ≈-Edge
<-Edge-respʳ-≈ record { ≡arity = ≡a } (<-arity x<y₁) = <-arity (proj₁ <-resp₂-≡ ≡a x<y₁)
<-Edge-respʳ-≈ {_} {x} record { ≡arity = ≡.refl ; ≡label = ≡.refl } (<-label ≡.refl x<y₁)
    = <-label ≡.refl (<-respʳ-≈ (≡.sym (HL.≈-reflexive ≡.refl)) x<y₁)
  where
    module x = Edge x
    open DTOP (HL.decTotalOrder x.arity) using (<-respʳ-≈)
<-Edge-respʳ-≈ record { ≡arity = ≡.refl ; ≡label = ≡.refl; ≡ports = ≡.refl} (<-ports ≡.refl ≡.refl x<y₁)
    = <-ports
        ≡.refl
        (≡.cong (cast _) (≡.sym (HL.≈-reflexive ≡.refl)))
        (Lex.<-respectsʳ
            ≡-isPartialEquivalence
            FinProp.<-respʳ-≡
            (≡⇒Pointwise-≡ (≡.sym (VecCast.≈-reflexive ≡.refl)))
            x<y₁)
  where
    open IsEquivalence ≡.isEquivalence using () renaming (isPartialEquivalence to ≡-isPartialEquivalence)

open Tri
open ≈-Edge
tri : {v : }  Trichotomous (≈-Edge {v}) (<-Edge {v})
tri x y with <-cmp x.arity y.arity
  where
    module x = Edge x
    module y = Edge y
tri x y | tri< x<y x≢y y≮x = tri< (<-arity x<y) (λ x≡y  x≢y (≡arity x≡y)) ¬y<x
  where
    ¬y<x :  ¬ <-Edge y x
    ¬y<x (<-arity y<x) = y≮x y<x
    ¬y<x (<-label ≡a _) = x≢y (≡.sym ≡a)
    ¬y<x (<-ports ≡a _ _) = x≢y (≡.sym ≡a)
tri x y | tri≈ x≮y ≡.refl y≮x = compare-label
  where
    module x = Edge x
    module y = Edge y
    open StrictTotalOrder (HL.strictTotalOrder x.arity) using (compare)
    import Relation.Binary.Properties.DecTotalOrder
    open DTOP (HL.decTotalOrder x.arity) using (<-respˡ-≈)
    compare-label : Tri (<-Edge x y) (≈-Edge x y) (<-Edge y x)
    compare-label with compare x.label y.label
    ... | tri< x<y x≢y y≮x′ = tri<
            (<-label ≡.refl (<-respˡ-≈ (≡.sym (HL.≈-reflexive ≡.refl)) x<y))
            (λ x≡y  x≢y (≡.trans (≡.sym (HL.≈-reflexive ≡.refl)) (≡label x≡y)))
            ¬y<x
      where
        ¬y<x :  ¬ <-Edge y x
        ¬y<x (<-arity y<x) = y≮x y<x
        ¬y<x (<-label _ y<x) = y≮x′ (<-respˡ-≈ (HL.≈-reflexive ≡.refl) y<x)
        ¬y<x (<-ports _ ≡l _) = x≢y (≡.trans (≡.sym ≡l) (cast-is-id ≡.refl y.label))
    ... | tri≈ x≮y′ x≡y′ y≮x′ = compare-ports
      where
        compare-ports : Tri (<-Edge x y) (≈-Edge x y) (<-Edge y x)
        compare-ports with Lex.<-cmp ≡.sym FinProp.<-cmp x.ports y.ports
        ... | tri< x<y x≢y y≮x″ =
                tri<
                  (<-ports ≡.refl
                    (HL.≈-reflexive x≡y′)
                    (Lex.<-respectsˡ
                      ≡-isPartialEquivalence
                      FinProp.<-respˡ-≡
                      (≡⇒Pointwise-≡ (≡.sym (VecCast.≈-reflexive ≡.refl)))
                      x<y))
                  (λ x≡y  x≢y (≡⇒Pointwise-≡ (≡.trans (≡.sym (VecCast.≈-reflexive ≡.refl)) (≡ports x≡y))))
                  ¬y<x
          where
            open IsEquivalence ≡.isEquivalence using () renaming (isPartialEquivalence to ≡-isPartialEquivalence)
            ¬y<x :  ¬ <-Edge y x
            ¬y<x (<-arity y<x) = y≮x y<x
            ¬y<x (<-label _ y<x) = y≮x′ (<-respˡ-≈ (HL.≈-reflexive ≡.refl) y<x)
            ¬y<x (<-ports _ _ y<x) =
                y≮x″
                  (Lex.<-respectsˡ
                    ≡-isPartialEquivalence
                    FinProp.<-respˡ-≡
                    (≡⇒Pointwise-≡ (VecCast.≈-reflexive ≡.refl))
                    y<x)
        ... | tri≈ x≮y″ x≡y″ y≮x″ = tri≈
                ¬x<y
                (record { ≡arity = ≡.refl ; ≡label = HL.≈-reflexive x≡y′ ; ≡ports = VecCast.≈-reflexive (Pointwise-≡⇒≡ x≡y″) })
                ¬y<x
          where
            open IsEquivalence ≡.isEquivalence using () renaming (isPartialEquivalence to ≡-isPartialEquivalence)
            ¬x<y : ¬ <-Edge x y
            ¬x<y (<-arity x<y) = x≮y x<y
            ¬x<y (<-label _ x<y) = x≮y′ (<-respˡ-≈ (HL.≈-reflexive ≡.refl) x<y)
            ¬x<y (<-ports _ _ x<y) =
                x≮y″
                  (Lex.<-respectsˡ
                    ≡-isPartialEquivalence
                    FinProp.<-respˡ-≡
                    (≡⇒Pointwise-≡ (VecCast.≈-reflexive ≡.refl))
                    x<y)
            ¬y<x : ¬ <-Edge y x
            ¬y<x (<-arity y<x) = y≮x y<x
            ¬y<x (<-label _ y<x) = y≮x′ (<-respˡ-≈ (HL.≈-reflexive ≡.refl) y<x)
            ¬y<x (<-ports _ _ y<x) =
                y≮x″
                  (Lex.<-respectsˡ
                    ≡-isPartialEquivalence
                    FinProp.<-respˡ-≡
                    (≡⇒Pointwise-≡ (VecCast.≈-reflexive ≡.refl))
                    y<x)

        ... | tri> x≮y″ x≢y y<x =
                tri>
                  ¬x<y
                  (λ x≡y  x≢y (≡⇒Pointwise-≡ (≡.trans (≡.sym (VecCast.≈-reflexive ≡.refl)) (≡ports x≡y))))
                  (<-ports
                    ≡.refl
                    (HL.≈-sym (HL.≈-reflexive x≡y′))
                    (Lex.<-respectsˡ
                      ≡-isPartialEquivalence
                      FinProp.<-respˡ-≡
                      (≡⇒Pointwise-≡ (≡.sym (VecCast.≈-reflexive ≡.refl)))
                      y<x))
          where
            open IsEquivalence ≡.isEquivalence using () renaming (isPartialEquivalence to ≡-isPartialEquivalence)
            ¬x<y : ¬ <-Edge x y
            ¬x<y (<-arity x<y) = x≮y x<y
            ¬x<y (<-label _ x<y) = x≮y′ (<-respˡ-≈ (HL.≈-reflexive ≡.refl) x<y)
            ¬x<y (<-ports _ _ x<y) =
                x≮y″
                  (Lex.<-respectsˡ
                    ≡-isPartialEquivalence
                    FinProp.<-respˡ-≡
                    (≡⇒Pointwise-≡ (VecCast.≈-reflexive ≡.refl))
                    x<y)
    ... | tri> x≮y′ x≢y y<x = tri>
            ¬x<y
            (λ x≡y  x≢y (≡.trans (≡.sym (HL.≈-reflexive ≡.refl)) (≡label x≡y)))
            (<-label ≡.refl (<-respˡ-≈ (≡.sym (HL.≈-reflexive ≡.refl)) y<x))
      where
        ¬x<y : ¬ <-Edge x y
        ¬x<y (<-arity x<y) = x≮y x<y
        ¬x<y (<-label ≡a x<y) = x≮y′ (<-respˡ-≈ (HL.≈-reflexive ≡.refl) x<y)
        ¬x<y (<-ports _ ≡l _) = x≢y (≡.trans (≡.sym (HL.≈-reflexive ≡.refl)) ≡l)
tri x y | tri> x≮y x≢y y<x = tri> ¬x<y (λ x≡y  x≢y (≡arity x≡y)) (<-arity y<x)
  where
    ¬x<y :  ¬ <-Edge x y
    ¬x<y (<-arity x<y) = x≮y x<y
    ¬x<y (<-label ≡a x<y) = x≢y ≡a
    ¬x<y (<-ports ≡a _ _) = x≢y ≡a

isStrictTotalOrder : {v : }  IsStrictTotalOrder (≈-Edge {v}) (<-Edge {v})
isStrictTotalOrder = record
    { isStrictPartialOrder = record
        { isEquivalence = record
            { refl = ≈-Edge-refl
            ; sym = ≈-Edge-sym
            ; trans = ≈-Edge-trans
            }
        ; irrefl = <-Edge-irrefl
        ; trans = <-Edge-trans
        ; <-resp-≈ = <-Edge-respʳ-≈ , <-Edge-respˡ-≈
        }
    ; compare = tri
    }

strictTotalOrder : {v : }  StrictTotalOrder 0 0 0ℓ
strictTotalOrder {v} = record
    { Carrier = Edge v
    ; _≈_ = ≈-Edge {v}
    ; _<_ = <-Edge {v}
    ; isStrictTotalOrder = isStrictTotalOrder {v}
    }

showEdge : {v : }  Edge v  String
showEdge record { arity = a ; label = l ; ports = p} = HL.showLabel a l <+> showVec showFin p

open module STOP {v} = STOP (strictTotalOrder {v}) using (decTotalOrder) public