thys/StateMonad.thy
author ibm-PC\ibm <xingyuanzhang@126.com>
Fri, 12 Sep 2014 00:41:17 +0800
changeset 23 452e8b557b63
parent 6 38cef5407d82
permissions -rw-r--r--
good
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     1
theory StateMonad
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     2
imports 
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     3
       "~~/src/HOL/Library/Monad_Syntax" 
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     4
begin
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     5
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     6
datatype ('result, 'state) SM = SM "'state => ('result \<times> 'state) option"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     7
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     8
fun execute :: "('result, 'state) SM \<Rightarrow> 'state \<Rightarrow> ('result \<times> 'state) option" where
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     9
  "execute (SM f) = f"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    10
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    11
lemma SM_cases [case_names succeed fail]:
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    12
  fixes f and s
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    13
  assumes succeed: "\<And>x s'. execute f h = Some (x, s') \<Longrightarrow> P"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    14
  assumes fail: "execute f h = None \<Longrightarrow> P"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    15
  shows P
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    16
  using assms by (cases "execute f h") auto
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    17
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    18
lemma SM_execute [simp]:
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    19
  "SM (execute f) = f" by (cases f) simp_all
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    20
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    21
lemma SM_eqI:
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    22
  "(\<And>h. execute f h = execute g h) \<Longrightarrow> f = g"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    23
    by (cases f, cases g) (auto simp: fun_eq_iff)
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    24
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    25
ML {* structure Execute_Simps = Named_Thms
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    26
(
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    27
  val name = @{binding execute_simps}
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    28
  val description = "simplification rules for execute"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    29
) *}
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    30
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    31
setup Execute_Simps.setup
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    32
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    33
lemma execute_Let [execute_simps]:
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    34
  "execute (let x = t in f x) = (let x = t in execute (f x))"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    35
  by (simp add: Let_def)
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    36
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    37
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    38
subsubsection {* Specialised lifters *}
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    39
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    40
definition sm :: "('state \<Rightarrow> 'a \<times> 'state) \<Rightarrow> ('a, 'state) SM" where
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    41
  "sm f = SM (Some \<circ> f)"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    42
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    43
definition tap :: "('state \<Rightarrow> 'a) \<Rightarrow> ('a, 'state) SM" where
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    44
  "tap f = SM (\<lambda>s. Some (f s, s))"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    45
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    46
definition "sm_get = tap id"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    47
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    48
definition "sm_map f = sm (\<lambda> s.((), f s))"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    49
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    50
definition "sm_set s' = sm_map (\<lambda> s. s)"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    51
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    52
lemma execute_tap [execute_simps]:
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    53
  "execute (tap f) h = Some (f h, h)"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    54
  by (simp add: tap_def)
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    55
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    56
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    57
lemma execute_heap [execute_simps]:
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    58
  "execute (sm f) = Some \<circ> f"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    59
  by (simp add: sm_def)
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    60
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    61
definition guard :: "('state \<Rightarrow> bool) \<Rightarrow> ('state \<Rightarrow> 'a \<times> 'state) 
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    62
                        \<Rightarrow> ('a, 'state) SM" where
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    63
 "guard P f = SM (\<lambda>h. if P h then Some (f h) else None)"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    64
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    65
lemma execute_guard [execute_simps]:
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    66
  "\<not> P h \<Longrightarrow> execute (guard P f) h = None"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    67
  "P h \<Longrightarrow> execute (guard P f) h = Some (f h)"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    68
  by (simp_all add: guard_def)
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    69
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    70
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    71
subsubsection {* Predicate classifying successful computations *}
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    72
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    73
definition success :: "('a, 'state) SM \<Rightarrow> 'state \<Rightarrow> bool" where
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    74
  "success f h = (execute f h \<noteq> None)"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    75
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    76
lemma successI:
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    77
  "execute f h \<noteq> None \<Longrightarrow> success f h"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    78
  by (simp add: success_def)
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    79
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    80
lemma successE:
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    81
  assumes "success f h"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    82
  obtains r h' where "r = fst (the (execute c h))"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    83
    and "h' = snd (the (execute c h))"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    84
    and "execute f h \<noteq> None"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    85
  using assms by (simp add: success_def)
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    86
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    87
ML {* structure Success_Intros = Named_Thms
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    88
(
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    89
  val name = @{binding success_intros}
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    90
  val description = "introduction rules for success"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    91
) *}
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    92
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    93
setup Success_Intros.setup
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    94
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    95
lemma success_tapI [success_intros]:
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    96
  "success (tap f) h"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    97
  by (rule successI) (simp add: execute_simps)
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    98
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    99
lemma success_heapI [success_intros]:
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   100
  "success (sm f) h"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   101
  by (rule successI) (simp add: execute_simps)
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   102
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   103
lemma success_guardI [success_intros]:
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   104
  "P h \<Longrightarrow> success (guard P f) h"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   105
  by (rule successI) (simp add: execute_guard)
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   106
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   107
lemma success_LetI [success_intros]:
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   108
  "x = t \<Longrightarrow> success (f x) h \<Longrightarrow> success (let x = t in f x) h"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   109
  by (simp add: Let_def)
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   110
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   111
lemma success_ifI:
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   112
  "(c \<Longrightarrow> success t h) \<Longrightarrow> (\<not> c \<Longrightarrow> success e h) \<Longrightarrow>
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   113
    success (if c then t else e) h"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   114
  by (simp add: success_def)
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   115
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   116
subsubsection {* Predicate for a simple relational calculus *}
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   117
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   118
text {*
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   119
  The @{text effect} predicate states that when a computation @{text c}
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   120
  runs with the state @{text h} will result in return value @{text r}
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   121
  and a state @{text "h'"}, i.e.~no exception occurs.
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   122
*}  
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   123
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   124
definition effect :: "('a, 'state) SM \<Rightarrow> 'state \<Rightarrow> 'state \<Rightarrow> 'a \<Rightarrow> bool" where
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   125
  effect_def: "effect c h h' r = (execute c h = Some (r, h'))"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   126
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   127
lemma effectI:
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   128
  "execute c h = Some (r, h') \<Longrightarrow> effect c h h' r"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   129
  by (simp add: effect_def)
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   130
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   131
lemma effectE:
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   132
  assumes "effect c h h' r"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   133
  obtains "r = fst (the (execute c h))"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   134
    and "h' = snd (the (execute c h))"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   135
    and "success c h"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   136
