src/HOLCF/ex/Loop.ML
changeset 1168 74be52691d62
parent 1043 ffa68eb2730b
child 1267 bca91b4e1710
equal deleted inserted replaced
1167:cbd32a0f2f41 1168:74be52691d62
    11 (* --------------------------------------------------------------------------- *)
    11 (* --------------------------------------------------------------------------- *)
    12 (* access to definitions                                                       *)
    12 (* access to definitions                                                       *)
    13 (* --------------------------------------------------------------------------- *)
    13 (* --------------------------------------------------------------------------- *)
    14 
    14 
    15 val step_def2 = prove_goalw Loop.thy [step_def]
    15 val step_def2 = prove_goalw Loop.thy [step_def]
    16 "step[b][g][x] = If b[x] then g[x] else x fi"
    16 "step`b`g`x = If b`x then g`x else x fi"
    17  (fn prems =>
    17  (fn prems =>
    18 	[
    18 	[
    19 	(simp_tac Cfun_ss 1)
    19 	(simp_tac Cfun_ss 1)
    20 	]);
    20 	]);
    21 
    21 
    22 val while_def2 = prove_goalw Loop.thy [while_def]
    22 val while_def2 = prove_goalw Loop.thy [while_def]
    23 "while[b][g] = fix[LAM f x. If b[x] then f[g[x]] else x fi]"
    23 "while`b`g = fix`(LAM f x. If b`x then f`(g`x) else x fi)"
    24  (fn prems =>
    24  (fn prems =>
    25 	[
    25 	[
    26 	(simp_tac Cfun_ss 1)
    26 	(simp_tac Cfun_ss 1)
    27 	]);
    27 	]);
    28 
    28 
    30 (* ------------------------------------------------------------------------- *)
    30 (* ------------------------------------------------------------------------- *)
    31 (* rekursive properties of while                                             *)
    31 (* rekursive properties of while                                             *)
    32 (* ------------------------------------------------------------------------- *)
    32 (* ------------------------------------------------------------------------- *)
    33 
    33 
    34 val while_unfold = prove_goal Loop.thy 
    34 val while_unfold = prove_goal Loop.thy 
    35 "while[b][g][x] = If b[x] then while[b][g][g[x]] else x fi"
    35 "while`b`g`x = If b`x then while`b`g`(g`x) else x fi"
    36  (fn prems =>
    36  (fn prems =>
    37 	[
    37 	[
    38 	(fix_tac5  while_def2 1),
    38 	(fix_tac5  while_def2 1),
    39 	(simp_tac Cfun_ss 1)
    39 	(simp_tac Cfun_ss 1)
    40 	]);
    40 	]);
    41 
    41 
    42 val while_unfold2 = prove_goal Loop.thy 
    42 val while_unfold2 = prove_goal Loop.thy 
    43 	"!x.while[b][g][x] = while[b][g][iterate(k,step[b][g],x)]"
    43 	"!x.while`b`g`x = while`b`g`(iterate k (step`b`g) x)"
    44  (fn prems =>
    44  (fn prems =>
    45 	[
    45 	[
    46 	(nat_ind_tac "k" 1),
    46 	(nat_ind_tac "k" 1),
    47 	(simp_tac (HOLCF_ss addsimps [iterate_0,iterate_Suc]) 1),
    47 	(simp_tac (HOLCF_ss addsimps [iterate_0,iterate_Suc]) 1),
    48 	(rtac allI 1),
    48 	(rtac allI 1),
    51 	(rtac refl 2),
    51 	(rtac refl 2),
    52 	(rtac (iterate_Suc2 RS ssubst) 1),
    52 	(rtac (iterate_Suc2 RS ssubst) 1),
    53 	(rtac trans 1),
    53 	(rtac trans 1),
    54 	(etac spec 2),
    54 	(etac spec 2),
    55 	(rtac (step_def2 RS ssubst) 1),
    55 	(rtac (step_def2 RS ssubst) 1),
    56 	(res_inst_tac [("p","b[x]")] trE 1),
    56 	(res_inst_tac [("p","b`x")] trE 1),
    57 	(asm_simp_tac HOLCF_ss 1),
    57 	(asm_simp_tac HOLCF_ss 1),
    58 	(rtac (while_unfold RS ssubst) 1),
    58 	(rtac (while_unfold RS ssubst) 1),
    59 	(res_inst_tac [("s","UU"),("t","b[UU]")] ssubst 1),
    59 	(res_inst_tac [("s","UU"),("t","b`UU")]ssubst 1),
    60 	(etac (flat_tr RS flat_codom RS disjE) 1),
    60 	(etac (flat_tr RS flat_codom RS disjE) 1),
    61 	(atac 1),
    61 	(atac 1),
    62 	(etac spec 1),
    62 	(etac spec 1),
    63 	(simp_tac HOLCF_ss 1),
    63 	(simp_tac HOLCF_ss 1),
    64 	(asm_simp_tac HOLCF_ss 1),
    64 	(asm_simp_tac HOLCF_ss 1),
    66 	(rtac (while_unfold RS ssubst) 1),
    66 	(rtac (while_unfold RS ssubst) 1),
    67 	(asm_simp_tac HOLCF_ss 1)
    67 	(asm_simp_tac HOLCF_ss 1)
    68 	]);
    68 	]);
    69 
    69 
    70 val while_unfold3 = prove_goal Loop.thy 
    70 val while_unfold3 = prove_goal Loop.thy 
    71 	"while[b][g][x] = while[b][g][step[b][g][x]]"
    71 	"while`b`g`x = while`b`g`(step`b`g`x)"
    72  (fn prems =>
    72  (fn prems =>
    73 	[
    73 	[
    74 	(res_inst_tac [("s","while[b][g][iterate(Suc(0),step[b][g],x)]")] trans 1),
    74 	(res_inst_tac [("s",
       
    75 		"while`b`g`(iterate (Suc 0) (step`b`g) x)")] trans 1),
    75 	(rtac (while_unfold2 RS spec) 1),
    76 	(rtac (while_unfold2 RS spec) 1),
    76 	(simp_tac iterate_ss 1)
    77 	(simp_tac iterate_ss 1)
    77 	]);
    78 	]);
    78 
    79 
    79 
    80 
    80 (* --------------------------------------------------------------------------- *)
    81 (* --------------------------------------------------------------------------- *)
    81 (* properties of while and iterations                                          *)
    82 (* properties of while and iterations                                          *)
    82 (* --------------------------------------------------------------------------- *)
    83 (* --------------------------------------------------------------------------- *)
    83 
    84 
    84 val loop_lemma1 = prove_goal Loop.thy
    85 val loop_lemma1 = prove_goal Loop.thy
    85 "[|? y.b[y]=FF; iterate(k,step[b][g],x)=UU|]==>iterate(Suc(k),step[b][g],x)=UU"
    86 "[|? y.b`y=FF; iterate k (step`b`g) x = UU|]==>iterate(Suc k) (step`b`g) x=UU"
    86  (fn prems =>
    87  (fn prems =>
    87 	[
    88 	[
    88 	(cut_facts_tac prems 1),
    89 	(cut_facts_tac prems 1),
    89 	(simp_tac iterate_ss 1),
    90 	(simp_tac iterate_ss 1),
    90 	(rtac trans 1),
    91 	(rtac trans 1),
    95 	(asm_simp_tac HOLCF_ss 1),
    96 	(asm_simp_tac HOLCF_ss 1),
    96 	(asm_simp_tac HOLCF_ss 1)
    97 	(asm_simp_tac HOLCF_ss 1)
    97 	]);
    98 	]);
    98 
    99 
    99 val loop_lemma2 = prove_goal Loop.thy
   100 val loop_lemma2 = prove_goal Loop.thy
   100 "[|? y.b[y]=FF;~iterate(Suc(k),step[b][g],x)=UU |]==>\
   101 "[|? y.b`y=FF;iterate (Suc k) (step`b`g) x ~=UU |]==>\
   101 \~iterate(k,step[b][g],x)=UU"
   102 \iterate k (step`b`g) x ~=UU"
   102  (fn prems =>
   103  (fn prems =>
   103 	[
   104 	[
   104 	(cut_facts_tac prems 1),
   105 	(cut_facts_tac prems 1),
   105 	(rtac contrapos 1),
   106 	(rtac contrapos 1),
   106 	(etac  loop_lemma1 2),
   107 	(etac  loop_lemma1 2),
   107 	(atac 1),
   108 	(atac 1),
   108 	(atac 1)
   109 	(atac 1)
   109 	]);
   110 	]);
   110 
   111 
   111 val loop_lemma3 = prove_goal Loop.thy
   112 val loop_lemma3 = prove_goal Loop.thy
   112 "[|!x. INV(x) & b[x]=TT & ~g[x]=UU --> INV(g[x]);\
   113 "[|!x. INV x & b`x=TT & g`x~=UU --> INV (g`x);\
   113 \? y.b[y]=FF; INV(x)|] ==>\
   114 \? y.b`y=FF; INV x|] ==>\
   114 \~iterate(k,step[b][g],x)=UU --> INV(iterate(k,step[b][g],x))"
   115 \iterate k (step`b`g) x ~=UU --> INV (iterate k (step`b`g) x)"
   115  (fn prems =>
   116  (fn prems =>
   116 	[
   117 	[
   117 	(cut_facts_tac prems 1),
   118 	(cut_facts_tac prems 1),
   118 	(nat_ind_tac "k" 1),
   119 	(nat_ind_tac "k" 1),
   119 	(asm_simp_tac iterate_ss 1),
   120 	(asm_simp_tac iterate_ss 1),
   120 	(strip_tac 1),
   121 	(strip_tac 1),
   121 	(simp_tac (iterate_ss addsimps [step_def2]) 1),
   122 	(simp_tac (iterate_ss addsimps [step_def2]) 1),
   122 	(res_inst_tac [("p","b[iterate(k1, step[b][g], x)]")] trE 1),
   123 	(res_inst_tac [("p","b`(iterate k1 (step`b`g) x)")] trE 1),
   123 	(etac notE 1),
   124 	(etac notE 1),
   124 	(asm_simp_tac (HOLCF_ss addsimps [step_def2,iterate_Suc] ) 1),
   125 	(asm_simp_tac (HOLCF_ss addsimps [step_def2,iterate_Suc] ) 1),
   125 	(asm_simp_tac HOLCF_ss  1),
   126 	(asm_simp_tac HOLCF_ss  1),
   126 	(rtac mp 1),
   127 	(rtac mp 1),
   127 	(etac spec  1),
   128 	(etac spec  1),
   128 	(asm_simp_tac (HOLCF_ss addsimps [loop_lemma2] ) 1),
   129 	(asm_simp_tac (HOLCF_ss addsimps [loop_lemma2] ) 1),
   129 	(res_inst_tac [("s","iterate(Suc(k1), step[b][g], x)"),
   130 	(res_inst_tac [("s","iterate (Suc k1) (step`b`g) x"),
   130 		("t","g[iterate(k1, step[b][g], x)]")] ssubst 1),
   131 		("t","g`(iterate k1 (step`b`g) x)")] ssubst 1),
   131 	(atac 2),
   132 	(atac 2),
   132 	(asm_simp_tac (HOLCF_ss addsimps [iterate_Suc,step_def2] ) 1),
   133 	(asm_simp_tac (HOLCF_ss addsimps [iterate_Suc,step_def2] ) 1),
   133 	(asm_simp_tac (HOLCF_ss addsimps [loop_lemma2] ) 1)
   134 	(asm_simp_tac (HOLCF_ss addsimps [loop_lemma2] ) 1)
   134 	]);
   135 	]);
   135 
   136 
   136 
   137 
   137 val loop_lemma4 = prove_goal Loop.thy
   138 val loop_lemma4 = prove_goal Loop.thy
   138 "!x. b[iterate(k,step[b][g],x)]=FF --> while[b][g][x]=iterate(k,step[b][g],x)"
   139 "!x. b`(iterate k (step`b`g) x)=FF --> while`b`g`x= iterate k (step`b`g) x"
   139  (fn prems =>
   140  (fn prems =>
   140 	[
   141 	[
   141 	(nat_ind_tac "k" 1),
   142 	(nat_ind_tac "k" 1),
   142 	(simp_tac iterate_ss 1),
   143 	(simp_tac iterate_ss 1),
   143 	(strip_tac 1),
   144 	(strip_tac 1),
   150 	(rtac while_unfold3 1),
   151 	(rtac while_unfold3 1),
   151 	(asm_simp_tac HOLCF_ss  1)
   152 	(asm_simp_tac HOLCF_ss  1)
   152 	]);
   153 	]);
   153 
   154 
   154 val loop_lemma5 = prove_goal Loop.thy
   155 val loop_lemma5 = prove_goal Loop.thy
   155 "!k. ~b[iterate(k,step[b][g],x)]=FF ==>\
   156 "!k. b`(iterate k (step`b`g) x) ~= FF ==>\
   156 \ !m. while[b][g][iterate(m,step[b][g],x)]=UU"
   157 \ !m. while`b`g`(iterate m (step`b`g) x)=UU"
   157  (fn prems =>
   158  (fn prems =>
   158 	[
   159 	[
   159 	(cut_facts_tac prems 1),
   160 	(cut_facts_tac prems 1),
   160 	(rtac (while_def2 RS ssubst) 1),
   161 	(rtac (while_def2 RS ssubst) 1),
   161 	(rtac fix_ind 1),
   162 	(rtac fix_ind 1),
   162 	(rtac (allI RS adm_all) 1),
   163 	(rtac (allI RS adm_all) 1),
   163 	(rtac adm_eq 1),
   164 	(rtac adm_eq 1),
   164 	(contX_tacR 1),
   165 	(cont_tacR 1),
   165 	(simp_tac HOLCF_ss  1),
   166 	(simp_tac HOLCF_ss  1),
   166 	(rtac allI 1),
   167 	(rtac allI 1),
   167 	(simp_tac HOLCF_ss  1),
   168 	(simp_tac HOLCF_ss  1),
   168 	(res_inst_tac [("p","b[iterate(m, step[b][g],x)]")] trE 1),
   169 	(res_inst_tac [("p","b`(iterate m (step`b`g) x)")] trE 1),
   169 	(asm_simp_tac HOLCF_ss  1),
   170 	(asm_simp_tac HOLCF_ss  1),
   170 	(asm_simp_tac HOLCF_ss  1),
   171 	(asm_simp_tac HOLCF_ss  1),
   171 	(res_inst_tac [("s","xa[iterate(Suc(m), step[b][g], x)]")] trans 1),
   172 	(res_inst_tac [("s","xa`(iterate (Suc m) (step`b`g) x)")] trans 1),
   172 	(etac spec 2),
   173 	(etac spec 2),
   173 	(rtac cfun_arg_cong 1),
   174 	(rtac cfun_arg_cong 1),
   174 	(rtac trans 1),
   175 	(rtac trans 1),
   175 	(rtac (iterate_Suc RS sym) 2),
   176 	(rtac (iterate_Suc RS sym) 2),
   176 	(asm_simp_tac (HOLCF_ss addsimps [step_def2]) 1),
   177 	(asm_simp_tac (HOLCF_ss addsimps [step_def2]) 1),
   178 	(contr_tac 1)
   179 	(contr_tac 1)
   179 	]);
   180 	]);
   180 
   181 
   181 
   182 
   182 val loop_lemma6 = prove_goal Loop.thy
   183 val loop_lemma6 = prove_goal Loop.thy
   183 "!k. ~b[iterate(k,step[b][g],x)]=FF ==> while[b][g][x]=UU"
   184 "!k. b`(iterate k (step`b`g) x) ~= FF ==> while`b`g`x=UU"
   184  (fn prems =>
   185  (fn prems =>
   185 	[
   186 	[
   186 	(res_inst_tac [("t","x")] (iterate_0 RS subst) 1),
   187 	(res_inst_tac [("t","x")] (iterate_0 RS subst) 1),
   187 	(rtac (loop_lemma5 RS spec) 1),
   188 	(rtac (loop_lemma5 RS spec) 1),
   188 	(resolve_tac prems 1)
   189 	(resolve_tac prems 1)
   189 	]);
   190 	]);
   190 
   191 
   191 val loop_lemma7 = prove_goal Loop.thy
   192 val loop_lemma7 = prove_goal Loop.thy
   192 "~while[b][g][x]=UU ==> ? k. b[iterate(k,step[b][g],x)]=FF"
   193 "while`b`g`x ~= UU ==> ? k. b`(iterate k (step`b`g) x) = FF"
   193  (fn prems =>
   194  (fn prems =>
   194 	[
   195 	[
   195 	(cut_facts_tac prems 1),
   196 	(cut_facts_tac prems 1),
   196 	(etac swap 1),
   197 	(etac swap 1),
   197 	(rtac loop_lemma6 1),
   198 	(rtac loop_lemma6 1),
   198 	(fast_tac HOL_cs 1)
   199 	(fast_tac HOL_cs 1)
   199 	]);
   200 	]);
   200 
   201 
   201 val loop_lemma8 = prove_goal Loop.thy
   202 val loop_lemma8 = prove_goal Loop.thy
   202 "~while[b][g][x]=UU ==> ? y. b[y]=FF"
   203 "while`b`g`x ~= UU ==> ? y. b`y=FF"
   203  (fn prems =>
   204  (fn prems =>
   204 	[
   205 	[
   205 	(cut_facts_tac prems 1),
   206 	(cut_facts_tac prems 1),
   206 	(dtac loop_lemma7 1),
   207 	(dtac loop_lemma7 1),
   207 	(fast_tac HOL_cs 1)
   208 	(fast_tac HOL_cs 1)
   208 	]);
   209 	]);
   209 
   210 
   210 
   211 
   211 (* --------------------------------------------------------------------------- *)
   212 (* ------------------------------------------------------------------------- *)
   212 (* an invariant rule for loops                                                 *)
   213 (* an invariant rule for loops                                               *)
   213 (* --------------------------------------------------------------------------- *)
   214 (* ------------------------------------------------------------------------- *)
   214 
   215 
   215 val loop_inv2 = prove_goal Loop.thy
   216 val loop_inv2 = prove_goal Loop.thy
   216 "[| (!y. INV(y) & b[y]=TT & ~g[y]=UU --> INV(g[y]));\
   217 "[| (!y. INV y & b`y=TT & g`y ~= UU --> INV (g`y));\
   217 \   (!y. INV(y) & b[y]=FF --> Q(y));\
   218 \   (!y. INV y & b`y=FF --> Q y);\
   218 \   INV(x); ~while[b][g][x]=UU |] ==> Q(while[b][g][x])"
   219 \   INV x; while`b`g`x~=UU |] ==> Q (while`b`g`x)"
   219  (fn prems =>
   220  (fn prems =>
   220 	[
   221 	[
   221 	(res_inst_tac [("P","%k.b[iterate(k,step[b][g],x)]=FF")] exE 1),
   222 	(res_inst_tac [("P","%k. b`(iterate k (step`b`g) x)=FF")] exE 1),
   222 	(rtac loop_lemma7 1),
   223 	(rtac loop_lemma7 1),
   223 	(resolve_tac prems 1),
   224 	(resolve_tac prems 1),
   224 	(rtac ((loop_lemma4 RS spec RS mp) RS ssubst) 1),
   225 	(rtac ((loop_lemma4 RS spec RS mp) RS ssubst) 1),
   225 	(atac 1),
   226 	(atac 1),
   226 	(rtac (nth_elem (1,prems) RS spec RS mp) 1),
   227 	(rtac (nth_elem (1,prems) RS spec RS mp) 1),
   238 	(atac 1),
   239 	(atac 1),
   239 	(rtac refl 1)
   240 	(rtac refl 1)
   240 	]);
   241 	]);
   241 
   242 
   242 val loop_inv3 = prove_goal Loop.thy
   243 val loop_inv3 = prove_goal Loop.thy
   243 "[| !!y.[| INV(y); b[y]=TT; ~g[y]=UU|] ==> INV(g[y]);\
   244 "[| !!y.[| INV y; b`y=TT; g`y~=UU|] ==> INV (g`y);\
   244 \   !!y.[| INV(y); b[y]=FF|]==> Q(y);\
   245 \   !!y.[| INV y; b`y=FF|]==> Q y;\
   245 \   INV(x); ~while[b][g][x]=UU |] ==> Q(while[b][g][x])"
   246 \   INV x; while`b`g`x~=UU |] ==> Q (while`b`g`x)"
   246  (fn prems =>
   247  (fn prems =>
   247 	[
   248 	[
   248 	(rtac loop_inv2 1),
   249 	(rtac loop_inv2 1),
   249 	(rtac allI 1),
   250 	(rtac allI 1),
   250 	(rtac impI 1),
   251 	(rtac impI 1),
   261 	(resolve_tac prems 1)
   262 	(resolve_tac prems 1)
   262 	]);
   263 	]);
   263 
   264 
   264 val loop_inv = prove_goal Loop.thy
   265 val loop_inv = prove_goal Loop.thy
   265 "[| P(x);\
   266 "[| P(x);\
   266 \   !!y.P(y) ==> INV(y);\
   267 \   !!y.P y ==> INV y;\
   267 \   !!y.[| INV(y); b[y]=TT; ~g[y]=UU|] ==> INV(g[y]);\
   268 \   !!y.[| INV y; b`y=TT; g`y~=UU|] ==> INV (g`y);\
   268 \   !!y.[| INV(y); b[y]=FF|]==> Q(y);\
   269 \   !!y.[| INV y; b`y=FF|]==> Q y;\
   269 \   ~while[b][g][x]=UU |] ==> Q(while[b][g][x])"
   270 \   while`b`g`x ~= UU |] ==> Q (while`b`g`x)"
   270  (fn prems =>
   271  (fn prems =>
   271 	[
   272 	[
   272 	(rtac loop_inv3 1),
   273 	(rtac loop_inv3 1),
   273 	(eresolve_tac prems 1),
   274 	(eresolve_tac prems 1),
   274 	(atac 1),
   275 	(atac 1),