src/HOL/Induct/Com.thy
author paulson
Wed Apr 07 14:25:48 2004 +0200 (2004-04-07)
changeset 14527 bc9e5587d05a
parent 13075 d3e1d554cd6d
child 16417 9bc16273c2d4
permissions -rw-r--r--
IsaMakefile
     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 = Main:
    12 
    13 typedecl loc
    14 
    15 types  state = "loc => nat"
    16        n2n2n = "nat => nat => nat"
    17 
    18 arities loc :: type
    19 
    20 datatype
    21   exp = N nat
    22       | X loc
    23       | Op n2n2n exp exp
    24       | valOf com exp          ("VALOF _ RESULTIS _"  60)
    25 and
    26   com = SKIP
    27       | ":="  loc exp          (infixl  60)
    28       | Semi  com com          ("_;;_"  [60, 60] 60)
    29       | Cond  exp com com      ("IF _ THEN _ ELSE _"  60)
    30       | While exp com          ("WHILE _ DO _"  60)
    31 
    32 
    33 subsection {* Commands *}
    34 
    35 text{* Execution of commands *}
    36 consts  exec    :: "((exp*state) * (nat*state)) set => ((com*state)*state)set"
    37        "@exec"  :: "((exp*state) * (nat*state)) set => 
    38                     [com*state,state] => bool"     ("_/ -[_]-> _" [50,0,50] 50)
    39 
    40 translations  "csig -[eval]-> s" == "(csig,s) \<in> exec eval"
    41 
    42 syntax  eval'   :: "[exp*state,nat*state] => 
    43 		    ((exp*state) * (nat*state)) set => bool"
    44 					   ("_/ -|[_]-> _" [50,0,50] 50)
    45 
    46 translations
    47     "esig -|[eval]-> ns" => "(esig,ns) \<in> eval"
    48 
    49 text{*Command execution.  Natural numbers represent Booleans: 0=True, 1=False*}
    50 inductive "exec eval"
    51   intros
    52     Skip:    "(SKIP,s) -[eval]-> s"
    53 
    54     Assign:  "(e,s) -|[eval]-> (v,s') ==> (x := e, s) -[eval]-> s'(x:=v)"
    55 
    56     Semi:    "[| (c0,s) -[eval]-> s2; (c1,s2) -[eval]-> s1 |] 
    57              ==> (c0 ;; c1, s) -[eval]-> s1"
    58 
    59     IfTrue: "[| (e,s) -|[eval]-> (0,s');  (c0,s') -[eval]-> s1 |] 
    60              ==> (IF e THEN c0 ELSE c1, s) -[eval]-> s1"
    61 
    62     IfFalse: "[| (e,s) -|[eval]->  (Suc 0, s');  (c1,s') -[eval]-> s1 |] 
    63               ==> (IF e THEN c0 ELSE c1, s) -[eval]-> s1"
    64 
    65     WhileFalse: "(e,s) -|[eval]-> (Suc 0, s1) 
    66                  ==> (WHILE e DO c, s) -[eval]-> s1"
    67 
    68     WhileTrue:  "[| (e,s) -|[eval]-> (0,s1);
    69                     (c,s1) -[eval]-> s2;  (WHILE e DO c, s2) -[eval]-> s3 |] 
    70                  ==> (WHILE e DO c, s) -[eval]-> s3"
    71 
    72 declare exec.intros [intro]
    73 
    74 
    75 inductive_cases
    76 	[elim!]: "(SKIP,s) -[eval]-> t"
    77     and [elim!]: "(x:=a,s) -[eval]-> t"
    78     and	[elim!]: "(c1;;c2, s) -[eval]-> t"
    79     and	[elim!]: "(IF e THEN c1 ELSE c2, s) -[eval]-> t"
    80     and	exec_WHILE_case: "(WHILE b DO c,s) -[eval]-> t"
    81 
    82 
    83 text{*Justifies using "exec" in the inductive definition of "eval"*}
    84 lemma exec_mono: "A<=B ==> exec(A) <= exec(B)"
    85 apply (unfold exec.defs )
    86 apply (rule lfp_mono)
    87 apply (assumption | rule basic_monos)+
    88 done
    89 
    90 ML {*
    91 Unify.trace_bound := 30;
    92 Unify.search_bound := 60;
    93 *}
    94 
    95 text{*Command execution is functional (deterministic) provided evaluation is*}
    96 theorem single_valued_exec: "single_valued ev ==> single_valued(exec ev)"
    97 apply (simp add: single_valued_def)
    98 apply (intro allI) 
    99 apply (rule impI)
   100 apply (erule exec.induct)
   101 apply (blast elim: exec_WHILE_case)+
   102 done
   103 
   104 
   105 subsection {* Expressions *}
   106 
   107 text{* Evaluation of arithmetic expressions *}
   108 consts  eval    :: "((exp*state) * (nat*state)) set"
   109        "-|->"   :: "[exp*state,nat*state] => bool"         (infixl 50)
   110 
   111 translations
   112     "esig -|-> (n,s)" <= "(esig,n,s) \<in> eval"
   113     "esig -|-> ns"    == "(esig,ns ) \<in> eval"
   114   
   115 inductive eval
   116   intros 
   117     N [intro!]: "(N(n),s) -|-> (n,s)"
   118 
   119     X [intro!]: "(X(x),s) -|-> (s(x),s)"
   120 
   121     Op [intro]: "[| (e0,s) -|-> (n0,s0);  (e1,s0)  -|-> (n1,s1) |] 
   122                  ==> (Op f e0 e1, s) -|-> (f n0 n1, s1)"
   123 
   124     valOf [intro]: "[| (c,s) -[eval]-> s0;  (e,s0)  -|-> (n,s1) |] 
   125                     ==> (VALOF c RESULTIS e, s) -|-> (n, s1)"
   126 
   127   monos exec_mono
   128 
   129 
   130 inductive_cases
   131 	[elim!]: "(N(n),sigma) -|-> (n',s')"
   132     and [elim!]: "(X(x),sigma) -|-> (n,s')"
   133     and	[elim!]: "(Op f a1 a2,sigma)  -|-> (n,s')"
   134     and	[elim!]: "(VALOF c RESULTIS e, s) -|-> (n, s1)"
   135 
   136 
   137 lemma var_assign_eval [intro!]: "(X x, s(x:=n)) -|-> (n, s(x:=n))"
   138 by (rule fun_upd_same [THEN subst], fast)
   139 
   140 
   141 text{* Make the induction rule look nicer -- though eta_contract makes the new
   142     version look worse than it is...*}
   143 
   144 lemma split_lemma:
   145      "{((e,s),(n,s')). P e s n s'} = Collect (split (%v. split (split P v)))"
   146 by auto
   147 
   148 text{*New induction rule.  Note the form of the VALOF induction hypothesis*}
   149 lemma eval_induct:
   150   "[| (e,s) -|-> (n,s');                                          
   151       !!n s. P (N n) s n s;                                       
   152       !!s x. P (X x) s (s x) s;                                   
   153       !!e0 e1 f n0 n1 s s0 s1.                                    
   154          [| (e0,s) -|-> (n0,s0); P e0 s n0 s0;                    
   155             (e1,s0) -|-> (n1,s1); P e1 s0 n1 s1                   
   156          |] ==> P (Op f e0 e1) s (f n0 n1) s1;                    
   157       !!c e n s s0 s1.                                            
   158          [| (c,s) -[eval Int {((e,s),(n,s')). P e s n s'}]-> s0;  
   159             (c,s) -[eval]-> s0;                                   
   160             (e,s0) -|-> (n,s1); P e s0 n s1 |]                    
   161          ==> P (VALOF c RESULTIS e) s n s1                        
   162    |] ==> P e s n s'"
   163 apply (erule eval.induct, blast) 
   164 apply blast 
   165 apply blast 
   166 apply (frule Int_lower1 [THEN exec_mono, THEN subsetD])
   167 apply (auto simp add: split_lemma)
   168 done
   169 
   170 
   171 text{*Lemma for Function_eval.  The major premise is that (c,s) executes to s1
   172   using eval restricted to its functional part.  Note that the execution
   173   (c,s) -[eval]-> s2 can use unrestricted eval!  The reason is that 
   174   the execution (c,s) -[eval Int {...}]-> s1 assures us that execution is
   175   functional on the argument (c,s).
   176 *}
   177 lemma com_Unique:
   178  "(c,s) -[eval Int {((e,s),(n,t)). \<forall>nt'. (e,s) -|-> nt' --> (n,t)=nt'}]-> s1  
   179   ==> \<forall>s2. (c,s) -[eval]-> s2 --> s2=s1"
   180 apply (erule exec.induct, simp_all)
   181       apply blast
   182      apply force
   183     apply blast
   184    apply blast
   185   apply blast
   186  apply (blast elim: exec_WHILE_case)
   187 apply (erule_tac V = "(?c,s2) -[?ev]-> s3" in thin_rl)
   188 apply clarify
   189 apply (erule exec_WHILE_case, blast+) 
   190 done
   191 
   192 
   193 text{*Expression evaluation is functional, or deterministic*}
   194 theorem single_valued_eval: "single_valued eval"
   195 apply (unfold single_valued_def)
   196 apply (intro allI, rule impI) 
   197 apply (simp (no_asm_simp) only: split_tupled_all)
   198 apply (erule eval_induct)
   199 apply (drule_tac [4] com_Unique)
   200 apply (simp_all (no_asm_use))
   201 apply blast+
   202 done
   203 
   204 
   205 lemma eval_N_E_lemma: "(e,s) -|-> (v,s') ==> (e = N n) --> (v=n & s'=s)"
   206 by (erule eval_induct, simp_all)
   207 
   208 lemmas eval_N_E [dest!] = eval_N_E_lemma [THEN mp, OF _ refl]
   209 
   210 
   211 text{*This theorem says that "WHILE TRUE DO c" cannot terminate*}
   212 lemma while_true_E [rule_format]:
   213      "(c', s) -[eval]-> t ==> (c' = WHILE (N 0) DO c) --> False"
   214 by (erule exec.induct, 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 [rule_format]:
   221      "(c',s) -[eval]-> t 
   222       ==> (c' = WHILE e DO c) -->  
   223           (IF e THEN c;;c' ELSE SKIP, s) -[eval]-> t"
   224 by (erule exec.induct, auto)
   225 
   226 lemma while_if2 [rule_format]:
   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 (erule exec.induct, 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 [rule_format]:
   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 (erule exec.induct, auto)
   248 
   249 lemma if_semi2 [rule_format]:
   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 (erule exec.induct, 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 [rule_format]:
   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 (erule eval_induct, auto)
   270 
   271 
   272 lemma valof_valof2 [rule_format]:
   273      "(e',s) -|-> (v,s')
   274       ==> (e' = VALOF c1;;c2 RESULTIS e) -->  
   275           (VALOF c1 RESULTIS (VALOF c2 RESULTIS e), s) -|-> (v,s')"
   276 by (erule eval_induct, auto)
   277 
   278 theorem valof_valof:
   279      "((VALOF c1 RESULTIS (VALOF c2 RESULTIS e), s) -|-> (v,s'))  =   
   280       ((VALOF c1;;c2 RESULTIS e, s) -|-> (v,s'))"
   281 by (blast intro: valof_valof1 valof_valof2)
   282 
   283 
   284 subsection{* Equivalence of  VALOF SKIP RESULTIS e  and  e *}
   285 
   286 lemma valof_skip1 [rule_format]:
   287      "(e',s) -|-> (v,s')
   288       ==> (e' = VALOF SKIP RESULTIS e) -->  
   289           (e, s) -|-> (v,s')"
   290 by (erule eval_induct, auto)
   291 
   292 lemma valof_skip2:
   293      "(e,s) -|-> (v,s') ==> (VALOF SKIP RESULTIS e, s) -|-> (v,s')"
   294 by blast
   295 
   296 theorem valof_skip:
   297      "((VALOF SKIP RESULTIS e, s) -|-> (v,s'))  =  ((e, s) -|-> (v,s'))"
   298 by (blast intro: valof_skip1 valof_skip2)
   299 
   300 
   301 subsection{* Equivalence of  VALOF x:=e RESULTIS x  and  e *}
   302 
   303 lemma valof_assign1 [rule_format]:
   304      "(e',s) -|-> (v,s'')
   305       ==> (e' = VALOF x:=e RESULTIS X x) -->  
   306           (\<exists>s'. (e, s) -|-> (v,s') & (s'' = s'(x:=v)))"
   307 apply (erule eval_induct)
   308 apply (simp_all del: fun_upd_apply, clarify, auto) 
   309 done
   310 
   311 lemma valof_assign2:
   312      "(e,s) -|-> (v,s') ==> (VALOF x:=e RESULTIS X x, s) -|-> (v,s'(x:=v))"
   313 by blast
   314 
   315 
   316 end