src/ZF/ex/binfn.ML
author lcp
Mon Nov 15 14:41:25 1993 +0100 (1993-11-15)
changeset 120 09287f26bfb8
parent 7 268f93ab3bc4
permissions -rw-r--r--
changed all co- and co_ to co
ZF/ex/llistfn: new coinduction example: flip
ZF/ex/llist_eq: now uses standard pairs not qpairs
clasohm@0
     1
(*  Title: 	ZF/ex/bin.ML
clasohm@0
     2
    ID:         $Id$
clasohm@0
     3
    Author: 	Lawrence C Paulson, Cambridge University Computer Laboratory
clasohm@0
     4
    Copyright   1993  University of Cambridge
clasohm@0
     5
clasohm@0
     6
For bin.thy.  Arithmetic on binary integers.
clasohm@0
     7
*)
clasohm@0
     8
clasohm@0
     9
open BinFn;
clasohm@0
    10
clasohm@0
    11
clasohm@0
    12
(** bin_rec -- by Vset recursion **)
clasohm@0
    13
clasohm@0
    14
goal BinFn.thy "bin_rec(Plus,a,b,h) = a";
clasohm@0
    15
by (rtac (bin_rec_def RS def_Vrec RS trans) 1);
clasohm@0
    16
by (rewrite_goals_tac Bin.con_defs);
lcp@7
    17
by (simp_tac rank_ss 1);
clasohm@0
    18
val bin_rec_Plus = result();
clasohm@0
    19
clasohm@0
    20
goal BinFn.thy "bin_rec(Minus,a,b,h) = b";
clasohm@0
    21
by (rtac (bin_rec_def RS def_Vrec RS trans) 1);
clasohm@0
    22
by (rewrite_goals_tac Bin.con_defs);
lcp@7
    23
by (simp_tac rank_ss 1);
clasohm@0
    24
val bin_rec_Minus = result();
clasohm@0
    25
clasohm@0
    26
goal BinFn.thy "bin_rec(w$$x,a,b,h) = h(w, x, bin_rec(w,a,b,h))";
clasohm@0
    27
by (rtac (bin_rec_def RS def_Vrec RS trans) 1);
clasohm@0
    28
by (rewrite_goals_tac Bin.con_defs);
lcp@7
    29
by (simp_tac rank_ss 1);
clasohm@0
    30
val bin_rec_Bcons = result();
clasohm@0
    31
clasohm@0
    32
(*Type checking*)
clasohm@0
    33
val prems = goal BinFn.thy
clasohm@0
    34
    "[| w: bin;    \
clasohm@0
    35
\       a: C(Plus);   b: C(Minus);       \
clasohm@0
    36
\       !!w x r. [| w: bin;  x: bool;  r: C(w) |] ==> h(w,x,r): C(w$$x)  \
clasohm@0
    37
\    |] ==> bin_rec(w,a,b,h) : C(w)";
clasohm@0
    38
by (bin_ind_tac "w" prems 1);
clasohm@0
    39
by (ALLGOALS 
lcp@7
    40
    (asm_simp_tac (ZF_ss addsimps (prems@[bin_rec_Plus,bin_rec_Minus,
clasohm@0
    41
					 bin_rec_Bcons]))));
clasohm@0
    42
val bin_rec_type = result();
clasohm@0
    43
clasohm@0
    44
(** Versions for use with definitions **)
clasohm@0
    45
clasohm@0
    46
val [rew] = goal BinFn.thy
clasohm@0
    47
    "[| !!w. j(w)==bin_rec(w,a,b,h) |] ==> j(Plus) = a";
clasohm@0
    48
by (rewtac rew);
clasohm@0
    49
by (rtac bin_rec_Plus 1);
clasohm@0
    50
val def_bin_rec_Plus = result();
clasohm@0
    51
clasohm@0
    52
val [rew] = goal BinFn.thy
clasohm@0
    53
    "[| !!w. j(w)==bin_rec(w,a,b,h) |] ==> j(Minus) = b";
clasohm@0
    54
by (rewtac rew);
clasohm@0
    55
by (rtac bin_rec_Minus 1);
clasohm@0
    56
val def_bin_rec_Minus = result();
clasohm@0
    57
