While functional for defining tail-recursive functions
authornipkow
Wed Jul 26 19:43:28 2000 +0200 (2000-07-26 ago)
changeset 9448755330e55e18
parent 9447 e5180c869772
child 9449 2f814053a6cc
While functional for defining tail-recursive functions
src/HOL/While.ML
src/HOL/While.thy
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/HOL/While.ML	Wed Jul 26 19:43:28 2000 +0200
     1.3 @@ -0,0 +1,92 @@
     1.4 +(*  Title:      HOL/While
     1.5 +    ID:         $Id$
     1.6 +    Author:     Tobias Nipkow
     1.7 +    Copyright   2000 TU Muenchen
     1.8 +*)
     1.9 +
    1.10 +goalw_cterm [] (cterm_of (sign_of thy)
    1.11 + (HOLogic.mk_Trueprop (hd while_aux.tcs)));
    1.12 +br wf_same_fstI 1;
    1.13 +br wf_same_fstI 1;
    1.14 +by (asm_full_simp_tac (simpset() addsimps [wf_iff_no_infinite_down_chain]) 1);
    1.15 +by(Blast_tac 1);
    1.16 +val while_aux_tc = result();
    1.17 +
    1.18 +Goal
    1.19 + "while_aux(b,c,s) = (if ? f. f 0 = s & (!i. b(f i) & c(f i) = f(i+1)) \
    1.20 +\                     then arbitrary \
    1.21 +\                     else (if b s then while_aux(b,c,c s) else s))";
    1.22 +by(rtac (while_aux_tc RS (hd while_aux.simps) RS trans) 1);
    1.23 + by(simp_tac (simpset() addsimps [same_fst_def]) 1);
    1.24 +br refl 1;
    1.25 +qed "while_aux_unfold";
    1.26 +
    1.27 +(*** The recursion equation for while: directly executable! ***)
    1.28 +
    1.29 +Goalw [while_def] "while b c s = (if b s then while b c (c s) else s)";
    1.30 +by(rtac (while_aux_unfold RS trans) 1);
    1.31 +by (Auto_tac);
    1.32 +by(stac while_aux_unfold 1);
    1.33 +by(Asm_full_simp_tac 1);
    1.34 +by(Clarify_tac 1);
    1.35 +by(eres_inst_tac [("x","%i. f(Suc i)")] allE 1);
    1.36 +by(Blast_tac 1);
    1.37 +qed "while_unfold";
    1.38 +
    1.39 +(*** The proof rule for while; P is the invariant ***)
    1.40 +
    1.41 +val [prem1,prem2,prem3] = Goal
    1.42 +"[| !!s. [| P s; b s |] ==> P(c s); \
    1.43 +\   !!s. [| P s; ~b s |] ==> Q s; \
    1.44 +\   wf{(t,s). P s & b s & t = c s} |] \
    1.45 +\ ==> P s --> Q(while b c s)";
    1.46 +by(res_inst_tac [("a","s")] (prem3 RS wf_induct) 1);
    1.47 +by(Asm_full_simp_tac 1);
    1.48 +by(Clarify_tac 1);
    1.49 +by(stac while_unfold 1);
    1.50 +by(asm_full_simp_tac (simpset() addsimps [prem1,prem2]) 1);
    1.51 +qed_spec_mp "while_rule";
    1.52 +
    1.53 +(*** An application: computation of the lfp on finite sets via iteration ***)
    1.54 +
    1.55 +Goal
    1.56 + "[| mono f; finite U; f U = U |] \
    1.57 +\ ==> lfp f = fst(while (%(A,fA). A~=fA) (%(A,fA). (fA, f fA)) ({},f{}))";
    1.58 +by(res_inst_tac [("P","%(A,B).(A <= U & B = f A & A <= B & B <= lfp f)")]
    1.59 +     while_rule 1);
    1.60 +   by(stac lfp_Tarski 1);
    1.61 +    ba 1;
    1.62 +   by(Clarsimp_tac 1);
    1.63 +   by(blast_tac (claset() addDs [monoD]) 1);
    1.64 +  by(fast_tac (claset() addSIs [lfp_lowerbound] addss simpset()) 1);
    1.65 + by(res_inst_tac [("r","((Pow U <*> UNIV) <*> (Pow U <*> UNIV)) Int \
    1.66 + \                      inv_image finite_psubset (op - U o fst)")]
    1.67 +                 wf_subset 1);
    1.68 +  by(blast_tac (claset() addIs
    1.69 +      [wf_finite_psubset,Int_lower2 RSN (2,wf_subset)]) 1);
    1.70 + by(clarsimp_tac (claset(),simpset() addsimps
    1.71 +      [inv_image_def,finite_psubset_def,order_less_le]) 1);
    1.72 + by(blast_tac (claset() addSIs [finite_Diff] addDs [monoD]) 1);
    1.73 +by(stac lfp_Tarski 1);
    1.74 + ba 1;
    1.75 +by(asm_simp_tac (simpset() addsimps [monoD]) 1);
    1.76 +qed "lfp_conv_while";
    1.77 +
    1.78 +(*** An example; requires integers
    1.79 +
    1.80 +Goal "{f n|n. A n | B n} = {f n|n. A n} Un {f n|n. B n}";
    1.81 +by(Blast_tac 1);
    1.82 +qed "lemma";
    1.83 +
    1.84 +Goal "P(lfp (%N::int set. {#0} Un {(n + #2) mod #6 |n. n:N})) = P{#0,#4,#2}";
    1.85 +by(stac (read_instantiate [("U","{#0,#1,#2,#3,#4,#5}")] lfp_conv_while) 1);
    1.86 +   br monoI 1;
    1.87 +   by(Blast_tac 1);
    1.88 +  by(Simp_tac 1);
    1.89 + by(simp_tac (simpset() addsimps [lemma,set_eq_subset]) 1);
    1.90 +(* The fixpoint computation is performed purely by rewriting: *)
    1.91 +by(simp_tac (simpset() addsimps [while_unfold,lemma,set_eq_subset]
    1.92 +     delsimps [subset_empty]) 1);
    1.93 +result();
    1.94 +
    1.95 +***)
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/src/HOL/While.thy	Wed Jul 26 19:43:28 2000 +0200
     2.3 @@ -0,0 +1,28 @@
     2.4 +(*  Title:      HOL/While
     2.5 +    ID:         $Id$
     2.6 +    Author:     Tobias Nipkow
     2.7 +    Copyright   2000 TU Muenchen
     2.8 +
     2.9 +Defines a while-combinator "while" and proves
    2.10 +a) an unrestricted unfolding law (even if while diverges!)
    2.11 +   (I got this idea from Wolfgang Goerigk)
    2.12 +b) the invariant rule for reasoning about while
    2.13 +*)
    2.14 +
    2.15 +While = Recdef +
    2.16 +
    2.17 +consts while_aux :: "('a => bool) * ('a => 'a) * 'a => 'a"
    2.18 +recdef while_aux
    2.19 + "same_fst (%b. True) (%b. same_fst (%c. True) (%c.
    2.20 +  {(t,s).  b s & c s = t &
    2.21 +           ~(? f. f 0 = s & (!i. b(f i) & c(f i) = f(i+1)))}))"
    2.22 +"while_aux(b,c,s) =
    2.23 +  (if (? f. f 0 = s & (!i. b(f i) & c(f i) = f(i+1)))
    2.24 +   then arbitrary
    2.25 +   else if b s then while_aux(b,c,c s) else s)"
    2.26 +
    2.27 +constdefs
    2.28 + while :: "('a => bool) => ('a => 'a) => 'a => 'a"
    2.29 +"while b c s == while_aux(b,c,s)"
    2.30 +
    2.31 +end