src/HOL/Imperative_HOL/Heap_Monad.thy
author haftmann
Mon, 05 Jul 2010 16:46:23 +0200
changeset 37719 271ecd4fb9f9
parent 37709 70fafefbcc98
child 37724 6607ccf77946
permissions -rw-r--r--
moved "open" operations from Heap.thy to Array.thy and Ref.thy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
     1
(*  Title:      HOL/Library/Heap_Monad.thy
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
     2
    Author:     John Matthews, Galois Connections; Alexander Krauss, Lukas Bulwahn & Florian Haftmann, TU Muenchen
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
     3
*)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
     4
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
     5
header {* A monad with a polymorphic heap *}
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
     6
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
     7
theory Heap_Monad
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
     8
imports Heap
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
     9
begin
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    10
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    11
subsection {* The monad *}
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    12
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    13
subsubsection {* Monad combinators *}
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    14
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    15
text {* Monadic heap actions either produce values
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    16
  and transform the heap, or fail *}
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
    17
datatype 'a Heap = Heap "heap \<Rightarrow> ('a \<times> heap) option"
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    18
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
    19
primrec execute :: "'a Heap \<Rightarrow> heap \<Rightarrow> ('a \<times> heap) option" where
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
    20
  [code del]: "execute (Heap f) = f"
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    21
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    22
lemma Heap_execute [simp]:
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    23
  "Heap (execute f) = f" by (cases f) simp_all
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    24
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    25
lemma Heap_eqI:
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    26
  "(\<And>h. execute f h = execute g h) \<Longrightarrow> f = g"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    27
    by (cases f, cases g) (auto simp: expand_fun_eq)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    28
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    29
lemma Heap_eqI':
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    30
  "(\<And>h. (\<lambda>x. execute (f x) h) = (\<lambda>y. execute (g y) h)) \<Longrightarrow> f = g"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    31
    by (auto simp: expand_fun_eq intro: Heap_eqI)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    32
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
    33
definition heap :: "(heap \<Rightarrow> 'a \<times> heap) \<Rightarrow> 'a Heap" where
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
    34
  [code del]: "heap f = Heap (Some \<circ> f)"
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    35
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    36
lemma execute_heap [simp]:
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
    37
  "execute (heap f) = Some \<circ> f"
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    38
  by (simp add: heap_def)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    39
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
    40
lemma heap_cases [case_names succeed fail]:
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
    41
  fixes f and h
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
    42
  assumes succeed: "\<And>x h'. execute f h = Some (x, h') \<Longrightarrow> P"
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
    43
  assumes fail: "execute f h = None \<Longrightarrow> P"
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
    44
  shows P
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
    45
  using assms by (cases "execute f h") auto
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    46
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
    47
definition return :: "'a \<Rightarrow> 'a Heap" where
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    48
  [code del]: "return x = heap (Pair x)"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    49
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    50
lemma execute_return [simp]:
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
    51
  "execute (return x) = Some \<circ> Pair x"
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    52
  by (simp add: return_def)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    53
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
    54
definition raise :: "string \<Rightarrow> 'a Heap" where -- {* the string is just decoration *}
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
    55
  [code del]: "raise s = Heap (\<lambda>_. None)"
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    56
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    57
lemma execute_raise [simp]:
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
    58
  "execute (raise s) = (\<lambda>_. None)"
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    59
  by (simp add: raise_def)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    60
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
    61
definition bindM :: "'a Heap \<Rightarrow> ('a \<Rightarrow> 'b Heap) \<Rightarrow> 'b Heap" (infixl ">>=" 54) where
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
    62
  [code del]: "f >>= g = Heap (\<lambda>h. case execute f h of
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
    63
                  Some (x, h') \<Rightarrow> execute (g x) h'
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
    64
                | None \<Rightarrow> None)"
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
    65
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
    66
notation bindM (infixl "\<guillemotright>=" 54)
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
    67
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
    68
lemma execute_bind [simp]:
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
    69
  "execute f h = Some (x, h') \<Longrightarrow> execute (f \<guillemotright>= g) h = execute (g x) h'"
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
    70
  "execute f h = None \<Longrightarrow> execute (f \<guillemotright>= g) h = None"
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
    71
  by (simp_all add: bindM_def)
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
    72
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
    73
lemma execute_bind_heap [simp]:
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
    74
  "execute (heap f \<guillemotright>= g) h = execute (g (fst (f h))) (snd (f h))"
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
    75
  by (simp add: bindM_def split_def)
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
    76
  
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
    77
lemma return_bind [simp]: "return x \<guillemotright>= f = f x"
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
    78
  by (rule Heap_eqI) simp
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
    79
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
    80
lemma bind_return [simp]: "f \<guillemotright>= return = f"
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
    81
  by (rule Heap_eqI) (simp add: bindM_def split: option.splits)
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
    82
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
    83
