src/HOL/Imperative_HOL/Heap_Monad.thy
changeset 37792 ba0bc31b90d7
parent 37787 30dc3abf4a58
child 37816 e550439d4422
     1.1 --- a/src/HOL/Imperative_HOL/Heap_Monad.thy	Tue Jul 13 11:50:22 2010 +0200
     1.2 +++ b/src/HOL/Imperative_HOL/Heap_Monad.thy	Tue Jul 13 12:00:11 2010 +0200
     1.3 @@ -5,7 +5,7 @@
     1.4  header {* A monad with a polymorphic heap and primitive reasoning infrastructure *}
     1.5  
     1.6  theory Heap_Monad
     1.7 -imports Heap
     1.8 +imports Heap Monad_Syntax
     1.9  begin
    1.10  
    1.11  subsection {* The monad *}
    1.12 @@ -259,12 +259,16 @@
    1.13    obtains "False"
    1.14    using assms by (rule crelE) (simp add: success_def execute_simps)
    1.15  
    1.16 -definition bind :: "'a Heap \<Rightarrow> ('a \<Rightarrow> 'b Heap) \<Rightarrow> 'b Heap" (infixl ">>=" 54) where
    1.17 -  [code del]: "f >>= g = Heap (\<lambda>h. case execute f h of
    1.18 +definition bind :: "'a Heap \<Rightarrow> ('a \<Rightarrow> 'b Heap) \<Rightarrow> 'b Heap" where
    1.19 +  [code del]: "bind f g = Heap (\<lambda>h. case execute f h of
    1.20                    Some (x, h') \<Rightarrow> execute (g x) h'
    1.21                  | None \<Rightarrow> None)"
    1.22  
    1.23 -notation bind (infixl "\<guillemotright>=" 54)
    1.24 +setup {*
    1.25 +  Adhoc_Overloading.add_variant 
    1.26 +    @{const_name Monad_Syntax.bindM} @{const_name Heap_Monad.bind}
    1.27 +*}
    1.28 +
    1.29  
    1.30  lemma execute_bind [execute_simps]:
    1.31    "execute f h = Some (x, h') \<Longrightarrow> execute (f \<guillemotright>= g) h = execute (g x) h'"
    1.32 @@ -314,92 +318,6 @@
    1.33  lemma raise_bind [simp]: "raise e \<guillemotright>= f = raise e"
    1.34    by (rule Heap_eqI) (simp add: execute_simps)
    1.35  
    1.36 -abbreviation chain :: "'a Heap \<Rightarrow> 'b Heap \<Rightarrow> 'b Heap"  (infixl ">>" 54) where
    1.37 -  "f >> g \<equiv> f >>= (\<lambda>_. g)"
    1.38 -
    1.39 -notation chain (infixl "\<guillemotright>" 54)
    1.40 -
    1.41 -
    1.42 -subsubsection {* do-syntax *}
    1.43 -
    1.44 -text {*
    1.45 -  We provide a convenient do-notation for monadic expressions
    1.46 -  well-known from Haskell.  @{const Let} is printed
    1.47 -  specially in do-expressions.
    1.48 -*}
    1.49 -
    1.50 -nonterminals do_expr
    1.51 -
    1.52 -syntax
    1.53 -  "_do" :: "do_expr \<Rightarrow> 'a"
    1.54 -    ("(do (_)//done)" [12] 100)
    1.55 -  "_bind" :: "pttrn \<Rightarrow> 'a \<Rightarrow> do_expr \<Rightarrow> do_expr"
    1.56 -    ("_ <- _;//_" [1000, 13, 12] 12)
    1.57 -  "_chain" :: "'a \<Rightarrow> do_expr \<Rightarrow> do_expr"
    1.58 -    ("_;//_" [13, 12] 12)
    1.59 -  "_let" :: "pttrn \<Rightarrow> 'a \<Rightarrow> do_expr \<Rightarrow> do_expr"
    1.60 -    ("let _ = _;//_" [1000, 13, 12] 12)
    1.61 -  "_nil" :: "'a \<Rightarrow> do_expr"
    1.62 -    ("_" [12] 12)
    1.63 -
    1.64 -syntax (xsymbols)
    1.65 -  "_bind" :: "pttrn \<Rightarrow> 'a \<Rightarrow> do_expr \<Rightarrow> do_expr"
    1.66 -    ("_ \<leftarrow> _;//_" [1000, 13, 12] 12)
    1.67 -
    1.68 -translations
    1.69 -  "_do f" => "f"
    1.70 -  "_bind x f g" => "f \<guillemotright>= (\<lambda>x. g)"
    1.71 -  "_chain f g" => "f \<guillemotright> g"
    1.72 -  "_let x t f" => "CONST Let t (\<lambda>x. f)"
    1.73 -  "_nil f" => "f"
    1.74 -
    1.75 -print_translation {*
    1.76 -let
    1.77 -  fun dest_abs_eta (Abs (abs as (_, ty, _))) =
    1.78 -        let
    1.79 -          val (v, t) = Syntax.variant_abs abs;
    1.80 -        in (Free (v, ty), t) end
    1.81 -    | dest_abs_eta t =
    1.82 -        let
    1.83 -          val (v, t) = Syntax.variant_abs ("", dummyT, t $ Bound 0);
    1.84 -        in (Free (v, dummyT), t) end;
    1.85 -  fun unfold_monad (Const (@{const_syntax bind}, _) $ f $ g) =
    1.86 -        let
    1.87 -          val (v, g') = dest_abs_eta g;
    1.88 -          val vs = fold_aterms (fn Free (v, _) => insert (op =) v | _ => I) v [];
    1.89 -          val v_used = fold_aterms
    1.90 -            (fn Free (w, _) => (fn s => s orelse member (op =) vs w) | _ => I) g' false;
    1.91 -        in if v_used then
    1.92 -          Const (@{syntax_const "_bind"}, dummyT) $ v $ f $ unfold_monad g'
    1.93 -        else
    1.94 -          Const (@{syntax_const "_chain"}, dummyT) $ f $ unfold_monad g'
    1.95 -        end
    1.96 -    | unfold_monad (Const (@{const_syntax chain}, _) $ f $ g) =
    1.97 -        Const (@{syntax_const "_chain"}, dummyT) $ f $ unfold_monad g
    1.98 -    | unfold_monad (Const (@{const_syntax Let}, _) $ f $ g) =
    1.99 -        let
   1.100 -          val (v, g') = dest_abs_eta g;
   1.101 -        in Const (@{syntax_const "_let"}, dummyT) $ v $ f $ unfold_monad g' end
   1.102 -    | unfold_monad (Const (@{const_syntax Pair}, _) $ f) =
   1.103 -        Const (@{const_syntax return}, dummyT) $ f
   1.104 -    | unfold_monad f = f;
   1.105 -  fun contains_bind (Const (@{const_syntax bind}, _) $ _ $ _) = true
   1.106 -    | contains_bind (Const (@{const_syntax Let}, _) $ _ $ Abs (_, _, t)) =
   1.107 -        contains_bind t;
   1.108 -  fun bind_monad_tr' (f::g::ts) = list_comb
   1.109 -    (Const (@{syntax_const "_do"}, dummyT) $
   1.110 -      unfold_monad (Const (@{const_syntax bind}, dummyT) $ f $ g), ts);
   1.111 -  fun Let_monad_tr' (f :: (g as Abs (_, _, g')) :: ts) =
   1.112 -    if contains_bind g' then list_comb
   1.113 -      (Const (@{syntax_const "_do"}, dummyT) $
   1.114 -        unfold_monad (Const (@{const_syntax Let}, dummyT) $ f $ g), ts)
   1.115 -    else raise Match;
   1.116 -in
   1.117 - [(@{const_syntax bind}, bind_monad_tr'),
   1.118 -  (@{const_syntax Let}, Let_monad_tr')]
   1.119 -end;
   1.120 -*}
   1.121 -
   1.122  
   1.123  subsection {* Generic combinators *}
   1.124  
   1.125 @@ -451,11 +369,11 @@
   1.126  
   1.127  primrec fold_map :: "('a \<Rightarrow> 'b Heap) \<Rightarrow> 'a list \<Rightarrow> 'b list Heap" where
   1.128    "fold_map f [] = return []"
   1.129 -| "fold_map f (x # xs) = do
   1.130 +| "fold_map f (x # xs) = do {
   1.131       y \<leftarrow> f x;
   1.132       ys \<leftarrow> fold_map f xs;
   1.133       return (y # ys)
   1.134 -   done"
   1.135 +   }"
   1.136  
   1.137  lemma fold_map_append:
   1.138    "fold_map f (xs @ ys) = fold_map f xs \<guillemotright>= (\<lambda>xs. fold_map f ys \<guillemotright>= (\<lambda>ys. return (xs @ ys)))"
   1.139 @@ -611,7 +529,7 @@
   1.140  text {* Monad *}
   1.141  
   1.142  code_type Heap (Haskell "Heap.ST/ Heap.RealWorld/ _")
   1.143 -code_monad "op \<guillemotright>=" Haskell
   1.144 +code_monad bind Haskell
   1.145  code_const return (Haskell "return")
   1.146  code_const Heap_Monad.raise' (Haskell "error/ _")
   1.147