Debian Codegraph @

doc/README/Text/Regex.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Examples of regular expressions and matching
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --with-K #-}
8
9 module README.Text.Regex where
10
11 open import Data.Bool using (true; false)
12 open import Data.List.Base using (_∷_; [])
13 open import Data.String
14 open import Function.Base using () renaming (_$′_ to _$_)
15 open import Relation.Nullary.Decidable using (yes)
16 open import Relation.Nullary.Decidable using (True; False; from-yes)
17
18 -- Our library available via the Text.Regex module is safe but it works on
19 -- lists of characters.
20
21 -- To use it on strings we have to rely on unsafe theorems about the
22 -- conversions between strings and lists of characters being inverses.
23 -- For convenience we use the following unsafe module for this README.
24 open import Text.Regex.String.Unsafe
25
26 ------------------------------------------------------------------------
27 -- Defining regular expressions
28
29 -- The type of regular expressions is Exp.
30
31 -- Some examples of regular expressions using:
32
33 -- [_] for the union of ranges it contains
34 -- _─_ for a range
35 -- singleton for an exact character
36 -- _∙_ for the concatenation of two regular expressions
37 -- _∣_ for the sum of two regular expressions
38 -- _⋆ for the Kleene star (zero or more matches of the regular expression)
39 -- _⁇ for an optional regular expression
40
41 ℕ* : Exp
42 ℕ* = [ '1' ─ '9' ∷ [] ] -- a non-zero digit
43 ∙ [ '0' ─ '9' ∷ [] ] ⋆ -- followed by zero or more digits
44
45 ℕ : Exp
46 ℕ = ℕ* ∣ singleton '0' -- ℕ* or exactly 0
47
48 ℤ : Exp
49 ℤ = ((singleton '-') ⁇ ∙ ℕ*) -- an optional minus sign followed by a ℕ*
50 ∣ singleton '0' -- or exactly 0
51
52 ------------------------------------------------------------------------
53 -- An expression's semantics
54
55 -- The semantics of these regular expression is defined in terms of the
56 -- lists of characters they match. The type (str ∈ e) states that the
57 -- string str matches the expression e.
58
59 -- It is decidable, and the proof is called _∈?_.
60 -- We can run it on a few examples to check that it matches our intuition:
61
62 -- Valid: starts with a non-zero digit, followed by 3 digits
63 _ : True ("1848" ∈? ℕ*)
64 _ = _
65
66 -- Valid: exactly 0
67 _ : True ("0" ∈? ℕ)
68 _ = _
69
70 -- Invalid: starts with a leading 0
71 _ : False ("007" ∈? ℕ)
72 _ = _
73
74 -- Invalid: no negative ℕ number
75 _ : False ("-666" ∈? ℕ)
76 _ = _
77
78 -- Valid: a negative integer
79 _ : True ("-666" ∈? ℤ)
80 _ = _
81
82 -- Invalid: no negative 0
83 _ : False ("-0" ∈? ℤ)
84 _ = _
85
86 ------------------------------------------------------------------------
87 -- Matching algorithms
88
89 -- The proof that _∈_ is decidable gives us the ability to check whether
90 -- a whole string matches a regular expression. But we may want to use
91 -- other matching algorithms detecting a prefix, infix, or suffix of the
92 -- input string that matches the regular expression.
93
94 -- This is what the Regex type gives us.
95
96 -- For instance, the following value corresponds to finding an infix
97 -- substring matching the string "agda" or "agdai"
98
99 agda : Exp
100 agda = singleton 'a'
101 ∙ singleton 'g'
102 ∙ singleton 'd'
103 ∙ singleton 'a'
104 ∙ (singleton 'i' ⁇)
105
106 infixAgda : Regex
107 infixAgda = record
108 { fromStart = false
109 ; tillEnd = false
110 ; expression = agda
111 }
112
113 -- The search function gives us the ability to look for matches
114
115 -- Valid: agda in the middle
116 _ : True (search "Maria Magdalena" infixAgda)
117 _ = _
118
119 -- By changing the value of fromStart and tillEnd we can control where the
120 -- substring should be. We can insist on the match being at the end of the
121 -- input for instance:
122
123 suffixAgda : Regex
124 suffixAgda = record
125 { fromStart = false
126 ; tillEnd = true
127 ; expression = agda
128 }
129
130 -- Invalid: agda is in the middle
131 _ : False (search "Maria Magdalena" suffixAgda)
132 _ = _
133
134 -- Valid: agda as a suffix
135 _ : True (search "README.agda" suffixAgda)
136 _ = _
137
138 -- Valid: agdai as a suffix
139 _ : True (search "README.agdai" suffixAgda)
140 _ = _
141
142
143 ------------------------------------------------------------------------
144 -- Advanced uses
145
146 -- Search does not just return a boolean, it returns an informative answer.
147 -- Infix matches are for instance represented using the `Infix` relation on
148 -- list. Such a proof pinpoints the exact position of the match:
149
150 open import Data.List.Relation.Binary.Infix.Heterogeneous
151 open import Data.List.Relation.Binary.Infix.Heterogeneous.Properties
152 open import Data.List.Relation.Binary.Pointwise using (≡⇒Pointwise-≡)
153 open import Relation.Binary.PropositionalEquality
154
155 -- Here is an example of a match: it gives back the substring, the inductive
156 -- proof that it is accepted by the regular expression and its precise location
157 -- inside the input string
158 mariamAGDAlena : Match "Maria Magdalena" infixAgda
159 mariamAGDAlena = record
160 { string = "agda" -- we have found "agda"
161 ; match = from-yes ("agda" ∈? agda) -- a proof of the match
162 ; related = proof -- and its location
163 }
164
165 where
166
167 proof : Infix _≡_ (toList "agda") (toList "Maria Magdalena")
168 proof = toList "Maria M"
169 ++ⁱ fromPointwise (≡⇒Pointwise-≡ refl)
170 ⁱ++ toList "lena"
171
172
173 -- And here is the proof that search returns such an object
174 _ : search "Maria Magdalena" infixAgda ≡ yes mariamAGDAlena
175 _ = refl