lemma bind_bind [simp]: "(f \<guillemotright>= g) \<guillemotright>= k = f \<guillemotright>= (\<lambda>x. g x \<guillemotright>= k)"
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
    84
  by (rule Heap_eqI) (simp add: bindM_def split: option.splits)
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
    85
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
    86
lemma raise_bind [simp]: "raise e \<guillemotright>= f = raise e"
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
    87
  by (rule Heap_eqI) simp
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
    88
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
    89
abbreviation chainM :: "'a Heap \<Rightarrow> 'b Heap \<Rightarrow> 'b Heap"  (infixl ">>" 54) where
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
    90
  "f >> g \<equiv> f >>= (\<lambda>_. g)"
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
    91
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
    92
notation chainM (infixl "\<guillemotright>" 54)
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
    93
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    94
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    95
subsubsection {* do-syntax *}
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    96
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    97
text {*
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    98
  We provide a convenient do-notation for monadic expressions
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    99
  well-known from Haskell.  @{const Let} is printed
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   100
  specially in do-expressions.
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   101
*}
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   102
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   103
nonterminals do_expr
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   104
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   105
syntax
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   106
  "_do" :: "do_expr \<Rightarrow> 'a"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   107
    ("(do (_)//done)" [12] 100)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   108
  "_bindM" :: "pttrn \<Rightarrow> 'a \<Rightarrow> do_expr \<Rightarrow> do_expr"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   109
    ("_ <- _;//_" [1000, 13, 12] 12)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   110
  "_chainM" :: "'a \<Rightarrow> do_expr \<Rightarrow> do_expr"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   111
    ("_;//_" [13, 12] 12)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   112
  "_let" :: "pttrn \<Rightarrow> 'a \<Rightarrow> do_expr \<Rightarrow> do_expr"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   113
    ("let _ = _;//_" [1000, 13, 12] 12)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   114
  "_nil" :: "'a \<Rightarrow> do_expr"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   115
    ("_" [12] 12)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   116
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   117
syntax (xsymbols)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   118
  "_bindM" :: "pttrn \<Rightarrow> 'a \<Rightarrow> do_expr \<Rightarrow> do_expr"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   119
    ("_ \<leftarrow> _;//_" [1000, 13, 12] 12)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   120
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   121
translations
28145
af3923ed4786 dropped "run" marker in monad syntax
haftmann
parents: 28054
diff changeset
   122
  "_do f" => "f"
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   123
  "_bindM x f g" => "f \<guillemotright>= (\<lambda>x. g)"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   124
  "_chainM f g" => "f \<guillemotright> g"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   125
  "_let x t f" => "CONST Let t (\<lambda>x. f)"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   126
  "_nil f" => "f"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   127
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   128
print_translation {*
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   129
let
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   130
  fun dest_abs_eta (Abs (abs as (_, ty, _))) =
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   131
        let
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   132
          val (v, t) = Syntax.variant_abs abs;
28145
af3923ed4786 dropped "run" marker in monad syntax
haftmann
parents: 28054
diff changeset
   133
        in (Free (v, ty), t) end
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   134
    | dest_abs_eta t =
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   135
        let
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   136
          val (v, t) = Syntax.variant_abs ("", dummyT, t $ Bound 0);
28145
af3923ed4786 dropped "run" marker in monad syntax
haftmann
parents: 28054
diff changeset
   137
        in (Free (v, dummyT), t) end;
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   138
  fun unfold_monad (Const (@{const_syntax bindM}, _) $ f $ g) =
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   139
        let
28145
af3923ed4786 dropped "run" marker in monad syntax
haftmann
parents: 28054
diff changeset
   140
          val (v, g') = dest_abs_eta g;
af3923ed4786 dropped "run" marker in monad syntax
haftmann
parents: 28054
diff changeset
   141
          val vs = fold_aterms (fn Free (v, _) => insert (op =) v | _ => I) v [];
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   142
          val v_used = fold_aterms
28145
af3923ed4786 dropped "run" marker in monad syntax
haftmann
parents: 28054
diff changeset
   143
            (fn Free (w, _) => (fn s => s orelse member (op =) vs w) | _ => I) g' false;
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   144
        in if v_used then
35113
1a0c129bb2e0 modernized translations;
wenzelm
parents: 34051
diff changeset
   145
          Const (@{syntax_const "_bindM"}, dummyT) $ v $ f $ unfold_monad g'
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   146
        else
35113
1a0c129bb2e0 modernized translations;
wenzelm
parents: 34051
diff changeset
   147
          Const (@{syntax_const "_chainM"}, dummyT) $ f $ unfold_monad g'
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   148
        end
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   149
    | unfold_monad (Const (@{const_syntax chainM}, _) $ f $ g) =
35113
1a0c129bb2e0 modernized translations;
wenzelm
parents: 34051
diff changeset
   150
        Const (@{syntax_const "_chainM"}, dummyT) $ f $ unfold_monad g
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   151
    | unfold_monad (Const (@{const_syntax Let}, _) $ f $ g) =
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   152
        let
28145
af3923ed4786 dropped "run" marker in monad syntax
haftmann
parents: 28054
diff changeset
   153
          val (v, g') = dest_abs_eta g;
35113
1a0c129bb2e0 modernized translations;
wenzelm
parents: 34051
diff changeset
   154
        in Const (@{syntax_const "_let"}, dummyT) $ v $ f $ unfold_monad g' end
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   155
    | unfold_monad (Const (@{const_syntax Pair}, _) $ f) =
28145
af3923ed4786 dropped "run" marker in monad syntax
haftmann
parents: 28054
diff changeset
   156
        Const (@{const_syntax return}, dummyT) $ f
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   157
    | unfold_monad f = f;
28145
af3923ed4786 dropped "run" marker in monad syntax
haftmann
parents: 28054
diff changeset
   158
  fun contains_bindM (Const (@{const_syntax bindM}, _) $ _ $ _) = true
af3923ed4786 dropped "run" marker in monad syntax
haftmann
parents: 28054
diff changeset
   159
    | contains_bindM (Const (@{const_syntax Let}, _) $ _ $ Abs (_, _, t)) =
af3923ed4786 dropped "run" marker in monad syntax
haftmann
parents: 28054
diff changeset
   160
        contains_bindM t;
af3923ed4786 dropped "run" marker in monad syntax
haftmann
parents: 28054
diff changeset
   161
  fun bindM_monad_tr' (f::g::ts) = list_comb
35113
1a0c129bb2e0 modernized translations;
wenzelm
parents: 34051
diff changeset
   162
    (Const (@{syntax_const "_do"}, dummyT) $
1a0c129bb2e0 modernized translations;
wenzelm
parents: 34051
diff changeset
   163
      unfold_monad (Const (@{const_syntax bindM}, dummyT) $ f $ g), ts);
1a0c129bb2e0 modernized translations;
wenzelm
parents: 34051
diff changeset
   164
  fun Let_monad_tr' (f :: (g as Abs (_, _, g')) :: ts) =
