src/HOL/Induct/Com.thy
author urbanc
Tue Jun 05 09:56:19 2007 +0200 (2007-06-05)
changeset 23243 a37d3e6e8323
parent 19736 d8d0f8f51d69
child 23746 a455e69c31cc
permissions -rw-r--r--
included Class.thy in the compiling process for Nominal/Examples
     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 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 
    38 abbreviation
    39   exec_rel  ("_/ -[_]-> _" [50,0,50] 50)
    40   "csig -[eval]-> s == (csig,s) \<in> exec eval"
    41 
    42 abbreviation (input)
    43   generic_rel  ("_/ -|[_]-> _" [50,0,50] 50)
    44   "esig -|[eval]-> ns == (esig,ns) \<in> eval"
    45 
    46 text{*Command execution.  Natural numbers represent Booleans: 0=True, 1=False*}
    47 inductive "exec eval"
    48   intros
    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 (unfold exec.defs )
    83 apply (rule lfp_mono)
    84 apply (assumption | rule basic_monos)+
    85 done
    86 
    87 ML {*
    88 Unify.trace_bound := 30;
    89 Unify.search_bound := 60;
    90 *}
    91 
    92 text{*Command execution is functional (deterministic) provided evaluation is*}
    93 theorem single_valued_exec: "single_valued ev ==> single_valued(exec ev)"
    94 apply (simp add: single_valued_def)
    95 apply (intro allI)
    96 apply (rule impI)
    97 apply (erule exec.induct)
    98 apply (blast elim: exec_WHILE_case)+
    99 done
   100 
   101 
   102 subsection {* Expressions *}
   103 
   104 text{* Evaluation of arithmetic expressions *}
   105 consts
   106   eval    :: "((exp*state) * (nat*state)) set"
   107 
   108 abbreviation
   109   eval_rel :: "[exp*state,nat*state] => bool"         (infixl "-|->" 50)
   110   "esig -|-> ns == (esig, ns) \<in> eval"
   111 
   112 inductive eval
   113   intros
   114     N [intro!]: "(N(n),s) -|-> (n,s)"
   115 
   116     X [intro!]: "(X(x),s) -|-> (s(x),s)"
   117 
   118     Op [intro]: "[| (e0,s) -|-> (n0,s0);  (e1,s0)  -|-> (n1,s1) |]
   119                  ==> (Op f e0 e1, s) -|-> (f n0 n1, s1)"
   120 
   121     valOf [intro]: "[| (c,s) -[eval]-> s0;  (e,s0)  -|-> (n,s1) |]
   122                     ==> (VALOF c RESULTIS e, s) -|-> (n, s1)"
   123 
   124   monos exec_mono
   125 
   126 
   127 inductive_cases
   128         [elim!]: "(N(n),sigma) -|-> (n',s')"
   129     and [elim!]: "(X(x),sigma) -|-> (n,s')"
   130     and [elim!]: "(Op f a1 a2,sigma)  -|-> (n,s')"
   131     and [elim!]: "(VALOF c RESULTIS e, s) -|-> (n, s1)"
   132 
   133 
   134 lemma var_assign_eval [intro!]: "(X x, s(x:=n)) -|-> (n, s(x:=n))"
   135 by (rule fun_upd_same [THEN subst], fast)
   136 
   137 
   138 text{* Make the induction rule look nicer -- though eta_contract makes the new
   139     version look worse than it is...*}
   140 
   141 lemma split_lemma:
   142      "{((e,s),(n,s')). P e s n s'} = Collect (split (%v. split (split P v)))"
   143 by auto
   144 
   145 text{*New induction rule.  Note the form of the VALOF induction hypothesis*}
   146 lemma eval_induct
   147   [case_names N X Op valOf, consumes 1, induct set: eval]:
   148   "[| (e,s) -|-> (n,s');
   149       !!n s. P (N n) s n s;
   150       !!s x. P (X x) s (s x) s;
   151       !!e0 e1 f n0 n1 s s0 s1.
   152          [| (e0,s) -|-> (n0,s0); P e0 s n0 s0;
   153             (e1,s0) -|-> (n1,s1); P e1 s0 n1 s1
   154          |] ==> P (Op f e0 e1) s (f n0 n1) s1;
   155       !!c e n s s0 s1.
   156          [| (c,s) -[eval Int {((e,s),(n,s')). P e s n s'}]-> s0;
   157             (c,s) -[eval]-> s0;
   158             (e,s0) -|-> (n,s1); P e s0 n s1 |]
   159          ==> P (VALOF c RESULTIS e) s n s1
   160    |] ==> P e s n s'"
   161 apply (induct set: eval)
   162 apply blast
   163 apply blast
   164 apply blast
   165 apply (frule Int_lower1 [THEN exec_mono, THEN subsetD])
   166 apply (auto simp add: split_lemma)
   167 done
   168 
   169 
   170 text{*Lemma for Function_eval.  The major premise is that (c,s) executes to s1
   171   using eval restricted to its functional part.  Note that the execution
   172   (c,s) -[eval]-> s2 can use unrestricted eval!  The reason is that
   173   the execution (c,s) -[eval Int {...}]-> s1 assures us that execution is
   174   functional on the argument (c,s).
   175 *}
   176 lemma com_Unique:
   177  "(c,s) -[eval Int {((e,s),(n,t)). \<forall>nt'. (e,s) -|-> nt' --> (n,t)=nt'}]-> s1
   178   ==> \<forall>s2. (c,s) -[eval]-> s2 --> s2=s1"
   179 apply (induct set: exec)
   180       apply 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 lemma eval_N_E [dest!]: "(N n, s) -|-> (v, s') ==> (v = n & s' = s)"
   205   by (induct e == "N n" s v s' set: eval) simp_all
   206 
   207 text{*This theorem says that "WHILE TRUE DO c" cannot terminate*}
   208 lemma while_true_E:
   209     "(c', s) -[eval]-> t ==> c' = WHILE (N 0) DO c ==> False"
   210   by (induct set: exec) auto
   211 
   212 
   213 subsection{* Equivalence of IF e THEN c;;(WHILE e DO c) ELSE SKIP  and
   214        WHILE e DO c *}
   215 
   216 lemma while_if1:
   217      "(c',s) -[eval]-> t
   218       ==> c' = WHILE e DO c ==>
   219           (IF e THEN c;;c' ELSE SKIP, s) -[eval]-> t"
   220   by (induct set: exec) auto
   221 
   222 lemma while_if2:
   223      "(c',s) -[eval]-> t
   224       ==> c' = IF e THEN c;;(WHILE e DO c) ELSE SKIP ==>
   225           (WHILE e DO c, s) -[eval]-> t"
   226   by (induct set: exec) auto
   227 
   228 
   229 theorem while_if:
   230      "((IF e THEN c;;(WHILE e DO c) ELSE SKIP, s) -[eval]-> t)  =
   231       ((WHILE e DO c, s) -[eval]-> t)"
   232 by (blast intro: while_if1 while_if2)
   233 
   234 
   235 
   236 subsection{* Equivalence of  (IF e THEN c1 ELSE c2);;c
   237                          and  IF e THEN (c1;;c) ELSE (c2;;c)   *}
   238 
   239 lemma if_semi1:
   240      "(c',s) -[eval]-> t
   241       ==> c' = (IF e THEN c1 ELSE c2);;c ==>
   242           (IF e THEN (c1;;c) ELSE (c2;;c), s) -[eval]-> t"
   243   by (induct set: exec) auto
   244 
   245 lemma if_semi2:
   246      "(c',s) -[eval]-> t
   247       ==> c' = IF e THEN (c1;;c) ELSE (c2;;c) ==>
   248           ((IF e THEN c1 ELSE c2);;c, s) -[eval]-> t"
   249   by (induct set: exec) auto
   250 
   251 theorem if_semi: "(((IF e THEN c1 ELSE c2);;c, s) -[eval]-> t)  =
   252                   ((IF e THEN (c1;;c) ELSE (c2;;c), s) -[eval]-> t)"
   253   by (blast intro: if_semi1 if_semi2)
   254 
   255 
   256 
   257 subsection{* Equivalence of  VALOF c1 RESULTIS (VALOF c2 RESULTIS e)
   258                   and  VALOF c1;;c2 RESULTIS e
   259  *}
   260 
   261 lemma valof_valof1:
   262      "(e',s) -|-> (v,s')
   263       ==> e' = VALOF c1 RESULTIS (VALOF c2 RESULTIS e) ==>
   264           (VALOF c1;;c2 RESULTIS e, s) -|-> (v,s')"
   265   by (induct set: eval) auto
   266 
   267 lemma valof_valof2:
   268      "(e',s) -|-> (v,s')
   269       ==> e' = VALOF c1;;c2 RESULTIS e ==>
   270           (VALOF c1 RESULTIS (VALOF c2 RESULTIS e), s) -|-> (v,s')"
   271   by (induct set: eval) auto
   272 
   273 theorem valof_valof:
   274      "((VALOF c1 RESULTIS (VALOF c2 RESULTIS e), s) -|-> (v,s'))  =
   275       ((VALOF c1;;c2 RESULTIS e, s) -|-> (v,s'))"
   276   by (blast intro: valof_valof1 valof_valof2)
   277 
   278 
   279 subsection{* Equivalence of  VALOF SKIP RESULTIS e  and  e *}
   280 
   281 lemma valof_skip1:
   282      "(e',s) -|-> (v,s')
   283       ==> e' = VALOF SKIP RESULTIS e ==>
   284           (e, s) -|-> (v,s')"
   285   by (induct set: eval) auto
   286 
   287 lemma valof_skip2:
   288     "(e,s) -|-> (v,s') ==> (VALOF SKIP RESULTIS e, s) -|-> (v,s')"
   289   by blast
   290 
   291 theorem valof_skip:
   292     "((VALOF SKIP RESULTIS e, s) -|-> (v,s'))  =  ((e, s) -|-> (v,s'))"
   293   by (blast intro: valof_skip1 valof_skip2)
   294 
   295 
   296 subsection{* Equivalence of  VALOF x:=e RESULTIS x  and  e *}
   297 
   298 lemma valof_assign1:
   299      "(e',s) -|-> (v,s'')
   300       ==> e' = VALOF x:=e RESULTIS X x ==>
   301           (\<exists>s'. (e, s) -|-> (v,s') & (s'' = s'(x:=v)))"
   302   by (induct set: eval) (simp_all del: fun_upd_apply, clarify, auto)
   303 
   304 lemma valof_assign2:
   305     "(e,s) -|-> (v,s') ==> (VALOF x:=e RESULTIS X x, s) -|-> (v,s'(x:=v))"
   306   by blast
   307 
   308 end