Debian Codegraph @

src/Data/Char/Properties.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Properties of operations on characters
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --cubical-compatible --safe #-}
8
9 module Data.Char.Properties where
10
11 open import Data.Bool.Base using (Bool)
12 open import Data.Char.Base using (Char; _≈_; _≉_; _<_; _≤_; toℕ)
13 import Data.Nat.Base as ℕ using (ℕ; _<_; _≤_)
14 import Data.Nat.Properties as ℕ
15 using (_<?_; <-cmp; <-isStrictPartialOrder; <-isStrictTotalOrder
16 ; <-strictPartialOrder; <-strictTotalOrder; <-irrefl; <-trans; <-asym
17 ; _≟_)
18 open import Data.Product.Base using (_,_)
19 open import Function.Base using (const; _∘′_)
20 open import Relation.Nullary using (¬_; yes; no)
21 open import Relation.Nullary.Decidable using (map′; isYes)
22 open import Relation.Binary.Core using (_⇒_)
23 open import Relation.Binary.Bundles
24 using (Setoid; DecSetoid; StrictPartialOrder; StrictTotalOrder; Preorder
25 ; Poset; DecPoset)
26 open import Relation.Binary.Structures
27 using (IsDecEquivalence; IsStrictPartialOrder; IsStrictTotalOrder
28 ; IsPreorder; IsPartialOrder; IsDecPartialOrder; IsEquivalence)
29 open import Relation.Binary.Definitions
30 using (Decidable; DecidableEquality; Trichotomous; Irreflexive
31 ; Transitive; Asymmetric; Antisymmetric; Symmetric; Substitutive
32 ; Reflexive; tri<; tri≈; tri>)
33 import Relation.Binary.Construct.On as On
34 using (decidable; transitive; asymmetric; isStrictPartialOrder
35 ; isStrictTotalOrder; strictPartialOrder; strictTotalOrder)
36 import Relation.Binary.Construct.Closure.Reflexive as Refl
37 using (Refl; reflexive)
38 import Relation.Binary.Construct.Closure.Reflexive.Properties as Refl
39 using (trans; antisym; decidable)
40 open import Relation.Binary.PropositionalEquality.Core as ≡
41 using (_≡_; _≢_; refl; cong; sym; trans; subst)
42 import Relation.Binary.PropositionalEquality.Properties as ≡ using
43 (isDecEquivalence; setoid; decSetoid; isEquivalence)
44
45 ------------------------------------------------------------------------
46 -- Primitive properties
47
48 open import Agda.Builtin.Char.Properties
49 renaming ( primCharToNatInjective to toℕ-injective)
50 public
51
52 ------------------------------------------------------------------------
53 -- Properties of _≈_
54
55 ≈⇒≡ : _≈_ ⇒ _≡_
56 ≈⇒≡ = toℕ-injective _ _
57
58 ≉⇒≢ : _≉_ ⇒ _≢_
59 ≉⇒≢ p refl = p refl
60
61 ≈-reflexive : _≡_ ⇒ _≈_
62 ≈-reflexive = cong toℕ
63
64 ------------------------------------------------------------------------
65 -- Properties of _≡_
66
67 infix 4 _≟_
68 _≟_ : DecidableEquality Char
69 x ≟ y = map′ ≈⇒≡ ≈-reflexive (toℕ x ℕ.≟ toℕ y)
70
71 setoid : Setoid _ _
72 setoid = ≡.setoid Char
73
74 decSetoid : DecSetoid _ _
75 decSetoid = ≡.decSetoid _≟_
76
77 isDecEquivalence : IsDecEquivalence _≡_
78 isDecEquivalence = ≡.isDecEquivalence _≟_
79
80 ------------------------------------------------------------------------
81 -- Boolean equality test.
82 --
83 -- Why is the definition _==_ = primCharEquality not used? One reason
84 -- is that the present definition can sometimes improve type
85 -- inference, at least with the version of Agda that is current at the
86 -- time of writing: see unit-test below.
87
88 infix 4 _==_
89 _==_ : Char → Char → Bool
90 c₁ == c₂ = isYes (c₁ ≟ c₂)
91
92 private
93
94 -- The following unit test does not type-check (at the time of
95 -- writing) if _==_ is replaced by primCharEquality.
96
97 data P : (Char → Bool) → Set where
98 MkP : (c : Char) → P (c ==_)
99
100 unit-test : P ('x' ==_)
101 unit-test = MkP _
102
103 ------------------------------------------------------------------------
104 -- Properties of _<_
105
106 infix 4 _<?_
107 _<?_ : Decidable _<_
108 _<?_ = On.decidable toℕ ℕ._<_ ℕ._<?_
109
110 <-cmp : Trichotomous _≡_ _<_
111 <-cmp c d with ℕ.<-cmp (toℕ c) (toℕ d)
112 ... | tri< lt ¬eq ¬gt = tri< lt (≉⇒≢ ¬eq) ¬gt
113 ... | tri≈ ¬lt eq ¬gt = tri≈ ¬lt (≈⇒≡ eq) ¬gt
114 ... | tri> ¬lt ¬eq gt = tri> ¬lt (≉⇒≢ ¬eq) gt
115
116 <-irrefl : Irreflexive _≡_ _<_
117 <-irrefl = ℕ.<-irrefl ∘′ cong toℕ
118
119 <-trans : Transitive _<_
120 <-trans {c} {d} {e} = On.transitive toℕ ℕ._<_ ℕ.<-trans {c} {d} {e}
121
122 <-asym : Asymmetric _<_
123 <-asym {c} {d} = On.asymmetric toℕ ℕ._<_ ℕ.<-asym {c} {d}
124
125 <-isStrictPartialOrder : IsStrictPartialOrder _≡_ _<_
126 <-isStrictPartialOrder = record
127 { isEquivalence = ≡.isEquivalence
128 ; irrefl = <-irrefl
129 ; trans = λ {a} {b} {c} → <-trans {a} {b} {c}
130 ; <-resp-≈ = (λ {c} → ≡.subst (c <_))
131 , (λ {c} → ≡.subst (_< c))
132 }
133
134 <-isStrictTotalOrder : IsStrictTotalOrder _≡_ _<_
135 <-isStrictTotalOrder = record
136 { isStrictPartialOrder = <-isStrictPartialOrder
137 ; compare = <-cmp
138 }
139
140 <-strictPartialOrder : StrictPartialOrder _ _ _
141 <-strictPartialOrder = record
142 { isStrictPartialOrder = <-isStrictPartialOrder
143 }
144
145 <-strictTotalOrder : StrictTotalOrder _ _ _
146 <-strictTotalOrder = record
147 { isStrictTotalOrder = <-isStrictTotalOrder
148 }
149
150 ------------------------------------------------------------------------
151 -- Properties of _≤_
152
153 infix 4 _≤?_
154 _≤?_ : Decidable _≤_
155 _≤?_ = Refl.decidable <-cmp
156
157 ≤-reflexive : _≡_ ⇒ _≤_
158 ≤-reflexive = Refl.reflexive
159
160 ≤-trans : Transitive _≤_
161 ≤-trans = Refl.trans (λ {a} {b} {c} → <-trans {a} {b} {c})
162
163 ≤-antisym : Antisymmetric _≡_ _≤_
164 ≤-antisym = Refl.antisym _≡_ refl ℕ.<-asym
165
166 ≤-isPreorder : IsPreorder _≡_ _≤_
167 ≤-isPreorder = record
168 { isEquivalence = ≡.isEquivalence
169 ; reflexive = ≤-reflexive
170 ; trans = ≤-trans
171 }
172
173 ≤-isPartialOrder : IsPartialOrder _≡_ _≤_
174 ≤-isPartialOrder = record
175 { isPreorder = ≤-isPreorder
176 ; antisym = ≤-antisym
177 }
178
179 ≤-isDecPartialOrder : IsDecPartialOrder _≡_ _≤_
180 ≤-isDecPartialOrder = record
181 { isPartialOrder = ≤-isPartialOrder
182 ; _≟_ = _≟_
183 ; _≤?_ = _≤?_
184 }
185
186 ≤-preorder : Preorder _ _ _
187 ≤-preorder = record { isPreorder = ≤-isPreorder }
188
189 ≤-poset : Poset _ _ _
190 ≤-poset = record { isPartialOrder = ≤-isPartialOrder }
191
192 ≤-decPoset : DecPoset _ _ _
193 ≤-decPoset = record { isDecPartialOrder = ≤-isDecPartialOrder }
194
195 ------------------------------------------------------------------------
196 -- DEPRECATED NAMES
197 ------------------------------------------------------------------------
198 -- Please use the new names as continuing support for the old names is
199 -- not guaranteed.
200
201 -- Version 1.5
202
203 ≈-refl : Reflexive _≈_
204 ≈-refl = refl
205 {-# WARNING_ON_USAGE ≈-refl
206 "Warning: ≈-refl was deprecated in v1.5.
207 Please use Propositional Equality's refl instead."
208 #-}
209
210 ≈-sym : Symmetric _≈_
211 ≈-sym = sym
212 {-# WARNING_ON_USAGE ≈-sym
213 "Warning: ≈-sym was deprecated in v1.5.
214 Please use Propositional Equality's sym instead."
215 #-}
216
217 ≈-trans : Transitive _≈_
218 ≈-trans = trans
219 {-# WARNING_ON_USAGE ≈-trans
220 "Warning: ≈-trans was deprecated in v1.5.
221 Please use Propositional Equality's trans instead."
222 #-}
223
224 ≈-subst : ∀ {ℓ} → Substitutive _≈_ ℓ
225 ≈-subst P x≈y p = subst P (≈⇒≡ x≈y) p
226 {-# WARNING_ON_USAGE ≈-subst
227 "Warning: ≈-subst was deprecated in v1.5.
228 Please use Propositional Equality's subst instead."
229 #-}
230
231 infix 4 _≈?_
232 _≈?_ : Decidable _≈_
233 x ≈? y = toℕ x ℕ.≟ toℕ y
234
235 ≈-isEquivalence : IsEquivalence _≈_
236 ≈-isEquivalence = record
237 { refl = refl
238 ; sym = sym
239 ; trans = trans
240 }
241 ≈-setoid : Setoid _ _
242 ≈-setoid = record
243 { isEquivalence = ≈-isEquivalence
244 }
245 ≈-isDecEquivalence : IsDecEquivalence _≈_
246 ≈-isDecEquivalence = record
247 { isEquivalence = ≈-isEquivalence
248 ; _≟_ = _≈?_
249 }
250 ≈-decSetoid : DecSetoid _ _
251 ≈-decSetoid = record
252 { isDecEquivalence = ≈-isDecEquivalence
253 }
254 {-# WARNING_ON_USAGE _≈?_
255 "Warning: _≈?_ was deprecated in v1.5.
256 Please use _≟_ instead."
257 #-}
258 {-# WARNING_ON_USAGE ≈-isEquivalence
259 "Warning: ≈-isEquivalence was deprecated in v1.5.
260 Please use Propositional Equality's isEquivalence instead."
261 #-}
262 {-# WARNING_ON_USAGE ≈-setoid
263 "Warning: ≈-setoid was deprecated in v1.5.
264 Please use Propositional Equality's setoid instead."
265 #-}
266 {-# WARNING_ON_USAGE ≈-isDecEquivalence
267 "Warning: ≈-isDecEquivalence was deprecated in v1.5.
268 Please use Propositional Equality's isDecEquivalence instead."
269 #-}
270 {-# WARNING_ON_USAGE ≈-decSetoid
271 "Warning: ≈-decSetoid was deprecated in v1.5.
272 Please use Propositional Equality's decSetoid instead."
273 #-}
274
275 ≡-setoid : Setoid _ _
276 ≡-setoid = setoid
277 {-# WARNING_ON_USAGE ≡-setoid
278 "Warning: ≡-setoid was deprecated in v1.5.
279 Please use setoid instead."
280 #-}
281
282 ≡-decSetoid : DecSetoid _ _
283 ≡-decSetoid = decSetoid
284 {-# WARNING_ON_USAGE ≡-decSetoid
285 "Warning: ≡-decSetoid was deprecated in v1.5.
286 Please use decSetoid instead."
287 #-}
288
289 <-isStrictPartialOrder-≈ : IsStrictPartialOrder _≈_ _<_
290 <-isStrictPartialOrder-≈ = On.isStrictPartialOrder toℕ ℕ.<-isStrictPartialOrder
291 {-# WARNING_ON_USAGE <-isStrictPartialOrder-≈
292 "Warning: <-isStrictPartialOrder-≈ was deprecated in v1.5.
293 Please use <-isStrictPartialOrder instead."
294 #-}
295
296 <-isStrictTotalOrder-≈ : IsStrictTotalOrder _≈_ _<_
297 <-isStrictTotalOrder-≈ = On.isStrictTotalOrder toℕ ℕ.<-isStrictTotalOrder
298 {-# WARNING_ON_USAGE <-isStrictTotalOrder-≈
299 "Warning: <-isStrictTotalOrder-≈ was deprecated in v1.5.
300 Please use <-isStrictTotalOrder instead."
301 #-}
302
303 <-strictPartialOrder-≈ : StrictPartialOrder _ _ _
304 <-strictPartialOrder-≈ = On.strictPartialOrder ℕ.<-strictPartialOrder toℕ
305 {-# WARNING_ON_USAGE <-strictPartialOrder-≈
306 "Warning: <-strictPartialOrder-≈ was deprecated in v1.5.
307 Please use <-strictPartialOrder instead."
308 #-}
309
310 <-strictTotalOrder-≈ : StrictTotalOrder _ _ _
311 <-strictTotalOrder-≈ = On.strictTotalOrder ℕ.<-strictTotalOrder toℕ
312 {-# WARNING_ON_USAGE <-strictTotalOrder-≈
313 "Warning: <-strictTotalOrder-≈ was deprecated in v1.5.
314 Please use <-strictTotalOrder instead."
315 #-}
316