src/HOL/IMPP/Natural.thy
author bulwahn
Fri Oct 21 11:17:14 2011 +0200 (2011-10-21)
changeset 45231 d85a2fdc586c
parent 41589 bbd861837ebc
child 46459 73823dbbecc4
permissions -rw-r--r--
replacing code_inline by code_unfold, removing obsolete code_unfold, code_inline del now that the ancient code generator is removed
oheimb@8177
     1
(*  Title:      HOL/IMPP/Natural.thy
oheimb@8177
     2
    Author:     David von Oheimb (based on a theory by Tobias Nipkow et al), TUM
oheimb@8177
     3
*)
oheimb@8177
     4
wenzelm@17477
     5
header {* Natural semantics of commands *}
wenzelm@17477
     6
wenzelm@17477
     7
theory Natural
wenzelm@17477
     8
imports Com
wenzelm@17477
     9
begin
oheimb@8177
    10
oheimb@8177
    11
(** Execution of commands **)
oheimb@8177
    12
oheimb@8177
    13
consts
oheimb@8177
    14
  newlocs :: locals
wenzelm@17477
    15
  setlocs :: "state => locals => state"
wenzelm@17477
    16
  getlocs :: "state => locals"
wenzelm@17477
    17
  update  :: "state => vname => val => state"     ("_/[_/::=/_]" [900,0,0] 900)
wenzelm@27362
    18
wenzelm@27362
    19
abbreviation
wenzelm@27362
    20
  loc :: "state => locals"  ("_<_>" [75,0] 75) where
wenzelm@27362
    21
  "s<X> == getlocs s X"
oheimb@8177
    22
berghofe@23746
    23
inductive
berghofe@23746
    24
  evalc :: "[com,state,    state] => bool"  ("<_,_>/ -c-> _" [0,0,  51] 51)
berghofe@23746
    25
  where
wenzelm@17477
    26
    Skip:    "<SKIP,s> -c-> s"
oheimb@8177
    27
berghofe@23746
    28
  | Assign:  "<X :== a,s> -c-> s[X::=a s]"
oheimb@8177
    29
berghofe@23746
    30
  | Local:   "<c, s0[Loc Y::= a s0]> -c-> s1 ==>
wenzelm@17477
    31
              <LOCAL Y := a IN c, s0> -c-> s1[Loc Y::=s0<Y>]"
oheimb@8177
    32
berghofe@23746
    33
  | Semi:    "[| <c0,s0> -c-> s1; <c1,s1> -c-> s2 |] ==>
wenzelm@17477
    34
              <c0;; c1, s0> -c-> s2"
oheimb@8177
    35
berghofe@23746
    36
  | IfTrue:  "[| b s; <c0,s> -c-> s1 |] ==>
wenzelm@17477
    37
              <IF b THEN c0 ELSE c1, s> -c-> s1"
oheimb@8177
    38
berghofe@23746
    39
  | IfFalse: "[| ~b s; <c1,s> -c-> s1 |] ==>
wenzelm@17477
    40
              <IF b THEN c0 ELSE c1, s> -c-> s1"
oheimb@8177
    41
berghofe@23746
    42
  | WhileFalse: "~b s ==> <WHILE b DO c,s> -c-> s"
oheimb@8177
    43
berghofe@23746
    44
  | WhileTrue:  "[| b s0;  <c,s0> -c-> s1;  <WHILE b DO c, s1> -c-> s2 |] ==>
wenzelm@17477
    45
                 <WHILE b DO c, s0> -c-> s2"
oheimb@8177
    46
berghofe@23746
    47
  | Body:       "<the (body pn), s0> -c-> s1 ==>
wenzelm@17477
    48
                 <BODY pn, s0> -c-> s1"
oheimb@8177
    49
berghofe@23746
    50
  | Call:       "<BODY pn, (setlocs s0 newlocs)[Loc Arg::=a s0]> -c-> s1 ==>
wenzelm@17477
    51
                 <X:=CALL pn(a), s0> -c-> (setlocs s1 (getlocs s0))
wenzelm@17477
    52
                                          [X::=s1<Res>]"
oheimb@8177
    53
