src/HOL/Analysis/metric_arith.ML
author wenzelm
Tue, 19 Oct 2021 15:20:31 +0200
changeset 74547 54055f568d76
parent 74546 6df92c387063
child 74627 c690435c47ee
permissions -rw-r--r--
proper tactic combinator;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
74546
6df92c387063 proper file headers;
wenzelm
parents: 74545
diff changeset
     1
(*  Title:      HOL/Analysis/metric_arith.ML
6df92c387063 proper file headers;
wenzelm
parents: 74545
diff changeset
     2
    Author:     Maximilian Schäffeler (port from HOL Light)
6df92c387063 proper file headers;
wenzelm
parents: 74545
diff changeset
     3
6df92c387063 proper file headers;
wenzelm
parents: 74545
diff changeset
     4
A decision procedure for metric spaces.
6df92c387063 proper file headers;
wenzelm
parents: 74545
diff changeset
     5
*)
6df92c387063 proper file headers;
wenzelm
parents: 74545
diff changeset
     6
6df92c387063 proper file headers;
wenzelm
parents: 74545
diff changeset
     7
signature METRIC_ARITH =
6df92c387063 proper file headers;
wenzelm
parents: 74545
diff changeset
     8
sig
6df92c387063 proper file headers;
wenzelm
parents: 74545
diff changeset
     9
  val trace: bool Config.T
70951
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
    10
  val metric_arith_tac : Proof.context -> int -> tactic
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
    11
end
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
    12
74546
6df92c387063 proper file headers;
wenzelm
parents: 74545
diff changeset
    13
structure Metric_Arith : METRIC_ARITH =
6df92c387063 proper file headers;
wenzelm
parents: 74545
diff changeset
    14
struct
70951
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
    15
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
    16
fun default d x = case x of SOME y => SOME y | NONE => d
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
    17
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
    18
(* apply f to both cterms in ct_pair, merge results *)
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
    19
fun app_union_ct_pair f ct_pair = uncurry (union (op aconvc)) (apply2 f ct_pair)
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
    20
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
    21
val trace = Attrib.setup_config_bool \<^binding>\<open>metric_trace\<close> (K false)
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
    22
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
    23
fun trace_tac ctxt msg =
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
    24
  if Config.get ctxt trace then print_tac ctxt msg
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
    25
  else all_tac
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
    26
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
    27
fun argo_trace_ctxt ctxt =
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
    28
  if Config.get ctxt trace
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
    29
  then Config.map (Argo_Tactic.trace) (K "basic") ctxt
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
    30
  else ctxt
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
    31
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
    32
fun IF_UNSOLVED' tac i = IF_UNSOLVED (tac i)
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
    33
fun REPEAT' tac i = REPEAT (tac i)
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
    34
74274
36f2c4a5c21b clarified signature;
wenzelm
parents: 74269
diff changeset
    35
fun free_in v ct = Cterms.defined (Cterms.build (Drule.add_frees_cterm ct)) v
70951
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
    36
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
    37
(* build a cterm set with elements cts of type ty *)
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
    38
fun mk_ct_set ctxt ty =
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
    39
  map Thm.term_of #>
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
    40
  HOLogic.mk_set ty #>
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
    41
  Thm.cterm_of ctxt
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
    42
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
    43
fun prenex_tac ctxt =
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
    44
  let
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
    45
    val prenex_simps = Proof_Context.get_thms ctxt @{named_theorems metric_prenex}
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
    46
    val prenex_ctxt = put_simpset HOL_basic_ss ctxt addsimps prenex_simps
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
    47
  in
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
    48
    simp_tac prenex_ctxt THEN'
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
    49
    K (trace_tac ctxt "Prenex form")
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
    50
  end
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
    51
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
    52
fun nnf_tac ctxt =
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
    53
  let
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
    54
    val nnf_simps = Proof_Context.get_thms ctxt @{named_theorems metric_nnf}
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
    55
    val nnf_ctxt = put_simpset HOL_basic_ss ctxt addsimps nnf_simps
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
    56
  in
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
    57
    simp_tac nnf_ctxt THEN'
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
    58
    K (trace_tac ctxt "NNF form")
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
    59
  end
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
    60
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
    61
fun unfold_tac ctxt =
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
    62
  asm_full_simp_tac (put_simpset HOL_basic_ss ctxt addsimps (
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
    63
    Proof_Context.get_thms ctxt @{named_theorems metric_unfold}))
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
    64
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
    65