clasohm@0
    58
val [rew] = goal BinFn.thy
clasohm@0
    59
    "[| !!w. j(w)==bin_rec(w,a,b,h) |] ==> j(w$$x) = h(w,x,j(w))";
clasohm@0
    60
by (rewtac rew);
clasohm@0
    61
by (rtac bin_rec_Bcons 1);
clasohm@0
    62
val def_bin_rec_Bcons = result();
clasohm@0
    63
clasohm@0
    64
fun bin_recs def = map standard
clasohm@0
    65
	([def] RL [def_bin_rec_Plus, def_bin_rec_Minus, def_bin_rec_Bcons]);
clasohm@0
    66
clasohm@0
    67
(** Type checking **)
clasohm@0
    68
clasohm@0
    69
val bin_typechecks0 = bin_rec_type :: Bin.intrs;
clasohm@0
    70
clasohm@0
    71
goalw BinFn.thy [integ_of_bin_def]
clasohm@0
    72
    "!!w. w: bin ==> integ_of_bin(w) : integ";
clasohm@0
    73
by (typechk_tac (bin_typechecks0@integ_typechecks@
clasohm@0
    74
		 nat_typechecks@[bool_into_nat]));
clasohm@0
    75
val integ_of_bin_type = result();
clasohm@0
    76
clasohm@0
    77
goalw BinFn.thy [bin_succ_def]
clasohm@0
    78
    "!!w. w: bin ==> bin_succ(w) : bin";
clasohm@0
    79
by (typechk_tac (bin_typechecks0@bool_typechecks));
clasohm@0
    80
val bin_succ_type = result();
clasohm@0
    81
clasohm@0
    82
goalw BinFn.thy [bin_pred_def]
clasohm@0
    83
    "!!w. w: bin ==> bin_pred(w) : bin";
clasohm@0
    84
by (typechk_tac (bin_typechecks0@bool_typechecks));
clasohm@0
    85
val bin_pred_type = result();
clasohm@0
    86
clasohm@0
    87
goalw BinFn.thy [bin_minus_def]
clasohm@0
    88
    "!!w. w: bin ==> bin_minus(w) : bin";
clasohm@0
    89
by (typechk_tac ([bin_pred_type]@bin_typechecks0@bool_typechecks));
clasohm@0
    90
val bin_minus_type = result();
clasohm@0
    91
clasohm@0
    92
goalw BinFn.thy [bin_add_def]
clasohm@0
    93
    "!!v w. [| v: bin; w: bin |] ==> bin_add(v,w) : bin";
clasohm@0
    94
by (typechk_tac ([bin_succ_type,bin_pred_type]@bin_typechecks0@
clasohm@0
    95
		 bool_typechecks@ZF_typechecks));
clasohm@0
    96
val bin_add_type = result();
clasohm@0
    97
clasohm@0
    98
goalw BinFn.thy [bin_mult_def]
clasohm@0
    99
    "!!v w. [| v: bin; w: bin |] ==> bin_mult(v,w) : bin";
clasohm@0
   100
by (typechk_tac ([bin_minus_type,bin_add_type]@bin_typechecks0@
clasohm@0
   101
		 bool_typechecks));
clasohm@0
   102
val bin_mult_type = result();
clasohm@0
   103
clasohm@0
   104
val bin_typechecks = bin_typechecks0 @
clasohm@0
   105
    [integ_of_bin_type, bin_succ_type, bin_pred_type, 
clasohm@0
   106
     bin_minus_type, bin_add_type, bin_mult_type];
clasohm@0
   107
clasohm@0
   108
val bin_ss = integ_ss 
lcp@7
   109
    addsimps([bool_1I, bool_0I,
clasohm@0
   110
	     bin_rec_Plus, bin_rec_Minus, bin_rec_Bcons] @ 
lcp@120
   111
	     bin_recs integ_of_bin_def @ bool_simps @ bin_typechecks);
clasohm@0
   112
clasohm@0
   113
val typechecks = bin_typechecks @ integ_typechecks @ nat_typechecks @
clasohm@0
   114
                 [bool_subset_nat RS subsetD];
clasohm@0
   115
clasohm@0
   116
(**** The carry/borrow functions, bin_succ and bin_pred ****)
clasohm@0
   117
