177 (fn _ => EVERY [simp_tac lin_ss 1, TRY (simple_arith_tac 1)]); |
177 (fn _ => EVERY [simp_tac lin_ss 1, TRY (simple_arith_tac 1)]); |
178 fun linear_cmul 0 tm = zero |
178 fun linear_cmul 0 tm = zero |
179 | linear_cmul n tm = |
179 | linear_cmul n tm = |
180 case tm of |
180 case tm of |
181 Const("HOL.plus_class.plus",_)$a$b => addC$(linear_cmul n a)$(linear_cmul n b) |
181 Const("HOL.plus_class.plus",_)$a$b => addC$(linear_cmul n a)$(linear_cmul n b) |
182 | Const ("HOL.times_class.times",_)$c$x => mulC$(numeral1 ((curry (op *)) n) c)$x |
182 | Const ("HOL.times_class.times",_)$c$x => mulC$(numeral1 ((curry op *) n) c)$x |
183 | Const("HOL.minus_class.minus",_)$a$b => subC$(linear_cmul n a)$(linear_cmul n b) |
183 | Const("HOL.minus_class.minus",_)$a$b => subC$(linear_cmul n a)$(linear_cmul n b) |
184 | (m as Const("HOL.minus_class.uminus",_))$a => m$(linear_cmul n a) |
184 | (m as Const("HOL.minus_class.uminus",_))$a => m$(linear_cmul n a) |
185 | _ => numeral1 ((curry (op *)) n) tm; |
185 | _ => numeral1 ((curry op *) n) tm; |
186 fun earlier [] x y = false |
186 fun earlier [] x y = false |
187 | earlier (h::t) x y = |
187 | earlier (h::t) x y = |
188 if h aconv y then false else if h aconv x then true else earlier t x y; |
188 if h aconv y then false else if h aconv x then true else earlier t x y; |
189 |
189 |
190 fun linear_add vars tm1 tm2 = |
190 fun linear_add vars tm1 tm2 = |
191 case (tm1,tm2) of |
191 case (tm1,tm2) of |
192 (Const("HOL.plus_class.plus",_)$(Const("HOL.times_class.times",_)$c1$x1)$r1, |
192 (Const("HOL.plus_class.plus",_)$(Const("HOL.times_class.times",_)$c1$x1)$r1, |
193 Const("HOL.plus_class.plus",_)$(Const("HOL.times_class.times",_)$c2$x2)$r2) => |
193 Const("HOL.plus_class.plus",_)$(Const("HOL.times_class.times",_)$c2$x2)$r2) => |
194 if x1 = x2 then |
194 if x1 = x2 then |
195 let val c = numeral2 (curry (op +)) c1 c2 |
195 let val c = numeral2 (curry op +) c1 c2 |
196 in if c = zero then linear_add vars r1 r2 |
196 in if c = zero then linear_add vars r1 r2 |
197 else addC$(mulC$c$x1)$(linear_add vars r1 r2) |
197 else addC$(mulC$c$x1)$(linear_add vars r1 r2) |
198 end |
198 end |
199 else if earlier vars x1 x2 then addC$(mulC$ c1 $ x1)$(linear_add vars r1 tm2) |
199 else if earlier vars x1 x2 then addC$(mulC$ c1 $ x1)$(linear_add vars r1 tm2) |
200 else addC$(mulC$c2$x2)$(linear_add vars tm1 r2) |
200 else addC$(mulC$c2$x2)$(linear_add vars tm1 r2) |
201 | (Const("HOL.plus_class.plus",_) $ (Const("HOL.times_class.times",_)$c1$x1)$r1 ,_) => |
201 | (Const("HOL.plus_class.plus",_) $ (Const("HOL.times_class.times",_)$c1$x1)$r1 ,_) => |
202 addC$(mulC$c1$x1)$(linear_add vars r1 tm2) |
202 addC$(mulC$c1$x1)$(linear_add vars r1 tm2) |
203 | (_, Const("HOL.plus_class.plus",_)$(Const("HOL.times_class.times",_)$c2$x2)$r2) => |
203 | (_, Const("HOL.plus_class.plus",_)$(Const("HOL.times_class.times",_)$c2$x2)$r2) => |
204 addC$(mulC$c2$x2)$(linear_add vars tm1 r2) |
204 addC$(mulC$c2$x2)$(linear_add vars tm1 r2) |
205 | (_,_) => numeral2 (curry (op +)) tm1 tm2; |
205 | (_,_) => numeral2 (curry op +) tm1 tm2; |
206 |
206 |
207 fun linear_neg tm = linear_cmul ~1 tm; |
207 fun linear_neg tm = linear_cmul ~1 tm; |
208 fun linear_sub vars tm1 tm2 = linear_add vars tm1 (linear_neg tm2); |
208 fun linear_sub vars tm1 tm2 = linear_add vars tm1 (linear_neg tm2); |
209 |
209 |
210 |
210 |