src/HOL/Real_Asymp/multiseries_expansion_bounds.ML
author haftmann
Tue, 21 Apr 2020 07:28:17 +0000
changeset 71788 ca3ac5238c41
parent 69597 ff784d5a5bfb
permissions -rw-r--r--
hooks for foundational terms: protection of foundational terms during simplification
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
68630
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
     1
signature MULTISERIES_EXPANSION =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
     2
sig
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
     3
include MULTISERIES_EXPANSION;
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
     4
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
     5
type lower_bound_thm = thm;
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
     6
type upper_bound_thm = thm;
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
     7
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
     8
datatype limit_result = Exact_Limit of term | Limit_Bounds of term option * term option
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
     9
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
    10
type lower_bound = expansion_thm * lower_bound_thm;
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
    11
type upper_bound = expansion_thm * upper_bound_thm;
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
    12
datatype bounds = 
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
    13
  Exact of expansion_thm | Bounds of lower_bound option * upper_bound option
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
    14
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
    15
val is_vacuous : bounds -> bool
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
    16
val lift_bounds : basis -> bounds -> bounds
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
    17
val get_expanded_fun_bounds : bounds -> term
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
    18
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
    19
val find_smaller_expansion :
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
    20
  Lazy_Eval.eval_ctxt -> thm * thm * Asymptotic_Basis.basis -> thm * thm * thm
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
    21
val find_greater_expansion :
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
    22
  Lazy_Eval.eval_ctxt -> thm * thm * Asymptotic_Basis.basis -> thm * thm * thm
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
    23
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
    24
val mult_expansion_bounds :
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
    25
  Lazy_Eval.eval_ctxt -> Asymptotic_Basis.basis -> bounds -> bounds -> bounds
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
    26
val powr_expansion_bounds :
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
    27
  Lazy_Eval.eval_ctxt -> Asymptotic_Basis.basis -> bounds -> bounds -> bounds * basis
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
    28
val powr_nat_expansion_bounds :
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
    29
  Lazy_Eval.eval_ctxt -> Asymptotic_Basis.basis -> bounds -> bounds -> bounds * basis
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
    30
val powr_const_expansion_bounds : Lazy_Eval.eval_ctxt -> bounds * term * basis -> bounds
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
    31
val power_expansion_bounds : Lazy_Eval.eval_ctxt -> bounds * term * basis -> bounds
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
    32
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
    33
val sgn_expansion_bounds : Lazy_Eval.eval_ctxt -> bounds * basis -> bounds
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
    34
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
    35
val expand_term_bounds : Lazy_Eval.eval_ctxt -> term -> basis -> bounds * basis
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
    36
val expand_terms_bounds : Lazy_Eval.eval_ctxt -> term list -> basis -> bounds list * basis
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
    37
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
    38
val prove_nhds_bounds : Lazy_Eval.eval_ctxt -> bounds * Asymptotic_Basis.basis -> thm
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
    39
val prove_at_infinity_bounds : Lazy_Eval.eval_ctxt -> bounds * Asymptotic_Basis.basis -> thm
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
    40
val prove_at_top_bounds : Lazy_Eval.eval_ctxt -> bounds * Asymptotic_Basis.basis -> thm
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
    41
val prove_at_bot_bounds : Lazy_Eval.eval_ctxt -> bounds * Asymptotic_Basis.basis -> thm
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
    42
val prove_at_0_bounds : Lazy_Eval.eval_ctxt -> bounds * Asymptotic_Basis.basis -> thm
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
    43
val prove_at_right_0_bounds : Lazy_Eval.eval_ctxt -> bounds * Asymptotic_Basis.basis -> thm
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
    44
val prove_at_left_0_bounds : Lazy_Eval.eval_ctxt -> bounds * Asymptotic_Basis.basis -> thm
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
    45
val prove_eventually_nonzero_bounds : Lazy_Eval.eval_ctxt -> bounds * Asymptotic_Basis.basis -> thm
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
    46
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
    47
val prove_eventually_less_bounds :
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
    48
  Lazy_Eval.eval_ctxt -> bounds * bounds * Asymptotic_Basis.basis -> thm
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
    49
val prove_eventually_greater_bounds :
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
    50
  Lazy_Eval.eval_ctxt -> bounds * bounds * Asymptotic_Basis.basis -> thm
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
    51
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
    52
val prove_smallo_bounds :
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
    53
  Lazy_Eval.eval_ctxt -> bounds * bounds * Asymptotic_Basis.basis -> thm
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
    54
val prove_bigo_bounds :
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
    55
  Lazy_Eval.eval_ctxt -> bounds * bounds * Asymptotic_Basis.basis -> thm
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
    56
val prove_bigtheta_bounds :
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
    57
  Lazy_Eval.eval_ctxt -> bounds * bounds * Asymptotic_Basis.basis -> thm
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
    58
val prove_asymp_equiv_bounds :
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
    59
  Lazy_Eval.eval_ctxt -> bounds * bounds * Asymptotic_Basis.basis -> thm
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
    60
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
    61
val limit_of_expansion_bounds :
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
    62
  Lazy_Eval.eval_ctxt -> bounds * Asymptotic_Basis.basis -> limit_result
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
    63
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
    64
end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
    65
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
    66
structure Multiseries_Expansion : MULTISERIES_EXPANSION =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
    67
struct
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
    68
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
    69
open Multiseries_Expansion;
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
    70
open Exp_Log_Expression;
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
    71
open Asymptotic_Basis;
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
    72
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
    73
type lower_bound_thm = thm;
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
    74
type upper_bound_thm = thm;
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
    75
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
    76
datatype limit_result = Exact_Limit of term | Limit_Bounds of term option * term option
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
    77
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
    78
type lower_bound = expansion_thm * lower_bound_thm;
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
    79
type upper_bound = expansion_thm * upper_bound_thm;
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
    80
datatype bounds = 
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
    81
  Exact of expansion_thm | Bounds of lower_bound option * upper_bound option
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
    82
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
    83
fun is_vacuous (Bounds (NONE, NONE)) = true
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
    84
  | is_vacuous _ = false
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
    85
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
    86
fun mk_const_expansion ectxt basis c =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
    87
  let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
    88
    val ctxt = Lazy_Eval.get_ctxt ectxt
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
    89
    val thm = Drule.infer_instantiate' ctxt [NONE, SOME (Thm.cterm_of ctxt c)] 
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
    90
                @{thm expands_to_const}
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
    91
  in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
    92
    thm OF [get_basis_wf_thm basis, mk_expansion_level_eq_thm basis]
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
    93
  end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
    94
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 68630
diff changeset
    95
fun dest_eventually (Const (\<^const_name>\<open>Filter.eventually\<close>, _) $ p $ f) = (p, f)
68630
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
    96
  | dest_eventually t = raise TERM ("dest_eventually", [t])
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
    97
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
    98
