src/HOL/Library/Heap_Monad.thy
author haftmann
Sat, 06 Sep 2008 14:02:36 +0200
changeset 28145 af3923ed4786
parent 28054 2b84d34c5d02
child 28562 4e74209f113e
permissions -rw-r--r--
dropped "run" marker in monad syntax
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
    ID:         $Id$
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
     3
    Author:     John Matthews, Galois Connections; Alexander Krauss, Lukas Bulwahn & Florian Haftmann, TU Muenchen
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
     4
*)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
     5
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
     6
header {* A monad with a polymorphic heap *}
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
     7
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
     8
theory Heap_Monad
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
     9
imports Heap
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    10
begin
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    11
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    12
subsection {* The monad *}
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    13
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    14
subsubsection {* Monad combinators *}
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    15
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    16
datatype exception = Exn
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    17
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    18
text {* Monadic heap actions either produce values
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    19
  and transform the heap, or fail *}
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    20
datatype 'a Heap = Heap "heap \<Rightarrow> ('a + exception) \<times> heap"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    21
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    22
primrec
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    23
  execute :: "'a Heap \<Rightarrow> heap \<Rightarrow> ('a + exception) \<times> heap" where
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    24
  "execute (Heap f) = f"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    25
lemmas [code del] = execute.simps
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    26
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    27
lemma Heap_execute [simp]:
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    28
  "Heap (execute f) = f" by (cases f) simp_all
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    29
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    30
lemma Heap_eqI:
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    31
  "(\<And>h. execute f h = execute g h) \<Longrightarrow> f = g"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    32
    by (cases f, cases g) (auto simp: expand_fun_eq)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    33
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    34
lemma Heap_eqI':
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    35
  "(\<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
    36
    by (auto simp: expand_fun_eq intro: Heap_eqI)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    37
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    38
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
    39
proof
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    40
  fix g :: "heap \<Rightarrow> ('a + exception) \<times> heap" 
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    41
  assume "\<And>f. PROP P f"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    42
  then show "PROP P (Heap g)" .
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    43
next
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    44
  fix f :: "'a Heap" 
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    45
  assume assm: "\<And>g. PROP P (Heap g)"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    46
  then have "PROP P (Heap (execute f))" .
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    47
  then show "PROP P f" by simp
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    48
qed
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    49
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    50
definition
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    51
  heap :: "(heap \<Rightarrow> 'a \<times> heap) \<Rightarrow> 'a Heap" where
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    52
  [code del]: "heap f = Heap (\<lambda>h. apfst Inl (f h))"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    53
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    54
lemma execute_heap [simp]:
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    55
  "execute (heap f) h = apfst Inl (f h)"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    56
  by (simp add: heap_def)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    57
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    58
definition
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    59
  bindM :: "'a Heap \<Rightarrow> ('a \<Rightarrow> 'b Heap) \<Rightarrow> 'b Heap" (infixl ">>=" 54) where
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    60
  [code del]: "f >>= g = Heap (\<lambda>h. case execute f h of
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    61
                  (Inl x, h') \<Rightarrow> execute (g x) h'
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    62
                | r \<Rightarrow> r)"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    63
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    64
notation
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    65
  bindM (infixl "\<guillemotright>=" 54)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    66
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    67
abbreviation
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    68
  chainM :: "'a Heap \<Rightarrow> 'b Heap \<Rightarrow> 'b Heap"  (infixl ">>" 54) where
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    69
  "f >> g \<equiv> f >>= (\<lambda>_. g)"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    70
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    71
notation
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    72
  chainM (infixl "\<guillemotright>" 54)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    73
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    74
definition
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    75
  return :: "'a \<Rightarrow> 'a Heap" where
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    76
  [code del]: "return x = heap (Pair x)"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    77
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    78
lemma execute_return [simp]:
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    79
  "execute (return x) h = apfst Inl (x, h)"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    80
  by (simp add: return_def)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    81
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    82
definition
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    83
  raise :: "string \<Rightarrow> 'a Heap" where -- {* the string is just decoration *}
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    84
  [code del]: "raise s = Heap (Pair (Inr Exn))"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    85
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    86
notation (latex output)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    87
  "raise" ("\<^raw:{\textsf{raise}}>")
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    88
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    89
lemma execute_raise [simp]:
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    90
  "execute (raise s) h = (Inr Exn, h)"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    91
  by (simp add: raise_def)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    92
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    93
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    94
subsubsection {* do-syntax *}
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    95
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    96
text {*
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    97
  We provide a convenient do-notation for monadic expressions
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    98
  well-known from Haskell.  @{const Let} is printed
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    99
  specially in do-expressions.
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   100
*}
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   101
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   102
nonterminals do_expr
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   103
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   104
syntax
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   105
  "_do" :: "do_expr \<Rightarrow> 'a"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   106
    ("(do (_)//done)" [12] 100)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   107
  "_bindM" :: "pttrn \<Rightarrow> 'a \<Rightarrow> do_expr \<Rightarrow> do_expr"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   108
    ("_ <- _;//_" [1000, 13, 12] 12)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   109
  "_chainM" :: "'a \<Rightarrow> do_expr \<Rightarrow> do_expr"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   110
    ("_;//_" [13, 12] 12)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   111
  "_let" :: "pttrn \<Rightarrow> 'a \<Rightarrow> do_expr \<Rightarrow> do_expr"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   112
    ("let _ = _;//_" [1000, 13, 12] 12)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   113
  "_nil" :: "'a \<Rightarrow> do_expr"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   114
    ("_" [12] 12)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   115
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   116
syntax (xsymbols)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   117
  "_bindM" :: "pttrn \<Rightarrow> 'a \<Rightarrow> do_expr \<Rightarrow> do_expr"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   118
    ("_ \<leftarrow> _;//_" [1000, 13, 12] 12)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   119
