| 1 |
%< |
| 2 |
FILE: zet_ind.ml |
| 3 |
|
| 4 |
DESCRIPTION: induction for integers |
| 5 |
|
| 6 |
AUTHOR: Ton Kalker |
| 7 |
|
| 8 |
DATE: 22-09-89 |
| 9 |
>% |
| 10 |
|
| 11 |
let zet_derive_existence_thm k tml = |
| 12 |
(let th = SPEC k zet_Axiom in |
| 13 |
let vars = map(genvar o type_of) (fst(strip_forall(concl th))) in |
| 14 |
let exists = CONJUNCT1 (CONV_RULE EXISTS_UNIQUE_CONV (SPECL vars th)) in |
| 15 |
let e_fn = fst(dest_exists (concl exists)) in |
| 16 |
letrec splt l = |
| 17 |
if (is_var (hd l)) then |
| 18 |
(let bv,C,av = splt (tl l) in ((hd l).bv,C,av)) else |
| 19 |
if (is_const (hd l) or (is_comb (hd l))) then |
| 20 |
[],(hd l),(tl l) else fail in |
| 21 |
let bv,Con,av =splt(snd(strip_comb(lhs(snd(strip_forall(hd tml)))))) in |
| 22 |
let rhsfn = let cv = genvar(type_of Con) in |
| 23 |
let r = rhs(snd(strip_forall(hd tml))) in |
| 24 |
list_mk_abs(cv. (bv @ av),r) in |
| 25 |
let th_inst = INST_TYPE (snd(match e_fn rhsfn)) exists in |
| 26 |
let get_c t = |
| 27 |
let args = snd(strip_comb(lhs(snd(strip_forall t)))) in |
| 28 |
let c = fst(strip_comb(find (\t.is_const t or is_comb t) args)) in |
| 29 |
(if (is_const c) then c else fail) in |
| 30 |
let cs = map get_c tml in |
| 31 |
let exl = CONJUNCTS (SELECT_RULE th_inst) in |
| 32 |
let fn = fst(dest_exists(concl th_inst)) in |
| 33 |
let same_c c cl = |
| 34 |
( |
| 35 |
(c = fst(strip_comb(rand(lhs(snd(dest_imp(snd(strip_forall(concl cl))))))))) ? |
| 36 |
(c = fst(strip_comb(rand(lhs(snd(strip_forall(concl cl))))))) |
| 37 |
) in |
| 38 |
letrec get_ths cs exl = |
| 39 |
if (null cs) then [] else |
| 40 |
(let (c,ex) = remove (same_c(hd cs)) exl in |
| 41 |
(c.(get_ths (tl cs) ex))) in |
| 42 |
let ths = (get_ths cs exl) in |
| 43 |
let argvars = map (genvar o type_of) (bv @ av) in |
| 44 |
let ap_ths th = |
| 45 |
let v = map (genvar o type_of) (fst(strip_forall(concl th))) in |
| 46 |
let th1 = SPECL v th in |
| 47 |
let th2 = (if (is_imp (concl th1)) then |
| 48 |
(let as,_= dest_imp (concl th1) in |
| 49 |
let th3 = UNDISCH th1 in |
| 50 |
let th4 = rev_itlist (C AP_THM) argvars th3 in |
| 51 |
DISCH as th4) |
| 52 |
else (rev_itlist (C AP_THM) argvars th1) |
| 53 |
) in |
| 54 |
(GENL (v @ argvars) th2) in |
| 55 |
let th1 = LIST_CONJ (map ap_ths ths) in |
| 56 |
let sel = mk_select(dest_exists (concl th_inst)) in |
| 57 |
GEN_ALL(EXISTS(mk_exists(fn,subst [fn,sel](concl th1)),sel)th1))? |
| 58 |
failwith `Can't derive existence theorem`;; |
| 59 |
|
| 60 |
let zet_instantiate_existence_thm eth tml = |
| 61 |
(let cls = map (snd o strip_forall) tml in |
| 62 |
letrec splt l = |
| 63 |
if (is_var (hd l)) then |
| 64 |
(let bv,C,av = splt (tl l) in ((hd l).bv,C,av)) else |
| 65 |
if (is_const (hd l) or (is_comb (hd l))) then |
| 66 |
[],(hd l),(tl l) else fail in |
| 67 |
let dest tm = |
| 68 |
let ((Fn,(bv,C,av)),r)=(((I # splt) o strip_comb) # I)(dest_eq tm) in |
| 69 |
(Fn,bv,C,av,r) in |
| 70 |
let destcl = (map dest cls) in |
| 71 |
let prep_tm tm = (if (is_imp (snd(strip_forall tm))) then |
| 72 |
(let vl,body = strip_forall tm in |
| 73 |
list_mk_forall(vl,snd(dest_imp body))) else tm) in |
| 74 |
let ecls = map prep_tm (conjuncts(snd(dest_exists(snd(strip_forall(concl eth)))))) in |
| 75 |
let fns,spec,beta = (I # split) |
| 76 |
(split(map mk_fn (combine(ecls,destcl)))) in |
| 77 |
let ethsp = SPECL fns eth in |
| 78 |
let conjs = map (uncurry SPECL) |
| 79 |
(combine(spec,CONJUNCTS(SELECT_RULE ethsp))) in |
| 80 |
let rule (th1,th2) = ((CONV_RULE (RAND_CONV (REWRITE_CONV th1)) th2) ? |
| 81 |
(CONV_RULE (RAND_CONV(RAND_CONV(REWRITE_CONV th1))) th2)) in |
| 82 |
let th = LIST_CONJ (map (GEN_ALL o rule) (combine(beta,conjs))) in |
| 83 |
let fn = fst(dest_exists (concl ethsp)) and |
| 84 |
fsel = mk_select(dest_exists(concl ethsp)) in |
| 85 |
(EXISTS (mk_exists(fn,subst [fn,fsel] (concl th)),fsel) th))? |
| 86 |
failwith `Can't instantiate existence theorem`;; |
| 87 |
|
| 88 |
let prepare_goal k tml = |
| 89 |
letrec splt l = |
| 90 |
if (is_var (hd l)) then |
| 91 |
(let bv,C,av = splt (tl l) in ((hd l).bv,C,av)) else |
| 92 |
if (is_const (hd l) or (is_comb (hd l))) then |
| 93 |
[],(hd l),(tl l) else fail in |
| 94 |
let term_type tm =fst(snd(splt(snd(strip_comb(lhs(snd(strip_forall tm))))))) in |
| 95 |
let mk_term tm = |
| 96 |
(let strip_term = snd(strip_forall tm) in |
| 97 |
let Con = term_type tm in |
| 98 |
let con = fst(strip_comb Con) in |
| 99 |
let y = hd(frees Con) ? "zero" in |
| 100 |
(if y = "zero" then strip_term |
| 101 |
if con = "$plus" then "(^k leq ^y) ==> ^strip_term" |
| 102 |
if con = "$minus" then "(^y leq ^k) ==> ^strip_term" |
| 103 |
else fail)) in |
| 104 |
let lvars t = subtract |
| 105 |
(freesl(snd(strip_comb(lhs(snd (strip_forall t)))))) |
| 106 |
(fst(strip_forall t)) in |
| 107 |
list_mk_conj(map (\tm.list_mk_forall(lvars tm,mk_term tm)) tml);; |
| 108 |
|
| 109 |
|
| 110 |
let zet_prove_rec_fn_exists k tml = |
| 111 |
(let eth = zet_derive_existence_thm k tml in |
| 112 |
let ith = zet_instantiate_existence_thm eth tml in |
| 113 |
letrec get_avars tm = |
| 114 |
if (is_var (rand tm)) then (get_avars (rator tm)) else |
| 115 |
(snd(strip_comb (rator tm)),rand tm) in |
| 116 |
let cl = snd(strip_forall(hd tml)) in |
| 117 |
let fn = fst(strip_comb(lhs cl)) and |
| 118 |
av,tv = (map (genvar o type_of) # (genvar o type_of)) |
| 119 |
(get_avars (lhs cl)) in |
| 120 |
let prep_tm tm = (if (is_imp (snd(strip_forall tm))) then |
| 121 |
(let vl,body = strip_forall tm in |
| 122 |
list_mk_forall(vl,snd(dest_imp body))) else tm) in |
| 123 |
if is_const fn |
| 124 |
then failwith(fst(dest_const fn)^` previously defined`) |
| 125 |
else |
| 126 |
let goal = ([],mk_exists(fn,prepare_goal k tml)) in |
| 127 |
let etac th = |
| 128 |
let efn = fst(strip_comb(lhs(snd(strip_forall(prep_tm(concl th)))))) in |
| 129 |
EXISTS_TAC (list_mk_abs(av@[tv],list_mk_comb(efn,tv.av))) in |
| 130 |
let fn_beta th (A,gl) = |
| 131 |
let efn = fst(strip_comb(lhs(snd(strip_forall(prep_tm(concl th)))))) in |
| 132 |
let eabs = (list_mk_abs(av@[tv],list_mk_comb(efn,tv.av))) in |
| 133 |
let epat = list_mk_comb(eabs, map (genvar o type_of) (av @ [tv])) in |
| 134 |
let tms = find_terms (\tm. can (match epat) tm) gl in |
| 135 |
PURE_ONCE_REWRITE_TAC (map LIST_BETA_CONV tms) (A,gl) in |
| 136 |
GEN_ALL(TAC_PROOF(goal, |
| 137 |
STRIP_ASSUME_TAC ith THEN FIRST_ASSUM etac THEN |
| 138 |
REPEAT STRIP_TAC THEN FIRST_ASSUM fn_beta THEN |
| 139 |
RES_TAC THEN FIRST_ASSUM MATCH_ACCEPT_TAC)))?\tok |
| 140 |
failwith(`Can't solve recursion equation: `^tok);; |
| 141 |
|
| 142 |
let zet_recursive_definition k infix_flag name tml = |
| 143 |
let thm = zet_prove_rec_fn_exists k tml |
| 144 |
in |
| 145 |
new_specification |
| 146 |
name |
| 147 |
[(infix_flag=>`infix`|`constant`), |
| 148 |
((fst o dest_var o fst o dest_exists o concl) thm)] |
| 149 |
thm;; |
| 150 |
|
| 151 |
let zet_zero_definition = zet_recursive_definition "zero";; |
| 152 |
|
| 153 |
let zet_one_definition = zet_recursive_definition "een";; |
| 154 |
|