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