src/HOL/Imperative_HOL/Heap_Monad.thy
changeset 37772 026ed2fc15d4
parent 37771 1bec64044b5e
child 37787 30dc3abf4a58
     1.1 --- a/src/HOL/Imperative_HOL/Heap_Monad.thy	Mon Jul 12 16:05:08 2010 +0200
     1.2 +++ b/src/HOL/Imperative_HOL/Heap_Monad.thy	Mon Jul 12 16:19:15 2010 +0200
     1.3 @@ -476,151 +476,6 @@
     1.4    ultimately show ?case by (simp, simp only: execute_bind(1), simp)
     1.5  qed
     1.6  
     1.7 -
     1.8 -subsubsection {* A monadic combinator for simple recursive functions *}
     1.9 -
    1.10 -text {* Using a locale to fix arguments f and g of MREC *}
    1.11 -
    1.12 -locale mrec =
    1.13 -  fixes f :: "'a \<Rightarrow> ('b + 'a) Heap"
    1.14 -  and g :: "'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b Heap"
    1.15 -begin
    1.16 -
    1.17 -function (default "\<lambda>(x, h). None") mrec :: "'a \<Rightarrow> heap \<Rightarrow> ('b \<times> heap) option" where
    1.18 -  "mrec x h = (case execute (f x) h of
    1.19 -     Some (Inl r, h') \<Rightarrow> Some (r, h')
    1.20 -   | Some (Inr s, h') \<Rightarrow> (case mrec s h' of
    1.21 -             Some (z, h'') \<Rightarrow> execute (g x s z) h''
    1.22 -           | None \<Rightarrow> None)
    1.23 -   | None \<Rightarrow> None)"
    1.24 -by auto
    1.25 -
    1.26 -lemma graph_implies_dom:
    1.27 -  "mrec_graph x y \<Longrightarrow> mrec_dom x"
    1.28 -apply (induct rule:mrec_graph.induct) 
    1.29 -apply (rule accpI)
    1.30 -apply (erule mrec_rel.cases)
    1.31 -by simp
    1.32 -
    1.33 -lemma mrec_default: "\<not> mrec_dom (x, h) \<Longrightarrow> mrec x h = None"
    1.34 -  unfolding mrec_def 
    1.35 -  by (rule fundef_default_value[OF mrec_sumC_def graph_implies_dom, of _ _ "(x, h)", simplified])
    1.36 -
    1.37 -lemma mrec_di_reverse: 
    1.38 -  assumes "\<not> mrec_dom (x, h)"
    1.39 -  shows "
    1.40 -   (case execute (f x) h of
    1.41 -     Some (Inl r, h') \<Rightarrow> False
    1.42 -   | Some (Inr s, h') \<Rightarrow> \<not> mrec_dom (s, h')
    1.43 -   | None \<Rightarrow> False
    1.44 -   )" 
    1.45 -using assms apply (auto split: option.split sum.split)
    1.46 -apply (rule ccontr)
    1.47 -apply (erule notE, rule accpI, elim mrec_rel.cases, auto)+
    1.48 -done
    1.49 -
    1.50 -lemma mrec_rule:
    1.51 -  "mrec x h = 
    1.52 -   (case execute (f x) h of
    1.53 -     Some (Inl r, h') \<Rightarrow> Some (r, h')
    1.54 -   | Some (Inr s, h') \<Rightarrow> 
    1.55 -          (case mrec s h' of
    1.56 -             Some (z, h'') \<Rightarrow> execute (g x s z) h''
    1.57 -           | None \<Rightarrow> None)
    1.58 -   | None \<Rightarrow> None
    1.59 -   )"
    1.60 -apply (cases "mrec_dom (x,h)", simp)
    1.61 -apply (frule mrec_default)
    1.62 -apply (frule mrec_di_reverse, simp)
    1.63 -by (auto split: sum.split option.split simp: mrec_default)
    1.64 -
    1.65 -definition
    1.66 -  "MREC x = Heap (mrec x)"
    1.67 -
    1.68 -lemma MREC_rule:
    1.69 -  "MREC x = 
    1.70 -  (do y \<leftarrow> f x;
    1.71 -                (case y of 
    1.72 -                Inl r \<Rightarrow> return r
    1.73 -              | Inr s \<Rightarrow> 
    1.74 -                do z \<leftarrow> MREC s ;
    1.75 -                   g x s z
    1.76 -                done) done)"
    1.77 -  unfolding MREC_def
    1.78 -  unfolding bind_def return_def
    1.79 -  apply simp
    1.80 -  apply (rule ext)
    1.81 -  apply (unfold mrec_rule[of x])
    1.82 -  by (auto split: option.splits prod.splits sum.splits)
    1.83 -
    1.84 -lemma MREC_pinduct:
    1.85 -  assumes "execute (MREC x) h = Some (r, h')"
    1.86 -  assumes non_rec_case: "\<And> x h h' r. execute (f x) h = Some (Inl r, h') \<Longrightarrow> P x h h' r"
    1.87 -  assumes rec_case: "\<And> x h h1 h2 h' s z r. execute (f x) h = Some (Inr s, h1) \<Longrightarrow> execute (MREC s) h1 = Some (z, h2) \<Longrightarrow> P s h1 h2 z
    1.88 -    \<Longrightarrow> execute (g x s z) h2 = Some (r, h') \<Longrightarrow> P x h h' r"
    1.89 -  shows "P x h h' r"
    1.90 -proof -
    1.91 -  from assms(1) have mrec: "mrec x h = Some (r, h')"
    1.92 -    unfolding MREC_def execute.simps .
    1.93 -  from mrec have dom: "mrec_dom (x, h)"
    1.94 -    apply -
    1.95 -    apply (rule ccontr)
    1.96 -    apply (drule mrec_default) by auto
    1.97 -  from mrec have h'_r: "h' = snd (the (mrec x h))" "r = fst (the (mrec x h))"
    1.98 -    by auto
    1.99 -  from mrec have "P x h (snd (the (mrec x h))) (fst (the (mrec x h)))"
   1.100 -  proof (induct arbitrary: r h' rule: mrec.pinduct[OF dom])
   1.101 -    case (1 x h)
   1.102 -    obtain rr h' where "the (mrec x h) = (rr, h')" by fastsimp
   1.103 -    show ?case
   1.104 -    proof (cases "execute (f x) h")
   1.105 -      case (Some result)
   1.106 -      then obtain a h1 where exec_f: "execute (f x) h = Some (a, h1)" by fastsimp
   1.107 -      note Inl' = this
   1.108 -      show ?thesis
   1.109 -      proof (cases a)
   1.110 -        case (Inl aa)
   1.111 -        from this Inl' 1(1) exec_f mrec non_rec_case show ?thesis
   1.112 -          by auto
   1.113 -      next
   1.114 -        case (Inr b)
   1.115 -        note Inr' = this
   1.116 -        show ?thesis
   1.117 -        proof (cases "mrec b h1")
   1.118 -          case (Some result)
   1.119 -          then obtain aaa h2 where mrec_rec: "mrec b h1 = Some (aaa, h2)" by fastsimp
   1.120 -          moreover from this have "P b h1 (snd (the (mrec b h1))) (fst (the (mrec b h1)))"
   1.121 -            apply (intro 1(2))
   1.122 -            apply (auto simp add: Inr Inl')
   1.123 -            done
   1.124 -          moreover note mrec mrec_rec exec_f Inl' Inr' 1(1) 1(3)
   1.125 -          ultimately show ?thesis
   1.126 -            apply auto
   1.127 -            apply (rule rec_case)
   1.128 -            apply auto
   1.129 -            unfolding MREC_def by auto
   1.130 -        next
   1.131 -          case None
   1.132 -          from this 1(1) exec_f mrec Inr' 1(3) show ?thesis by auto
   1.133 -        qed
   1.134 -      qed
   1.135 -    next
   1.136 -      case None
   1.137 -      from this 1(1) mrec 1(3) show ?thesis by simp
   1.138 -    qed
   1.139 -  qed
   1.140 -  from this h'_r show ?thesis by simp
   1.141 -qed
   1.142 -
   1.143 -end
   1.144 -
   1.145 -text {* Providing global versions of the constant and the theorems *}
   1.146 -
   1.147 -abbreviation "MREC == mrec.MREC"
   1.148 -lemmas MREC_rule = mrec.MREC_rule
   1.149 -lemmas MREC_pinduct = mrec.MREC_pinduct
   1.150 -
   1.151 -
   1.152  subsection {* Code generator setup *}
   1.153  
   1.154  subsubsection {* Logical intermediate layer *}