thys2/ClosedForms.thy
changeset 473 37d14cbce020
parent 472 6953d2786e7c
child 475 10fd25fba2ba
equal deleted inserted replaced
472:6953d2786e7c 473:37d14cbce020
   503 
   503 
   504 
   504 
   505 inductive 
   505 inductive 
   506   frewrites:: "rrexp list \<Rightarrow> rrexp list \<Rightarrow> bool" ("_ \<leadsto>f* _" [10, 10] 10)
   506   frewrites:: "rrexp list \<Rightarrow> rrexp list \<Rightarrow> bool" ("_ \<leadsto>f* _" [10, 10] 10)
   507 where 
   507 where 
   508   rs1[intro, simp]:"rs \<leadsto>f* rs"
   508   [intro, simp]:"rs \<leadsto>f* rs"
   509 | rs2[intro]: "\<lbrakk>rs1 \<leadsto>f* rs2; rs2 \<leadsto>f rs3\<rbrakk> \<Longrightarrow> rs1 \<leadsto>f* rs3"
   509 | [intro]: "\<lbrakk>rs1 \<leadsto>f* rs2; rs2 \<leadsto>f rs3\<rbrakk> \<Longrightarrow> rs1 \<leadsto>f* rs3"
       
   510 
       
   511 inductive grewrite:: "rrexp list \<Rightarrow> rrexp list \<Rightarrow> bool" ("_ \<leadsto>g _" [10, 10] 10)
       
   512   where
       
   513   "(RZERO # rs) \<leadsto>g rs"
       
   514 | "((RALTS rs) # rsa) \<leadsto>g (rs @ rsa)"
       
   515 | "rs1 \<leadsto>g rs2 \<Longrightarrow> (r # rs1) \<leadsto>g (r # rs2)"
       
   516 | "rsa @ [a] @ rsb @ [a] @ rsc \<leadsto>g rsa @ [a] @ rsb @ rsc" 
       
   517 
       
   518 
       
   519 inductive 
       
   520   grewrites:: "rrexp list \<Rightarrow> rrexp list \<Rightarrow> bool" ("_ \<leadsto>g* _" [10, 10] 10)
       
   521 where 
       
   522   [intro, simp]:"rs \<leadsto>g* rs"
       
   523 | [intro]: "\<lbrakk>rs1 \<leadsto>g* rs2; rs2 \<leadsto>g rs3\<rbrakk> \<Longrightarrow> rs1 \<leadsto>g* rs3"
       
   524 (*
       
   525 inductive 
       
   526   frewrites2:: "rrexp list \<Rightarrow> rrexp list \<Rightarrow> bool" ("_ <\<leadsto>f* _" [10, 10] 10)
       
   527 where 
       
   528  [intro]: "\<lbrakk>rs1 \<leadsto>f* rs2; rs2 \<leadsto>f* rs1\<rbrakk> \<Longrightarrow> rs1 <\<leadsto>f* rs2"
       
   529 *)
   510 
   530 
   511 lemma fr_in_rstar : "r1 \<leadsto>f r2 \<Longrightarrow> r1 \<leadsto>f* r2"
   531 lemma fr_in_rstar : "r1 \<leadsto>f r2 \<Longrightarrow> r1 \<leadsto>f* r2"
   512   using frewrites.intros(1) frewrites.intros(2) by blast
   532   using frewrites.intros(1) frewrites.intros(2) by blast
   513  
   533  
   514 lemma freal_trans[trans]: 
   534 lemma freal_trans[trans]: 
   558   using many_steps_later apply blast
   578   using many_steps_later apply blast
   559   apply (simp add: frewrites_append)
   579   apply (simp add: frewrites_append)
   560   apply (metis append.assoc append_Cons frewrite.intros(2) frewrites_append many_steps_later)
   580   apply (metis append.assoc append_Cons frewrite.intros(2) frewrites_append many_steps_later)
   561   using frewrites_cons by auto
   581   using frewrites_cons by auto
   562 
   582 
   563 
   583 lemma frewrites_middle:
   564 
   584   shows "rs1 \<leadsto>f* rs2 \<Longrightarrow> r # (RALTS rs # rs1) \<leadsto>f* r # (rs @ rs1)"
       
   585   by (simp add: fr_in_rstar frewrite.intros(2) frewrite.intros(3))
       
   586 
       
   587 lemma frewrites_alt:
       
   588   shows "rs1 \<leadsto>f* rs2 \<Longrightarrow> (RALT r1 r2) # rs1 \<leadsto>f* r1 # r2 # rs2"  
       
   589   by (metis Cons_eq_appendI append_self_conv2 frewrite.intros(2) frewrites_cons many_steps_later)
   565 
   590 
   566 lemma many_steps_later1:
   591 lemma many_steps_later1:
   567   shows " \<lbrakk>rs1 \<leadsto>f* rs2\<rbrakk>
   592   shows " \<lbrakk>rs1 \<leadsto>f* rs2\<rbrakk>
   568        \<Longrightarrow> (RONE # rs1) \<leadsto>f* (RONE # rs2)"
   593        \<Longrightarrow> (RONE # rs1) \<leadsto>f* (RONE # rs2)"
   569   oops
   594   oops
   575   apply(case_tac a)
   600   apply(case_tac a)
   576        apply simp+
   601        apply simp+
   577   using frewrite.intros(1) many_steps_later apply blast
   602   using frewrite.intros(1) many_steps_later apply blast
   578      apply(case_tac "x = x3")
   603      apply(case_tac "x = x3")
   579       apply simp
   604       apply simp
   580   
       
   581   using frewrites_cons apply presburger
   605   using frewrites_cons apply presburger
   582   using frewrite.intros(1) many_steps_later apply fastforce
   606   using frewrite.intros(1) many_steps_later apply fastforce
   583   apply(case_tac "rnullable x41")
   607   apply(case_tac "rnullable x41")
   584   apply simp
   608      apply simp+
   585 
   609      apply (simp add: frewrites_alt)
   586 
   610   apply (simp add: frewrites_cons)
       
   611    apply (simp add: frewrites_append)
       
   612   by (simp add: frewrites_cons)
   587 
   613 
   588 
   614 
   589 fun alt_set:: "rrexp \<Rightarrow> rrexp set"
   615 fun alt_set:: "rrexp \<Rightarrow> rrexp set"
   590   where
   616   where
   591   "alt_set (RALTS rs) = set rs"
   617   "alt_set (RALTS rs) = set rs"
   594 
   620 
   595 
   621 
   596 lemma rd_flts_set:
   622 lemma rd_flts_set:
   597   shows "rs1 \<leadsto>f* rs2 \<Longrightarrow> rdistinct rs1 (insert RZERO (rset \<union> (\<Union>(alt_set ` rset)))) \<leadsto>f*  
   623   shows "rs1 \<leadsto>f* rs2 \<Longrightarrow> rdistinct rs1 (insert RZERO (rset \<union> (\<Union>(alt_set ` rset)))) \<leadsto>f*  
   598                           rdistinct rs2 (rset \<union> (\<Union>(alt_set ` rset)))"
   624                           rdistinct rs2 (rset \<union> (\<Union>(alt_set ` rset)))"
   599   by (meson frewrite.intros(2) frewrites.simps)
   625 
       
   626   oops
       
   627 
       
   628 
   600   
   629   
   601 lemma rd_flts_set2:
   630 lemma rd_flts_set2:
   602   shows "rs1 \<leadsto>f* rs2 \<Longrightarrow> rdistinct rs1 ((rset \<union> (\<Union>(alt_set ` rset)))) \<leadsto>f*  
   631   shows "rs1 \<leadsto>f* rs2 \<Longrightarrow> rdistinct rs1 ((rset \<union> (\<Union>(alt_set ` rset)))) \<leadsto>f*  
   603                           rdistinct rs2 (rset \<union> (\<Union>(alt_set ` rset)))"
   632                           rdistinct rs2 (rset \<union> (\<Union>(alt_set ` rset)))"
   604   using frewrite.intros(2) by blast
   633   oops
   605 
   634 
   606 lemma rd_flts_incorrect:
   635 
   607   shows "rs1 \<leadsto>f* rs2 \<Longrightarrow> rdistinct rs1 rset \<leadsto>f* rdistinct rs2 rset"
       
   608   sledgehammer
       
   609   by (smt (verit, ccfv_threshold) frewrites.simps)
       
   610 
   636 
   611 lemma flts_does_rewrite:
   637 lemma flts_does_rewrite:
   612   shows "rs1 \<leadsto>f* rs2 \<Longrightarrow> rflts rs1 = rflts rs2"
   638   shows "rs1 \<leadsto>f* rs2 \<Longrightarrow> rflts rs1 = rflts rs2"
   613   oops
   639   oops
   614 
   640 
   615 lemma rflts_fltsder_derflts:
   641 lemma with_wo0_distinct:
   616   shows "rflts (map rsimp (rdistinct (map (rder x3) (rflts rs)) rset)) = 
   642   shows "rdistinct rs rset \<leadsto>f* rdistinct rs (insert RZERO rset)"
   617          rflts (map rsimp (rdistinct (rflts (map (rder x3) rs)) rset))"
   643   apply(induct rs arbitrary: rset)
   618   sorry
   644    apply simp
       
   645   apply(case_tac a)
       
   646   apply(case_tac "RZERO \<in> rset")
       
   647         apply simp+
       
   648   using fr_in_rstar frewrite.intros(1) apply presburger
       
   649   apply (case_tac "RONE \<in> rset")
       
   650        apply simp+
       
   651   using frewrites_cons apply presburger
       
   652      apply(case_tac "a \<in> rset")
       
   653   apply simp
       
   654   apply (simp add: frewrites_cons)
       
   655      apply(case_tac "a \<in> rset")
       
   656   apply simp
       
   657   apply (simp add: frewrites_cons)
       
   658        apply(case_tac "a \<in> rset")
       
   659   apply simp
       
   660   apply (simp add: frewrites_cons)
       
   661        apply(case_tac "a \<in> rset")
       
   662   apply simp
       
   663   apply (simp add: frewrites_cons)
       
   664   done
       
   665 
       
   666 lemma rdistinct_concat:
       
   667   shows "set rs \<subseteq> rset \<Longrightarrow> rdistinct (rs @ rsa) rset = rdistinct rsa rset"
       
   668   apply(induct rs)
       
   669    apply simp+
       
   670   done
       
   671 
       
   672 lemma rdistinct_concat2:
       
   673   shows "\<forall>r \<in> set rs. r \<in> rset \<Longrightarrow> rdistinct (rs @ rsa) rset = rdistinct rsa rset"
       
   674   by (simp add: rdistinct_concat subsetI)
       
   675 
       
   676 lemma frewrite_with_distinct:
       
   677   shows " \<lbrakk>rs2 \<leadsto>f rs3\<rbrakk>
       
   678        \<Longrightarrow> rdistinct rs2
       
   679             (insert RZERO (rset \<union> \<Union> (alt_set ` rset))) \<leadsto>f* 
       
   680            rdistinct rs3 
       
   681             (insert RZERO (rset \<union> \<Union> (alt_set ` rset)))"
       
   682   apply(induct rs2 rs3 rule: frewrite.induct)
       
   683     apply(case_tac "RZERO \<in>  (rset \<union> \<Union> (alt_set ` rset))")
       
   684   apply simp
       
   685     apply simp
       
   686    apply(case_tac "RALTS rs \<in> rset")
       
   687     apply simp
       
   688   apply(subgoal_tac "\<forall>r \<in> set rs. r \<in> \<Union> (alt_set ` rset)")
       
   689   apply(subgoal_tac " rdistinct (rs @ rsa) (insert RZERO (rset \<union> \<Union> (alt_set ` rset))) = 
       
   690                        rdistinct rsa (insert RZERO (rset \<union> \<Union> (alt_set ` rset)))")
       
   691   using frewrites.intros(1) apply presburger
       
   692      apply (simp add: rdistinct_concat2)
       
   693     apply simp
       
   694   using alt_set.simps(1) apply blast
       
   695    apply(case_tac "RALTS rs \<in> rset \<union> \<Union>(alt_set ` rset)")
       
   696   
       
   697 
       
   698   sorry
       
   699 
       
   700 
       
   701 lemma frewrites_with_distinct:
       
   702   shows "rs1 \<leadsto>f* rs2 \<Longrightarrow>
       
   703  rs1 @ (rdistinct rsa (insert RZERO (set rs1 \<union> \<Union>(alt_set ` (set rs1) )))) \<leadsto>f* 
       
   704  rs2 @ (rdistinct rsb (insert RZERO (set rs2 \<union> \<Union>(alt_set ` (set rs2) ))))"
       
   705   apply(induct rs1 rs2 rule: frewrites.induct)
       
   706    apply simp
       
   707   
       
   708 
       
   709   sorry
       
   710 
       
   711 (*a more refined notion of \<leadsto>* is needed,
       
   712 this lemma fails when rs1 contains some RALTS rs where elements
       
   713 of rs appear in later parts of rs1, which will be picked up by rs2
       
   714 and deduplicated*)
       
   715 lemma wrong_frewrites_with_distinct2:
       
   716   shows "rs1 \<leadsto>f* rs2 \<Longrightarrow>
       
   717  (rdistinct rs1 {RZERO}) \<leadsto>f* rdistinct rs2 {RZERO}"
       
   718   oops
       
   719 
       
   720 lemma frewrite_single_step:
       
   721   shows " rs2 \<leadsto>f rs3 \<Longrightarrow> rsimp (RALTS rs2) = rsimp (RALTS rs3)"
       
   722   apply(induct rs2 rs3 rule: frewrite.induct)
       
   723     apply simp
       
   724   using simp_flatten apply blast
       
   725   by (metis (no_types, opaque_lifting) list.simps(9) rsimp.simps(2) simp_flatten2)
       
   726 
       
   727 lemma frewrites_equivalent_simp:
       
   728   shows "rs1 \<leadsto>f* rs2 \<Longrightarrow> rsimp (RALTS rs1) = rsimp (RALTS rs2)"
       
   729   apply(induct rs1 rs2 rule: frewrites.induct)
       
   730    apply simp
       
   731   using frewrite_single_step by presburger
       
   732 
       
   733 lemma frewrites_dB_wwo0_simp:
       
   734   shows "rdistinct rs1 {RZERO} \<leadsto>f* rdistinct rs2 {RZERO} 
       
   735          \<Longrightarrow> rsimp (RALTS (rdistinct rs1 {})) = rsimp (RALTS (rdistinct rs2 {}))"
       
   736 
       
   737   sorry
       
   738 
   619 
   739 
   620 
   740 
   621 lemma simp_der_flts:
   741 lemma simp_der_flts:
   622   shows "rsimp (RALTS (rdistinct (map (rder x) (rflts rs)) {})) = 
   742   shows "rsimp (RALTS (rdistinct (map (rder x) (rflts rs)) {})) = 
   623          rsimp (RALTS (rdistinct (rflts (map (rder x) rs)) {}))"
   743          rsimp (RALTS (rdistinct (rflts (map (rder x) rs)) {}))"
   624 
   744   apply(subgoal_tac "map (rder x) (rflts rs) \<leadsto>f* rflts (map (rder x) rs)")
   625   apply(induct rs)
   745    apply(subgoal_tac "rdistinct (map (rder x) (rflts rs)) {RZERO} 
   626    apply simp
   746                     \<leadsto>f* rdistinct ( rflts (map (rder x) rs)) {RZERO}")
   627   apply(case_tac a)
   747   apply(subgoal_tac "rsimp (RALTS (rdistinct (map (rder x) (rflts rs)) {})) 
   628        apply simp+
   748                    = rsimp (RALTS ( rdistinct ( rflts (map (rder x) rs)) {}))")
   629       prefer 2
   749   apply meson
   630   apply simp
   750   using frewrites_dB_wwo0_simp apply blast
   631       apply(case_tac "RZERO \<in> rset")
   751   using frewrites_with_distinct2 apply blast
   632        apply simp+
   752   using early_late_der_frewrites by blast
   633   using distinct_flts_no0 apply presburger
       
   634      apply (case_tac "x = x3")
       
   635   prefer 2
       
   636   apply simp
       
   637   using distinct_flts_no0 apply presburger
       
   638   apply(case_tac "RONE \<in> rset")
       
   639      apply simp+
       
   640   
       
   641   sorry
       
   642 
   753 
   643 
   754 
   644 lemma simp_der_pierce_flts:
   755 lemma simp_der_pierce_flts:
   645   shows " rsimp (
   756   shows " rsimp (
   646 rsimp_ALTs (rdistinct (map (rder x) (rflts (map (rsimp \<circ> (\<lambda>r. rders_simp r xs)) rs))) {})
   757 rsimp_ALTs (rdistinct (map (rder x) (rflts (map (rsimp \<circ> (\<lambda>r. rders_simp r xs)) rs))) {})