src/HOL/Induct/Com.thy
author wenzelm
Wed May 12 14:17:26 2010 +0200 (2010-05-12)
changeset 36862 952b2b102a0a
parent 32367 a508148f7c25
child 41818 6d4c3ee8219d
permissions -rw-r--r--
removed obsolete CVS Ids;
     1 (*  Title:      HOL/Induct/Com.thy
     2     Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
     3     Copyright   1997  University of Cambridge
     4 
     5 Example of Mutual Induction via Iteratived Inductive Definitions: Commands
     6 *)
     7 
     8 header{*Mutual Induction via Iteratived Inductive Definitions*}
     9 
    10 theory Com imports Main begin
    11 
    12 typedecl loc
    13 types  state = "loc => nat"
    14 
    15 datatype
    16   exp = N nat
    17       | X loc
    18       | Op "nat => nat => nat" exp exp
    19       | valOf com exp          ("VALOF _ RESULTIS _"  60)
    20 and
    21   com = SKIP
    22       | Assign loc exp         (infixl ":=" 60)
    23       | Semi com com           ("_;;_"  [60, 60] 60)
    24       | Cond exp com com       ("IF _ THEN _ ELSE _"  60)
    25       | While exp com          ("WHILE _ DO _"  60)
    26 
    27 
    28 subsection {* Commands *}
    29 
    30 text{* Execution of commands *}
    31 
    32 abbreviation (input)
    33   generic_rel  ("_/ -|[_]-> _" [50,0,50] 50)  where
    34   "esig -|[eval]-> ns == (esig,ns) \<in> eval"
    35 
    36 text{*Command execution.  Natural numbers represent Booleans: 0=True, 1=False*}
    37 
    38 inductive_set
    39   exec :: "((exp*state) * (nat*state)) set => ((com*state)*state)set"
    40   and exec_rel :: "com * state => ((exp*state) * (nat*state)) set => state => bool"
    41     ("_/ -[_]-> _" [50,0,50] 50)
    42   for eval :: "((exp*state) * (nat*state)) set"
    43   where
    44     "csig -[eval]-> s == (csig,s) \<in> exec eval"
    45 
    46   | Skip:    "(SKIP,s) -[eval]-> s"
    47 
    48   | Assign:  "(e,s) -|[eval]-> (v,s') ==> (x := e, s) -[eval]-> s'(x:=v)"
    49 
    50   | Semi:    "[| (c0,s) -[eval]-> s2; (c1,s2) -[eval]-> s1 |]
    51              ==> (c0 ;; c1, s) -[eval]-> s1"
    52 
    53   | IfTrue: "[| (e,s) -|[eval]-> (0,s');  (c0,s') -[eval]-> s1 |]
    54              ==> (IF e THEN c0 ELSE c1, s) -[eval]-> s1"
    55 
    56   | IfFalse: "[| (e,s) -|[eval]->  (Suc 0, s');  (c1,s') -[eval]-> s1 |]
    57               ==> (IF e THEN c0 ELSE c1, s) -[eval]-> s1"
    58 
    59   | WhileFalse: "(e,s) -|[eval]-> (Suc 0, s1)
    60                  ==> (WHILE e DO c, s) -[eval]-> s1"
    61 
    62   | WhileTrue:  "[| (e,s) -|[eval]-> (0,s1);
    63                     (c,s1) -[eval]-> s2;  (WHILE e DO c, s2) -[eval]-> s3 |]
    64                  ==> (WHILE e DO c, s) -[eval]-> s3"
    65 
    66 declare exec.intros [intro]
    67 
    68 
    69 inductive_cases
    70         [elim!]: "(SKIP,s) -[eval]-> t"
    71     and [elim!]: "(x:=a,s) -[eval]-> t"
    72     and [elim!]: "(c1;;c2, s) -[eval]-> t"
    73     and [elim!]: "(IF e THEN c1 ELSE c2, s) -[eval]-> t"
    74     and exec_WHILE_case: "(WHILE b DO c,s) -[eval]-> t"
    75 
    76 
    77 text{*Justifies using "exec" in the inductive definition of "eval"*}
    78 lemma exec_mono: "A<=B ==> exec(A) <= exec(B)"
    79 apply (rule subsetI)
    80 apply (simp add: split_paired_all)
    81 apply (erule exec.induct)
    82 apply blast+
    83 done
    84 
    85 lemma [pred_set_conv]:
    86   "((\<lambda>x x' y y'. ((x, x'), (y, y')) \<in> R) <= (\<lambda>x x' y y'. ((x, x'), (y, y')) \<in> S)) = (R <= S)"
    87   by (auto simp add: le_fun_def le_bool_def mem_def)
    88 
    89 lemma [pred_set_conv]:
    90   "((\<lambda>x x' y. ((x, x'), y) \<in> R) <= (\<lambda>x x' y. ((x, x'), y) \<in> S)) = (R <= S)"
    91   by (auto simp add: le_fun_def le_bool_def mem_def)
    92 
    93 text{*Command execution is functional (deterministic) provided evaluation is*}
    94 theorem single_valued_exec: "single_valued ev ==> single_valued(exec ev)"
    95 apply (simp add: single_valued_def)
    96 apply (intro allI)
    97 apply (rule impI)
    98 apply (erule exec.induct)
    99 apply (blast elim: exec_WHILE_case)+
   100 done
   101 
   102 
   103 subsection {* Expressions *}
   104 
   105 text{* Evaluation of arithmetic expressions *}
   106 
   107 inductive_set
   108   eval    :: "((exp*state) * (nat*state)) set"
   109   and eval_rel :: "[exp*state,nat*state] => bool"  (infixl "-|->" 50)
   110   where
   111     "esig -|-> ns == (esig, ns) \<in> eval"
   112 
   113   | N [intro!]: "(N(n),s) -|-> (n,s)"
   114 
   115   | X [intro!]: "(X(x),s) -|-> (s(x),s)"
   116 
   117   | Op [intro]: "[| (e0,s) -|-> (n0,s0);  (e1,s0)  -|-> (n1,s1) |]
   118                  ==> (Op f e0 e1, s) -|-> (f n0 n1, s1)"
   119 
   120   | valOf [intro]: "[| (c,s) -[eval]-> s0;  (e,s0)  -|-> (n,s1) |]
   121                     ==> (VALOF c RESULTIS e, s) -|-> (n, s1)"
   122 
   123   monos exec_mono
   124 
   125 
   126 inductive_cases
   127         [elim!]: "(N(n),sigma) -|-> (n',s')"
   128     and [elim!]: "(X(x),sigma) -|-> (n,s')"
   129     and [elim!]: "(Op f a1 a2,sigma)  -|-> (n,s')"
   130     and [elim!]: "(VALOF c RESULTIS e, s) -|-> (n, s1)"
   131 
   132 
   133 lemma var_assign_eval [intro!]: "(X x, s(x:=n)) -|-> (n, s(x:=n))"
   134 by (rule fun_upd_same [THEN subst], fast)
   135 
   136 
   137 text{* Make the induction rule look nicer -- though @{text eta_contract} makes the new
   138     version look worse than it is...*}
   139 
   140 lemma split_lemma:
   141      "{((e,s),(n,s')). P e s n s'} = Collect (split (%v. split (split P v)))"
   142 by auto
   143 
   144 text{*New induction rule.  Note the form of the VALOF induction hypothesis*}
   145 lemma eval_induct
   146   [case_names N X Op valOf, consumes 1, induct set: eval]:
   147   "[| (e,s) -|-> (n,s');
   148       !!n s. P (N n) s n s;
   149       !!s x. P (X x) s (s x) s;
   150       !!e0 e1 f n0 n1 s s0 s1.
   151          [| (e0,s) -|-> (n0,s0); P e0 s n0 s0;
   152             (e1,s0) -|-> (n1,s1); P e1 s0 n1 s1
   153          |] ==> P (Op f e0 e1) s (f n0 n1) s1;
   154       !!c e n s s0 s1.
   155          [| (c,s) -[eval Int {((e,s),(n,s')). P e s n s'}]-> s0;
   156             (c,s) -[eval]-> s0;
   157             (e,s0) -|-> (n,s1); P e s0 n s1 |]
   158          ==> P (VALOF c RESULTIS e) s n s1
   159    |] ==> P e s n s'"
   160 apply (induct set: eval)
   161 apply blast
   162 apply blast
   163 apply blast
   164 apply (frule Int_lower1 [THEN exec_mono, THEN subsetD])
   165 apply (auto simp add: split_lemma)
   166 done
   167 
   168 
   169 text{*Lemma for @{text Function_eval}.  The major premise is that @{text "(c,s)"} executes to @{text "s1"}
   170   using eval restricted to its functional part.  Note that the execution
   171   @{text "(c,s) -[eval]-> s2"} can use unrestricted @{text eval}!  The reason is that
   172   the execution @{text "(c,s) -[eval Int {...}]-> s1"} assures us that execution is
   173   functional on the argument @{text "(c,s)"}.
   174 *}
   175 lemma com_Unique:
   176  "(c,s) -[eval Int {((e,s),(n,t)). \<forall>nt'. (e,s) -|-> nt' --> (n,t)=nt'}]-> s1
   177   ==> \<forall>s2. (c,s) -[eval]-> s2 --> s2=s1"
   178 apply (induct set: exec)
   179       apply simp_all
   180       apply blast
   181      apply force
   182     apply blast
   183    apply blast
   184   apply blast
   185  apply (blast elim: exec_WHILE_case)
   186 apply (erule_tac V = "(?c,s2) -[?ev]-> s3" in thin_rl)
   187 apply clarify
   188 apply (erule exec_WHILE_case, blast+)
   189 done
   190 
   191 
   192 text{*Expression evaluation is functional, or deterministic*}
   193 theorem single_valued_eval: "single_valued eval"
   194 apply (unfold single_valued_def)
   195 apply (intro allI, rule impI)
   196 apply (simp (no_asm_simp) only: split_tupled_all)
   197 apply (erule eval_induct)
   198 apply (drule_tac [4] com_Unique)
   199 apply (simp_all (no_asm_use))
   200 apply blast+
   201 done
   202 
   203 lemma eval_N_E [dest!]: "(N n, s) -|-> (v, s') ==> (v = n & s' = s)"
   204   by (induct e == "N n" s v s' set: eval) simp_all
   205 
   206 text{*This theorem says that "WHILE TRUE DO c" cannot terminate*}
   207 lemma while_true_E:
   208     "(c', s) -[eval]-> t ==> c' = WHILE (N 0) DO c ==> False"
   209   by (induct set: exec) auto
   210 
   211 
   212 subsection{* Equivalence of IF e THEN c;;(WHILE e DO c) ELSE SKIP  and
   213        WHILE e DO c *}
   214 
   215 lemma while_if1:
   216      "(c',s) -[eval]-> t
   217       ==> c' = WHILE e DO c ==>
   218           (IF e THEN c;;c' ELSE SKIP, s) -[eval]-> t"
   219   by (induct set: exec) auto
   220 
   221 lemma while_if2:
   222      "(c',s) -[eval]-> t
   223       ==> c' = IF e THEN c;;(WHILE e DO c) ELSE SKIP ==>
   224           (WHILE e DO c, s) -[eval]-> t"
   225   by (induct set: exec) auto
   226 
   227 
   228 theorem while_if:
   229      "((IF e THEN c;;(WHILE e DO c) ELSE SKIP, s) -[eval]-> t)  =
   230       ((WHILE e DO c, s) -[eval]-> t)"
   231 by (blast intro: while_if1 while_if2)
   232 
   233 
   234 
   235 subsection{* Equivalence of  (IF e THEN c1 ELSE c2);;c
   236                          and  IF e THEN (c1;;c) ELSE (c2;;c)   *}
   237 
   238 lemma if_semi1:
   239      "(c',s) -[eval]-> t
   240       ==> c' = (IF e THEN c1 ELSE c2);;c ==>
   241           (IF e THEN (c1;;c) ELSE (c2;;c), s) -[eval]-> t"
   242   by (induct set: exec) auto
   243 
   244 lemma if_semi2:
   245      "(c',s) -[eval]-> t
   246       ==> c' = IF e THEN (c1;;c) ELSE (c2;;c) ==>
   247           ((IF e THEN c1 ELSE c2);;c, s) -[eval]-> t"
   248   by (induct set: exec) auto
   249 
   250 theorem if_semi: "(((IF e THEN c1 ELSE c2);;c, s) -[eval]-> t)  =
   251                   ((IF e THEN (c1;;c) ELSE (c2;;c), s) -[eval]-> t)"
   252   by (blast intro: if_semi1 if_semi2)
   253 
   254 
   255 
   256 subsection{* Equivalence of  VALOF c1 RESULTIS (VALOF c2 RESULTIS e)
   257                   and  VALOF c1;;c2 RESULTIS e
   258  *}
   259 
   260 lemma valof_valof1:
   261      "(e',s) -|-> (v,s')
   262       ==> e' = VALOF c1 RESULTIS (VALOF c2 RESULTIS e) ==>
   263           (VALOF c1;;c2 RESULTIS e, s) -|-> (v,s')"
   264   by (induct set: eval) auto
   265 
   266 lemma valof_valof2:
   267      "(e',s) -|-> (v,s')
   268       ==> e' = VALOF c1;;c2 RESULTIS e ==>
   269           (VALOF c1 RESULTIS (VALOF c2 RESULTIS e), s) -|-> (v,s')"
   270   by (induct set: eval) auto
   271 
   272 theorem valof_valof:
   273      "((VALOF c1 RESULTIS (VALOF c2 RESULTIS e), s) -|-> (v,s'))  =
   274       ((VALOF c1;;c2 RESULTIS e, s) -|-> (v,s'))"
   275   by (blast intro: valof_valof1 valof_valof2)
   276 
   277 
   278 subsection{* Equivalence of  VALOF SKIP RESULTIS e  and  e *}
   279 
   280 lemma valof_skip1:
   281      "(e',s) -|-> (v,s')
   282       ==> e' = VALOF SKIP RESULTIS e ==>
   283           (e, s) -|-> (v,s')"
   284   by (induct set: eval) auto
   285 
   286 lemma valof_skip2:
   287     "(e,s) -|-> (v,s') ==> (VALOF SKIP RESULTIS e, s) -|-> (v,s')"
   288   by blast
   289 
   290 theorem valof_skip:
   291     "((VALOF SKIP RESULTIS e, s) -|-> (v,s'))  =  ((e, s) -|-> (v,s'))"
   292   by (blast intro: valof_skip1 valof_skip2)
   293 
   294 
   295 subsection{* Equivalence of  VALOF x:=e RESULTIS x  and  e *}
   296 
   297 lemma valof_assign1:
   298      "(e',s) -|-> (v,s'')
   299       ==> e' = VALOF x:=e RESULTIS X x ==>
   300           (\<exists>s'. (e, s) -|-> (v,s') & (s'' = s'(x:=v)))"
   301   by (induct set: eval) (simp_all del: fun_upd_apply, clarify, auto)
   302 
   303 lemma valof_assign2:
   304     "(e,s) -|-> (v,s') ==> (VALOF x:=e RESULTIS X x, s) -|-> (v,s'(x:=v))"
   305   by blast
   306 
   307 end