clasohm@0
   118
(** Lemmas **)
clasohm@0
   119
clasohm@0
   120
goal Integ.thy 
clasohm@0
   121
    "!!z v. [| z $+ v = z' $+ v';  \
clasohm@0
   122
\       z: integ; z': integ;  v: integ; v': integ;  w: integ |]   \
clasohm@0
   123
\    ==> z $+ (v $+ w) = z' $+ (v' $+ w)";
lcp@7
   124
by (asm_simp_tac (integ_ss addsimps ([zadd_assoc RS sym])) 1);
clasohm@0
   125
val zadd_assoc_cong = result();
clasohm@0
   126
clasohm@0
   127
goal Integ.thy 
clasohm@0
   128
    "!!z v w. [| z: integ;  v: integ;  w: integ |]   \
clasohm@0
   129
\    ==> z $+ (v $+ w) = v $+ (z $+ w)";
clasohm@0
   130
by (REPEAT (ares_tac [zadd_commute RS zadd_assoc_cong] 1));
clasohm@0
   131
val zadd_assoc_swap = result();
clasohm@0
   132
lcp@7
   133
val zadd_cong = 
lcp@7
   134
    read_instantiate_sg (sign_of Integ.thy) [("t","op $+")] subst_context2;
clasohm@0
   135
clasohm@0
   136
val zadd_kill = (refl RS zadd_cong);
clasohm@0
   137
val zadd_assoc_swap_kill = zadd_kill RSN (4, zadd_assoc_swap RS trans);
clasohm@0
   138
clasohm@0
   139
