| 1 | % FILE : wordn.ml % |
| 2 | % DESCRIPTION : wordn type definition package. % |
| 3 | % % |
| 4 | % READS FILES : wordn.th % |
| 5 | % WRITES FILES : <none> % |
| 6 | % % |
| 7 | % AUTHOR : (c) T. Melham % |
| 8 | % DATE : 87.09.23 % |
| 9 | % % |
| 10 | % NOTE : PRELIMINARY RELEASE. Please do not distribute. % |
| 11 | % (This was cleared with T. Melham for use with the % |
| 12 | % SECD proof example distribution. - BtG) % |
| 13 | % % |
| 14 | % REVISIONS : 09.04.91 - BtG - made compatible with Version 1.12. % |
| 15 | % loadx & HOLdir % |
| 16 | % EXISTS_IMP_FORALL_CONV % |
| 17 | % tok_of_int % |
| 18 | % BOOL_EQ_CONV % |
| 19 | % define_new_type_bijections % |
| 20 | % misc. other fixes... % |
| 21 | |
| 22 | if draft_mode() |
| 23 | then (print_newline(); |
| 24 | print_string`wordn declared a new parent`; |
| 25 | print_newline(); |
| 26 | new_parent`wordn`) |
| 27 | else load_theory`wordn`;; |
| 28 | |
| 29 | load (`bus`, get_flag_value `print_lib`);; |
| 30 | |
| 31 | % Fetch theorems from wordn.th % |
| 32 | let wordn_DEF_THM = theorem `wordn` `wordn_DEF_THM` |
| 33 | and exists_wordn_rep = theorem `wordn` `exists_wordn_rep`;; |
| 34 | |
| 35 | % Fetch theorems from bus. % |
| 36 | let Width_Wire = theorem `bus` `Width_Wire` |
| 37 | and Width_Bus = theorem `bus` `Width_Bus` |
| 38 | and Hd_bus = INST_TYPE [":bool",":*"] (definition `bus` `Hd_bus`) |
| 39 | and Tl_bus = INST_TYPE [":bool",":*"] (definition `bus` `Tl_bus`);; |
| 40 | |
| 41 | |
| 42 | % --------------------------------------------------------------------- % |
| 43 | % procedures for cleaning up the type axiom (wordn_DEF_THM). % |
| 44 | % --------------------------------------------------------------------- % |
| 45 | |
| 46 | % BINDER_CONV conv "B x. tm[x]" applies conv to tm[x] % |
| 47 | let BINDER_CONV conv = (RAND_CONV (ABS_CONV conv));; |
| 48 | |
| 49 | % LHS_CONV conv "OP t1 t2" applies conv to t1 % |
| 50 | let LHS_CONV = RATOR_CONV o RAND_CONV ;; |
| 51 | |
| 52 | % DEPTH_FORALL_CONV : BINDER_CONV in depth % |
| 53 | letrec DEPTH_FORALL_CONV conv tm = |
| 54 | if (is_forall tm) then |
| 55 | BINDER_CONV (DEPTH_FORALL_CONV conv) tm else |
| 56 | conv tm;; |
| 57 | |
| 58 | % --------------------------------------------------------------------- % |
| 59 | % The following code is for ELIM_WID_CONV. I wish this conversion was % |
| 60 | % much faster and simpler, but this will have to do for now... % |
| 61 | % --------------------------------------------------------------------- % |
| 62 | |
| 63 | % A couple of lemmas. % |
| 64 | let Wid_Wire = INST_TYPE [":bool",":*"] Width_Wire and |
| 65 | Wid_Bus = INST_TYPE [":bool",":*"] Width_Bus;; |
| 66 | |
| 67 | % Complicated rule for use in LEN_SIMP_CONV below % |
| 68 | letrec CHOOSE_RULE th1 th2 = |
| 69 | if (not(is_exists (concl th1))) then SUBS [th1] th2 else |
| 70 | let (v,body) = dest_exists (concl th1) in |
| 71 | let var = genvar (type_of v) in |
| 72 | let tm = subst [var,v] body in |
| 73 | let thm1 = CHOOSE_RULE (ASSUME tm) th2 in |
| 74 | let thm2 = EXISTS(mk_exists(var,concl thm1),var) thm1 in |
| 75 | CHOOSE (var,th1)thm2;; |
| 76 | |
| 77 | % Also complicated.... % |
| 78 | letrec EXISTS_RULE evars th = |
| 79 | if (null evars) then th else |
| 80 | let ev = hd(evars) in |
| 81 | let evar = genvar (type_of ev) in |
| 82 | let epat = mk_exists(evar,subst_occs [[2]] [evar,ev] (concl th)) in |
| 83 | let th' = EXISTS (epat,ev) th in |
| 84 | EXISTS_RULE (tl evars) th';; |
| 85 | |
| 86 | % Complicated... used in LEN_SIMP_CONV below... % |
| 87 | letrec DO_ASM th asm = |
| 88 | if (not(is_exists (concl asm))) then |
| 89 | let wit = (lhs(snd(strip_exists (concl th)))) in |
| 90 | let var = genvar (type_of wit) in |
| 91 | let subs = |
| 92 | if (is_const wit) then |
| 93 | subst_occs [[1;3]] [var,wit] else |
| 94 | subst [var,wit] in |
| 95 | let epat = |
| 96 | mk_exists (var,subs (mk_conj(concl th,concl asm))) in |
| 97 | EXISTS (epat,wit) (CONJ th asm) else |
| 98 | let (v,body) = dest_exists (concl asm) in |
| 99 | let thm1 = DO_ASM th (ASSUME body) in CHOOSE (v,asm)thm1;; |
| 100 | |
| 101 | % WID_SIMP_CONV : "?l. (?a1..an. l = tm[a1...an]) /\ tm2[l]" % |
| 102 | % % |
| 103 | % ---> "?a1...an. tm2[tm[a1...an]]" % |
| 104 | let WID_SIMP_CONV tm = |
| 105 | let th1,th2 = CONJ_PAIR (ASSUME (snd(dest_exists tm))) in |
| 106 | let th3 = CHOOSE_RULE th1 th2 in |
| 107 | let imp1 = DISCH tm (CHOOSE ((fst(dest_exists tm)),(ASSUME tm)) th3) in |
| 108 | let res = snd(dest_imp(concl imp1)) in |
| 109 | let asm = ASSUME res in |
| 110 | let th4 = REFL (rand(rhs(snd(strip_exists res)))) in |
| 111 | let evars = rev(fst(strip_exists res)) in |
| 112 | let th5 = EXISTS_RULE evars th4 in |
| 113 | let imp2 = DISCH res (DO_ASM th5 asm) in |
| 114 | IMP_ANTISYM_RULE imp1 imp2;; |
| 115 | |
| 116 | % ELIM_WID_CONV : Length l = SUC(SUC(SUC 0)) --> ?a b c. l = [a;b;c] % |
| 117 | letrec ELIM_WID_CONV tm = |
| 118 | let bus = rand(lhs tm) and wid = rhs tm in |
| 119 | if (wid = "SUC 0") then TRANS (REFL tm) (SPEC bus (Wid_Wire)) else |
| 120 | let thm1 = SPECL [bus;(rand (rand wid))] Wid_Bus in |
| 121 | let conv1 = |
| 122 | (RAND_CONV(BINDER_CONV(BINDER_CONV(LHS_CONV ELIM_WID_CONV)))) in |
| 123 | let conv2 = (RAND_CONV(BINDER_CONV WID_SIMP_CONV)) in |
| 124 | CONV_RULE (conv1 THENC conv2) thm1;; |
| 125 | |
| 126 | % Eliminate all "Width bus = SUC(SUC 0)" terms in favour of % |
| 127 | % ?a b.bus = Bus a (Wire b).. etc % |
| 128 | let RM_WID_CONV = |
| 129 | BINDER_CONV(BINDER_CONV(BINDER_CONV(LHS_CONV ELIM_WID_CONV)));; |
| 130 | |
| 131 | % iterated version of EXISTS_IMP_FORALL_CONV % |
| 132 | letrec E_I_CONV tm = |
| 133 | if (is_exists(fst(dest_imp tm))) then |
| 134 | (LEFT_IMP_EXISTS_CONV THENC (BINDER_CONV E_I_CONV)) tm else |
| 135 | REFL tm;; |
| 136 | |
| 137 | % !x b c. x = tm ==> t[x,a,b,c] = t[tm,a,b,c] % |
| 138 | let EQ_ANTE_ELIM tm = |
| 139 | let vars,body = strip_forall tm in |
| 140 | let x,t = dest_eq(fst(dest_imp body)) in |
| 141 | let specl = map (\tm. if (tm=x) then t else tm) vars in |
| 142 | let genl = filter (\tm. not(tm = x)) vars in |
| 143 | let imp1 = DISCH tm (GENL genl (MP (SPECL specl (ASSUME tm)) (REFL t))) in |
| 144 | let asm1 = (SPECL genl (ASSUME (snd(dest_imp(concl imp1))))) in |
| 145 | let th = (DISCH "^x = ^t" (SUBS [SYM(ASSUME "^x = ^t")] asm1)) in |
| 146 | let imp2 = DISCH_ALL (GENL vars th) in |
| 147 | IMP_ANTISYM_RULE imp1 imp2;; |
| 148 | |
| 149 | % Extract components of a bus % |
| 150 | letrec extract_bus var tm = |
| 151 | if (fst(dest_comb tm) = "Wire:bool->(bool)bus") then |
| 152 | ["Hd_bus ^var:bool"] else |
| 153 | let Bus,[b;bus] = strip_comb tm in |
| 154 | "Hd_bus ^var:bool". extract_bus "Tl_bus ^var:(bool)bus" bus;; |
| 155 | |
| 156 | % Make the function for specialization. % |
| 157 | let spec_fn n thm = |
| 158 | let (f,body) = (I # (snd o dest_abs o rand)) (dest_forall(concl thm)) in |
| 159 | let bus = rand(rhs(snd(strip_forall body))) and |
| 160 | var = genvar ":(bool)bus" in |
| 161 | let argsl = extract_bus var bus in |
| 162 | letrec mk_f_ty n = if (n=0) then ":*" else |
| 163 | mk_type(`fun`,[":bool";mk_f_ty (n-1)]) in |
| 164 | let name = mk_primed_var(`f`,mk_f_ty n) in |
| 165 | let fspec = mk_abs(var,list_mk_comb (name,argsl)) in |
| 166 | let thm1 = SPEC fspec thm in |
| 167 | CONV_RULE (BINDER_CONV(DEPTH_FORALL_CONV(RAND_CONV BETA_CONV))) thm1;; |
| 168 | |
| 169 | % HD_TL_SIMP = rewrite with Hd and Tl, % |
| 170 | % only works on Hd(Tl(Tl(Tl ... (Cons a (Cons ...)) % |
| 171 | letrec HD_TL_SIMP tm = |
| 172 | let op1,op2,val = (I # dest_comb) (dest_comb tm) in |
| 173 | let on1 = fst(dest_const op1) and |
| 174 | on2 = if (is_const op2) then |
| 175 | fst(dest_const op2) else |
| 176 | fst(dest_const (rator op2)) in |
| 177 | (if (on1 = `Hd_bus` & on2 = `Bus`) then |
| 178 | REWR_CONV (CONJUNCT2 Hd_bus) tm else |
| 179 | if (on1 = `Hd_bus` & on2 = `Wire`) then |
| 180 | REWR_CONV (CONJUNCT1 Hd_bus) tm else |
| 181 | if (on1 = `Tl_bus` & on2 = `Bus`) then |
| 182 | REWR_CONV Tl_bus tm else |
| 183 | if (on1 = `Hd_bus` or on1 = `Tl_bus`) then |
| 184 | ((RAND_CONV HD_TL_SIMP) THENC HD_TL_SIMP) tm else fail);; |
| 185 | |
| 186 | % Simplify a rhs of the form f t1 t2 t3.. % |
| 187 | letrec SIMP_RHS tm = |
| 188 | if (is_var tm) then REFL tm else |
| 189 | MK_COMB(SIMP_RHS (rator tm), HD_TL_SIMP (rand tm));; |
| 190 | |
| 191 | let sub_conv conv tm = |
| 192 | let rator,bv,body = (I # dest_abs) (dest_comb tm) in |
| 193 | MK_COMB(REFL rator,MK_ABS(GEN bv (conv body))) ;; |
| 194 | |
| 195 | letrec list_gen_alpha l = |
| 196 | if (null l) then ALL_CONV else |
| 197 | GEN_ALPHA_CONV (hd l) THENC (sub_conv (list_gen_alpha (tl l)));; |
| 198 | |
| 199 | let cleanup tm = |
| 200 | let vars,body = (strip_forall tm) in |
| 201 | letrec mk_new_vars n l = |
| 202 | if (null l) then [] else |
| 203 | (mk_primed_var(`b`^(string_of_int n), ":bool"). |
| 204 | mk_new_vars (n+1) (tl l)) in |
| 205 | let newvars = mk_new_vars 0 vars in |
| 206 | list_gen_alpha newvars tm;; |
| 207 | |
| 208 | % --------------------------------------------------------------------- % |
| 209 | % Now, the main program % |
| 210 | % --------------------------------------------------------------------- % |
| 211 | |
| 212 | % define_wordn_type: construct and axiomatize a new type of n-bit words % |
| 213 | % for given n. % |
| 214 | % % |
| 215 | % E.g. define_wordn_type 3 --> defines: % |
| 216 | % % |
| 217 | % 1) a type :word3 % |
| 218 | % 2) a constant Word3:(bool)bus->word3 % |
| 219 | % % |
| 220 | % and proves that word3 has the following axiom: % |
| 221 | % % |
| 222 | % |- !f. ?!fn. !b0 b1 b2. fn(Word3 [b0;b1;b2] = f b0 b1 b2 % |
| 223 | % % |
| 224 | % the axiom is stored under the name `Word3` and is returned. % |
| 225 | % % |
| 226 | % For convenience, some extra theorems are also returned. These follow % |
| 227 | % from the axiom, but for speed are here proved from the type definition% |
| 228 | |
| 229 | let define_wordn_type n = |
| 230 | if n<1 then fail else |
| 231 | letrec mk_n_tm n = if (n=0) then "0" else "SUC ^(mk_n_tm (n-1))" in |
| 232 | let e_th = SPEC (mk_n_tm (n-1)) exists_wordn_rep in |
| 233 | let name = `word` ^ (string_of_int n) in |
| 234 | let ty_ax = (new_type_definition |
| 235 | (name, rator(snd(dest_exists(concl e_th))), e_th)) in |
| 236 | let ty_ISO_DEF = define_new_type_bijections |
| 237 | (name^`_ISO_DEF`) (`ABS_`^name) (`REP_`^name) ty_ax in |
| 238 | let [A_R; R_A] = CONJUNCTS ty_ISO_DEF in |
| 239 | let [R_11; R_ONTO; A_11; A_ONTO] = |
| 240 | map (\f. f ty_ISO_DEF) |
| 241 | [prove_rep_fn_one_one; prove_rep_fn_onto; |
| 242 | prove_abs_fn_one_one; prove_abs_fn_onto] in |
| 243 | let th_inst = INST_TYPE [":*",":**";mk_type (name,[]), ":*"] |
| 244 | wordn_DEF_THM in |
| 245 | let A,R = (I # rator) (dest_comb(lhs(snd(dest_forall(concl A_R))))) in |
| 246 | let th_concl = LIST_MP [A_ONTO;R_A] (SPECL [mk_n_tm (n-1);A;R] th_inst) in |
| 247 | let thm1 = CONV_RULE RM_WID_CONV th_concl in |
| 248 | let thm2 = CONV_RULE |
| 249 | (BINDER_CONV (BINDER_CONV (BINDER_CONV E_I_CONV))) thm1 in |
| 250 | let thm3 = CONV_RULE (BINDER_CONV(BINDER_CONV EQ_ANTE_ELIM)) thm2 in |
| 251 | let wordn_name = `Word` ^ (string_of_int n) in |
| 252 | let wordn_def = new_definition |
| 253 | (wordn_name ^ `_DEF`, |
| 254 | "^(mk_primed_var(wordn_name,type_of A)) = ^A") in |
| 255 | let thm4 = spec_fn n (SUBS [SYM wordn_def] thm3) in |
| 256 | let thm5 = CONV_RULE |
| 257 | (BINDER_CONV (DEPTH_FORALL_CONV(RAND_CONV SIMP_RHS))) thm4 in |
| 258 | let thm6 = CONV_RULE (BINDER_CONV cleanup) thm5 in |
| 259 | let ax = save_thm(`Word`^(string_of_int n),GEN_ALL thm6) in |
| 260 | let wvar = mk_primed_var (`w`, mk_type(name,[])) in |
| 261 | let busvar = mk_primed_var (`bus`, ":(bool)bus") in |
| 262 | let bitsn_name = `Bits` ^ (string_of_int n) in |
| 263 | let bitsn_def = new_definition |
| 264 | (bitsn_name ^ `_DEF`, |
| 265 | "^(mk_primed_var(bitsn_name,type_of R)) = ^R") in |
| 266 | let wdef,bdef = SYM wordn_def, SYM bitsn_def in |
| 267 | let wordn_bitsn = CONV_RULE (GEN_ALPHA_CONV wvar)(SUBS [wdef;bdef] A_R) in |
| 268 | let ntm = mk_const(string_of_int n, ":num") in |
| 269 | letrec conv tm = |
| 270 | if (tm = "0") then REFL tm else |
| 271 | (num_CONV THENC RAND_CONV conv) tm in |
| 272 | let ntm_thm = SYM(conv ntm) in |
| 273 | let th1 = CONV_RULE(BINDER_CONV(LHS_CONV BETA_CONV)) R_A in |
| 274 | let len_bitsn_wordn = CONV_RULE (GEN_ALPHA_CONV busvar) |
| 275 | (SUBS [wdef;bdef;ntm_thm] th1) in |
| 276 | let sptm = (rand(lhs(concl(SPEC_ALL wordn_bitsn)))) in |
| 277 | let spthm = SPEC sptm len_bitsn_wordn in |
| 278 | let subs_th = SUBS [EQT_INTRO (REFL sptm)] |
| 279 | (SUBS [SPEC_ALL wordn_bitsn] spthm) in |
| 280 | let len_bitsn = GEN_ALL(EQT_ELIM subs_th) in |
| 281 | let th2 = GEN_ALL(CONV_RULE (GEN_ALPHA_CONV wvar) (SPEC wvar R_11)) in |
| 282 | let bits_11 = SUBS [bdef] th2 in |
| 283 | let th3 = CONV_RULE(BINDER_CONV(LHS_CONV BETA_CONV)) R_ONTO in |
| 284 | let th4 = CONV_RULE (GEN_ALPHA_CONV busvar) (SUBS [bdef;ntm_thm] th3) in |
| 285 | let len_bits_onto = |
| 286 | CONV_RULE (BINDER_CONV(RAND_CONV(GEN_ALPHA_CONV wvar))) th4 in |
| 287 | let ax_body = |
| 288 | snd(strip_forall(snd(dest_abs(rand(snd(dest_forall(concl ax))))))) in |
| 289 | let blist = rand(rand(lhs ax_body)) in |
| 290 | let len_thm = (SUBS [ntm_thm] (Width_CONV "Width ^blist")) in |
| 291 | let bits_onto = GEN_ALL(EQ_MP (SPEC blist len_bits_onto) len_thm) in |
| 292 | let bitsn_wordn = GEN_ALL(EQ_MP (SPEC blist len_bitsn_wordn) len_thm) in |
| 293 | let A_11_beta = CONV_RULE (ONCE_DEPTH_CONV BETA_CONV) A_11 in |
| 294 | letrec prime_blist tm = |
| 295 | if (is_const(rator tm)) then |
| 296 | "Wire ^((mk_var o ((\t.t ^ `'`) # I) o dest_var) (rand tm))" else |
| 297 | let c,[h;t] = strip_comb tm in |
| 298 | "^c ^((mk_var o ((\t.t ^ `'`) # I) o dest_var) h) ^(prime_blist t)" in |
| 299 | let blist' = prime_blist blist in |
| 300 | let blist'_len = (SUBS [ntm_thm] (Width_CONV "Width ^blist'")) in |
| 301 | let A_11_SPEC = SUBS [ntm_thm;wdef] (SPECL [blist;blist'] A_11_beta) in |
| 302 | let w_11 = GEN_ALL(LIST_MP [len_thm;blist'_len] A_11_SPEC) in |
| 303 | [ax;wordn_bitsn;bitsn_wordn;len_bitsn_wordn;len_bitsn; |
| 304 | bits_11;bits_onto;len_bits_onto;w_11];; |
| 305 | |
| 306 | % --------------------------------------------------------------------- % |
| 307 | % AUXILLIARY PROGRAMS % |
| 308 | % --------------------------------------------------------------------- % |
| 309 | |
| 310 | % BINDER_CONV conv "B x. tm[x]" applies conv to tm[x] % |
| 311 | let BINDER_CONV conv = (RAND_CONV (ABS_CONV conv));; |
| 312 | |
| 313 | % DEPTH_FORALL_CONV : BINDER_CONV in depth % |
| 314 | letrec DEPTH_FORALL_CONV conv tm = |
| 315 | if (is_forall tm) then |
| 316 | BINDER_CONV (DEPTH_FORALL_CONV conv) tm else |
| 317 | conv tm;; |
| 318 | |
| 319 | letrec DEPTH_EXISTS_CONV conv tm = |
| 320 | if (is_exists tm) then |
| 321 | BINDER_CONV (DEPTH_EXISTS_CONV conv) tm else |
| 322 | conv tm;; |
| 323 | |
| 324 | % LHS_CONV conv "OP t1 t2" applies conv to t1 % |
| 325 | let LHS_CONV = RATOR_CONV o RAND_CONV ;; |
| 326 | |
| 327 | % --------------------------------------------------------------------- % |
| 328 | % procedure for proving induction theorem from wordn type axiom % |
| 329 | % --------------------------------------------------------------------- % |
| 330 | |
| 331 | % BINOP_CONV conv "OP t1 t2" applies conv to t1 and t2 % |
| 332 | let BINOP_CONV conv = (RAND_CONV conv) THENC (LHS_CONV conv);; |
| 333 | |
| 334 | % A few little lemmas % |
| 335 | let lemma1 = GEN_ALL(CONJUNCT1 (SPEC_ALL AND_CLAUSES)) and |
| 336 | lemma2 = GEN_ALL(CONJUNCT1(CONJUNCT2 (SPEC_ALL EQ_CLAUSES))) and |
| 337 | lemma3 = GEN_ALL(CONJUNCT1 (SPEC_ALL EQ_CLAUSES));; |
| 338 | |
| 339 | % prove_wordn_induction_thm: from a wordn type axiom, prove induction % |
| 340 | % thm % |
| 341 | % % |
| 342 | % E.g. Input: % |
| 343 | % % |
| 344 | % |- !f. ?! fn. !b0 b1 b2 b3. fn(Word4 b0 b1 b2 b3 ) = f b0 b1 b2 b3 % |
| 345 | % % |
| 346 | % Output: % |
| 347 | % % |
| 348 | % |- !P. (!b0 b1 b2 b3. P(Word4 b0 b1 b2 b3 )) ==> (!x. P x) % |
| 349 | |
| 350 | let prove_wordn_induction_thm th = |
| 351 | let fn = fst(dest_abs(rand(concl(SPEC_ALL th)))) in |
| 352 | let [ty;rty] = snd(dest_type (type_of fn)) in |
| 353 | let th1 = CONV_RULE (DEPTH_FORALL_CONV(EXISTS_UNIQUE_CONV)) |
| 354 | (INST_TYPE [":bool",rty] th) in |
| 355 | let th2 = GEN_ALL(CONJUNCT2 (SPEC_ALL th1)) in |
| 356 | let tyvar = genvar ty and |
| 357 | vars = |
| 358 | fst(strip_forall(snd(dest_abs(rand(snd(dest_forall(concl th))))))) in |
| 359 | let fns = [list_mk_abs (vars,"T");"\^tyvar.T";"P:^ty->bool"] in |
| 360 | let th3 = SPECL fns th2 in |
| 361 | let BETA = DEPTH_FORALL_CONV(BINOP_CONV LIST_BETA_CONV) and |
| 362 | ETA_REFL = EVERY_CONV [REPEATC (REWR_CONV FORALL_SIMP); |
| 363 | REWR_CONV REFL_CLAUSE] in |
| 364 | let conv1 = EVERY_CONV |
| 365 | [(LHS_CONV o LHS_CONV o EVERY_CONV) [BETA; ETA_REFL]; |
| 366 | LHS_CONV (REWR_CONV lemma1); |
| 367 | LHS_CONV ((DEPTH_FORALL_CONV o EVERY_CONV) |
| 368 | [RAND_CONV LIST_BETA_CONV; |
| 369 | REWR_CONV lemma2])] in |
| 370 | let conv2 = (RAND_CONV o EVERY_CONV) |
| 371 | [FUN_EQ_CONV;BINDER_CONV(LHS_CONV BETA_CONV); |
| 372 | BINDER_CONV(REWR_CONV lemma3)] in |
| 373 | GEN_ALL(CONV_RULE (conv1 THENC conv2) th3);; |
| 374 | |
| 375 | % --------------------------------------------------------------------- % |
| 376 | % cases for wordn types. % |
| 377 | % --------------------------------------------------------------------- % |
| 378 | |
| 379 | let lemma1 = CONJUNCT1 NOT_CLAUSES;; |
| 380 | |
| 381 | % prove_wordn_cases_thm : prove a cases theorem for a wordn type % |
| 382 | % % |
| 383 | % E.g. Input: % |
| 384 | % % |
| 385 | % |- !P. (!b0 b1 b2 b3. P(Word4 b0 b1 b2 b3 )) ==> (!x. P x) % |
| 386 | % % |
| 387 | % Output: % |
| 388 | % % |
| 389 | % |- !x. ?b0 b1 b2 b3. x = Word4 b0 b1 b2 b3 % |
| 390 | |
| 391 | let prove_wordn_cases_thm th = |
| 392 | (let x,P = dest_forall(snd(dest_imp(snd(dest_forall(concl th))))) in |
| 393 | let xvar = genvar (type_of x) in |
| 394 | let th1 = SPEC x (UNDISCH(SPEC "\^xvar.~(^x = ^xvar)" th)) in |
| 395 | let th2 = NOT_INTRO(DISCH_ALL (MP (CONV_RULE BETA_CONV th1) (REFL x))) in |
| 396 | letrec conv1 tm = |
| 397 | if (is_forall(dest_neg tm)) then |
| 398 | (NOT_FORALL_CONV THENC BINDER_CONV conv1) tm else |
| 399 | ((RAND_CONV BETA_CONV) THENC (REWR_CONV lemma1)) tm in |
| 400 | GEN_ALL(CONV_RULE conv1 th2)) ? failwith `Can't prove cases thm`;; |
| 401 | |
| 402 | % Define a wordn type and prove all the standard theorems. % |
| 403 | let declare_wordn_type flag n = |
| 404 | let wn = `Word` ^ string_of_int n in |
| 405 | let bn = `Bits` ^ string_of_int n in |
| 406 | let [ax;wn_bn;bn_wn;wid_bn_wn;wid_bn;b_11;b_onto;wid_b_onto;w_11] = |
| 407 | define_wordn_type n in |
| 408 | let ind = prove_wordn_induction_thm ax in |
| 409 | let cases = prove_wordn_cases_thm ind in |
| 410 | if (flag) then |
| 411 | (save_thm(wn ^ `_` ^ bn,wn_bn); |
| 412 | save_thm(bn ^ `_` ^ wn,bn_wn); |
| 413 | save_thm(`Width_` ^ bn ^ `_` ^ wn,wid_bn_wn); |
| 414 | save_thm(`Width_` ^ bn, wid_bn); |
| 415 | save_thm(bn ^ `_11`,b_11); |
| 416 | save_thm(bn ^ `_Onto`,b_onto); |
| 417 | save_thm(`Width_` ^ bn ^ `_Onto`,wid_b_onto); |
| 418 | save_thm(wn ^ `_Induct`,ind); |
| 419 | save_thm(wn ^ `_Onto`,cases); |
| 420 | save_thm(wn ^ `_11`,w_11);()); |
| 421 | [ax;wn_bn;bn_wn;wid_bn_wn;wid_bn;b_11;b_onto;wid_b_onto;ind;cases;w_11];; |
| 422 | |
| 423 | % ------------------------------------------------------------------ % |
| 424 | % Other tools... % |
| 425 | % ------------------------------------------------------------------ % |
| 426 | |
| 427 | % Make an OL bus. % |
| 428 | % eg. mk_ol_bus ":(*)list" ["T";"F";"F";"F"];; % |
| 429 | % "Bus T(Bus F(Bus F(Wire F)))" : term % |
| 430 | |
| 431 | letrec mk_ol_bus ty l = |
| 432 | if (null l) then fail else |
| 433 | if (null (tl l)) then "Wire ^(hd l)" else |
| 434 | "Bus ^(hd l) ^(mk_ol_bus ty (tl l))";; |
| 435 | |
| 436 | %< Old version; replaced below. |
| 437 | % wordn_CONV : "#1010" ---> |- #1010 = Word4 [F;T;F;T] % |
| 438 | let wordn_CONV tm = |
| 439 | (let str,ty = dest_const tm in |
| 440 | let size = (hd(rev(explode(fst(dest_type ty))))) in |
| 441 | if (not (ty = (mk_type(`word` ^ size,[])))) then fail else |
| 442 | let hash.bits = explode str in |
| 443 | if (not (hash = `#`)) then fail else |
| 444 | let const = mk_const(`Word`^size,":(bool)bus -> ^ty") and |
| 445 | args = rev(map(\x.if (x=`1`) then "T" else "F") bits) in |
| 446 | mk_thm([], "^tm = ^const ^(mk_ol_bus ":bool" args)"))? |
| 447 | failwith `wordn_CONV`;; |
| 448 | >% |
| 449 | |
| 450 | % Modified 89.06.26 - BtG : % |
| 451 | % * Works for :wordn where n>9 % |
| 452 | % * uses same bit ordering for both buses and wordn's: % |
| 453 | % wordn_CONV : "#1010" ---> |- #1010 = Word4 [T;F;T;F] % |
| 454 | let wordn_CONV tm = |
| 455 | (let str,ty = dest_const tm in |
| 456 | let size = (implode ((tl o tl o tl o tl)(explode(fst(dest_type ty))))) in |
| 457 | if (not (ty = (mk_type(`word` ^ size,[])))) then fail else |
| 458 | let hash.bits = explode str in |
| 459 | if (not (hash = `#`)) then fail else |
| 460 | let const = mk_const(`Word`^size,":(bool)bus -> ^ty") and |
| 461 | args = map(\x.if (x=`1`) then "T" else "F") bits in |
| 462 | mk_thm([], "^tm = ^const ^(mk_ol_bus ":bool" args)"))? |
| 463 | failwith `wordn_CONV`;; |
| 464 | |
| 465 | % wordn_EQ_CONV th "#aaa = #bbb" --> is #aaa = #bbb ? % |
| 466 | let wordn_EQ_CONV th tm = |
| 467 | let conv1 = EVERY_CONV [RAND_CONV wordn_CONV; |
| 468 | RATOR_CONV (RAND_CONV wordn_CONV)] in |
| 469 | (conv1 THENC REWR_CONV th THENC BUS_EQ_CONV bool_EQ_CONV) tm;; |
| 470 | |
| 471 | % --------------------------------------------------------------------- % |
| 472 | % induction for wordn types: same as for recursive types. % |
| 473 | % --------------------------------------------------------------------- % |
| 474 | |
| 475 | % BINDER_CONV conv "B x. tm[x]" applies conv to tm[x] % |
| 476 | let BINDER_CONV conv = (RAND_CONV (ABS_CONV conv));; |
| 477 | |
| 478 | % DEPTH_FORALL_CONV : BINDER_CONV in depth % |
| 479 | letrec DEPTH_FORALL_CONV conv tm = |
| 480 | if (is_forall tm) then |
| 481 | BINDER_CONV (DEPTH_FORALL_CONV conv) tm else |
| 482 | conv tm;; |
| 483 | |
| 484 | % INDUCT_THEN th th_tac goal : general induction tactic for rec types % |
| 485 | % % |
| 486 | % Induct using induction thm th. Use th_tac on induction hyps. % |
| 487 | |
| 488 | % Why is this redefined from the built-in version? Is this a % |
| 489 | % left-over from the implementation of this before hol88 was ready? % |
| 490 | % Assuming this is the case, I eliminate it for now... % |
| 491 | % 89.06.26 - BtG % |
| 492 | %< |
| 493 | let INDUCT_THEN th th_tac (A,t) = |
| 494 | (let x,body = dest_forall t in |
| 495 | let tyi = snd(match (fst(dest_forall (concl th))) "\^x.T") in |
| 496 | let spec = SPEC (mk_abs(x,body)) (INST_TYPE tyi th) in |
| 497 | let thm = CONV_RULE(RAND_CONV(BINDER_CONV BETA_CONV)) spec in |
| 498 | let thm' = DISCH_ALL (CONV_RULE (GEN_ALPHA_CONV x) (UNDISCH_ALL thm)) in |
| 499 | let tac = |
| 500 | (MATCH_MP_TAC thm' THEN |
| 501 | REPEAT CONJ_TAC THEN |
| 502 | FIRST [CONV_TAC (DEPTH_FORALL_CONV BETA_CONV); |
| 503 | REPEAT GEN_TAC THEN |
| 504 | DISCH_THEN (\th. |
| 505 | CONV_TAC (DEPTH_FORALL_CONV BETA_CONV) THEN |
| 506 | MAP_EVERY (th_tac o (CONV_RULE BETA_CONV)) (CONJUNCTS th))]) in |
| 507 | (tac (A,t))) ? failwith `INDUCT_THEN`;; |
| 508 | >% |
| 509 | |
| 510 | % --------------------------------------------------------------------- % |
| 511 | % Tools for proving exhustive cases theorems for wordn % |
| 512 | % --------------------------------------------------------------------- % |
| 513 | let DISJ_ASSOC = |
| 514 | TAC_PROOF(([], "((A \/ B) \/ C) = (A \/ B \/ C)"), |
| 515 | BOOL_CASES_TAC "A:bool" THEN REWRITE_TAC[]);; |
| 516 | |
| 517 | % --------------------------------------------------------------------- % |
| 518 | % Modified 89.06.26 - BtG : % |
| 519 | % Now works for the ordering of bits built into wordn_CONV % |
| 520 | % --------------------------------------------------------------------- % |
| 521 | let prove_wordn_const_cases ind = |
| 522 | let n = |
| 523 | let ty = hd(snd(dest_type(type_of(fst(dest_forall(concl ind)))))) in |
| 524 | int_of_string(hd(rev(explode(fst(dest_type ty))))) in |
| 525 | let ntok = string_of_int n in |
| 526 | letrec gen_bus i = |
| 527 | let v = mk_var(`b` ^ (string_of_int i),":bool") in |
| 528 | if (i=1) then "Wire ^v" else |
| 529 | "Bus ^v ^(gen_bus (i-1))" in |
| 530 | letrec gen_vars i = |
| 531 | if (i=0) then [] else |
| 532 | let v = mk_var(`b` ^ (string_of_int i),":bool") in |
| 533 | v .(gen_vars (i-1)) in |
| 534 | let vars = gen_vars n in |
| 535 | let wdty = mk_type(`fun`,[":(bool)bus";mk_type(`word`^ntok,[])]) in |
| 536 | let wd = mk_const(`Word` ^ ntok, wdty) in |
| 537 | let bus = gen_bus n in |
| 538 | let refl = REFL "^wd ^bus" in |
| 539 | let conv b th tm = |
| 540 | let l,r = dest_eq tm in |
| 541 | let bv = genvar ":bool" in |
| 542 | let pat = "^l = ^(subst [bv,b] r)" in |
| 543 | SUBST_CONV [th,bv] pat tm in |
| 544 | let do_case bit th = |
| 545 | let cases_th = SPEC bit BOOL_CASES_AX in |
| 546 | let T_case = ASSUME(fst(dest_disj(concl cases_th))) in |
| 547 | let F_case = ASSUME(snd(dest_disj(concl cases_th))) in |
| 548 | let T_thm = CONV_RULE (ONCE_DEPTH_CONV (conv bit T_case)) th in |
| 549 | let F_thm = CONV_RULE (ONCE_DEPTH_CONV (conv bit F_case)) th in |
| 550 | let T_or_thm = DISJ1 T_thm (concl F_thm) and |
| 551 | F_or_thm = DISJ2 (concl T_thm) F_thm in |
| 552 | (DISJ_CASES cases_th T_or_thm F_or_thm) in |
| 553 | letrec do_cases bits th = |
| 554 | if (null bits) then th else |
| 555 | do_case (hd bits) (do_cases (tl bits) th) in |
| 556 | let cases_thm = do_cases vars refl in |
| 557 | let wconv tm = |
| 558 | let l,(W,bits) = (I # dest_comb) (dest_eq tm) in |
| 559 | letrec mk_bits b = |
| 560 | if (is_const (rator b)) then |
| 561 | [if(rand b) = "T" then `1` else `0`] else |
| 562 | let C,[h;t] = strip_comb b in |
| 563 | (if h = "T" then `1` else `0`).(mk_bits t) in |
| 564 | let const = mk_const(implode (`#` . (mk_bits bits)), |
| 565 | mk_type(`word`^ntok,[])) in |
| 566 | let eqn = SYM(wordn_CONV const) in |
| 567 | RAND_CONV (REWR_CONV eqn) tm in |
| 568 | let const_thm = CONV_RULE(ONCE_DEPTH_CONV wconv) cases_thm in |
| 569 | let var = genvar(mk_type(`word`^ntok,[])) in |
| 570 | let P = mk_abs(var,subst [var,"^wd ^bus"] (concl const_thm)) in |
| 571 | let ind_thm = CONV_RULE(ONCE_DEPTH_CONV BETA_CONV) (SPEC P ind) in |
| 572 | MP ind_thm (GENL vars const_thm);; |
| 573 | |
| 574 | |
| 575 | % --------------------------------------------------------------------- % |
| 576 | % Function definitions on wordn % |
| 577 | % --------------------------------------------------------------------- % |
| 578 | |
| 579 | % derive_existence_thm th tm % |
| 580 | % % |
| 581 | % If th is a wordn axiom and tm is a term giving a definition, % |
| 582 | % derive an existence theorem for doing the definition. % |
| 583 | % The existence theorem is suitably type-instantiated. % |
| 584 | % % |
| 585 | % E.g. Input % |
| 586 | % % |
| 587 | % |- !f. ?! fn. !b0 b1 b2 b3. fn(Word4 b0 b1 b2 b3 ) = f b0 b1 b2 b3 % |
| 588 | % % |
| 589 | % "FN x(Word4 a b c d)y = (x + y = 7) /\ a /\ b" % |
| 590 | % % |
| 591 | % Output: % |
| 592 | % % |
| 593 | % |- !g1. ?fn. !g2 g3 g4 g5 g6 g7. % |
| 594 | % fn (Word4 g2 g3 g4 g5) g6 g7 = g1 g2 g3 g4 g5 g6 g7 % |
| 595 | % % |
| 596 | % Note: the g's are genvars % |
| 597 | |
| 598 | let derive_wordn_existence_thm th tm = |
| 599 | (let var = (genvar o type_of) (fst(dest_forall(concl th))) in |
| 600 | let exists = CONJUNCT1 (CONV_RULE EXISTS_UNIQUE_CONV (SPEC var th)) in |
| 601 | let e_fn = fst(dest_exists (concl exists)) in |
| 602 | letrec splt l = |
| 603 | if (is_var (hd l)) then |
| 604 | (let bv,C,av = splt (tl l) in ((hd l).bv,C,av)) else |
| 605 | if (is_const (hd l) or (is_comb (hd l))) then |
| 606 | [],(hd l),(tl l) else fail in |
| 607 | let bv,Con,av =splt(snd(strip_comb(lhs(snd(strip_forall tm))))) in |
| 608 | let rhsfn = let cv = genvar(type_of Con) in |
| 609 | let r = rhs(snd(strip_forall tm)) in |
| 610 | list_mk_abs(cv. (bv @ av),r) in |
| 611 | let th_inst = INST_TYPE (snd(match e_fn rhsfn)) exists in |
| 612 | let fn = fst(dest_exists(concl th_inst)) in |
| 613 | let argvars = map (genvar o type_of) (bv @ av) in |
| 614 | let ap_ths th = |
| 615 | let v = map (genvar o type_of) (fst(strip_forall(concl th))) in |
| 616 | let th1 = rev_itlist (C AP_THM) argvars (SPECL v th) in |
| 617 | (GENL (v @ argvars) th1) in |
| 618 | let th1 = (ap_ths (SELECT_RULE th_inst)) in |
| 619 | let sel = mk_select(dest_exists (concl th_inst)) in |
| 620 | GEN_ALL(EXISTS(mk_exists(fn,subst [fn,sel](concl th1)),sel)th1))? |
| 621 | failwith `Can't derive existence theorem`;; |
| 622 | |
| 623 | % mk_fn: make the function for the rhs of a clause in the existence thm % |
| 624 | % % |
| 625 | % returns: 1) the function % |
| 626 | % 2) a list of variables that the clause should be SPECl % |
| 627 | % 3) a pre-done beta-conversion of the rhs. % |
| 628 | |
| 629 | let mk_fn (cl,(Fn,bv,C,av,r)) = |
| 630 | let lcl = hd(snd(strip_comb(lhs(snd(strip_forall cl))))) in |
| 631 | let lclvars = tl(snd(strip_comb(lhs(snd(strip_forall cl))))) in |
| 632 | let m = (fst(match lcl C)) @ combine((bv @ av),lclvars) in |
| 633 | let cl' = subst m (snd(strip_forall cl)) in |
| 634 | let nonrec = snd(strip_comb(rhs cl')) in |
| 635 | let varsub = map (\v. (genvar (type_of v)),v) nonrec in |
| 636 | let fn = list_mk_abs(fst(split varsub),subst varsub r) in |
| 637 | let specl = map (\v.(fst(rev_assoc v m))? v) (fst(strip_forall cl)) in |
| 638 | let beta = LIST_BETA_CONV(list_mk_comb(fn,snd(strip_comb(rhs cl')))) in |
| 639 | (fn,specl,beta);; |
| 640 | |
| 641 | % instantiate_existence_thm eth tm : instantiate eth to match tm % |
| 642 | % % |
| 643 | % E.g. INPUT: % |
| 644 | % % |
| 645 | % |- !g1. ?fn. !g2 g3 g4 g5 g6 g7. % |
| 646 | % fn (Word4 g2 g3 g4 g5) g6 g7 = g1 g2 g3 g4 g5 g6 g7 % |
| 647 | % % |
| 648 | % "FN x(Word4 a b c d)y = (x + y = 7) /\ a /\ b" % |
| 649 | % % |
| 650 | % OUTPUT: % |
| 651 | % % |
| 652 | % |- ?fn. !a b c d x y. fn(Word4 a b c d)x y = (x + y = 7) /\ a /\ b % |
| 653 | |
| 654 | let instantiate_wordn_existence_thm eth tm = |
| 655 | (let cl = snd(strip_forall tm) in |
| 656 | letrec splt l = |
| 657 | if (is_var (hd l)) then |
| 658 | (let bv,C,av = splt (tl l) in ((hd l).bv,C,av)) else |
| 659 | if (is_const (hd l) or (is_comb (hd l))) then |
| 660 | [],(hd l),(tl l) else fail in |
| 661 | let dest tm = |
| 662 | let ((Fn,(bv,C,av)),r)=(((I # splt) o strip_comb) # I)(dest_eq tm) in |
| 663 | (Fn,bv,C,av,r) in |
| 664 | let destcl = dest cl in |
| 665 | let ecl = snd(dest_exists(snd(strip_forall(concl eth)))) in |
| 666 | let spfn,spec,beta = mk_fn (ecl,destcl) in |
| 667 | let ethsp = SPEC spfn eth in |
| 668 | let SP = SPECL spec (SELECT_RULE ethsp) in |
| 669 | let rule (th1,th2) = CONV_RULE (RAND_CONV (REWR_CONV th1)) th2 in |
| 670 | let th = GEN_ALL(rule(beta,SP)) in |
| 671 | let fn = fst(dest_exists (concl ethsp)) and |
| 672 | fsel = mk_select(dest_exists(concl ethsp)) in |
| 673 | (EXISTS (mk_exists(fn,subst [fn,fsel] (concl th)),fsel) th))? |
| 674 | failwith `Can't instantiate existence theorem`;; |
| 675 | |
| 676 | % prove_wordn_fn_exists th tm % |
| 677 | % % |
| 678 | % Given 1) th, a wordn type axiom. % |
| 679 | % 2) tm, the specification of a function on wordn values. % |
| 680 | % % |
| 681 | % proves that such a function exists. % |
| 682 | |
| 683 | % Quantify the equations of the function spec. % |
| 684 | let closeup tm = |
| 685 | let lvars t = subtract |
| 686 | (freesl(snd(strip_comb(lhs(snd (strip_forall t)))))) |
| 687 | (fst(strip_forall t)) in |
| 688 | list_mk_conj(map (\tm.list_mk_forall(lvars tm,tm)) (conjuncts tm));; |
| 689 | |
| 690 | let prove_wordn_fn_exists th tm = |
| 691 | (let eth = derive_wordn_existence_thm th tm in |
| 692 | let ith = instantiate_wordn_existence_thm eth tm in |
| 693 | letrec get_avars tm = |
| 694 | if (is_var (rand tm)) then (get_avars (rator tm)) else |
| 695 | (snd(strip_comb (rator tm)),rand tm) in |
| 696 | let cl = snd(strip_forall tm) in |
| 697 | let fn = fst(strip_comb(lhs cl)) and |
| 698 | av,tv = (map (genvar o type_of) # (genvar o type_of)) |
| 699 | (get_avars (lhs cl)) in |
| 700 | let goal = ([],mk_exists(fn,closeup tm)) in |
| 701 | let etac th = |
| 702 | let efn = fst(strip_comb(lhs(snd(strip_forall(concl th))))) in |
| 703 | EXISTS_TAC (list_mk_abs(av@[tv],list_mk_comb(efn,tv.av))) in |
| 704 | let fn_beta th (A,gl) = |
| 705 | let efn = fst(strip_comb(lhs(snd(strip_forall(concl th))))) in |
| 706 | let eabs = (list_mk_abs(av@[tv],list_mk_comb(efn,tv.av))) in |
| 707 | let epat = list_mk_comb(eabs, map (genvar o type_of) (av @ [tv])) in |
| 708 | let tms = find_terms (\tm. can (match epat) tm) gl in |
| 709 | SUBST_TAC (map LIST_BETA_CONV tms) (A,gl) in |
| 710 | GEN_ALL(TAC_PROOF(goal, |
| 711 | STRIP_ASSUME_TAC ith THEN FIRST_ASSUM etac THEN |
| 712 | REPEAT STRIP_TAC THEN FIRST_ASSUM fn_beta THEN |
| 713 | FIRST_ASSUM MATCH_ACCEPT_TAC)))? |
| 714 | failwith `Can't prove that function exists`;; |
| 715 | |
| 716 | % Make a new recursive function definition. % |
| 717 | let new_wordn_definition infix_flag th name tm = |
| 718 | let thm = prove_wordn_fn_exists th tm in |
| 719 | if (is_forall (concl thm)) then |
| 720 | failwith `definition contains free vars` else |
| 721 | let def = mk_eq(fst(dest_exists (concl thm)), |
| 722 | mk_select(dest_exists (concl thm))) in |
| 723 | let defn = if (infix_flag) then |
| 724 | new_infix_definition (name ^ `_DEF`,def) else |
| 725 | new_definition (name ^ `_DEF`,def) in |
| 726 | save_thm (name,SUBS [SYM defn] (SELECT_RULE thm));; |