src/HOL/Library/Sum_of_Squares/sum_of_squares.ML
changeset 58631 41333b45bff9
parent 57889 049e13f616d4
child 58635 010b610eb55d
equal deleted inserted replaced
58630:71cdb885b3bb 58631:41333b45bff9
     8 signature SUM_OF_SQUARES =
     8 signature SUM_OF_SQUARES =
     9 sig
     9 sig
    10   datatype proof_method = Certificate of RealArith.pss_tree | Prover of string -> string
    10   datatype proof_method = Certificate of RealArith.pss_tree | Prover of string -> string
    11   val sos_tac: (RealArith.pss_tree -> unit) -> proof_method -> Proof.context -> int -> tactic
    11   val sos_tac: (RealArith.pss_tree -> unit) -> proof_method -> Proof.context -> int -> tactic
    12   val trace: bool Config.T
    12   val trace: bool Config.T
       
    13   val debug: bool Config.T
       
    14   val trace_message: Proof.context -> (unit -> string) -> unit
       
    15   val debug_message: Proof.context -> (unit -> string) -> unit
    13   exception Failure of string;
    16   exception Failure of string;
    14 end
    17 end
    15 
    18 
    16 structure Sum_of_Squares: SUM_OF_SQUARES =
    19 structure Sum_of_Squares: SUM_OF_SQUARES =
    17 struct
    20 struct
    50 
    53 
    51 val abs_rat = Rat.abs;
    54 val abs_rat = Rat.abs;
    52 val pow2 = rat_pow rat_2;
    55 val pow2 = rat_pow rat_2;
    53 val pow10 = rat_pow rat_10;
    56 val pow10 = rat_pow rat_10;
    54 
    57 
       
    58 
    55 val trace = Attrib.setup_config_bool @{binding sos_trace} (K false);
    59 val trace = Attrib.setup_config_bool @{binding sos_trace} (K false);
       
    60 val debug = Attrib.setup_config_bool @{binding sos_debug} (K false);
       
    61 
       
    62 fun trace_message ctxt msg =
       
    63   if Config.get ctxt trace orelse Config.get ctxt debug then tracing (msg ()) else ();
       
    64 fun debug_message ctxt msg = if Config.get ctxt debug then tracing (msg ()) else ();
    56 
    65 
    57 exception Sanity;
    66 exception Sanity;
    58 
    67 
    59 exception Unsolvable;
    68 exception Unsolvable;
    60 
    69 
   708     fun int_element (_, v) i = FuncUtil.Intfunc.tryapplyd v i rat_0
   717     fun int_element (_, v) i = FuncUtil.Intfunc.tryapplyd v i rat_0
   709 
   718 
   710     fun find_rounding d =
   719     fun find_rounding d =
   711       let
   720       let
   712         val _ =
   721         val _ =
   713           if Config.get ctxt trace
   722           debug_message ctxt (fn () => "Trying rounding with limit "^Rat.string_of_rat d ^ "\n")
   714           then writeln ("Trying rounding with limit "^Rat.string_of_rat d ^ "\n")
       
   715           else ()
       
   716         val vec = nice_vector d raw_vec
   723         val vec = nice_vector d raw_vec
   717         val blockmat =
   724         val blockmat =
   718           iter (1, dim vec)
   725           iter (1, dim vec)
   719             (fn i => fn a => bmatrix_add (bmatrix_cmul (int_element vec i) (nth mats i)) a)
   726             (fn i => fn a => bmatrix_add (bmatrix_cmul (int_element vec i) (nth mats i)) a)
   720             (bmatrix_neg (nth mats 0))
   727             (bmatrix_neg (nth mats 0))
   753   end
   760   end
   754 
   761 
   755 
   762 
   756 (* Iterative deepening.                                                      *)
   763 (* Iterative deepening.                                                      *)
   757 
   764 
   758 fun deepen f n =
   765 fun deepen ctxt f n =
   759   (writeln ("Searching with depth limit " ^ string_of_int n);
   766   (trace_message ctxt (fn () => "Searching with depth limit " ^ string_of_int n);
   760     (f n handle Failure s => (writeln ("failed with message: " ^ s); deepen f (n + 1))));
   767     (f n handle Failure s =>
       
   768       (trace_message ctxt (fn () => "failed with message: " ^ s); deepen ctxt f (n + 1))));
   761 
   769 
   762 
   770 
   763 (* Map back polynomials and their composites to a positivstellensatz.        *)
   771 (* Map back polynomials and their composites to a positivstellensatz.        *)
   764 
   772 
   765 fun cterm_of_sqterm (c, p) = RealArith.Product (RealArith.Rational_lt c, RealArith.Square p);
   773 fun cterm_of_sqterm (c, p) = RealArith.Product (RealArith.Rational_lt c, RealArith.Square p);
   837                         tryfind (fn i =>
   845                         tryfind (fn i =>
   838                             (d, i, real_positivnullstellensatz_general ctxt prover false d eq' leq
   846                             (d, i, real_positivnullstellensatz_general ctxt prover false d eq' leq
   839                               (poly_neg(poly_pow pol i))))
   847                               (poly_neg(poly_pow pol i))))
   840                           (0 upto k)
   848                           (0 upto k)
   841                       end
   849                       end
   842                     val (_,i,(cert_ideal,cert_cone)) = deepen tryall 0
   850                     val (_,i,(cert_ideal,cert_cone)) = deepen ctxt tryall 0
   843                     val proofs_ideal =
   851                     val proofs_ideal =
   844                       map2 (fn q => fn (_,ax) => RealArith.Eqmul(q,ax)) cert_ideal eq
   852                       map2 (fn q => fn (_,ax) => RealArith.Eqmul(q,ax)) cert_ideal eq
   845                     val proofs_cone = map cterm_of_sos cert_cone
   853                     val proofs_cone = map cterm_of_sos cert_cone
   846                     val proof_ne =
   854                     val proof_ne =
   847                       if null ltp then RealArith.Rational_lt Rat.one
   855                       if null ltp then RealArith.Rational_lt Rat.one