fun pre_arith_tac ctxt =
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
    66
  simp_tac (put_simpset HOL_basic_ss ctxt addsimps (
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
    67
    Proof_Context.get_thms ctxt @{named_theorems metric_pre_arith})) THEN'
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
    68
    K (trace_tac ctxt "Prepared for decision procedure")
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
    69
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
    70
fun dist_refl_sym_tac ctxt =
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
    71
  let
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
    72
    val refl_sym_simps = @{thms dist_self dist_commute add_0_right add_0_left simp_thms}
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
    73
    val refl_sym_ctxt = put_simpset HOL_basic_ss ctxt addsimps refl_sym_simps
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
    74
  in
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
    75
    simp_tac refl_sym_ctxt THEN'
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
    76
    K (trace_tac ctxt ("Simplified using " ^ @{make_string} refl_sym_simps))
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
    77
  end
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
    78
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
    79
fun is_exists ct =
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
    80
  case Thm.term_of ct of
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
    81
    Const (\<^const_name>\<open>HOL.Ex\<close>,_)$_ => true
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
    82
  | Const (\<^const_name>\<open>Trueprop\<close>,_)$_ => is_exists (Thm.dest_arg ct)
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
    83
  | _ => false
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
    84
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
    85
fun is_forall ct =
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
    86
  case Thm.term_of ct of
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
    87
    Const (\<^const_name>\<open>HOL.All\<close>,_)$_ => true
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
    88
  | Const (\<^const_name>\<open>Trueprop\<close>,_)$_ => is_forall (Thm.dest_arg ct)
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
    89
  | _ => false
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
    90
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
    91
fun dist_ty mty = mty --> mty --> \<^typ>\<open>real\<close>
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
    92
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
    93
(* find all free points in ct of type metric_ty *)
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
    94