berghofe@23746
    54
inductive
berghofe@23746
    55
  evaln :: "[com,state,nat,state] => bool"  ("<_,_>/ -_-> _" [0,0,0,51] 51)
berghofe@23746
    56
  where
wenzelm@17477
    57
    Skip:    "<SKIP,s> -n-> s"
wenzelm@17477
    58
berghofe@23746
    59
  | Assign:  "<X :== a,s> -n-> s[X::=a s]"
oheimb@8177
    60
berghofe@23746
    61
  | Local:   "<c, s0[Loc Y::= a s0]> -n-> s1 ==>
wenzelm@17477
    62
              <LOCAL Y := a IN c, s0> -n-> s1[Loc Y::=s0<Y>]"
wenzelm@17477
    63
berghofe@23746
    64
  | Semi:    "[| <c0,s0> -n-> s1; <c1,s1> -n-> s2 |] ==>
wenzelm@17477
    65
              <c0;; c1, s0> -n-> s2"
oheimb@8177
    66
berghofe@23746
    67
  | IfTrue:  "[| b s; <c0,s> -n-> s1 |] ==>
wenzelm@17477
    68
              <IF b THEN c0 ELSE c1, s> -n-> s1"
oheimb@8177
    69
berghofe@23746
    70
  | IfFalse: "[| ~b s; <c1,s> -n-> s1 |] ==>
wenzelm@17477
    71
              <IF b THEN c0 ELSE c1, s> -n-> s1"
oheimb@8177
    72
berghofe@23746
    73
  | WhileFalse: "~b s ==> <WHILE b DO c,s> -n-> s"
wenzelm@17477
    74
berghofe@23746
    75
  | WhileTrue:  "[| b s0;  <c,s0> -n-> s1;  <WHILE b DO c, s1> -n-> s2 |] ==>
wenzelm@17477
    76
                 <WHILE b DO c, s0> -n-> s2"
oheimb@8177
    77
berghofe@23746
    78
  | Body:       "<the (body pn), s0> -    n-> s1 ==>
wenzelm@17477
    79
                 <BODY pn, s0> -Suc n-> s1"
oheimb@8177
    80
berghofe@23746
    81
  | Call:       "<BODY pn, (setlocs s0 newlocs)[Loc Arg::=a s0]> -n-> s1 ==>
wenzelm@17477
    82
                 <X:=CALL pn(a), s0> -n-> (setlocs s1 (getlocs s0))
wenzelm@17477
    83
                                          [X::=s1<Res>]"
oheimb@8177
    84
oheimb@8177
    85
wenzelm@17477
    86
inductive_cases evalc_elim_cases:
wenzelm@17477
    87
  "<SKIP,s> -c-> t"  "<X:==a,s> -c-> t"  "<LOCAL Y:=a IN c,s> -c-> t"
wenzelm@17477
    88
  "<c1;;c2,s> -c-> t"  "<IF b THEN c1 ELSE c2,s> -c-> t"
wenzelm@17477
    89
  "<BODY P,s> -c-> s1"  "<X:=CALL P(a),s> -c-> s1"
oheimb@8177
    90
wenzelm@17477
    91
inductive_cases evaln_elim_cases:
wenzelm@17477
    92
  "<SKIP,s> -n-> t"  "<X:==a,s> -n-> t"  "<LOCAL Y:=a IN c,s> -n-> t"
wenzelm@17477
    93
  "<c1;;c2,s> -n-> t"  "<IF b THEN c1 ELSE c2,s> -n-> t"
wenzelm@17477
    94
  "<BODY P,s> -n-> s1"  "<X:=CALL P(a),s> -n-> s1"
wenzelm@17477
    95
wenzelm@17477
    96
inductive_cases evalc_WHILE_case: "<WHILE b DO c,s> -c-> t"
wenzelm@17477
    97
inductive_cases evaln_WHILE_case: "<WHILE b DO c,s> -n-> t"
wenzelm@17477
    98
wenzelm@19803
    99
declare evalc.intros [intro]
wenzelm@19803
   100
declare evaln.intros [intro]
wenzelm@19803
   101