(*Pushes 'constants' of the form $#m to the right -- LOOPS if two!*)
clasohm@0
   140
val zadd_assoc_znat = standard (znat_type RS zadd_assoc_swap);
clasohm@0
   141
clasohm@0
   142
goal Integ.thy 
clasohm@0
   143
    "!!z w. [| z: integ;  w: integ |]   \
clasohm@0
   144
\    ==> w $+ (z $+ (w $+ z)) = w $+ (w $+ (z $+ z))";
clasohm@0
   145
by (REPEAT (ares_tac [zadd_kill, zadd_assoc_swap] 1));
clasohm@0
   146
val zadd_swap_pairs = result();
clasohm@0
   147
clasohm@0
   148
lcp@7
   149
val carry_ss = bin_ss addsimps 
clasohm@0
   150
               (bin_recs bin_succ_def @ bin_recs bin_pred_def);
clasohm@0
   151
clasohm@0
   152
goal BinFn.thy
clasohm@0
   153
    "!!w. w: bin ==> integ_of_bin(bin_succ(w)) = $#1 $+ integ_of_bin(w)";
clasohm@0
   154
by (etac Bin.induct 1);
lcp@7
   155
by (simp_tac (carry_ss addsimps [zadd_0_right]) 1);
lcp@7
   156
by (simp_tac (carry_ss addsimps [zadd_zminus_inverse]) 1);
clasohm@0
   157
by (etac boolE 1);
lcp@7
   158
by (ALLGOALS (asm_simp_tac (carry_ss addsimps [zadd_assoc])));
clasohm@0
   159
by (REPEAT (ares_tac (zadd_swap_pairs::typechecks) 1));
clasohm@0
   160
val integ_of_bin_succ = result();
clasohm@0
   161
clasohm@0
   162
goal BinFn.thy
clasohm@0
   163
    "!!w. w: bin ==> integ_of_bin(bin_pred(w)) = $~ ($#1) $+ integ_of_bin(w)";
clasohm@0
   164
by (etac Bin.induct 1);
lcp@7
   165
by (simp_tac (carry_ss addsimps [zadd_0_right]) 1);
lcp@7
   166
by (simp_tac (carry_ss addsimps [zadd_zminus_inverse]) 1);
clasohm@0
   167
by (etac boolE 1);
clasohm@0
   168
by (ALLGOALS 
lcp@7
   169
    (asm_simp_tac 
lcp@7
   170
     (carry_ss addsimps [zadd_assoc RS sym,
clasohm@0
   171
			zadd_zminus_inverse, zadd_zminus_inverse2])));
clasohm@0
   172
by (REPEAT (ares_tac ([zadd_commute, zadd_cong, refl]@typechecks) 1));
clasohm@0
   173
val integ_of_bin_pred = result();
clasohm@0
   174
clasohm@0
   175
(*These two results replace the definitions of bin_succ and bin_pred*)
clasohm@0
   176
clasohm@0
   177
clasohm@0
   178
(*** bin_minus: (unary!) negation of binary integers ***)
clasohm@0
   179
clasohm@0
   180
val bin_minus_ss =
lcp@7
   181
    bin_ss addsimps (bin_recs bin_minus_def @
clasohm@0
   182
		    [integ_of_bin_succ, integ_of_bin_pred]);
clasohm@0
   183
clasohm@0
   184
goal BinFn.thy
clasohm@0
   185
    "!!w. w: bin ==> integ_of_bin(bin_minus(w)) = $~ integ_of_bin(w)";
clasohm@0
   186
by (etac Bin.induct 1);
lcp@7
   187
by (simp_tac (bin_minus_ss addsimps [zminus_0]) 1);
lcp@7
   188
by (simp_tac (bin_minus_ss addsimps [zadd_0_right]) 1);
clasohm@0
   189
by (etac boolE 1);
clasohm@0
   190
by (ALLGOALS 
lcp@7
   191
    (asm_simp_tac (bin_minus_ss addsimps [zminus_zadd_distrib, zadd_assoc])));
clasohm@0
   192
val integ_of_bin_minus = result();
clasohm@0
   193
clasohm@0
   194
clasohm@0
   195
(*** bin_add: binary addition ***)
clasohm@0
   196
clasohm@0
   197
goalw BinFn.thy [bin_add_def] "!!w. w: bin ==> bin_add(Plus,w) = w";
lcp@7
   198
by (asm_simp_tac bin_ss 1);
clasohm@0
   199
val bin_add_Plus = result();
clasohm@0
   200
clasohm@0
   201
goalw BinFn.thy [bin_add_def] "!!w. w: bin ==> bin_add(Minus,w) = bin_pred(w)";
lcp@7
   202
by (asm_simp_tac bin_ss 1);
clasohm@0
   203
val bin_add_Minus = result();
clasohm@0
   204
clasohm@0
   205
goalw BinFn.thy [bin_add_def] "bin_add(v$$x,Plus) = v$$x";
lcp@7
   206
by (simp_tac bin_ss 1);
clasohm@0
   207
val bin_add_Bcons_Plus = result();
clasohm@0
   208
clasohm@0
   209
goalw BinFn.thy [bin_add_def] "bin_add(v$$x,Minus) = bin_pred(v$$x)";
lcp@7
   210
by (simp_tac bin_ss 1);
clasohm@0
   211
val bin_add_Bcons_Minus = result();
clasohm@0
   212
clasohm@0
   213
goalw BinFn.thy [bin_add_def]
clasohm@0
   214
    "!!w y. [| w: bin;  y: bool |] ==> \
clasohm@0
   215
\           bin_add(v$$x, w$$y) = \
clasohm@0
   216
\           bin_add(v, cond(x and y, bin_succ(w), w)) $$ (x xor y)";
lcp@7
   217
by (asm_simp_tac bin_ss 1);
clasohm@0
   218
val bin_add_Bcons_Bcons = result();
clasohm@0
   219
clasohm@0
   220
val bin_add_rews = [bin_add_Plus, bin_add_Minus, bin_add_Bcons_Plus,
clasohm@0
   221
		    bin_add_Bcons_Minus, bin_add_Bcons_Bcons,
clasohm@0
   222
		    integ_of_bin_succ, integ_of_bin_pred];
clasohm@0
   223
lcp@7
   224
val bin_add_ss = bin_ss addsimps ([bool_subset_nat RS subsetD] @ bin_add_rews);
clasohm@0
   225
clasohm@0
   226
goal BinFn.thy
clasohm@0
   227
    "!!v. v: bin ==> \
clasohm@0
   228
\         ALL w: bin. integ_of_bin(bin_add(v,w)) = \
clasohm@0
   229
\                     integ_of_bin(v) $+ integ_of_bin(w)";
clasohm@0
   230
by (etac Bin.induct 1);
lcp@7
   231
by (simp_tac bin_add_ss 1);
lcp@7
   232
by (simp_tac bin_add_ss 1);
clasohm@0
   233
by (rtac ballI 1);
clasohm@0
   234
by (bin_ind_tac "wa" [] 1);
lcp@7
   235
by (asm_simp_tac (bin_add_ss addsimps [zadd_0_right]) 1);
lcp@7
   236
by (asm_simp_tac bin_add_ss 1);
clasohm@0
   237
by (REPEAT (ares_tac (zadd_commute::typechecks) 1));
clasohm@0
   238
by (etac boolE 1);
lcp@7
   239
by (asm_simp_tac (bin_add_ss addsimps [zadd_assoc, zadd_swap_pairs]) 2);
clasohm@0
   240
by (REPEAT (ares_tac ([refl, zadd_kill, zadd_assoc_swap_kill]@typechecks) 2));
clasohm@0
   241
by (etac boolE 1);
lcp@7
   242
by (ALLGOALS (asm_simp_tac (bin_add_ss addsimps [zadd_assoc,zadd_swap_pairs])));
clasohm@0
   243
by (REPEAT (ares_tac ([refl, zadd_kill, zadd_assoc_swap_kill RS sym]@
clasohm@0
   244
		      typechecks) 1));
clasohm@0
   245
val integ_of_bin_add_lemma = result();
clasohm@0
   246
clasohm@0
   247
val integ_of_bin_add = integ_of_bin_add_lemma RS bspec;
clasohm@0
   248
clasohm@0
   249
clasohm@0
   250
(*** bin_add: binary multiplication ***)
clasohm@0
   251
clasohm@0
   252
val bin_mult_ss =
lcp@7
   253
    bin_ss addsimps (bin_recs bin_mult_def @ 
clasohm@0
   254
		       [integ_of_bin_minus, integ_of_bin_add]);
clasohm@0
   255
clasohm@0
   256
clasohm@0
   257
val major::prems = goal BinFn.thy
clasohm@0
   258
    "[| v: bin; w: bin |] ==>	\
clasohm@0
   259
\    integ_of_bin(bin_mult(v,w)) = \
clasohm@0
   260
\    integ_of_bin(v) $* integ_of_bin(w)";
clasohm@0
   261
by (cut_facts_tac prems 1);
clasohm@0
   262
by (bin_ind_tac "v" [major] 1);
lcp@7
   263
by (asm_simp_tac (bin_mult_ss addsimps [zmult_0]) 1);
lcp@7
   264
by (asm_simp_tac (bin_mult_ss addsimps [zmult_1,zmult_zminus]) 1);
clasohm@0
   265
by (etac boolE 1);
lcp@7
   266
by (asm_simp_tac (bin_mult_ss addsimps [zadd_zmult_distrib]) 2);
lcp@7
   267
by (asm_simp_tac 
lcp@7
   268
    (bin_mult_ss addsimps [zadd_zmult_distrib, zmult_1, zadd_assoc]) 1);
clasohm@0
   269
by (REPEAT (ares_tac ([zadd_commute, zadd_assoc_swap_kill RS sym]@
clasohm@0
   270
		      typechecks) 1));
clasohm@0
   271
val integ_of_bin_mult = result();
clasohm@0
   272
clasohm@0
   273
(**** Computations ****)
clasohm@0
   274
clasohm@0
   275
(** extra rules for bin_succ, bin_pred **)
clasohm@0
   276
clasohm@0
   277
val [bin_succ_Plus, bin_succ_Minus, _] = bin_recs bin_succ_def;
clasohm@0
   278
val [bin_pred_Plus, bin_pred_Minus, _] = bin_recs bin_pred_def;
clasohm@0
   279
clasohm@0
   280
goal BinFn.thy "bin_succ(w$$1) = bin_succ(w) $$ 0";
lcp@7
   281
by (simp_tac carry_ss 1);
clasohm@0
   282
val bin_succ_Bcons1 = result();
clasohm@0
   283
clasohm@0
   284
goal BinFn.thy "bin_succ(w$$0) = w$$1";
lcp@7
   285
by (simp_tac carry_ss 1);
clasohm@0
   286
val bin_succ_Bcons0 = result();
clasohm@0
   287
clasohm@0
   288
goal BinFn.thy "bin_pred(w$$1) = w$$0";
lcp@7
   289
by (simp_tac carry_ss 1);
clasohm@0
   290
val bin_pred_Bcons1 = result();
clasohm@0
   291
clasohm@0
   292
goal BinFn.thy "bin_pred(w$$0) = bin_pred(w) $$ 1";
lcp@7
   293
by (simp_tac carry_ss 1);
clasohm@0
   294
val bin_pred_Bcons0 = result();
clasohm@0
   295
clasohm@0
   296
(** extra rules for bin_minus **)
clasohm@0
   297
clasohm@0
   298
val [bin_minus_Plus, bin_minus_Minus, _] = bin_recs bin_minus_def;
clasohm@0
   299
clasohm@0
   300
goal BinFn.thy "bin_minus(w$$1) = bin_pred(bin_minus(w) $$ 0)";
lcp@7
   301
by (simp_tac bin_minus_ss 1);
clasohm@0
   302
val bin_minus_Bcons1 = result();
clasohm@0
   303
clasohm@0
   304
goal BinFn.thy "bin_minus(w$$0) = bin_minus(w) $$ 0";
lcp@7
   305
by (simp_tac bin_minus_ss 1);
clasohm@0
   306
val bin_minus_Bcons0 = result();
clasohm@0
   307
clasohm@0
   308
(** extra rules for bin_add **)
clasohm@0
   309
clasohm@0
   310
goal BinFn.thy 
clasohm@0
   311
    "!!w. w: bin ==> bin_add(v$$1, w$$1) = bin_add(v, bin_succ(w)) $$ 0";
lcp@7
   312
by (asm_simp_tac bin_add_ss 1);
clasohm@0
   313
val bin_add_Bcons_Bcons11 = result();
clasohm@0
   314
clasohm@0
   315
goal BinFn.thy 
clasohm@0
   316
    "!!w. w: bin ==> bin_add(v$$1, w$$0) = bin_add(v,w) $$ 1";
lcp@7
   317
by (asm_simp_tac bin_add_ss 1);
clasohm@0
   318
val bin_add_Bcons_Bcons10 = result();
clasohm@0
   319
clasohm@0
   320
goal BinFn.thy 
clasohm@0
   321
    "!!w y.[| w: bin;  y: bool |] ==> bin_add(v$$0, w$$y) = bin_add(v,w) $$ y";
lcp@7
   322
by (asm_simp_tac bin_add_ss 1);
clasohm@0
   323
val bin_add_Bcons_Bcons0 = result();
clasohm@0
   324
clasohm@0
   325
(** extra rules for bin_mult **)
clasohm@0
   326
clasohm@0
   327
val [bin_mult_Plus, bin_mult_Minus, _] = bin_recs bin_mult_def;
clasohm@0
   328
clasohm@0
   329
goal BinFn.thy "bin_mult(v$$1, w) = bin_add(bin_mult(v,w)$$0, w)";
lcp@7
   330
by (simp_tac bin_mult_ss 1);
clasohm@0
   331
val bin_mult_Bcons1 = result();
clasohm@0
   332
clasohm@0
   333
goal BinFn.thy "bin_mult(v$$0, w) = bin_mult(v,w)$$0";
lcp@7
   334
by (simp_tac bin_mult_ss 1);
clasohm@0
   335
val bin_mult_Bcons0 = result();
clasohm@0
   336
clasohm@0
   337
clasohm@0
   338
(*** The computation simpset ***)
clasohm@0
   339
clasohm@0
   340
val bin_comp_ss = integ_ss 
lcp@7
   341
    addsimps [bin_succ_Plus, bin_succ_Minus,
clasohm@0
   342
	     bin_succ_Bcons1, bin_succ_Bcons0,
clasohm@0
   343
	     bin_pred_Plus, bin_pred_Minus,
clasohm@0
   344
	     bin_pred_Bcons1, bin_pred_Bcons0,
clasohm@0
   345
	     bin_minus_Plus, bin_minus_Minus,
clasohm@0
   346
	     bin_minus_Bcons1, bin_minus_Bcons0,
clasohm@0
   347
	     bin_add_Plus, bin_add_Minus, bin_add_Bcons_Plus, 
clasohm@0
   348
	     bin_add_Bcons_Minus, bin_add_Bcons_Bcons0, 
clasohm@0
   349
	     bin_add_Bcons_Bcons10, bin_add_Bcons_Bcons11,
clasohm@0
   350
	     bin_mult_Plus, bin_mult_Minus,
clasohm@0
   351
	     bin_mult_Bcons1, bin_mult_Bcons0]
lcp@7
   352
    setsolver (type_auto_tac ([bool_1I, bool_0I] @ bin_typechecks0));
clasohm@0
   353
clasohm@0
   354
(*** Examples of performing binary arithmetic by simplification ***)
clasohm@0
   355
clasohm@0
   356
proof_timing := true;
clasohm@0
   357
(*All runtimes below are on a SPARCserver 10*)
clasohm@0
   358
clasohm@0
   359
(* 13+19 = 32 *)
clasohm@0
   360
goal BinFn.thy
clasohm@0
   361
    "bin_add(Plus$$1$$1$$0$$1, Plus$$1$$0$$0$$1$$1) = Plus$$1$$0$$0$$0$$0$$0";
lcp@7
   362
by (simp_tac bin_comp_ss 1);	(*0.6 secs*)
clasohm@0
   363
result();
clasohm@0
   364
clasohm@0
   365
bin_add(binary_of_int 13, binary_of_int 19);
clasohm@0
   366
clasohm@0
   367
(* 1234+5678 = 6912 *)
clasohm@0
   368
goal BinFn.thy
clasohm@0
   369
    "bin_add(Plus$$1$$0$$0$$1$$1$$0$$1$$0$$0$$1$$0, \
clasohm@0
   370
\	     Plus$$1$$0$$1$$1$$0$$0$$0$$1$$0$$1$$1$$1$$0) = \
clasohm@0
   371
\    Plus$$1$$1$$0$$1$$1$$0$$0$$0$$0$$0$$0$$0$$0";
lcp@7
   372
by (simp_tac bin_comp_ss 1);	(*2.6 secs*)
clasohm@0
   373
result();
clasohm@0
   374
clasohm@0
   375
bin_add(binary_of_int 1234, binary_of_int 5678);
clasohm@0
   376
clasohm@0
   377
(* 1359-2468 = ~1109 *)
clasohm@0
   378
goal BinFn.thy
clasohm@0
   379
    "bin_add(Plus$$1$$0$$1$$0$$1$$0$$0$$1$$1$$1$$1,		\
clasohm@0
   380
\	     Minus$$0$$1$$1$$0$$0$$1$$0$$1$$1$$1$$0$$0) = 	\
clasohm@0
   381
\    Minus$$1$$0$$1$$1$$1$$0$$1$$0$$1$$0$$1$$1";
lcp@7
   382
by (simp_tac bin_comp_ss 1);	(*2.3 secs*)
clasohm@0
   383
result();
clasohm@0
   384
clasohm@0
   385
bin_add(binary_of_int 1359, binary_of_int ~2468);
clasohm@0
   386
clasohm@0
   387
(* 93746-46375 = 47371 *)
clasohm@0
   388
