Skip to content
This repository was archived by the owner on Jul 24, 2024. It is now read-only.

Commit f496ef4

Browse files
committed
feat(computability/{language/regular_expressions): Map along a function (#13197)
Define `language.map` and `regular_expression.map`. Co-authored-by: Fox Thomson
1 parent 7ece83e commit f496ef4

File tree

2 files changed

+69
-5
lines changed

2 files changed

+69
-5
lines changed

src/computability/language.lean

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ The operations in this file define a [Kleene algebra](https://en.wikipedia.org/w
1515
over the languages.
1616
-/
1717

18-
universes u v
19-
2018
open list set
2119

22-
variables {α : Type u}
20+
universes v
21+
22+
variables {α β γ : Type*}
2323

2424
/-- A language is a set of strings over an alphabet. -/
2525
@[derive [has_mem (list α), has_singleton (list α), has_insert (list α), complete_boolean_algebra]]
@@ -87,6 +87,18 @@ instance : semiring (language α) :=
8787

8888
@[simp] lemma add_self (l : language α) : l + l = l := sup_idem
8989

90+
/-- Maps the alphabet of a language. -/
91+
def map (f : α → β) : language α →+* language β :=
92+
{ to_fun := image (list.map f),
93+
map_zero' := image_empty _,
94+
map_one' := image_singleton,
95+
map_add' := image_union _,
96+
map_mul' := λ _ _, image_image2_distrib $ map_append _ }
97+
98+
@[simp] lemma map_id (l : language α) : map id l = l := by simp [map]
99+
@[simp] lemma map_map (g : β → γ) (f : α → β) (l : language α) : map g (map f l) = map (g ∘ f) l :=
100+
by simp [map, image_image]
101+
90102
lemma star_def_nonempty (l : language α) :
91103
l.star = {x | ∃ S : list (list α), x = S.join ∧ ∀ y ∈ S, y ∈ l ∧ y ≠ []} :=
92104
begin
@@ -155,6 +167,13 @@ begin
155167
{ rintro ⟨_, S, rfl, rfl, hS⟩, exact ⟨S, rfl, hS⟩ }
156168
end
157169

170+
@[simp] lemma map_star (f : α → β) (l : language α) : map f (star l) = star (map f l) :=
171+
begin
172+
rw [star_eq_supr_pow, star_eq_supr_pow],
173+
simp_rw ←map_pow,
174+
exact image_Union,
175+
end
176+
158177
lemma mul_self_star_comm (l : language α) : l.star * l = l * l.star :=
159178
by simp only [star_eq_supr_pow, mul_supr, supr_mul, ← pow_succ, ← pow_succ']
160179

src/computability/regular_expressions.lean

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@ computer science such as the POSIX standard.
1919
* `attribute [pattern] has_mul.mul` has been added into this file, it could be moved.
2020
-/
2121

22+
open list set
23+
2224
universe u
2325

24-
variables {α : Type u} [dec : decidable_eq α]
26+
variablesβ γ : Type*} [dec : decidable_eq α]
2527

2628
/--
2729
This is the definition of regular expressions. The names used here is to mirror the definition
@@ -60,7 +62,7 @@ attribute [pattern] has_mul.mul
6062
@[simp] lemma comp_def (P Q : regular_expression α) : comp P Q = P * Q := rfl
6163

6264
/-- `matches P` provides a language which contains all strings that `P` matches -/
63-
def matches : regular_expression α → language α
65+
@[simp] def matches : regular_expression α → language α
6466
| 0 := 0
6567
| 1 := 1
6668
| (char a) := {[a]}
@@ -313,4 +315,47 @@ begin
313315
exact eq.decidable _ _
314316
end
315317

318+
omit dec
319+
320+
/-- Map the alphabet of a regular expression. -/
321+
@[simp] def map (f : α → β) : regular_expression α → regular_expression β
322+
| 0 := 0
323+
| 1 := 1
324+
| (char a) := char (f a)
325+
| (R + S) := map R + map S
326+
| (R * S) := map R * map S
327+
| (star R) := star (map R)
328+
329+
@[simp] lemma map_id : ∀ (P : regular_expression α), P.map id = P
330+
| 0 := rfl
331+
| 1 := rfl
332+
| (char a) := rfl
333+
| (R + S) := by simp_rw [map, map_id]
334+
| (R * S) := by simp_rw [map, map_id]
335+
| (star R) := by simp_rw [map, map_id]
336+
337+
@[simp] lemma map_map (g : β → γ) (f : α → β) :
338+
∀ (P : regular_expression α), (P.map f).map g = P.map (g ∘ f)
339+
| 0 := rfl
340+
| 1 := rfl
341+
| (char a) := rfl
342+
| (R + S) := by simp_rw [map, map_map]
343+
| (R * S) := by simp_rw [map, map_map]
344+
| (star R) := by simp_rw [map, map_map]
345+
346+
/-- The language of the map is the map of the language. -/
347+
@[simp] lemma matches_map (f : α → β) :
348+
∀ P : regular_expression α, (P.map f).matches = language.map f P.matches
349+
| 0 := (map_zero _).symm
350+
| 1 := (map_one _).symm
351+
| (char a) := by { rw eq_comm, exact image_singleton }
352+
| (R + S) := by simp only [matches_map, map, matches_add, map_add]
353+
| (R * S) := by simp only [matches_map, map, matches_mul, map_mul]
354+
| (star R) := begin
355+
simp_rw [map, matches, matches_map],
356+
rw [language.star_eq_supr_pow, language.star_eq_supr_pow],
357+
simp_rw ←map_pow,
358+
exact image_Union.symm,
359+
end
360+
316361
end regular_expression

0 commit comments

Comments
 (0)