# HG changeset patch # User Christian Urban # Date 1382966384 0 # Node ID cfba674a8fdfcc3e1ceb2c8c7ef077b97bb7cdac # Parent ef48e378c44e78f430fb4560e4e0ed749931a0b1 added matcher diff -r ef48e378c44e -r cfba674a8fdf progs/Matcher.thy --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/progs/Matcher.thy Mon Oct 28 13:19:44 2013 +0000 @@ -0,0 +1,206 @@ +theory Matcher + imports "Main" +begin + +section {* Regular Expressions *} + +datatype rexp = + NULL +| EMPTY +| CHAR char +| SEQ rexp rexp +| ALT rexp rexp +| STAR rexp + + +section {* Sequential Composition of Sets *} + +definition + Seq :: "string set \ string set \ string set" ("_ ;; _" [100,100] 100) +where + "A ;; B = {s1 @ s2 | s1 s2. s1 \ A \ s2 \ B}" + +text {* Two Simple Properties about Sequential Composition *} + +lemma seq_empty [simp]: + shows "A ;; {[]} = A" + and "{[]} ;; A = A" +by (simp_all add: Seq_def) + +lemma seq_null [simp]: + shows "A ;; {} = {}" + and "{} ;; A = {}" +by (simp_all add: Seq_def) + +section {* Kleene Star for Sets *} + +inductive_set + Star :: "string set \ string set" ("_\" [101] 102) + for A :: "string set" +where + start[intro]: "[] \ A\" +| step[intro]: "\s1 \ A; s2 \ A\\ \ s1 @ s2 \ A\" + + +text {* A Standard Property of Star *} + +lemma star_cases: + shows "A\ = {[]} \ A ;; A\" +unfolding Seq_def +by (auto) (metis Star.simps) + +lemma star_decomp: + assumes a: "c # x \ A\" + shows "\a b. x = a @ b \ c # a \ A \ b \ A\" +using a +by (induct x\"c # x" rule: Star.induct) + (auto simp add: append_eq_Cons_conv) + + +section {* Semantics of Regular Expressions *} + +fun + L :: "rexp \ string set" +where + "L (NULL) = {}" +| "L (EMPTY) = {[]}" +| "L (CHAR c) = {[c]}" +| "L (SEQ r1 r2) = (L r1) ;; (L r2)" +| "L (ALT r1 r2) = (L r1) \ (L r2)" +| "L (STAR r) = (L r)\" + +section {* The Matcher *} + +fun + nullable :: "rexp \ bool" +where + "nullable (NULL) = False" +| "nullable (EMPTY) = True" +| "nullable (CHAR c) = False" +| "nullable (ALT r1 r2) = (nullable r1 \ nullable r2)" +| "nullable (SEQ r1 r2) = (nullable r1 \ nullable r2)" +| "nullable (STAR r) = True" + +fun + der :: "char \ rexp \ rexp" +where + "der c (NULL) = NULL" +| "der c (EMPTY) = NULL" +| "der c (CHAR c') = (if c = c' then EMPTY else NULL)" +| "der c (ALT r1 r2) = ALT (der c r1) (der c r2)" +| "der c (SEQ r1 r2) = ALT (SEQ (der c r1) r2) (if nullable r1 then der c r2 else NULL)" +| "der c (STAR r) = SEQ (der c r) (STAR r)" + +fun + ders :: "string \ rexp \ rexp" +where + "ders [] r = r" +| "ders (c # s) r = ders s (der c r)" + +fun + matcher :: "rexp \ string \ bool" +where + "matcher r s = nullable (ders s r)" + + +section {* Correctness Proof of the Matcher *} + +lemma nullable_correctness: + shows "nullable r \ [] \ (L r)" +by (induct r) (auto simp add: Seq_def) +section {* Left-Quotient of a Set *} + +definition + Der :: "char \ string set \ string set" +where + "Der c A \ {s. [c] @ s \ A}" + +lemma Der_null [simp]: + shows "Der c {} = {}" +unfolding Der_def +by auto + +lemma Der_empty [simp]: + shows "Der c {[]} = {}" +unfolding Der_def +by auto + +lemma Der_char [simp]: + shows "Der c {[d]} = (if c = d then {[]} else {})" +unfolding Der_def +by auto + +lemma Der_union [simp]: + shows "Der c (A \ B) = Der c A \ Der c B" +unfolding Der_def +by auto + +lemma Der_seq [simp]: + shows "Der c (A ;; B) = (Der c A) ;; B \ (if [] \ A then Der c B else {})" +unfolding Der_def Seq_def +by (auto simp add: Cons_eq_append_conv) + +lemma Der_star [simp]: + shows "Der c (A\) = (Der c A) ;; A\" +proof - + have "Der c (A\) = Der c ({[]} \ A ;; A\)" + by (simp only: star_cases[symmetric]) + also have "... = Der c (A ;; A\)" + by (simp only: Der_union Der_empty) (simp) + also have "... = (Der c A) ;; A\ \ (if [] \ A then Der c (A\) else {})" + by simp + also have "... = (Der c A) ;; A\" + unfolding Seq_def Der_def + by (auto dest: star_decomp) + finally show "Der c (A\) = (Der c A) ;; A\" . +qed + + +lemma der_correctness: + shows "L (der c r) = Der c (L r)" +by (induct r) + (simp_all add: nullable_correctness) + +lemma matcher_correctness: + shows "matcher r s \ s \ L r" +by (induct s arbitrary: r) + (simp_all add: nullable_correctness der_correctness Der_def) + +section {* Examples *} + +definition + "CHRA \ CHAR (CHR ''a'')" + +definition + "ALT1 \ ALT CHRA EMPTY" + +definition + "SEQ3 \ SEQ (SEQ ALT1 ALT1) ALT1" + +value "matcher SEQ3 ''aaa''" + +value "matcher NULL []" +value "matcher (CHAR (CHR ''a'')) [CHR ''a'']" +value "matcher (CHAR a) [a,a]" +value "matcher (STAR (CHAR a)) []" +value "matcher (STAR (CHAR a)) [a,a]" +value "matcher (SEQ (CHAR (CHR ''a'')) (SEQ (STAR (CHAR (CHR ''b''))) (CHAR (CHR ''c'')))) ''abbbbc''" +value "matcher (SEQ (CHAR (CHR ''a'')) (SEQ (STAR (CHAR (CHR ''b''))) (CHAR (CHR ''c'')))) ''abbcbbc''" + +section {* Incorrect Matcher - fun-definition rejected *} + +fun + match :: "rexp list \ string \ bool" +where + "match [] [] = True" +| "match [] (c # s) = False" +| "match (NULL # rs) s = False" +| "match (EMPTY # rs) s = match rs s" +| "match (CHAR c # rs) [] = False" +| "match (CHAR c # rs) (d # s) = (if c = d then match rs s else False)" +| "match (ALT r1 r2 # rs) s = (match (r1 # rs) s \ match (r2 # rs) s)" +| "match (SEQ r1 r2 # rs) s = match (r1 # r2 # rs) s" +| "match (STAR r # rs) s = (match rs s \ match (r # (STAR r) # rs) s)" + + +end \ No newline at end of file