Debian Codegraph @

src/Text/Regex/Derivative/Brzozowski.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Regular expressions: Brzozowski derivative
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --cubical-compatible --safe #-}
8
9 open import Relation.Binary.Bundles using (DecPoset)
10
11 module Text.Regex.Derivative.Brzozowski {a e r} (P? : DecPoset a e r) where
12
13 open import Data.List.Base using (List; []; _∷_)
14 open import Data.List.Relation.Binary.Equality.Propositional
15 open import Data.Sum.Base as Sum using (inj₁; inj₂)
16
17 open import Function.Base using (_$_; _∘′_; case_of_)
18 open import Relation.Nullary.Decidable using (Dec; yes; no)
19 open import Relation.Nullary.Negation using (contradiction)
20 open import Relation.Nullary.Decidable using (map′; ¬?)
21 open import Relation.Binary.Definitions using (Decidable)
22 open import Relation.Binary.PropositionalEquality.Core using (_≡_; refl; cong)
23
24 open DecPoset P? using (preorder) renaming (Carrier to A)
25 open import Text.Regex.Base preorder as R hiding (_∣_; _∙_; _⋆)
26 open import Text.Regex.Properties P?
27 open import Text.Regex.SmartConstructors preorder
28
29 open import Data.List.Relation.Ternary.Appending.Propositional {A = A}
30 open import Data.List.Relation.Ternary.Appending.Propositional.Properties {A = A}
31
32 ------------------------------------------------------------------------
33 -- Action of characters on regular expressions
34
35 private
36 decToExp : ∀ {p} {P : Set p} → Dec P → Exp
37 decToExp (yes _) = ε
38 decToExp (no _) = ∅
39
40 eat : A → Exp → Exp
41 eat a ε = ∅
42 eat a [ rs ] = decToExp ((a ∷ []) ∈?[ rs ])
43 eat a [^ rs ] = decToExp ((a ∷ []) ∈?[^ rs ])
44 eat a (e R.∣ f) = eat a e ∣ eat a f
45 eat a (e R.∙ f) = case []∈? e of λ where
46 (yes _) → (eat a e ∙ f) ∣ (eat a f)
47 (no ¬p) → eat a e ∙ f
48 eat a (e R.⋆) = eat a e ∙ (e ⋆)
49
50 ------------------------------------------------------------------------
51 -- This action is sound and complete with respect to matching
52
53 eat-sound : ∀ x {xs} e → xs ∈ eat x e → (x ∷ xs) ∈ e
54 eat-sound x ε pr = contradiction pr ∉∅
55 eat-sound x [ rs ] pr with (x ∷ []) ∈?[ rs ]
56 ... | yes p = case pr of λ where ε → p
57 ... | no _ = contradiction pr ∉∅
58 eat-sound x [^ rs ] pr with (x ∷ []) ∈?[^ rs ]
59 ... | yes p = case pr of λ where ε → p
60 ... | no _ = contradiction pr ∉∅
61 eat-sound x (e R.∣ f) pr with ∣-sound (eat x e) (eat x f) pr
62 ... | sum pr′ = sum $ Sum.map (eat-sound x e) (eat-sound x f) pr′
63 eat-sound x (e R.∙ f) pr with []∈? e
64 ... | yes []∈e with ∣-sound (eat x e ∙ f) (eat x f) pr
65 ... | sum (inj₂ pr') = prod ([] ++ _) []∈e (eat-sound x f pr')
66 ... | sum (inj₁ pr') with ∙-sound (eat x e) f pr'
67 ... | prod eq p q = prod (refl ∷ eq) (eat-sound x e p) q
68 eat-sound x (e R.∙ f) pr | no ¬p with ∙-sound (eat x e) f pr
69 ... | prod eq p q = prod (refl ∷ eq) (eat-sound x e p) q
70 eat-sound x (e R.⋆) pr with ∙-sound (eat x e) (e ⋆) pr
71 ... | prod eq p q =
72 star (sum (inj₂ (prod (refl ∷ eq) (eat-sound x e p) (⋆-sound e q))))
73
74 eat-complete′ : ∀ x {xs w} e → w ≋ (x ∷ xs) → w ∈ e → xs ∈ eat x e
75 eat-complete : ∀ x {xs} e → (x ∷ xs) ∈ e → xs ∈ eat x e
76
77 eat-complete x e = eat-complete′ x e ≋-refl
78
79 eat-complete′ x [ rs ] (refl ∷ []) [ p ]
80 with (x ∷ []) ∈?[ rs ]
81 ... | yes _ = ε
82 ... | no ¬p = contradiction [ p ] ¬p
83 eat-complete′ x [^ rs ] (refl ∷ []) [^ p ]
84 with (x ∷ []) ∈?[^ rs ]
85 ... | yes _ = ε
86 ... | no ¬p = contradiction [^ p ] ¬p
87 eat-complete′ x (e R.∣ f) eq (sum p) =
88 ∣-complete (eat x e) (eat x f) $ sum $
89 Sum.map (eat-complete′ x e eq) (eat-complete′ x f eq) p
90 eat-complete′ x (e R.∙ f) eq p with []∈? e
91 eat-complete′ x (e R.∙ f) (refl ∷ eq) (prod ([]++ _) p q) | no []∉e
92 = contradiction p []∉e
93 eat-complete′ x (e R.∙ f) (refl ∷ eq) (prod (refl ∷ app) p q) | no []∉e
94 = ∙-complete (eat x e) f (prod (respʳ-≋ app eq) (eat-complete x e p) q)
95 eat-complete′ x (e R.∙ f) eq (prod ([]++ eq′) p q) | yes []∈e
96 = ∣-complete (eat x e ∙ f) (eat x f) $ sum $ inj₂
97 $ eat-complete′ x f (≋-trans eq′ eq) q
98 eat-complete′ x (e R.∙ f) (refl ∷ eq) (prod (refl ∷ app) p q) | yes []∈e
99 = ∣-complete (eat x e ∙ f) (eat x f) $ sum $ inj₁
100 $ ∙-complete (eat x e) f (prod (respʳ-≋ app eq) (eat-complete x e p) q)
101 eat-complete′ x (e R.⋆) eq (star (sum (inj₂ (prod ([]++ app) p q))))
102 = eat-complete′ x (e R.⋆) (≋-trans app eq) q
103 eat-complete′ x (e R.⋆) (refl ∷ eq) (star (sum (inj₂ (prod (refl ∷ app) p q))))
104 = ∙-complete (eat x e) (e ⋆) $ prod (respʳ-≋ app eq) (eat-complete x e p)
105 $ ⋆-complete e q
106
107 ------------------------------------------------------------------------
108 -- Consequence: matching is decidable
109
110 infix 4 _∈?_ _∉?_
111
112 _∈?_ : Decidable _∈_
113 [] ∈? e = []∈? e
114 (x ∷ xs) ∈? e = map′ (eat-sound x e) (eat-complete x e) (xs ∈? eat x e)
115
116 _∉?_ : Decidable _∉_
117 xs ∉? e = ¬? (xs ∈? e)