fun find_points ctxt metric_ty ct =
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
    95
  let
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
    96
    fun find ct =
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
    97
      (if Thm.typ_of_cterm ct = metric_ty then [ct] else []) @ (
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
    98
        case Thm.term_of ct of
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
    99
          _ $ _ =>
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   100
          app_union_ct_pair find (Thm.dest_comb ct)
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   101
        | Abs (_, _, _) =>
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   102
          (* ensure the point doesn't contain the bound variable *)
74525
c960bfcb91db discontinued Term.dest_abs / Logic.dest_all, which are officially superseded by Variable.dest_abs etc., but there are also Term.dest_abs_global to recover existing tools easily;
wenzelm
parents: 74518
diff changeset
   103
          let val (var, bod) = Thm.dest_abs_global ct in
70951
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   104
            filter (free_in var #> not) (find bod)
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   105
          end
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   106
        | _ => [])
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   107
    val points = find ct
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   108
  in
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   109
    case points of
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   110
      [] =>
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   111
      (* if no point can be found, invent one *)
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   112
      let
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   113
        val free_name = Term.variant_frees (Thm.term_of ct) [("x", metric_ty)]
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   114
      in
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   115
        map (Free #> Thm.cterm_of ctxt) free_name
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   116
      end
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   117
    | _ => points
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   118
  end
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   119
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   120
(* find all cterms "dist x y" in ct, where x and y have type metric_ty *)
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   121
fun find_dist metric_ty ct =
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   122
  let
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   123
    val dty = dist_ty metric_ty
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   124
    fun find ct =
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   125
      case Thm.term_of ct of
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   126
        Const (\<^const_name>\<open>dist\<close>, ty) $ _ $ _ =>
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   127
        if ty = dty then [ct] else []
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   128
      | _ $ _ =>
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   129
        app_union_ct_pair find (Thm.dest_comb ct)
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   130
      | Abs (_, _, _) =>
74525
c960bfcb91db discontinued Term.dest_abs / Logic.dest_all, which are officially superseded by Variable.dest_abs etc., but there are also Term.dest_abs_global to recover existing tools easily;
wenzelm
parents: 74518
diff changeset
   131
        let val (var, bod) = Thm.dest_abs_global ct in
70951
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   132
          filter (free_in var #> not) (find bod)
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   133
        end
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   134
      | _ => []
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   135
  in
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   136
    find ct
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   137
  end
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   138
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   139
(* find all "x=y", where x has type metric_ty *)
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   140
fun find_eq metric_ty ct =
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   141
  let
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   142
    fun find ct =
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   143
      case Thm.term_of ct of
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   144
        Const (\<^const_name>\<open>HOL.eq\<close>, ty) $ _ $ _ =>
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   145
          if fst (dest_funT ty) = metric_ty
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   146
          then [ct]
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   147
          else app_union_ct_pair find (Thm.dest_binop ct)
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   148
      | _ $ _ => app_union_ct_pair find (Thm.dest_comb ct)
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   149
      | Abs (_, _, _) =>
74525
c960bfcb91db discontinued Term.dest_abs / Logic.dest_all, which are officially superseded by Variable.dest_abs etc., but there are also Term.dest_abs_global to recover existing tools easily;
wenzelm
parents: 74518
diff changeset
   150
        let val (var, bod) = Thm.dest_abs_global ct in
70951
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   151
          filter (free_in var #> not) (find bod)
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   152
        end
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   153
      | _ => []
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   154
  in
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   155
    find ct
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   156
  end
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   157
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   158
(* rewrite ct of the form "dist x y" using maxdist_thm *)
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   159
fun maxdist_conv ctxt fset_ct ct =
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   160
  let
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   161
    val (xct, yct) = Thm.dest_binop ct
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   162
    val solve_prems =
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   163
      rule_by_tactic ctxt (ALLGOALS (simp_tac (put_simpset HOL_ss ctxt
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   164
        addsimps @{thms finite.emptyI finite_insert empty_iff insert_iff})))
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   165
    val image_simp =
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   166
      Simplifier.rewrite (put_simpset HOL_ss ctxt addsimps @{thms image_empty image_insert})
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   167
    val dist_refl_sym_simp =
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   168
      Simplifier.rewrite (put_simpset HOL_ss ctxt addsimps @{thms dist_commute dist_self})
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   169
    val algebra_simp =
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   170
      Simplifier.rewrite (put_simpset HOL_ss ctxt addsimps
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   171
        @{thms diff_self diff_0_right diff_0 abs_zero abs_minus_cancel abs_minus_commute})
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   172
    val insert_simp =
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   173
      Simplifier.rewrite (put_simpset HOL_ss ctxt addsimps @{thms insert_absorb2 insert_commute})
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   174
    val sup_simp =
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   175
      Simplifier.rewrite (put_simpset HOL_ss ctxt addsimps @{thms cSup_singleton Sup_insert_insert})
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   176
    val real_abs_dist_simp =
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   177
      Simplifier.rewrite (put_simpset HOL_ss ctxt addsimps @{thms real_abs_dist})
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   178
    val maxdist_thm =
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   179
      @{thm maxdist_thm} |>
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   180
      infer_instantiate' ctxt [SOME fset_ct, SOME xct, SOME yct] |>
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   181
      solve_prems
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   182
  in
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   183
    ((Conv.rewr_conv maxdist_thm) then_conv
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   184
    (* SUP to Sup *)
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   185
    image_simp then_conv
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   186
    dist_refl_sym_simp then_conv
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   187
    algebra_simp then_conv
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   188
    (* eliminate duplicate terms in set *)
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   189
    insert_simp then_conv
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   190
    (* Sup to max *)
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   191
    sup_simp then_conv
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   192
    real_abs_dist_simp) ct
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   193
  end
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   194
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   195
(* rewrite ct of the form "x=y" using metric_eq_thm *)
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   196
fun metric_eq_conv ctxt fset_ct ct =
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   197
  let
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   198
    val (xct, yct) = Thm.dest_binop ct
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   199
    val solve_prems =
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   200
      rule_by_tactic ctxt (ALLGOALS (simp_tac (put_simpset HOL_ss ctxt
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   201
        addsimps @{thms empty_iff insert_iff})))
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   202
    val ball_simp =
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   203
      Simplifier.rewrite (put_simpset HOL_ss ctxt addsimps
70954
23e6eef4e6aa avoid referring to lemmas by index
immler
parents: 70951
diff changeset
   204
        @{thms Set.ball_empty ball_insert})
70951
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   205
    val dist_refl_sym_simp =
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   206
      Simplifier.rewrite (put_simpset HOL_ss ctxt addsimps @{thms dist_commute dist_self})
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   207
    val metric_eq_thm =
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   208
      @{thm metric_eq_thm} |>
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   209
      infer_instantiate' ctxt [SOME xct, SOME fset_ct, SOME yct] |>
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   210
      solve_prems
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   211
  in
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   212
    ((Conv.rewr_conv metric_eq_thm) then_conv
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   213
    (* convert \<forall>x\<in>{x\<^sub>1,...,x\<^sub>n}. P x to P x\<^sub>1 \<and> ... \<and> P x\<^sub>n *)
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   214
    ball_simp then_conv
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   215
    dist_refl_sym_simp) ct
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   216
  end
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   217
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   218
(* build list of theorems "0 \<le> dist x y" for all dist terms in ct *)
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   219
fun augment_dist_pos ctxt metric_ty ct =
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   220
  let fun inst dist_ct =
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   221
    let val (xct, yct) = Thm.dest_binop dist_ct
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   222
    in infer_instantiate' ctxt [SOME xct, SOME yct] @{thm zero_le_dist} end
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   223
  in map inst (find_dist metric_ty ct) end
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   224
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   225
(* apply maxdist_conv and metric_eq_conv to the goal, thereby embedding the goal in (\<real>\<^sup>n,dist\<^sub>\<infinity>) *)
74547
54055f568d76 proper tactic combinator;
wenzelm
parents: 74546
diff changeset
   226
fun embedding_tac ctxt metric_ty = CSUBGOAL (fn (goal, i) =>
70951
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   227
  let
74547
54055f568d76 proper tactic combinator;
wenzelm
parents: 74546
diff changeset
   228
    val points = find_points ctxt metric_ty goal
70951
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   229
    val fset_ct = mk_ct_set ctxt metric_ty points
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   230
    (* embed all subterms of the form "dist x y" in (\<real>\<^sup>n,dist\<^sub>\<infinity>) *)
74547
54055f568d76 proper tactic combinator;
wenzelm
parents: 74546
diff changeset
   231
    val eq1 = map (maxdist_conv ctxt fset_ct) (find_dist metric_ty goal)
70951
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   232
    (* replace point equality by equality of components in \<real>\<^sup>n *)
74547
54055f568d76 proper tactic combinator;
wenzelm
parents: 74546
diff changeset
   233
    val eq2 = map (metric_eq_conv ctxt fset_ct) (find_eq metric_ty goal)
70951
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   234
  in
74545
6c123914883a clarified context;
wenzelm
parents: 74525
diff changeset
   235
    (K (trace_tac ctxt "Embedding into \<real>\<^sup>n") THEN'
74547
54055f568d76 proper tactic combinator;
wenzelm
parents: 74546
diff changeset
   236
      CONVERSION (Conv.top_sweep_rewrs_conv (eq1 @ eq2) ctxt)) i
54055f568d76 proper tactic combinator;
wenzelm
parents: 74546
diff changeset
   237
  end)
70951
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   238
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   239
(* decision procedure for linear real arithmetic *)
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   240
fun lin_real_arith_tac ctxt metric_ty i goal =
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   241
  let
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   242
    val dist_thms = augment_dist_pos ctxt metric_ty (Thm.cprem_of goal 1)
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   243
    val ctxt' = argo_trace_ctxt ctxt
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   244
  in (Argo_Tactic.argo_tac ctxt' dist_thms) i goal end
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   245
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   246
fun basic_metric_arith_tac ctxt metric_ty =
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   247
  HEADGOAL (dist_refl_sym_tac ctxt THEN'
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   248
  IF_UNSOLVED' (embedding_tac ctxt metric_ty) THEN'
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   249
  IF_UNSOLVED' (pre_arith_tac ctxt) THEN'
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   250
  IF_UNSOLVED' (lin_real_arith_tac ctxt metric_ty))
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   251
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   252
(* tries to infer the metric space from ct from dist terms,
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   253
   if no dist terms are present, equality terms will be used *)
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   254
fun guess_metric ctxt ct =
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   255
let
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   256
  fun find_dist ct =
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   257
    case Thm.term_of ct of
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   258
      Const (\<^const_name>\<open>dist\<close>, ty) $ _ $ _  => SOME (fst (dest_funT ty))
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   259
    | _ $ _ =>
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   260
      let val (s, t) = Thm.dest_comb ct in
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   261
        default (find_dist t) (find_dist s)
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   262
      end
74525
c960bfcb91db discontinued Term.dest_abs / Logic.dest_all, which are officially superseded by Variable.dest_abs etc., but there are also Term.dest_abs_global to recover existing tools easily;
wenzelm
parents: 74518
diff changeset
   263
    | Abs (_, _, _) => find_dist (snd (Thm.dest_abs_global ct))
70951
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   264
    | _ => NONE
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   265
  fun find_eq ct =
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   266
    case Thm.term_of ct of
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   267
      Const (\<^const_name>\<open>HOL.eq\<close>, ty) $ x $ _ =>
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   268
      let val (l, r) = Thm.dest_binop ct in
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   269
        if Sign.of_sort (Proof_Context.theory_of ctxt) (type_of x, \<^sort>\<open>metric_space\<close>)
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   270
        then SOME (fst (dest_funT ty))
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   271
        else default (find_dist r) (find_eq l)
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   272
      end
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   273
    | _ $ _ =>
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   274
      let val (s, t) = Thm.dest_comb ct in
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   275
        default (find_eq t) (find_eq s)
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   276
      end
74525
c960bfcb91db discontinued Term.dest_abs / Logic.dest_all, which are officially superseded by Variable.dest_abs etc., but there are also Term.dest_abs_global to recover existing tools easily;
wenzelm
parents: 74518
diff changeset
   277
    | Abs (_, _, _) => find_eq (snd (Thm.dest_abs_global ct))
70951
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   278
    | _ => NONE
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   279
  in
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   280
    case default (find_eq ct) (find_dist ct) of
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   281
      SOME ty => ty
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   282
    | NONE => error "No Metric Space was found"
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   283
  end
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   284
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   285
(* eliminate \<exists> by proving the goal for a single witness from the metric space *)
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   286
fun elim_exists ctxt goal =
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   287
  let
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   288
    val ct = Thm.cprem_of goal 1
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   289
    val metric_ty = guess_metric ctxt ct
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   290
    val points = find_points ctxt metric_ty ct
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   291
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   292
    fun try_point ctxt pt =
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   293
      let val ex_rule = infer_instantiate' ctxt [NONE, SOME pt] @{thm exI}
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   294
      in
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   295
        HEADGOAL (resolve_tac ctxt [ex_rule] ORELSE'
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   296
        (* variable doesn't occur in body *)
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   297
        resolve_tac ctxt @{thms exI}) THEN
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   298
        trace_tac ctxt ("Removed existential quantifier, try " ^ @{make_string} pt) THEN
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   299
        try_points ctxt
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   300
      end
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   301
    and try_points ctxt goal = (
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   302
      if is_exists (Thm.cprem_of goal 1) then
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   303
        FIRST (map (try_point ctxt) points)
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   304
      else if is_forall (Thm.cprem_of goal 1) then
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   305
        HEADGOAL (resolve_tac ctxt @{thms HOL.allI} THEN'
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   306
        Subgoal.FOCUS (fn {context = ctxt', ...} =>
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   307
          trace_tac ctxt "Removed universal quantifier" THEN
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   308
          try_points ctxt') ctxt)
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   309
      else basic_metric_arith_tac ctxt metric_ty) goal
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   310
  in
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   311
    try_points ctxt goal
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   312
  end
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   313
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   314
fun metric_arith_tac ctxt =
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   315
  (* unfold common definitions to get rid of sets *)
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   316
  unfold_tac ctxt THEN'
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   317
  (* remove all meta-level connectives *)
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   318
  IF_UNSOLVED' (Object_Logic.full_atomize_tac ctxt) THEN'
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   319
  (* convert goal to prenex form *)
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   320
  IF_UNSOLVED' (prenex_tac ctxt) THEN'
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   321
  (* and NNF to ? *)
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   322
  IF_UNSOLVED' (nnf_tac ctxt) THEN'
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   323
  (* turn all universally quantified variables into free variables, by focusing the subgoal *)
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   324
  REPEAT' (resolve_tac ctxt @{thms HOL.allI}) THEN'
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   325
  IF_UNSOLVED' (SUBPROOF (fn {context=ctxt', ...} =>
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   326
    trace_tac ctxt' "Focused on subgoal" THEN
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   327
    elim_exists ctxt') ctxt)
74546
6df92c387063 proper file headers;
wenzelm
parents: 74545
diff changeset
   328
70951
678b2abe9f7d decision procedure for metric spaces, implemented by Maximilian Schäffeler
immler
parents:
diff changeset
   329
end