1 needs "../formal_lp/hypermap/arith_link.hl";;
6 (* Based on the code of calc_int.ml *)
10 val my_mk_realintconst : num -> term
11 val my_dest_realintconst : term -> num
12 val my_real_int_neg_conv : term -> thm
13 val my_real_int_add_conv : term -> thm
14 val my_real_int_sub_conv : term -> thm
15 val my_real_int_mul_conv : term -> thm
21 let test = Arith_misc.test;;
23 let to_numeral = Arith_nat.NUM_TO_NUMERAL_CONV;;
24 let from_numeral = Arith_nat.NUMERAL_TO_NUM_CONV;;
25 let mk_num = Arith_nat.mk_numeral_array;;
26 let dest_num = Arith_hash.dest_numeral_hash;;
27 let num_suc = Arith_nat.NUM_SUC_HASH_CONV;;
28 let num_add = Arith_nat.NUM_ADD_HASH_CONV;;
29 let num_mul = Arith_nat.NUM_MULT_HASH_CONV;;
30 let num_gt0 = Arith_nat.NUM_GT0_HASH_CONV;;
33 let my_mk_realintconst n =
34 if n >=/ Int 0 then mk_comb(amp_op_real, mk_num n)
35 else mk_comb(neg_op_real, mk_comb(amp_op_real, mk_num (minus_num n)));;
38 let my_dest_realintconst tm =
39 let ltm, rtm = dest_comb tm in
40 if (ltm = neg_op_real) then
41 let amp_tm, n_tm = dest_comb rtm in
42 if (amp_tm = amp_op_real) then
43 minus_num (dest_num n_tm)
45 failwith "my_dest_realintconst: --(&n) expected"
47 if (ltm = amp_op_real) then
50 failwith "my_dest_realintconst: &n expected";;
54 let is_neg_comb tm = is_comb tm && rator tm = neg_op_real;;
57 let replace_numerals = (rand o concl o DEPTH_CONV from_numeral);;
58 let REPLACE_NUMERALS = CONV_RULE (DEPTH_CONV from_numeral);;
61 let zero_const = replace_numerals `&0`;;
64 (***************************************)
68 let neg_0 = (REPLACE_NUMERALS o prove)(`-- &0 = &0`, REAL_ARITH_TAC) and
69 neg_neg = prove(`--(--(&n)) = &n`, REAL_ARITH_TAC);;
72 let my_real_int_neg_conv tm =
73 let neg_tm, rtm = dest_comb tm in
74 if (neg_tm = neg_op_real) then
75 if (rtm = zero_const) then
78 let neg_tm, rtm = dest_comb rtm in
79 let amp_tm, n_tm = dest_comb rtm in
80 if (neg_tm = neg_op_real && amp_tm = amp_op_real) then
81 INST[n_tm, n_var_num] neg_neg
83 failwith "my_real_int_neg_conv: --(--(&n)) expected"
85 failwith "my_real_int_neg_conv: --x expected";;
89 let tm = `-- -- &12241`;;
92 test 100000 REAL_INT_NEG_CONV tm;;
94 test 100000 my_real_int_neg_conv tm;;
99 (***************************************)
103 let pth1 = prove(`(--(&m) + --(&n) = --(&(m + n)))`, REWRITE_TAC[GSYM REAL_OF_NUM_ADD; REAL_NEG_ADD]) and
104 pth2 = prove(`(--(&m) + &(m + n) = &n)`, REWRITE_TAC[GSYM REAL_OF_NUM_ADD] THEN REAL_ARITH_TAC) and
105 pth3 = prove(`(--(&(m + n)) + &m = --(&n))`, REWRITE_TAC[GSYM REAL_OF_NUM_ADD] THEN REAL_ARITH_TAC) and
106 pth4 = prove(`(&(m + n) + --(&m) = &n)`, REWRITE_TAC[GSYM REAL_OF_NUM_ADD] THEN REAL_ARITH_TAC) and
107 pth5 = prove(`(&m + --(&(m + n)) = --(&n))`, REWRITE_TAC[GSYM REAL_OF_NUM_ADD] THEN REAL_ARITH_TAC) and
108 pth6 = prove(`(&m + &n = &(m + n))`, REWRITE_TAC[GSYM REAL_OF_NUM_ADD]);;
111 let my_real_int_add_conv =
112 let dest = dest_binop add_op_real in
114 try let l,r = dest tm in
115 if rator l = neg_op_real then
116 if rator r = neg_op_real then
117 let th1 = INST [rand(rand l), m_var_num; rand(rand r), n_var_num] pth1 in
118 let tm1 = rand(rand(rand(concl th1))) in
119 let th2 = AP_TERM neg_op_real (AP_TERM amp_op_real (num_add tm1)) in
122 let m = rand(rand l) and n = rand r in
123 let m' = dest_num m and n' = dest_num n in
125 let p = mk_num (n' -/ m') in
126 let th1 = INST [m,m_var_num; p,n_var_num] pth2 in
127 let th2 = num_add (rand(rand(lhand(concl th1)))) in
128 let th3 = AP_TERM (rator tm) (AP_TERM amp_op_real (SYM th2)) in
131 let p = mk_num (m' -/ n') in
132 let th1 = INST [n,m_var_num; p,n_var_num] pth3 in
133 let th2 = num_add (rand(rand(lhand(lhand(concl th1))))) in
134 let th3 = AP_TERM neg_op_real (AP_TERM amp_op_real (SYM th2)) in
135 let th4 = AP_THM (AP_TERM add_op_real th3) (rand tm) in
138 if rator r = neg_op_real then
139 let m = rand l and n = rand(rand r) in
140 let m' = dest_num m and n' = dest_num n in
142 let p = mk_num (m' -/ n') in
143 let th1 = INST [n,m_var_num; p,n_var_num] pth4 in
144 let th2 = num_add (rand(lhand(lhand(concl th1)))) in
145 let th3 = AP_TERM add_op_real (AP_TERM amp_op_real (SYM th2)) in
146 let th4 = AP_THM th3 (rand tm) in
149 let p = mk_num (n' -/ m') in
150 let th1 = INST [m,m_var_num; p,n_var_num] pth5 in
151 let th2 = num_add (rand(rand(rand(lhand(concl th1))))) in
152 let th3 = AP_TERM neg_op_real (AP_TERM amp_op_real (SYM th2)) in
153 let th4 = AP_TERM (rator tm) th3 in
156 let th1 = INST [rand l,m_var_num; rand r,n_var_num] pth6 in
157 let tm1 = rand(rand(concl th1)) in
158 let th2 = AP_TERM amp_op_real (num_add tm1) in
160 with Failure _ -> failwith "my_real_int_add_conv");;
164 let tm = `&3252375395 + --(&3454570237434)`;;
165 let tm' = replace_numerals tm;;
168 test 1000 REAL_INT_ADD_CONV tm;;
170 test 1000 my_real_int_add_conv tm';;
174 (****************************************)
178 let real_sub' = SPEC_ALL real_sub;;
180 let my_real_int_sub_conv tm =
181 let x_tm, y_tm = dest_binop sub_op_real tm in
182 let th0 = INST[x_tm, x_var_real; y_tm, y_var_real] real_sub' in
183 if (is_neg_comb y_tm) then
184 let ltm, rtm = dest_comb(rand(concl th0)) in
185 let neg_th = my_real_int_neg_conv rtm in
186 let th1 = AP_TERM ltm neg_th in
187 let th2 = my_real_int_add_conv (rand(concl th1)) in
188 TRANS th0 (TRANS th1 th2)
190 let th1 = my_real_int_add_conv (rand(concl th0)) in
195 let tm = `-- &35252352362346236236 - (-- &12236236363523)`;;
196 let tm' = replace_numerals tm;;
199 test 1000 REAL_INT_SUB_CONV tm;;
201 test 1000 my_real_int_sub_conv tm';;
207 (****************************************)
212 let mul_pp = prove(`&m * &n = &(m * n)`, REWRITE_TAC[REAL_OF_NUM_MUL]);;
213 let mul_nn = prove(`--(&m) * --(&n) = &(m * n)`, REWRITE_TAC[REAL_NEG_MUL2; mul_pp]) and
214 mul_np = prove(`--(&m) * &n = --(&(m * n))`, REWRITE_TAC[REAL_MUL_LNEG; mul_pp]) and
215 mul_pn = prove(`&m * --(&n) = --(&(m * n))`, REWRITE_TAC[REAL_MUL_RNEG; mul_pp]);;
218 let my_real_int_mul_conv tm =
219 let l, r = dest_binop mul_op_real tm in
220 if rator l = neg_op_real then
221 if rator r = neg_op_real then
222 let th1 = INST [rand(rand l), m_var_num; rand(rand r), n_var_num] mul_nn in
223 let tm1 = rand(rand(concl th1)) in
224 let th2 = AP_TERM amp_op_real (num_mul tm1) in
227 let th1 = INST [rand(rand l), m_var_num; rand r, n_var_num] mul_np in
228 let tm1 = rand(rand(rand(concl th1))) in
229 let th2 = AP_TERM neg_op_real (AP_TERM amp_op_real (num_mul tm1)) in
232 if rator r = neg_op_real then
233 let th1 = INST[rand l, m_var_num; rand(rand r), n_var_num] mul_pn in
234 let tm1 = rand(rand(rand(concl th1))) in
235 let th2 = AP_TERM neg_op_real (AP_TERM amp_op_real (num_mul tm1)) in
238 let th1 = INST[rand l, m_var_num; rand r, n_var_num] mul_pp in
239 let tm1 = rand(rand(concl th1)) in
240 let th2 = AP_TERM amp_op_real (num_mul tm1) in
246 let amp x = mk_comb(amp_op_real, x);;
247 let negate x = mk_comb(neg_op_real, x);;
249 let x = num_of_string "398537263103485";;
250 let y = num_of_string "243089539573957";;
253 let xx = amp (mk_num x) and yy = amp (mk_num y);;
254 let xxx = amp (mk_numeral x) and yyy = amp (mk_numeral y);;
258 test 100 REAL_INT_MUL_CONV (mk_binop mul_op_real (negate xxx) yyy);;
261 test 100 my_real_int_mul_conv (mk_binop mul_op_real (negate xx) yy);;
263 (DEPTH_CONV NUM_TO_NUMERAL_CONV) (concl(REAL_BITS_MUL_CONV (mk_binop mul_op_real xx yy)))