theory Reg_demo_completed imports Main begin (* The aim of this demo is to: (1) define a datatype representing regular expressions (2) define the language expressed by a regular expression (3) define the reverse of a regular expression (4) prove that the language of the reverse of an expression e is equal to the reverse of the language of e. *) (* -------------------------------------------- *) (* Defining the datatype of regular expressions *) (* Define a datatype representing regular expressions, for instance the regular expression \ ( (ab|c)* ) Then define a few regular expressions, like the one above Remember the syntax: datatype 'a list = Nil | Cons 'a "'a list" Note: you can define notation for your constructors; for example Trans 'a 'b (infixr "[\]" 60) means that (Trans a b) can be noted (a [\] b) where the operator is infix, associates to the right, and is of priority 60. *) datatype regexp = Alternative regexp regexp (infixr "\" 50) | Char char ("<_>") | Concatenate regexp regexp (infixr "\" 60) | Negate regexp ("! _" 40) | Repeat regexp ("_ \" 40) | Empty ("\") | Single text \ What are the 50 and 60 above? They're precedence for the operators that resolve syntax conflicts, e.g. below where either the alternative or concat operation could be the inner one. Fiddle and/or consult the manual if it seems wrong. Could we do more with the CHR stuff? Maybe. We can definitely put numerals in every type. The Isabelle convention is that there is a syntactic class such as the class of types that support +, or support 0. \ term "(a \ b) \ c" term "(a \ b) \ c" term "Repeat (Concatenate (Char (CHR ''a'')) (Alternative ))" (* What about some simple derived reg-exps? *) definition repeat_one :: "regexp \ regexp" ("_ \") where "repeat_one r = Concatenate r (Repeat r)" definition all_regexp :: "regexp" where "all_regexp = Alternative Empty (Negate Empty)" definition null_regexp :: "regexp" where "null_regexp = Negate all_regexp" (* --------------------------------------------- *) (* Defining the language of a regular expression *) type_synonym word = "char list" (* Define a recursive function computing the language of a given regular expression: lang :: "regexp \ word set" Try it on small examples. Hint: You might need to define an inductive set. Recall the notation: inductive_set Fin :: "'a set set" where emptyI: "{} \ Fin" | insertI: "A \ Fin \ insert a A \ Fin" *) definition concat_languages :: "word set \ word set \ word set" where "concat_languages R S = {xs. \ys zs. xs = ys @ zs \ ys \ R \ zs \ S}" lemma concat_languagesE: "xs \ concat_languages R S \ (\ys zs. xs = ys @ zs \ ys \ R \ zs \ S \ Q) \ Q" by (auto simp: concat_languages_def) lemma concat_languages_appendI: "xs \ R \ ys \ S \ xs @ ys \ concat_languages R S" by (auto simp: concat_languages_def) inductive_set repeat_language :: "word set \ word set" for R where empty[simp, intro!]: "[] \ repeat_language R" | step: "xs \ R \ ys \ repeat_language R \ xs @ ys \ repeat_language R" primrec language :: "regexp \ word set" where "language Empty = {[]}" | "language (Negate r) = (- language r)" | "language (Alternative r s) = (language r) \ (language s)" | "language (Char c) = {[c]}" | "language Single = (\c. {[c]})" | "language (Concatenate r s) = concat_languages (language r) (language s)" | "language (Repeat r) = repeat_language (language r)" lemma language_all_regexp[simp]: "language all_regexp = UNIV" by (simp add: all_regexp_def) lemma language_null_regexp[simp]: "language null_regexp = {}" by (simp add: null_regexp_def) lemma language_null_regexpE[elim!]: "x \ language null_regexp \ P" by simp lemma "''abc'' \ language ( \ all_regexp)" by (simp add: concat_languages_def) text \ What about performance of sticking all these things in the simp-set? You can mostly trust that the simp-set lookup is using a "smart" order-log-n lookup and making the set big doesn't matter *too* much. \ lemma repeat_null_helper_lemma: "xs \ repeat_language {} \ xs = []" apply (induct rule: repeat_language.induct) apply simp apply simp done lemma "language (Repeat null_regexp) = language \" apply simp apply (safe elim!: repeat_null_helper_lemma) done (* -------------------------------------------- *) (* Defining the reverse of a regular expression *) primrec reverse :: "regexp \ regexp" where "reverse Empty = Empty" | "reverse (Negate r) = Negate (reverse r)" | "reverse (Alternative r s) = Alternative (reverse r) (reverse s)" | "reverse (Char c) = (Char c)" | "reverse (Concatenate r s) = Concatenate (reverse s) (reverse r)" | "reverse (Repeat r) = Repeat (reverse r)" | "reverse Single = Single" (* ------------------------------------------------------- *) (* Proof that the language of the reverse of an expression e is equal to the reverse of the language of e "lang (rrev e) = rev ` (lang e)" *) lemma rev_concat_languages: "rev ` concat_languages S T = concat_languages (rev ` T) (rev ` S)" apply (simp add: concat_languages_def) apply (fastforce intro: rev_image_eqI) done lemma rev_bij: "bij rev" by (metis involuntory_imp_bij rev_rev_ident) lemma repeat_language_append: "xs \ repeat_language R \ ys \ repeat_language R \ xs @ ys \ repeat_language R" apply (induct rule: repeat_language.induct) apply simp apply (simp add: repeat_language.step) done lemmas repeat_language_single = repeat_language.step[where ys="[]", simplified] lemma repeat_language_rev_imp: "xs \ repeat_language (rev ` R) \ xs \ rev ` repeat_language R" apply (induct rule: repeat_language.induct) apply simp apply clarsimp apply (rule image_eqI) apply (rule rev_is_rev_conv[THEN iffD1]) apply simp apply (rule repeat_language_append) apply simp apply (rule repeat_language_single) apply simp done lemma repeat_language_rev_imp2: "xs \ repeat_language R \ xs \ rev ` repeat_language (rev ` R)" apply (induct rule: repeat_language.induct) apply simp apply clarsimp apply (rule image_eqI) apply (rule rev_is_rev_conv[THEN iffD1]) apply simp apply (rule repeat_language_append) apply simp apply (rule repeat_language_single) apply simp done lemma repeat_language_rev_eq: "repeat_language (rev ` R) = rev ` repeat_language R" by (auto dest: repeat_language_rev_imp2 simp: image_comp) lemma "language (reverse r) = rev ` (language r)" apply (induct r) apply (simp_all add: image_Un rev_concat_languages) apply (simp add: bij_image_Compl_eq rev_bij) apply (simp add: repeat_language_rev_eq) apply (safe, simp_all) done primrec no_negate :: "regexp \ bool" where "no_negate Empty = True" | "no_negate (Negate r) = False" | "no_negate (Alternative r s) = (no_negate r \ no_negate s)" | "no_negate (Char c) = True" | "no_negate (Concatenate r s) = (no_negate r \ no_negate s)" | "no_negate (Repeat r) = no_negate r" | "no_negate Single = True" primrec matches_empty :: "regexp \ bool" where "matches_empty Empty = True" | "matches_empty (Negate r) = (\ matches_empty r)" | "matches_empty (Alternative r s) = (matches_empty r \ matches_empty s)" | "matches_empty (Char c) = False" | "matches_empty (Concatenate r s) = (matches_empty r \ matches_empty s)" | "matches_empty (Repeat r) = True" | "matches_empty Single = False" primrec regexp_step :: "regexp \ char \ regexp" where "regexp_step Empty c = null_regexp" | "regexp_step (Char c) d = (if c = d then Empty else null_regexp)" | "regexp_step (Alternative r s) c = Alternative (regexp_step r c) (regexp_step s c)" | "regexp_step (Negate r) c = null_regexp" | "regexp_step (Concatenate r s) c = (Alternative (if matches_empty r then regexp_step s c else null_regexp) (Concatenate (regexp_step r c) s))" | "regexp_step (Repeat r) c = Concatenate (regexp_step r c) (Repeat r)" | "regexp_step Single c = Empty" lemma matches_empty_equiv: "matches_empty r = ([] \ language r)" by (induct r, simp_all add: concat_languages_def) lemma repeat_language_nonempty_cases: "xs \ repeat_language R \ xs \ [] \ \ys zs. ys \ [] \ ys \ R \ zs \ repeat_language R \ xs = ys @ zs" apply (induct rule: repeat_language.induct) apply simp apply auto done lemma regexp_step: "no_negate r \ language (regexp_step r x) = {xs. x # xs \ language r}" apply (induct r, simp_all) apply auto[1] apply (clarsimp simp: matches_empty_equiv) apply (safe elim!: concat_languagesE, simp_all add: concat_languages_appendI[where xs="x # xs" for x xs, simplified] concat_languages_appendI)[1] apply (fastforce dest: concat_languages_appendI) apply (auto simp: Cons_eq_append_conv concat_languages_appendI concat_languages_appendI[where xs="x # xs" for x xs, simplified])[2] apply (safe elim!: concat_languagesE) apply (simp add: repeat_language_append[where xs="x # xs" for x xs, simplified] repeat_language_single) apply (drule repeat_language_nonempty_cases) apply simp apply clarsimp apply (clarsimp simp: Cons_eq_append_conv) apply (simp add: concat_languages_appendI) done end