1a0c129bb2e0 modernized translations;
wenzelm
parents: 34051
diff changeset
   165
    if contains_bindM g' then list_comb
1a0c129bb2e0 modernized translations;
wenzelm
parents: 34051
diff changeset
   166
      (Const (@{syntax_const "_do"}, dummyT) $
1a0c129bb2e0 modernized translations;
wenzelm
parents: 34051
diff changeset
   167
        unfold_monad (Const (@{const_syntax Let}, dummyT) $ f $ g), ts)
28145
af3923ed4786 dropped "run" marker in monad syntax
haftmann
parents: 28054
diff changeset
   168
    else raise Match;
35113
1a0c129bb2e0 modernized translations;
wenzelm
parents: 34051
diff changeset
   169
in
1a0c129bb2e0 modernized translations;
wenzelm
parents: 34051
diff changeset
   170
 [(@{const_syntax bindM}, bindM_monad_tr'),
1a0c129bb2e0 modernized translations;
wenzelm
parents: 34051
diff changeset
   171
  (@{const_syntax Let}, Let_monad_tr')]
1a0c129bb2e0 modernized translations;
wenzelm
parents: 34051
diff changeset
   172
end;
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   173
*}
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   174
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   175
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   176
subsection {* Monad properties *}
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   177
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   178
subsection {* Generic combinators *}
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   179
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   180
definition assert :: "('a \<Rightarrow> bool) \<Rightarrow> 'a \<Rightarrow> 'a Heap" where
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   181
  "assert P x = (if P x then return x else raise ''assert'')"
28742
07073b1087dd moved assert to Heap_Monad.thy
haftmann
parents: 28663
diff changeset
   182
07073b1087dd moved assert to Heap_Monad.thy
haftmann
parents: 28663
diff changeset
   183
lemma assert_cong [fundef_cong]:
07073b1087dd moved assert to Heap_Monad.thy
haftmann
parents: 28663
diff changeset
   184
  assumes "P = P'"
07073b1087dd moved assert to Heap_Monad.thy
haftmann
parents: 28663
diff changeset
   185
  assumes "\<And>x. P' x \<Longrightarrow> f x = f' x"
07073b1087dd moved assert to Heap_Monad.thy
haftmann
parents: 28663
diff changeset
   186
  shows "(assert P x >>= f) = (assert P' x >>= f')"
07073b1087dd moved assert to Heap_Monad.thy
haftmann
parents: 28663
diff changeset
   187
  using assms by (auto simp add: assert_def return_bind raise_bind)
07073b1087dd moved assert to Heap_Monad.thy
haftmann
parents: 28663
diff changeset
   188
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   189
definition liftM :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a \<Rightarrow> 'b Heap" where
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   190
  "liftM f = return o f"
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   191
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   192
lemma liftM_collapse [simp]:
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   193
  "liftM f x = return (f x)"
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   194
  by (simp add: liftM_def)
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   195
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   196
lemma bind_liftM:
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   197
  "(f \<guillemotright>= liftM g) = (f \<guillemotright>= (\<lambda>x. return (g x)))"
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   198
  by (simp add: liftM_def comp_def)
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   199
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   200
primrec mapM :: "('a \<Rightarrow> 'b Heap) \<Rightarrow> 'a list \<Rightarrow> 'b list Heap" where
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   201
  "mapM f [] = return []"
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   202
| "mapM f (x#xs) = do
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   203
     y \<leftarrow> f x;
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   204
     ys \<leftarrow> mapM f xs;
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   205
     return (y # ys)
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   206
   done"
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   207
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   208
34051
1a82e2e29d67 added Imperative_HOL examples; added tail-recursive combinator for monadic heap functions; adopted code generation of references; added lemmas
bulwahn
parents: 32069
diff changeset
   209
subsubsection {* A monadic combinator for simple recursive functions *}
36057
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   210
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   211
text {* Using a locale to fix arguments f and g of MREC *}
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   212
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   213
locale mrec =
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   214
  fixes f :: "'a \<Rightarrow> ('b + 'a) Heap"
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   215
  and g :: "'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b Heap"
36057
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   216
begin
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   217
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   218
function (default "\<lambda>(x, h). None") mrec :: "'a \<Rightarrow> heap \<Rightarrow> ('b \<times> heap) option" where
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   219
  "mrec x h = (case execute (f x) h of
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   220
     Some (Inl r, h') \<Rightarrow> Some (r, h')
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   221
   | Some (Inr s, h') \<Rightarrow> (case mrec s h' of
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   222
             Some (z, h'') \<Rightarrow> execute (g x s z) h''
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   223
           | None \<Rightarrow> None)
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   224
   | None \<Rightarrow> None)"
34051
1a82e2e29d67 added Imperative_HOL examples; added tail-recursive combinator for monadic heap functions; adopted code generation of references; added lemmas
bulwahn
parents: 32069
diff changeset
   225
by auto
1a82e2e29d67 added Imperative_HOL examples; added tail-recursive combinator for monadic heap functions; adopted code generation of references; added lemmas
bulwahn
parents: 32069
diff changeset
   226
1a82e2e29d67 added Imperative_HOL examples; added tail-recursive combinator for monadic heap functions; adopted code generation of references; added lemmas
bulwahn
parents: 32069
diff changeset
   227
lemma graph_implies_dom:
35423
6ef9525a5727 eliminated hard tabs;
wenzelm
parents: 35113
diff changeset
   228
  "mrec_graph x y \<Longrightarrow> mrec_dom x"
34051
1a82e2e29d67 added Imperative_HOL examples; added tail-recursive combinator for monadic heap functions; adopted code generation of references; added lemmas
bulwahn
parents: 32069
diff changeset
   229
apply (induct rule:mrec_graph.induct) 
1a82e2e29d67 added Imperative_HOL examples; added tail-recursive combinator for monadic heap functions; adopted code generation of references; added lemmas
bulwahn
parents: 32069
diff changeset
   230
apply (rule accpI)
1a82e2e29d67 added Imperative_HOL examples; added tail-recursive combinator for monadic heap functions; adopted code generation of references; added lemmas
bulwahn
parents: 32069
diff changeset
   231
apply (erule mrec_rel.cases)
1a82e2e29d67 added Imperative_HOL examples; added tail-recursive combinator for monadic heap functions; adopted code generation of references; added lemmas
bulwahn
parents: 32069
diff changeset
   232
by simp
1a82e2e29d67 added Imperative_HOL examples; added tail-recursive combinator for monadic heap functions; adopted code generation of references; added lemmas
bulwahn
parents: 32069
diff changeset
   233
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   234
lemma mrec_default: "\<not> mrec_dom (x, h) \<Longrightarrow> mrec x h = None"
35423
6ef9525a5727 eliminated hard tabs;
wenzelm
parents: 35113
diff changeset
   235
  unfolding mrec_def 
36057
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   236
  by (rule fundef_default_value[OF mrec_sumC_def graph_implies_dom, of _ _ "(x, h)", simplified])
34051
1a82e2e29d67 added Imperative_HOL examples; added tail-recursive combinator for monadic heap functions; adopted code generation of references; added lemmas
bulwahn
parents: 32069
diff changeset
   237
36057
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   238
lemma mrec_di_reverse: 
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   239
  assumes "\<not> mrec_dom (x, h)"
34051
1a82e2e29d67 added Imperative_HOL examples; added tail-recursive combinator for monadic heap functions; adopted code generation of references; added lemmas
bulwahn
parents: 32069
diff changeset
   240
  shows "
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   241
   (case execute (f x) h of
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   242
     Some (Inl r, h') \<Rightarrow> False
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   243
   | Some (Inr s, h') \<Rightarrow> \<not> mrec_dom (s, h')
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   244
   | None \<Rightarrow> False
34051
1a82e2e29d67 added Imperative_HOL examples; added tail-recursive combinator for monadic heap functions; adopted code generation of references; added lemmas
bulwahn
parents: 32069
diff changeset
   245
   )" 
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   246
using assms apply (auto split: option.split sum.split)
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   247
apply (rule ccontr)
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   248
apply (erule notE, rule accpI, elim mrec_rel.cases, auto)+
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   249
done
34051
1a82e2e29d67 added Imperative_HOL examples; added tail-recursive combinator for monadic heap functions; adopted code generation of references; added lemmas
bulwahn
parents: 32069
diff changeset
   250
1a82e2e29d67 added Imperative_HOL examples; added tail-recursive combinator for monadic heap functions; adopted code generation of references; added lemmas
bulwahn
parents: 32069
diff changeset
   251
lemma mrec_rule:
36057
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   252
  "mrec x h = 
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   253
   (case execute (f x) h of
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   254
     Some (Inl r, h') \<Rightarrow> Some (r, h')
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   255
   | Some (Inr s, h') \<Rightarrow> 
36057
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   256
          (case mrec s h' of
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   257
             Some (z, h'') \<Rightarrow> execute (g x s z) h''
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   258
           | None \<Rightarrow> None)
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   259
   | None \<Rightarrow> None
34051
1a82e2e29d67 added Imperative_HOL examples; added tail-recursive combinator for monadic heap functions; adopted code generation of references; added lemmas
bulwahn
parents: 32069
diff changeset
   260
   )"
36057
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   261
apply (cases "mrec_dom (x,h)", simp)
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   262
apply (frule mrec_default)
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   263
apply (frule mrec_di_reverse, simp)
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   264
by (auto split: sum.split option.split simp: mrec_default)
34051
1a82e2e29d67 added Imperative_HOL examples; added tail-recursive combinator for monadic heap functions; adopted code generation of references; added lemmas
bulwahn
parents: 32069
diff changeset
   265
1a82e2e29d67 added Imperative_HOL examples; added tail-recursive combinator for monadic heap functions; adopted code generation of references; added lemmas
bulwahn
parents: 32069
diff changeset
   266
definition
36057
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   267
  "MREC x = Heap (mrec x)"
34051
1a82e2e29d67 added Imperative_HOL examples; added tail-recursive combinator for monadic heap functions; adopted code generation of references; added lemmas
bulwahn
parents: 32069
diff changeset
   268
1a82e2e29d67 added Imperative_HOL examples; added tail-recursive combinator for monadic heap functions; adopted code generation of references; added lemmas
bulwahn
parents: 32069
diff changeset
   269
lemma MREC_rule:
36057
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   270
  "MREC x = 
34051
1a82e2e29d67 added Imperative_HOL examples; added tail-recursive combinator for monadic heap functions; adopted code generation of references; added lemmas
bulwahn
parents: 32069
diff changeset
   271
  (do y \<leftarrow> f x;
1a82e2e29d67 added Imperative_HOL examples; added tail-recursive combinator for monadic heap functions; adopted code generation of references; added lemmas
bulwahn
parents: 32069
diff changeset
   272
                (case y of 
1a82e2e29d67 added Imperative_HOL examples; added tail-recursive combinator for monadic heap functions; adopted code generation of references; added lemmas
bulwahn
parents: 32069
diff changeset
   273
                Inl r \<Rightarrow> return r
1a82e2e29d67 added Imperative_HOL examples; added tail-recursive combinator for monadic heap functions; adopted code generation of references; added lemmas
bulwahn
parents: 32069
diff changeset
   274
              | Inr s \<Rightarrow> 
36057
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   275
                do z \<leftarrow> MREC s ;
34051
1a82e2e29d67 added Imperative_HOL examples; added tail-recursive combinator for monadic heap functions; adopted code generation of references; added lemmas
bulwahn
parents: 32069
diff changeset
   276
                   g x s z
1a82e2e29d67 added Imperative_HOL examples; added tail-recursive combinator for monadic heap functions; adopted code generation of references; added lemmas
bulwahn
parents: 32069
diff changeset
   277
                done) done)"
1a82e2e29d67 added Imperative_HOL examples; added tail-recursive combinator for monadic heap functions; adopted code generation of references; added lemmas
bulwahn
parents: 32069
diff changeset
   278
  unfolding MREC_def
1a82e2e29d67 added Imperative_HOL examples; added tail-recursive combinator for monadic heap functions; adopted code generation of references; added lemmas
bulwahn
parents: 32069
diff changeset
   279
  unfolding bindM_def return_def
1a82e2e29d67 added Imperative_HOL examples; added tail-recursive combinator for monadic heap functions; adopted code generation of references; added lemmas
bulwahn
parents: 32069
diff changeset
   280
  apply simp
1a82e2e29d67 added Imperative_HOL examples; added tail-recursive combinator for monadic heap functions; adopted code generation of references; added lemmas
bulwahn
parents: 32069
diff changeset
   281
  apply (rule ext)
36057
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   282
  apply (unfold mrec_rule[of x])
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   283
  by (auto split: option.splits prod.splits sum.splits)
36057
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   284
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   285
lemma MREC_pinduct:
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   286
  assumes "execute (MREC x) h = Some (r, h')"
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   287
  assumes non_rec_case: "\<And> x h h' r. execute (f x) h = Some (Inl r, h') \<Longrightarrow> P x h h' r"
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   288
  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
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   289
    \<Longrightarrow> execute (g x s z) h2 = Some (r, h') \<Longrightarrow> P x h h' r"
36057
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   290
  shows "P x h h' r"
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   291
proof -
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   292
  from assms(1) have mrec: "mrec x h = Some (r, h')"
36057
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   293
    unfolding MREC_def execute.simps .
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   294
  from mrec have dom: "mrec_dom (x, h)"
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   295
    apply -
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   296
    apply (rule ccontr)
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   297
    apply (drule mrec_default) by auto
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   298
  from mrec have h'_r: "h' = snd (the (mrec x h))" "r = fst (the (mrec x h))"
36057
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   299
    by auto
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   300
  from mrec have "P x h (snd (the (mrec x h))) (fst (the (mrec x h)))"
36057
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   301
  proof (induct arbitrary: r h' rule: mrec.pinduct[OF dom])
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   302
    case (1 x h)
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   303
    obtain rr h' where "the (mrec x h) = (rr, h')" by fastsimp
36057
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   304
    show ?case
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   305
    proof (cases "execute (f x) h")
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   306
      case (Some result)
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   307
      then obtain a h1 where exec_f: "execute (f x) h = Some (a, h1)" by fastsimp
36057
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   308
      note Inl' = this
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   309
      show ?thesis
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   310
      proof (cases a)
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   311
        case (Inl aa)
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   312
        from this Inl' 1(1) exec_f mrec non_rec_case show ?thesis
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   313
          by auto
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   314
      next
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   315
        case (Inr b)
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   316
        note Inr' = this
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   317
        show ?thesis
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   318
        proof (cases "mrec b h1")
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   319
          case (Some result)
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   320
          then obtain aaa h2 where mrec_rec: "mrec b h1 = Some (aaa, h2)" by fastsimp
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   321
          moreover from this have "P b h1 (snd (the (mrec b h1))) (fst (the (mrec b h1)))"
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   322
            apply (intro 1(2))
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   323
            apply (auto simp add: Inr Inl')
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   324
            done
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   325
          moreover note mrec mrec_rec exec_f Inl' Inr' 1(1) 1(3)
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   326
          ultimately show ?thesis
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   327
            apply auto
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   328
            apply (rule rec_case)
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   329
            apply auto
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   330
            unfolding MREC_def by auto
36057
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   331
        next
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   332
          case None
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   333
          from this 1(1) exec_f mrec Inr' 1(3) show ?thesis by auto
36057
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   334
        qed
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   335
      qed
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   336
    next
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   337
      case None
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   338
      from this 1(1) mrec 1(3) show ?thesis by simp
36057
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   339
    qed
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   340
  qed
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   341
  from this h'_r show ?thesis by simp
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   342
qed
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   343
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   344
end
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   345
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   346
text {* Providing global versions of the constant and the theorems *}
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   347
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   348
abbreviation "MREC == mrec.MREC"
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   349
lemmas MREC_rule = mrec.MREC_rule
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   350
lemmas MREC_pinduct = mrec.MREC_pinduct
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   351
36176
3fe7e97ccca8 replaced generic 'hide' command by more conventional 'hide_class', 'hide_type', 'hide_const', 'hide_fact' -- frees some popular keywords;
wenzelm
parents: 36078
diff changeset
   352
hide_const (open) heap execute
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   353
26182
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   354
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   355
subsection {* Code generator setup *}
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   356
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   357
subsubsection {* Logical intermediate layer *}
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   358
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   359
primrec raise' :: "String.literal \<Rightarrow> 'a Heap" where
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   360
  [code del, code_post]: "raise' (STR s) = raise s"
26182
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   361
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   362
lemma raise_raise' [code_inline]:
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   363
  "raise s = raise' (STR s)"
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   364
  by simp
26182
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   365
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   366
code_datatype raise' -- {* avoid @{const "Heap"} formally *}
26182
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   367
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   368
hide_const (open) raise'
26182
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   369
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   370
27707
54bf1fea9252 SML_imp, OCaml_imp
haftmann
parents: 27695
diff changeset
   371
subsubsection {* SML and OCaml *}
26182
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   372
26752
6b276119139b corrected ML semantics
haftmann
parents: 26182
diff changeset
   373
code_type Heap (SML "unit/ ->/ _")
27826
4e50590ea9bc changed code setup
haftmann
parents: 27707
diff changeset
   374
code_const "op \<guillemotright>=" (SML "!(fn/ f'_/ =>/ fn/ ()/ =>/ f'_/ (_/ ())/ ())")
27707
54bf1fea9252 SML_imp, OCaml_imp
haftmann
parents: 27695
diff changeset
   375
code_const return (SML "!(fn/ ()/ =>/ _)")
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   376
code_const Heap_Monad.raise' (SML "!(raise/ Fail/ _)")
26182
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   377
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   378
code_type Heap (OCaml "_")
27826
4e50590ea9bc changed code setup
haftmann
parents: 27707
diff changeset
   379
code_const "op \<guillemotright>=" (OCaml "!(fun/ f'_/ ()/ ->/ f'_/ (_/ ())/ ())")
27707
54bf1fea9252 SML_imp, OCaml_imp
haftmann
parents: 27695
diff changeset
   380
code_const return (OCaml "!(fun/ ()/ ->/ _)")
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   381
code_const Heap_Monad.raise' (OCaml "failwith/ _")
27707
54bf1fea9252 SML_imp, OCaml_imp
haftmann
parents: 27695
diff changeset
   382
31871
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   383
setup {*
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   384
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   385
let
27707
54bf1fea9252 SML_imp, OCaml_imp
haftmann
parents: 27695
diff changeset
   386
31871
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   387
open Code_Thingol;
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   388
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   389
fun imp_program naming =
27707
54bf1fea9252 SML_imp, OCaml_imp
haftmann
parents: 27695
diff changeset
   390
31871
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   391
  let
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   392
    fun is_const c = case lookup_const naming c
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   393
     of SOME c' => (fn c'' => c' = c'')
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   394
      | NONE => K false;
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   395
    val is_bindM = is_const @{const_name bindM};
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   396
    val is_return = is_const @{const_name return};
31893
7d8a89390cbf adaptated to changes in term representation
haftmann
parents: 31871
diff changeset
   397
    val dummy_name = "";
31871
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   398
    val dummy_type = ITyVar dummy_name;
31893
7d8a89390cbf adaptated to changes in term representation
haftmann
parents: 31871
diff changeset
   399
    val dummy_case_term = IVar NONE;
31871
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   400
    (*assumption: dummy values are not relevant for serialization*)
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   401
    val unitt = case lookup_const naming @{const_name Unity}
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   402
     of SOME unit' => IConst (unit', (([], []), []))
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   403
      | NONE => error ("Must include " ^ @{const_name Unity} ^ " in generated constants.");
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   404
    fun dest_abs ((v, ty) `|=> t, _) = ((v, ty), t)
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   405
      | dest_abs (t, ty) =
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   406
          let
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   407
            val vs = fold_varnames cons t [];
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   408
            val v = Name.variant vs "x";
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   409
            val ty' = (hd o fst o unfold_fun) ty;
31893
7d8a89390cbf adaptated to changes in term representation
haftmann
parents: 31871
diff changeset
   410
          in ((SOME v, ty'), t `$ IVar (SOME v)) end;
31871
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   411
    fun force (t as IConst (c, _) `$ t') = if is_return c
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   412
          then t' else t `$ unitt
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   413
      | force t = t `$ unitt;
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   414
    fun tr_bind' [(t1, _), (t2, ty2)] =
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   415
      let
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   416
        val ((v, ty), t) = dest_abs (t2, ty2);
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   417
      in ICase (((force t1, ty), [(IVar v, tr_bind'' t)]), dummy_case_term) end
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   418
    and tr_bind'' t = case unfold_app t
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   419
         of (IConst (c, (_, ty1 :: ty2 :: _)), [x1, x2]) => if is_bindM c
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   420
              then tr_bind' [(x1, ty1), (x2, ty2)]
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   421
              else force t
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   422
          | _ => force t;
31893
7d8a89390cbf adaptated to changes in term representation
haftmann
parents: 31871
diff changeset
   423
    fun imp_monad_bind'' ts = (SOME dummy_name, dummy_type) `|=> ICase (((IVar (SOME dummy_name), dummy_type),
31871
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   424
      [(unitt, tr_bind' ts)]), dummy_case_term)
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   425
    and imp_monad_bind' (const as (c, (_, tys))) ts = if is_bindM c then case (ts, tys)
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   426
       of ([t1, t2], ty1 :: ty2 :: _) => imp_monad_bind'' [(t1, ty1), (t2, ty2)]
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   427
        | ([t1, t2, t3], ty1 :: ty2 :: _) => imp_monad_bind'' [(t1, ty1), (t2, ty2)] `$ t3
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   428
        | (ts, _) => imp_monad_bind (eta_expand 2 (const, ts))
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   429
      else IConst const `$$ map imp_monad_bind ts
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   430
    and imp_monad_bind (IConst const) = imp_monad_bind' const []
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   431
      | imp_monad_bind (t as IVar _) = t
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   432
      | imp_monad_bind (t as _ `$ _) = (case unfold_app t
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   433
         of (IConst const, ts) => imp_monad_bind' const ts
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   434
          | (t, ts) => imp_monad_bind t `$$ map imp_monad_bind ts)
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   435
      | imp_monad_bind (v_ty `|=> t) = v_ty `|=> imp_monad_bind t
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   436
      | imp_monad_bind (ICase (((t, ty), pats), t0)) = ICase
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   437
          (((imp_monad_bind t, ty),
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   438
            (map o pairself) imp_monad_bind pats),
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   439
              imp_monad_bind t0);
28663
bd8438543bf2 code identifier namings are no longer imperative
haftmann
parents: 28562
diff changeset
   440
31871
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   441
  in (Graph.map_nodes o map_terms_stmt) imp_monad_bind end;
27707
54bf1fea9252 SML_imp, OCaml_imp
haftmann
parents: 27695
diff changeset
   442
54bf1fea9252 SML_imp, OCaml_imp
haftmann
parents: 27695
diff changeset
   443
in
54bf1fea9252 SML_imp, OCaml_imp
haftmann
parents: 27695
diff changeset
   444
31871
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   445
Code_Target.extend_target ("SML_imp", ("SML", imp_program))
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   446
#> Code_Target.extend_target ("OCaml_imp", ("OCaml", imp_program))
27707
54bf1fea9252 SML_imp, OCaml_imp
haftmann
parents: 27695
diff changeset
   447
54bf1fea9252 SML_imp, OCaml_imp
haftmann
parents: 27695
diff changeset
   448
end
31871
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   449
27707
54bf1fea9252 SML_imp, OCaml_imp
haftmann
parents: 27695
diff changeset
   450
*}
54bf1fea9252 SML_imp, OCaml_imp
haftmann
parents: 27695
diff changeset
   451
26182
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   452
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   453
subsubsection {* Haskell *}
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   454
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   455
text {* Adaption layer *}
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   456
29793
86cac1fab613 changed name space policy for Haskell includes
haftmann
parents: 29399
diff changeset
   457
code_include Haskell "Heap"
26182
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   458
{*import qualified Control.Monad;
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   459
import qualified Control.Monad.ST;
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   460
import qualified Data.STRef;
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   461
import qualified Data.Array.ST;
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   462
27695
033732c90ebd Haskell now living in the RealWorld
haftmann
parents: 27673
diff changeset
   463
type RealWorld = Control.Monad.ST.RealWorld;
26182
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   464
type ST s a = Control.Monad.ST.ST s a;
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   465
type STRef s a = Data.STRef.STRef s a;
27673
52056ddac194 fixed code generator setup
haftmann
parents: 26753
diff changeset
   466
type STArray s a = Data.Array.ST.STArray s Int a;
26182
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   467
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   468
newSTRef = Data.STRef.newSTRef;
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   469
readSTRef = Data.STRef.readSTRef;
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   470
writeSTRef = Data.STRef.writeSTRef;
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   471
27673
52056ddac194 fixed code generator setup
haftmann
parents: 26753
diff changeset
   472
newArray :: (Int, Int) -> a -> ST s (STArray s a);
26182
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   473
newArray = Data.Array.ST.newArray;
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   474
27673
52056ddac194 fixed code generator setup
haftmann
parents: 26753
diff changeset
   475
newListArray :: (Int, Int) -> [a] -> ST s (STArray s a);
26182
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   476
newListArray = Data.Array.ST.newListArray;
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   477
27673
52056ddac194 fixed code generator setup
haftmann
parents: 26753
diff changeset
   478
lengthArray :: STArray s a -> ST s Int;
52056ddac194 fixed code generator setup
haftmann
parents: 26753
diff changeset
   479
lengthArray a = Control.Monad.liftM snd (Data.Array.ST.getBounds a);
26182
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   480
27673
52056ddac194 fixed code generator setup
haftmann
parents: 26753
diff changeset
   481
readArray :: STArray s a -> Int -> ST s a;
26182
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   482
readArray = Data.Array.ST.readArray;
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   483
27673
52056ddac194 fixed code generator setup
haftmann
parents: 26753
diff changeset
   484
writeArray :: STArray s a -> Int -> a -> ST s ();
26182
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   485
writeArray = Data.Array.ST.writeArray;*}
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   486
29793
86cac1fab613 changed name space policy for Haskell includes
haftmann
parents: 29399
diff changeset
   487
code_reserved Haskell Heap
26182
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   488
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   489
text {* Monad *}
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   490
29793
86cac1fab613 changed name space policy for Haskell includes
haftmann
parents: 29399
diff changeset
   491
code_type Heap (Haskell "Heap.ST/ Heap.RealWorld/ _")
28145
af3923ed4786 dropped "run" marker in monad syntax
haftmann
parents: 28054
diff changeset
   492
code_monad "op \<guillemotright>=" Haskell
26182
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   493
code_const return (Haskell "return")
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   494
code_const Heap_Monad.raise' (Haskell "error/ _")
26182
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   495
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   496
end