Debian Codegraph @

src/Relation/Nary.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Heterogeneous N-ary Relations
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --cubical-compatible --safe #-}
8
9 module Relation.Nary where
10
11 ------------------------------------------------------------------------
12 -- Concrete examples can be found in README.Nary. This file's comments
13 -- are more focused on the implementation details and the motivations
14 -- behind the design decisions.
15 ------------------------------------------------------------------------
16
17 open import Data.Unit.Base using (⊤)
18 open import Data.Bool.Base using (true; false)
19 open import Data.Empty using (⊥; ⊥-elim)
20 open import Data.Nat.Base using (zero; suc)
21 open import Data.Product.Base as Product using (_×_; _,_)
22 open import Data.Product.Nary.NonDependent
23 open import Data.Sum.Base using (_⊎_)
24 open import Function.Base using (_$_; _∘′_)
25 open import Function.Nary.NonDependent
26 open import Level using (Level; _⊔_; Lift)
27 open import Relation.Binary.PropositionalEquality.Core using (_≡_; cong; subst)
28 open import Relation.Nullary.Negation.Core using (¬_)
29 open import Relation.Nullary.Decidable.Core as Dec
30 using (Dec; yes; no; _because_; _×-dec_)
31 import Relation.Unary as Unary
32 using (Pred; Satisfiable; Universal; IUniversal)
33
34 private
35 variable
36 r : Level
37 R : Set r
38
39 ------------------------------------------------------------------------
40 -- Generic type constructors
41
42 -- `Relation.Unary` provides users with a wealth of combinators to work
43 -- with indexed sets. We can generalise these to n-ary relations.
44
45 -- The crucial thing to notice here is that because we are explicitly
46 -- considering that the input function should be a `Set`-ended `Arrows`,
47 -- all the other parameters are inferrable. This allows us to make the
48 -- number arguments (`n`) implicit.
49 ------------------------------------------------------------------------
50
51 ------------------------------------------------------------------------
52 -- Quantifiers
53
54 -- If we already know how to quantify over one variable, we can easily
55 -- describe how to quantify over `n` variables by induction over said `n`.
56
57 quantₙ : (∀ {i l} {I : Set i} → (I → Set l) → Set (i ⊔ l)) →
58 ∀ n {ls} {as : Sets n ls} →
59 Arrows n as (Set r) → Set (r ⊔ (⨆ n ls))
60 quantₙ Q zero f = f
61 quantₙ Q (suc n) f = Q (λ x → quantₙ Q n (f x))
62
63 infix 5 ∃⟨_⟩ Π[_] ∀[_]
64
65 -- existential quantifier
66
67 ∃⟨_⟩ : ∀ {n ls r} {as : Sets n ls} → as ⇉ Set r → Set (r ⊔ (⨆ n ls))
68 ∃⟨_⟩ = quantₙ Unary.Satisfiable _
69
70 -- explicit universal quantifiers
71
72 Π[_] : ∀ {n ls r} {as : Sets n ls} → as ⇉ Set r → Set (r ⊔ (⨆ n ls))
73 Π[_] = quantₙ Unary.Universal _
74
75 -- implicit universal quantifiers
76
77 ∀[_] : ∀ {n ls r} {as : Sets n ls} → as ⇉ Set r → Set (r ⊔ (⨆ n ls))
78 ∀[_] = quantₙ Unary.IUniversal _
79
80 -- ≟-mapₙ : ∀ n. (con : A₁ → ⋯ → Aₙ → R) →
81 -- Injectiveₙ n con →
82 -- ∀ a₁₁ a₁₂ ⋯ aₙ₁ aₙ₂ →
83 -- Dec (a₁₁ ≡ a₁₂) → ⋯ → Dec (aₙ₁ ≡ aₙ₂) →
84 -- Dec (con a₁₁ ⋯ aₙ₁ ≡ con a₁₂ ⋯ aₙ₂)
85
86 ≟-mapₙ : ∀ n {ls} {as : Sets n ls} (con : Arrows n as R) → Injectiveₙ n con →
87 ∀ {l r} → Arrows n (Dec <$> Equalₙ n l r) (Dec (uncurryₙ n con l ≡ uncurryₙ n con r))
88 ≟-mapₙ n con con-inj =
89 curryₙ n λ a?s → let as? = Product-dec n a?s in
90 Dec.map′ (cong (uncurryₙ n con) ∘′ fromEqualₙ n) con-inj as?
91
92 ------------------------------------------------------------------------
93 -- Substitution
94
95 module _ {n r ls} {as : Sets n ls} (P : as ⇉ Set r) where
96
97 -- Substitutionₙ : ∀ n. ∀ a₁₁ a₁₂ ⋯ aₙ₁ aₙ₂ →
98 -- a₁₁ ≡ a₁₂ → ⋯ → aₙ₁ ≡ aₙ₂ →
99 -- P a₁₁ ⋯ aₙ₁ → P a₁₂ ⋯ aₙ₂
100
101 Substitutionₙ : Set (r ⊔ (⨆ n ls))
102 Substitutionₙ = ∀ {l r} → Equalₙ n l r ⇉ (uncurryₙ n P l → uncurryₙ n P r)
103
104 substₙ : Substitutionₙ
105 substₙ = curryₙ n (subst (uncurryₙ n P) ∘′ fromEqualₙ n)
106
107 ------------------------------------------------------------------------
108 -- Pointwise liftings of k-ary operators
109
110 -- Rather than having multiple ad-hoc lifting functions for various
111 -- arities we have a fully generic liftₙ functional which lifts a k-ary
112 -- operator to work with k n-ary functions whose respective codomains
113 -- match the domains of the operator.
114 -- The type of liftₙ is fairly unreadable. Here it is written with ellipsis:
115
116 -- liftₙ : ∀ k n. (B₁ → ⋯ → Bₖ → R) →
117 -- (A₁ → ⋯ → Aₙ → B₁) →
118 -- ⋮
119 -- (A₁ → ⋯ → Aₙ → B₁) →
120 -- (A₁ → ⋯ → Aₙ → R)
121
122 liftₙ : ∀ k n {ls rs} {as : Sets n ls} {bs : Sets k rs} →
123 Arrows k bs R → Arrows k (smap _ (Arrows n as) k bs) (Arrows n as R)
124 liftₙ k n op = curry⊤ₙ k λ fs →
125 curry⊤ₙ n λ vs →
126 uncurry⊤ₙ k op $
127 palg _ _ k (λ f → uncurry⊤ₙ n f vs) fs where
128
129 -- The bulk of the work happens in this auxiliary definition:
130 palg : ∀ f (F : ∀ {l} → Set l → Set (f l)) n {ls} {as : Sets n ls} →
131 (∀ {l} {r : Set l} → F r → r) → Product⊤ n (smap f F n as) → Product⊤ n as
132 palg f F zero alg ps = _
133 palg f F (suc n) alg (p , ps) = alg p , palg f F n alg ps
134
135
136 -- implication
137
138 infixr 6 _⇒_
139 _⇒_ : ∀ {n} {ls r s} {as : Sets n ls} →
140 as ⇉ Set r → as ⇉ Set s → as ⇉ Set (r ⊔ s)
141 _⇒_ = liftₙ 2 _ (λ A B → A → B)
142
143 -- conjunction
144
145 infixr 7 _∩_
146 _∩_ : ∀ {n} {ls r s} {as : Sets n ls} →
147 as ⇉ Set r → as ⇉ Set s → as ⇉ Set (r ⊔ s)
148 _∩_ = liftₙ 2 _ _×_
149
150 -- disjunction
151
152 infixr 8 _∪_
153 _∪_ : ∀ {n} {ls r s} {as : Sets n ls} →
154 as ⇉ Set r → as ⇉ Set s → as ⇉ Set (r ⊔ s)
155 _∪_ = liftₙ 2 _ _⊎_
156
157 -- negation
158
159 ∁ : ∀ {n ls r} {as : Sets n ls} → as ⇉ Set r → as ⇉ Set r
160 ∁ = liftₙ 1 _ ¬_
161
162
163 apply⊤ₙ : ∀ {n ls r} {as : Sets n ls} {R : as ⇉ Set r} →
164 Π[ R ] → (vs : Product⊤ n as) → uncurry⊤ₙ n R vs
165 apply⊤ₙ {zero} prf vs = prf
166 apply⊤ₙ {suc n} prf (v , vs) = apply⊤ₙ (prf v) vs
167
168 applyₙ : ∀ {n ls r} {as : Sets n ls} {R : as ⇉ Set r} →
169 Π[ R ] → (vs : Product n as) → uncurry⊤ₙ n R (toProduct⊤ n vs)
170 applyₙ {n} prf vs = apply⊤ₙ prf (toProduct⊤ n vs)
171
172 iapply⊤ₙ : ∀ {n ls r} {as : Sets n ls} {R : as ⇉ Set r} →
173 ∀[ R ] → {vs : Product⊤ n as} → uncurry⊤ₙ n R vs
174 iapply⊤ₙ {zero} prf = prf
175 iapply⊤ₙ {suc n} prf = iapply⊤ₙ {n} prf
176
177 iapplyₙ : ∀ {n ls r} {as : Sets n ls} {R : as ⇉ Set r} →
178 ∀[ R ] → {vs : Product n as} → uncurry⊤ₙ n R (toProduct⊤ n vs)
179 iapplyₙ {n} prf = iapply⊤ₙ {n} prf
180
181 ------------------------------------------------------------------------
182 -- Properties of N-ary relations
183
184 -- Decidability
185
186 Decidable : ∀ {n ls r} {as : Sets n ls} → as ⇉ Set r → Set (r ⊔ ⨆ n ls)
187 Decidable R = Π[ mapₙ _ Dec R ]
188
189 -- erasure
190
191 ⌊_⌋ : ∀ {n ls r} {as : Sets n ls} {R : as ⇉ Set r} → Decidable R → as ⇉ Set r
192 ⌊_⌋ {zero} R? = Lift _ (Dec.True R?)
193 ⌊_⌋ {suc n} R? a = ⌊ R? a ⌋
194
195 -- equivalence between R and its erasure
196
197 fromWitness : ∀ {n ls r} {as : Sets n ls} (R : as ⇉ Set r) (R? : Decidable R) →
198 ∀[ ⌊ R? ⌋ ⇒ R ]
199 fromWitness {zero} R R? with R?
200 ... | yes r = λ _ → r
201 ... | false because _ = λ ()
202 fromWitness {suc n} R R? = fromWitness (R _) (R? _)
203
204 toWitness : ∀ {n ls r} {as : Sets n ls} (R : as ⇉ Set r) (R? : Decidable R) →
205 ∀[ R ⇒ ⌊ R? ⌋ ]
206 toWitness {zero} R R? with R?
207 ... | true because _ = _
208 ... | no ¬r = ⊥-elim ∘′ ¬r
209 toWitness {suc n} R R? = toWitness (R _) (R? _)