|
| 1 | +import Mathlib.Tactic.SplitIfs |
| 2 | +import Mathlib.Tactic.CongrM |
| 3 | + |
| 4 | +import Katydid.Regex.Language |
| 5 | +import Katydid.Regex.Regex |
| 6 | + |
| 7 | +namespace Denote |
| 8 | + |
| 9 | +def denote {α: Type} (r: Regex α): Language.Lang α := |
| 10 | + match r with |
| 11 | + | Regex.emptyset => Language.emptyset |
| 12 | + | Regex.emptystr => Language.emptystr |
| 13 | + | Regex.char a => Language.char a |
| 14 | + | Regex.or x y => Language.or (denote x) (denote y) |
| 15 | + | Regex.concat x y => Language.concat (denote x) (denote y) |
| 16 | + | Regex.star x => Language.star (denote x) |
| 17 | + |
| 18 | +private theorem decide_prop (P: Prop) [dP: Decidable P]: |
| 19 | + (P <-> True) \/ (P <-> False) := by |
| 20 | + match dP with |
| 21 | + | isTrue hp => |
| 22 | + left |
| 23 | + simp only [iff_true] |
| 24 | + exact hp |
| 25 | + | isFalse hp => |
| 26 | + right |
| 27 | + simp only [iff_false] |
| 28 | + exact hp |
| 29 | + |
| 30 | +private theorem decide_eq (x y: α) [dα: DecidableEq α]: |
| 31 | + (x == y) = true <-> (x = y) := by |
| 32 | + apply beq_iff_eq |
| 33 | + |
| 34 | +def denote_onlyif {α: Type} [DecidableEq α] (condition: Prop) [dcond: Decidable condition] (r: Regex α): |
| 35 | + denote (Regex.onlyif condition r) = Language.onlyif condition (denote r) := by |
| 36 | + unfold Language.onlyif |
| 37 | + unfold Regex.onlyif |
| 38 | + funext xs |
| 39 | + have hc : (condition <-> True) \/ (condition <-> False) := |
| 40 | + decide_prop condition |
| 41 | + cases hc with |
| 42 | + | inl ctrue => |
| 43 | + split_ifs |
| 44 | + case pos hc' => |
| 45 | + rw [ctrue] |
| 46 | + simp |
| 47 | + case neg hc' => |
| 48 | + rw [ctrue] at hc' |
| 49 | + contradiction |
| 50 | + | inr cfalse => |
| 51 | + split_ifs |
| 52 | + case neg hc' => |
| 53 | + rw [cfalse] |
| 54 | + simp [denote] |
| 55 | + case pos hc' => |
| 56 | + rw [cfalse] at hc' |
| 57 | + contradiction |
| 58 | + |
| 59 | +theorem null_commutes {α: Type} (r: Regex α): |
| 60 | + ((Regex.null r) = true) = Language.null (denote r) := by |
| 61 | + induction r with |
| 62 | + | emptyset => |
| 63 | + unfold denote |
| 64 | + rw [Language.null_emptyset] |
| 65 | + unfold Regex.null |
| 66 | + apply Bool.false_eq_true |
| 67 | + | emptystr => |
| 68 | + unfold denote |
| 69 | + rw [Language.null_emptystr] |
| 70 | + unfold Regex.null |
| 71 | + simp only |
| 72 | + | char a => |
| 73 | + unfold denote |
| 74 | + rw [Language.null_char] |
| 75 | + unfold Regex.null |
| 76 | + apply Bool.false_eq_true |
| 77 | + | or p q ihp ihq => |
| 78 | + unfold denote |
| 79 | + rw [Language.null_or] |
| 80 | + unfold Regex.null |
| 81 | + rw [<- ihp] |
| 82 | + rw [<- ihq] |
| 83 | + rw [Bool.or_eq_true] |
| 84 | + | concat p q ihp ihq => |
| 85 | + unfold denote |
| 86 | + rw [Language.null_concat] |
| 87 | + unfold Regex.null |
| 88 | + rw [<- ihp] |
| 89 | + rw [<- ihq] |
| 90 | + rw [Bool.and_eq_true] |
| 91 | + | star r ih => |
| 92 | + unfold denote |
| 93 | + rw [Language.null_star] |
| 94 | + unfold Regex.null |
| 95 | + simp only |
| 96 | + |
| 97 | +theorem derive_commutes {α: Type} [dα: DecidableEq α] (r: Regex α) (x: α): |
| 98 | + denote (Regex.derive r x) = Language.derive (denote r) x := by |
| 99 | + induction r with |
| 100 | + | emptyset => |
| 101 | + simp only [denote] |
| 102 | + rw [Language.derive_emptyset] |
| 103 | + | emptystr => |
| 104 | + simp only [denote] |
| 105 | + rw [Language.derive_emptystr] |
| 106 | + | char a => |
| 107 | + simp only [denote] |
| 108 | + rw [Language.derive_char] |
| 109 | + unfold Regex.derive |
| 110 | + rw [denote_onlyif] |
| 111 | + simp only [denote] |
| 112 | + rw [beq_iff_eq] |
| 113 | + | or p q ihp ihq => |
| 114 | + simp only [denote] |
| 115 | + rw [Language.derive_or] |
| 116 | + unfold Language.or |
| 117 | + rw [ihp] |
| 118 | + rw [ihq] |
| 119 | + | concat p q ihp ihq => |
| 120 | + simp only [denote] |
| 121 | + rw [Language.derive_concat] |
| 122 | + rw [<- ihp] |
| 123 | + rw [<- ihq] |
| 124 | + rw [denote_onlyif] |
| 125 | + congrm (Language.or (Language.concat (denote (Regex.derive p x)) (denote q)) ?_) |
| 126 | + rw [null_commutes] |
| 127 | + | star r ih => |
| 128 | + simp only [denote] |
| 129 | + rw [Language.derive_star] |
| 130 | + guard_target = |
| 131 | + Language.concat (denote (Regex.derive r x)) (Language.star (denote r)) |
| 132 | + = Language.concat (Language.derive (denote r) x) (Language.star (denote r)) |
| 133 | + congrm ((Language.concat ?_ (Language.star (denote r)))) |
| 134 | + guard_target = denote (r.derive x) = Language.derive (denote r) x |
| 135 | + exact ih |
0 commit comments