theory Reg imports Main begin datatype regexp = Atom char | Alt regexp regexp | Conc regexp regexp (infix "\" 60) | Star regexp ("_*" [100] 100) | Neg regexp definition "Plus r \ r \ r*" definition "U \ Alt (Atom CHR ''x'') (Neg (Atom CHR ''x''))" definition "Null = Neg U" definition "\ \ Null*" definition "Maybe r \ Alt \ r" primrec String :: "char list \ regexp" where "String [] = \" | "String (c#cs) = Atom c \ String cs" primrec Rev :: "regexp \ regexp" where "Rev (Atom c) = Atom c" | "Rev (Alt a b) = Alt (Rev a) (Rev b)" | "Rev (a \ b) = Rev b \ Rev a" | "Rev (a*) = (Rev a)*" | "Rev (Neg a) = Neg (Rev a)" type_synonym word = "char list" definition conc :: "word set \ word set \ word set" where "conc A B = {xs @ ys | xs ys. xs \ A \ ys \ B}" inductive_set star :: "word set \ word set" for L where star_e [simp,intro!]: "[] \ star L" | star_step: "ys \ L \ xs \ star L \ xs @ ys \ star L" primrec lang :: "regexp \ word set" where "lang (Atom c) = { [c] }" | "lang (Alt a b) = lang a \ lang b" | "lang (a \ b) = conc (lang a) (lang b)" | "lang (a*) = star (lang a)" | "lang (Neg a) = -lang a" lemma U[simp]: "lang U = UNIV" apply (simp add: U_def) apply auto done lemma Null[simp]: "lang Null = {}" apply (simp add: Null_def) done lemma star_empty: "x \ star {} \ x = []" apply (induct rule: star.induct) apply simp apply clarsimp done lemma \[simp]: "lang \ = {[]}" apply (simp add: \_def) apply (rule set_eqI) apply (rule iffI) prefer 2 apply clarsimp apply clarsimp apply (erule star_empty) done lemma "lang (String ''xyz'') = {''xyz''}" apply (simp add: conc_def) done instantiation regexp :: "{zero,one,plus}" begin definition [simp]: "0 \ Null" definition [simp]: "1 \ \" definition [simp]: "a + b \ Alt a b" instance .. end lemma "lang (a + 0) = lang a" apply simp done lemma "lang (x \ 1) = lang x" apply simp apply (unfold conc_def) apply simp done lemma "lang (x + y) = lang (y + x)" apply simp apply auto done lemma "lang (a \ (b \ c)) = lang ((a \ b) \ c)" apply (clarsimp simp: conc_def) apply safe apply (rule_tac x="xs @ xsa" in exI) apply clarsimp apply auto[1] apply (rule_tac x="xsa" in exI) apply auto done lemma conc_rev[simp]: "rev ` conc A B = conc (rev ` B) (rev ` A)" apply (rule equalityI) apply (fastforce simp: conc_def) apply (clarsimp simp: conc_def) apply (rule_tac x="xb@xa" in image_eqI) apply simp apply auto done lemma star_one: "xs \ L \ xs \ star L" by (rule star_step [where xs="[]", simplified]) lemma star_append: "xs \ star L \ ys \ L \ ys @ xs \ star L" apply (induct rule: star.induct) apply (simp add: star_one) apply clarsimp apply (drule_tac xs="ys @ xs" in star_step, assumption) apply simp done lemma star1 [intro!]: "x \ star L \ rev x \ star (rev ` L)" apply (induct rule: star.induct) apply simp apply clarsimp apply (rule star_append) apply assumption apply clarsimp done lemma star2: "x \ star (rev ` L) \ x \ rev ` star L" apply (induct rule: star.induct) apply clarsimp apply clarsimp apply (rule_tac x="x@xa" in image_eqI) apply simp apply (erule (1) star_append) done lemma star_rev [simp]: "rev ` star L = star (rev ` L)" apply (rule equalityI) apply clarsimp apply (clarsimp simp: star2) done lemma bij_rev: "bij rev" apply (rule bijI) apply (rule injI) apply simp apply (rule_tac f=rev in surjI) apply simp done lemma "lang (Rev r) = rev ` lang r" apply (induct r) apply simp apply simp apply auto[1] apply simp apply simp apply (simp add: bij_image_Compl_eq bij_rev) done end