src/HOL/Library/normarith.ML
changeset 31445 c8a474a919a7
parent 31344 fc09ec06b89b
child 31446 2d91b2416de8
equal deleted inserted replaced
31440:ee1d5bec4ce3 31445:c8a474a919a7
    47  fun int_lincomb_add l r = Intfunc.combine (curry op +/) (fn x => x =/ Rat.zero) l r
    47  fun int_lincomb_add l r = Intfunc.combine (curry op +/) (fn x => x =/ Rat.zero) l r
    48  fun int_lincomb_sub l r = int_lincomb_add l (int_lincomb_neg r)
    48  fun int_lincomb_sub l r = int_lincomb_add l (int_lincomb_neg r)
    49  fun int_lincomb_eq l r = Intfunc.is_undefined (int_lincomb_sub l r)
    49  fun int_lincomb_eq l r = Intfunc.is_undefined (int_lincomb_sub l r)
    50 
    50 
    51 fun vector_lincomb t = case term_of t of 
    51 fun vector_lincomb t = case term_of t of 
    52    Const(@{const_name plus},Type("fun",[Type("Finite_Cartesian_Product.^",_),_])) $ _ $ _ =>
    52    Const(@{const_name plus}, _) $ _ $ _ =>
    53     cterm_lincomb_add (vector_lincomb (dest_arg1 t)) (vector_lincomb (dest_arg t))
    53     cterm_lincomb_add (vector_lincomb (dest_arg1 t)) (vector_lincomb (dest_arg t))
    54  | Const(@{const_name minus},Type("fun",[Type("Finite_Cartesian_Product.^",_),_])) $ _ $ _ =>
    54  | Const(@{const_name minus}, _) $ _ $ _ =>
    55     cterm_lincomb_sub (vector_lincomb (dest_arg1 t)) (vector_lincomb (dest_arg t))
    55     cterm_lincomb_sub (vector_lincomb (dest_arg1 t)) (vector_lincomb (dest_arg t))
    56  | Const(@{const_name vector_scalar_mult},Type("fun",[Type("Finite_Cartesian_Product.^",_),_]))$_$_ =>
    56  | Const(@{const_name scaleR}, _)$_$_ =>
    57     cterm_lincomb_cmul (dest_ratconst (dest_arg1 t)) (vector_lincomb (dest_arg t))
    57     cterm_lincomb_cmul (dest_ratconst (dest_arg1 t)) (vector_lincomb (dest_arg t))
    58  | Const(@{const_name uminus},Type("fun",[Type("Finite_Cartesian_Product.^",_),_]))$_ =>
    58  | Const(@{const_name uminus}, _)$_ =>
    59      cterm_lincomb_neg (vector_lincomb (dest_arg t))
    59      cterm_lincomb_neg (vector_lincomb (dest_arg t))
    60  | Const(@{const_name vec},_)$_ => 
    60 (* FIXME: how should we handle numerals?
       
    61  | Const(@ {const_name vec},_)$_ => 
    61    let 
    62    let 
    62      val b = ((snd o HOLogic.dest_number o term_of o dest_arg) t = 0 
    63      val b = ((snd o HOLogic.dest_number o term_of o dest_arg) t = 0 
    63                handle TERM _=> false)
    64                handle TERM _=> false)
    64    in if b then Ctermfunc.onefunc (t,Rat.one)
    65    in if b then Ctermfunc.onefunc (t,Rat.one)
    65       else Ctermfunc.undefined
    66       else Ctermfunc.undefined
    66    end
    67    end
       
    68 *)
    67  | _ => Ctermfunc.onefunc (t,Rat.one)
    69  | _ => Ctermfunc.onefunc (t,Rat.one)
    68 
    70 
    69  fun vector_lincombs ts =
    71  fun vector_lincombs ts =
    70   fold_rev 
    72   fold_rev 
    71    (fn t => fn fns => case AList.lookup (op aconvc) fns t of
    73    (fn t => fn fns => case AList.lookup (op aconvc) fns t of
   186  val apply_pth3 = rewr_conv @{thm pth_3};
   188  val apply_pth3 = rewr_conv @{thm pth_3};
   187  val apply_pth4 = rewrs_conv @{thms pth_4};
   189  val apply_pth4 = rewrs_conv @{thms pth_4};
   188  val apply_pth5 = rewr_conv @{thm pth_5};
   190  val apply_pth5 = rewr_conv @{thm pth_5};
   189  val apply_pth6 = rewr_conv @{thm pth_6};
   191  val apply_pth6 = rewr_conv @{thm pth_6};
   190  val apply_pth7 = rewrs_conv @{thms pth_7};
   192  val apply_pth7 = rewrs_conv @{thms pth_7};
   191  val apply_pth8 = rewr_conv @{thm pth_8} then_conv arg1_conv field_comp_conv then_conv (try_conv (rewr_conv (mk_meta_eq @{thm vector_smult_lzero})));
   193  val apply_pth8 = rewr_conv @{thm pth_8} then_conv arg1_conv field_comp_conv then_conv (try_conv (rewr_conv (mk_meta_eq @{thm scaleR_zero_left})));
   192  val apply_pth9 = rewrs_conv @{thms pth_9} then_conv arg1_conv (arg1_conv field_comp_conv);
   194  val apply_pth9 = rewrs_conv @{thms pth_9} then_conv arg1_conv (arg1_conv field_comp_conv);
   193  val apply_ptha = rewr_conv @{thm pth_a};
   195  val apply_ptha = rewr_conv @{thm pth_a};
   194  val apply_pthb = rewrs_conv @{thms pth_b};
   196  val apply_pthb = rewrs_conv @{thms pth_b};
   195  val apply_pthc = rewrs_conv @{thms pth_c};
   197  val apply_pthc = rewrs_conv @{thms pth_c};
   196  val apply_pthd = try_conv (rewr_conv @{thm pth_d});
   198  val apply_pthd = try_conv (rewr_conv @{thm pth_d});
   197 
   199 
   198 fun headvector t = case t of 
   200 fun headvector t = case t of 
   199   Const(@{const_name plus}, Type("fun",[Type("Finite_Cartesian_Product.^",_),_]))$
   201   Const(@{const_name plus}, _)$
   200    (Const(@{const_name vector_scalar_mult}, _)$l$v)$r => v
   202    (Const(@{const_name scaleR}, _)$l$v)$r => v
   201  | Const(@{const_name vector_scalar_mult}, _)$l$v => v
   203  | Const(@{const_name scaleR}, _)$l$v => v
   202  | _ => error "headvector: non-canonical term"
   204  | _ => error "headvector: non-canonical term"
   203 
   205 
   204 fun vector_cmul_conv ct =
   206 fun vector_cmul_conv ct =
   205    ((apply_pth5 then_conv arg1_conv field_comp_conv) else_conv
   207    ((apply_pth5 then_conv arg1_conv field_comp_conv) else_conv
   206     (apply_pth6 then_conv binop_conv vector_cmul_conv)) ct
   208     (apply_pth6 then_conv binop_conv vector_cmul_conv)) ct
   232    val lth = vector_canon_conv l 
   234    val lth = vector_canon_conv l 
   233    val rth = vector_canon_conv r
   235    val rth = vector_canon_conv r
   234    val th = Drule.binop_cong_rule p lth rth 
   236    val th = Drule.binop_cong_rule p lth rth 
   235   in fconv_rule (arg_conv vector_add_conv) th end
   237   in fconv_rule (arg_conv vector_add_conv) th end
   236 
   238 
   237 | Const(@{const_name vector_scalar_mult}, _)$_$_ =>
   239 | Const(@{const_name scaleR}, _)$_$_ =>
   238   let 
   240   let 
   239    val (p,r) = Thm.dest_comb ct
   241    val (p,r) = Thm.dest_comb ct
   240    val rth = Drule.arg_cong_rule p (vector_canon_conv r) 
   242    val rth = Drule.arg_cong_rule p (vector_canon_conv r) 
   241   in fconv_rule (arg_conv (apply_pth4 else_conv vector_cmul_conv)) rth
   243   in fconv_rule (arg_conv (apply_pth4 else_conv vector_cmul_conv)) rth
   242   end
   244   end
   243 
   245 
   244 | Const(@{const_name minus},_)$_$_ => (apply_pth2 then_conv vector_canon_conv) ct
   246 | Const(@{const_name minus},_)$_$_ => (apply_pth2 then_conv vector_canon_conv) ct
   245 
   247 
   246 | Const(@{const_name uminus},_)$_ => (apply_pth3 then_conv vector_canon_conv) ct
   248 | Const(@{const_name uminus},_)$_ => (apply_pth3 then_conv vector_canon_conv) ct
   247 
   249 
       
   250 (* FIXME
   248 | Const(@{const_name vec},_)$n => 
   251 | Const(@{const_name vec},_)$n => 
   249   let val n = Thm.dest_arg ct
   252   let val n = Thm.dest_arg ct
   250   in if is_ratconst n andalso not (dest_ratconst n =/ Rat.zero) 
   253   in if is_ratconst n andalso not (dest_ratconst n =/ Rat.zero) 
   251      then reflexive ct else apply_pth1 ct
   254      then reflexive ct else apply_pth1 ct
   252   end
   255   end
   253 
   256 *)
   254 | _ => apply_pth1 ct
   257 | _ => apply_pth1 ct
   255 
   258 
   256 fun norm_canon_conv ct = case term_of ct of
   259 fun norm_canon_conv ct = case term_of ct of
   257   Const(@{const_name norm},_)$_ => arg_conv vector_canon_conv ct
   260   Const(@{const_name norm},_)$_ => arg_conv vector_canon_conv ct
   258  | _ => raise CTERM ("norm_canon_conv", [ct])
   261  | _ => raise CTERM ("norm_canon_conv", [ct])
   264 fun int_flip v eq = 
   267 fun int_flip v eq = 
   265   if Intfunc.defined eq v 
   268   if Intfunc.defined eq v 
   266   then Intfunc.update (v, Rat.neg (Intfunc.apply eq v)) eq else eq;
   269   then Intfunc.update (v, Rat.neg (Intfunc.apply eq v)) eq else eq;
   267 
   270 
   268 local
   271 local
   269  val pth_zero = @{thm "norm_0"}
   272  val pth_zero = @{thm norm_zero}
   270  val tv_n = (hd o tl o dest_ctyp o ctyp_of_term o dest_arg o dest_arg1 o dest_arg o cprop_of)
   273  val tv_n = (ctyp_of_term o dest_arg o dest_arg1 o dest_arg o cprop_of)
   271              pth_zero
   274              pth_zero
   272  val concl = dest_arg o cprop_of 
   275  val concl = dest_arg o cprop_of 
   273  fun real_vector_combo_prover ctxt translator (nubs,ges,gts) = 
   276  fun real_vector_combo_prover ctxt translator (nubs,ges,gts) = 
   274   let 
   277   let 
   275    (* FIXME: Should be computed statically!!*)
   278    (* FIXME: Should be computed statically!!*)
   325                  map (inequality_canon_rule ctxt) nubs @ ges
   328                  map (inequality_canon_rule ctxt) nubs @ ges
   326    val zerodests = filter
   329    val zerodests = filter
   327         (fn t => null (Ctermfunc.dom (vector_lincomb t))) (map snd rawdests)
   330         (fn t => null (Ctermfunc.dom (vector_lincomb t))) (map snd rawdests)
   328 
   331 
   329   in RealArith.real_linear_prover translator
   332   in RealArith.real_linear_prover translator
   330         (map (fn t => instantiate ([(tv_n,(hd o tl o dest_ctyp o ctyp_of_term) t)],[]) pth_zero)
   333         (map (fn t => instantiate ([(tv_n, ctyp_of_term t)],[]) pth_zero)
   331             zerodests,
   334             zerodests,
   332         map (fconv_rule (once_depth_conv (norm_canon_conv) then_conv
   335         map (fconv_rule (once_depth_conv (norm_canon_conv) then_conv
   333                        arg_conv (arg_conv real_poly_conv))) ges',
   336                        arg_conv (arg_conv real_poly_conv))) ges',
   334         map (fconv_rule (once_depth_conv (norm_canon_conv) then_conv 
   337         map (fconv_rule (once_depth_conv (norm_canon_conv) then_conv 
   335                        arg_conv (arg_conv real_poly_conv))) gts)
   338                        arg_conv (arg_conv real_poly_conv))) gts)
   389          (fold_rev (splitequation ctxt) eqs ges,gts)
   392          (fold_rev (splitequation ctxt) eqs ges,gts)
   390 end;
   393 end;
   391 
   394 
   392   fun init_conv ctxt = 
   395   fun init_conv ctxt = 
   393    Simplifier.rewrite (Simplifier.context ctxt 
   396    Simplifier.rewrite (Simplifier.context ctxt 
   394      (HOL_basic_ss addsimps ([@{thm vec_0}, @{thm vec_1}, @{thm vector_dist_norm}, @{thm diff_0_right}, @{thm right_minus}, @{thm diff_self}, @{thm norm_0}] @ @{thms arithmetic_simps} @ @{thms norm_pths})))
   397      (HOL_basic_ss addsimps ([(*@{thm vec_0}, @{thm vec_1},*) @{thm vector_dist_norm}, @{thm diff_0_right}, @{thm right_minus}, @{thm diff_self}, @{thm norm_zero}] @ @{thms arithmetic_simps} @ @{thms norm_pths})))
   395    then_conv field_comp_conv 
   398    then_conv field_comp_conv 
   396    then_conv nnf_conv
   399    then_conv nnf_conv
   397 
   400 
   398  fun pure ctxt = RealArith.gen_prover_real_arith ctxt (real_vector_prover ctxt);
   401  fun pure ctxt = RealArith.gen_prover_real_arith ctxt (real_vector_prover ctxt);
   399  fun norm_arith ctxt ct = 
   402  fun norm_arith ctxt ct =