Debian Codegraph @

src/Relation/Unary/Closure/Base.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Closures of a unary relation with respect to a binary one.
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --cubical-compatible --safe #-}
8
9 open import Relation.Binary.Core using (Rel)
10
11 module Relation.Unary.Closure.Base {a b} {A : Set a} (R : Rel A b) where
12
13 open import Level using (_⊔_)
14 open import Data.Product.Base using (Σ-syntax; _×_; _,_; -,_)
15 open import Function.Base using (flip)
16 open import Relation.Binary.Definitions using (Reflexive; Transitive)
17 open import Relation.Unary using (Pred)
18
19 ------------------------------------------------------------------------
20 -- Definitions
21
22 -- Box
23
24 -- We start with the definition of □ ("box") which is named after the
25 -- box modality in modal logic. `□ T x` states that all the elements one
26 -- step away from `x` with respect to the relation R satisfy `T`.
27
28 □ : ∀ {t} → Pred A t → Pred A (a ⊔ b ⊔ t)
29 □ T x = ∀ {y} → R x y → T y
30
31 -- Use cases of □ include:
32 -- * The definition of the accessibility predicate corresponding to R:
33 -- data Acc (x : A) : Set (a ⊔ b) where
34 -- step : □ Acc x → Acc x
35
36 -- * The characterization of stability under weakening: picking R to be
37 -- `Data.List.Relation.Sublist.Inductive`, `∀ {Γ} → Tm Γ → □ T Γ`
38 -- corresponds to the fact that we have a notion of weakening for `Tm`.
39
40 -- Diamond
41
42 -- We then have the definition of ◇ ("diamond") which is named after the
43 -- diamond modality in modal logic. In modal logic, `◇ T x` states that
44 -- there exists an element one step away from x with respect to the
45 -- relation R that satisfies T. It is worth noting that the modal logic
46 -- metaphor breaks down here: this only is a closure operator if the
47 -- step we take is *backwards* with respect to R.
48
49 ◇ : ∀ {t} → Pred A t → Pred A (a ⊔ b ⊔ t)
50 ◇ T x = Σ[ support ∈ A ] (R support x × T support)
51
52 -- Use cases of ◇ include:
53 -- * The characterization of strengthening: picking R to be
54 -- `Data.List.Relation.Sublist.Inductive`, `∀ {Γ} → Tm Γ → ◇ Tm Γ`
55 -- is the type of a function strengthening a term to its support:
56 -- all the unused variables are discarded early on by the `related`
57 -- proof.
58 -- Cf. Conor McBride's "Everybody's got to be somewhere" for a more
59 -- detailed treatment of such an example.
60
61 -- Closed
62
63 -- Whenever we have a value in one context, we can get one in any
64 -- related context.
65
66 record Closed {t} (T : Pred A t) : Set (a ⊔ b ⊔ t) where
67 field next : ∀ {x} → T x → □ T x
68
69
70 ------------------------------------------------------------------------
71 -- Basic functions relating □ and ◇
72
73 module _ {t p} {T : Pred A t} {P : Pred A p} where
74
75 curry : (∀ {x} → ◇ T x → P x) → (∀ {x} → T x → □ P x)
76 curry f tx x∼y = f (-, x∼y , tx)
77
78 uncurry : (∀ {x} → T x → □ P x) → (∀ {x} → ◇ T x → P x)
79 uncurry f (_ , y∼x , ty) = f ty y∼x
80
81 ------------------------------------------------------------------------
82 -- Properties
83
84 module □ {t} {T : Pred A t} where
85
86 reindex : Transitive R → ∀ {x y} → R x y → □ T x → □ T y
87 reindex trans x∼y □Tx y∼z = □Tx (trans x∼y y∼z)
88
89 -- Provided that R is reflexive and Transitive, □ is a comonad
90 map : ∀ {u} {U : Pred A u} {x} → (∀ {x} → T x → U x) → □ T x → □ U x
91 map f □Tx x~y = f (□Tx x~y)
92
93 extract : Reflexive R → ∀ {x} → □ T x → T x
94 extract refl □Tx = □Tx refl
95
96 duplicate : Transitive R → ∀ {x} → □ T x → □ (□ T) x
97 duplicate trans □Tx x∼y y∼z = □Tx (trans x∼y y∼z)
98
99 -- Provided that R is transitive, □ is a closure operator
100 -- i.e. for any `T`, `□ T` is closed.
101 closed : Transitive R → Closed (□ T)
102 closed trans = record { next = duplicate trans }
103
104 module ◇ {t} {T : Pred A t} where
105
106 reindex : Transitive R → ∀ {x y} → R x y → ◇ T x → ◇ T y
107 reindex trans x∼y (z , z∼x , tz) = z , trans z∼x x∼y , tz
108
109 -- Provided that R is reflexive and Transitive, ◇ is a monad
110 map : ∀ {u} {U : Pred A u} {x} → (∀ {x} → T x → U x) → ◇ T x → ◇ U x
111 map f (y , y∼x , ty) = y , y∼x , f ty
112
113 pure : Reflexive R → ∀ {x} → T x → ◇ T x
114 pure refl tx = -, refl , tx
115
116 join : Transitive R → ∀ {x} → ◇ (◇ T) x → ◇ T x
117 join trans (_ , y∼x , _ , z∼y , tz) = _ , trans z∼y y∼x , tz
118
119 -- Provided that R is transitive, ◇ is a closure operator
120 -- i.e. for any `T`, `◇ T` is closed.
121 closed : Transitive R → Closed (◇ T)
122 closed trans = record { next = λ ◇Tx x∼y → reindex trans x∼y ◇Tx }
123
124 run : Closed T → ∀ {x} → ◇ T x → T x
125 run closed (_ , y∼x , ty) = Closed.next closed ty y∼x