src/HOL/Imperative_HOL/ex/SatChecker.thy
changeset 37771 1bec64044b5e
parent 37726 17b05b104390
child 37775 7371534297a9
equal deleted inserted replaced
37770:cddb3106adb8 37771:1bec64044b5e
     3 *)
     3 *)
     4 
     4 
     5 header {* An efficient checker for proofs from a SAT solver *}
     5 header {* An efficient checker for proofs from a SAT solver *}
     6 
     6 
     7 theory SatChecker
     7 theory SatChecker
     8 imports RBT_Impl Sorted_List "~~/src/HOL/Imperative_HOL/Imperative_HOL" 
     8 imports RBT_Impl Sorted_List Imperative_HOL
     9 begin
     9 begin
    10 
    10 
    11 section{* General settings and functions for our representation of clauses *}
    11 section{* General settings and functions for our representation of clauses *}
    12 
    12 
    13 subsection{* Types for Literals, Clauses and ProofSteps *}
    13 subsection{* Types for Literals, Clauses and ProofSteps *}
   215 assumes "crel (res_mem l xs) h h' r"
   215 assumes "crel (res_mem l xs) h h' r"
   216   shows "l \<in> set xs \<and> r = remove1 l xs"
   216   shows "l \<in> set xs \<and> r = remove1 l xs"
   217 using assms
   217 using assms
   218 proof (induct xs arbitrary: r)
   218 proof (induct xs arbitrary: r)
   219   case Nil
   219   case Nil
   220   thus ?case unfolding res_mem.simps by (auto elim: crel_raise)
   220   thus ?case unfolding res_mem.simps by (auto elim: crel_raiseE)
   221 next
   221 next
   222   case (Cons x xs')
   222   case (Cons x xs')
   223   thus ?case
   223   thus ?case
   224     unfolding res_mem.simps
   224     unfolding res_mem.simps
   225     by (elim crel_raise crel_return crel_if crelE) auto
   225     by (elim crel_raiseE crel_returnE crel_ifE crel_bindE) auto
   226 qed
   226 qed
   227 
   227 
   228 lemma resolve1_Inv:
   228 lemma resolve1_Inv:
   229 assumes "crel (resolve1 l xs ys) h h' r"
   229 assumes "crel (resolve1 l xs ys) h h' r"
   230   shows "l \<in> set xs \<and> r = merge (remove1 l xs) ys"
   230   shows "l \<in> set xs \<and> r = merge (remove1 l xs) ys"
   231 using assms
   231 using assms
   232 proof (induct xs ys arbitrary: r rule: resolve1.induct)
   232 proof (induct xs ys arbitrary: r rule: resolve1.induct)
   233   case (1 l x xs y ys r)
   233   case (1 l x xs y ys r)
   234   thus ?case
   234   thus ?case
   235     unfolding resolve1.simps
   235     unfolding resolve1.simps
   236     by (elim crelE crel_if crel_return) auto
   236     by (elim crel_bindE crel_ifE crel_returnE) auto
   237 next
   237 next
   238   case (2 l ys r)
   238   case (2 l ys r)
   239   thus ?case
   239   thus ?case
   240     unfolding resolve1.simps
   240     unfolding resolve1.simps
   241     by (elim crel_raise) auto
   241     by (elim crel_raiseE) auto
   242 next
   242 next
   243   case (3 l v va r)
   243   case (3 l v va r)
   244   thus ?case
   244   thus ?case
   245     unfolding resolve1.simps
   245     unfolding resolve1.simps
   246     by (fastsimp dest!: res_mem)
   246     by (fastsimp dest!: res_mem)
   252 using assms
   252 using assms
   253 proof (induct xs ys arbitrary: r rule: resolve2.induct)
   253 proof (induct xs ys arbitrary: r rule: resolve2.induct)
   254   case (1 l x xs y ys r)
   254   case (1 l x xs y ys r)
   255   thus ?case
   255   thus ?case
   256     unfolding resolve2.simps
   256     unfolding resolve2.simps
   257     by (elim crelE crel_if crel_return) auto
   257     by (elim crel_bindE crel_ifE crel_returnE) auto
   258 next
   258 next
   259   case (2 l ys r)
   259   case (2 l ys r)
   260   thus ?case
   260   thus ?case
   261     unfolding resolve2.simps
   261     unfolding resolve2.simps
   262     by (elim crel_raise) auto
   262     by (elim crel_raiseE) auto
   263 next
   263 next
   264   case (3 l v va r)
   264   case (3 l v va r)
   265   thus ?case
   265   thus ?case
   266     unfolding resolve2.simps
   266     unfolding resolve2.simps
   267     by (fastsimp dest!: res_mem simp add: merge_Nil)
   267     by (fastsimp dest!: res_mem simp add: merge_Nil)
   310     from "1.hyps"(3) [OF cond res_thm] cond return have ?case by auto
   310     from "1.hyps"(3) [OF cond res_thm] cond return have ?case by auto
   311   } moreover
   311   } moreover
   312   note "1.prems"
   312   note "1.prems"
   313   ultimately show ?case
   313   ultimately show ?case
   314     unfolding res_thm'.simps
   314     unfolding res_thm'.simps
   315     apply (elim crelE crel_if crel_return)
   315     apply (elim crel_bindE crel_ifE crel_returnE)
   316     apply simp
   316     apply simp
   317     apply simp
   317     apply simp
   318     apply simp
   318     apply simp
   319     apply simp
   319     apply simp
   320     apply fastsimp
   320     apply fastsimp
   321     done
   321     done
   322 next
   322 next
   323   case (2 l ys r)
   323   case (2 l ys r)
   324   thus ?case
   324   thus ?case
   325     unfolding res_thm'.simps
   325     unfolding res_thm'.simps
   326     by (elim crel_raise) auto
   326     by (elim crel_raiseE) auto
   327 next
   327 next
   328   case (3 l v va r)
   328   case (3 l v va r)
   329   thus ?case
   329   thus ?case
   330     unfolding res_thm'.simps
   330     unfolding res_thm'.simps
   331     by (elim crel_raise) auto
   331     by (elim crel_raiseE) auto
   332 qed
   332 qed
   333 
   333 
   334 lemma res_mem_no_heap:
   334 lemma res_mem_no_heap:
   335 assumes "crel (res_mem l xs) h h' r"
   335 assumes "crel (res_mem l xs) h h' r"
   336   shows "h = h'"
   336   shows "h = h'"
   337 using assms
   337 using assms
   338 apply (induct xs arbitrary: r)
   338 apply (induct xs arbitrary: r)
   339 unfolding res_mem.simps
   339 unfolding res_mem.simps
   340 apply (elim crel_raise)
   340 apply (elim crel_raiseE)
   341 apply auto
   341 apply auto
   342 apply (elim crel_if crelE crel_raise crel_return)
   342 apply (elim crel_ifE crel_bindE crel_raiseE crel_returnE)
   343 apply auto
   343 apply auto
   344 done
   344 done
   345 
   345 
   346 lemma resolve1_no_heap:
   346 lemma resolve1_no_heap:
   347 assumes "crel (resolve1 l xs ys) h h' r"
   347 assumes "crel (resolve1 l xs ys) h h' r"
   348   shows "h = h'"
   348   shows "h = h'"
   349 using assms
   349 using assms
   350 apply (induct xs ys arbitrary: r rule: resolve1.induct)
   350 apply (induct xs ys arbitrary: r rule: resolve1.induct)
   351 unfolding resolve1.simps
   351 unfolding resolve1.simps
   352 apply (elim crelE crel_if crel_return crel_raise)
   352 apply (elim crel_bindE crel_ifE crel_returnE crel_raiseE)
   353 apply (auto simp add: res_mem_no_heap)
   353 apply (auto simp add: res_mem_no_heap)
   354 by (elim crel_raise) auto
   354 by (elim crel_raiseE) auto
   355 
   355 
   356 lemma resolve2_no_heap:
   356 lemma resolve2_no_heap:
   357 assumes "crel (resolve2 l xs ys) h h' r"
   357 assumes "crel (resolve2 l xs ys) h h' r"
   358   shows "h = h'"
   358   shows "h = h'"
   359 using assms
   359 using assms
   360 apply (induct xs ys arbitrary: r rule: resolve2.induct)
   360 apply (induct xs ys arbitrary: r rule: resolve2.induct)
   361 unfolding resolve2.simps
   361 unfolding resolve2.simps
   362 apply (elim crelE crel_if crel_return crel_raise)
   362 apply (elim crel_bindE crel_ifE crel_returnE crel_raiseE)
   363 apply (auto simp add: res_mem_no_heap)
   363 apply (auto simp add: res_mem_no_heap)
   364 by (elim crel_raise) auto
   364 by (elim crel_raiseE) auto
   365 
   365 
   366 
   366 
   367 lemma res_thm'_no_heap:
   367 lemma res_thm'_no_heap:
   368   assumes "crel (res_thm' l xs ys) h h' r"
   368   assumes "crel (res_thm' l xs ys) h h' r"
   369   shows "h = h'"
   369   shows "h = h'"
   370   using assms
   370   using assms
   371 proof (induct xs ys arbitrary: r rule: res_thm'.induct)
   371 proof (induct xs ys arbitrary: r rule: res_thm'.induct)
   372   case (1 l x xs y ys r)
   372   case (1 l x xs y ys r)
   373   thus ?thesis
   373   thus ?thesis
   374     unfolding res_thm'.simps
   374     unfolding res_thm'.simps
   375     by (elim crelE crel_if crel_return)
   375     by (elim crel_bindE crel_ifE crel_returnE)
   376   (auto simp add: resolve1_no_heap resolve2_no_heap)
   376   (auto simp add: resolve1_no_heap resolve2_no_heap)
   377 next
   377 next
   378   case (2 l ys r)
   378   case (2 l ys r)
   379   thus ?case
   379   thus ?case
   380     unfolding res_thm'.simps
   380     unfolding res_thm'.simps
   381     by (elim crel_raise) auto
   381     by (elim crel_raiseE) auto
   382 next
   382 next
   383   case (3 l v va r)
   383   case (3 l v va r)
   384   thus ?case
   384   thus ?case
   385     unfolding res_thm'.simps
   385     unfolding res_thm'.simps
   386     by (elim crel_raise) auto
   386     by (elim crel_raiseE) auto
   387 qed
   387 qed
   388 
   388 
   389 
   389 
   390 lemma res_thm'_Inv2:
   390 lemma res_thm'_Inv2:
   391   assumes res_thm: "crel (res_thm' l xs ys) h h' rcl"
   391   assumes res_thm: "crel (res_thm' l xs ys) h h' rcl"
   464   assumes correct_a: "correctArray r a h"
   464   assumes correct_a: "correctArray r a h"
   465   assumes correct_cli: "correctClause r cli \<and> sorted cli \<and> distinct cli"
   465   assumes correct_cli: "correctClause r cli \<and> sorted cli \<and> distinct cli"
   466   shows "h = h' \<and> correctClause r rs \<and> sorted rs \<and> distinct rs"
   466   shows "h = h' \<and> correctClause r rs \<and> sorted rs \<and> distinct rs"
   467 proof -
   467 proof -
   468   from res_thm have l_not_zero: "l \<noteq> 0" 
   468   from res_thm have l_not_zero: "l \<noteq> 0" 
   469     by (auto elim: crel_raise)
   469     by (auto elim: crel_raiseE)
   470   {
   470   {
   471     fix clj
   471     fix clj
   472     let ?rs = "merge (remove l cli) (remove (compl l) clj)"
   472     let ?rs = "merge (remove l cli) (remove (compl l) clj)"
   473     let ?rs' = "merge (remove (compl l) cli) (remove l clj)"
   473     let ?rs' = "merge (remove (compl l) cli) (remove l clj)"
   474     assume "h = h'" "Some clj = get_array a h' ! j" "j < Array.length a h'"
   474     assume "h = h'" "Some clj = get_array a h' ! j" "j < Array.length a h'"
   492     from res_thm'_no_heap[OF this] res_thm'_Inv2[OF this l_not_zero clj correct_cli]
   492     from res_thm'_no_heap[OF this] res_thm'_Inv2[OF this l_not_zero clj correct_cli]
   493     have "h = h' \<and> correctClause r rs \<and> sorted rs \<and> distinct rs" by simp
   493     have "h = h' \<and> correctClause r rs \<and> sorted rs \<and> distinct rs" by simp
   494   }
   494   }
   495   with assms show ?thesis
   495   with assms show ?thesis
   496     unfolding res_thm2.simps get_clause_def
   496     unfolding res_thm2.simps get_clause_def
   497     by (elim crelE crelE' crel_if crel_nth crel_raise crel_return crel_option_case) auto
   497     by (elim crel_bindE crel_ifE crel_nthE crel_raiseE crel_returnE crel_option_case) auto
   498 qed
   498 qed
   499 
   499 
   500 lemma foldM_Inv2:
   500 lemma foldM_Inv2:
   501   assumes "crel (foldM (res_thm2 a) rs cli) h h' rcl"
   501   assumes "crel (foldM (res_thm2 a) rs cli) h h' rcl"
   502   assumes correct_a: "correctArray r a h"
   502   assumes correct_a: "correctArray r a h"
   504   shows "h = h' \<and> correctClause r rcl \<and> sorted rcl \<and> distinct rcl"
   504   shows "h = h' \<and> correctClause r rcl \<and> sorted rcl \<and> distinct rcl"
   505 using assms
   505 using assms
   506 proof (induct rs arbitrary: h h' cli)
   506 proof (induct rs arbitrary: h h' cli)
   507   case Nil thus ?case
   507   case Nil thus ?case
   508     unfolding foldM.simps
   508     unfolding foldM.simps
   509     by (elim crel_return) auto
   509     by (elim crel_returnE) auto
   510 next
   510 next
   511   case (Cons x xs)
   511   case (Cons x xs)
   512   {
   512   {
   513     fix h1 ret
   513     fix h1 ret
   514     obtain l j where x_is: "x = (l, j)" by fastsimp
   514     obtain l j where x_is: "x = (l, j)" by fastsimp
   521     from step Cons.hyps [OF foldM correct_a ret] have
   521     from step Cons.hyps [OF foldM correct_a ret] have
   522     "h = h' \<and> correctClause r rcl \<and> sorted rcl \<and> distinct rcl" by auto
   522     "h = h' \<and> correctClause r rcl \<and> sorted rcl \<and> distinct rcl" by auto
   523   }
   523   }
   524   with Cons show ?case
   524   with Cons show ?case
   525     unfolding foldM.simps
   525     unfolding foldM.simps
   526     by (elim crelE) auto
   526     by (elim crel_bindE) auto
   527 qed
   527 qed
   528 
   528 
   529 
   529 
   530 lemma step_correct2:
   530 lemma step_correct2:
   531   assumes crel: "crel (doProofStep2 a step rcs) h h' res"
   531   assumes crel: "crel (doProofStep2 a step rcs) h h' res"
   534 proof (cases "(a,step,rcs)" rule: doProofStep2.cases)
   534 proof (cases "(a,step,rcs)" rule: doProofStep2.cases)
   535   case (1 a saveTo i rs rcs)
   535   case (1 a saveTo i rs rcs)
   536   with crel correctArray
   536   with crel correctArray
   537   show ?thesis
   537   show ?thesis
   538     apply auto
   538     apply auto
   539     apply (auto simp: get_clause_def elim!: crelE crel_nth)
   539     apply (auto simp: get_clause_def elim!: crel_bindE crel_nthE)
   540     apply (auto elim!: crelE crel_nth crel_option_case crel_raise 
   540     apply (auto elim!: crel_bindE crel_nthE crel_option_case crel_raiseE
   541       crel_return crel_upd)
   541       crel_returnE crel_updE)
   542     apply (frule foldM_Inv2)
   542     apply (frule foldM_Inv2)
   543     apply assumption
   543     apply assumption
   544     apply (simp add: correctArray_def)
   544     apply (simp add: correctArray_def)
   545     apply (drule_tac x="y" in bspec)
   545     apply (drule_tac x="y" in bspec)
   546     apply (rule array_ranI[where i=i])
   546     apply (rule array_ranI[where i=i])
   547     by (auto intro: correctArray_update)
   547     by (auto intro: correctArray_update)
   548 next
   548 next
   549   case (2 a cid rcs)
   549   case (2 a cid rcs)
   550   with crel correctArray
   550   with crel correctArray
   551   show ?thesis
   551   show ?thesis
   552     by (auto simp: correctArray_def elim!: crelE crel_upd crel_return
   552     by (auto simp: correctArray_def elim!: crel_bindE crel_updE crel_returnE
   553      dest: array_ran_upd_array_None)
   553      dest: array_ran_upd_array_None)
   554 next
   554 next
   555   case (3 a cid c rcs)
   555   case (3 a cid c rcs)
   556   with crel correctArray
   556   with crel correctArray
   557   show ?thesis
   557   show ?thesis
   558     apply (auto elim!: crelE crel_upd crel_return)
   558     apply (auto elim!: crel_bindE crel_updE crel_returnE)
   559     apply (auto simp: correctArray_def dest!: array_ran_upd_array_Some)
   559     apply (auto simp: correctArray_def dest!: array_ran_upd_array_Some)
   560     apply (auto intro: correctClause_mono)
   560     apply (auto intro: correctClause_mono)
   561     by (auto simp: correctClause_def)
   561     by (auto simp: correctClause_def)
   562 next
   562 next
   563   case 4
   563   case 4
   564   with crel correctArray
   564   with crel correctArray
   565   show ?thesis by (auto elim: crel_raise)
   565   show ?thesis by (auto elim: crel_raiseE)
   566 next
   566 next
   567   case 5
   567   case 5
   568   with crel correctArray
   568   with crel correctArray
   569   show ?thesis by (auto elim: crel_raise)
   569   show ?thesis by (auto elim: crel_raiseE)
   570 qed
   570 qed
   571   
   571   
   572 
   572 
   573 theorem fold_steps_correct:
   573 theorem fold_steps_correct:
   574   assumes "crel (foldM (doProofStep2 a) steps rcs) h h' res"
   574   assumes "crel (foldM (doProofStep2 a) steps rcs) h h' res"
   575   assumes "correctArray rcs a h"
   575   assumes "correctArray rcs a h"
   576   shows "correctArray res a h'"
   576   shows "correctArray res a h'"
   577 using assms
   577 using assms
   578 by (induct steps arbitrary: rcs h h' res)
   578 by (induct steps arbitrary: rcs h h' res)
   579  (auto elim!: crelE crel_return dest:step_correct2)
   579  (auto elim!: crel_bindE crel_returnE dest:step_correct2)
   580 
   580 
   581 theorem checker_soundness:
   581 theorem checker_soundness:
   582   assumes "crel (checker n p i) h h' cs"
   582   assumes "crel (checker n p i) h h' cs"
   583   shows "inconsistent cs"
   583   shows "inconsistent cs"
   584 using assms unfolding checker_def
   584 using assms unfolding checker_def
   585 apply (elim crelE crel_nth crel_if crel_return crel_raise crel_new_weak)
   585 apply (elim crel_bindE crel_nthE crel_ifE crel_returnE crel_raiseE crel_newE)
   586 prefer 2 apply simp
   586 prefer 2 apply simp
   587 apply auto
   587 apply auto
   588 apply (drule fold_steps_correct)
   588 apply (drule fold_steps_correct)
   589 apply (simp add: correctArray_def array_ran_def)
   589 apply (simp add: correctArray_def array_ran_def)
   590 apply (rule implies_empty_inconsistent)
   590 apply (rule implies_empty_inconsistent)