proof (rule that)
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   137
  from assms have *: "execute c h = Some (r, h')" by (simp add: effect_def)
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   138
  then show "success c h" by (simp add: success_def)
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   139
  from * have "fst (the (execute c h)) = r" and "snd (the (execute c h)) = h'"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   140
    by simp_all
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   141
  then show "r = fst (the (execute c h))"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   142
    and "h' = snd (the (execute c h))" by simp_all
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   143
qed
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   144
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   145
lemma effect_success:
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   146
  "effect c h h' r \<Longrightarrow> success c h"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   147
  by (simp add: effect_def success_def)
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   148
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   149
lemma success_effectE:
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   150
  assumes "success c h"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   151
  obtains r h' where "effect c h h' r"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   152
  using assms by (auto simp add: effect_def success_def)
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   153
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   154
lemma effect_deterministic:
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   155
  assumes "effect f h h' a"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   156
    and "effect f h h'' b"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   157
  shows "a = b" and "h' = h''"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   158
  using assms unfolding effect_def by auto
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   159
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   160
ML {* structure Effect_Intros = Named_Thms
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   161
(
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   162
  val name = @{binding effect_intros}
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   163
  val description = "introduction rules for effect"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   164
) *}
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   165
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   166
ML {* structure Effect_Elims = Named_Thms
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   167
(
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   168
  val name = @{binding effect_elims}
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   169
  val description = "elimination rules for effect"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   170
) *}
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   171
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   172
setup "Effect_Intros.setup #> Effect_Elims.setup"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   173
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   174
lemma effect_LetI [effect_intros]:
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   175
  assumes "x = t" "effect (f x) h h' r"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   176
  shows "effect (let x = t in f x) h h' r"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   177
  using assms by simp
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   178
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   179
lemma effect_LetE [effect_elims]:
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   180
  assumes "effect (let x = t in f x) h h' r"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   181
  obtains "effect (f t) h h' r"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   182
  using assms by simp
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   183
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   184
lemma effect_ifI:
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   185
  assumes "c \<Longrightarrow> effect t h h' r"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   186
    and "\<not> c \<Longrightarrow> effect e h h' r"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   187
  shows "effect (if c then t else e) h h' r"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   188
  by (cases c) (simp_all add: assms)
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   189
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   190
lemma effect_ifE:
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   191
  assumes "effect (if c then t else e) h h' r"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   192
  obtains "c" "effect t h h' r"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   193
    | "\<not> c" "effect e h h' r"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   194
  using assms by (cases c) simp_all
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   195
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   196
lemma effect_tapI [effect_intros]:
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   197
  assumes "h' = h" "r = f h"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   198
  shows "effect (tap f) h h' r"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   199
  by (rule effectI) (simp add: assms execute_simps)
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   200
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   201
lemma effect_tapE [effect_elims]:
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   202
  assumes "effect (tap f) h h' r"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   203
  obtains "h' = h" and "r = f h"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   204
  using assms by (rule effectE) (auto simp add: execute_simps)
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   205
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   206
lemma effect_heapI [effect_intros]:
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   207
  assumes "h' = snd (f h)" "r = fst (f h)"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   208
  shows "effect (sm f) h h' r"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   209
  by (rule effectI) (simp add: assms execute_simps)
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   210
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   211
lemma effect_heapE [effect_elims]:
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   212
  assumes "effect (sm f) h h' r"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   213
  obtains "h' = snd (f h)" and "r = fst (f h)"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   214
  using assms by (rule effectE) (simp add: execute_simps)
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   215
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   216
lemma effect_guardI [effect_intros]:
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   217
  assumes "P h" "h' = snd (f h)" "r = fst (f h)"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   218
  shows "effect (guard P f) h h' r"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   219
  by (rule effectI) (simp add: assms execute_simps)
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   220
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   221
lemma effect_guardE [effect_elims]:
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   222
  assumes "effect (guard P f) h h' r"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   223
  obtains "h' = snd (f h)" "r = fst (f h)" "P h"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   224
  using assms by (rule effectE)
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   225
    (auto simp add: execute_simps elim!: successE, 
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   226
        cases "P h", auto simp add: execute_simps)
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   227
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   228
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   229
subsubsection {* Monad combinators *}
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   230
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   231
definition return :: "'a \<Rightarrow> ('a, 'state) SM" where
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   232
       "return x = sm (Pair x)"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   233
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   234
lemma execute_return [execute_simps]:
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   235
  "execute (return x) = Some \<circ> Pair x"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   236
  by (simp add: return_def execute_simps)
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   237
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   238
lemma success_returnI [success_intros]:
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   239
  "success (return x) h"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   240
  by (rule successI) (simp add: execute_simps)
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   241
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   242
lemma effect_returnI [effect_intros]:
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   243
  "h = h' \<Longrightarrow> effect (return x) h h' x"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   244
  by (rule effectI) (simp add: execute_simps)
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   245
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   246
lemma effect_returnE [effect_elims]:
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   247
  assumes "effect (return x) h h' r"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   248
  obtains "r = x" "h' = h"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   249
  using assms by (rule effectE) (simp add: execute_simps)
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   250
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   251
definition raise :: "string \<Rightarrow> ('a, 'state) SM" where 
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   252
      -- {* the string is just decoration *}
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   253
               "raise s = SM (\<lambda>_. None)"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   254
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   255
lemma execute_raise [execute_simps]:
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   256
  "execute (raise s) = (\<lambda>_. None)"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   257
  by (simp add: raise_def)
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   258
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   259
lemma effect_raiseE [effect_elims]:
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   260
  assumes "effect (raise x) h h' r"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   261
  obtains "False"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   262
  using assms by (rule effectE) (simp add: success_def execute_simps)
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   263
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   264
definition bind :: "('a, 'state) SM \<Rightarrow> ('a \<Rightarrow> ('b, 'state) SM) \<Rightarrow> ('b, 'state) SM" where
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   265
  "bind f g = SM (\<lambda>h. case execute f h of
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   266
                  Some (x, h') \<Rightarrow> execute (g x) h'
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   267
                | None \<Rightarrow> None)"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   268
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   269
adhoc_overloading
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   270
  Monad_Syntax.bind StateMonad.bind
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   271
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   272
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   273
lemma execute_bind [execute_simps]:
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   274
  "execute f h = Some (x, h') \<Longrightarrow> execute (f \<guillemotright>= g) h = execute (g x) h'"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   275
  "execute f h = None \<Longrightarrow> execute (f \<guillemotright>= g) h = None"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   276
  by (simp_all add: bind_def)
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   277
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   278
lemma execute_bind_case:
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   279
  "execute (f \<guillemotright>= g) h = (case (execute f h) of
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   280
    Some (x, h') \<Rightarrow> execute (g x) h' | None \<Rightarrow> None)"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   281
  by (simp add: bind_def)
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   282
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   283
lemma execute_bind_success:
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   284
  "success f h \<Longrightarrow> execute (f \<guillemotright>= g) h = execute (g (fst (the (execute f h)))) (snd (the (execute f h)))"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   285
  by (cases f h rule: SM_cases) (auto elim!: successE simp add: bind_def)
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   286
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   287
lemma success_bind_executeI:
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   288
  "execute f h = Some (x, h') \<Longrightarrow> success (g x) h' \<Longrightarrow> success (f \<guillemotright>= g) h"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   289
  by (auto intro!: successI elim!: successE simp add: bind_def)
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   290
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   291
lemma success_bind_effectI [success_intros]:
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   292
  "effect f h h' x \<Longrightarrow> success (g x) h' \<Longrightarrow> success (f \<guillemotright>= g) h"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   293
  by (auto simp add: effect_def success_def bind_def)
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   294
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   295
lemma effect_bindI [effect_intros]:
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   296
  assumes "effect f h h' r" "effect (g r) h' h'' r'"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   297
  shows "effect (f \<guillemotright>= g) h h'' r'"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   298
  using assms
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   299
  apply (auto intro!: effectI elim!: effectE successE)
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   300
  apply (subst execute_bind, simp_all)
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   301
  done
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   302
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   303
lemma effect_bindE [effect_elims]:
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   304
  assumes "effect (f \<guillemotright>= g) h h'' r'"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   305
  obtains h' r where "effect f h h' r" "effect (g r) h' h'' r'"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   306
  using assms by (auto simp add: effect_def bind_def split: option.split_asm)
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   307
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   308
lemma execute_bind_eq_SomeI:
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   309
  assumes "execute f h = Some (x, h')"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   310
    and "execute (g x) h' = Some (y, h'')"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   311
  shows "execute (f \<guillemotright>= g) h = Some (y, h'')"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   312
  using assms by (simp add: bind_def)
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   313
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   314
lemma return_bind [simp]: "return x \<guillemotright>= f = f x"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   315
  by (rule SM_eqI) (simp add: execute_bind execute_simps)
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   316
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   317
lemma bind_return [simp]: "f \<guillemotright>= return = f"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   318
  by (rule SM_eqI) (simp add: bind_def execute_simps split: option.splits)
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   319
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   320
lemma bind_bind [simp]: "(f \<guillemotright>= g) \<guillemotright>= k = (f :: ('a, 'state) SM) \<guillemotright>= (\<lambda>x. g x \<guillemotright>= k)"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   321
  by (rule SM_eqI) (simp add: bind_def execute_simps split: option.splits)
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   322
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   323
lemma raise_bind [simp]: "raise e \<guillemotright>= f = raise e"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   324
  by (rule SM_eqI) (simp add: execute_simps)
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   325
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   326
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   327
subsection {* Generic combinators *}
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   328
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   329
subsubsection {* Assertions *}
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   330
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   331
definition assert :: "('a \<Rightarrow> bool) \<Rightarrow> 'a \<Rightarrow> ('a, 'state) SM" where
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   332
  "assert P x = (if P x then return x else raise ''assert'')"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   333
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   334
lemma execute_assert [execute_simps]:
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   335
  "P x \<Longrightarrow> execute (assert P x) h = Some (x, h)"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   336
  "\<not> P x \<Longrightarrow> execute (assert P x) h = None"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   337
  by (simp_all add: assert_def execute_simps)
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   338
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   339
lemma success_assertI [success_intros]:
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   340
  "P x \<Longrightarrow> success (assert P x) h"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   341
  by (rule successI) (simp add: execute_assert)
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   342
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   343
lemma effect_assertI [effect_intros]:
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   344
  "P x \<Longrightarrow> h' = h \<Longrightarrow> r = x \<Longrightarrow> effect (assert P x) h h' r"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   345
  by (rule effectI) (simp add: execute_assert)
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   346
 
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   347
lemma effect_assertE [effect_elims]:
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   348
  assumes "effect (assert P x) h h' r"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   349
  obtains "P x" "r = x" "h' = h"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   350
  using assms by (rule effectE) (cases "P x", simp_all add: execute_assert success_def)
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   351
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   352
lemma assert_cong [fundef_cong]:
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   353
  assumes "P = P'"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   354
  assumes "\<And>x. P' x \<Longrightarrow> f x = f' x"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   355
  shows "(assert P x >>= f) = (assert P' x >>= f')"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   356
  by (rule SM_eqI) (insert assms, simp add: assert_def)
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   357
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   358
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   359
subsubsection {* Plain lifting *}
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   360
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   361
definition lift :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a \<Rightarrow> ('b, 'state) SM" where
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   362
  "lift f = return o f"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   363
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   364
lemma lift_collapse [simp]:
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   365
  "lift f x = return (f x)"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   366
  by (simp add: lift_def)
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   367
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   368
lemma bind_lift:
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   369
  "(f \<guillemotright>= lift g) = (f \<guillemotright>= (\<lambda>x. return (g x)))"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   370
  by (simp add: lift_def comp_def)
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   371
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   372
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   373
subsubsection {* Iteration -- warning: this is rarely useful! *}
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   374
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   375
primrec fold_map :: "('a \<Rightarrow> ('b, 'state) SM) \<Rightarrow> 'a list \<Rightarrow> ('b list, 'state) SM" where
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   376
  "fold_map f [] = return []"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   377
