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