src/HOL/Imperative_HOL/Heap_Monad.thy
changeset 51485 637aa1649ac7
parent 51143 0a2371e7ced3
child 52388 f6d1ca0c6faf
equal deleted inserted replaced
51484:49eb8d73ae10 51485:637aa1649ac7
   319     and "execute (g x) h' = Some (y, h'')"
   319     and "execute (g x) h' = Some (y, h'')"
   320   shows "execute (f \<guillemotright>= g) h = Some (y, h'')"
   320   shows "execute (f \<guillemotright>= g) h = Some (y, h'')"
   321   using assms by (simp add: bind_def)
   321   using assms by (simp add: bind_def)
   322 
   322 
   323 lemma return_bind [simp]: "return x \<guillemotright>= f = f x"
   323 lemma return_bind [simp]: "return x \<guillemotright>= f = f x"
   324   by (rule Heap_eqI) (simp add: execute_bind execute_simps)
   324   by (rule Heap_eqI) (simp add: execute_simps)
   325 
   325 
   326 lemma bind_return [simp]: "f \<guillemotright>= return = f"
   326 lemma bind_return [simp]: "f \<guillemotright>= return = f"
   327   by (rule Heap_eqI) (simp add: bind_def execute_simps split: option.splits)
   327   by (rule Heap_eqI) (simp add: bind_def execute_simps split: option.splits)
   328 
   328 
   329 lemma bind_bind [simp]: "(f \<guillemotright>= g) \<guillemotright>= k = (f :: 'a Heap) \<guillemotright>= (\<lambda>x. g x \<guillemotright>= k)"
   329 lemma bind_bind [simp]: "(f \<guillemotright>= g) \<guillemotright>= k = (f :: 'a Heap) \<guillemotright>= (\<lambda>x. g x \<guillemotright>= k)"
   415   "Heap_ord = img_ord execute (fun_ord option_ord)"
   415   "Heap_ord = img_ord execute (fun_ord option_ord)"
   416 
   416 
   417 definition Heap_lub :: "'a Heap set \<Rightarrow> 'a Heap" where
   417 definition Heap_lub :: "'a Heap set \<Rightarrow> 'a Heap" where
   418   "Heap_lub = img_lub execute Heap (fun_lub (flat_lub None))"
   418   "Heap_lub = img_lub execute Heap (fun_lub (flat_lub None))"
   419 
   419 
   420 interpretation heap!: partial_function_definitions Heap_ord Heap_lub
   420 lemma heap_interpretation: "partial_function_definitions Heap_ord Heap_lub"
   421 proof -
   421 proof -
   422   have "partial_function_definitions (fun_ord option_ord) (fun_lub (flat_lub None))"
   422   have "partial_function_definitions (fun_ord option_ord) (fun_lub (flat_lub None))"
   423     by (rule partial_function_lift) (rule flat_interpretation)
   423     by (rule partial_function_lift) (rule flat_interpretation)
   424   then have "partial_function_definitions (img_ord execute (fun_ord option_ord))
   424   then have "partial_function_definitions (img_ord execute (fun_ord option_ord))
   425       (img_lub execute Heap (fun_lub (flat_lub None)))"
   425       (img_lub execute Heap (fun_lub (flat_lub None)))"
   426     by (rule partial_function_image) (auto intro: Heap_eqI)
   426     by (rule partial_function_image) (auto intro: Heap_eqI)
   427   then show "partial_function_definitions Heap_ord Heap_lub"
   427   then show "partial_function_definitions Heap_ord Heap_lub"
   428     by (simp only: Heap_ord_def Heap_lub_def)
   428     by (simp only: Heap_ord_def Heap_lub_def)
   429 qed
   429 qed
   430 
   430 
       
   431 interpretation heap!: partial_function_definitions Heap_ord Heap_lub
       
   432 by (fact heap_interpretation)
       
   433 
       
   434 lemma heap_step_admissible: 
       
   435   "option.admissible
       
   436       (\<lambda>f:: 'a => ('b * 'c) option. \<forall>h h' r. f h = Some (r, h') \<longrightarrow> P x h h' r)"
       
   437 proof (rule ccpo.admissibleI[OF option.ccpo])
       
   438   fix A :: "('a \<Rightarrow> ('b * 'c) option) set"
       
   439   assume ch: "Complete_Partial_Order.chain option.le_fun A"
       
   440     and IH: "\<forall>f\<in>A. \<forall>h h' r. f h = Some (r, h') \<longrightarrow> P x h h' r"
       
   441   from ch have ch': "\<And>x. Complete_Partial_Order.chain option_ord {y. \<exists>f\<in>A. y = f x}" by (rule chain_fun)
       
   442   show "\<forall>h h' r. option.lub_fun A h = Some (r, h') \<longrightarrow> P x h h' r"
       
   443   proof (intro allI impI)
       
   444     fix h h' r assume "option.lub_fun A h = Some (r, h')"
       
   445     from flat_lub_in_chain[OF ch' this[unfolded fun_lub_def]]
       
   446     have "Some (r, h') \<in> {y. \<exists>f\<in>A. y = f h}" by simp
       
   447     then have "\<exists>f\<in>A. f h = Some (r, h')" by auto
       
   448     with IH show "P x h h' r" by auto
       
   449   qed
       
   450 qed
       
   451 
       
   452 lemma admissible_heap: 
       
   453   "heap.admissible (\<lambda>f. \<forall>x h h' r. effect (f x) h h' r \<longrightarrow> P x h h' r)"
       
   454 proof (rule admissible_fun[OF heap_interpretation])
       
   455   fix x
       
   456   show "ccpo.admissible Heap_lub Heap_ord (\<lambda>a. \<forall>h h' r. effect a h h' r \<longrightarrow> P x h h' r)"
       
   457     unfolding Heap_ord_def Heap_lub_def
       
   458   proof (intro admissible_image partial_function_lift flat_interpretation)
       
   459     show "option.admissible ((\<lambda>a. \<forall>h h' r. effect a h h' r \<longrightarrow> P x h h' r) \<circ> Heap)"
       
   460       unfolding comp_def effect_def execute.simps
       
   461       by (rule heap_step_admissible)
       
   462   qed (auto simp add: Heap_eqI)
       
   463 qed
       
   464 
       
   465 lemma fixp_induct_heap:
       
   466   fixes F :: "'c \<Rightarrow> 'c" and
       
   467     U :: "'c \<Rightarrow> 'b \<Rightarrow> 'a Heap" and
       
   468     C :: "('b \<Rightarrow> 'a Heap) \<Rightarrow> 'c" and
       
   469     P :: "'b \<Rightarrow> heap \<Rightarrow> heap \<Rightarrow> 'a \<Rightarrow> bool"
       
   470   assumes mono: "\<And>x. monotone (fun_ord Heap_ord) Heap_ord (\<lambda>f. U (F (C f)) x)"
       
   471   assumes eq: "f \<equiv> C (ccpo.fixp (fun_lub Heap_lub) (fun_ord Heap_ord) (\<lambda>f. U (F (C f))))"
       
   472   assumes inverse2: "\<And>f. U (C f) = f"
       
   473   assumes step: "\<And>f x h h' r. (\<And>x h h' r. effect (U f x) h h' r \<Longrightarrow> P x h h' r) 
       
   474     \<Longrightarrow> effect (U (F f) x) h h' r \<Longrightarrow> P x h h' r"
       
   475   assumes defined: "effect (U f x) h h' r"
       
   476   shows "P x h h' r"
       
   477   using step defined heap.fixp_induct_uc[of U F C, OF mono eq inverse2 admissible_heap, of P]
       
   478   by blast
       
   479 
   431 declaration {* Partial_Function.init "heap" @{term heap.fixp_fun}
   480 declaration {* Partial_Function.init "heap" @{term heap.fixp_fun}
   432   @{term heap.mono_body} @{thm heap.fixp_rule_uc} NONE *}
   481   @{term heap.mono_body} @{thm heap.fixp_rule_uc} (SOME @{thm fixp_induct_heap}) *}
   433 
   482 
   434 
   483 
   435 abbreviation "mono_Heap \<equiv> monotone (fun_ord Heap_ord) Heap_ord"
   484 abbreviation "mono_Heap \<equiv> monotone (fun_ord Heap_ord) Heap_ord"
   436 
   485 
   437 lemma Heap_ordI:
   486 lemma Heap_ordI: