Debian Codegraph @

src/Text/Regex/Base.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Regular expressions: basic types and semantics
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --cubical-compatible --safe #-}
8
9 open import Relation.Binary.Bundles using (Preorder)
10
11 module Text.Regex.Base {a e r} (P : Preorder a e r) where
12
13 open import Level using (_⊔_)
14
15 open import Data.Bool.Base using (Bool)
16 open import Data.List.Base as L using (List; []; _∷_)
17 open import Data.List.Relation.Unary.Any using (Any)
18 open import Data.List.Relation.Unary.All using (All)
19 open import Data.Sum.Base using (_⊎_)
20
21 open import Relation.Nullary.Negation.Core using (¬_)
22
23 open Preorder P using (_≈_) renaming (Carrier to A; _∼_ to _≤_)
24 open import Data.List.Relation.Ternary.Appending.Propositional {A = A}
25
26 ------------------------------------------------------------------------
27 -- Regular expressions on the alphabet A
28
29 infix 10 [_] _─_
30 data Range : Set a where
31 [_] : (a : A) → Range
32 _─_ : (lb ub : A) → Range
33
34 infixr 5 _∣_
35 infixr 6 _∙_
36 infixl 7 _⋆
37 infix 10 [^_]
38
39 data Exp : Set a where
40 ε : Exp
41 [_] : (rs : List Range) → Exp
42 [^_] : (rs : List Range) → Exp
43 _∣_ : (e f : Exp) → Exp
44 _∙_ : (e f : Exp) → Exp
45 _⋆ : (e : Exp) → Exp
46
47 -- A regular expression has additional parameters:
48 -- * should the match begin at the very start of the input?
49 -- * should it span until the very end?
50
51 record Regex : Set a where
52 field
53 fromStart : Bool
54 tillEnd : Bool
55 expression : Exp
56
57 updateExp : (Exp → Exp) → Regex → Regex
58 updateExp f r = record r { expression = f (Regex.expression r) }
59
60 ------------------------------------------------------------------------
61 -- Derived notions: nothing, anything, and singleton
62
63 pattern ∅ = [ List.[] ]
64 pattern · = [^ List.[] ]
65
66 pattern singleton a = [ Range.[ a ] ∷ [] ]
67
68 ------------------------------------------------------------------------
69 -- Semantics: matching words
70
71 infix 4 _∈ᴿ_ _∉ᴿ_
72 data _∈ᴿ_ (c : A) : Range → Set (a ⊔ r ⊔ e) where
73 [_] : ∀ {val} → c ≈ val → c ∈ᴿ [ val ]
74 _─_ : ∀ {lb ub} → lb ≤ c → c ≤ ub → c ∈ᴿ (lb ─ ub)
75
76 _∉ᴿ_ : A → Range → Set (a ⊔ r ⊔ e)
77 a ∉ᴿ r = ¬ (a ∈ᴿ r)
78
79 infix 4 _∈_ _∉_
80 data _∈_ : List A → Exp → Set (a ⊔ r ⊔ e) where
81 ε : [] ∈ ε
82 [_] : ∀ {a rs} → Any (a ∈ᴿ_) rs → L.[ a ] ∈ [ rs ]
83 [^_] : ∀ {a rs} → All (a ∉ᴿ_) rs → L.[ a ] ∈ [^ rs ]
84 sum : ∀ {w e f} → (w ∈ e) ⊎ (w ∈ f) → w ∈ (e ∣ f)
85 prod : ∀ {v w vw e f} → Appending v w vw → v ∈ e → w ∈ f → vw ∈ (e ∙ f)
86 star : ∀ {v e} → v ∈ (ε ∣ e ∙ (e ⋆)) → v ∈ (e ⋆)
87
88 _∉_ : List A → Exp → Set (a ⊔ r ⊔ e)
89 w ∉ e = ¬ (w ∈ e)