goal BinFn.thy
clasohm@0
   389
    "bin_add(Plus$$1$$0$$1$$1$$0$$1$$1$$1$$0$$0$$0$$1$$1$$0$$0$$1$$0, \
clasohm@0
   390
\	     Minus$$0$$1$$0$$0$$1$$0$$1$$0$$1$$1$$0$$1$$1$$0$$0$$1) = \
clasohm@0
   391
\    Plus$$0$$1$$0$$1$$1$$1$$0$$0$$1$$0$$0$$0$$0$$1$$0$$1$$1";
lcp@7
   392
by (simp_tac bin_comp_ss 1);	(*3.9 secs*)
clasohm@0
   393
result();
clasohm@0
   394
clasohm@0
   395
bin_add(binary_of_int 93746, binary_of_int ~46375);
clasohm@0
   396
clasohm@0
   397
(* negation of 65745 *)
clasohm@0
   398
goal BinFn.thy
clasohm@0
   399
    "bin_minus(Plus$$1$$0$$0$$0$$0$$0$$0$$0$$0$$1$$1$$0$$1$$0$$0$$0$$1) = \
clasohm@0
   400
\    Minus$$0$$1$$1$$1$$1$$1$$1$$1$$1$$0$$0$$1$$0$$1$$1$$1$$1";
lcp@7
   401