syntax (latex output)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   120
  "_do" :: "do_expr \<Rightarrow> 'a"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   121
    ("(\<^raw:{\textsf{do}}> (_))" [12] 100)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   122
  "_let" :: "pttrn \<Rightarrow> 'a \<Rightarrow> do_expr \<Rightarrow> do_expr"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   123
    ("\<^raw:\textsf{let}> _ = _;//_" [1000, 13, 12] 12)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   124
notation (latex output)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   125
  "return" ("\<^raw:{\textsf{return}}>")
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   126
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   127
translations
28145
af3923ed4786 dropped "run" marker in monad syntax
haftmann
parents: 28054
diff changeset
   128
  "_do f" => "f"
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   129
  "_bindM x f g" => "f \<guillemotright>= (\<lambda>x. g)"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   130
  "_chainM f g" => "f \<guillemotright> g"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   131
  "_let x t f" => "CONST Let t (\<lambda>x. f)"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   132
  "_nil f" => "f"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   133
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   134
print_translation {*
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   135
let
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   136
  fun dest_abs_eta (Abs (abs as (_, ty, _))) =
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   137
        let
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   138
          val (v, t) = Syntax.variant_abs abs;
28145
af3923ed4786 dropped "run" marker in monad syntax
haftmann
parents: 28054
diff changeset
   139
        in (Free (v, ty), t) end
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   140
    | dest_abs_eta t =
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   141
        let
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   142
          val (v, t) = Syntax.variant_abs ("", dummyT, t $ Bound 0);
28145
af3923ed4786 dropped "run" marker in monad syntax
haftmann
parents: 28054
diff changeset
   143
        in (Free (v, dummyT), t) end;
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   144
  fun unfold_monad (Const (@{const_syntax bindM}, _) $ f $ g) =
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   145
        let
28145
af3923ed4786 dropped "run" marker in monad syntax
haftmann
parents: 28054
diff changeset
   146
          val (v, g') = dest_abs_eta g;
af3923ed4786 dropped "run" marker in monad syntax
haftmann
parents: 28054
diff changeset
   147
          val vs = fold_aterms (fn Free (v, _) => insert (op =) v | _ => I) v [];
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   148
          val v_used = fold_aterms
28145
af3923ed4786 dropped "run" marker in monad syntax
haftmann
parents: 28054
diff changeset
   149
            (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
   150
        in if v_used then
28145
af3923ed4786 dropped "run" marker in monad syntax
haftmann
parents: 28054
diff changeset
   151
          Const ("_bindM", dummyT) $ v $ f $ unfold_monad g'
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   152
        else
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   153
          Const ("_chainM", dummyT) $ f $ unfold_monad g'
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   154
        end
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   155
    | unfold_monad (Const (@{const_syntax chainM}, _) $ f $ g) =
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   156
        Const ("_chainM", dummyT) $ f $ unfold_monad g
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   157
    | unfold_monad (Const (@{const_syntax Let}, _) $ f $ g) =
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   158
        let
28145
af3923ed4786 dropped "run" marker in monad syntax
haftmann
parents: 28054
diff changeset
   159
          val (v, g') = dest_abs_eta g;
af3923ed4786 dropped "run" marker in monad syntax
haftmann
parents: 28054
diff changeset
   160
        in Const ("_let", dummyT) $ v $ f $ unfold_monad g' end
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   161
    | unfold_monad (Const (@{const_syntax Pair}, _) $ f) =
28145
af3923ed4786 dropped "run" marker in monad syntax
haftmann
parents: 28054
diff changeset
   162
        Const (@{const_syntax return}, dummyT) $ f
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   163
    | unfold_monad f = f;
28145
af3923ed4786 dropped "run" marker in monad syntax
haftmann
parents: 28054
diff changeset
   164
  fun contains_bindM (Const (@{const_syntax bindM}, _) $ _ $ _) = true
af3923ed4786 dropped "run" marker in monad syntax
haftmann
parents: 28054
diff changeset
   165
    | contains_bindM (Const (@{const_syntax Let}, _) $ _ $ Abs (_, _, t)) =
af3923ed4786 dropped "run" marker in monad syntax
haftmann
parents: 28054
diff changeset
   166
        contains_bindM t;
af3923ed4786 dropped "run" marker in monad syntax
haftmann
parents: 28054
diff changeset
   167
  fun bindM_monad_tr' (f::g::ts) = list_comb
af3923ed4786 dropped "run" marker in monad syntax
haftmann
parents: 28054
diff changeset
   168
    (Const ("_do", dummyT) $ unfold_monad (Const (@{const_syntax bindM}, dummyT) $ f $ g), ts);
af3923ed4786 dropped "run" marker in monad syntax
haftmann
parents: 28054
diff changeset
   169
  fun Let_monad_tr' (f :: (g as Abs (_, _, g')) :: ts) = if contains_bindM g' then list_comb
af3923ed4786 dropped "run" marker in monad syntax
haftmann
parents: 28054
diff changeset
   170
      (Const ("_do", dummyT) $ unfold_monad (Const (@{const_syntax Let}, dummyT) $ f $ g), ts)
af3923ed4786 dropped "run" marker in monad syntax
haftmann
parents: 28054
diff changeset
   171
    else raise Match;
af3923ed4786 dropped "run" marker in monad syntax
haftmann
parents: 28054
diff changeset
   172
in [
af3923ed4786 dropped "run" marker in monad syntax
haftmann
parents: 28054
diff changeset
   173
  (@{const_syntax bindM}, bindM_monad_tr'),
af3923ed4786 dropped "run" marker in monad syntax
haftmann
parents: 28054
diff changeset
   174
  (@{const_syntax Let}, Let_monad_tr')
af3923ed4786 dropped "run" marker in monad syntax
haftmann
parents: 28054
diff changeset
   175
] end;
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   176
*}
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   177
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   178
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   179
subsection {* Monad properties *}
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   180
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   181
subsubsection {* Monad laws *}
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   182
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   183
lemma return_bind: "return x \<guillemotright>= f = f x"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   184
  by (simp add: bindM_def return_def)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   185
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   186
lemma bind_return: "f \<guillemotright>= return = f"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   187
proof (rule Heap_eqI)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   188
  fix h
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   189
  show "execute (f \<guillemotright>= return) h = execute f h"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   190
    by (auto simp add: bindM_def return_def split: sum.splits prod.splits)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   191
qed
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   192
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   193
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
   194
  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
   195
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   196
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
   197
  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
   198
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   199
lemma raise_bind: "raise e \<guillemotright>= f = raise e"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   200
  by (simp add: raise_def bindM_def)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   201
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   202
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   203
lemmas monad_simp = return_bind bind_return bind_bind raise_bind
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   204
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   205
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   206
subsection {* Generic combinators *}
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   207
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   208
definition
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   209
  liftM :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a \<Rightarrow> 'b Heap"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   210
where
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   211
  "liftM f = return o f"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   212
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   213
definition
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   214
  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
   215
where
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   216
  "(f >>== g) = (\<lambda>x. f x \<guillemotright>= g)"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   217
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   218
notation
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   219
  compM (infixl "\<guillemotright>==" 54)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   220
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   221
lemma liftM_collapse: "liftM f x = return (f x)"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   222
  by (simp add: liftM_def)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   223
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   224
lemma liftM_compM: "liftM f \<guillemotright>== g = g o f"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   225
  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
   226
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   227
lemma compM_return: "f \<guillemotright>== return = f"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   228
  by (simp add: compM_def monad_simp)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   229
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   230
lemma compM_compM: "(f \<guillemotright>== g) \<guillemotright>== h = f \<guillemotright>== (g \<guillemotright>== h)"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   231
  by (simp add: compM_def monad_simp)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   232
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   233
lemma liftM_bind:
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   234
  "(\<lambda>x. liftM f x \<guillemotright>= liftM g) = liftM (\<lambda>x. g (f x))"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   235
  by (rule Heap_eqI') (simp add: monad_simp liftM_def bindM_def)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   236
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   237
lemma liftM_comp:
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   238
  "liftM f o g = liftM (f o g)"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   239
  by (rule Heap_eqI') (simp add: liftM_def)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   240
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   241
lemmas monad_simp' = monad_simp liftM_compM compM_return
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   242
  compM_compM liftM_bind liftM_comp
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   243
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   244
primrec 
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   245
  mapM :: "('a \<Rightarrow> 'b Heap) \<Rightarrow> 'a list \<Rightarrow> 'b list Heap"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   246
where
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   247
  "mapM f [] = return []"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   248
  | "mapM f (x#xs) = do y \<leftarrow> f x;
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   249
                        ys \<leftarrow> mapM f xs;
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   250
                        return (y # ys)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   251
                     done"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   252
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   253
primrec
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   254
  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
   255
where
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   256
  "foldM f [] s = return s"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   257
  | "foldM f (x#xs) s = f x s \<guillemotright>= foldM f xs"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   258
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   259
hide (open) const heap execute
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   260
26182
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   261
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   262
subsection {* Code generator setup *}
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   263
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   264
subsubsection {* Logical intermediate layer *}
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   265
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   266
definition
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   267
  Fail :: "message_string \<Rightarrow> exception"
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   268
where
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   269
  [code func del]: "Fail s = Exn"
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   270
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   271
definition
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   272
  raise_exc :: "exception \<Rightarrow> 'a Heap"
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   273
where
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   274
  [code func del]: "raise_exc e = raise []"
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   275
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   276
lemma raise_raise_exc [code func, code inline]:
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   277
  "raise s = raise_exc (Fail (STR s))"
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   278
  unfolding Fail_def raise_exc_def raise_def ..
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   279
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   280
hide (open) const Fail raise_exc
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   281
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   282
27707
54bf1fea9252 SML_imp, OCaml_imp
haftmann
parents: 27695
diff changeset
   283
subsubsection {* SML and OCaml *}
26182
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   284
26752
6b276119139b corrected ML semantics
haftmann
parents: 26182
diff changeset
   285
code_type Heap (SML "unit/ ->/ _")
26182
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   286
code_const Heap (SML "raise/ (Fail/ \"bare Heap\")")
27826
4e50590ea9bc changed code setup
haftmann
parents: 27707
diff changeset
   287
code_const "op \<guillemotright>=" (SML "!(fn/ f'_/ =>/ fn/ ()/ =>/ f'_/ (_/ ())/ ())")
27707
54bf1fea9252 SML_imp, OCaml_imp
haftmann
parents: 27695
diff changeset
   288
code_const return (SML "!(fn/ ()/ =>/ _)")
26182
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   289
code_const "Heap_Monad.Fail" (SML "Fail")
27707
54bf1fea9252 SML_imp, OCaml_imp
haftmann
parents: 27695
diff changeset
   290
code_const "Heap_Monad.raise_exc" (SML "!(fn/ ()/ =>/ raise/ _)")
26182
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   291
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   292
code_type Heap (OCaml "_")
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   293
code_const Heap (OCaml "failwith/ \"bare Heap\"")
27826
4e50590ea9bc changed code setup
haftmann
parents: 27707
diff changeset
   294
code_const "op \<guillemotright>=" (OCaml "!(fun/ f'_/ ()/ ->/ f'_/ (_/ ())/ ())")
27707
54bf1fea9252 SML_imp, OCaml_imp
haftmann
parents: 27695
diff changeset
   295
code_const return (OCaml "!(fun/ ()/ ->/ _)")
26182
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   296
code_const "Heap_Monad.Fail" (OCaml "Failure")
27707
54bf1fea9252 SML_imp, OCaml_imp
haftmann
parents: 27695
diff changeset
   297
code_const "Heap_Monad.raise_exc" (OCaml "!(fun/ ()/ ->/ raise/ _)")
54bf1fea9252 SML_imp, OCaml_imp
haftmann
parents: 27695
diff changeset
   298
54bf1fea9252 SML_imp, OCaml_imp
haftmann
parents: 27695
diff changeset
   299
ML {*
54bf1fea9252 SML_imp, OCaml_imp
haftmann
parents: 27695
diff changeset
   300
local
54bf1fea9252 SML_imp, OCaml_imp
haftmann
parents: 27695
diff changeset
   301
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents: 27826
diff changeset
   302
open Code_Thingol;
27707
54bf1fea9252 SML_imp, OCaml_imp
haftmann
parents: 27695
diff changeset
   303
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents: 27826
diff changeset
   304
val bind' = Code_Name.const @{theory} @{const_name bindM};
2b84d34c5d02 restructured and split code serializer module
haftmann
parents: 27826
diff changeset
   305
val return' = Code_Name.const @{theory} @{const_name return};
2b84d34c5d02 restructured and split code serializer module
haftmann
parents: 27826
diff changeset
   306
val unit' = Code_Name.const @{theory} @{const_name Unity};
27707
54bf1fea9252 SML_imp, OCaml_imp
haftmann
parents: 27695
diff changeset
   307
54bf1fea9252 SML_imp, OCaml_imp
haftmann
parents: 27695
diff changeset
   308
fun imp_monad_bind'' ts =
54bf1fea9252 SML_imp, OCaml_imp
haftmann
parents: 27695
diff changeset
   309
  let
54bf1fea9252 SML_imp, OCaml_imp
haftmann
parents: 27695
diff changeset
   310
    val dummy_name = "";
54bf1fea9252 SML_imp, OCaml_imp
haftmann
parents: 27695
diff changeset
   311
    val dummy_type = ITyVar dummy_name;
54bf1fea9252 SML_imp, OCaml_imp
haftmann
parents: 27695
diff changeset
   312
    val dummy_case_term = IVar dummy_name;
54bf1fea9252 SML_imp, OCaml_imp
haftmann
parents: 27695
diff changeset
   313
    (*assumption: dummy values are not relevant for serialization*)
54bf1fea9252 SML_imp, OCaml_imp
haftmann
parents: 27695
diff changeset
   314
    val unitt = IConst (unit', ([], []));
54bf1fea9252 SML_imp, OCaml_imp
haftmann
parents: 27695
diff changeset
   315
    fun dest_abs ((v, ty) `|-> t, _) = ((v, ty), t)
54bf1fea9252 SML_imp, OCaml_imp
haftmann
parents: 27695
diff changeset
   316
      | dest_abs (t, ty) =
54bf1fea9252 SML_imp, OCaml_imp
haftmann
parents: 27695
diff changeset
   317
          let
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents: 27826
diff changeset
   318
            val vs = Code_Thingol.fold_varnames cons t [];
27707
54bf1fea9252 SML_imp, OCaml_imp
haftmann
parents: 27695
diff changeset
   319
            val v = Name.variant vs "x";
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents: 27826
diff changeset
   320
            val ty' = (hd o fst o Code_Thingol.unfold_fun) ty;
27707
54bf1fea9252 SML_imp, OCaml_imp
haftmann
parents: 27695
diff changeset
   321
          in ((v, ty'), t `$ IVar v) end;
54bf1fea9252 SML_imp, OCaml_imp
haftmann
parents: 27695
diff changeset
   322
    fun force (t as IConst (c, _) `$ t') = if c = return'
54bf1fea9252 SML_imp, OCaml_imp
haftmann
parents: 27695
diff changeset
   323
          then t' else t `$ unitt
54bf1fea9252 SML_imp, OCaml_imp
haftmann
parents: 27695
diff changeset
   324
      | force t = t `$ unitt;
54bf1fea9252 SML_imp, OCaml_imp
haftmann
parents: 27695
diff changeset
   325
    fun tr_bind' [(t1, _), (t2, ty2)] =
54bf1fea9252 SML_imp, OCaml_imp
haftmann
parents: 27695
diff changeset
   326
      let
54bf1fea9252 SML_imp, OCaml_imp
haftmann
parents: 27695
diff changeset
   327
        val ((v, ty), t) = dest_abs (t2, ty2);
54bf1fea9252 SML_imp, OCaml_imp
haftmann
parents: 27695
diff changeset
   328
      in ICase (((force t1, ty), [(IVar v, tr_bind'' t)]), dummy_case_term) end
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents: 27826
diff changeset
   329
    and tr_bind'' t = case Code_Thingol.unfold_app t
27707
54bf1fea9252 SML_imp, OCaml_imp
haftmann
parents: 27695
diff changeset
   330
         of (IConst (c, (_, ty1 :: ty2 :: _)), [x1, x2]) => if c = bind'
54bf1fea9252 SML_imp, OCaml_imp
haftmann
parents: 27695
diff changeset
   331
              then tr_bind' [(x1, ty1), (x2, ty2)]
54bf1fea9252 SML_imp, OCaml_imp
haftmann
parents: 27695
diff changeset
   332
              else force t
54bf1fea9252 SML_imp, OCaml_imp
haftmann
parents: 27695
diff changeset
   333
          | _ => force t;
54bf1fea9252 SML_imp, OCaml_imp
haftmann
parents: 27695
diff changeset
   334
  in (dummy_name, dummy_type) `|-> ICase (((IVar dummy_name, dummy_type),
54bf1fea9252 SML_imp, OCaml_imp
haftmann
parents: 27695
diff changeset
   335
    [(unitt, tr_bind' ts)]), dummy_case_term) end
54bf1fea9252 SML_imp, OCaml_imp
haftmann
parents: 27695
diff changeset
   336
and imp_monad_bind' (const as (c, (_, tys))) ts = if c = bind' then case (ts, tys)
54bf1fea9252 SML_imp, OCaml_imp
haftmann
parents: 27695
diff changeset
   337
   of ([t1, t2], ty1 :: ty2 :: _) => imp_monad_bind'' [(t1, ty1), (t2, ty2)]
54bf1fea9252 SML_imp, OCaml_imp
haftmann
parents: 27695
diff changeset
   338
    | ([t1, t2, t3], ty1 :: ty2 :: _) => imp_monad_bind'' [(t1, ty1), (t2, ty2)] `$ t3
54bf1fea9252 SML_imp, OCaml_imp
haftmann
parents: 27695
diff changeset
   339
    | (ts, _) => imp_monad_bind (eta_expand 2 (const, ts))
54bf1fea9252 SML_imp, OCaml_imp
haftmann
parents: 27695
diff changeset
   340
  else IConst const `$$ map imp_monad_bind ts
54bf1fea9252 SML_imp, OCaml_imp
haftmann
parents: 27695
diff changeset
   341
and imp_monad_bind (IConst const) = imp_monad_bind' const []
54bf1fea9252 SML_imp, OCaml_imp
haftmann
parents: 27695
diff changeset
   342
  | imp_monad_bind (t as IVar _) = t
54bf1fea9252 SML_imp, OCaml_imp
haftmann
parents: 27695
diff changeset
   343
  | imp_monad_bind (t as _ `$ _) = (case unfold_app t
54bf1fea9252 SML_imp, OCaml_imp
haftmann
parents: 27695
diff changeset
   344
     of (IConst const, ts) => imp_monad_bind' const ts
54bf1fea9252 SML_imp, OCaml_imp
haftmann
parents: 27695
diff changeset
   345
      | (t, ts) => imp_monad_bind t `$$ map imp_monad_bind ts)
54bf1fea9252 SML_imp, OCaml_imp
haftmann
parents: 27695
diff changeset
   346
  | imp_monad_bind (v_ty `|-> t) = v_ty `|-> imp_monad_bind t
54bf1fea9252 SML_imp, OCaml_imp
haftmann
parents: 27695
diff changeset
   347
  | imp_monad_bind (ICase (((t, ty), pats), t0)) = ICase
54bf1fea9252 SML_imp, OCaml_imp
haftmann
parents: 27695
diff changeset
   348
      (((imp_monad_bind t, ty), (map o pairself) imp_monad_bind pats), imp_monad_bind t0);
54bf1fea9252 SML_imp, OCaml_imp
haftmann
parents: 27695
diff changeset
   349
54bf1fea9252 SML_imp, OCaml_imp
haftmann
parents: 27695
diff changeset
   350
in
54bf1fea9252 SML_imp, OCaml_imp
haftmann
parents: 27695
diff changeset
   351
54bf1fea9252 SML_imp, OCaml_imp
haftmann
parents: 27695
diff changeset
   352
val imp_program = (Graph.map_nodes o map_terms_stmt) imp_monad_bind;
54bf1fea9252 SML_imp, OCaml_imp
haftmann
parents: 27695
diff changeset
   353
54bf1fea9252 SML_imp, OCaml_imp
haftmann
parents: 27695
diff changeset
   354
end
54bf1fea9252 SML_imp, OCaml_imp
haftmann
parents: 27695
diff changeset
   355
*}
54bf1fea9252 SML_imp, OCaml_imp
haftmann
parents: 27695
diff changeset
   356
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents: 27826
diff changeset
   357
setup {* Code_Target.extend_target ("SML_imp", ("SML", imp_program)) *}
2b84d34c5d02 restructured and split code serializer module
haftmann
parents: 27826
diff changeset
   358
setup {* Code_Target.extend_target ("OCaml_imp", ("OCaml", imp_program)) *}
26182
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   359
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   360
code_reserved OCaml Failure raise
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   361
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   362
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   363
subsubsection {* Haskell *}
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   364
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   365
text {* Adaption layer *}
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   366
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   367
code_include Haskell "STMonad"
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   368
{*import qualified Control.Monad;
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   369
import qualified Control.Monad.ST;
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   370
import qualified Data.STRef;
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   371
import qualified Data.Array.ST;
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   372
27695
033732c90ebd Haskell now living in the RealWorld
haftmann
parents: 27673
diff changeset
   373
type RealWorld = Control.Monad.ST.RealWorld;
26182
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   374
type ST s a = Control.Monad.ST.ST s a;
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   375
type STRef s a = Data.STRef.STRef s a;
27673
52056ddac194 fixed code generator setup
haftmann
parents: 26753
diff changeset
   376
type STArray s a = Data.Array.ST.STArray s Int a;
26182
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   377
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   378
runST :: (forall s. ST s a) -> a;
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   379
runST s = Control.Monad.ST.runST s;
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   380
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   381
newSTRef = Data.STRef.newSTRef;
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   382
readSTRef = Data.STRef.readSTRef;
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   383
writeSTRef = Data.STRef.writeSTRef;
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   384
27673
52056ddac194 fixed code generator setup
haftmann
parents: 26753
diff changeset
   385
newArray :: (Int, Int) -> a -> ST s (STArray s a);
26182
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   386
newArray = Data.Array.ST.newArray;
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   387
27673
52056ddac194 fixed code generator setup
haftmann
parents: 26753
diff changeset
   388
newListArray :: (Int, Int) -> [a] -> ST s (STArray s a);
26182
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   389
newListArray = Data.Array.ST.newListArray;
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   390
27673
52056ddac194 fixed code generator setup
haftmann
parents: 26753
diff changeset
   391
lengthArray :: STArray s a -> ST s Int;
52056ddac194 fixed code generator setup
haftmann
parents: 26753
diff changeset
   392
lengthArray a = Control.Monad.liftM snd (Data.Array.ST.getBounds a);
26182
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   393
27673
52056ddac194 fixed code generator setup
haftmann
parents: 26753
diff changeset
   394
readArray :: STArray s a -> Int -> ST s a;
26182
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   395
readArray = Data.Array.ST.readArray;
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   396
27673
52056ddac194 fixed code generator setup
haftmann
parents: 26753
diff changeset
   397
writeArray :: STArray s a -> Int -> a -> ST s ();
26182
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   398
writeArray = Data.Array.ST.writeArray;*}
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   399
27695
033732c90ebd Haskell now living in the RealWorld
haftmann
parents: 27673
diff changeset
   400
code_reserved Haskell RealWorld ST STRef Array
26182
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   401
  runST
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   402
  newSTRef reasSTRef writeSTRef
27673
52056ddac194 fixed code generator setup
haftmann
parents: 26753
diff changeset
   403
  newArray newListArray lengthArray readArray writeArray
26182
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   404
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   405
text {* Monad *}
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   406
27695
033732c90ebd Haskell now living in the RealWorld
haftmann
parents: 27673
diff changeset
   407
code_type Heap (Haskell "ST/ RealWorld/ _")
033732c90ebd Haskell now living in the RealWorld
haftmann
parents: 27673
diff changeset
   408
code_const Heap (Haskell "error/ \"bare Heap\"")
28145
af3923ed4786 dropped "run" marker in monad syntax
haftmann
parents: 28054
diff changeset
   409
code_monad "op \<guillemotright>=" Haskell
26182
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   410
code_const return (Haskell "return")
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   411
code_const "Heap_Monad.Fail" (Haskell "_")
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   412
code_const "Heap_Monad.raise_exc" (Haskell "error")
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   413
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   414
end