Debian Codegraph @

src/Relation/Binary/Construct/Closure/Symmetric.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Symmetric closures of binary relations
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --cubical-compatible --safe #-}
8
9 module Relation.Binary.Construct.Closure.Symmetric where
10
11 open import Function.Base using (id; _on_)
12 open import Level using (Level)
13 open import Relation.Binary.Core using (Rel; _=[_]⇒_; _⇒_)
14 open import Relation.Binary.Definitions using (Symmetric)
15 import Relation.Binary.Construct.On as On
16
17 private
18 variable
19 a ℓ ℓ₁ ℓ₂ : Level
20 A B : Set a
21 R S : Rel A ℓ
22
23 ------------------------------------------------------------------------
24 -- Definition
25
26 data SymClosure {A : Set a} (R : Rel A ℓ) (a b : A) : Set ℓ where
27 fwd : R a b → SymClosure R a b
28 bwd : R b a → SymClosure R a b
29
30 ------------------------------------------------------------------------
31 -- Properties
32
33 -- Symmetric closures are symmetric.
34 symmetric : (R : Rel A ℓ) → Symmetric (SymClosure R)
35 symmetric _ (fwd aRb) = bwd aRb
36 symmetric _ (bwd bRa) = fwd bRa
37
38 ------------------------------------------------------------------------
39 -- Operations
40
41 -- A generalised variant of `map` which allows the index type to change.
42 gmap : (f : A → B) → R =[ f ]⇒ S → SymClosure R =[ f ]⇒ SymClosure S
43 gmap _ g (fwd aRb) = fwd (g aRb)
44 gmap _ g (bwd bRa) = bwd (g bRa)
45
46 map : R ⇒ S → SymClosure R ⇒ SymClosure S
47 map = gmap id
48
49 fold : Symmetric S → R ⇒ S → SymClosure R ⇒ S
50 fold S-sym R⇒S (fwd aRb) = R⇒S aRb
51 fold S-sym R⇒S (bwd bRa) = S-sym (R⇒S bRa)
52
53 -- A generalised variant of `fold`.
54 gfold : Symmetric S → (f : A → B) → R =[ f ]⇒ S → SymClosure R =[ f ]⇒ S
55 gfold {S = S} S-sym f R⇒S = fold (On.symmetric f S S-sym) R⇒S
56
57 -- `return` could also be called `singleton`.
58 return : R ⇒ SymClosure R
59 return = fwd
60
61 -- `join` could also be called `concat`.
62 join : SymClosure (SymClosure R) ⇒ SymClosure R
63 join = fold (symmetric _) id
64
65 infix 10 _⋆
66
67 _⋆ : R ⇒ SymClosure S → SymClosure R ⇒ SymClosure S
68 _⋆ f m = join (map f m)