| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Regular expressions: search algorithms |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 8 |
|
| 9 |
open import Relation.Binary.Bundles using (DecPoset) |
| 10 |
|
| 11 |
module Text.Regex.Search {a e r} (P? : DecPoset a e r) where |
| 12 |
|
| 13 |
open import Level using (_⊔_) |
| 14 |
open import Data.Bool.Base using (if_then_else_; true; false) |
| 15 |
open import Data.List.Base using (List; []; _∷_) |
| 16 |
open import Data.Sum.Base as Sum using (_⊎_; inj₁; inj₂; [_,_]′) |
| 17 |
open import Function.Base using (id; _∘′_; _∘_) |
| 18 |
|
| 19 |
open import Data.List.Relation.Binary.Prefix.Heterogeneous |
| 20 |
using (Prefix; []; _∷_) hiding (module Prefix) |
| 21 |
open import Data.List.Relation.Binary.Infix.Heterogeneous |
| 22 |
using (Infix; here; there) hiding (module Infix) |
| 23 |
import Data.List.Relation.Binary.Infix.Heterogeneous.Properties as Infixₚ |
| 24 |
open import Data.List.Relation.Binary.Pointwise |
| 25 |
as Pointwise using (Pointwise; []; _∷_) |
| 26 |
open import Data.List.Relation.Binary.Suffix.Heterogeneous |
| 27 |
using (Suffix; here; there) hiding (module Suffix) |
| 28 |
|
| 29 |
open import Relation.Nullary using (Dec; ¬_; yes; no) |
| 30 |
open import Relation.Nullary.Decidable using (map′) |
| 31 |
open import Relation.Nullary.Negation using (contradiction) |
| 32 |
open import Relation.Binary.Core using (Rel; _⇒_) |
| 33 |
open import Relation.Binary.Definitions using (Decidable) |
| 34 |
open import Relation.Binary.PropositionalEquality.Core |
| 35 |
|
| 36 |
open DecPoset P? using (preorder) renaming (Carrier to A) |
| 37 |
open import Text.Regex.Base preorder |
| 38 |
open import Text.Regex.Properties P? |
| 39 |
open import Text.Regex.Derivative.Brzozowski P? |
| 40 |
|
| 41 |
------------------------------------------------------------------------ |
| 42 |
-- Type corresponding to a match |
| 43 |
|
| 44 |
-- Users have control over whether the match should start at the |
| 45 |
-- beginning or stop at the end. So we have a precise type of spans |
| 46 |
-- ensuring their demands are respected |
| 47 |
Span : ∀ {r} → Regex → Rel A r → Rel (List A) (a ⊔ r) |
| 48 |
Span regex = |
| 49 |
if Regex.fromStart regex |
| 50 |
then if Regex.tillEnd regex |
| 51 |
then Pointwise |
| 52 |
else Prefix |
| 53 |
else if Regex.tillEnd regex |
| 54 |
then Suffix |
| 55 |
else Infix |
| 56 |
|
| 57 |
-- All matches are selecting an infix sublist |
| 58 |
toInfix : ∀ {r} {R : Rel A r} e → Span e R ⇒ Infix R |
| 59 |
toInfix e with Regex.fromStart e | Regex.tillEnd e |
| 60 |
... | true | true = Infixₚ.fromPointwise |
| 61 |
... | true | false = here |
| 62 |
... | false | true = Infixₚ.fromSuffix |
| 63 |
... | false | false = id |
| 64 |
|
| 65 |
-- A match is a list, a proof it matches the regular expression, |
| 66 |
-- and a proof it is the right sort of sublist. |
| 67 |
record Match {s} (R : Rel (List A) s) (xs : List A) (exp : Exp) |
| 68 |
: Set (a ⊔ e ⊔ r ⊔ s) where |
| 69 |
constructor mkMatch |
| 70 |
field |
| 71 |
list : List A |
| 72 |
match : list ∈ exp |
| 73 |
related : R list xs |
| 74 |
open Match public |
| 75 |
|
| 76 |
map : ∀ {r s} {R : Rel (List A) r} {S : Rel (List A) s} {xs ys e} → |
| 77 |
(∀ {a} → R a xs → S a ys) → Match R xs e → Match S ys e |
| 78 |
map f (mkMatch ys ys∈e pys) = mkMatch ys ys∈e (f pys) |
| 79 |
|
| 80 |
------------------------------------------------------------------------ |
| 81 |
-- Search algorithms |
| 82 |
|
| 83 |
module Prefix where |
| 84 |
|
| 85 |
[]ᴹ : ∀ {xs e} → [] ∈ e → Match (Prefix _≡_) xs e |
| 86 |
[]ᴹ p = mkMatch [] p [] |
| 87 |
|
| 88 |
[]⁻¹ᴹ : ∀ {e} → Match (Prefix _≡_) [] e → [] ∈ e |
| 89 |
[]⁻¹ᴹ (mkMatch .[] p []) = p |
| 90 |
|
| 91 |
infixr 5 _∷ᴹ_ _∷⁻¹ᴹ_ |
| 92 |
|
| 93 |
_∷ᴹ_ : ∀ {xs e} x → Match (Prefix _≡_) xs (eat x e) → Match (Prefix _≡_) (x ∷ xs) e |
| 94 |
x ∷ᴹ (mkMatch ys ys∈e\x ys≤xs) = mkMatch (x ∷ ys) (eat-sound x _ ys∈e\x) (refl ∷ ys≤xs) |
| 95 |
|
| 96 |
_∷⁻¹ᴹ_ : ∀ {xs x e} → [] ∉ e → |
| 97 |
Match (Prefix _≡_) (x ∷ xs) e → Match (Prefix _≡_) xs (eat x e) |
| 98 |
[]∉e ∷⁻¹ᴹ (mkMatch .[] []∈e []) = contradiction []∈e []∉e |
| 99 |
[]∉e ∷⁻¹ᴹ (mkMatch (._ ∷ ys) ys∈e (refl ∷ ys≤xs)) = mkMatch ys (eat-complete _ _ ys∈e) ys≤xs |
| 100 |
|
| 101 |
shortest : Decidable (Match (Prefix _≡_)) |
| 102 |
shortest xs ∅ = no (∉∅ ∘ match) |
| 103 |
shortest xs e with []∈? e |
| 104 |
... | yes []∈e = yes ([]ᴹ []∈e) |
| 105 |
shortest [] e | no []∉e = no ([]∉e ∘′ []⁻¹ᴹ) |
| 106 |
shortest (x ∷ xs) e | no []∉e with shortest xs (eat x e) |
| 107 |
... | yes p = yes (x ∷ᴹ p) |
| 108 |
... | no ¬p = no (¬p ∘ ([]∉e ∷⁻¹ᴹ_)) |
| 109 |
|
| 110 |
longest : Decidable (Match (Prefix _≡_)) |
| 111 |
longest [] e = map′ []ᴹ []⁻¹ᴹ ([]∈? e) |
| 112 |
longest xs ∅ = no (∉∅ ∘ match) |
| 113 |
longest (x ∷ xs) e with longest xs (eat x e) |
| 114 |
... | yes p = yes (x ∷ᴹ p) |
| 115 |
... | no ¬p with []∈? e |
| 116 |
... | yes []∈e = yes ([]ᴹ []∈e) |
| 117 |
... | no []∉e = no (¬p ∘ ([]∉e ∷⁻¹ᴹ_)) |
| 118 |
|
| 119 |
module Infix where |
| 120 |
|
| 121 |
[]⁻¹ᴹ : ∀ {e acc} → Match (Infix _≡_) [] e ⊎ Match (Prefix _≡_) [] acc → [] ∈ e ⊎ [] ∈ acc |
| 122 |
[]⁻¹ᴹ (inj₁ (mkMatch .[] []∈e (here []))) = inj₁ []∈e |
| 123 |
[]⁻¹ᴹ (inj₂ (mkMatch .[] []∈acc [])) = inj₂ []∈acc |
| 124 |
|
| 125 |
step : ∀ {e acc} x {xs} → Match (Infix _≡_) xs e ⊎ Match (Prefix _≡_) xs (eat x (acc ∣ e)) → |
| 126 |
Match (Infix _≡_) (x ∷ xs) e ⊎ Match (Prefix _≡_) (x ∷ xs) acc |
| 127 |
step x (inj₁ (mkMatch ys ys∈e p)) = inj₁ (mkMatch ys ys∈e (there p)) |
| 128 |
step {e} {acc} x (inj₂ (mkMatch ys ys∈e p)) with eat-sound x (acc ∣ e) ys∈e |
| 129 |
... | sum (inj₂ xys∈e) = inj₁ (mkMatch (x ∷ ys) xys∈e (here (refl ∷ p))) |
| 130 |
... | sum (inj₁ xys∈e) = inj₂ (mkMatch (x ∷ ys) xys∈e (refl ∷ p)) |
| 131 |
|
| 132 |
step⁻¹ : ∀ {e acc} x {xs} → |
| 133 |
[] ∉ e → [] ∉ acc → |
| 134 |
Match (Infix _≡_) (x ∷ xs) e ⊎ Match (Prefix _≡_) (x ∷ xs) acc → |
| 135 |
Match (Infix _≡_) xs e ⊎ Match (Prefix _≡_) xs (eat x (acc ∣ e)) |
| 136 |
-- can't possibly be the empty match |
| 137 |
step⁻¹ x []∉e []∉acc (inj₁ (mkMatch .[] ys∈e (here []))) = contradiction ys∈e []∉e |
| 138 |
step⁻¹ x []∉e []∉acc (inj₂ (mkMatch .[] ys∈e [])) = contradiction ys∈e []∉acc |
| 139 |
-- if it starts 'there', it's an infix solution |
| 140 |
step⁻¹ x []∉e []∉acc (inj₁ (mkMatch ys ys∈e (there p))) = inj₁ (mkMatch ys ys∈e p) |
| 141 |
-- if it starts 'here' we're in prefix territory |
| 142 |
step⁻¹ {e} {acc} x []∉e []∉acc (inj₁ (mkMatch (.x ∷ ys) ys∈e (here (refl ∷ p)))) |
| 143 |
= inj₂ (mkMatch ys (eat-complete x (acc ∣ e) (sum (inj₂ ys∈e))) p) |
| 144 |
step⁻¹ {e} {acc} x []∉e []∉acc (inj₂ (mkMatch (.x ∷ ys) ys∈e (refl ∷ p))) |
| 145 |
= inj₂ (mkMatch ys (eat-complete x (acc ∣ e) (sum (inj₁ ys∈e))) p) |
| 146 |
|
| 147 |
-- search non-deterministically: at each step, the `acc` regex is |
| 148 |
-- changed to accomodate the fact the match may be starting just now |
| 149 |
searchND : ∀ xs e acc → [] ∉ e → Dec (Match (Infix _≡_) xs e ⊎ Match (Prefix _≡_) xs acc) |
| 150 |
searchND xs e acc []∉e with []∈? acc |
| 151 |
... | yes []∈acc with Prefix.longest xs acc -- get the best match possible |
| 152 |
... | yes longer = yes (inj₂ longer) |
| 153 |
... | no noMatch = contradiction (mkMatch [] []∈acc []) noMatch |
| 154 |
searchND [] e acc []∉e | no []∉acc = no ([ []∉e , []∉acc ]′ ∘′ []⁻¹ᴹ) |
| 155 |
searchND (x ∷ xs) e acc []∉e | no []∉acc |
| 156 |
= map′ (step x) (step⁻¹ x []∉e []∉acc) (searchND xs e (eat x (acc ∣ e)) []∉e) |
| 157 |
|
| 158 |
search : Decidable (Match (Infix _≡_)) |
| 159 |
search xs e with []∈? e |
| 160 |
... | yes []∈e = yes (mkMatch [] []∈e (here [])) |
| 161 |
... | no []∉e with searchND xs e ∅ []∉e |
| 162 |
... | no ¬p = no (¬p ∘′ inj₁) |
| 163 |
... | yes (inj₁ p) = yes p |
| 164 |
... | yes (inj₂ p) = contradiction (match p) ∉∅ |
| 165 |
|
| 166 |
module Whole where |
| 167 |
|
| 168 |
whole : ∀ xs e → xs ∈ e → Match (Pointwise _≡_) xs e |
| 169 |
whole xs e p = mkMatch xs p (Pointwise.refl refl) |
| 170 |
|
| 171 |
whole⁻¹ : ∀ xs e → Match (Pointwise _≡_) xs e → xs ∈ e |
| 172 |
whole⁻¹ xs e (mkMatch ys ys∈e p) with Pointwise.Pointwise-≡⇒≡ p |
| 173 |
whole⁻¹ xs e (mkMatch .xs xs∈e p) | refl = xs∈e |
| 174 |
|
| 175 |
search : Decidable (Match (Pointwise _≡_)) |
| 176 |
search xs e = map′ (whole xs e) (whole⁻¹ xs e) (xs ∈? e) |
| 177 |
|
| 178 |
module Suffix where |
| 179 |
|
| 180 |
[]⁻¹ᴹ : ∀ {e acc} → Match (Suffix _≡_) [] e ⊎ Match (Pointwise _≡_) [] acc → [] ∈ e ⊎ [] ∈ acc |
| 181 |
[]⁻¹ᴹ (inj₁ (mkMatch .[] ys∈e (here []))) = inj₁ ys∈e |
| 182 |
[]⁻¹ᴹ (inj₂ (mkMatch .[] ys∈acc [])) = inj₂ ys∈acc |
| 183 |
|
| 184 |
step : ∀ {e acc} x {xs} → |
| 185 |
Match (Suffix _≡_) xs e ⊎ Match (Pointwise _≡_) xs (eat x (e ∣ acc)) → |
| 186 |
Match (Suffix _≡_) (x ∷ xs) e ⊎ Match (Pointwise _≡_) (x ∷ xs) acc |
| 187 |
step x (inj₁ (mkMatch ys ys∈e p)) = inj₁ (mkMatch ys ys∈e (there p)) |
| 188 |
step {e} {acc} x (inj₂ (mkMatch ys ys∈e p)) with eat-sound x (e ∣ acc) ys∈e |
| 189 |
... | sum (inj₁ xys∈e) = inj₁ (mkMatch (x ∷ ys) xys∈e (here (refl ∷ p))) |
| 190 |
... | sum (inj₂ xys∈acc) = inj₂ (mkMatch (x ∷ ys) xys∈acc (refl ∷ p)) |
| 191 |
|
| 192 |
step⁻¹ : ∀ {e acc} x {xs} → |
| 193 |
Match (Suffix _≡_) (x ∷ xs) e ⊎ Match (Pointwise _≡_) (x ∷ xs) acc → |
| 194 |
Match (Suffix _≡_) xs e ⊎ Match (Pointwise _≡_) xs (eat x (e ∣ acc)) |
| 195 |
-- match starts later |
| 196 |
step⁻¹ x (inj₁ (mkMatch ys ys∈e (there p))) = inj₁ (mkMatch ys ys∈e p) |
| 197 |
-- match starts here! |
| 198 |
step⁻¹ {e} {acc} x (inj₁ (mkMatch (.x ∷ ys) ys∈e (here (refl ∷ p)))) |
| 199 |
= inj₂ (mkMatch ys (eat-complete x (e ∣ acc) (sum (inj₁ ys∈e))) p) |
| 200 |
step⁻¹ {e} {acc} x (inj₂ (mkMatch (.x ∷ ys) ys∈e (refl ∷ p))) |
| 201 |
= inj₂ (mkMatch ys (eat-complete x (e ∣ acc) (sum (inj₂ ys∈e))) p) |
| 202 |
|
| 203 |
searchND : ∀ xs e acc → Dec (Match (Suffix _≡_) xs e ⊎ Match (Pointwise _≡_) xs acc) |
| 204 |
searchND [] e acc with []∈? e | []∈? acc |
| 205 |
... | yes []∈e | _ = yes (inj₁ (mkMatch [] []∈e (here []))) |
| 206 |
... | _ | yes []∈acc = yes (inj₂ (mkMatch [] []∈acc [])) |
| 207 |
... | no []∉e | no []∉acc = no ([ []∉e , []∉acc ]′ ∘′ []⁻¹ᴹ) |
| 208 |
searchND (x ∷ xs) e acc |
| 209 |
= map′ (step x) (step⁻¹ x) (searchND xs e (eat x (e ∣ acc))) |
| 210 |
|
| 211 |
search : Decidable (Match (Suffix _≡_)) |
| 212 |
search xs e with searchND xs e ∅ |
| 213 |
... | no ¬p = no (¬p ∘′ inj₁) |
| 214 |
... | yes (inj₁ p) = yes p |
| 215 |
... | yes (inj₂ p) = contradiction (match p) ∉∅ |
| 216 |
|
| 217 |
------------------------------------------------------------------------ |
| 218 |
-- Search for the user-specified span |
| 219 |
|
| 220 |
search : ∀ xs e → Dec (Match (Span e _≡_) xs (Regex.expression e)) |
| 221 |
search xs e with Regex.fromStart e | Regex.tillEnd e |
| 222 |
... | true | true = Whole.search xs (Regex.expression e) |
| 223 |
... | true | false = Prefix.shortest xs (Regex.expression e) |
| 224 |
... | false | true = Suffix.search xs (Regex.expression e) |
| 225 |
... | false | false = Infix.search xs (Regex.expression e) |