fun dest_binop (f $ a $ b) = (f, a, b)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
    99
  | dest_binop t = raise TERM ("dest_binop", [t])
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   100
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   101
fun get_lbound_from_thm thm =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   102
  thm |> Thm.concl_of |> HOLogic.dest_Trueprop |> dest_eventually |> fst
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   103
  |> strip_abs |> snd |> dest_binop |> #2
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   104
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   105
fun get_ubound_from_thm thm =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   106
  thm |> Thm.concl_of |> HOLogic.dest_Trueprop |> dest_eventually |> fst
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   107
  |> strip_abs |> snd |> dest_binop |> #3
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   108
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   109
fun transfer_bounds eq_thm (Exact thm) = 
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   110
      Exact (@{thm expands_to_meta_eq_cong'} OF [thm, eq_thm])
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   111
  | transfer_bounds eq_thm (Bounds (lb, ub)) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   112
      Bounds 
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   113
        (Option.map (apsnd (fn thm => @{thm transfer_lower_bound} OF [thm, eq_thm])) lb,
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   114
         Option.map (apsnd (fn thm => @{thm transfer_upper_bound} OF [thm, eq_thm])) ub)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   115
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 68630
diff changeset
   116
fun dest_le (\<^term>\<open>(<=) :: real => _\<close> $ l $ r) = (l, r)
68630
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   117
  | dest_le t = raise TERM ("dest_le", [t])
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   118
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   119
fun abconv (t, t') = Envir.beta_eta_contract t aconv Envir.beta_eta_contract t'
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   120
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 68630
diff changeset
   121
val realT = \<^typ>\<open>Real.real\<close>
68630
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   122
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   123
fun check_bounds e (Exact thm) = let val _ = check_expansion e thm in Exact thm end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   124
  | check_bounds e (Bounds bnds) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   125
      let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   126
        fun msg lower = if lower then "check_lower_bound" else "check_upper_bound"
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   127
        fun check lower (exp_thm, le_thm) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   128
          let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   129
            val (expanded_fun, bound_fun) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   130
              le_thm |> Thm.concl_of |> HOLogic.dest_Trueprop |> dest_eventually |> fst
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   131
                |> strip_abs |> snd |> dest_le |> (if lower then swap else I) 
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   132
                |> apply2 (fn t => Abs ("x", realT, t))
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   133
          in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   134
            if not (abconv (get_expanded_fun exp_thm, bound_fun)) then
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   135
              raise TERM (msg lower, map Thm.concl_of [exp_thm, le_thm])
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   136
            else if not (abconv (expr_to_term e, expanded_fun)) then
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   137
              raise TERM (msg lower, [expr_to_term e, Thm.concl_of le_thm])
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   138
            else
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   139
              ()
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   140
          end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   141
        val _ = (Option.map (check true) (fst bnds), Option.map (check false) (snd bnds))
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   142
      in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   143
        Bounds bnds
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   144
      end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   145
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   146
fun cong_bounds eq_thm (Exact thm) = 
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   147
      Exact (@{thm expands_to_cong_reverse} OF [eq_thm, thm])
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   148
  | cong_bounds eq_thm (Bounds (lb, ub)) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   149
      Bounds 
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   150
        (Option.map (apsnd (fn thm => @{thm lower_bound_cong} OF [eq_thm, thm])) lb,
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   151
         Option.map (apsnd (fn thm => @{thm upper_bound_cong} OF [eq_thm, thm])) ub)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   152
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   153
fun mk_trivial_bounds ectxt f thm basis =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   154
  let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   155
    val eq_thm = Thm.reflexive (Thm.cterm_of (Lazy_Eval.get_ctxt ectxt) f)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   156
    val lb_thm = @{thm trivial_boundsI(1)} OF [thm, eq_thm]
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   157
    val ub_thm = @{thm trivial_boundsI(2)} OF [thm, eq_thm]
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   158
    val lb = get_lbound_from_thm lb_thm
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   159
    val ub = get_ubound_from_thm ub_thm
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   160
    val (lthm, uthm) = apply2 (mk_const_expansion ectxt basis) (lb, ub)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   161
  in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   162
    Bounds (SOME (lthm, lb_thm), SOME (uthm, ub_thm))
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   163
  end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   164
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   165
fun get_basis_size basis = length (get_basis_list basis)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   166
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   167
fun trim_expansion_while_pos ectxt (thm, basis) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   168
  let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   169
    val n = get_basis_size basis
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 68630
diff changeset
   170
    val es = SOME (replicate n \<^term>\<open>0 :: real\<close>)
68630
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   171
  in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   172
    trim_expansion_while_greater false es false NONE ectxt (thm, basis)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   173
  end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   174
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   175
fun lift_bounds basis (Exact thm) = Exact (lift basis thm)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   176
  | lift_bounds basis (Bounds bnds) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   177
      bnds |> apply2 (Option.map (apfst (lift basis))) |> Bounds 
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   178
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   179
fun mono_bound mono_thm thm = @{thm mono_bound} OF [mono_thm, thm]
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   180
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   181
fun get_lower_bound (Bounds (lb, _)) = lb
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   182
  | get_lower_bound (Exact thm) = SOME (thm, thm RS @{thm exact_to_bound})
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   183
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   184
fun get_upper_bound (Bounds (_, ub)) = ub
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   185
  | get_upper_bound (Exact thm) = SOME (thm, thm RS @{thm exact_to_bound})      
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   186
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   187
fun get_expanded_fun_bounds_aux f (_, thm) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   188
  let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   189
    val t = thm |> Thm.concl_of |> HOLogic.dest_Trueprop |> dest_comb |> fst |> dest_comb |> snd
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   190
  in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   191
    case Envir.eta_long [] t of
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 68630
diff changeset
   192
      Abs (x, T, \<^term>\<open>(<=) :: real => _\<close> $ lhs $ rhs) => Abs (x, T, f (lhs, rhs))
68630
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   193
    | _ => raise THM ("get_expanded_fun_bounds", 0, [thm])
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   194
  end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   195
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   196
fun get_expanded_fun_bounds (Exact thm) = get_expanded_fun thm
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   197
  | get_expanded_fun_bounds (Bounds (NONE, NONE)) = raise TERM ("get_expanded_fun_bounds", [])
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   198
  | get_expanded_fun_bounds (Bounds (SOME l, _)) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   199
      get_expanded_fun_bounds_aux snd l
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   200
  | get_expanded_fun_bounds (Bounds (_, SOME u)) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   201
      get_expanded_fun_bounds_aux fst u
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   202
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   203
fun expand_add_bounds _ [thm, _] (Exact thm1, Exact thm2) basis =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   204
      Exact (thm OF [get_basis_wf_thm basis, thm1, thm2])
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   205
  | expand_add_bounds swap [thm1, thm2] bnds basis =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   206
      let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   207
        fun comb (SOME (a, b), SOME (c, d)) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   208
              SOME (thm1 OF [get_basis_wf_thm basis, a, c], thm2 OF [b, d])
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   209
          | comb _ = NONE
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   210
        val ((a, b), (c, d)) = (apply2 get_lower_bound bnds, apply2 get_upper_bound bnds)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   211
      in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   212
        if swap then
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   213
          Bounds (comb (a, d), comb (c, b))
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   214
        else
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   215
          Bounds (comb (a, b), comb (c, d))
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   216
      end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   217
  | expand_add_bounds _ _ _ _ = raise Match
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   218
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   219
fun mk_refl_thm ectxt t =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   220
  let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   221
    val ctxt = Lazy_Eval.get_ctxt ectxt
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   222
    val ct = Thm.cterm_of ctxt t
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   223
  in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   224
    Drule.infer_instantiate' ctxt [SOME ct] @{thm eventually_le_self}
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   225
  end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   226
  
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   227
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   228
fun find_smaller_expansion ectxt (thm1, thm2, basis) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   229
      case compare_expansions ectxt (thm1, thm2, basis) of
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   230
        (LESS, less_thm, thm1, _) =>
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   231
          (thm1, mk_refl_thm ectxt (get_expanded_fun thm1), less_thm RS @{thm eventually_less_imp_le})
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   232
      | (GREATER, gt_thm, _, thm2) =>
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   233
          (thm2, gt_thm RS @{thm eventually_less_imp_le}, mk_refl_thm ectxt (get_expanded_fun thm2))
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   234
      | (EQUAL, eq_thm, thm1, _) =>
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   235
          (thm1, mk_refl_thm ectxt (get_expanded_fun thm1), eq_thm RS @{thm eventually_eq_imp_le})
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   236
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   237
fun find_greater_expansion ectxt (thm1, thm2, basis) =
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 68630
diff changeset
   238
      case compare_expansions ectxt (\<^print> (thm1, thm2, basis)) of
68630
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   239
        (LESS, less_thm, _, thm2) =>
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   240
          (thm2, less_thm RS @{thm eventually_less_imp_le}, mk_refl_thm ectxt (get_expanded_fun thm2))
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   241
      | (GREATER, gt_thm, thm1, _) =>
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   242
          (thm1, mk_refl_thm ectxt (get_expanded_fun thm1), gt_thm RS @{thm eventually_less_imp_le})
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   243
      | (EQUAL, eq_thm, thm1, _) =>
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   244
          (thm1, mk_refl_thm ectxt (get_expanded_fun thm1), eq_thm RS @{thm eventually_eq_imp_ge})
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   245
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   246
fun determine_sign ectxt (thm, basis) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   247
  case ev_zeroness_oracle ectxt (get_expanded_fun thm) of
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   248
    SOME zero_thm => (thm, zero_thm, (true, true))
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   249
  | NONE => (
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   250
      case trim_expansion true (SOME Sgn_Trim) ectxt (thm, basis) of
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   251
        (thm, IsPos, SOME pos_thm) =>
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   252
          (thm, @{thm expands_to_imp_eventually_pos} OF [get_basis_wf_thm basis, thm, pos_thm],
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   253
            (false, true))
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   254
      | (thm, IsNeg, SOME neg_thm) =>
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   255
          (thm, @{thm expands_to_imp_eventually_neg} OF [get_basis_wf_thm basis, thm, neg_thm],
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   256
            (true, false))
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   257
      | _ => raise TERM ("Unexpected zeroness result in determine_sign", []))
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   258
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   259
val mult_bounds_thms = @{thms mult_bounds_real[eventuallized]}
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   260
fun mult_bounds_thm n = List.nth (mult_bounds_thms, n)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   261
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   262
val powr_bounds_thms = @{thms powr_bounds_real[eventuallized]}
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   263
fun powr_bounds_thm n = List.nth (powr_bounds_thms, n)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   264
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   265
fun mk_nonstrict_thm [thm1, thm2] sgn_thm = (
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   266
      case Thm.concl_of sgn_thm |> HOLogic.dest_Trueprop of
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 68630
diff changeset
   267
        Const (\<^const_name>\<open>Filter.eventually\<close>, _) $ t $ _ => (
68630
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   268
          case Envir.eta_long [] t of
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 68630
diff changeset
   269
            Abs (_, _, Const (\<^const_name>\<open>HOL.eq\<close>, _) $ _ $ _) => sgn_thm RS thm1
68630
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   270
          | _ => sgn_thm RS thm2)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   271
      | _ => sgn_thm RS thm2)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   272
  | mk_nonstrict_thm _ _ = raise Match    
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   273
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   274
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   275
val mk_nonneg_thm = mk_nonstrict_thm @{thms eventually_eq_imp_ge eventually_less_imp_le}
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   276
val mk_nonpos_thm = mk_nonstrict_thm @{thms eventually_eq_imp_le eventually_less_imp_le}
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   277
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   278
fun mult_expansion_bounds_right basis
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   279
      ((l1_thm, l1_le_thm), (u1_thm, u1_ge_thm)) (thm2, sgn_thm, sgns) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   280
  let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   281
    val in_bounds_thm = @{thm eventually_atLeastAtMostI} OF [l1_le_thm, u1_ge_thm]
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   282
    fun mult_expansions (thm1, thm2) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   283
      @{thm expands_to_mult} OF [get_basis_wf_thm basis, thm1, thm2]
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   284
  in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   285
    if snd sgns then
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   286
      (mult_expansions (l1_thm, thm2), mult_expansions (u1_thm, thm2),
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   287
        @{thm mult_right_bounds(1)[eventuallized]} OF [in_bounds_thm, mk_nonneg_thm sgn_thm])
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   288
    else
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   289
      (mult_expansions (u1_thm, thm2), mult_expansions (l1_thm, thm2),
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   290
        @{thm mult_right_bounds(2)[eventuallized]} OF [in_bounds_thm, mk_nonpos_thm sgn_thm])
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   291
  end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   292
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   293
fun mult_expansion_bounds_left basis
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   294
      (thm1, sgn_thm, sgns) ((l2_thm, l2_le_thm), (u2_thm, u2_ge_thm)) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   295
  let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   296
    val in_bounds_thm = @{thm eventually_atLeastAtMostI} OF [l2_le_thm, u2_ge_thm]
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   297
    fun mult_expansions (thm1, thm2) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   298
      @{thm expands_to_mult} OF [get_basis_wf_thm basis, thm1, thm2]
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   299
  in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   300
    if snd sgns then
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   301
      (mult_expansions (thm1, l2_thm), mult_expansions (thm1, u2_thm),
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   302
        @{thm mult_left_bounds(1)[eventuallized]} OF [in_bounds_thm, mk_nonneg_thm sgn_thm])
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   303
    else
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   304
      (mult_expansions (thm1, u2_thm), mult_expansions (thm1, l2_thm),
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   305
        @{thm mult_left_bounds(2)[eventuallized]} OF [in_bounds_thm, mk_nonpos_thm sgn_thm])
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   306
  end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   307
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   308
fun mult_expansion_bounds_2 ectxt basis
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   309
     ((l1, l1_le_thm, l1sgn_thm, l1_sgn), (u1, u1_ge_thm, u1sgn_thm, u1_sgn))
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   310
     ((l2, l2_le_thm, l2sgn_thm, l2_sgn), (u2, u2_ge_thm, u2sgn_thm, u2_sgn)) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   311
  let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   312
    val sgns = (l1_sgn, u1_sgn, l2_sgn, u2_sgn)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   313
    val in_bounds_thms =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   314
      map (fn thms => @{thm eventually_atLeastAtMostI} OF thms)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   315
        [[l1_le_thm, u1_ge_thm], [l2_le_thm, u2_ge_thm]]
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   316
    fun mult_expansions (thm1, thm2) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   317
      @{thm expands_to_mult} OF [get_basis_wf_thm basis, thm1, thm2]
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   318
  in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   319
    case sgns of
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   320
      ((_, true), _, (_, true), _) =>
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   321
        (mult_expansions (l1, l2), mult_expansions (u1, u2),
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   322
          mult_bounds_thm 0 OF ([mk_nonneg_thm l1sgn_thm, mk_nonneg_thm l2sgn_thm] @ in_bounds_thms))
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   323
    | (_, (true, _), (_, true), _) =>
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   324
        (mult_expansions (l1, u2), mult_expansions (u1, l2),
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   325
          mult_bounds_thm 1 OF ([mk_nonpos_thm u1sgn_thm, mk_nonneg_thm l2sgn_thm] @ in_bounds_thms))
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   326
    | ((_, true), _, _, (true, _)) =>
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   327
        (mult_expansions (u1, l2), mult_expansions (l1, u2),
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   328
          mult_bounds_thm 2 OF ([mk_nonneg_thm l1sgn_thm, mk_nonpos_thm u2sgn_thm] @ in_bounds_thms))
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   329
    | (_, (true, _), _, (true, _)) =>
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   330
        (mult_expansions (u1, u2), mult_expansions (l1, l2),
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   331
        mult_bounds_thm 3 OF ([mk_nonpos_thm u1sgn_thm, mk_nonpos_thm u2sgn_thm] @ in_bounds_thms))
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   332
    | ((true, _), (_, true), (_, true), _) =>
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   333
        (mult_expansions (l1, u2), mult_expansions (u1, u2),
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   334
        mult_bounds_thm 4 OF ([mk_nonpos_thm l1sgn_thm, mk_nonneg_thm u1sgn_thm, mk_nonneg_thm l2sgn_thm] @ in_bounds_thms))
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   335
    | ((true, _), (_, true), _, (true, _)) =>
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   336
        (mult_expansions (u1, l2), mult_expansions (l1, l2),
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   337
        mult_bounds_thm 5 OF ([mk_nonpos_thm l1sgn_thm, mk_nonneg_thm u1sgn_thm, mk_nonpos_thm u2sgn_thm] @ in_bounds_thms))
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   338
    | ((_, true), _, (true, _), (_, true)) =>
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   339
        (mult_expansions (u1, l2), mult_expansions (u1, u2),
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   340
        mult_bounds_thm 6 OF ([mk_nonneg_thm l1sgn_thm, mk_nonpos_thm l2sgn_thm, mk_nonneg_thm u2sgn_thm] @ in_bounds_thms))
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   341
    | (_, (true, _), (true, _), (_, true)) =>
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   342
        (mult_expansions (l1, u2), mult_expansions (l1, l2),
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   343
        mult_bounds_thm 7 OF ([mk_nonpos_thm u1sgn_thm, mk_nonpos_thm l2sgn_thm, mk_nonneg_thm u2sgn_thm] @ in_bounds_thms))
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   344
    | ((true, _), (_, true), (true, _), (_, true)) =>
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   345
        let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   346
          val l1u2 = mult_expansions (l1, u2)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   347
          val u1l2 = mult_expansions (u1, l2)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   348
          val l1l2 = mult_expansions (l1, l2)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   349
          val u1u2 = mult_expansions (u1, u2)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   350
          val (l, lthm1, lthm2) = find_smaller_expansion ectxt (l1u2, u1l2, basis)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   351
          val (u, uthm1, uthm2) = find_greater_expansion ectxt (l1l2, u1u2, basis)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   352
        in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   353
          (l, u, mult_bounds_thm 8 OF
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   354
            ([mk_nonpos_thm l1sgn_thm, mk_nonneg_thm u1sgn_thm, mk_nonpos_thm l2sgn_thm, 
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   355
                mk_nonneg_thm u2sgn_thm, lthm1, lthm2, uthm1, uthm2] @ in_bounds_thms))
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   356
        end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   357
        
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   358
    | _ => raise Match
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   359
  end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   360
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   361
fun convert_bounds (lthm, uthm, in_bounds_thm) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   362
  Bounds (SOME (lthm, in_bounds_thm RS @{thm eventually_atLeastAtMostD(1)}),
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   363
    SOME (uthm, in_bounds_thm RS @{thm eventually_atLeastAtMostD(2)}))
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   364
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   365
fun convert_bounds' (Exact thm) = (thm, thm, thm RS @{thm expands_to_trivial_bounds})
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   366
  | convert_bounds' (Bounds (SOME (lthm, ge_thm), SOME (uthm, le_thm))) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   367
      (lthm, uthm, @{thm eventually_in_atLeastAtMostI} OF [ge_thm, le_thm])
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   368
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   369
fun mult_expansion_bounds _ basis (Exact thm1) (Exact thm2) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   370
      Exact (@{thm expands_to_mult} OF [get_basis_wf_thm basis, thm1, thm2])
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   371
  | mult_expansion_bounds ectxt basis (Bounds (SOME l1, SOME u1)) (Exact thm2) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   372
      mult_expansion_bounds_right basis (l1, u1) (determine_sign ectxt (thm2, basis))
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   373
      |> convert_bounds
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   374
  | mult_expansion_bounds ectxt basis (Exact thm1) (Bounds (SOME l2, SOME u2)) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   375
      mult_expansion_bounds_left basis (determine_sign ectxt (thm1, basis)) (l2, u2)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   376
      |> convert_bounds
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   377
  | mult_expansion_bounds ectxt basis (Bounds (SOME l1, SOME u1)) (Bounds (SOME l2, SOME u2)) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   378
      let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   379
        fun prep (thm, le_thm) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   380
          case determine_sign ectxt (thm, basis) of
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   381
            (thm, sgn_thm, sgns) => (thm, le_thm, sgn_thm, sgns)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   382
      in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   383
        mult_expansion_bounds_2 ectxt basis (prep l1, prep u1) (prep l2, prep u2)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   384
        |> convert_bounds
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   385
      end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   386
  | mult_expansion_bounds _ _ _ _ = Bounds (NONE, NONE)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   387
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   388
fun inverse_expansion ectxt basis thm = 
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   389
      case trim_expansion true (SOME Simple_Trim) ectxt (thm, basis) of
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   390
        (thm, _, SOME trimmed_thm) =>
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   391
          @{thm expands_to_inverse} OF [trimmed_thm, get_basis_wf_thm basis, thm]
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   392
      | _ => raise TERM ("zero denominator", [get_expanded_fun thm])
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   393
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   394
fun divide_expansion ectxt basis thm1 thm2 =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   395
      case trim_expansion true (SOME Simple_Trim) ectxt (thm2, basis) of
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   396
        (thm2, _, SOME trimmed_thm) =>
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   397
          @{thm expands_to_divide} OF [trimmed_thm, get_basis_wf_thm basis, thm1, thm2]
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   398
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   399
fun forget_trimmedness_sign trimmed_thm =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   400
  case Thm.concl_of trimmed_thm |> HOLogic.dest_Trueprop of
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 68630
diff changeset
   401
    Const (\<^const_name>\<open>Multiseries_Expansion.trimmed\<close>, _) $ _ => trimmed_thm
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 68630
diff changeset
   402
  | Const (\<^const_name>\<open>Multiseries_Expansion.trimmed_pos\<close>, _) $ _ =>
68630
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   403
      trimmed_thm RS @{thm trimmed_pos_imp_trimmed}
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 68630
diff changeset
   404
  | Const (\<^const_name>\<open>Multiseries_Expansion.trimmed_neg\<close>, _) $ _ =>
68630
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   405
      trimmed_thm RS @{thm trimmed_neg_imp_trimmed}
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   406
  | _ => raise THM ("forget_trimmedness_sign", 0, [trimmed_thm])
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   407
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   408
fun inverse_expansion_bounds ectxt basis (Exact thm) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   409
      Exact (inverse_expansion ectxt basis thm)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   410
  | inverse_expansion_bounds ectxt basis (Bounds (SOME (lthm, le_thm), SOME (uthm, ge_thm))) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   411
      let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   412
        fun trim thm = trim_expansion true (SOME Sgn_Trim) ectxt (thm, basis)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   413
        fun inverse thm trimmed_thm = @{thm expands_to_inverse} OF
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   414
          [forget_trimmedness_sign trimmed_thm, get_basis_wf_thm basis, thm]
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   415
      in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   416
        case (trim lthm, trim uthm) of
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   417
          ((lthm, IsPos, SOME trimmed_thm1), (uthm, _, SOME trimmed_thm2)) =>
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   418
            (inverse uthm trimmed_thm2, inverse lthm trimmed_thm1,
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   419
               @{thm inverse_bounds_real(1)[eventuallized, OF expands_to_imp_eventually_pos]} OF
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   420
                 [get_basis_wf_thm basis, lthm, trimmed_thm1, le_thm, ge_thm]) |> convert_bounds
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   421
        | ((lthm, _, SOME trimmed_thm1), (uthm, IsNeg, SOME trimmed_thm2)) =>
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   422
            (inverse uthm trimmed_thm2, inverse lthm trimmed_thm1,
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   423
               @{thm inverse_bounds_real(2)[eventuallized, OF expands_to_imp_eventually_neg]} OF
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   424
                 [get_basis_wf_thm basis, uthm, trimmed_thm2, le_thm, ge_thm]) |> convert_bounds
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   425
        | _ => raise TERM ("zero denominator", map get_expanded_fun [lthm, uthm])
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   426
      end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   427
  | inverse_expansion_bounds _ _ _ = Bounds (NONE, NONE)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   428
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   429
fun trimmed_thm_to_inverse_sgn_thm basis thm trimmed_thm =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   430
  case trimmed_thm |> Thm.concl_of |> HOLogic.dest_Trueprop of
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 68630
diff changeset
   431
    Const (\<^const_name>\<open>Multiseries_Expansion.trimmed_pos\<close>, _) $ _ =>
68630
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   432
      @{thm pos_imp_inverse_pos[eventuallized, OF expands_to_imp_eventually_pos]} OF
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   433
        [get_basis_wf_thm basis, thm, trimmed_thm]
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 68630
diff changeset
   434
  | Const (\<^const_name>\<open>Multiseries_Expansion.trimmed_neg\<close>, _) $ _ =>
68630
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   435
      @{thm neg_imp_inverse_neg[eventuallized, OF expands_to_imp_eventually_neg]} OF
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   436
        [get_basis_wf_thm basis, thm, trimmed_thm]
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   437
  | _ => raise THM ("trimmed_thm_to_inverse_sgn_thm", 0, [trimmed_thm])
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   438
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   439
fun transfer_divide_bounds (lthm, uthm, in_bounds_thm) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   440
  let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   441
    val f = Conv.fconv_rule (Conv.try_conv (Conv.rewr_conv @{thm transfer_divide_bounds(4)}))
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   442
    val conv = Conv.repeat_conv (Conv.rewrs_conv @{thms transfer_divide_bounds(1-3)})
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   443
  in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   444
    (f lthm, f uthm, Conv.fconv_rule conv in_bounds_thm)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   445
  end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   446
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   447
fun divide_expansion_bounds ectxt basis (Exact thm1) (Exact thm2) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   448
      Exact (divide_expansion ectxt basis thm1 thm2)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   449
  | divide_expansion_bounds ectxt basis (Bounds (SOME l1, SOME u1)) (Exact thm2) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   450
      let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   451
        val (thm2, sgn, SOME trimmed_thm) = trim_expansion true (SOME Sgn_Trim) ectxt (thm2, basis)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   452
        val thm2' = @{thm expands_to_inverse} OF 
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   453
          [forget_trimmedness_sign trimmed_thm, get_basis_wf_thm basis, thm2]
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   454
        val sgn_thm = trimmed_thm_to_inverse_sgn_thm basis thm2 trimmed_thm
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   455
      in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   456
        mult_expansion_bounds_right basis (l1, u1) (thm2', sgn_thm, (sgn = IsNeg, sgn = IsPos))
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   457
        |> transfer_divide_bounds
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   458
        |> convert_bounds
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   459
      end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   460
  | divide_expansion_bounds ectxt basis bnds1 (Bounds (SOME (lthm, ge_thm), SOME (uthm, le_thm))) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   461
      let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   462
        fun trim thm =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   463
          case trim_expansion true (SOME Sgn_Trim) ectxt (thm, basis) of
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   464
            (thm, sgn, SOME trimmed_thm) => (thm, sgn, trimmed_thm)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   465
          | _ => raise TERM ("zero divisor", [get_expanded_fun thm])
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   466
        fun inverse thm trimmed_thm = @{thm expands_to_inverse} OF
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   467
          [forget_trimmedness_sign trimmed_thm, get_basis_wf_thm basis, thm]
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   468
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   469
        val (lthm, sgnl, ltrimmed_thm) = trim lthm
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   470
        val (uthm, sgnu, utrimmed_thm) = trim uthm
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   471
        val (uthm', lthm') = (inverse lthm ltrimmed_thm, inverse uthm utrimmed_thm)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   472
        val in_bounds_thm' =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   473
          if sgnl = IsPos then
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   474
            @{thm inverse_bounds_real(1)[eventuallized, OF expands_to_imp_eventually_pos]} OF
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   475
                 [get_basis_wf_thm basis, lthm, ltrimmed_thm, ge_thm, le_thm]
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   476
          else if sgnu = IsNeg then
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   477
            @{thm inverse_bounds_real(2)[eventuallized, OF expands_to_imp_eventually_neg]} OF
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   478
                 [get_basis_wf_thm basis, uthm, utrimmed_thm, ge_thm, le_thm]
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   479
          else
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   480
            raise TERM ("zero divisor", map get_expanded_fun [lthm, uthm])
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   481
        val [ge_thm', le_thm'] =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   482
          map (fn thm => in_bounds_thm' RS thm) @{thms eventually_atLeastAtMostD}
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   483
        val bnds2' = ((lthm', ge_thm'), (uthm', le_thm'))
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   484
        val usgn_thm' = trimmed_thm_to_inverse_sgn_thm basis lthm ltrimmed_thm
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   485
        val lsgn_thm' = trimmed_thm_to_inverse_sgn_thm basis uthm utrimmed_thm
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   486
      in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   487
        case bnds1 of
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   488
          Exact thm1 =>
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   489
            (mult_expansion_bounds_left basis (determine_sign ectxt (thm1, basis)) bnds2')
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   490
            |> transfer_divide_bounds
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   491
            |> convert_bounds
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   492
        | Bounds (SOME l1, SOME u1) =>
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   493
            let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   494
              fun prep (thm, le_thm) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   495
                case determine_sign ectxt (thm, basis) of
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   496
                  (thm, sgn_thm, sgns) => (thm, le_thm, sgn_thm, sgns)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   497
            in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   498
              mult_expansion_bounds_2 ectxt basis (prep l1, prep u1)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   499
                ((lthm', ge_thm', lsgn_thm', (sgnl = IsNeg, sgnl = IsPos)),
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   500
                 (uthm', le_thm', usgn_thm', (sgnu = IsNeg, sgnu = IsPos)))
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   501
              |> transfer_divide_bounds
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   502
              |> convert_bounds
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   503
            end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   504
        | _ => Bounds (NONE, NONE)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   505
      end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   506
  | divide_expansion_bounds _ _ _ _ = Bounds (NONE, NONE)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   507
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   508
fun abs_expansion ectxt basis thm =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   509
      let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   510
        val (thm, nz, SOME trimmed_thm) = trim_expansion true (SOME Sgn_Trim) ectxt (thm, basis)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   511
        val thm' =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   512
          case nz of 
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   513
            IsPos => @{thm expands_to_abs_pos} 
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   514
          | IsNeg => @{thm expands_to_abs_neg}
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   515
          | _ => raise TERM ("Unexpected trim result during expansion of abs", [])
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   516
      in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   517
        thm' OF [trimmed_thm, get_basis_wf_thm basis, thm]
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   518
      end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   519
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   520
fun abs_trivial_bounds ectxt f =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   521
  let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   522
    val ctxt = Lazy_Eval.get_ctxt ectxt
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   523
  in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   524
    Drule.infer_instantiate' ctxt [SOME (Thm.cterm_of ctxt f)] @{thm eventually_abs_ge_0}
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   525
  end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   526
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   527
fun abs_expansion_bounds ectxt basis (Exact thm) = Exact (abs_expansion ectxt basis thm)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   528
  | abs_expansion_bounds ectxt basis (bnds as Bounds (SOME (lthm, ge_thm), NONE)) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   529
      let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   530
        val (lthm, lsgn_thm, lsgns) = determine_sign ectxt (lthm, basis)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   531
        val lbnd =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   532
          if snd lsgns then
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   533
            (lthm, @{thm eventually_le_abs_nonneg} OF [mk_nonneg_thm lsgn_thm, ge_thm])
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   534
          else
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   535
            (zero_expansion basis, abs_trivial_bounds ectxt (get_expanded_fun_bounds bnds))
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   536
      in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   537
        Bounds (SOME lbnd, NONE)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   538
      end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   539
  | abs_expansion_bounds ectxt basis (bnds as Bounds (NONE, SOME (uthm, le_thm))) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   540
      let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   541
        val (uthm, usgn_thm, usgns) = determine_sign ectxt (uthm, basis)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   542
        val lbnd =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   543
          if fst usgns then
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   544
            (@{thm expands_to_uminus} OF [get_basis_wf_thm basis, uthm],
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   545
               @{thm eventually_le_abs_nonpos} OF [mk_nonpos_thm usgn_thm, le_thm])
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   546
          else
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   547
            (zero_expansion basis, abs_trivial_bounds ectxt (get_expanded_fun_bounds bnds))
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   548
      in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   549
        Bounds (SOME lbnd, NONE)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   550
      end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   551
  | abs_expansion_bounds ectxt basis (Bounds (SOME (lthm, ge_thm), SOME (uthm, le_thm))) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   552
      let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   553
        val in_bounds_thm = @{thm eventually_atLeastAtMostI} OF [ge_thm, le_thm]
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   554
        val (lthm, lsgn_thm, lsgns) = determine_sign ectxt (lthm, basis)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   555
        val (uthm, usgn_thm, usgns) = determine_sign ectxt (uthm, basis)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   556
        fun minus thm = @{thm expands_to_uminus} OF [get_basis_wf_thm basis, thm]
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   557
      in (
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   558
        case (lsgns, usgns) of
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   559
          ((_, true), _) =>
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   560
            (lthm, uthm,
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   561
               @{thm nonneg_abs_bounds[eventuallized]} OF [mk_nonneg_thm lsgn_thm, in_bounds_thm])
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   562
        | (_, (true, _)) =>
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   563
            (minus uthm, minus lthm,
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   564
               @{thm nonpos_abs_bounds[eventuallized]} OF [mk_nonpos_thm usgn_thm, in_bounds_thm])
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   565
        | ((true, _), (_, true)) => (
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   566
            case find_greater_expansion ectxt (minus lthm, uthm, basis) of
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   567
              (u'_thm, le_thm1, le_thm2) =>
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 68630
diff changeset
   568
                (mk_const_expansion ectxt basis \<^term>\<open>0::real\<close>, u'_thm,
68630
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   569
                  @{thm indet_abs_bounds[eventuallized]} OF 
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   570
                    [mk_nonpos_thm lsgn_thm, mk_nonneg_thm usgn_thm, 
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   571
                       in_bounds_thm, le_thm1, le_thm2]))
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   572
        | _ => raise TERM ("Unexpected zeroness result in abs_expansion_bounds", []))
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   573
        |> convert_bounds
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   574
      end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   575
  | abs_expansion_bounds _ _ _ = Bounds (NONE, NONE) (* TODO *)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   576
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   577
fun abs_expansion_lower_bound ectxt basis (Exact thm) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   578
      let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   579
        val thm' = abs_expansion ectxt basis thm
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   580
      in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   581
        (thm', thm RS @{thm expands_to_abs_nonneg}, thm' RS @{thm exact_to_bound})
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   582
      end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   583
  | abs_expansion_lower_bound ectxt basis (Bounds (SOME (lthm, ge_thm), SOME (uthm, le_thm))) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   584
      let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   585
        val in_bounds_thm = @{thm eventually_atLeastAtMostI} OF [ge_thm, le_thm]
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   586
        val (lthm, lsgn_thm, lsgns) = determine_sign ectxt (lthm, basis)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   587
        val (uthm, usgn_thm, usgns) = determine_sign ectxt (uthm, basis)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   588
        fun minus thm = @{thm expands_to_uminus} OF [get_basis_wf_thm basis, thm]
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   589
        val [absthm1, absthm2] =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   590
          @{thms eventually_atLeastAtMostD(1)[OF nonneg_abs_bounds[eventuallized]]
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   591
                 eventually_atLeastAtMostD(1)[OF nonpos_abs_bounds[eventuallized]]}
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   592
      in (
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   593
        case (lsgns, usgns) of
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   594
          ((_, true), _) =>
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   595
            (lthm, mk_nonneg_thm lsgn_thm,
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   596
               absthm1 OF [mk_nonneg_thm lsgn_thm, in_bounds_thm])
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   597
        | (_, (true, _)) =>
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   598
            (minus uthm, mk_nonpos_thm usgn_thm RS @{thm eventually_nonpos_flip},
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   599
               absthm2 OF [mk_nonpos_thm usgn_thm, in_bounds_thm])
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   600
        | ((true, _), (_, true)) => raise TERM ("abs_expansion_lower_bound", [])
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   601
        | _ => raise TERM ("Unexpected zeroness result in abs_expansion_bounds", []))
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   602
      end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   603
  | abs_expansion_lower_bound _ _ _ = raise TERM ("abs_expansion_lower_bound", [])
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   604
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   605
fun powr_expansion_bounds_right ectxt basis
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   606
      ((l1_thm, l1_le_thm), (u1_thm, u1_ge_thm)) (thm2, sgn_thm, g_sgns) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   607
  let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   608
    val in_bounds_thm = @{thm eventually_atLeastAtMostI} OF [l1_le_thm, u1_ge_thm]
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   609
    val (l1_thm, l1_sgn_thm, l1_sgns) = determine_sign ectxt (l1_thm, basis)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   610
    val l1_sgn_thm = if snd g_sgns then mk_nonneg_thm l1_sgn_thm else l1_sgn_thm
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   611
    val (l_thm, basis') = powr_expansion ectxt (l1_thm, thm2, basis)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   612
    val (u_thm, basis'') = powr_expansion ectxt (lift basis' u1_thm, lift basis' thm2, basis')
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   613
    val l_thm = lift basis'' l_thm
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   614
  in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   615
    if (snd g_sgns andalso not (snd l1_sgns)) orelse (not (snd g_sgns) andalso fst l1_sgns) then
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   616
      raise TERM ("Non-positive base in powr.", [])
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   617
    else if snd g_sgns then
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   618
      ((l_thm, u_thm, @{thm powr_right_bounds(1)[eventuallized]}
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   619
          OF [l1_sgn_thm, in_bounds_thm, mk_nonneg_thm sgn_thm]), basis'')
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   620
    else
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   621
      ((u_thm, l_thm, @{thm powr_right_bounds(2)[eventuallized]}
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   622
          OF [l1_sgn_thm, in_bounds_thm, mk_nonpos_thm sgn_thm]), basis'')
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   623
  end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   624
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   625
fun compare_expansion_to_1 ectxt (thm, basis) =
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 68630
diff changeset
   626
  prove_asymptotic_relation ectxt (thm, const_expansion ectxt basis \<^term>\<open>1 :: real\<close>, basis)
68630
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   627
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   628
fun powr_expansion_bounds_left ectxt basis
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   629
      thm1 ((l2_thm, l2_le_thm), (u2_thm, u2_ge_thm)) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   630
  let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   631
    val in_bounds_thm = @{thm eventually_atLeastAtMostI} OF [l2_le_thm, u2_ge_thm]
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   632
    val (thm1, _, SOME trimmed_pos_thm) = trim_expansion true (SOME Pos_Trim) ectxt (thm1, basis)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   633
    val pos_thm = @{thm expands_to_imp_eventually_pos} OF
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   634
      [get_basis_wf_thm basis, thm1, trimmed_pos_thm]
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   635
    val (l_thm, basis') = powr_expansion ectxt (thm1, l2_thm, basis)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   636
    val (u_thm, basis'') = powr_expansion ectxt (lift basis' thm1, lift basis' u2_thm, basis')
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   637
    val l_thm = lift basis'' l_thm
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   638
    val (cmp_1, cmp_1_thm) = compare_expansion_to_1 ectxt (thm1, basis)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   639
  in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   640
    case cmp_1 of
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   641
      LESS =>
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   642
        ((u_thm, l_thm, @{thm powr_left_bounds(2)[eventuallized]} OF
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   643
          [pos_thm, in_bounds_thm, mk_nonpos_thm cmp_1_thm]), basis'')
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   644
    | _ =>
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   645
        ((l_thm, u_thm, @{thm powr_left_bounds(1)[eventuallized]} OF
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   646
          [pos_thm, in_bounds_thm, mk_nonneg_thm cmp_1_thm]), basis'')
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   647
  end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   648
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   649
fun powr_expansion_bounds_2 ectxt basis
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   650
     ((l1, l1_le_thm, l1pos_thm, l1_sgn), (u1, u1_ge_thm, _, _))
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   651
     ((l2, l2_le_thm, l2sgn_thm, l2_sgn), (u2, u2_ge_thm, u2sgn_thm, u2_sgn)) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   652
  let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   653
    fun do_force_pos () =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   654
      if fst l1_sgn then raise TERM ("Non-positive base in power", []) else ()
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   655
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   656
    fun compare_expansion_to_1' thm =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   657
      let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   658
        val (cmp, sgn_thm) = compare_expansion_to_1 ectxt (thm, basis)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   659
        val sgn = (cmp <> GREATER, cmp <> LESS)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   660
      in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   661
        (sgn, sgn_thm)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   662
      end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   663
    val (l1_sgn, l1sgn_thm) = compare_expansion_to_1' l1
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   664
    val (u1_sgn, u1sgn_thm) = compare_expansion_to_1' u1
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   665
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   666
    val sgns = (l1_sgn, u1_sgn, l2_sgn, u2_sgn)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   667
    val in_bounds_thms =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   668
      map (fn thms => @{thm eventually_atLeastAtMostI} OF thms)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   669
        [[l1_le_thm, u1_ge_thm], [l2_le_thm, u2_ge_thm]]
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   670
    fun f n force_pos (a, b) (c, d) thms =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   671
      let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   672
        val _ = if force_pos then do_force_pos () else ()
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   673
        val l1pos_thm = if force_pos then l1pos_thm else mk_nonneg_thm l1pos_thm
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   674
        val (thm1, basis1) = powr_expansion ectxt (a, b, basis)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   675
        val (thm2, basis2) = powr_expansion ectxt (lift basis1 c, lift basis1 d, basis1)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   676
        val thm1 = lift basis2 thm1
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   677
      in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   678
        ((thm1, thm2, powr_bounds_thm n OF (l1pos_thm :: thms @ in_bounds_thms)), basis2)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   679
      end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   680
  in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   681
    case sgns of
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   682
      ((_, true), _, (_, true), _) =>
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   683
         f 0 false (l1, l2) (u1, u2) [mk_nonneg_thm l1sgn_thm, mk_nonneg_thm l2sgn_thm]
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   684
    | (_, (true, _), (_, true), _) =>
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   685
        f 1 false (l1, u2) (u1, l2) [mk_nonpos_thm u1sgn_thm, mk_nonneg_thm l2sgn_thm]
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   686
    | ((_, true), _, _, (true, _)) =>
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   687
        f 2 false (u1, l2) (l1, u2) [mk_nonneg_thm l1sgn_thm, mk_nonpos_thm u2sgn_thm]
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   688
    | (_, (true, _), _, (true, _)) =>
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   689
        f 3 true (u1, u2) (l1, l2) [mk_nonpos_thm u1sgn_thm, mk_nonpos_thm u2sgn_thm]
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   690
    | ((true, _), (_, true), (_, true), _) =>
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   691
        f 4 false (l1, u2) (u1, u2) 
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   692
          [mk_nonpos_thm l1sgn_thm, mk_nonneg_thm u1sgn_thm, mk_nonneg_thm l2sgn_thm]
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   693
    | ((true, _), (_, true), _, (true, _)) =>
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   694
        f 5 true (u1, l2) (l1, l2)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   695
          [mk_nonpos_thm l1sgn_thm, mk_nonneg_thm u1sgn_thm, mk_nonpos_thm u2sgn_thm]
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   696
    | ((_, true), _, (true, _), (_, true)) =>
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   697
        f 6 false (u1, l2) (u1, u2)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   698
          [mk_nonneg_thm l1sgn_thm, mk_nonpos_thm l2sgn_thm, mk_nonneg_thm u2sgn_thm]
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   699
    | (_, (true, _), (true, _), (_, true)) =>
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   700
        f 7 true (l1, u2) (l1, l2)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   701
          [mk_nonpos_thm u1sgn_thm, mk_nonpos_thm l2sgn_thm, mk_nonneg_thm u2sgn_thm]
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   702
    | ((true, _), (_, true), (true, _), (_, true)) =>
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   703
        let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   704
          val _ = do_force_pos ()
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   705
          val (l1u2, basis1) = powr_expansion ectxt (l1, u2, basis)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   706
          val (u1l2, basis2) = powr_expansion ectxt (lift basis1 u1, lift basis1 l2, basis1)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   707
          val (l1l2, basis3) = powr_expansion ectxt (lift basis2 l1, lift basis2 l2, basis2)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   708
          val (u1u2, basis4) = powr_expansion ectxt (lift basis3 u1, lift basis3 u2, basis3)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   709
          val [l1u2, u1l2, l1l2] = map (lift basis4) [l1u2, u1l2, l1l2]
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   710
          (* TODO The bases might blow up unnecessarily a bit here *)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   711
          val (l, lthm1, lthm2) = find_smaller_expansion ectxt (l1u2, u1l2, basis4)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   712
          val (u, uthm1, uthm2) = find_greater_expansion ectxt (l1l2, u1u2, basis4)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   713
        in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   714
          ((l, u, powr_bounds_thm 8 OF
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   715
            ([l1pos_thm, mk_nonpos_thm l1sgn_thm, mk_nonneg_thm u1sgn_thm, mk_nonpos_thm l2sgn_thm, 
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   716
                mk_nonneg_thm u2sgn_thm, lthm1, lthm2, uthm1, uthm2] @ in_bounds_thms)), basis4)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   717
        end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   718
        
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   719
    | _ => raise Match
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   720
  end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   721
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   722
fun powr_expansion_bounds_aux ectxt basis (Exact thm1) (Exact thm2) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   723
      powr_expansion ectxt (thm1, thm2, basis) |> apfst Exact
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   724
  | powr_expansion_bounds_aux ectxt basis (Bounds (SOME l1, SOME u1)) (Exact thm2) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   725
      powr_expansion_bounds_right ectxt basis (l1, u1) (determine_sign ectxt (thm2, basis))
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   726
      |> apfst convert_bounds
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   727
  | powr_expansion_bounds_aux ectxt basis (Exact thm1) (Bounds (SOME l2, SOME u2)) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   728
      powr_expansion_bounds_left ectxt basis thm1 (l2, u2)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   729
      |> apfst convert_bounds
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   730
  | powr_expansion_bounds_aux ectxt basis (Bounds (SOME l1, SOME u1)) (Bounds (SOME l2, SOME u2)) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   731
      let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   732
        fun prep (thm, le_thm) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   733
          case determine_sign ectxt (thm, basis) of
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   734
            (thm, sgn_thm, sgns) => (thm, le_thm, sgn_thm, sgns)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   735
      in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   736
        powr_expansion_bounds_2 ectxt basis (prep l1, prep u1) (prep l2, prep u2)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   737
        |> apfst convert_bounds
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   738
      end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   739
  | powr_expansion_bounds_aux _ basis _ _ = (Bounds (NONE, NONE), basis)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   740
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   741
fun powr_expansion_bounds ectxt basis bnds1 bnds2 =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   742
  case ev_zeroness_oracle ectxt (get_expanded_fun_bounds bnds1) of
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   743
    SOME zero_thm => 
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   744
      let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   745
        val t = Thm.cterm_of (Lazy_Eval.get_ctxt ectxt) (get_expanded_fun_bounds bnds2)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   746
        val thm = @{thm expands_to_powr_0} OF
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   747
          [zero_thm, Thm.reflexive t, get_basis_wf_thm basis, mk_expansion_level_eq_thm basis]
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   748
      in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   749
        (Exact thm, basis)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   750
      end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   751
  | NONE => powr_expansion_bounds_aux ectxt basis bnds1 bnds2
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   752
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   753
val powr_nat_bounds_transfer_le = @{thms powr_nat_bounds_transfer_le[eventuallized]}
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   754
fun powr_nat_bounds_transfer_le' n = List.nth (powr_nat_bounds_transfer_le, n - 1)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   755
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   756
fun powr_nat_expansion_bounds ectxt basis bnds1 bnds2 =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   757
  let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   758
    fun aux (Exact thm1) (Exact thm2) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   759
          apfst Exact (powr_nat_expansion ectxt (thm1, thm2, basis))
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   760
      | aux bnds1 bnds2 =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   761
          case get_lower_bound bnds1 of
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   762
            NONE => (Bounds (NONE, NONE), basis)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   763
          | SOME (lthm1, ge_thm1) =>
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   764
              let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   765
                val (lthm1, l1_sgn_thm, sgns1) = determine_sign ectxt (lthm1, basis)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   766
                val bnds1 =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   767
                  case bnds1 of
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   768
                    Exact _ => Exact lthm1
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   769
                  | Bounds (SOME (_, ge_thm), upper) => Bounds (SOME (lthm1, ge_thm), upper)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   770
                  | _ => raise Match
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   771
                val _ = if not (snd sgns1) then
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   772
                  raise TERM ("Unexpected sign in powr_nat_expansion_bounds", []) else ()
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   773
                val (bnds, basis') = powr_expansion_bounds ectxt basis bnds1 bnds2
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   774
                val lower = Option.map (apsnd (fn ge_thm' =>
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   775
                  @{thm powr_nat_bounds_transfer_ge[eventuallized]} OF
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   776
                    [mk_nonneg_thm l1_sgn_thm, ge_thm1, ge_thm'])) (get_lower_bound bnds)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   777
                fun determine_sign' NONE = NONE
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   778
                  | determine_sign' (SOME (thm, le_thm)) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   779
                      case determine_sign ectxt (thm, basis) of
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   780
                        (thm, sgn_thm, sgns) => SOME (thm, sgn_thm, sgns, le_thm)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   781
                fun do_transfer n thms = powr_nat_bounds_transfer_le' n OF thms
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   782
                fun transfer_upper (uthm', le_thm') =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   783
                  if not (fst sgns1) then
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   784
                    (uthm', do_transfer 1 [l1_sgn_thm, ge_thm1, le_thm'])
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   785
                  else
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   786
                    case determine_sign' (get_lower_bound bnds2) of
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   787
                      SOME (_, l2_sgn_thm, (false, true), ge_thm2) =>
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   788
                        (uthm', do_transfer 2 
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   789
                           [mk_nonneg_thm l1_sgn_thm, l2_sgn_thm, ge_thm1, ge_thm2, le_thm'])
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   790
                    | _ => (
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   791
                        case determine_sign' (get_upper_bound bnds2) of
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   792
                          SOME (_, u2_sgn_thm, (true, false), le_thm2) =>
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   793
                            (uthm', do_transfer 3
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   794
                               [mk_nonneg_thm l1_sgn_thm, u2_sgn_thm, ge_thm1, le_thm2, le_thm'])
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   795
                        | _ =>
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   796
                            let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   797
                              val (uthm'', le_u'_thm1, le_u'_thm2) = find_greater_expansion ectxt 
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 68630
diff changeset
   798
                                (uthm', const_expansion ectxt basis \<^term>\<open>1::real\<close>, basis)
68630
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   799
                            in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   800
                              (uthm'', do_transfer 4
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   801
                               [mk_nonneg_thm l1_sgn_thm, ge_thm1, le_thm', le_u'_thm1, le_u'_thm2])
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   802
                            end)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   803
              in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   804
                (Bounds (lower, Option.map transfer_upper (get_upper_bound bnds)), basis')
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   805
              end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   806
  in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   807
    case get_lower_bound bnds1 of
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   808
      SOME (lthm, _) =>
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   809
        let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   810
          val (lthm, _, sgns) = determine_sign ectxt (lthm, basis)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   811
          val bnds1 =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   812
            case bnds1 of
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   813
              Exact _ => Exact lthm
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   814
            | Bounds (SOME (_, le_thm), upper) => Bounds (SOME (lthm, le_thm), upper)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   815
            | _ => raise Match
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   816
        in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   817
          case sgns of
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   818
            (_, true) => aux bnds1 bnds2
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   819
          | _ =>
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   820
              let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   821
                val abs_bnds = abs_expansion_bounds ectxt basis bnds1
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   822
                fun transfer (NONE, _) = (Bounds (NONE, NONE), basis)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   823
                  | transfer (SOME (uthm, le_thm), basis) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   824
                      let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   825
                        val neg_uthm = @{thm expands_to_uminus} OF [get_basis_wf_thm basis, uthm]
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   826
                        val [ge_thm, le_thm] =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   827
                          map (fn thm => le_thm RS thm) @{thms powr_nat_bounds_transfer_abs}
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   828
                      in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   829
                        (Bounds (SOME (neg_uthm, ge_thm), SOME (uthm, le_thm)), basis)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   830
                      end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   831
              in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   832
                aux abs_bnds bnds2
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   833
                |> apfst get_upper_bound (* TODO better bounds possible *)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   834
                |> transfer
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   835
              end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   836
        end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   837
    | _ => (Bounds (NONE, NONE), basis)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   838
  end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   839
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   840
fun ln_expansion_bounds' ectxt (lthm, ltrimmed_thm, lb_thm) ub basis =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   841
      let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   842
        val (lthm', basis') = ln_expansion ectxt ltrimmed_thm lthm basis
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   843
        val wf_thm = get_basis_wf_thm basis
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   844
        val lb_thm' = @{thm expands_to_lb_ln} OF [lthm, ltrimmed_thm, wf_thm, lb_thm]
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   845
      in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   846
        case ub of
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   847
          NONE => (Bounds (SOME (lthm', lb_thm'), NONE), basis')
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   848
        | SOME (uthm, utrimmed_thm, ub_thm) =>
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   849
            let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   850
              val lifting = mk_lifting (extract_basis_list uthm) basis'
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   851
              val uthm = lift_expands_to_thm lifting uthm
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   852
              val utrimmed_thm = lift_trimmed_pos_thm lifting utrimmed_thm
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   853
              val (uthm, _, utrimmed_thm) = retrim_pos_expansion ectxt (uthm, basis', utrimmed_thm)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   854
              val (uthm', basis'') = ln_expansion ectxt utrimmed_thm uthm basis'
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   855
              val lthm' = lift basis'' lthm'
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   856
              val ub_thm' = @{thm expands_to_ub_ln} OF [lthm, ltrimmed_thm, wf_thm, lb_thm, ub_thm]
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   857
            in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   858
              (Bounds (SOME (lthm', lb_thm'), SOME (uthm', ub_thm')), basis'')
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   859
            end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   860
      end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   861
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   862
fun floor_expansion_bounds ectxt (bnds, basis) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   863
      let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   864
        val wf_thm = get_basis_wf_thm basis
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   865
        fun mk_lb (exp_thm, le_thm) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   866
          let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   867
            val exp_thm' = @{thm expands_to_minus} OF
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 68630
diff changeset
   868
              [wf_thm, exp_thm, const_expansion ectxt basis \<^term>\<open>1::real\<close>]
68630
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   869
            val le_thm = @{thm rfloor_bound(1)} OF [le_thm]
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   870
          in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   871
            (exp_thm', le_thm)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   872
          end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   873
        val mk_ub = apsnd (fn thm => @{thm rfloor_bound(2)} OF [thm])
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   874
        val bnds' =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   875
          Bounds (Option.map mk_lb (get_lower_bound bnds), Option.map mk_ub (get_upper_bound bnds))
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   876
      in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   877
        (bnds', basis)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   878
      end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   879
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   880
fun ceiling_expansion_bounds ectxt (bnds, basis) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   881
      let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   882
        val wf_thm = get_basis_wf_thm basis
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   883
        fun mk_ub (exp_thm, le_thm) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   884
          let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   885
            val exp_thm' = @{thm expands_to_add} OF
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 68630
diff changeset
   886
              [wf_thm, exp_thm, const_expansion ectxt basis \<^term>\<open>1::real\<close>]
68630
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   887
            val le_thm = @{thm rceil_bound(2)} OF [le_thm]
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   888
          in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   889
            (exp_thm', le_thm)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   890
          end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   891
        val mk_lb = apsnd (fn thm => @{thm rceil_bound(1)} OF [thm])
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   892
        val bnds' =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   893
          Bounds (Option.map mk_lb (get_lower_bound bnds), Option.map mk_ub (get_upper_bound bnds))
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   894
      in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   895
        (bnds', basis)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   896
      end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   897
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   898
fun natmod_expansion_bounds _ (Bounds (NONE, NONE), _, _) = Bounds (NONE, NONE)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   899
  | natmod_expansion_bounds _ (_, Bounds (NONE, NONE), _) = Bounds (NONE, NONE)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   900
  | natmod_expansion_bounds ectxt (bnds1, bnds2, basis) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   901
  let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   902
    val (f, g) = apply2 (Thm.reflexive o Thm.cterm_of (Lazy_Eval.get_ctxt ectxt) o
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   903
      get_expanded_fun_bounds) (bnds1, bnds2)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   904
    val ge_lower_thm = @{thm natmod_trivial_lower_bound} OF [f, g]
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   905
    fun minus1 thm = @{thm expands_to_minus} OF
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 68630
diff changeset
   906
          [get_basis_wf_thm basis, thm, const_expansion ectxt basis \<^term>\<open>1::real\<close>]
68630
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   907
    fun find_upper uthm1 le1_thm u_nonneg_thm =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   908
      let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   909
        val upper1 = (uthm1, @{thm natmod_upper_bound'} OF [g, u_nonneg_thm, le1_thm])
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   910
        val upper2 =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   911
          case (get_lower_bound bnds2, get_upper_bound bnds2) of
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   912
            (SOME (lthm2, ge2_thm), SOME (uthm2, le2_thm)) => (
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   913
              case determine_sign ectxt (minus1 lthm2, basis) of
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   914
                (_, sgn_thm, (_, true)) => SOME (minus1 uthm2,
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   915
                  @{thm natmod_upper_bound} OF [f, ge2_thm, le2_thm, mk_nonneg_thm sgn_thm])
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   916
              | _ => NONE)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   917
          | _ => NONE
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   918
      in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   919
        case upper2 of
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   920
          NONE => upper1
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   921
        | SOME upper2 =>
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   922
            case compare_expansions ectxt (fst upper1, fst upper2, basis) of
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   923
              (GREATER, _, _, _) => upper2
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   924
            | _ => upper1
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   925
      end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   926
  in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   927
    case get_upper_bound bnds1 of
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   928
      NONE => Bounds (SOME (zero_expansion basis, ge_lower_thm) , NONE)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   929
    | SOME (uthm1, le1_thm) =>
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   930
        case determine_sign ectxt (uthm1, basis) of
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   931
          (_, sgn_thm, (true, _)) => Exact (@{thm expands_to_natmod_nonpos} OF
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   932
            [g, mk_nonpos_thm sgn_thm, le1_thm, zero_expansion basis])
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   933
        | (uthm1, sgn_thm, (_, true)) =>
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   934
            Bounds (SOME (zero_expansion basis, ge_lower_thm),
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   935
              SOME (find_upper uthm1 le1_thm (mk_nonneg_thm sgn_thm)))
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   936
        | _ => raise TERM ("Unexpected result in natmod_expansion_bounds", [])
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   937
  end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   938
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   939
fun sin_cos_expansion thm _ [] =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   940
      (thm RS @{thm expands_to_sin_real}, thm RS @{thm expands_to_cos_real})
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   941
  | sin_cos_expansion thm basis ((IsNeg, neg_thm) :: _) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   942
      let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   943
        val neg_thm = @{thm compare_reals_diff_sgnD(1)} OF [neg_thm]
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   944
        val [thm1, thm2] = 
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   945
          map (fn thm' => thm' OF [neg_thm, get_basis_wf_thm basis, thm]) 
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   946
            @{thms expands_to_sin_ms_neg_exp expands_to_cos_ms_neg_exp}
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   947
      in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   948
        (thm1, thm2)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   949
      end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   950
  | sin_cos_expansion thm basis ((IsZero, zero_thm) :: e_thms) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   951
      let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   952
        val zero_thm = @{thm compare_reals_diff_sgnD(2)} OF [zero_thm]
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   953
        val thm' = expands_to_hd thm
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   954
        val (sin_thm, cos_thm) = (sin_cos_expansion thm' (tl_basis basis) e_thms)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   955
        fun mk_thm thm' = 
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   956
          (thm' OF [zero_thm, get_basis_wf_thm basis, thm, sin_thm, cos_thm]) |> solve_eval_eq
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   957
        val [thm1, thm2] = 
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   958
          map mk_thm @{thms expands_to_sin_ms_zero_exp expands_to_cos_ms_zero_exp}
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   959
      in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   960
        (thm1, thm2)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   961
      end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   962
  | sin_cos_expansion _ _ _ = raise Match
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   963
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   964
fun ln_expansion_bounds ectxt (Exact thm, basis) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   965
          let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   966
            val (thm, _, trimmed_thm) = trim_expansion true (SOME Pos_Trim) ectxt (thm, basis)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   967
          in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   968
            case trimmed_thm of
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   969
              NONE => raise TERM ("ln_expansion_bounds", [get_expanded_fun thm])
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   970
            | SOME trimmed_thm => 
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   971
                ln_expansion ectxt trimmed_thm thm basis |> apfst Exact
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   972
          end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   973
  | ln_expansion_bounds _ (Bounds (NONE, _), _) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   974
      raise TERM ("ln_expansion_bounds", [])
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   975
  | ln_expansion_bounds ectxt (Bounds (SOME (lthm, lb_thm), ub), basis) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   976
      let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   977
        fun trim thm = trim_expansion true (SOME Pos_Trim) ectxt (thm, basis)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   978
        val (lthm, _, trimmed_thm) = trim lthm
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   979
        val ub =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   980
          Option.mapPartial (fn (uthm, ub_thm) => 
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   981
            case trim uthm of 
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   982
              (uthm, _, SOME trimmed_thm) => SOME (uthm, trimmed_thm, ub_thm)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   983
            | _ => NONE)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   984
          ub
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   985
      in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   986
        case trimmed_thm of
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   987
          NONE => raise TERM ("ln_expansion_bounds", [get_expanded_fun lthm])
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   988
        | SOME trimmed_thm => ln_expansion_bounds' ectxt (lthm, trimmed_thm, lb_thm) ub basis
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   989
      end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   990
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   991
fun powr_const_expansion_bounds ectxt (Exact thm, p, basis) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   992
      Exact (powr_const_expansion ectxt (thm, p, basis))
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   993
  | powr_const_expansion_bounds _ (Bounds (NONE, NONE), _, _) = Bounds (NONE, NONE)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   994
  | powr_const_expansion_bounds ectxt (bnds as Bounds (NONE, _), p, basis) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   995
      Bounds (SOME (zero_expansion basis, @{thm eventually_powr_const_nonneg} OF
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   996
        map (Thm.reflexive o Thm.cterm_of (Lazy_Eval.get_ctxt ectxt))
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   997
                   [get_expanded_fun_bounds bnds, p]), NONE)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   998
  | powr_const_expansion_bounds ectxt (bnds as Bounds (SOME (lthm, ge_thm), upper), p, basis) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
   999
      let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1000
        val (lthm, lsgn_thm, sgns) = determine_sign ectxt (lthm, basis)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1001
        val _ = if snd sgns then ()
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1002
          else raise TERM ("Negative base for powr.", [])
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1003
        val (sgn, SOME sgn_thm) = zeroness_oracle true (SOME Sgn_Trim) ectxt p
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1004
      in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1005
        if sgn = IsNeg andalso fst sgns then
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1006
          Bounds (SOME (zero_expansion basis,
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1007
            @{thm eventually_powr_const_nonneg} OF
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1008
                 map (Thm.reflexive o Thm.cterm_of (Lazy_Eval.get_ctxt ectxt))
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1009
                   [get_expanded_fun_bounds bnds, p]), NONE)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1010
        else
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1011
          let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1012
            val sgn_thm =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1013
              case sgn of
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1014
                IsPos => sgn_thm RS @{thm less_imp_le}
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1015
              | IsZero => sgn_thm RS @{thm eq_zero_imp_nonneg}
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1016
              | IsNeg => sgn_thm RS @{thm less_imp_le}
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1017
              | _ => raise TERM ("Unexpected zeroness result in powr_const_expansion_bounds", [])
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1018
            val lthm' = powr_const_expansion ectxt (lthm, p, basis)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1019
            val lbnd = (lthm',
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1020
              if sgn <> IsNeg then
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1021
                @{thm eventually_powr_const_mono_nonneg[OF _ _ eventually_le_self]} OF
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1022
                  [sgn_thm, mk_nonneg_thm lsgn_thm, ge_thm]
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1023
              else
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1024
                @{thm eventually_powr_const_mono_nonpos[OF _ _ eventually_le_self]} OF
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1025
                  [sgn_thm, lsgn_thm, ge_thm])
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1026
            fun transfer_upper_bound (uthm, le_thm) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1027
              (powr_const_expansion ectxt (uthm, p, basis),
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1028
                 if sgn <> IsNeg then
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1029
                   @{thm eventually_powr_const_mono_nonneg} OF
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1030
                     [sgn_thm, mk_nonneg_thm lsgn_thm, ge_thm, le_thm]
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1031
                 else
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1032
                   @{thm eventually_powr_const_mono_nonpos} OF
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1033
                     [sgn_thm, lsgn_thm, ge_thm, le_thm])
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1034
          in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1035
            Bounds ((SOME lbnd, Option.map transfer_upper_bound upper) |>
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1036
              (if sgn = IsNeg then swap else I))
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1037
          end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1038
      end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1039
        handle Bind => raise TERM ("Unexpected zeroness result in powr_const_expansion_bounds", [])
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1040
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1041
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1042
(* TODO stub *)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1043
fun nonneg_power_expansion_bounds ectxt (Bounds (SOME (lthm, ge_thm), upper), n, basis) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1044
      let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1045
        val (lthm, lsgn_thm, l1sgns) = determine_sign ectxt (lthm, basis)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1046
        val _ = if not (snd l1sgns) then
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1047
          raise TERM ("Unexpected zeroness result in nonneg_power_expansion_bounds", []) else ()
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1048
        val nonneg_thm = mk_nonneg_thm lsgn_thm
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1049
        val ctxt = Lazy_Eval.get_ctxt ectxt
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1050
        val n_thm = Thm.reflexive (Thm.cterm_of ctxt n)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1051
        val lbnd =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1052
          (power_expansion ectxt (lthm, n, basis),
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1053
             @{thm eventually_power_mono[OF _ eventually_le_self]} OF
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1054
               [nonneg_thm, ge_thm, n_thm])
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1055
        fun transfer_upper (uthm, le_thm) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1056
          (power_expansion ectxt (uthm, n, basis),
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1057
             @{thm eventually_power_mono} OF
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1058
               [nonneg_thm, ge_thm, le_thm, n_thm])
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1059
      in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1060
        Bounds (SOME lbnd, Option.map transfer_upper upper)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1061
      end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1062
  | nonneg_power_expansion_bounds _ _ = Bounds (NONE, NONE)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1063
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1064
fun odd_power_expansion_bounds ectxt odd_thm (bnds, n, basis) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1065
      let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1066
        fun transfer (thm, le_thm) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1067
          (power_expansion ectxt (thm, n, basis),
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1068
             @{thm eventually_mono_power_odd} OF [odd_thm, le_thm])
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1069
      in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1070
        bnds |> apply2 (Option.map transfer) |> Bounds
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1071
      end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1072
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1073
fun get_parity' ectxt t =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1074
  let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1075
    (* TODO: Throw a tactic at it *)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1076
    val ctxt = Lazy_Eval.get_ctxt ectxt
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1077
    val par = get_parity (Thm.cterm_of ctxt t)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1078
    fun is_unknown Unknown_Parity = true
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1079
      | is_unknown _ = false
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1080
    val _ =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1081
       if not (is_unknown par) orelse not (#verbose (#ctxt ectxt)) then () else
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1082
         let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1083
           val p = Pretty.str ("real_asymp failed to determine whether the following term " ^
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1084
             "is even or odd:")
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1085
           val p = Pretty.chunks [p, Pretty.indent 2 (Syntax.pretty_term ctxt t)]
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1086
         in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1087
           Pretty.writeln p
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1088
         end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1089
  in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1090
    par
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1091
  end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1092
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1093
fun reflexive ectxt t = Thm.reflexive (Thm.cterm_of (Lazy_Eval.get_ctxt ectxt) t)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1094
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1095
fun unknown_parity_power_expansion_lower_bound ectxt ((SOME (lthm, ge_thm), _), n, basis) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1096
      let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1097
        val lpow_thm = power_expansion ectxt (lthm, n, basis)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1098
        val (lthm', le_thm1, le_thm2) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1099
          find_smaller_expansion ectxt (lpow_thm, zero_expansion basis, basis)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1100
      in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1101
        SOME (lthm', @{thm eventually_lower_bound_power_indet} OF [ge_thm, le_thm1, le_thm2])
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1102
      end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1103
  | unknown_parity_power_expansion_lower_bound _ _ = NONE
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1104
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1105
fun unknown_parity_power_expansion_upper_bound ectxt
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1106
    ((SOME (lthm, ge_thm), SOME (uthm, le_thm)), n, basis) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1107
      let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1108
        val lthm = @{thm expands_to_uminus} OF [get_basis_wf_thm basis, lthm]
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1109
        val (uthm', ge_thm1, ge_thm2) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1110
          find_greater_expansion ectxt (lthm, uthm, basis)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1111
        val uthm' = power_expansion ectxt (uthm', n, basis)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1112
      in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1113
        SOME (uthm', @{thm eventually_upper_bound_power_indet} OF 
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1114
          [ge_thm, le_thm, ge_thm1, ge_thm2, reflexive ectxt n])
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1115
      end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1116
  | unknown_parity_power_expansion_upper_bound _ _ = NONE
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1117
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1118
fun even_power_expansion_bounds ectxt even_thm (bnds, n, basis) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1119
  let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1120
    fun handle_indet_case bnds =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1121
      let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1122
        val lower = (zero_expansion basis, @{thm eventually_lower_bound_power_even_indet} OF
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1123
          [even_thm, reflexive ectxt (get_expanded_fun_bounds (Bounds bnds))])
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1124
        val upper = unknown_parity_power_expansion_upper_bound ectxt (bnds, n, basis)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1125
      in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1126
        (SOME lower, upper)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1127
      end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1128
  in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1129
    case snd bnds of
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1130
      NONE => handle_indet_case bnds
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1131
    | SOME (uthm, le_thm) =>
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1132
        let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1133
          val (uthm, usgn_thm, usgns) = determine_sign ectxt (uthm, basis)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1134
          val bnds = (fst bnds, SOME (uthm, le_thm))
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1135
        in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1136
          if fst usgns then
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1137
            let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1138
              val lthm' = power_expansion ectxt (uthm, n, basis)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1139
              val ge_thm' = @{thm eventually_lower_bound_power_even_nonpos} OF
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1140
                [even_thm, mk_nonpos_thm usgn_thm, le_thm]
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1141
              fun transfer_lower_bound (lthm, ge_thm) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1142
                (power_expansion ectxt (lthm, n, basis),
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1143
                   @{thm eventually_upper_bound_power_even_nonpos} OF
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1144
                     [even_thm, mk_nonpos_thm usgn_thm, ge_thm, le_thm])
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1145
            in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1146
              (SOME (lthm', ge_thm'), Option.map transfer_lower_bound (fst bnds))
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1147
            end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1148
          else
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1149
            handle_indet_case bnds
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1150
        end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1151
  end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1152
  
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1153
fun power_expansion_bounds ectxt (Exact thm, n, basis) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1154
      Exact (power_expansion ectxt (thm, n, basis))
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1155
  | power_expansion_bounds ectxt (Bounds bnds, n, basis) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1156
      let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1157
        val parity = get_parity' ectxt n
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1158
        fun handle_non_nonneg_case bnds = Bounds (
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1159
          case parity of
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1160
            Odd _ => raise Match
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1161
          | Even even_thm => even_power_expansion_bounds ectxt even_thm (bnds, n, basis)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1162
          | Unknown_Parity =>
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1163
              (unknown_parity_power_expansion_lower_bound ectxt (bnds, n, basis),
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1164
               unknown_parity_power_expansion_upper_bound ectxt (bnds, n, basis)))
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1165
      in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1166
        case parity of
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1167
          Odd odd_thm => odd_power_expansion_bounds ectxt odd_thm (bnds, n, basis)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1168
        | _ =>
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1169
          case fst bnds of
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1170
            NONE => handle_non_nonneg_case bnds
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1171
          | SOME (lthm, ge_thm) =>
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1172
              let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1173
                val (lthm, lsgn_thm, lsgns) = determine_sign ectxt (lthm, basis)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1174
                val bnds = (SOME (lthm, ge_thm), snd bnds)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1175
              in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1176
                if snd lsgns then
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1177
                  let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1178
                    val nthm = reflexive ectxt n
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1179
                    val lthm' = power_expansion ectxt (lthm, n, basis)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1180
                    val ge_thm' = @{thm eventually_power_mono[OF _ eventually_le_self]} OF
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1181
                      [mk_nonneg_thm lsgn_thm, ge_thm, nthm]
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1182
                    fun transfer_upper (uthm, le_thm) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1183
                      (power_expansion ectxt (uthm, n, basis),
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1184
                         @{thm eventually_power_mono} OF
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1185
                           [mk_nonneg_thm lsgn_thm, ge_thm, le_thm, nthm])
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1186
                  in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1187
                    Bounds (SOME (lthm', ge_thm'), Option.map transfer_upper (snd bnds))
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1188
                  end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1189
                else
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1190
                  handle_non_nonneg_case bnds
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1191
              end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1192
      end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1193
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1194
fun sgn_expansion_bounds ectxt (Exact thm, basis) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1195
      Exact (sgn_expansion ectxt (thm, basis))
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1196
  | sgn_expansion_bounds ectxt (Bounds bnds, basis) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1197
      let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1198
        fun aux (thm, le_thm) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1199
          (sgn_expansion ectxt (thm, basis), mono_bound @{thm mono_sgn_real} le_thm)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1200
        val (lower, upper) = apply2 (Option.map aux) bnds
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1201
        fun get_bound_exp (SOME (thm, _)) = SOME (get_expansion thm)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1202
          | get_bound_exp _ = NONE
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 68630
diff changeset
  1203
        fun is_const (SOME (Const (\<^const_name>\<open>Multiseries_Expansion.const_expansion\<close>, _) $ c'),
68630
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1204
              c) = c aconv c'
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1205
          | is_const _ = false
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1206
        fun aconv' (SOME a, SOME b) = a aconv b
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1207
          | aconv' _ = false
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1208
      in
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 68630
diff changeset
  1209
        if is_const (get_bound_exp lower, \<^term>\<open>\<lambda>x::real. 1 :: real\<close>) then
68630
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1210
          let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1211
            val SOME (lthm, ge_thm) = lower
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1212
          in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1213
            Exact (@{thm eventually_sgn_ge_1D} OF [ge_thm, lthm])
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1214
          end
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 68630
diff changeset
  1215
        else if is_const (get_bound_exp upper, \<^term>\<open>\<lambda>x::real. -1 :: real\<close>) then
68630
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1216
          let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1217
            val SOME (uthm, le_thm) = upper
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1218
          in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1219
            Exact (@{thm eventually_sgn_le_neg1D} OF [le_thm, uthm])
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1220
          end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1221
        else if aconv' (apply2 get_bound_exp (lower, upper)) then
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1222
          let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1223
            val (SOME (lthm, ge_thm), SOME (uthm, le_thm)) = (lower, upper)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1224
          in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1225
            Exact (@{thm expands_to_squeeze} OF [ge_thm, le_thm, lthm, uthm])
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1226
          end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1227
        else
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1228
          Bounds (lower, upper)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1229
      end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1230
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1231
fun sin_cos_expansion_bounds sin ectxt e basis =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1232
      let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1233
        val f = if sin then fst else snd
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1234
        fun trivial_bounds basis =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1235
          mk_trivial_bounds ectxt (expr_to_term e) 
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1236
            (if sin then @{thm trivial_bounds_sin} else @{thm trivial_bounds_cos}) basis
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1237
        fun mk_expansion (thm, basis') =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1238
          case trim_expansion_while_pos ectxt (thm, basis') of
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1239
            (_, Trimmed _, _) => (trivial_bounds basis, basis)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1240
          | (thm, Aborted _, e_thms) => 
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1241
              (Exact (f (sin_cos_expansion thm basis' e_thms)), basis')
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1242
      in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1243
        case expand_bounds' ectxt e basis of
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1244
          (Exact thm, basis') => mk_expansion (thm, basis')
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1245
        | _ => (trivial_bounds basis, basis)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1246
     end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1247
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1248
and mono_expansion mono_thm expand_fun ectxt e basis =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1249
      case expand_bounds' ectxt e basis of
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1250
        (Exact thm, basis) => expand_fun ectxt thm basis |> apfst Exact
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1251
      | (Bounds (SOME (lthm, lb_thm), NONE), basis) =>
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1252
          expand_fun ectxt lthm basis
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1253
          |> apfst (fn lthm => Bounds (SOME (lthm, mono_bound mono_thm lb_thm), NONE))
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1254
      | (Bounds (NONE, SOME (uthm, ub_thm)), basis) =>
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1255
          expand_fun ectxt uthm basis
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1256
          |> apfst (fn uthm => Bounds (NONE, SOME (uthm, mono_bound mono_thm ub_thm)))
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1257
      | (Bounds (SOME (lthm, lb_thm), SOME (uthm, ub_thm)), basis) =>
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1258
          let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1259
            val (lthm', basis') = expand_fun ectxt lthm basis
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1260
            val (uthm', basis'') = expand_fun ectxt (lift basis' uthm) basis'
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1261
            val lthm' = lift basis'' lthm'
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1262
            val (lb_thm', ub_thm') = apply2 (mono_bound mono_thm) (lb_thm, ub_thm)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1263
          in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1264
            (Bounds (SOME (lthm', lb_thm'), SOME (uthm', ub_thm')), basis'')
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1265
          end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1266
      | x => x
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1267
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1268
and minmax_expansion_bounds max thm ectxt (e1, e2) basis =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1269
      case try_prove_ev_eq ectxt (apply2 expr_to_term (e1, e2)) of
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1270
        SOME eq_thm =>
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1271
          let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1272
            val eq_thm' =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1273
              eq_thm RS (if max then @{thm max_eventually_eq} else @{thm min_eventually_eq})
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1274
          in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1275
            expand_bounds' ectxt e1 basis |> apfst (cong_bounds eq_thm')
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1276
          end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1277
      | NONE =>
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1278
          let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1279
            val (bnds1, basis') = expand_bounds' ectxt e1 basis
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1280
            val (bnds2, basis'') = expand_bounds' ectxt e2 basis'
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1281
            val bnds1 = lift_bounds basis'' bnds1
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1282
            fun f (thm1, thm2) = 
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1283
              (if max then max_expansion else min_expansion) ectxt (thm1, thm2, basis'')
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1284
            fun handle_bound (SOME (exp_thm1, le_thm1), SOME (exp_thm2, le_thm2)) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1285
                  SOME (f (exp_thm1, exp_thm2), thm OF [le_thm1, le_thm2])
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1286
              | handle_bound _ = NONE
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1287
            val bnds = (bnds1, bnds2)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1288
            val bnds =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1289
              case (bnds1, bnds2) of
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1290
                (Exact thm1, Exact thm2) => Exact (f (thm1, thm2))
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1291
              | _ =>
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1292
                Bounds (handle_bound (apply2 get_lower_bound bnds), 
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1293
                  handle_bound (apply2 get_upper_bound bnds))
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1294
          in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1295
            (bnds, basis'')
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1296
          end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1297
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1298
and expand_bin_bounds swap thms ectxt (e1, e2) basis =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1299
      let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1300
        val (bnds1, basis') = expand_bounds' ectxt e1 basis
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1301
        val (bnds2, basis'') = expand_bounds' ectxt e2 basis'
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1302
        val bnds1 = lift_bounds basis'' bnds1
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1303
        val bnds = expand_add_bounds swap thms (bnds1, bnds2) basis''
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1304
      in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1305
        (bnds, basis'')
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1306
      end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1307
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1308
and expand_bounds'' ectxt (Add e12) basis =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1309
      expand_bin_bounds false @{thms expands_to_add combine_bounds_add} ectxt e12 basis
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1310
  | expand_bounds'' ectxt (Minus e12) basis =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1311
      expand_bin_bounds true @{thms expands_to_minus combine_bounds_diff} ectxt e12 basis
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1312
  | expand_bounds'' ectxt (Uminus e) basis = (
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1313
      case expand_bounds' ectxt e basis of
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1314
        (Exact thm, basis) =>
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1315
          (Exact (@{thm expands_to_uminus} OF [get_basis_wf_thm basis, thm]), basis)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1316
      | (Bounds bnds, basis) =>
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1317
          let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1318
            fun flip (thm1, thm2) = 
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1319
              (@{thm expands_to_uminus} OF [get_basis_wf_thm basis, thm1],
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1320
                 @{thm bounds_uminus} OF [thm2])
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1321
          in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1322
            (Bounds (apply2 (Option.map flip) (swap bnds)), basis)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1323
          end)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1324
  | expand_bounds'' ectxt (Mult (e1, e2)) basis =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1325
      let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1326
        val (bnds1, basis') = expand_bounds' ectxt e1 basis
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1327
        val (bnds2, basis'') = expand_bounds' ectxt e2 basis'
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1328
        val bnds1 = lift_bounds basis'' bnds1
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1329
        val bnds = mult_expansion_bounds ectxt basis'' bnds1 bnds2
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1330
     in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1331
       (bnds, basis'')
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1332
     end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1333
  | expand_bounds'' ectxt (Powr (e1, e2)) basis =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1334
      let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1335
        val (bnds1, basis') = expand_bounds' ectxt e1 basis
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1336
        val (bnds2, basis'') = expand_bounds' ectxt e2 basis'
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1337
        val bnds1 = lift_bounds basis'' bnds1
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1338
     in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1339
       powr_expansion_bounds ectxt basis'' bnds1 bnds2
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1340
     end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1341
  | expand_bounds'' ectxt (Powr_Nat (e1, e2)) basis =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1342
      let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1343
        val (bnds1, basis') = expand_bounds' ectxt e1 basis
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1344
        val (bnds2, basis'') = expand_bounds' ectxt e2 basis'
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1345
        val bnds1 = lift_bounds basis'' bnds1
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1346
     in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1347
       powr_nat_expansion_bounds ectxt basis'' bnds1 bnds2
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1348
     end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1349
  | expand_bounds'' ectxt (Powr' (e, p)) basis =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1350
      let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1351
        val (bnds, basis') = expand_bounds' ectxt e basis
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1352
      in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1353
        (powr_const_expansion_bounds ectxt (bnds, p, basis'), basis')
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1354
      end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1355
  | expand_bounds'' ectxt (Power (e, n)) basis =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1356
      let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1357
        val (bnds, basis') = expand_bounds' ectxt e basis
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1358
      in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1359
        (power_expansion_bounds ectxt (bnds, n, basis'), basis')
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1360
      end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1361
  | expand_bounds'' ectxt (Root (e, n)) basis =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1362
      mono_expansion (reflexive ectxt n RS @{thm mono_root_real})
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1363
        (fn ectxt => fn thm => fn basis => (root_expansion ectxt (thm, n, basis), basis))
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1364
        ectxt e basis
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1365
  | expand_bounds'' ectxt (Inverse e) basis =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1366
      let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1367
        val (bnds, basis') = expand_bounds' ectxt e basis
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1368
      in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1369
        (inverse_expansion_bounds ectxt basis' bnds, basis')
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1370
      end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1371
  | expand_bounds'' ectxt (Div (e1, e2)) basis =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1372
      let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1373
        val (bnds1, basis') = expand_bounds' ectxt e1 basis
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1374
        val (bnds2, basis'') = expand_bounds' ectxt e2 basis'
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1375
        val bnds1 = lift_bounds basis'' bnds1
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1376
      in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1377
        (divide_expansion_bounds ectxt basis'' bnds1 bnds2, basis'')
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1378
      end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1379
  | expand_bounds'' ectxt (Sin e) basis =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1380
      sin_cos_expansion_bounds true ectxt e basis
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1381
  | expand_bounds'' ectxt (Cos e) basis =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1382
      sin_cos_expansion_bounds false ectxt e basis
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1383
  | expand_bounds'' ectxt (Exp e) basis =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1384
      mono_expansion @{thm mono_exp_real} exp_expansion ectxt e basis
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1385
  | expand_bounds'' ectxt (Ln e) basis =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1386
      ln_expansion_bounds ectxt (expand_bounds' ectxt e basis)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1387
  | expand_bounds'' ectxt (ExpLn e) basis =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1388
      let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1389
        val (bnds, basis') = expand_bounds' ectxt e basis
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1390
      in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1391
        case get_lower_bound bnds of
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1392
          NONE => (Bounds (NONE, NONE), basis)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1393
        | SOME (lthm, ge_thm) =>
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1394
            case trim_expansion true (SOME Pos_Trim) ectxt (lthm, basis') of
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1395
              (_, _, NONE) => raise TERM ("Non-positive function under logarithm.", [])
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1396
            | (lthm, _, SOME trimmed_thm) =>
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1397
                let   
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1398
                  val bnds =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1399
                    case bnds of
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1400
                      Exact _ => Exact lthm
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1401
                    | Bounds (_, upper) => Bounds (SOME (lthm, ge_thm), upper)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1402
                  val eq_thm = @{thm expands_to_imp_exp_ln_eq} OF
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1403
                    [lthm, ge_thm, trimmed_thm, get_basis_wf_thm basis]
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1404
                in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1405
                  (cong_bounds eq_thm bnds, basis')
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1406
                end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1407
      end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1408
  | expand_bounds'' ectxt (LnPowr (e1, e2)) basis =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1409
      let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1410
        val (bnds1, basis') = expand_bounds' ectxt e1 basis
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1411
        val (bnds2, basis'') = expand_bounds' ectxt e2 basis'
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1412
        val bnds1 = lift_bounds basis'' bnds1
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1413
      in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1414
        case get_lower_bound bnds1 of
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1415
          NONE => (Bounds (NONE, NONE), basis)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1416
        | SOME (lthm, ge_thm) =>
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1417
            case trim_expansion true (SOME Pos_Trim) ectxt (lthm, basis'') of
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1418
              (_, _, NONE) => raise TERM ("Non-positive base for powr.", [])
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1419
            | (lthm, _, SOME trimmed_thm) =>
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1420
                let   
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1421
                  val bnds1 =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1422
                    case bnds1 of
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1423
                      Exact _ => Exact lthm
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1424
                    | Bounds (_, upper) => Bounds (SOME (lthm, ge_thm), upper)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1425
                  val eq_thm = @{thm expands_to_imp_ln_powr_eq} OF
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1426
                    [lthm, ge_thm, trimmed_thm, get_basis_wf_thm basis'']
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1427
                  val (ln_bnds, basis''') = ln_expansion_bounds ectxt (bnds1, basis'')
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1428
                  val bnds2 = lift_bounds basis''' bnds2
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1429
                  val bnds = mult_expansion_bounds ectxt basis''' ln_bnds bnds2
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1430
                in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1431
                  (cong_bounds eq_thm bnds, basis''')
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1432
                end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1433
      end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1434
  | expand_bounds'' ectxt (Absolute e) basis =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1435
     let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1436
       val (bnds, basis') = expand_bounds' ectxt e basis
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1437
     in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1438
       (abs_expansion_bounds ectxt basis' bnds, basis')
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1439
     end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1440
  | expand_bounds'' ectxt (Sgn e) basis =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1441
     let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1442
       val (bnds, basis') = expand_bounds' ectxt e basis
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1443
     in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1444
       (sgn_expansion_bounds ectxt (bnds, basis'), basis')
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1445
     end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1446
  | expand_bounds'' ectxt (Min e12) basis =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1447
      minmax_expansion_bounds false @{thm combine_bounds_min} ectxt e12 basis
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1448
  | expand_bounds'' ectxt (Max e12) basis =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1449
      minmax_expansion_bounds true @{thm combine_bounds_max} ectxt e12 basis
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1450
  | expand_bounds'' ectxt (Floor e) basis =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1451
      floor_expansion_bounds ectxt (expand_bounds' ectxt e basis)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1452
  | expand_bounds'' ectxt (Ceiling e) basis =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1453
      ceiling_expansion_bounds ectxt (expand_bounds' ectxt e basis)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1454
  | expand_bounds'' ectxt (Frac e) basis =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1455
      (mk_trivial_bounds ectxt (expr_to_term e) @{thm trivial_bounds_frac} basis, basis)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1456
  | expand_bounds'' ectxt (NatMod (e1, e2)) basis =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1457
      let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1458
        val (bnds1, basis') = expand_bounds' ectxt e1 basis
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1459
        val (bnds2, basis'') = expand_bounds' ectxt e2 basis'
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1460
        val bnds1 = lift_bounds basis'' bnds1
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1461
      in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1462
        (natmod_expansion_bounds ectxt (bnds1, bnds2, basis''), basis'')
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1463
      end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1464
  | expand_bounds'' ectxt (ArcTan e) basis =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1465
      mono_expansion @{thm mono_arctan_real} 
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1466
        (fn ectxt => fn thm => fn basis => (arctan_expansion ectxt basis thm, basis)) ectxt e basis
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1467
  | expand_bounds'' ectxt e basis =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1468
      expand ectxt e basis |> apfst Exact
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1469
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1470
and expand_bounds' ectxt e basis =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1471
      expand_bounds'' ectxt e basis |> apfst (check_bounds e)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1472
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1473
fun expand_term_bounds ectxt t basis =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1474
  let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1475
    val ctxt = Lazy_Eval.get_ctxt ectxt
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1476
    val (e, eq_thm) = reify ctxt t
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1477
    val (bounds, basis) = expand_bounds' ectxt e basis
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1478
  in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1479
    (transfer_bounds eq_thm bounds, basis)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1480
  end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1481
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1482
fun expand_terms_bounds ectxt ts basis =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1483
  let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1484
    val ctxt = Lazy_Eval.get_ctxt ectxt
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1485
    val e_eq_thms = map (reify ctxt) ts
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1486
    fun step (e, eq_thm) (bndss, basis) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1487
      let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1488
        val (bnds, basis) = expand_bounds' ectxt e basis
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1489
        val bnds = transfer_bounds eq_thm bnds
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1490
      in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1491
        (bnds :: bndss, basis)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1492
      end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1493
    val (thms, basis) = fold step e_eq_thms ([], basis)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1494
    fun lift bnds = lift_bounds basis bnds
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1495
  in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1496
    (map lift (rev thms), basis)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1497
  end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1498
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1499
fun prove_nhds_bounds ectxt (Exact thm, basis) = prove_nhds ectxt (thm, basis)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1500
  | prove_nhds_bounds ectxt (Bounds (SOME (lthm, ge_thm), SOME (uthm, le_thm)), basis) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1501
      let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1502
        fun extract_limit thm =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1503
          thm |> Thm.concl_of |> HOLogic.dest_Trueprop |> dest_comb |> fst |> dest_comb |> snd
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1504
            |> dest_comb |> snd
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1505
        val llim_thm = prove_nhds ectxt (lthm, basis)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1506
        val ulim_thm = prove_nhds ectxt (uthm, basis)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1507
        val (lim1, lim2) = apply2 extract_limit (llim_thm, ulim_thm)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1508
        val eq_thm = the (try_prove_real_eq true ectxt (lim1, lim2))
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1509
      in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1510
        @{thm tendsto_sandwich'} OF [ge_thm, le_thm, llim_thm, ulim_thm, eq_thm]
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1511
      end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1512
  | prove_nhds_bounds _ _ = raise TERM ("prove_nhds_bounds", [])
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1513
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1514
fun prove_at_top_bounds ectxt (Exact thm, basis) = prove_at_top ectxt (thm, basis)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1515
  | prove_at_top_bounds ectxt (Bounds (SOME (lthm, ge_thm), _), basis) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1516
      let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1517
        val llim_thm = prove_at_top ectxt (lthm, basis)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1518
      in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1519
        @{thm filterlim_at_top_mono} OF [llim_thm, ge_thm]
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1520
      end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1521
  | prove_at_top_bounds _ _ = raise TERM ("prove_at_top_bounds", [])
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1522
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1523
fun prove_at_bot_bounds ectxt (Exact thm, basis) = prove_at_bot ectxt (thm, basis)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1524
  | prove_at_bot_bounds ectxt (Bounds (_, SOME (uthm, le_thm)), basis) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1525
      let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1526
        val ulim_thm = prove_at_bot ectxt (uthm, basis)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1527
      in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1528
        @{thm filterlim_at_bot_mono} OF [ulim_thm, le_thm]
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1529
      end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1530
  | prove_at_bot_bounds _ _ = raise TERM ("prove_at_bot_bounds", [])
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1531
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1532
fun prove_at_infinity_bounds ectxt (Exact thm, basis) = prove_at_infinity ectxt (thm, basis)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1533
  | prove_at_infinity_bounds ectxt (Bounds (lb, ub), basis) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1534
      let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1535
        val wf_thm = get_basis_wf_thm basis
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1536
        fun assert_nz (SOME (thm, le_thm)) = (
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1537
          case ev_zeroness_oracle ectxt (get_expanded_fun thm) of
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1538
            SOME _ => NONE
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1539
          | NONE => SOME (thm, le_thm))
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1540
        | assert_nz NONE = NONE
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1541
        fun lb_at_top (lthm, ge_thm) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1542
          case trim_expansion true (SOME Sgn_Trim) ectxt (lthm, basis) of
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1543
            (lthm, IsPos, SOME trimmed_thm) => SOME
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1544
              let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1545
                val lim_thm = prove_at_infinity ectxt (lthm, basis)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1546
                val pos_thm = @{thm expands_to_imp_eventually_pos} OF [wf_thm, lthm, trimmed_thm]
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1547
                val lim_thm' =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1548
                  @{thm filterlim_at_infinity_imp_filterlim_at_top} OF [lim_thm, pos_thm]
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1549
              in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1550
                @{thm filterlim_at_top_imp_at_infinity[OF filterlim_at_top_mono]}
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1551
                  OF [lim_thm', ge_thm]
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1552
              end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1553
          | _ => NONE
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1554
        fun ub_at_top (uthm, le_thm) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1555
          case trim_expansion true (SOME Sgn_Trim) ectxt (uthm, basis) of
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1556
            (uthm, IsNeg, SOME trimmed_thm) => SOME
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1557
              let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1558
                val lim_thm = prove_at_infinity ectxt (uthm, basis)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1559
                val neg_thm = @{thm expands_to_imp_eventually_neg} OF [wf_thm, uthm, trimmed_thm]
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1560
                val lim_thm' =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1561
                  @{thm filterlim_at_infinity_imp_filterlim_at_bot} OF [lim_thm, neg_thm]
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1562
              in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1563
                @{thm filterlim_at_bot_imp_at_infinity[OF filterlim_at_bot_mono]}
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1564
                  OF [lim_thm', le_thm]
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1565
              end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1566
          | _ => NONE
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1567
      in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1568
        case Option.mapPartial lb_at_top (assert_nz lb) of
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1569
          SOME thm => thm
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1570
        | NONE =>
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1571
            case Option.mapPartial ub_at_top (assert_nz ub) of
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1572
              SOME thm => thm
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1573
            | NONE => raise TERM ("prove_at_infinity_bounds", [])
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1574
      end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1575
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1576
fun prove_eventually_pos_lower_bound ectxt basis (lthm, ge_thm) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1577
      case trim_expansion true (SOME Sgn_Trim) ectxt (lthm, basis) of
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1578
        (lthm, IsPos, SOME trimmed_thm) =>
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1579
          SOME (@{thm eventually_less_le} OF [@{thm expands_to_imp_eventually_pos} OF
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1580
            [get_basis_wf_thm basis, lthm, trimmed_thm], ge_thm])
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1581
      | _ => NONE
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1582
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1583
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1584
fun prove_eventually_neg_upper_bound ectxt basis (uthm, le_thm) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1585
      case trim_expansion true (SOME Sgn_Trim) ectxt (uthm, basis) of
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1586
        (uthm, IsNeg, SOME trimmed_thm) =>
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1587
          SOME (@{thm eventually_le_less} OF [le_thm, @{thm expands_to_imp_eventually_neg} OF
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1588
            [get_basis_wf_thm basis, uthm, trimmed_thm]])
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1589
      | _ => NONE
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1590
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1591
fun prove_eventually_nonzero_bounds ectxt (Exact thm, basis) = (
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1592
      case trim_expansion true (SOME Simple_Trim) ectxt (thm, basis) of
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1593
        (thm, _, SOME trimmed_thm) =>
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1594
          @{thm expands_to_imp_eventually_nz} OF [get_basis_wf_thm basis, thm, trimmed_thm]
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1595
      |  _ => raise TERM ("prove_eventually_nonzero", []))
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1596
  | prove_eventually_nonzero_bounds ectxt (Bounds (l, u), basis) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1597
      case Option.mapPartial (prove_eventually_pos_lower_bound ectxt basis) l of
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1598
        SOME thm => thm RS @{thm eventually_pos_imp_nz}
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1599
      | NONE =>
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1600
          case Option.mapPartial (prove_eventually_neg_upper_bound ectxt basis) u of
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1601
            SOME thm => thm RS @{thm eventually_neg_imp_nz}
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1602
          | NONE => raise TERM ("prove_eventually_nonzero", [])
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1603
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1604
fun prove_at_0_bounds ectxt (Exact thm, basis) = prove_at_0 ectxt (thm, basis)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1605
  | prove_at_0_bounds ectxt (Bounds bnds, basis) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1606
      let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1607
        val lim_thm = prove_nhds_bounds ectxt (Bounds bnds, basis)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1608
        val nz_thm = prove_eventually_nonzero_bounds ectxt (Bounds bnds, basis)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1609
      in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1610
        @{thm Topological_Spaces.filterlim_atI} OF [lim_thm, nz_thm]
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1611
      end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1612
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1613
fun prove_at_right_0_bounds ectxt (Exact thm, basis) = prove_at_right_0 ectxt (thm, basis)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1614
  | prove_at_right_0_bounds ectxt (Bounds (SOME l, SOME u), basis) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1615
      let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1616
        val lim_thm = prove_nhds_bounds ectxt (Bounds (SOME l, SOME u), basis)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1617
      in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1618
        case prove_eventually_pos_lower_bound ectxt basis l of
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1619
          SOME pos_thm => @{thm tendsto_imp_filterlim_at_right} OF [lim_thm, pos_thm]
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1620
        | NONE => raise TERM ("prove_at_right_0_bounds", [])
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1621
      end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1622
  | prove_at_right_0_bounds _ _ = raise TERM ("prove_at_right_0_bounds", [])
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1623
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1624
fun prove_at_left_0_bounds ectxt (Exact thm, basis) = prove_at_left_0 ectxt (thm, basis)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1625
  | prove_at_left_0_bounds ectxt (Bounds (SOME l, SOME u), basis) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1626
      let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1627
        val lim_thm = prove_nhds_bounds ectxt (Bounds (SOME l, SOME u), basis)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1628
      in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1629
        case prove_eventually_neg_upper_bound ectxt basis u of
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1630
          SOME neg_thm => @{thm tendsto_imp_filterlim_at_left} OF [lim_thm, neg_thm]
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1631
        | NONE => raise TERM ("prove_at_left_0_bounds", [])
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1632
      end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1633
  | prove_at_left_0_bounds _ _ = raise TERM ("prove_at_left_0_bounds", [])
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1634
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1635
fun prove_eventually_less_bounds ectxt (bnds1, bnds2, basis) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1636
      case (get_upper_bound bnds1, get_lower_bound bnds2) of
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1637
        (SOME (ub1_thm, le_ub1_thm), SOME (lb2_thm, ge_lb2_thm)) =>
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1638
          let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1639
            val thm = prove_eventually_less ectxt (ub1_thm, lb2_thm, basis)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1640
          in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1641
            @{thm eventually_le_less[OF _ eventually_less_le]} OF [le_ub1_thm, thm, ge_lb2_thm]
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1642
          end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1643
     | _ => raise TERM ("prove_asymptotically_less_bounds", [])
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1644
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1645
fun prove_eventually_greater_bounds ectxt (bnds1, bnds2, basis) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1646
      prove_eventually_less_bounds ectxt (bnds2, bnds1, basis)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1647
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1648
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1649
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1650
fun prove_o_bounds small ectxt (Exact thm1, Exact thm2, basis) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1651
      (if small then prove_smallo else prove_bigo) ectxt (thm1, thm2, basis)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1652
  | prove_o_bounds small ectxt (bnds1, bnds2, basis) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1653
      let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1654
        val exact = if small then prove_smallo else prove_bigo
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1655
        val s = if small then "prove_smallo_bounds" else "prove_bigo_bounds"
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1656
        val (l2thm', nonneg_thm, ge2_thm) = abs_expansion_lower_bound ectxt basis bnds2
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1657
        val ((l1thm, ge1_thm), (u1thm, le1_thm)) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1658
          case bnds1 of
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1659
            Exact thm1 => 
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1660
              let val x = (exact ectxt (thm1, l2thm', basis), thm1 RS @{thm exact_to_bound})
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1661
              in (x, x) end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1662
          | Bounds (SOME (l1thm, ge1_thm), SOME (u1thm, le1_thm)) =>
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1663
              ((exact ectxt (l1thm, l2thm', basis), ge1_thm),
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1664
               (exact ectxt (u1thm, l2thm', basis), le1_thm))
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1665
          | _ => raise TERM (s, [])
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1666
        val impthm = if small then @{thm bounds_smallo_imp_smallo} else @{thm bounds_bigo_imp_bigo}
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1667
      in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1668
        impthm OF [ge1_thm, le1_thm, ge2_thm, nonneg_thm, l1thm, u1thm]
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1669
      end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1670
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1671
val prove_smallo_bounds = prove_o_bounds true
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1672
val prove_bigo_bounds = prove_o_bounds false
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1673
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1674
fun prove_bigtheta_bounds ectxt (Exact thm1, Exact thm2, basis) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1675
      prove_bigtheta ectxt (thm1, thm2, basis)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1676
  | prove_bigtheta_bounds ectxt (bnds1, bnds2, basis) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1677
      let (* TODO inefficient *)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1678
        val thms = 
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1679
          Par_List.map (fn x => prove_bigo_bounds ectxt x) 
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1680
            [(bnds1, bnds2, basis), (bnds2, bnds1, basis)]
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1681
      in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1682
        @{thm bigthetaI[unfolded bigomega_iff_bigo]} OF thms
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1683
      end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1684
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1685
fun prove_asymp_equivs ectxt basis =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1686
      Par_List.map (fn (thm1, thm2) => prove_asymp_equiv ectxt (thm1, thm2, basis))
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1687
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1688
fun prove_asymp_equiv_bounds ectxt (Exact thm1, Exact thm2, basis) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1689
      prove_asymp_equiv ectxt (thm1, thm2, basis)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1690
  | prove_asymp_equiv_bounds ectxt (Exact thm1, 
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1691
        Bounds (SOME (l2, ge2_thm), SOME (u2, le2_thm)), basis) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1692
      let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1693
        val thms = prove_asymp_equivs ectxt basis [(thm1, l2), (thm1, u2)]
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1694
      in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1695
        @{thm asymp_equiv_sandwich_real'[OF _ _ eventually_atLeastAtMostI]} OF
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1696
          (thms @ [ge2_thm, le2_thm])
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1697
      end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1698
  | prove_asymp_equiv_bounds ectxt (Bounds (SOME (l1, ge1_thm), SOME (u1, le1_thm)), 
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1699
        Exact thm2, basis) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1700
      let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1701
        val thms = prove_asymp_equivs ectxt basis [(l1, thm2), (u1, thm2)]
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1702
      in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1703
        @{thm asymp_equiv_sandwich_real[OF _ _ eventually_atLeastAtMostI]} OF
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1704
          (thms @ [ge1_thm, le1_thm])
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1705
      end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1706
  | prove_asymp_equiv_bounds ectxt (Bounds (SOME (l1, ge1_thm), SOME (u1, le1_thm)), 
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1707
        Bounds (SOME (l2, ge2_thm), SOME (u2, le2_thm)), basis) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1708
      let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1709
        val thms = prove_asymp_equivs ectxt basis [(l1, u1), (u1, l2), (l2, u2)]
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1710
      in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1711
        @{thm asymp_equiv_sandwich_real''
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1712
            [OF _ _ _ eventually_atLeastAtMostI eventually_atLeastAtMostI]} OF
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1713
          (thms @ [ge1_thm, le1_thm, ge2_thm, le2_thm])
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1714
      end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1715
  | prove_asymp_equiv_bounds _ _ = raise TERM ("prove_asymp_equiv_bounds", [])
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1716
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1717
val dest_fun = dest_comb #> fst
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1718
val dest_arg = dest_comb #> snd
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1719
val concl_of' = Thm.concl_of #> HOLogic.dest_Trueprop
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1720
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1721
fun lim_eq ectxt (l1, l2) = (l1 aconv l2) orelse
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1722
  case (l1, l2) of
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 68630
diff changeset
  1723
    (Const (\<^const_name>\<open>nhds\<close>, _) $ a, Const (\<^const_name>\<open>nhds\<close>, _) $ b) => (
68630
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1724
      case try_prove_real_eq false ectxt (a, b) of
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1725
        SOME _ => true
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1726
      | _ => false)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1727
  | _ => false
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1728
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1729
fun limit_of_expansion_bounds ectxt (bnds, basis) =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1730
  let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1731
    fun get_limit thm =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1732
      limit_of_expansion (true, true) ectxt (thm, basis) |> snd |> concl_of' |> dest_fun |> dest_arg
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1733
  in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1734
    case bnds of
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1735
      Exact thm => get_limit thm |> Exact_Limit
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1736
    | Bounds (l, u) =>
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1737
        let
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1738
          val (llim, ulim) = apply2 (Option.map (fst #> get_limit)) (l, u)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1739
        in
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1740
          case (llim, ulim) of
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1741
            (SOME llim', SOME ulim') =>
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1742
              if lim_eq ectxt (llim', ulim') then Exact_Limit llim' else Limit_Bounds (llim, ulim)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1743
          | _ => Limit_Bounds (llim, ulim)
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1744
        end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1745
  end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1746
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1747
end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1748
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1749
structure Multiseries_Expansion_Bounds : EXPANSION_INTERFACE =
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1750
struct
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1751
open Multiseries_Expansion;
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1752
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1753
type T = bounds
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1754
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1755
val expand_term = expand_term_bounds
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1756
val expand_terms = expand_terms_bounds
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1757
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1758
val prove_nhds = prove_nhds_bounds
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1759
val prove_at_infinity = prove_at_infinity_bounds
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1760
val prove_at_top = prove_at_top_bounds
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1761
val prove_at_bot = prove_at_bot_bounds
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1762
val prove_at_0 = prove_at_0_bounds
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1763
val prove_at_right_0 = prove_at_right_0_bounds
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1764
val prove_at_left_0 = prove_at_left_0_bounds
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1765
val prove_eventually_nonzero = prove_eventually_nonzero_bounds
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1766
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1767
val prove_eventually_less = prove_eventually_less_bounds
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1768
val prove_eventually_greater = prove_eventually_greater_bounds
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1769
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1770
val prove_smallo = prove_smallo_bounds
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1771
val prove_bigo = prove_bigo_bounds
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1772
val prove_bigtheta = prove_bigtheta_bounds
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1773
val prove_asymp_equiv = prove_asymp_equiv_bounds
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1774
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1775
end
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1776
c55f6f0b3854 Added Real_Asymp package
Manuel Eberl <eberlm@in.tum.de>
parents:
diff changeset
  1777
structure Real_Asymp_Bounds = Real_Asymp(Multiseries_Expansion_Bounds)