by (simp_tac bin_comp_ss 1);	(*0.6 secs*)
clasohm@0
   402
result();
clasohm@0
   403
clasohm@0
   404
bin_minus(binary_of_int 65745);
clasohm@0
   405
clasohm@0
   406
(* negation of ~54321 *)
clasohm@0
   407
goal BinFn.thy
clasohm@0
   408
    "bin_minus(Minus$$0$$0$$1$$0$$1$$0$$1$$1$$1$$1$$0$$0$$1$$1$$1$$1) = \
clasohm@0
   409
\    Plus$$0$$1$$1$$0$$1$$0$$1$$0$$0$$0$$0$$1$$1$$0$$0$$0$$1";
lcp@7
   410
by (simp_tac bin_comp_ss 1);	(*0.7 secs*)
clasohm@0
   411
result();
clasohm@0
   412
clasohm@0
   413
bin_minus(binary_of_int ~54321);
clasohm@0
   414
clasohm@0
   415
(* 13*19 = 247 *)
clasohm@0
   416
goal BinFn.thy "bin_mult(Plus$$1$$1$$0$$1, Plus$$1$$0$$0$$1$$1) = \
clasohm@0
   417
\               Plus$$1$$1$$1$$1$$0$$1$$1$$1";
lcp@7
   418