| "fold_map f (x # xs) = do {
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   378
     y \<leftarrow> f x;
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   379
     ys \<leftarrow> fold_map f xs;
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   380
     return (y # ys)
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   381
   }"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   382
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   383
lemma fold_map_append:
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   384
  "fold_map f (xs @ ys) = fold_map f xs \<guillemotright>= (\<lambda>xs. fold_map f ys \<guillemotright>= (\<lambda>ys. return (xs @ ys)))"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   385
  by (induct xs) simp_all
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   386
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   387
lemma execute_fold_map_unchanged_heap [execute_simps]:
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   388
  assumes "\<And>x. x \<in> set xs \<Longrightarrow> \<exists>y. execute (f x) h = Some (y, h)"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   389
  shows "execute (fold_map f xs) h =
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   390
    Some (List.map (\<lambda>x. fst (the (execute (f x) h))) xs, h)"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   391
using assms proof (induct xs)
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   392
  case Nil show ?case by (simp add: execute_simps)
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   393
next
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   394
  case (Cons x xs)
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   395
  from Cons.prems obtain y
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   396
    where y: "execute (f x) h = Some (y, h)" by auto
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   397
  moreover from Cons.prems Cons.hyps have "execute (fold_map f xs) h =
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   398
    Some (map (\<lambda>x. fst (the (execute (f x) h))) xs, h)" by auto
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   399
  ultimately show ?case by (simp, simp only: execute_bind(1), simp add: execute_simps)
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   400
qed
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   401
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   402
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   403
subsection {* Partial function definition setup *}
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   404
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   405
definition SM_ord :: "('a, 'state) SM \<Rightarrow> ('a, 'state) SM \<Rightarrow> bool" where
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   406
  "SM_ord = img_ord execute (fun_ord option_ord)"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   407
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   408
definition SM_lub :: "('a , 'state) SM set \<Rightarrow> ('a, 'state) SM" where
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   409
  "SM_lub = img_lub execute SM (fun_lub (flat_lub None))"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   410
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   411
interpretation sm!: partial_function_definitions SM_ord SM_lub
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   412
proof -
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   413
  have "partial_function_definitions (fun_ord option_ord) (fun_lub (flat_lub None))"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   414
    by (rule partial_function_lift) (rule flat_interpretation)
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   415
  then have "partial_function_definitions (img_ord execute (fun_ord option_ord))
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   416
      (img_lub execute SM (fun_lub (flat_lub None)))"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   417
    by (rule partial_function_image) (auto intro: SM_eqI)
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   418
  then show "partial_function_definitions SM_ord SM_lub"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   419
    by (simp only: SM_ord_def SM_lub_def)
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   420
qed
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   421
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   422
declaration {* Partial_Function.init "sm" @{term sm.fixp_fun}
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   423
  @{term sm.mono_body} @{thm sm.fixp_rule_uc} @{thm sm.fixp_induct_uc}  NONE *}
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   424
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   425
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   426
abbreviation "mono_SM \<equiv> monotone (fun_ord SM_ord) SM_ord"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   427
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   428
lemma SM_ordI:
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   429
  assumes "\<And>h. execute x h = None \<or> execute x h = execute y h"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   430
  shows  "SM_ord x y"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   431
  using assms unfolding SM_ord_def img_ord_def fun_ord_def flat_ord_def
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   432
  by blast
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   433
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   434
lemma SM_ordE:
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   435
  assumes "SM_ord x y"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   436
  obtains "execute x h = None" | "execute x h = execute y h"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   437
  using assms unfolding SM_ord_def img_ord_def fun_ord_def flat_ord_def
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   438
  by atomize_elim blast
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   439
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   440
lemma bind_mono [partial_function_mono]:
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   441
  assumes mf: "mono_SM B" and mg: "\<And>y. mono_SM (\<lambda>f. C y f)"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   442
  shows "mono_SM (\<lambda>f. B f \<guillemotright>= (\<lambda>y. C y f))"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   443
