| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Regular expressions: smart constructors |
| 5 |
-- Computing the Brzozowski derivative of a regular expression may lead |
| 6 |
-- to a blow-up in the size of the expression. To keep it tractable it |
| 7 |
-- is crucial to use smart constructors. |
| 8 |
------------------------------------------------------------------------ |
| 9 |
|
| 10 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 11 |
|
| 12 |
open import Relation.Binary.Bundles using (Preorder) |
| 13 |
|
| 14 |
module Text.Regex.SmartConstructors {a e r} (P : Preorder a e r) where |
| 15 |
|
| 16 |
open import Data.List.Base using ([]) |
| 17 |
open import Data.List.Relation.Ternary.Appending.Propositional |
| 18 |
open import Data.Sum.Base using (inj₁; inj₂; fromInj₁; fromInj₂) |
| 19 |
|
| 20 |
open import Relation.Nullary.Decidable using (yes; no) |
| 21 |
open import Relation.Nullary.Negation using (contradiction) |
| 22 |
open import Relation.Binary.PropositionalEquality.Core using (refl) |
| 23 |
|
| 24 |
open import Text.Regex.Base P as R hiding (_∣_; _∙_; _⋆) |
| 25 |
open import Text.Regex.Properties.Core P |
| 26 |
|
| 27 |
------------------------------------------------------------------------ |
| 28 |
-- Sum |
| 29 |
|
| 30 |
infixr 5 _∣_ |
| 31 |
|
| 32 |
_∣_ : (e f : Exp) → Exp |
| 33 |
e ∣ f with is-∅ e | is-∅ f |
| 34 |
... | yes _ | _ = f |
| 35 |
... | _ | yes _ = e |
| 36 |
... | _ | _ = e R.∣ f |
| 37 |
|
| 38 |
∣-sound : ∀ {w} e f → w ∈ (e ∣ f) → w ∈ (e R.∣ f) |
| 39 |
∣-sound e f p with is-∅ e | is-∅ f |
| 40 |
... | yes _ | _ = sum (inj₂ p) |
| 41 |
... | no _ | yes _ = sum (inj₁ p) |
| 42 |
... | no _ | no _ = p |
| 43 |
|
| 44 |
∣-complete : ∀ {w} e f → w ∈ (e R.∣ f) → w ∈ (e ∣ f) |
| 45 |
∣-complete e f pr@(sum p) with is-∅ e | is-∅ f |
| 46 |
... | yes refl | _ = fromInj₂ (λ p → contradiction p ∉∅) p |
| 47 |
... | no _ | yes refl = fromInj₁ (λ p → contradiction p ∉∅) p |
| 48 |
... | no _ | no _ = pr |
| 49 |
|
| 50 |
------------------------------------------------------------------------ |
| 51 |
-- Product |
| 52 |
|
| 53 |
infixr 6 _∙_ |
| 54 |
|
| 55 |
_∙_ : (e f : Exp) → Exp |
| 56 |
e ∙ f with is-∅ e | is-ε e | is-∅ f | is-ε f |
| 57 |
... | yes _ | _ | _ | _ = R.∅ |
| 58 |
... | _ | yes _ | _ | _ = f |
| 59 |
... | _ | _ | yes _ | _ = R.∅ |
| 60 |
... | _ | _ | _ | yes _ = e |
| 61 |
... | _ | _ | _ | _ = e R.∙ f |
| 62 |
|
| 63 |
∙-sound : ∀ {w} e f → w ∈ (e ∙ f) → w ∈ (e R.∙ f) |
| 64 |
∙-sound e f p with is-∅ e | is-ε e | is-∅ f | is-ε f |
| 65 |
... | yes refl | _ | _ | _ = contradiction p ∉∅ |
| 66 |
... | no _ | yes refl | _ | _ = prod ([] ++ _) ε p |
| 67 |
... | no _ | no _ | yes refl | _ = contradiction p ∉∅ |
| 68 |
... | no _ | no _ | no _ | yes refl = prod (_ ++[]) p ε |
| 69 |
... | no _ | no _ | no _ | no _ = p |
| 70 |
|
| 71 |
∙-complete : ∀ {w} e f → w ∈ (e R.∙ f) → w ∈ (e ∙ f) |
| 72 |
∙-complete e f pr@(prod eq p q) with is-∅ e | is-ε e | is-∅ f | is-ε f |
| 73 |
... | yes refl | _ | _ | _ = contradiction p ∉∅ |
| 74 |
... | no _ | yes refl | _ | _ = ∈ε∙e-inv pr |
| 75 |
... | no _ | no _ | yes refl | _ = contradiction q ∉∅ |
| 76 |
... | no _ | no _ | no _ | yes refl = ∈e∙ε-inv pr |
| 77 |
... | no _ | no _ | no _ | no _ = pr |
| 78 |
|
| 79 |
------------------------------------------------------------------------ |
| 80 |
-- Kleene star |
| 81 |
|
| 82 |
infix 7 _⋆ |
| 83 |
|
| 84 |
_⋆ : Exp → Exp |
| 85 |
e ⋆ with is-∅ e | is-ε e |
| 86 |
... | yes _ | _ = R.ε |
| 87 |
... | _ | yes _ = R.ε |
| 88 |
... | _ | _ = e R.⋆ |
| 89 |
|
| 90 |
⋆-sound : ∀ {w} e → w ∈ (e ⋆) → w ∈ (e R.⋆) |
| 91 |
⋆-sound e p with is-∅ e | is-ε e |
| 92 |
... | yes refl | _ = star (sum (inj₁ p)) |
| 93 |
... | no _ | yes refl = star (sum (inj₁ p)) |
| 94 |
... | no _ | no _ = p |
| 95 |
|
| 96 |
⋆-complete : ∀ {w} e → w ∈ (e R.⋆) → w ∈ (e ⋆) |
| 97 |
⋆-complete e pr with is-∅ e | is-ε e |
| 98 |
... | yes refl | no _ = ∈∅⋆-inv pr |
| 99 |
... | no _ | yes refl = ∈ε⋆-inv pr |
| 100 |
... | no _ | no _ = pr |
| 101 |
|
| 102 |
------------------------------------------------------------------------ |
| 103 |
-- Derived notions: at least one and maybe one |
| 104 |
|
| 105 |
infixl 7 _+ _⁇ |
| 106 |
_+ : Exp → Exp |
| 107 |
e + = e ∙ e ⋆ |
| 108 |
|
| 109 |
_⁇ : Exp → Exp |
| 110 |
∅ ⁇ = ε |
| 111 |
e ⁇ = ε ∣ e |