by (simp_tac bin_comp_ss 1);	(*1.5 secs*)
clasohm@0
   419
result();
clasohm@0
   420
clasohm@0
   421
bin_mult(binary_of_int 13, binary_of_int 19);
clasohm@0
   422
clasohm@0
   423
(* ~84 * 51 = ~4284 *)
clasohm@0
   424
goal BinFn.thy
clasohm@0
   425
    "bin_mult(Minus$$0$$1$$0$$1$$1$$0$$0, Plus$$1$$1$$0$$0$$1$$1) = \
clasohm@0
   426
\    Minus$$0$$1$$1$$1$$1$$0$$1$$0$$0$$0$$1$$0$$0";
lcp@7
   427
by (simp_tac bin_comp_ss 1);	(*2.6 secs*)
clasohm@0
   428
result();
clasohm@0
   429
clasohm@0
   430
bin_mult(binary_of_int ~84, binary_of_int 51);
clasohm@0
   431
clasohm@0
   432
(* 255*255 = 65025;  the worst case for 8-bit operands *)
clasohm@0
   433
goal BinFn.thy
clasohm@0
   434
    "bin_mult(Plus$$1$$1$$1$$1$$1$$1$$1$$1, \
clasohm@0
   435
\             Plus$$1$$1$$1$$1$$1$$1$$1$$1) = \
clasohm@0
   436
\        Plus$$1$$1$$1$$1$$1$$1$$1$$0$$0$$0$$0$$0$$0$$0$$0$$1";
lcp@7
   437
by (simp_tac bin_comp_ss 1);	(*9.8 secs*)
clasohm@0
   438
result();
clasohm@0
   439
clasohm@0
   440
bin_mult(binary_of_int 255, binary_of_int 255);
clasohm@0
   441
clasohm@0
   442
(* 1359 * ~2468 = ~3354012 *)
clasohm@0
   443
goal BinFn.thy
clasohm@0
   444
    "bin_mult(Plus$$1$$0$$1$$0$$1$$0$$0$$1$$1$$1$$1, 		\
clasohm@0
   445
\	      Minus$$0$$1$$1$$0$$0$$1$$0$$1$$1$$1$$0$$0) = 	\
clasohm@0
   446
\    Minus$$0$$0$$1$$1$$0$$0$$1$$1$$0$$1$$0$$0$$1$$0$$0$$1$$1$$0$$0$$1$$0$$0";
lcp@7
   447
by (simp_tac bin_comp_ss 1);	(*13.7 secs*)
clasohm@0
   448
result();
clasohm@0
   449
clasohm@0
   450
bin_mult(binary_of_int 1359, binary_of_int ~2468);