wenzelm@19803
   102
declare evalc_elim_cases [elim!]
wenzelm@19803
   103
declare evaln_elim_cases [elim!]
wenzelm@19803
   104
wenzelm@19803
   105
(* evaluation of com is deterministic *)
wenzelm@19803
   106
lemma com_det [rule_format (no_asm)]: "<c,s> -c-> t ==> (!u. <c,s> -c-> u --> u=t)"
wenzelm@19803
   107
apply (erule evalc.induct)
wenzelm@19803
   108
apply (erule_tac [8] V = "<?c,s1> -c-> s2" in thin_rl)
wenzelm@24178
   109
(*blast needs unify_search_bound = 40*)
wenzelm@19803
   110
apply (best elim: evalc_WHILE_case)+
wenzelm@19803
   111
done
wenzelm@19803
   112
wenzelm@19803
   113
lemma evaln_evalc: "<c,s> -n-> t ==> <c,s> -c-> t"
wenzelm@19803
   114
apply (erule evaln.induct)
wenzelm@39159
   115
apply (tactic {* ALLGOALS (resolve_tac @{thms evalc.intros} THEN_ALL_NEW atac) *})
wenzelm@19803
   116
done
wenzelm@19803
   117
wenzelm@19803
   118
lemma Suc_le_D_lemma: "[| Suc n <= m'; (!!m. n <= m ==> P (Suc m)) |] ==> P m'"
wenzelm@19803
   119
apply (frule Suc_le_D)
wenzelm@19803
   120
apply blast
wenzelm@19803
   121
done
wenzelm@19803
   122
wenzelm@19803
   123
lemma evaln_nonstrict [rule_format]: "<c,s> -n-> t ==> !m. n<=m --> <c,s> -m-> t"
wenzelm@19803
   124
apply (erule evaln.induct)
wenzelm@39159
   125
apply (tactic {* ALLGOALS (EVERY'[strip_tac,TRY o etac @{thm Suc_le_D_lemma}, REPEAT o smp_tac 1]) *})
wenzelm@39159
   126
apply (tactic {* ALLGOALS (resolve_tac @{thms evaln.intros} THEN_ALL_NEW atac) *})
wenzelm@19803
   127
done
wenzelm@19803
   128
wenzelm@19803
   129
lemma evaln_Suc: "<c,s> -n-> s' ==> <c,s> -Suc n-> s'"
wenzelm@19803
   130
apply (erule evaln_nonstrict)
wenzelm@19803
   131
apply auto
wenzelm@19803
   132
done
wenzelm@19803
   133
wenzelm@19803
   134
lemma evaln_max2: "[| <c1,s1> -n1-> t1;  <c2,s2> -n2-> t2 |] ==>  
wenzelm@19803
   135
    ? n. <c1,s1> -n -> t1 & <c2,s2> -n -> t2"
wenzelm@19803
   136
apply (cut_tac m = "n1" and n = "n2" in nat_le_linear)
wenzelm@19803
   137
apply (blast dest: evaln_nonstrict)
wenzelm@19803
   138
done
wenzelm@19803
   139
wenzelm@19803
   140
lemma evalc_evaln: "<c,s> -c-> t ==> ? n. <c,s> -n-> t"
wenzelm@19803
   141
apply (erule evalc.induct)
wenzelm@19803
   142
apply (tactic {* ALLGOALS (REPEAT o etac exE) *})
wenzelm@39159
   143
apply (tactic {* TRYALL (EVERY' [datac @{thm evaln_max2} 1, REPEAT o eresolve_tac [exE, conjE]]) *})
wenzelm@39159
   144
apply (tactic {* ALLGOALS (rtac exI THEN' resolve_tac @{thms evaln.intros} THEN_ALL_NEW atac) *})
wenzelm@19803
   145
done
wenzelm@19803
   146
wenzelm@19803
   147
lemma eval_eq: "<c,s> -c-> t = (? n. <c,s> -n-> t)"
wenzelm@19803
   148
apply (fast elim: evalc_evaln evaln_evalc)
wenzelm@19803
   149
done
wenzelm@17477
   150
oheimb@8177
   151
end