proof (rule monotoneI)
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   444
  fix f g :: "'a \<Rightarrow> ('b, 'c) SM" assume fg: "fun_ord SM_ord f g"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   445
  from mf
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   446
  have 1: "SM_ord (B f) (B g)" by (rule monotoneD) (rule fg)
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   447
  from mg
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   448
  have 2: "\<And>y'. SM_ord (C y' f) (C y' g)" by (rule monotoneD) (rule fg)
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   449
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   450
  have "SM_ord (B f \<guillemotright>= (\<lambda>y. C y f)) (B g \<guillemotright>= (\<lambda>y. C y f))"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   451
    (is "SM_ord ?L ?R")
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   452
  proof (rule SM_ordI)
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   453
    fix h
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   454
    from 1 show "execute ?L h = None \<or> execute ?L h = execute ?R h"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   455
      by (rule SM_ordE[where h = h]) (auto simp: execute_bind_case)
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   456
  qed
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   457
  also
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   458
  have "SM_ord (B g \<guillemotright>= (\<lambda>y'. C y' f)) (B g \<guillemotright>= (\<lambda>y'. C y' g))"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   459
    (is "SM_ord ?L ?R")
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   460
  proof (rule SM_ordI)
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   461
    fix h
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   462
    show "execute ?L h = None \<or> execute ?L h = execute ?R h"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   463
    proof (cases "execute (B g) h")
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   464
      case None
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   465
      then have "execute ?L h = None" by (auto simp: execute_bind_case)
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   466
      thus ?thesis ..
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   467
    next
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   468
      case Some
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   469
      then obtain r h' where "execute (B g) h = Some (r, h')"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   470
        by (metis surjective_pairing)
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   471
      then have "execute ?L h = execute (C r f) h'"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   472
        "execute ?R h = execute (C r g) h'"
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   473
        by (auto simp: execute_bind_case)
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   474
      with 2[of r] show ?thesis by (auto elim: SM_ordE)
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   475
    qed
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   476
  qed
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   477
  finally (sm.leq_trans)
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   478
  show "SM_ord (B f \<guillemotright>= (\<lambda>y. C y f)) (B g \<guillemotright>= (\<lambda>y'. C y' g))" .
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   479
qed
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   480
38cef5407d82 updated various files to Isabelle-2013-2
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   481
end