| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Properties of operations on strings |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 8 |
|
| 9 |
module Data.String.Properties where |
| 10 |
|
| 11 |
open import Data.Bool.Base using (Bool) |
| 12 |
import Data.Char.Properties as Char |
| 13 |
import Data.List.Relation.Binary.Pointwise as Pointwise |
| 14 |
import Data.List.Relation.Binary.Lex.Strict as StrictLex |
| 15 |
open import Data.String.Base |
| 16 |
open import Function.Base using (_∘_; _$_) |
| 17 |
open import Relation.Nullary.Decidable using (yes; no) |
| 18 |
open import Relation.Nullary.Decidable using (map′; isYes) |
| 19 |
open import Relation.Binary.Core using (_⇒_) |
| 20 |
open import Relation.Binary.Bundles |
| 21 |
using (Setoid; DecSetoid; StrictPartialOrder; StrictTotalOrder |
| 22 |
; DecTotalOrder; DecPoset) |
| 23 |
open import Relation.Binary.Structures |
| 24 |
using (IsEquivalence; IsDecEquivalence; IsStrictPartialOrder |
| 25 |
; IsStrictTotalOrder; IsDecPartialOrder; IsDecTotalOrder) |
| 26 |
open import Relation.Binary.Definitions |
| 27 |
using (Reflexive; Symmetric; Transitive; Substitutive; Decidable |
| 28 |
; DecidableEquality) |
| 29 |
open import Relation.Binary.PropositionalEquality.Core |
| 30 |
import Relation.Binary.Construct.On as On |
| 31 |
import Relation.Binary.PropositionalEquality.Properties as PropEq |
| 32 |
|
| 33 |
------------------------------------------------------------------------ |
| 34 |
-- Primitive properties |
| 35 |
|
| 36 |
open import Agda.Builtin.String.Properties public |
| 37 |
renaming ( primStringToListInjective to toList-injective) |
| 38 |
|
| 39 |
------------------------------------------------------------------------ |
| 40 |
-- Properties of _≈_ |
| 41 |
|
| 42 |
≈⇒≡ : _≈_ ⇒ _≡_ |
| 43 |
≈⇒≡ = toList-injective _ _ |
| 44 |
∘ Pointwise.Pointwise-≡⇒≡ |
| 45 |
|
| 46 |
≈-reflexive : _≡_ ⇒ _≈_ |
| 47 |
≈-reflexive = Pointwise.≡⇒Pointwise-≡ |
| 48 |
∘ cong toList |
| 49 |
|
| 50 |
≈-refl : Reflexive _≈_ |
| 51 |
≈-refl {x} = ≈-reflexive {x} {x} refl |
| 52 |
|
| 53 |
≈-sym : Symmetric _≈_ |
| 54 |
≈-sym = Pointwise.symmetric sym |
| 55 |
|
| 56 |
≈-trans : Transitive _≈_ |
| 57 |
≈-trans = Pointwise.transitive trans |
| 58 |
|
| 59 |
≈-subst : ∀ {ℓ} → Substitutive _≈_ ℓ |
| 60 |
≈-subst P x≈y p = subst P (≈⇒≡ x≈y) p |
| 61 |
|
| 62 |
infix 4 _≈?_ |
| 63 |
_≈?_ : Decidable _≈_ |
| 64 |
x ≈? y = Pointwise.decidable Char._≟_ (toList x) (toList y) |
| 65 |
|
| 66 |
≈-isEquivalence : IsEquivalence _≈_ |
| 67 |
≈-isEquivalence = record |
| 68 |
{ refl = λ {i} → ≈-refl {i} |
| 69 |
; sym = λ {i j} → ≈-sym {i} {j} |
| 70 |
; trans = λ {i j k} → ≈-trans {i} {j} {k} |
| 71 |
} |
| 72 |
|
| 73 |
≈-setoid : Setoid _ _ |
| 74 |
≈-setoid = record |
| 75 |
{ isEquivalence = ≈-isEquivalence |
| 76 |
} |
| 77 |
|
| 78 |
≈-isDecEquivalence : IsDecEquivalence _≈_ |
| 79 |
≈-isDecEquivalence = record |
| 80 |
{ isEquivalence = ≈-isEquivalence |
| 81 |
; _≟_ = _≈?_ |
| 82 |
} |
| 83 |
|
| 84 |
≈-decSetoid : DecSetoid _ _ |
| 85 |
≈-decSetoid = record |
| 86 |
{ isDecEquivalence = ≈-isDecEquivalence |
| 87 |
} |
| 88 |
|
| 89 |
------------------------------------------------------------------------ |
| 90 |
-- Properties of _≡_ |
| 91 |
|
| 92 |
infix 4 _≟_ |
| 93 |
|
| 94 |
_≟_ : DecidableEquality String |
| 95 |
x ≟ y = map′ ≈⇒≡ ≈-reflexive $ x ≈? y |
| 96 |
|
| 97 |
≡-setoid : Setoid _ _ |
| 98 |
≡-setoid = PropEq.setoid String |
| 99 |
|
| 100 |
≡-decSetoid : DecSetoid _ _ |
| 101 |
≡-decSetoid = PropEq.decSetoid _≟_ |
| 102 |
|
| 103 |
------------------------------------------------------------------------ |
| 104 |
-- Properties of _<_ |
| 105 |
|
| 106 |
infix 4 _<?_ |
| 107 |
_<?_ : Decidable _<_ |
| 108 |
x <? y = StrictLex.<-decidable Char._≟_ Char._<?_ (toList x) (toList y) |
| 109 |
|
| 110 |
<-isStrictPartialOrder-≈ : IsStrictPartialOrder _≈_ _<_ |
| 111 |
<-isStrictPartialOrder-≈ = |
| 112 |
On.isStrictPartialOrder |
| 113 |
toList |
| 114 |
(StrictLex.<-isStrictPartialOrder Char.<-isStrictPartialOrder) |
| 115 |
|
| 116 |
<-isStrictTotalOrder-≈ : IsStrictTotalOrder _≈_ _<_ |
| 117 |
<-isStrictTotalOrder-≈ = |
| 118 |
On.isStrictTotalOrder |
| 119 |
toList |
| 120 |
(StrictLex.<-isStrictTotalOrder Char.<-isStrictTotalOrder) |
| 121 |
|
| 122 |
<-strictPartialOrder-≈ : StrictPartialOrder _ _ _ |
| 123 |
<-strictPartialOrder-≈ = |
| 124 |
On.strictPartialOrder |
| 125 |
(StrictLex.<-strictPartialOrder Char.<-strictPartialOrder) |
| 126 |
toList |
| 127 |
|
| 128 |
<-strictTotalOrder-≈ : StrictTotalOrder _ _ _ |
| 129 |
<-strictTotalOrder-≈ = |
| 130 |
On.strictTotalOrder |
| 131 |
(StrictLex.<-strictTotalOrder Char.<-strictTotalOrder) |
| 132 |
toList |
| 133 |
|
| 134 |
≤-isDecPartialOrder-≈ : IsDecPartialOrder _≈_ _≤_ |
| 135 |
≤-isDecPartialOrder-≈ = |
| 136 |
On.isDecPartialOrder |
| 137 |
toList |
| 138 |
(StrictLex.≤-isDecPartialOrder Char.<-isStrictTotalOrder) |
| 139 |
|
| 140 |
≤-isDecTotalOrder-≈ : IsDecTotalOrder _≈_ _≤_ |
| 141 |
≤-isDecTotalOrder-≈ = |
| 142 |
On.isDecTotalOrder |
| 143 |
toList |
| 144 |
(StrictLex.≤-isDecTotalOrder Char.<-isStrictTotalOrder) |
| 145 |
|
| 146 |
≤-decTotalOrder-≈ : DecTotalOrder _ _ _ |
| 147 |
≤-decTotalOrder-≈ = |
| 148 |
On.decTotalOrder |
| 149 |
(StrictLex.≤-decTotalOrder Char.<-strictTotalOrder) |
| 150 |
toList |
| 151 |
|
| 152 |
≤-decPoset-≈ : DecPoset _ _ _ |
| 153 |
≤-decPoset-≈ = |
| 154 |
On.decPoset |
| 155 |
(StrictLex.≤-decPoset Char.<-strictTotalOrder) |
| 156 |
toList |
| 157 |
|
| 158 |
------------------------------------------------------------------------ |
| 159 |
-- Alternative Boolean equality test. |
| 160 |
-- |
| 161 |
-- Why is the definition _==_ = primStringEquality not used? One |
| 162 |
-- reason is that the present definition can sometimes improve type |
| 163 |
-- inference, at least with the version of Agda that is current at the |
| 164 |
-- time of writing: see unit-test below. |
| 165 |
|
| 166 |
infix 4 _==_ |
| 167 |
_==_ : String → String → Bool |
| 168 |
s₁ == s₂ = isYes (s₁ ≟ s₂) |
| 169 |
|
| 170 |
private |
| 171 |
|
| 172 |
-- The following unit test does not type-check (at the time of |
| 173 |
-- writing) if _==_ is replaced by primStringEquality. |
| 174 |
|
| 175 |
data P : (String → Bool) → Set where |
| 176 |
p : (c : String) → P (_==_ c) |
| 177 |
|
| 178 |
unit-test : P (_==_ "") |
| 179 |
unit-test = p _ |