src/HOL/Tools/Metis/metis_reconstruct.ML
author blanchet
Mon, 06 Jun 2011 20:36:34 +0200
changeset 43159 29b55f292e0b
parent 43136 cf5cda219058
child 43162 9a8acc5adfa3
permissions -rw-r--r--
added support for helpers in new Metis, so far only for polymorphic type encodings
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
39958
88c9aa5666de tuned comments
blanchet
parents: 39953
diff changeset
     1
(*  Title:      HOL/Tools/Metis/metis_reconstruct.ML
39495
bb4fb9ffe2d1 added new "Metis_Reconstruct" module, temporarily empty
blanchet
parents:
diff changeset
     2
    Author:     Kong W. Susanto, Cambridge University Computer Laboratory
bb4fb9ffe2d1 added new "Metis_Reconstruct" module, temporarily empty
blanchet
parents:
diff changeset
     3
    Author:     Lawrence C. Paulson, Cambridge University Computer Laboratory
bb4fb9ffe2d1 added new "Metis_Reconstruct" module, temporarily empty
blanchet
parents:
diff changeset
     4
    Author:     Jasmin Blanchette, TU Muenchen
bb4fb9ffe2d1 added new "Metis_Reconstruct" module, temporarily empty
blanchet
parents:
diff changeset
     5
    Copyright   Cambridge University 2007
bb4fb9ffe2d1 added new "Metis_Reconstruct" module, temporarily empty
blanchet
parents:
diff changeset
     6
bb4fb9ffe2d1 added new "Metis_Reconstruct" module, temporarily empty
blanchet
parents:
diff changeset
     7
Proof reconstruction for Metis.
bb4fb9ffe2d1 added new "Metis_Reconstruct" module, temporarily empty
blanchet
parents:
diff changeset
     8
*)
bb4fb9ffe2d1 added new "Metis_Reconstruct" module, temporarily empty
blanchet
parents:
diff changeset
     9
bb4fb9ffe2d1 added new "Metis_Reconstruct" module, temporarily empty
blanchet
parents:
diff changeset
    10
signature METIS_RECONSTRUCT =
bb4fb9ffe2d1 added new "Metis_Reconstruct" module, temporarily empty
blanchet
parents:
diff changeset
    11
sig
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
    12
  type mode = Metis_Translate.mode
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
    13
42650
552eae49f97d reintroduce this idea of running "metisFT" after a failed "metis" -- I took it out in e85ce10cef1a because I couldn't think of a reasonable use case, but now that ATPs use sound encodings and include dangerous facts (e.g. True_or_False) it makes more sense than ever to run "metisFT" after "metis"
blanchet
parents: 42616
diff changeset
    14
  exception METIS of string * string
552eae49f97d reintroduce this idea of running "metisFT" after a failed "metis" -- I took it out in e85ce10cef1a because I couldn't think of a reasonable use case, but now that ATPs use sound encodings and include dangerous facts (e.g. True_or_False) it makes more sense than ever to run "metisFT" after "metis"
blanchet
parents: 42616
diff changeset
    15
39978
11bfb7e7cc86 added "trace_metis" configuration option, replacing old-fashioned references
blanchet
parents: 39964
diff changeset
    16
  val trace : bool Config.T
11bfb7e7cc86 added "trace_metis" configuration option, replacing old-fashioned references
blanchet
parents: 39964
diff changeset
    17
  val trace_msg : Proof.context -> (unit -> string) -> unit
40665
1a65f0c74827 added "verbose" option to Metis to shut up its warnings if necessary
blanchet
parents: 40264
diff changeset
    18
  val verbose : bool Config.T
1a65f0c74827 added "verbose" option to Metis to shut up its warnings if necessary
blanchet
parents: 40264
diff changeset
    19
  val verbose_warning : Proof.context -> string -> unit
43159
29b55f292e0b added support for helpers in new Metis, so far only for polymorphic type encodings
blanchet
parents: 43136
diff changeset
    20
  val hol_clause_from_metis :
29b55f292e0b added support for helpers in new Metis, so far only for polymorphic type encodings
blanchet
parents: 43136
diff changeset
    21
    Proof.context -> int Symtab.table -> Metis_Thm.thm -> term
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
    22
  val lookth : (Metis_Thm.thm * 'a) list -> Metis_Thm.thm -> 'a
43134
0c82e00ba63e make sure no warnings are given for polymorphic facts where we use a monomorphic instance
blanchet
parents: 43130
diff changeset
    23
  val untyped_aconv : term * term -> bool
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
    24
  val replay_one_inference :
43094
269300fb83d0 more work on new Metis
blanchet
parents: 43093
diff changeset
    25
    Proof.context -> mode -> (string * term) list -> int Symtab.table
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
    26
    -> Metis_Thm.thm * Metis_Proof.inference -> (Metis_Thm.thm * thm) list
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
    27
    -> (Metis_Thm.thm * thm) list
39964
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
    28
  val discharge_skolem_premises :
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
    29
    Proof.context -> (thm * term) option list -> thm -> thm
39495
bb4fb9ffe2d1 added new "Metis_Reconstruct" module, temporarily empty
blanchet
parents:
diff changeset
    30
end;
bb4fb9ffe2d1 added new "Metis_Reconstruct" module, temporarily empty
blanchet
parents:
diff changeset
    31
bb4fb9ffe2d1 added new "Metis_Reconstruct" module, temporarily empty
blanchet
parents:
diff changeset
    32
structure Metis_Reconstruct : METIS_RECONSTRUCT =
bb4fb9ffe2d1 added new "Metis_Reconstruct" module, temporarily empty
blanchet
parents:
diff changeset
    33
struct
bb4fb9ffe2d1 added new "Metis_Reconstruct" module, temporarily empty
blanchet
parents:
diff changeset
    34
43094
269300fb83d0 more work on new Metis
blanchet
parents: 43093
diff changeset
    35
open ATP_Problem
43085
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents: 42650
diff changeset
    36
open ATP_Translate
43094
269300fb83d0 more work on new Metis
blanchet
parents: 43093
diff changeset
    37
open ATP_Reconstruct
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
    38
open Metis_Translate
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
    39
42650
552eae49f97d reintroduce this idea of running "metisFT" after a failed "metis" -- I took it out in e85ce10cef1a because I couldn't think of a reasonable use case, but now that ATPs use sound encodings and include dangerous facts (e.g. True_or_False) it makes more sense than ever to run "metisFT" after "metis"
blanchet
parents: 42616
diff changeset
    40
exception METIS of string * string
552eae49f97d reintroduce this idea of running "metisFT" after a failed "metis" -- I took it out in e85ce10cef1a because I couldn't think of a reasonable use case, but now that ATPs use sound encodings and include dangerous facts (e.g. True_or_False) it makes more sense than ever to run "metisFT" after "metis"
blanchet
parents: 42616
diff changeset
    41
42616
92715b528e78 added Attrib.setup_config_XXX conveniences, with implicit setup of the background theory;
wenzelm
parents: 42570
diff changeset
    42
val trace = Attrib.setup_config_bool @{binding metis_trace} (K false)
92715b528e78 added Attrib.setup_config_XXX conveniences, with implicit setup of the background theory;
wenzelm
parents: 42570
diff changeset
    43
val verbose = Attrib.setup_config_bool @{binding metis_verbose} (K true)
39978
11bfb7e7cc86 added "trace_metis" configuration option, replacing old-fashioned references
blanchet
parents: 39964
diff changeset
    44
11bfb7e7cc86 added "trace_metis" configuration option, replacing old-fashioned references
blanchet
parents: 39964
diff changeset
    45
fun trace_msg ctxt msg = if Config.get ctxt trace then tracing (msg ()) else ()
40665
1a65f0c74827 added "verbose" option to Metis to shut up its warnings if necessary
blanchet
parents: 40264
diff changeset
    46
fun verbose_warning ctxt msg =
42650
552eae49f97d reintroduce this idea of running "metisFT" after a failed "metis" -- I took it out in e85ce10cef1a because I couldn't think of a reasonable use case, but now that ATPs use sound encodings and include dangerous facts (e.g. True_or_False) it makes more sense than ever to run "metisFT" after "metis"
blanchet
parents: 42616
diff changeset
    47
  if Config.get ctxt verbose then warning ("Metis: " ^ msg) else ()
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
    48
39498
e8aef7ea9cbb make "subst_translation" more robust w.r.t. type instantiations like {_1234 |-> 'a}
blanchet
parents: 39497
diff changeset
    49
datatype term_or_type = SomeTerm of term | SomeType of typ
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
    50
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
    51
fun terms_of [] = []
39498
e8aef7ea9cbb make "subst_translation" more robust w.r.t. type instantiations like {_1234 |-> 'a}
blanchet
parents: 39497
diff changeset
    52
  | terms_of (SomeTerm t :: tts) = t :: terms_of tts
e8aef7ea9cbb make "subst_translation" more robust w.r.t. type instantiations like {_1234 |-> 'a}
blanchet
parents: 39497
diff changeset
    53
  | terms_of (SomeType _ :: tts) = terms_of tts;
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
    54
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
    55
fun types_of [] = []
43135
8c32a0160b0d more uniform handling of tfree sort inference in ATP reconstruction code, based on what Metis always has done
blanchet
parents: 43134
diff changeset
    56
  | types_of (SomeTerm (Var ((a, idx), _)) :: tts) =
8c32a0160b0d more uniform handling of tfree sort inference in ATP reconstruction code, based on what Metis always has done
blanchet
parents: 43134
diff changeset
    57
    types_of tts
8c32a0160b0d more uniform handling of tfree sort inference in ATP reconstruction code, based on what Metis always has done
blanchet
parents: 43134
diff changeset
    58
    |> (if String.isPrefix metis_generated_var_prefix a then
8c32a0160b0d more uniform handling of tfree sort inference in ATP reconstruction code, based on what Metis always has done
blanchet
parents: 43134
diff changeset
    59
          (* Variable generated by Metis, which might have been a type
8c32a0160b0d more uniform handling of tfree sort inference in ATP reconstruction code, based on what Metis always has done
blanchet
parents: 43134
diff changeset
    60
             variable. *)
8c32a0160b0d more uniform handling of tfree sort inference in ATP reconstruction code, based on what Metis always has done
blanchet
parents: 43134
diff changeset
    61
          cons (TVar (("'" ^ a, idx), HOLogic.typeS))
8c32a0160b0d more uniform handling of tfree sort inference in ATP reconstruction code, based on what Metis always has done
blanchet
parents: 43134
diff changeset
    62
        else
8c32a0160b0d more uniform handling of tfree sort inference in ATP reconstruction code, based on what Metis always has done
blanchet
parents: 43134
diff changeset
    63
          I)
39498
e8aef7ea9cbb make "subst_translation" more robust w.r.t. type instantiations like {_1234 |-> 'a}
blanchet
parents: 39497
diff changeset
    64
  | types_of (SomeTerm _ :: tts) = types_of tts
43135
8c32a0160b0d more uniform handling of tfree sort inference in ATP reconstruction code, based on what Metis always has done
blanchet
parents: 43134
diff changeset
    65
  | types_of (SomeType T :: tts) = T :: types_of tts
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
    66
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
    67
fun apply_list rator nargs rands =
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
    68
  let val trands = terms_of rands
39498
e8aef7ea9cbb make "subst_translation" more robust w.r.t. type instantiations like {_1234 |-> 'a}
blanchet
parents: 39497
diff changeset
    69
  in  if length trands = nargs then SomeTerm (list_comb(rator, trands))
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
    70
      else raise Fail
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
    71
        ("apply_list: wrong number of arguments: " ^ Syntax.string_of_term_global Pure.thy rator ^
41491
a2ad5b824051 eliminated Int.toString;
wenzelm
parents: 41143
diff changeset
    72
          " expected " ^ string_of_int nargs ^
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
    73
          " received " ^ commas (map (Syntax.string_of_term_global Pure.thy) trands))
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
    74
  end;
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
    75
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
    76
fun infer_types ctxt =
42361
23f352990944 modernized structure Proof_Context;
wenzelm
parents: 42354
diff changeset
    77
  Syntax.check_terms (Proof_Context.set_mode Proof_Context.mode_pattern ctxt);
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
    78
43094
269300fb83d0 more work on new Metis
blanchet
parents: 43093
diff changeset
    79
(* We use 1 rather than 0 because variable references in clauses may otherwise
269300fb83d0 more work on new Metis
blanchet
parents: 43093
diff changeset
    80
   conflict with variable constraints in the goal...at least, type inference
269300fb83d0 more work on new Metis
blanchet
parents: 43093
diff changeset
    81
   often fails otherwise. See also "axiom_inf" below. *)
43135
8c32a0160b0d more uniform handling of tfree sort inference in ATP reconstruction code, based on what Metis always has done
blanchet
parents: 43134
diff changeset
    82
fun make_var (w, T) = Var ((w, 1), T)
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
    83
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
    84
(*Remove the "apply" operator from an HO term*)
43094
269300fb83d0 more work on new Metis
blanchet
parents: 43093
diff changeset
    85
fun strip_happ args (Metis_Term.Fn (".", [t, u])) = strip_happ (u :: args) t
269300fb83d0 more work on new Metis
blanchet
parents: 43093
diff changeset
    86
  | strip_happ args x = (x, args)
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
    87
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
    88
fun hol_type_from_metis_term _ (Metis_Term.Var v) =
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
    89
     (case strip_prefix_and_unascii tvar_prefix v of
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
    90
          SOME w => make_tvar w
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
    91
        | NONE   => make_tvar v)
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
    92
  | hol_type_from_metis_term ctxt (Metis_Term.Fn(x, tys)) =
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
    93
     (case strip_prefix_and_unascii type_const_prefix x of
41140
9c68004b8c9d added Sledgehammer support for higher-order propositional reasoning
blanchet
parents: 41139
diff changeset
    94
          SOME tc => Type (invert_const tc,
39498
e8aef7ea9cbb make "subst_translation" more robust w.r.t. type instantiations like {_1234 |-> 'a}
blanchet
parents: 39497
diff changeset
    95
                           map (hol_type_from_metis_term ctxt) tys)
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
    96
        | NONE    =>
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
    97
      case strip_prefix_and_unascii tfree_prefix x of
43135
8c32a0160b0d more uniform handling of tfree sort inference in ATP reconstruction code, based on what Metis always has done
blanchet
parents: 43134
diff changeset
    98
          SOME tf => make_tfree ctxt tf
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
    99
        | NONE    => raise Fail ("hol_type_from_metis_term: " ^ x));
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   100
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   101
(*Maps metis terms to isabelle terms*)
42341
5a00af7f4978 removed obsolete Skolem counter in new Skolemizer
blanchet
parents: 42339
diff changeset
   102
fun hol_term_from_metis_PT ctxt fol_tm =
42361
23f352990944 modernized structure Proof_Context;
wenzelm
parents: 42354
diff changeset
   103
  let val thy = Proof_Context.theory_of ctxt
39978
11bfb7e7cc86 added "trace_metis" configuration option, replacing old-fashioned references
blanchet
parents: 39964
diff changeset
   104
      val _ = trace_msg ctxt (fn () => "hol_term_from_metis_PT: " ^
11bfb7e7cc86 added "trace_metis" configuration option, replacing old-fashioned references
blanchet
parents: 39964
diff changeset
   105
                                       Metis_Term.toString fol_tm)
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   106
      fun tm_to_tt (Metis_Term.Var v) =
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   107
             (case strip_prefix_and_unascii tvar_prefix v of
39498
e8aef7ea9cbb make "subst_translation" more robust w.r.t. type instantiations like {_1234 |-> 'a}
blanchet
parents: 39497
diff changeset
   108
                  SOME w => SomeType (make_tvar w)
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   109
                | NONE =>
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   110
              case strip_prefix_and_unascii schematic_var_prefix v of
43135
8c32a0160b0d more uniform handling of tfree sort inference in ATP reconstruction code, based on what Metis always has done
blanchet
parents: 43134
diff changeset
   111
                  SOME w => SomeTerm (make_var (w, HOLogic.typeT))
8c32a0160b0d more uniform handling of tfree sort inference in ATP reconstruction code, based on what Metis always has done
blanchet
parents: 43134
diff changeset
   112
                | NONE   => SomeTerm (make_var (v, HOLogic.typeT)))
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   113
                    (*Var from Metis with a name like _nnn; possibly a type variable*)
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   114
        | tm_to_tt (Metis_Term.Fn ("{}", [arg])) = tm_to_tt arg   (*hBOOL*)
43094
269300fb83d0 more work on new Metis
blanchet
parents: 43093
diff changeset
   115
        | tm_to_tt (t as Metis_Term.Fn (".", _)) =
269300fb83d0 more work on new Metis
blanchet
parents: 43093
diff changeset
   116
            let val (rator,rands) = strip_happ [] t in
269300fb83d0 more work on new Metis
blanchet
parents: 43093
diff changeset
   117
              case rator of
269300fb83d0 more work on new Metis
blanchet
parents: 43093
diff changeset
   118
                Metis_Term.Fn(fname,ts) => applic_to_tt (fname, ts @ rands)
269300fb83d0 more work on new Metis
blanchet
parents: 43093
diff changeset
   119
              | _ => case tm_to_tt rator of
269300fb83d0 more work on new Metis
blanchet
parents: 43093
diff changeset
   120
                         SomeTerm t => SomeTerm (list_comb(t, terms_of (map tm_to_tt rands)))
269300fb83d0 more work on new Metis
blanchet
parents: 43093
diff changeset
   121
                       | _ => raise Fail "tm_to_tt: HO application"
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   122
            end
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   123
        | tm_to_tt (Metis_Term.Fn (fname, args)) = applic_to_tt (fname,args)
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   124
      and applic_to_tt ("=",ts) =
39498
e8aef7ea9cbb make "subst_translation" more robust w.r.t. type instantiations like {_1234 |-> 'a}
blanchet
parents: 39497
diff changeset
   125
            SomeTerm (list_comb(Const (@{const_name HOL.eq}, HOLogic.typeT), terms_of (map tm_to_tt ts)))
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   126
        | applic_to_tt (a,ts) =
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   127
            case strip_prefix_and_unascii const_prefix a of
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   128
                SOME b =>
39886
8a9f0c97d550 first step towards a new skolemizer that doesn't require "Eps"
blanchet
parents: 39550
diff changeset
   129
                  let
42570
77f94ac04f32 cleanup proxification/unproxification and make sure that "num_atp_type_args" is called on the proxy in the reconstruction code, since "c_fequal" has one type arg but the unproxified equal has 0
blanchet
parents: 42364
diff changeset
   130
                    val c = b |> invert_const |> unproxify_const
39886
8a9f0c97d550 first step towards a new skolemizer that doesn't require "Eps"
blanchet
parents: 39550
diff changeset
   131
                    val ntypes = num_type_args thy c
8a9f0c97d550 first step towards a new skolemizer that doesn't require "Eps"
blanchet
parents: 39550
diff changeset
   132
                    val nterms = length ts - ntypes
8a9f0c97d550 first step towards a new skolemizer that doesn't require "Eps"
blanchet
parents: 39550
diff changeset
   133
                    val tts = map tm_to_tt ts
8a9f0c97d550 first step towards a new skolemizer that doesn't require "Eps"
blanchet
parents: 39550
diff changeset
   134
                    val tys = types_of (List.take(tts,ntypes))
39939
6e9aff5ee9b5 paramify new skolems just like old ones (cf. reveal_old_skolem_terms)
blanchet
parents: 39896
diff changeset
   135
                    val t =
6e9aff5ee9b5 paramify new skolems just like old ones (cf. reveal_old_skolem_terms)
blanchet
parents: 39896
diff changeset
   136
                      if String.isPrefix new_skolem_const_prefix c then
42341
5a00af7f4978 removed obsolete Skolem counter in new Skolemizer
blanchet
parents: 42339
diff changeset
   137
                        Var ((new_skolem_var_name_from_const c, 1),
39939
6e9aff5ee9b5 paramify new skolems just like old ones (cf. reveal_old_skolem_terms)
blanchet
parents: 39896
diff changeset
   138
                             Type_Infer.paramify_vars (tl tys ---> hd tys))
6e9aff5ee9b5 paramify new skolems just like old ones (cf. reveal_old_skolem_terms)
blanchet
parents: 39896
diff changeset
   139
                      else
6e9aff5ee9b5 paramify new skolems just like old ones (cf. reveal_old_skolem_terms)
blanchet
parents: 39896
diff changeset
   140
                        Const (c, dummyT)
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   141
                  in if length tys = ntypes then
39886
8a9f0c97d550 first step towards a new skolemizer that doesn't require "Eps"
blanchet
parents: 39550
diff changeset
   142
                         apply_list t nterms (List.drop(tts,ntypes))
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   143
                     else
41491
a2ad5b824051 eliminated Int.toString;
wenzelm
parents: 41143
diff changeset
   144
                       raise Fail ("Constant " ^ c ^ " expects " ^ string_of_int ntypes ^
a2ad5b824051 eliminated Int.toString;
wenzelm
parents: 41143
diff changeset
   145
                                   " but gets " ^ string_of_int (length tys) ^
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   146
                                   " type arguments\n" ^
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   147
                                   cat_lines (map (Syntax.string_of_typ ctxt) tys) ^
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   148
                                   " the terms are \n" ^
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   149
                                   cat_lines (map (Syntax.string_of_term ctxt) (terms_of tts)))
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   150
                     end
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   151
              | NONE => (*Not a constant. Is it a type constructor?*)
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   152
            case strip_prefix_and_unascii type_const_prefix a of
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   153
                SOME b =>
41140
9c68004b8c9d added Sledgehammer support for higher-order propositional reasoning
blanchet
parents: 41139
diff changeset
   154
                SomeType (Type (invert_const b, types_of (map tm_to_tt ts)))
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   155
              | NONE => (*Maybe a TFree. Should then check that ts=[].*)
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   156
            case strip_prefix_and_unascii tfree_prefix a of
43135
8c32a0160b0d more uniform handling of tfree sort inference in ATP reconstruction code, based on what Metis always has done
blanchet
parents: 43134
diff changeset
   157
                SOME b => SomeType (make_tfree ctxt b)
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   158
              | NONE => (*a fixed variable? They are Skolem functions.*)
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   159
            case strip_prefix_and_unascii fixed_var_prefix a of
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   160
                SOME b =>
39498
e8aef7ea9cbb make "subst_translation" more robust w.r.t. type instantiations like {_1234 |-> 'a}
blanchet
parents: 39497
diff changeset
   161
                  let val opr = Free (b, HOLogic.typeT)
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   162
                  in  apply_list opr (length ts) (map tm_to_tt ts)  end
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   163
              | NONE => raise Fail ("unexpected metis function: " ^ a)
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   164
  in
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   165
    case tm_to_tt fol_tm of
39498
e8aef7ea9cbb make "subst_translation" more robust w.r.t. type instantiations like {_1234 |-> 'a}
blanchet
parents: 39497
diff changeset
   166
      SomeTerm t => t
e8aef7ea9cbb make "subst_translation" more robust w.r.t. type instantiations like {_1234 |-> 'a}
blanchet
parents: 39497
diff changeset
   167
    | SomeType T => raise TYPE ("fol_tm_to_tt: Term expected", [T], [])
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   168
  end
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   169
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   170
(*Maps fully-typed metis terms to isabelle terms*)
42341
5a00af7f4978 removed obsolete Skolem counter in new Skolemizer
blanchet
parents: 42339
diff changeset
   171
fun hol_term_from_metis_FT ctxt fol_tm =
39978
11bfb7e7cc86 added "trace_metis" configuration option, replacing old-fashioned references
blanchet
parents: 39964
diff changeset
   172
  let val _ = trace_msg ctxt (fn () => "hol_term_from_metis_FT: " ^
11bfb7e7cc86 added "trace_metis" configuration option, replacing old-fashioned references
blanchet
parents: 39964
diff changeset
   173
                                       Metis_Term.toString fol_tm)
42337
fef417b12f38 make new Skolemizer work also for "metisFT"
blanchet
parents: 42333
diff changeset
   174
      fun do_const c =
42570
77f94ac04f32 cleanup proxification/unproxification and make sure that "num_atp_type_args" is called on the proxy in the reconstruction code, since "c_fequal" has one type arg but the unproxified equal has 0
blanchet
parents: 42364
diff changeset
   175
        let val c = c |> invert_const |> unproxify_const in
42337
fef417b12f38 make new Skolemizer work also for "metisFT"
blanchet
parents: 42333
diff changeset
   176
          if String.isPrefix new_skolem_const_prefix c then
fef417b12f38 make new Skolemizer work also for "metisFT"
blanchet
parents: 42333
diff changeset
   177
            Var ((new_skolem_var_name_from_const c, 1), dummyT)
fef417b12f38 make new Skolemizer work also for "metisFT"
blanchet
parents: 42333
diff changeset
   178
          else
fef417b12f38 make new Skolemizer work also for "metisFT"
blanchet
parents: 42333
diff changeset
   179
            Const (c, dummyT)
fef417b12f38 make new Skolemizer work also for "metisFT"
blanchet
parents: 42333
diff changeset
   180
        end
43104
81d1b15aa0ae use ":" for type information (looks good in Metis's output) and handle it in new path finder
blanchet
parents: 43103
diff changeset
   181
      fun cvt (Metis_Term.Fn (":", [Metis_Term.Var v, _])) =
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   182
             (case strip_prefix_and_unascii schematic_var_prefix v of
43135
8c32a0160b0d more uniform handling of tfree sort inference in ATP reconstruction code, based on what Metis always has done
blanchet
parents: 43134
diff changeset
   183
                  SOME w =>  make_var (w, dummyT)
8c32a0160b0d more uniform handling of tfree sort inference in ATP reconstruction code, based on what Metis always has done
blanchet
parents: 43134
diff changeset
   184
                | NONE   => make_var (v, dummyT))
43104
81d1b15aa0ae use ":" for type information (looks good in Metis's output) and handle it in new path finder
blanchet
parents: 43103
diff changeset
   185
        | cvt (Metis_Term.Fn (":", [Metis_Term.Fn ("=",[]), _])) =
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   186
            Const (@{const_name HOL.eq}, HOLogic.typeT)
43104
81d1b15aa0ae use ":" for type information (looks good in Metis's output) and handle it in new path finder
blanchet
parents: 43103
diff changeset
   187
        | cvt (Metis_Term.Fn (":", [Metis_Term.Fn (x,[]), ty])) =
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   188
           (case strip_prefix_and_unascii const_prefix x of
42337
fef417b12f38 make new Skolemizer work also for "metisFT"
blanchet
parents: 42333
diff changeset
   189
                SOME c => do_const c
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   190
              | NONE => (*Not a constant. Is it a fixed variable??*)
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   191
            case strip_prefix_and_unascii fixed_var_prefix x of
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   192
                SOME v => Free (v, hol_type_from_metis_term ctxt ty)
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   193
              | NONE => raise Fail ("hol_term_from_metis_FT bad constant: " ^ x))
43104
81d1b15aa0ae use ":" for type information (looks good in Metis's output) and handle it in new path finder
blanchet
parents: 43103
diff changeset
   194
        | cvt (Metis_Term.Fn (":", [Metis_Term.Fn (".", [tm1,tm2]), _])) =
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   195
            cvt tm1 $ cvt tm2
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   196
        | cvt (Metis_Term.Fn (".",[tm1,tm2])) = (*untyped application*)
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   197
            cvt tm1 $ cvt tm2
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   198
        | cvt (Metis_Term.Fn ("{}", [arg])) = cvt arg   (*hBOOL*)
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   199
        | cvt (Metis_Term.Fn ("=", [tm1,tm2])) =
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   200
            list_comb(Const (@{const_name HOL.eq}, HOLogic.typeT), map cvt [tm1,tm2])
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   201
        | cvt (t as Metis_Term.Fn (x, [])) =
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   202
           (case strip_prefix_and_unascii const_prefix x of
42337
fef417b12f38 make new Skolemizer work also for "metisFT"
blanchet
parents: 42333
diff changeset
   203
                SOME c => do_const c
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   204
              | NONE => (*Not a constant. Is it a fixed variable??*)
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   205
            case strip_prefix_and_unascii fixed_var_prefix x of
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   206
                SOME v => Free (v, dummyT)
39978
11bfb7e7cc86 added "trace_metis" configuration option, replacing old-fashioned references
blanchet
parents: 39964
diff changeset
   207
              | NONE => (trace_msg ctxt (fn () =>
11bfb7e7cc86 added "trace_metis" configuration option, replacing old-fashioned references
blanchet
parents: 39964
diff changeset
   208
                                      "hol_term_from_metis_FT bad const: " ^ x);
42341
5a00af7f4978 removed obsolete Skolem counter in new Skolemizer
blanchet
parents: 42339
diff changeset
   209
                         hol_term_from_metis_PT ctxt t))
39978
11bfb7e7cc86 added "trace_metis" configuration option, replacing old-fashioned references
blanchet
parents: 39964
diff changeset
   210
        | cvt t = (trace_msg ctxt (fn () =>
11bfb7e7cc86 added "trace_metis" configuration option, replacing old-fashioned references
blanchet
parents: 39964
diff changeset
   211
                   "hol_term_from_metis_FT bad term: " ^ Metis_Term.toString t);
42341
5a00af7f4978 removed obsolete Skolem counter in new Skolemizer
blanchet
parents: 42339
diff changeset
   212
                   hol_term_from_metis_PT ctxt t)
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   213
  in fol_tm |> cvt end
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   214
43094
269300fb83d0 more work on new Metis
blanchet
parents: 43093
diff changeset
   215
fun atp_name_from_metis s =
43104
81d1b15aa0ae use ":" for type information (looks good in Metis's output) and handle it in new path finder
blanchet
parents: 43103
diff changeset
   216
  case find_first (fn (_, (s', _)) => s' = s) metis_name_table of
81d1b15aa0ae use ":" for type information (looks good in Metis's output) and handle it in new path finder
blanchet
parents: 43103
diff changeset
   217
    SOME ((s, _), (_, swap)) => (s, swap)
81d1b15aa0ae use ":" for type information (looks good in Metis's output) and handle it in new path finder
blanchet
parents: 43103
diff changeset
   218
  | _ => (s, false)
43094
269300fb83d0 more work on new Metis
blanchet
parents: 43093
diff changeset
   219
fun atp_term_from_metis (Metis_Term.Fn (s, tms)) =
43104
81d1b15aa0ae use ":" for type information (looks good in Metis's output) and handle it in new path finder
blanchet
parents: 43103
diff changeset
   220
    let val (s, swap) = atp_name_from_metis s in
81d1b15aa0ae use ":" for type information (looks good in Metis's output) and handle it in new path finder
blanchet
parents: 43103
diff changeset
   221
      ATerm (s, tms |> map atp_term_from_metis |> swap ? rev)
81d1b15aa0ae use ":" for type information (looks good in Metis's output) and handle it in new path finder
blanchet
parents: 43103
diff changeset
   222
    end
43094
269300fb83d0 more work on new Metis
blanchet
parents: 43093
diff changeset
   223
  | atp_term_from_metis (Metis_Term.Var s) = ATerm (s, [])
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   224
43135
8c32a0160b0d more uniform handling of tfree sort inference in ATP reconstruction code, based on what Metis always has done
blanchet
parents: 43134
diff changeset
   225
fun hol_term_from_metis_MX ctxt sym_tab =
8c32a0160b0d more uniform handling of tfree sort inference in ATP reconstruction code, based on what Metis always has done
blanchet
parents: 43134
diff changeset
   226
  atp_term_from_metis #> term_from_atp ctxt false sym_tab NONE
43094
269300fb83d0 more work on new Metis
blanchet
parents: 43093
diff changeset
   227
43135
8c32a0160b0d more uniform handling of tfree sort inference in ATP reconstruction code, based on what Metis always has done
blanchet
parents: 43134
diff changeset
   228
fun hol_term_from_metis ctxt FO _ = hol_term_from_metis_PT ctxt
8c32a0160b0d more uniform handling of tfree sort inference in ATP reconstruction code, based on what Metis always has done
blanchet
parents: 43134
diff changeset
   229
  | hol_term_from_metis ctxt HO _ = hol_term_from_metis_PT ctxt
8c32a0160b0d more uniform handling of tfree sort inference in ATP reconstruction code, based on what Metis always has done
blanchet
parents: 43134
diff changeset
   230
  | hol_term_from_metis ctxt FT _ = hol_term_from_metis_FT ctxt
8c32a0160b0d more uniform handling of tfree sort inference in ATP reconstruction code, based on what Metis always has done
blanchet
parents: 43134
diff changeset
   231
  | hol_term_from_metis ctxt MX sym_tab = hol_term_from_metis_MX ctxt sym_tab
43094
269300fb83d0 more work on new Metis
blanchet
parents: 43093
diff changeset
   232
43136
cf5cda219058 handle lightweight tags sym theorems gracefully in the presence of TVars with interesting type classes
blanchet
parents: 43135
diff changeset
   233
fun atp_literal_from_metis (pos, atom) =
cf5cda219058 handle lightweight tags sym theorems gracefully in the presence of TVars with interesting type classes
blanchet
parents: 43135
diff changeset
   234
  atom |> Metis_Term.Fn |> atp_term_from_metis |> AAtom |> not pos ? mk_anot
cf5cda219058 handle lightweight tags sym theorems gracefully in the presence of TVars with interesting type classes
blanchet
parents: 43135
diff changeset
   235
fun atp_clause_from_metis [] = AAtom (ATerm (tptp_false, []))
cf5cda219058 handle lightweight tags sym theorems gracefully in the presence of TVars with interesting type classes
blanchet
parents: 43135
diff changeset
   236
  | atp_clause_from_metis lits =
cf5cda219058 handle lightweight tags sym theorems gracefully in the presence of TVars with interesting type classes
blanchet
parents: 43135
diff changeset
   237
    lits |> map atp_literal_from_metis |> mk_aconns AOr
cf5cda219058 handle lightweight tags sym theorems gracefully in the presence of TVars with interesting type classes
blanchet
parents: 43135
diff changeset
   238
43159
29b55f292e0b added support for helpers in new Metis, so far only for polymorphic type encodings
blanchet
parents: 43136
diff changeset
   239
fun hol_clause_from_metis ctxt sym_tab =
29b55f292e0b added support for helpers in new Metis, so far only for polymorphic type encodings
blanchet
parents: 43136
diff changeset
   240
  Metis_Thm.clause
29b55f292e0b added support for helpers in new Metis, so far only for polymorphic type encodings
blanchet
parents: 43136
diff changeset
   241
  #> Metis_LiteralSet.toList
29b55f292e0b added support for helpers in new Metis, so far only for polymorphic type encodings
blanchet
parents: 43136
diff changeset
   242
  #> atp_clause_from_metis
29b55f292e0b added support for helpers in new Metis, so far only for polymorphic type encodings
blanchet
parents: 43136
diff changeset
   243
  #> prop_from_atp ctxt false sym_tab
43136
cf5cda219058 handle lightweight tags sym theorems gracefully in the presence of TVars with interesting type classes
blanchet
parents: 43135
diff changeset
   244
43094
269300fb83d0 more work on new Metis
blanchet
parents: 43093
diff changeset
   245
fun hol_terms_from_metis ctxt mode old_skolems sym_tab fol_tms =
43135
8c32a0160b0d more uniform handling of tfree sort inference in ATP reconstruction code, based on what Metis always has done
blanchet
parents: 43134
diff changeset
   246
  let val ts = map (hol_term_from_metis ctxt mode sym_tab) fol_tms
39978
11bfb7e7cc86 added "trace_metis" configuration option, replacing old-fashioned references
blanchet
parents: 39964
diff changeset
   247
      val _ = trace_msg ctxt (fn () => "  calling type inference:")
11bfb7e7cc86 added "trace_metis" configuration option, replacing old-fashioned references
blanchet
parents: 39964
diff changeset
   248
      val _ = app (fn t => trace_msg ctxt
11bfb7e7cc86 added "trace_metis" configuration option, replacing old-fashioned references
blanchet
parents: 39964
diff changeset
   249
                                     (fn () => Syntax.string_of_term ctxt t)) ts
39886
8a9f0c97d550 first step towards a new skolemizer that doesn't require "Eps"
blanchet
parents: 39550
diff changeset
   250
      val ts' = ts |> map (reveal_old_skolem_terms old_skolems)
8a9f0c97d550 first step towards a new skolemizer that doesn't require "Eps"
blanchet
parents: 39550
diff changeset
   251
                   |> infer_types ctxt
39978
11bfb7e7cc86 added "trace_metis" configuration option, replacing old-fashioned references
blanchet
parents: 39964
diff changeset
   252
      val _ = app (fn t => trace_msg ctxt
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   253
                    (fn () => "  final term: " ^ Syntax.string_of_term ctxt t ^
43128
a19826080596 tuned names
blanchet
parents: 43106
diff changeset
   254
                              " of type " ^ Syntax.string_of_typ ctxt (type_of t)))
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   255
                  ts'
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   256
  in  ts'  end;
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   257
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   258
(* ------------------------------------------------------------------------- *)
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   259
(* FOL step Inference Rules                                                  *)
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   260
(* ------------------------------------------------------------------------- *)
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   261
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   262
(*for debugging only*)
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   263
(*
39978
11bfb7e7cc86 added "trace_metis" configuration option, replacing old-fashioned references
blanchet
parents: 39964
diff changeset
   264
fun print_thpair ctxt (fth,th) =
11bfb7e7cc86 added "trace_metis" configuration option, replacing old-fashioned references
blanchet
parents: 39964
diff changeset
   265
  (trace_msg ctxt (fn () => "=============================================");
11bfb7e7cc86 added "trace_metis" configuration option, replacing old-fashioned references
blanchet
parents: 39964
diff changeset
   266
   trace_msg ctxt (fn () => "Metis: " ^ Metis_Thm.toString fth);
11bfb7e7cc86 added "trace_metis" configuration option, replacing old-fashioned references
blanchet
parents: 39964
diff changeset
   267
   trace_msg ctxt (fn () => "Isabelle: " ^ Display.string_of_thm_without_context th));
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   268
*)
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   269
43094
269300fb83d0 more work on new Metis
blanchet
parents: 43093
diff changeset
   270
fun lookth th_pairs fth =
269300fb83d0 more work on new Metis
blanchet
parents: 43093
diff changeset
   271
  the (AList.lookup (uncurry Metis_Thm.equal) th_pairs fth)
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   272
  handle Option.Option =>
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   273
         raise Fail ("Failed to find Metis theorem " ^ Metis_Thm.toString fth)
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   274
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   275
fun cterm_incr_types thy idx = cterm_of thy o (map_types (Logic.incr_tvar idx));
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   276
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   277
(* INFERENCE RULE: AXIOM *)
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   278
43135
8c32a0160b0d more uniform handling of tfree sort inference in ATP reconstruction code, based on what Metis always has done
blanchet
parents: 43134
diff changeset
   279
(* This causes variables to have an index of 1 by default. See also "make_var"
43094
269300fb83d0 more work on new Metis
blanchet
parents: 43093
diff changeset
   280
   above. *)
269300fb83d0 more work on new Metis
blanchet
parents: 43093
diff changeset
   281
fun axiom_inf th_pairs th = Thm.incr_indexes 1 (lookth th_pairs th)
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   282
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   283
(* INFERENCE RULE: ASSUME *)
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   284
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   285
val EXCLUDED_MIDDLE = @{lemma "P ==> ~ P ==> False" by (rule notE)}
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   286
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   287
fun inst_excluded_middle thy i_atm =
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   288
  let val th = EXCLUDED_MIDDLE
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   289
      val [vx] = Term.add_vars (prop_of th) []
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   290
      val substs = [(cterm_of thy (Var vx), cterm_of thy i_atm)]
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   291
  in  cterm_instantiate substs th  end;
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   292
43094
269300fb83d0 more work on new Metis
blanchet
parents: 43093
diff changeset
   293
fun assume_inf ctxt mode old_skolems sym_tab atm =
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   294
  inst_excluded_middle
42361
23f352990944 modernized structure Proof_Context;
wenzelm
parents: 42354
diff changeset
   295
      (Proof_Context.theory_of ctxt)
43094
269300fb83d0 more work on new Metis
blanchet
parents: 43093
diff changeset
   296
      (singleton (hol_terms_from_metis ctxt mode old_skolems sym_tab)
40259
c0e34371c2e2 prevent type errors because of inconsistent skolem Var types by giving fresh indices to Skolems
blanchet
parents: 40258
diff changeset
   297
                 (Metis_Term.Fn atm))
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   298
39498
e8aef7ea9cbb make "subst_translation" more robust w.r.t. type instantiations like {_1234 |-> 'a}
blanchet
parents: 39497
diff changeset
   299
(* INFERENCE RULE: INSTANTIATE (SUBST). Type instantiations are ignored. Trying
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   300
   to reconstruct them admits new possibilities of errors, e.g. concerning
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   301
   sorts. Instead we try to arrange that new TVars are distinct and that types
39498
e8aef7ea9cbb make "subst_translation" more robust w.r.t. type instantiations like {_1234 |-> 'a}
blanchet
parents: 39497
diff changeset
   302
   can be inferred from terms. *)
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   303
43094
269300fb83d0 more work on new Metis
blanchet
parents: 43093
diff changeset
   304
fun inst_inf ctxt mode old_skolems sym_tab th_pairs fsubst th =
42361
23f352990944 modernized structure Proof_Context;
wenzelm
parents: 42354
diff changeset
   305
  let val thy = Proof_Context.theory_of ctxt
43094
269300fb83d0 more work on new Metis
blanchet
parents: 43093
diff changeset
   306
      val i_th = lookth th_pairs th
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   307
      val i_th_vars = Term.add_vars (prop_of i_th) []
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   308
      fun find_var x = the (List.find (fn ((a,_),_) => a=x) i_th_vars)
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   309
      fun subst_translation (x,y) =
39498
e8aef7ea9cbb make "subst_translation" more robust w.r.t. type instantiations like {_1234 |-> 'a}
blanchet
parents: 39497
diff changeset
   310
        let val v = find_var x
39886
8a9f0c97d550 first step towards a new skolemizer that doesn't require "Eps"
blanchet
parents: 39550
diff changeset
   311
            (* We call "reveal_old_skolem_terms" and "infer_types" below. *)
43135
8c32a0160b0d more uniform handling of tfree sort inference in ATP reconstruction code, based on what Metis always has done
blanchet
parents: 43134
diff changeset
   312
            val t = hol_term_from_metis ctxt mode sym_tab y
39498
e8aef7ea9cbb make "subst_translation" more robust w.r.t. type instantiations like {_1234 |-> 'a}
blanchet
parents: 39497
diff changeset
   313
        in  SOME (cterm_of thy (Var v), t)  end
e8aef7ea9cbb make "subst_translation" more robust w.r.t. type instantiations like {_1234 |-> 'a}
blanchet
parents: 39497
diff changeset
   314
        handle Option.Option =>
39978
11bfb7e7cc86 added "trace_metis" configuration option, replacing old-fashioned references
blanchet
parents: 39964
diff changeset
   315
               (trace_msg ctxt (fn () => "\"find_var\" failed for " ^ x ^
11bfb7e7cc86 added "trace_metis" configuration option, replacing old-fashioned references
blanchet
parents: 39964
diff changeset
   316
                                         " in " ^ Display.string_of_thm ctxt i_th);
39498
e8aef7ea9cbb make "subst_translation" more robust w.r.t. type instantiations like {_1234 |-> 'a}
blanchet
parents: 39497
diff changeset
   317
                NONE)
e8aef7ea9cbb make "subst_translation" more robust w.r.t. type instantiations like {_1234 |-> 'a}
blanchet
parents: 39497
diff changeset
   318
             | TYPE _ =>
39978
11bfb7e7cc86 added "trace_metis" configuration option, replacing old-fashioned references
blanchet
parents: 39964
diff changeset
   319
               (trace_msg ctxt (fn () => "\"hol_term_from_metis\" failed for " ^ x ^
11bfb7e7cc86 added "trace_metis" configuration option, replacing old-fashioned references
blanchet
parents: 39964
diff changeset
   320
                                         " in " ^ Display.string_of_thm ctxt i_th);
39498
e8aef7ea9cbb make "subst_translation" more robust w.r.t. type instantiations like {_1234 |-> 'a}
blanchet
parents: 39497
diff changeset
   321
                NONE)
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   322
      fun remove_typeinst (a, t) =
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   323
            case strip_prefix_and_unascii schematic_var_prefix a of
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   324
                SOME b => SOME (b, t)
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   325
              | NONE => case strip_prefix_and_unascii tvar_prefix a of
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   326
                SOME _ => NONE          (*type instantiations are forbidden!*)
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   327
              | NONE => SOME (a,t)    (*internal Metis var?*)
39978
11bfb7e7cc86 added "trace_metis" configuration option, replacing old-fashioned references
blanchet
parents: 39964
diff changeset
   328
      val _ = trace_msg ctxt (fn () => "  isa th: " ^ Display.string_of_thm ctxt i_th)
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   329
      val substs = map_filter remove_typeinst (Metis_Subst.toList fsubst)
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   330
      val (vars,rawtms) = ListPair.unzip (map_filter subst_translation substs)
39886
8a9f0c97d550 first step towards a new skolemizer that doesn't require "Eps"
blanchet
parents: 39550
diff changeset
   331
      val tms = rawtms |> map (reveal_old_skolem_terms old_skolems)
8a9f0c97d550 first step towards a new skolemizer that doesn't require "Eps"
blanchet
parents: 39550
diff changeset
   332
                       |> infer_types ctxt
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   333
      val ctm_of = cterm_incr_types thy (1 + Thm.maxidx_of i_th)
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   334
      val substs' = ListPair.zip (vars, map ctm_of tms)
39978
11bfb7e7cc86 added "trace_metis" configuration option, replacing old-fashioned references
blanchet
parents: 39964
diff changeset
   335
      val _ = trace_msg ctxt (fn () =>
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   336
        cat_lines ("subst_translations:" ::
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   337
          (substs' |> map (fn (x, y) =>
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   338
            Syntax.string_of_term ctxt (term_of x) ^ " |-> " ^
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   339
            Syntax.string_of_term ctxt (term_of y)))));
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   340
  in cterm_instantiate substs' i_th end
42650
552eae49f97d reintroduce this idea of running "metisFT" after a failed "metis" -- I took it out in e85ce10cef1a because I couldn't think of a reasonable use case, but now that ATPs use sound encodings and include dangerous facts (e.g. True_or_False) it makes more sense than ever to run "metisFT" after "metis"
blanchet
parents: 42616
diff changeset
   341
  handle THM (msg, _, _) => raise METIS ("inst_inf", msg)
552eae49f97d reintroduce this idea of running "metisFT" after a failed "metis" -- I took it out in e85ce10cef1a because I couldn't think of a reasonable use case, but now that ATPs use sound encodings and include dangerous facts (e.g. True_or_False) it makes more sense than ever to run "metisFT" after "metis"
blanchet
parents: 42616
diff changeset
   342
       | ERROR msg => raise METIS ("inst_inf", msg)
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   343
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   344
(* INFERENCE RULE: RESOLVE *)
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   345
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   346
(* Like RSN, but we rename apart only the type variables. Vars here typically
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   347
   have an index of 1, and the use of RSN would increase this typically to 3.
43135
8c32a0160b0d more uniform handling of tfree sort inference in ATP reconstruction code, based on what Metis always has done
blanchet
parents: 43134
diff changeset
   348
   Instantiations of those Vars could then fail. See comment on "make_var". *)
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   349
fun resolve_inc_tyvars thy tha i thb =
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   350
  let
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   351
    val tha = Drule.incr_type_indexes (1 + Thm.maxidx_of thb) tha
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   352
    fun aux tha thb =
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   353
      case Thm.bicompose false (false, tha, nprems_of tha) i thb
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   354
           |> Seq.list_of |> distinct Thm.eq_thm of
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   355
        [th] => th
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   356
      | _ => raise THM ("resolve_inc_tyvars: unique result expected", i,
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   357
                        [tha, thb])
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   358
  in
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   359
    aux tha thb
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   360
    handle TERM z =>
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   361
           (* The unifier, which is invoked from "Thm.bicompose", will sometimes
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   362
              refuse to unify "?a::?'a" with "?a::?'b" or "?a::nat" and throw a
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   363
              "TERM" exception (with "add_ffpair" as first argument). We then
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   364
              perform unification of the types of variables by hand and try
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   365
              again. We could do this the first time around but this error
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   366
              occurs seldom and we don't want to break existing proofs in subtle
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   367
              ways or slow them down needlessly. *)
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   368
           case [] |> fold (Term.add_vars o prop_of) [tha, thb]
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   369
                   |> AList.group (op =)
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   370
                   |> maps (fn ((s, _), T :: Ts) =>
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   371
                               map (fn T' => (Free (s, T), Free (s, T'))) Ts)
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   372
                   |> rpair (Envir.empty ~1)
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   373
                   |-> fold (Pattern.unify thy)
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   374
                   |> Envir.type_env |> Vartab.dest
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   375
                   |> map (fn (x, (S, T)) =>
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   376
                              pairself (ctyp_of thy) (TVar (x, S), T)) of
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   377
             [] => raise TERM z
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   378
           | ps => aux (instantiate (ps, []) tha) (instantiate (ps, []) thb)
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   379
  end
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   380
40221
d10b68c6e6d4 do not let Metis be confused by higher-order reasoning leading to literals of the form "~ ~ p", which are really the same as "p"
blanchet
parents: 40158
diff changeset
   381
fun s_not (@{const Not} $ t) = t
d10b68c6e6d4 do not let Metis be confused by higher-order reasoning leading to literals of the form "~ ~ p", which are really the same as "p"
blanchet
parents: 40158
diff changeset
   382
  | s_not t = HOLogic.mk_not t
d10b68c6e6d4 do not let Metis be confused by higher-order reasoning leading to literals of the form "~ ~ p", which are really the same as "p"
blanchet
parents: 40158
diff changeset
   383
fun simp_not_not (@{const Not} $ t) = s_not (simp_not_not t)
d10b68c6e6d4 do not let Metis be confused by higher-order reasoning leading to literals of the form "~ ~ p", which are really the same as "p"
blanchet
parents: 40158
diff changeset
   384
  | simp_not_not t = t
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   385
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   386
(* Match untyped terms. *)
43134
0c82e00ba63e make sure no warnings are given for polymorphic facts where we use a monomorphic instance
blanchet
parents: 43130
diff changeset
   387
fun untyped_aconv (Const (a, _), Const(b, _)) = (a = b)
0c82e00ba63e make sure no warnings are given for polymorphic facts where we use a monomorphic instance
blanchet
parents: 43130
diff changeset
   388
  | untyped_aconv (Free (a, _), Free (b, _)) = (a = b)
0c82e00ba63e make sure no warnings are given for polymorphic facts where we use a monomorphic instance
blanchet
parents: 43130
diff changeset
   389
  | untyped_aconv (Var ((a, _), _), Var ((b, _), _)) = (a = b)
0c82e00ba63e make sure no warnings are given for polymorphic facts where we use a monomorphic instance
blanchet
parents: 43130
diff changeset
   390
  | untyped_aconv (Bound i, Bound j) = (i = j)
0c82e00ba63e make sure no warnings are given for polymorphic facts where we use a monomorphic instance
blanchet
parents: 43130
diff changeset
   391
  | untyped_aconv (Abs (_, _, t), Abs (_, _, u)) = untyped_aconv (t, u)
0c82e00ba63e make sure no warnings are given for polymorphic facts where we use a monomorphic instance
blanchet
parents: 43130
diff changeset
   392
  | untyped_aconv (t1 $ t2, u1 $ u2) =
0c82e00ba63e make sure no warnings are given for polymorphic facts where we use a monomorphic instance
blanchet
parents: 43130
diff changeset
   393
    untyped_aconv (t1, u1) andalso untyped_aconv (t2, u2)
0c82e00ba63e make sure no warnings are given for polymorphic facts where we use a monomorphic instance
blanchet
parents: 43130
diff changeset
   394
  | untyped_aconv _ = false
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   395
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   396
(* Finding the relative location of an untyped term within a list of terms *)
40221
d10b68c6e6d4 do not let Metis be confused by higher-order reasoning leading to literals of the form "~ ~ p", which are really the same as "p"
blanchet
parents: 40158
diff changeset
   397
fun index_of_literal lit haystack =
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   398
  let
40221
d10b68c6e6d4 do not let Metis be confused by higher-order reasoning leading to literals of the form "~ ~ p", which are really the same as "p"
blanchet
parents: 40158
diff changeset
   399
    val normalize = simp_not_not o Envir.eta_contract
d10b68c6e6d4 do not let Metis be confused by higher-order reasoning leading to literals of the form "~ ~ p", which are really the same as "p"
blanchet
parents: 40158
diff changeset
   400
    val match_lit =
43134
0c82e00ba63e make sure no warnings are given for polymorphic facts where we use a monomorphic instance
blanchet
parents: 43130
diff changeset
   401
      HOLogic.dest_Trueprop #> normalize
0c82e00ba63e make sure no warnings are given for polymorphic facts where we use a monomorphic instance
blanchet
parents: 43130
diff changeset
   402
      #> curry untyped_aconv (lit |> normalize)
40221
d10b68c6e6d4 do not let Metis be confused by higher-order reasoning leading to literals of the form "~ ~ p", which are really the same as "p"
blanchet
parents: 40158
diff changeset
   403
  in case find_index match_lit haystack of ~1 => raise Empty | n => n + 1 end
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   404
39893
25a339e1ff9b move functions closer to where they're used
blanchet
parents: 39887
diff changeset
   405
(* Permute a rule's premises to move the i-th premise to the last position. *)
25a339e1ff9b move functions closer to where they're used
blanchet
parents: 39887
diff changeset
   406
fun make_last i th =
25a339e1ff9b move functions closer to where they're used
blanchet
parents: 39887
diff changeset
   407
  let val n = nprems_of th
25a339e1ff9b move functions closer to where they're used
blanchet
parents: 39887
diff changeset
   408
  in  if 1 <= i andalso i <= n
25a339e1ff9b move functions closer to where they're used
blanchet
parents: 39887
diff changeset
   409
      then Thm.permute_prems (i-1) 1 th
25a339e1ff9b move functions closer to where they're used
blanchet
parents: 39887
diff changeset
   410
      else raise THM("select_literal", i, [th])
25a339e1ff9b move functions closer to where they're used
blanchet
parents: 39887
diff changeset
   411
  end;
25a339e1ff9b move functions closer to where they're used
blanchet
parents: 39887
diff changeset
   412
42348
187354e22c7d improve on 0b05cc14c2cb: make sure that a literal variable "?foo" isn't accidentally renamed "?Q", which might be enough to confuse the new Skolemizer (cf. "Clausify.thy" example)
blanchet
parents: 42344
diff changeset
   413
(* Maps a rule that ends "... ==> P ==> False" to "... ==> ~ P" while avoiding
42349
721e85fd2db3 make 48170228f562 work also with "HO_Reas" examples
blanchet
parents: 42348
diff changeset
   414
   to create double negations. The "select" wrapper is a trick to ensure that
721e85fd2db3 make 48170228f562 work also with "HO_Reas" examples
blanchet
parents: 42348
diff changeset
   415
   "P ==> ~ False ==> False" is rewritten to "P ==> False", not to "~ P". We
721e85fd2db3 make 48170228f562 work also with "HO_Reas" examples
blanchet
parents: 42348
diff changeset
   416
   don't use this trick in general because it makes the proof object uglier than
721e85fd2db3 make 48170228f562 work also with "HO_Reas" examples
blanchet
parents: 42348
diff changeset
   417
   necessary. FIXME. *)
721e85fd2db3 make 48170228f562 work also with "HO_Reas" examples
blanchet
parents: 42348
diff changeset
   418
fun negate_head th =
721e85fd2db3 make 48170228f562 work also with "HO_Reas" examples
blanchet
parents: 42348
diff changeset
   419
  if exists (fn t => t aconv @{prop "~ False"}) (prems_of th) then
721e85fd2db3 make 48170228f562 work also with "HO_Reas" examples
blanchet
parents: 42348
diff changeset
   420
    (th RS @{thm select_FalseI})
721e85fd2db3 make 48170228f562 work also with "HO_Reas" examples
blanchet
parents: 42348
diff changeset
   421
    |> fold (rewrite_rule o single)
721e85fd2db3 make 48170228f562 work also with "HO_Reas" examples
blanchet
parents: 42348
diff changeset
   422
            @{thms not_atomize_select atomize_not_select}
721e85fd2db3 make 48170228f562 work also with "HO_Reas" examples
blanchet
parents: 42348
diff changeset
   423
  else
721e85fd2db3 make 48170228f562 work also with "HO_Reas" examples
blanchet
parents: 42348
diff changeset
   424
    th |> fold (rewrite_rule o single) @{thms not_atomize atomize_not}
39893
25a339e1ff9b move functions closer to where they're used
blanchet
parents: 39887
diff changeset
   425
25a339e1ff9b move functions closer to where they're used
blanchet
parents: 39887
diff changeset
   426
(* Maps the clause  [P1,...Pn]==>False to [P1,...,P(i-1),P(i+1),...Pn] ==> ~P *)
42348
187354e22c7d improve on 0b05cc14c2cb: make sure that a literal variable "?foo" isn't accidentally renamed "?Q", which might be enough to confuse the new Skolemizer (cf. "Clausify.thy" example)
blanchet
parents: 42344
diff changeset
   427
val select_literal = negate_head oo make_last
39893
25a339e1ff9b move functions closer to where they're used
blanchet
parents: 39887
diff changeset
   428
43094
269300fb83d0 more work on new Metis
blanchet
parents: 43093
diff changeset
   429
fun resolve_inf ctxt mode old_skolems sym_tab th_pairs atm th1 th2 =
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   430
  let
42361
23f352990944 modernized structure Proof_Context;
wenzelm
parents: 42354
diff changeset
   431
    val thy = Proof_Context.theory_of ctxt
43094
269300fb83d0 more work on new Metis
blanchet
parents: 43093
diff changeset
   432
    val (i_th1, i_th2) = pairself (lookth th_pairs) (th1, th2)
39978
11bfb7e7cc86 added "trace_metis" configuration option, replacing old-fashioned references
blanchet
parents: 39964
diff changeset
   433
    val _ = trace_msg ctxt (fn () => "  isa th1 (pos): " ^ Display.string_of_thm ctxt i_th1)
11bfb7e7cc86 added "trace_metis" configuration option, replacing old-fashioned references
blanchet
parents: 39964
diff changeset
   434
    val _ = trace_msg ctxt (fn () => "  isa th2 (neg): " ^ Display.string_of_thm ctxt i_th2)
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   435
  in
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   436
    (* Trivial cases where one operand is type info *)
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   437
    if Thm.eq_thm (TrueI, i_th1) then
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   438
      i_th2
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   439
    else if Thm.eq_thm (TrueI, i_th2) then
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   440
      i_th1
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   441
    else
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   442
      let
40221
d10b68c6e6d4 do not let Metis be confused by higher-order reasoning leading to literals of the form "~ ~ p", which are really the same as "p"
blanchet
parents: 40158
diff changeset
   443
        val i_atm =
43094
269300fb83d0 more work on new Metis
blanchet
parents: 43093
diff changeset
   444
          singleton (hol_terms_from_metis ctxt mode old_skolems sym_tab)
40221
d10b68c6e6d4 do not let Metis be confused by higher-order reasoning leading to literals of the form "~ ~ p", which are really the same as "p"
blanchet
parents: 40158
diff changeset
   445
                    (Metis_Term.Fn atm)
39978
11bfb7e7cc86 added "trace_metis" configuration option, replacing old-fashioned references
blanchet
parents: 39964
diff changeset
   446
        val _ = trace_msg ctxt (fn () => "  atom: " ^ Syntax.string_of_term ctxt i_atm)
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   447
        val prems_th1 = prems_of i_th1
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   448
        val prems_th2 = prems_of i_th2
40221
d10b68c6e6d4 do not let Metis be confused by higher-order reasoning leading to literals of the form "~ ~ p", which are really the same as "p"
blanchet
parents: 40158
diff changeset
   449
        val index_th1 =
d10b68c6e6d4 do not let Metis be confused by higher-order reasoning leading to literals of the form "~ ~ p", which are really the same as "p"
blanchet
parents: 40158
diff changeset
   450
          index_of_literal (s_not i_atm) prems_th1
d10b68c6e6d4 do not let Metis be confused by higher-order reasoning leading to literals of the form "~ ~ p", which are really the same as "p"
blanchet
parents: 40158
diff changeset
   451
          handle Empty => raise Fail "Failed to find literal in th1"
41491
a2ad5b824051 eliminated Int.toString;
wenzelm
parents: 41143
diff changeset
   452
        val _ = trace_msg ctxt (fn () => "  index_th1: " ^ string_of_int index_th1)
40221
d10b68c6e6d4 do not let Metis be confused by higher-order reasoning leading to literals of the form "~ ~ p", which are really the same as "p"
blanchet
parents: 40158
diff changeset
   453
        val index_th2 =
d10b68c6e6d4 do not let Metis be confused by higher-order reasoning leading to literals of the form "~ ~ p", which are really the same as "p"
blanchet
parents: 40158
diff changeset
   454
          index_of_literal i_atm prems_th2
d10b68c6e6d4 do not let Metis be confused by higher-order reasoning leading to literals of the form "~ ~ p", which are really the same as "p"
blanchet
parents: 40158
diff changeset
   455
          handle Empty => raise Fail "Failed to find literal in th2"
41491
a2ad5b824051 eliminated Int.toString;
wenzelm
parents: 41143
diff changeset
   456
        val _ = trace_msg ctxt (fn () => "  index_th2: " ^ string_of_int index_th2)
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   457
    in
42348
187354e22c7d improve on 0b05cc14c2cb: make sure that a literal variable "?foo" isn't accidentally renamed "?Q", which might be enough to confuse the new Skolemizer (cf. "Clausify.thy" example)
blanchet
parents: 42344
diff changeset
   458
      resolve_inc_tyvars thy (select_literal index_th1 i_th1) index_th2 i_th2
42650
552eae49f97d reintroduce this idea of running "metisFT" after a failed "metis" -- I took it out in e85ce10cef1a because I couldn't think of a reasonable use case, but now that ATPs use sound encodings and include dangerous facts (e.g. True_or_False) it makes more sense than ever to run "metisFT" after "metis"
blanchet
parents: 42616
diff changeset
   459
      handle TERM (s, _) => raise METIS ("resolve_inf", s)
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   460
    end
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   461
  end;
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   462
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   463
(* INFERENCE RULE: REFL *)
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   464
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   465
val REFL_THM = Thm.incr_indexes 2 @{lemma "t ~= t ==> False" by simp}
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   466
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   467
val refl_x = cterm_of @{theory} (Var (hd (Term.add_vars (prop_of REFL_THM) [])));
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   468
val refl_idx = 1 + Thm.maxidx_of REFL_THM;
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   469
43094
269300fb83d0 more work on new Metis
blanchet
parents: 43093
diff changeset
   470
fun refl_inf ctxt mode old_skolems sym_tab t =
269300fb83d0 more work on new Metis
blanchet
parents: 43093
diff changeset
   471
  let
269300fb83d0 more work on new Metis
blanchet
parents: 43093
diff changeset
   472
    val thy = Proof_Context.theory_of ctxt
269300fb83d0 more work on new Metis
blanchet
parents: 43093
diff changeset
   473
    val i_t = singleton (hol_terms_from_metis ctxt mode old_skolems sym_tab) t
269300fb83d0 more work on new Metis
blanchet
parents: 43093
diff changeset
   474
    val _ = trace_msg ctxt (fn () => "  term: " ^ Syntax.string_of_term ctxt i_t)
269300fb83d0 more work on new Metis
blanchet
parents: 43093
diff changeset
   475
    val c_t = cterm_incr_types thy refl_idx i_t
269300fb83d0 more work on new Metis
blanchet
parents: 43093
diff changeset
   476
  in cterm_instantiate [(refl_x, c_t)] REFL_THM end
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   477
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   478
(* INFERENCE RULE: EQUALITY *)
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   479
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   480
val subst_em = @{lemma "s = t ==> P s ==> ~ P t ==> False" by simp}
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   481
val ssubst_em = @{lemma "s = t ==> P t ==> ~ P s ==> False" by simp}
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   482
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   483
val metis_eq = Metis_Term.Fn ("=", []);
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   484
43093
blanchet
parents: 43092
diff changeset
   485
(* Equality has no type arguments *)
blanchet
parents: 43092
diff changeset
   486
fun get_ty_arg_size _ (Const (@{const_name HOL.eq}, _)) = 0
blanchet
parents: 43092
diff changeset
   487
  | get_ty_arg_size thy (Const (s, _)) =
blanchet
parents: 43092
diff changeset
   488
    (num_type_args thy s handle TYPE _ => 0)
blanchet
parents: 43092
diff changeset
   489
  | get_ty_arg_size _ _ = 0
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   490
43094
269300fb83d0 more work on new Metis
blanchet
parents: 43093
diff changeset
   491
fun equality_inf ctxt mode old_skolems sym_tab (pos, atm) fp fr =
42361
23f352990944 modernized structure Proof_Context;
wenzelm
parents: 42354
diff changeset
   492
  let val thy = Proof_Context.theory_of ctxt
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   493
      val m_tm = Metis_Term.Fn atm
43094
269300fb83d0 more work on new Metis
blanchet
parents: 43093
diff changeset
   494
      val [i_atm, i_tm] =
269300fb83d0 more work on new Metis
blanchet
parents: 43093
diff changeset
   495
        hol_terms_from_metis ctxt mode old_skolems sym_tab [m_tm, fr]
39978
11bfb7e7cc86 added "trace_metis" configuration option, replacing old-fashioned references
blanchet
parents: 39964
diff changeset
   496
      val _ = trace_msg ctxt (fn () => "sign of the literal: " ^ Bool.toString pos)
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   497
      fun replace_item_list lx 0 (_::ls) = lx::ls
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   498
        | replace_item_list lx i (l::ls) = l :: replace_item_list lx (i-1) ls
43104
81d1b15aa0ae use ":" for type information (looks good in Metis's output) and handle it in new path finder
blanchet
parents: 43103
diff changeset
   499
      fun path_finder_fail mode tm ps t =
81d1b15aa0ae use ":" for type information (looks good in Metis's output) and handle it in new path finder
blanchet
parents: 43103
diff changeset
   500
        raise Fail ("Cannot replay Metis proof in Isabelle:\n" ^
81d1b15aa0ae use ":" for type information (looks good in Metis's output) and handle it in new path finder
blanchet
parents: 43103
diff changeset
   501
                    "equality_inf, path_finder_" ^ string_of_mode mode ^
81d1b15aa0ae use ":" for type information (looks good in Metis's output) and handle it in new path finder
blanchet
parents: 43103
diff changeset
   502
                    ": path = " ^ space_implode " " (map string_of_int ps) ^
81d1b15aa0ae use ":" for type information (looks good in Metis's output) and handle it in new path finder
blanchet
parents: 43103
diff changeset
   503
                    " isa-term: " ^ Syntax.string_of_term ctxt tm ^
81d1b15aa0ae use ":" for type information (looks good in Metis's output) and handle it in new path finder
blanchet
parents: 43103
diff changeset
   504
                    (case t of
81d1b15aa0ae use ":" for type information (looks good in Metis's output) and handle it in new path finder
blanchet
parents: 43103
diff changeset
   505
                       SOME t => " fol-term: " ^ Metis_Term.toString t
81d1b15aa0ae use ":" for type information (looks good in Metis's output) and handle it in new path finder
blanchet
parents: 43103
diff changeset
   506
                     | NONE => ""))
39498
e8aef7ea9cbb make "subst_translation" more robust w.r.t. type instantiations like {_1234 |-> 'a}
blanchet
parents: 39497
diff changeset
   507
      fun path_finder_FO tm [] = (tm, Bound 0)
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   508
        | path_finder_FO tm (p::ps) =
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   509
            let val (tm1,args) = strip_comb tm
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   510
                val adjustment = get_ty_arg_size thy tm1
43094
269300fb83d0 more work on new Metis
blanchet
parents: 43093
diff changeset
   511
                val p' = if adjustment > p then p else p - adjustment
42364
8c674b3b8e44 eliminated old List.nth;
wenzelm
parents: 42361
diff changeset
   512
                val tm_p = nth args p'
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   513
                  handle Subscript =>
42650
552eae49f97d reintroduce this idea of running "metisFT" after a failed "metis" -- I took it out in e85ce10cef1a because I couldn't think of a reasonable use case, but now that ATPs use sound encodings and include dangerous facts (e.g. True_or_False) it makes more sense than ever to run "metisFT" after "metis"
blanchet
parents: 42616
diff changeset
   514
                         raise METIS ("equality_inf",
552eae49f97d reintroduce this idea of running "metisFT" after a failed "metis" -- I took it out in e85ce10cef1a because I couldn't think of a reasonable use case, but now that ATPs use sound encodings and include dangerous facts (e.g. True_or_False) it makes more sense than ever to run "metisFT" after "metis"
blanchet
parents: 42616
diff changeset
   515
                                      string_of_int p ^ " adj " ^
552eae49f97d reintroduce this idea of running "metisFT" after a failed "metis" -- I took it out in e85ce10cef1a because I couldn't think of a reasonable use case, but now that ATPs use sound encodings and include dangerous facts (e.g. True_or_False) it makes more sense than ever to run "metisFT" after "metis"
blanchet
parents: 42616
diff changeset
   516
                                      string_of_int adjustment ^ " term " ^
552eae49f97d reintroduce this idea of running "metisFT" after a failed "metis" -- I took it out in e85ce10cef1a because I couldn't think of a reasonable use case, but now that ATPs use sound encodings and include dangerous facts (e.g. True_or_False) it makes more sense than ever to run "metisFT" after "metis"
blanchet
parents: 42616
diff changeset
   517
                                      Syntax.string_of_term ctxt tm)
41491
a2ad5b824051 eliminated Int.toString;
wenzelm
parents: 41143
diff changeset
   518
                val _ = trace_msg ctxt (fn () => "path_finder: " ^ string_of_int p ^
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   519
                                      "  " ^ Syntax.string_of_term ctxt tm_p)
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   520
                val (r,t) = path_finder_FO tm_p ps
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   521
            in
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   522
                (r, list_comb (tm1, replace_item_list t p' args))
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   523
            end
39498
e8aef7ea9cbb make "subst_translation" more robust w.r.t. type instantiations like {_1234 |-> 'a}
blanchet
parents: 39497
diff changeset
   524
      fun path_finder_HO tm [] = (tm, Bound 0)
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   525
        | path_finder_HO (t$u) (0::ps) = (fn(x,y) => (x, y$u)) (path_finder_HO t ps)
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   526
        | path_finder_HO (t$u) (_::ps) = (fn(x,y) => (x, t$y)) (path_finder_HO u ps)
43104
81d1b15aa0ae use ":" for type information (looks good in Metis's output) and handle it in new path finder
blanchet
parents: 43103
diff changeset
   527
        | path_finder_HO tm ps = path_finder_fail HO tm ps NONE
39498
e8aef7ea9cbb make "subst_translation" more robust w.r.t. type instantiations like {_1234 |-> 'a}
blanchet
parents: 39497
diff changeset
   528
      fun path_finder_FT tm [] _ = (tm, Bound 0)
43104
81d1b15aa0ae use ":" for type information (looks good in Metis's output) and handle it in new path finder
blanchet
parents: 43103
diff changeset
   529
        | path_finder_FT tm (0::ps) (Metis_Term.Fn (":", [t1, _])) =
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   530
            path_finder_FT tm ps t1
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   531
        | path_finder_FT (t$u) (0::ps) (Metis_Term.Fn (".", [t1, _])) =
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   532
            (fn(x,y) => (x, y$u)) (path_finder_FT t ps t1)
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   533
        | path_finder_FT (t$u) (1::ps) (Metis_Term.Fn (".", [_, t2])) =
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   534
            (fn(x,y) => (x, t$y)) (path_finder_FT u ps t2)
43104
81d1b15aa0ae use ":" for type information (looks good in Metis's output) and handle it in new path finder
blanchet
parents: 43103
diff changeset
   535
        | path_finder_FT tm ps t = path_finder_fail FT tm ps (SOME t)
43103
35962353e36b tuned name
blanchet
parents: 43097
diff changeset
   536
      fun path_finder_MX tm [] _ = (tm, Bound 0)
35962353e36b tuned name
blanchet
parents: 43097
diff changeset
   537
        | path_finder_MX tm (p :: ps) (t as Metis_Term.Fn (s, ts)) =
43130
d73fc2e55308 implemented missing hAPP and ti cases of new path finder
blanchet
parents: 43128
diff changeset
   538
          let val s = s |> unmangled_const_name in
d73fc2e55308 implemented missing hAPP and ti cases of new path finder
blanchet
parents: 43128
diff changeset
   539
            if s = metis_type_tag orelse s = prefixed_type_tag_name then
d73fc2e55308 implemented missing hAPP and ti cases of new path finder
blanchet
parents: 43128
diff changeset
   540
              path_finder_MX tm ps (nth ts p)
d73fc2e55308 implemented missing hAPP and ti cases of new path finder
blanchet
parents: 43128
diff changeset
   541
            else if s = metis_app_op orelse s = prefixed_app_op_name then
d73fc2e55308 implemented missing hAPP and ti cases of new path finder
blanchet
parents: 43128
diff changeset
   542
              let
d73fc2e55308 implemented missing hAPP and ti cases of new path finder
blanchet
parents: 43128
diff changeset
   543
                val (tm1, tm2) = dest_comb tm
d73fc2e55308 implemented missing hAPP and ti cases of new path finder
blanchet
parents: 43128
diff changeset
   544
                val p' = p - (length ts - 2)
d73fc2e55308 implemented missing hAPP and ti cases of new path finder
blanchet
parents: 43128
diff changeset
   545
              in
d73fc2e55308 implemented missing hAPP and ti cases of new path finder
blanchet
parents: 43128
diff changeset
   546
                if p' = 0 then
d73fc2e55308 implemented missing hAPP and ti cases of new path finder
blanchet
parents: 43128
diff changeset
   547
                  path_finder_MX tm1 ps (nth ts p) ||> (fn y => y $ tm2)
d73fc2e55308 implemented missing hAPP and ti cases of new path finder
blanchet
parents: 43128
diff changeset
   548
                else
d73fc2e55308 implemented missing hAPP and ti cases of new path finder
blanchet
parents: 43128
diff changeset
   549
                  path_finder_MX tm2 ps (nth ts p) ||> (fn y => tm1 $ y)
d73fc2e55308 implemented missing hAPP and ti cases of new path finder
blanchet
parents: 43128
diff changeset
   550
              end
d73fc2e55308 implemented missing hAPP and ti cases of new path finder
blanchet
parents: 43128
diff changeset
   551
            else
d73fc2e55308 implemented missing hAPP and ti cases of new path finder
blanchet
parents: 43128
diff changeset
   552
              let
d73fc2e55308 implemented missing hAPP and ti cases of new path finder
blanchet
parents: 43128
diff changeset
   553
                val (tm1, args) = strip_comb tm
d73fc2e55308 implemented missing hAPP and ti cases of new path finder
blanchet
parents: 43128
diff changeset
   554
                val adjustment = length ts - length args
d73fc2e55308 implemented missing hAPP and ti cases of new path finder
blanchet
parents: 43128
diff changeset
   555
                val p' = if adjustment > p then p else p - adjustment
d73fc2e55308 implemented missing hAPP and ti cases of new path finder
blanchet
parents: 43128
diff changeset
   556
                val tm_p = nth args p'
d73fc2e55308 implemented missing hAPP and ti cases of new path finder
blanchet
parents: 43128
diff changeset
   557
                  handle Subscript =>
d73fc2e55308 implemented missing hAPP and ti cases of new path finder
blanchet
parents: 43128
diff changeset
   558
                         path_finder_fail MX tm (p :: ps) (SOME t)
d73fc2e55308 implemented missing hAPP and ti cases of new path finder
blanchet
parents: 43128
diff changeset
   559
                val _ = trace_msg ctxt (fn () =>
d73fc2e55308 implemented missing hAPP and ti cases of new path finder
blanchet
parents: 43128
diff changeset
   560
                    "path_finder: " ^ string_of_int p ^ "  " ^
d73fc2e55308 implemented missing hAPP and ti cases of new path finder
blanchet
parents: 43128
diff changeset
   561
                    Syntax.string_of_term ctxt tm_p)
d73fc2e55308 implemented missing hAPP and ti cases of new path finder
blanchet
parents: 43128
diff changeset
   562
                val (r, t) = path_finder_MX tm_p ps (nth ts p)
d73fc2e55308 implemented missing hAPP and ti cases of new path finder
blanchet
parents: 43128
diff changeset
   563
              in (r, list_comb (tm1, replace_item_list t p' args)) end
d73fc2e55308 implemented missing hAPP and ti cases of new path finder
blanchet
parents: 43128
diff changeset
   564
          end
43104
81d1b15aa0ae use ":" for type information (looks good in Metis's output) and handle it in new path finder
blanchet
parents: 43103
diff changeset
   565
        | path_finder_MX tm ps t = path_finder_fail MX tm ps (SOME t)
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   566
      fun path_finder FO tm ps _ = path_finder_FO tm ps
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   567
        | path_finder HO (tm as Const(@{const_name HOL.eq},_) $ _ $ _) (p::ps) _ =
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   568
             (*equality: not curried, as other predicates are*)
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   569
             if p=0 then path_finder_HO tm (0::1::ps)  (*select first operand*)
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   570
             else path_finder_HO tm (p::ps)        (*1 selects second operand*)
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   571
        | path_finder HO tm (_ :: ps) (Metis_Term.Fn ("{}", [_])) =
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   572
             path_finder_HO tm ps      (*if not equality, ignore head to skip hBOOL*)
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   573
        | path_finder FT (tm as Const(@{const_name HOL.eq}, _) $ _ $ _) (p::ps)
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   574
                            (Metis_Term.Fn ("=", [t1,t2])) =
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   575
             (*equality: not curried, as other predicates are*)
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   576
             if p=0 then path_finder_FT tm (0::1::ps)
43094
269300fb83d0 more work on new Metis
blanchet
parents: 43093
diff changeset
   577
                          (Metis_Term.Fn (metis_app_op, [Metis_Term.Fn (metis_app_op, [metis_eq,t1]), t2]))
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   578
                          (*select first operand*)
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   579
             else path_finder_FT tm (p::ps)
43094
269300fb83d0 more work on new Metis
blanchet
parents: 43093
diff changeset
   580
                   (Metis_Term.Fn (metis_app_op, [metis_eq, t2]))
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   581
                   (*1 selects second operand*)
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   582
        | path_finder FT tm (_ :: ps) (Metis_Term.Fn ("{}", [t1])) = path_finder_FT tm ps t1
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   583
             (*if not equality, ignore head to skip the hBOOL predicate*)
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   584
        | path_finder FT tm ps t = path_finder_FT tm ps t  (*really an error case!*)
43103
35962353e36b tuned name
blanchet
parents: 43097
diff changeset
   585
        | path_finder MX tm ps t = path_finder_MX tm ps t
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   586
      fun path_finder_lit ((nt as Const (@{const_name Not}, _)) $ tm_a) idx =
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   587
            let val (tm, tm_rslt) = path_finder mode tm_a idx m_tm
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   588
            in (tm, nt $ tm_rslt) end
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   589
        | path_finder_lit tm_a idx = path_finder mode tm_a idx m_tm
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   590
      val (tm_subst, body) = path_finder_lit i_atm fp
39498
e8aef7ea9cbb make "subst_translation" more robust w.r.t. type instantiations like {_1234 |-> 'a}
blanchet
parents: 39497
diff changeset
   591
      val tm_abs = Abs ("x", type_of tm_subst, body)
39978
11bfb7e7cc86 added "trace_metis" configuration option, replacing old-fashioned references
blanchet
parents: 39964
diff changeset
   592
      val _ = trace_msg ctxt (fn () => "abstraction: " ^ Syntax.string_of_term ctxt tm_abs)
11bfb7e7cc86 added "trace_metis" configuration option, replacing old-fashioned references
blanchet
parents: 39964
diff changeset
   593
      val _ = trace_msg ctxt (fn () => "i_tm: " ^ Syntax.string_of_term ctxt i_tm)
11bfb7e7cc86 added "trace_metis" configuration option, replacing old-fashioned references
blanchet
parents: 39964
diff changeset
   594
      val _ = trace_msg ctxt (fn () => "located term: " ^ Syntax.string_of_term ctxt tm_subst)
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   595
      val imax = maxidx_of_term (i_tm $ tm_abs $ tm_subst)  (*ill typed but gives right max*)
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   596
      val subst' = Thm.incr_indexes (imax+1) (if pos then subst_em else ssubst_em)
39978
11bfb7e7cc86 added "trace_metis" configuration option, replacing old-fashioned references
blanchet
parents: 39964
diff changeset
   597
      val _ = trace_msg ctxt (fn () => "subst' " ^ Display.string_of_thm ctxt subst')
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   598
      val eq_terms = map (pairself (cterm_of thy))
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   599
        (ListPair.zip (OldTerm.term_vars (prop_of subst'), [tm_abs, tm_subst, i_tm]))
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   600
  in  cterm_instantiate eq_terms subst'  end;
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   601
43094
269300fb83d0 more work on new Metis
blanchet
parents: 43093
diff changeset
   602
val factor = Seq.hd o distinct_subgoals_tac
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   603
43094
269300fb83d0 more work on new Metis
blanchet
parents: 43093
diff changeset
   604
fun one_step ctxt mode old_skolems sym_tab th_pairs p =
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   605
  case p of
43094
269300fb83d0 more work on new Metis
blanchet
parents: 43093
diff changeset
   606
    (fol_th, Metis_Proof.Axiom _) => axiom_inf th_pairs fol_th |> factor
269300fb83d0 more work on new Metis
blanchet
parents: 43093
diff changeset
   607
  | (_, Metis_Proof.Assume f_atm) =>
269300fb83d0 more work on new Metis
blanchet
parents: 43093
diff changeset
   608
    assume_inf ctxt mode old_skolems sym_tab f_atm
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   609
  | (_, Metis_Proof.Metis_Subst (f_subst, f_th1)) =>
43094
269300fb83d0 more work on new Metis
blanchet
parents: 43093
diff changeset
   610
    inst_inf ctxt mode old_skolems sym_tab th_pairs f_subst f_th1 |> factor
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   611
  | (_, Metis_Proof.Resolve(f_atm, f_th1, f_th2)) =>
43094
269300fb83d0 more work on new Metis
blanchet
parents: 43093
diff changeset
   612
    resolve_inf ctxt mode old_skolems sym_tab th_pairs f_atm f_th1 f_th2
269300fb83d0 more work on new Metis
blanchet
parents: 43093
diff changeset
   613
    |> factor
269300fb83d0 more work on new Metis
blanchet
parents: 43093
diff changeset
   614
  | (_, Metis_Proof.Refl f_tm) => refl_inf ctxt mode old_skolems sym_tab f_tm
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   615
  | (_, Metis_Proof.Equality (f_lit, f_p, f_r)) =>
43094
269300fb83d0 more work on new Metis
blanchet
parents: 43093
diff changeset
   616
    equality_inf ctxt mode old_skolems sym_tab f_lit f_p f_r
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   617
39893
25a339e1ff9b move functions closer to where they're used
blanchet
parents: 39887
diff changeset
   618
fun flexflex_first_order th =
25a339e1ff9b move functions closer to where they're used
blanchet
parents: 39887
diff changeset
   619
  case Thm.tpairs_of th of
25a339e1ff9b move functions closer to where they're used
blanchet
parents: 39887
diff changeset
   620
      [] => th
25a339e1ff9b move functions closer to where they're used
blanchet
parents: 39887
diff changeset
   621
    | pairs =>
25a339e1ff9b move functions closer to where they're used
blanchet
parents: 39887
diff changeset
   622
        let val thy = theory_of_thm th
25a339e1ff9b move functions closer to where they're used
blanchet
parents: 39887
diff changeset
   623
            val (_, tenv) =
25a339e1ff9b move functions closer to where they're used
blanchet
parents: 39887
diff changeset
   624
              fold (Pattern.first_order_match thy) pairs (Vartab.empty, Vartab.empty)
25a339e1ff9b move functions closer to where they're used
blanchet
parents: 39887
diff changeset
   625
            val t_pairs = map Meson.term_pair_of (Vartab.dest tenv)
25a339e1ff9b move functions closer to where they're used
blanchet
parents: 39887
diff changeset
   626
            val th' = Thm.instantiate ([], map (pairself (cterm_of thy)) t_pairs) th
25a339e1ff9b move functions closer to where they're used
blanchet
parents: 39887
diff changeset
   627
        in  th'  end
25a339e1ff9b move functions closer to where they're used
blanchet
parents: 39887
diff changeset
   628
        handle THM _ => th;
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   629
39895
a91a84b1dfdd reintroduced code that keeps track of whether the Isabelle and Metis proofs are in sync -- generalized to work with the new skolemizer
blanchet
parents: 39893
diff changeset
   630
fun is_metis_literal_genuine (_, (s, _)) = not (String.isPrefix class_prefix s)
a91a84b1dfdd reintroduced code that keeps track of whether the Isabelle and Metis proofs are in sync -- generalized to work with the new skolemizer
blanchet
parents: 39893
diff changeset
   631
fun is_isabelle_literal_genuine t =
39953
aa54f347e5e2 hide uninteresting MESON/Metis constants and facts and remove "meson_" prefix to (now hidden) fact names
blanchet
parents: 39946
diff changeset
   632
  case t of _ $ (Const (@{const_name Meson.skolem}, _) $ _) => false | _ => true
39895
a91a84b1dfdd reintroduced code that keeps track of whether the Isabelle and Metis proofs are in sync -- generalized to work with the new skolemizer
blanchet
parents: 39893
diff changeset
   633
a91a84b1dfdd reintroduced code that keeps track of whether the Isabelle and Metis proofs are in sync -- generalized to work with the new skolemizer
blanchet
parents: 39893
diff changeset
   634
fun count p xs = fold (fn x => if p x then Integer.add 1 else I) xs 0
a91a84b1dfdd reintroduced code that keeps track of whether the Isabelle and Metis proofs are in sync -- generalized to work with the new skolemizer
blanchet
parents: 39893
diff changeset
   635
42333
23bb0784b5d0 try to repair out-of-sync situations in Metis
blanchet
parents: 42271
diff changeset
   636
(* Seldomly needed hack. A Metis clause is represented as a set, so duplicate
23bb0784b5d0 try to repair out-of-sync situations in Metis
blanchet
parents: 42271
diff changeset
   637
   disjuncts are impossible. In the Isabelle proof, in spite of efforts to
23bb0784b5d0 try to repair out-of-sync situations in Metis
blanchet
parents: 42271
diff changeset
   638
   eliminate them, duplicates sometimes appear with slightly different (but
23bb0784b5d0 try to repair out-of-sync situations in Metis
blanchet
parents: 42271
diff changeset
   639
   unifiable) types. *)
23bb0784b5d0 try to repair out-of-sync situations in Metis
blanchet
parents: 42271
diff changeset
   640
fun resynchronize ctxt fol_th th =
23bb0784b5d0 try to repair out-of-sync situations in Metis
blanchet
parents: 42271
diff changeset
   641
  let
23bb0784b5d0 try to repair out-of-sync situations in Metis
blanchet
parents: 42271
diff changeset
   642
    val num_metis_lits =
23bb0784b5d0 try to repair out-of-sync situations in Metis
blanchet
parents: 42271
diff changeset
   643
      count is_metis_literal_genuine
23bb0784b5d0 try to repair out-of-sync situations in Metis
blanchet
parents: 42271
diff changeset
   644
            (Metis_LiteralSet.toList (Metis_Thm.clause fol_th))
23bb0784b5d0 try to repair out-of-sync situations in Metis
blanchet
parents: 42271
diff changeset
   645
    val num_isabelle_lits = count is_isabelle_literal_genuine (prems_of th)
23bb0784b5d0 try to repair out-of-sync situations in Metis
blanchet
parents: 42271
diff changeset
   646
  in
23bb0784b5d0 try to repair out-of-sync situations in Metis
blanchet
parents: 42271
diff changeset
   647
    if num_metis_lits >= num_isabelle_lits then
23bb0784b5d0 try to repair out-of-sync situations in Metis
blanchet
parents: 42271
diff changeset
   648
      th
23bb0784b5d0 try to repair out-of-sync situations in Metis
blanchet
parents: 42271
diff changeset
   649
    else
23bb0784b5d0 try to repair out-of-sync situations in Metis
blanchet
parents: 42271
diff changeset
   650
      let
23bb0784b5d0 try to repair out-of-sync situations in Metis
blanchet
parents: 42271
diff changeset
   651
        val (prems0, concl) = th |> prop_of |> Logic.strip_horn
43134
0c82e00ba63e make sure no warnings are given for polymorphic facts where we use a monomorphic instance
blanchet
parents: 43130
diff changeset
   652
        val prems = prems0 |> distinct untyped_aconv
42333
23bb0784b5d0 try to repair out-of-sync situations in Metis
blanchet
parents: 42271
diff changeset
   653
        val goal = Logic.list_implies (prems, concl)
23bb0784b5d0 try to repair out-of-sync situations in Metis
blanchet
parents: 42271
diff changeset
   654
      in
23bb0784b5d0 try to repair out-of-sync situations in Metis
blanchet
parents: 42271
diff changeset
   655
        if length prems = length prems0 then
42650
552eae49f97d reintroduce this idea of running "metisFT" after a failed "metis" -- I took it out in e85ce10cef1a because I couldn't think of a reasonable use case, but now that ATPs use sound encodings and include dangerous facts (e.g. True_or_False) it makes more sense than ever to run "metisFT" after "metis"
blanchet
parents: 42616
diff changeset
   656
          raise METIS ("resynchronize", "Out of sync")
42333
23bb0784b5d0 try to repair out-of-sync situations in Metis
blanchet
parents: 42271
diff changeset
   657
        else
23bb0784b5d0 try to repair out-of-sync situations in Metis
blanchet
parents: 42271
diff changeset
   658
          Goal.prove ctxt [] [] goal (K (cut_rules_tac [th] 1
23bb0784b5d0 try to repair out-of-sync situations in Metis
blanchet
parents: 42271
diff changeset
   659
                                         THEN ALLGOALS assume_tac))
23bb0784b5d0 try to repair out-of-sync situations in Metis
blanchet
parents: 42271
diff changeset
   660
          |> resynchronize ctxt fol_th
23bb0784b5d0 try to repair out-of-sync situations in Metis
blanchet
parents: 42271
diff changeset
   661
      end
23bb0784b5d0 try to repair out-of-sync situations in Metis
blanchet
parents: 42271
diff changeset
   662
  end
23bb0784b5d0 try to repair out-of-sync situations in Metis
blanchet
parents: 42271
diff changeset
   663
43094
269300fb83d0 more work on new Metis
blanchet
parents: 43093
diff changeset
   664
fun replay_one_inference ctxt mode old_skolems sym_tab (fol_th, inf) th_pairs =
269300fb83d0 more work on new Metis
blanchet
parents: 43093
diff changeset
   665
  if not (null th_pairs) andalso
269300fb83d0 more work on new Metis
blanchet
parents: 43093
diff changeset
   666
     prop_of (snd (hd th_pairs)) aconv @{prop False} then
40868
177cd660abb7 give the Isabelle proof the benefice of the doubt when the Isabelle theorem has fewer literals than the Metis one -- this makes a difference on lemma "Let (x::'a, y::'a) (inv_image (r::'b * 'b => bool) (f::'a => 'b)) = ((f x, f y) : r)" apply (metis in_inv_image mem_def)
blanchet
parents: 40724
diff changeset
   667
    (* Isabelle sometimes identifies literals (premises) that are distinct in
177cd660abb7 give the Isabelle proof the benefice of the doubt when the Isabelle theorem has fewer literals than the Metis one -- this makes a difference on lemma "Let (x::'a, y::'a) (inv_image (r::'b * 'b => bool) (f::'a => 'b)) = ((f x, f y) : r)" apply (metis in_inv_image mem_def)
blanchet
parents: 40724
diff changeset
   668
       Metis (e.g., because of type variables). We give the Isabelle proof the
177cd660abb7 give the Isabelle proof the benefice of the doubt when the Isabelle theorem has fewer literals than the Metis one -- this makes a difference on lemma "Let (x::'a, y::'a) (inv_image (r::'b * 'b => bool) (f::'a => 'b)) = ((f x, f y) : r)" apply (metis in_inv_image mem_def)
blanchet
parents: 40724
diff changeset
   669
       benefice of the doubt. *)
43094
269300fb83d0 more work on new Metis
blanchet
parents: 43093
diff changeset
   670
    th_pairs
40868
177cd660abb7 give the Isabelle proof the benefice of the doubt when the Isabelle theorem has fewer literals than the Metis one -- this makes a difference on lemma "Let (x::'a, y::'a) (inv_image (r::'b * 'b => bool) (f::'a => 'b)) = ((f x, f y) : r)" apply (metis in_inv_image mem_def)
blanchet
parents: 40724
diff changeset
   671
  else
177cd660abb7 give the Isabelle proof the benefice of the doubt when the Isabelle theorem has fewer literals than the Metis one -- this makes a difference on lemma "Let (x::'a, y::'a) (inv_image (r::'b * 'b => bool) (f::'a => 'b)) = ((f x, f y) : r)" apply (metis in_inv_image mem_def)
blanchet
parents: 40724
diff changeset
   672
    let
177cd660abb7 give the Isabelle proof the benefice of the doubt when the Isabelle theorem has fewer literals than the Metis one -- this makes a difference on lemma "Let (x::'a, y::'a) (inv_image (r::'b * 'b => bool) (f::'a => 'b)) = ((f x, f y) : r)" apply (metis in_inv_image mem_def)
blanchet
parents: 40724
diff changeset
   673
      val _ = trace_msg ctxt
177cd660abb7 give the Isabelle proof the benefice of the doubt when the Isabelle theorem has fewer literals than the Metis one -- this makes a difference on lemma "Let (x::'a, y::'a) (inv_image (r::'b * 'b => bool) (f::'a => 'b)) = ((f x, f y) : r)" apply (metis in_inv_image mem_def)
blanchet
parents: 40724
diff changeset
   674
                  (fn () => "=============================================")
177cd660abb7 give the Isabelle proof the benefice of the doubt when the Isabelle theorem has fewer literals than the Metis one -- this makes a difference on lemma "Let (x::'a, y::'a) (inv_image (r::'b * 'b => bool) (f::'a => 'b)) = ((f x, f y) : r)" apply (metis in_inv_image mem_def)
blanchet
parents: 40724
diff changeset
   675
      val _ = trace_msg ctxt
177cd660abb7 give the Isabelle proof the benefice of the doubt when the Isabelle theorem has fewer literals than the Metis one -- this makes a difference on lemma "Let (x::'a, y::'a) (inv_image (r::'b * 'b => bool) (f::'a => 'b)) = ((f x, f y) : r)" apply (metis in_inv_image mem_def)
blanchet
parents: 40724
diff changeset
   676
                  (fn () => "METIS THM: " ^ Metis_Thm.toString fol_th)
177cd660abb7 give the Isabelle proof the benefice of the doubt when the Isabelle theorem has fewer literals than the Metis one -- this makes a difference on lemma "Let (x::'a, y::'a) (inv_image (r::'b * 'b => bool) (f::'a => 'b)) = ((f x, f y) : r)" apply (metis in_inv_image mem_def)
blanchet
parents: 40724
diff changeset
   677
      val _ = trace_msg ctxt
177cd660abb7 give the Isabelle proof the benefice of the doubt when the Isabelle theorem has fewer literals than the Metis one -- this makes a difference on lemma "Let (x::'a, y::'a) (inv_image (r::'b * 'b => bool) (f::'a => 'b)) = ((f x, f y) : r)" apply (metis in_inv_image mem_def)
blanchet
parents: 40724
diff changeset
   678
                  (fn () => "INFERENCE: " ^ Metis_Proof.inferenceToString inf)
43094
269300fb83d0 more work on new Metis
blanchet
parents: 43093
diff changeset
   679
      val th = one_step ctxt mode old_skolems sym_tab th_pairs (fol_th, inf)
40868
177cd660abb7 give the Isabelle proof the benefice of the doubt when the Isabelle theorem has fewer literals than the Metis one -- this makes a difference on lemma "Let (x::'a, y::'a) (inv_image (r::'b * 'b => bool) (f::'a => 'b)) = ((f x, f y) : r)" apply (metis in_inv_image mem_def)
blanchet
parents: 40724
diff changeset
   680
               |> flexflex_first_order
42333
23bb0784b5d0 try to repair out-of-sync situations in Metis
blanchet
parents: 42271
diff changeset
   681
               |> resynchronize ctxt fol_th
40868
177cd660abb7 give the Isabelle proof the benefice of the doubt when the Isabelle theorem has fewer literals than the Metis one -- this makes a difference on lemma "Let (x::'a, y::'a) (inv_image (r::'b * 'b => bool) (f::'a => 'b)) = ((f x, f y) : r)" apply (metis in_inv_image mem_def)
blanchet
parents: 40724
diff changeset
   682
      val _ = trace_msg ctxt
177cd660abb7 give the Isabelle proof the benefice of the doubt when the Isabelle theorem has fewer literals than the Metis one -- this makes a difference on lemma "Let (x::'a, y::'a) (inv_image (r::'b * 'b => bool) (f::'a => 'b)) = ((f x, f y) : r)" apply (metis in_inv_image mem_def)
blanchet
parents: 40724
diff changeset
   683
                  (fn () => "ISABELLE THM: " ^ Display.string_of_thm ctxt th)
177cd660abb7 give the Isabelle proof the benefice of the doubt when the Isabelle theorem has fewer literals than the Metis one -- this makes a difference on lemma "Let (x::'a, y::'a) (inv_image (r::'b * 'b => bool) (f::'a => 'b)) = ((f x, f y) : r)" apply (metis in_inv_image mem_def)
blanchet
parents: 40724
diff changeset
   684
      val _ = trace_msg ctxt
177cd660abb7 give the Isabelle proof the benefice of the doubt when the Isabelle theorem has fewer literals than the Metis one -- this makes a difference on lemma "Let (x::'a, y::'a) (inv_image (r::'b * 'b => bool) (f::'a => 'b)) = ((f x, f y) : r)" apply (metis in_inv_image mem_def)
blanchet
parents: 40724
diff changeset
   685
                  (fn () => "=============================================")
43094
269300fb83d0 more work on new Metis
blanchet
parents: 43093
diff changeset
   686
    in (fol_th, th) :: th_pairs end
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   687
42342
6babd86a54a4 handle case where the same Skolem name is given different types in different subgoals in the new Skolemizer (this can happen if several type-instances of the same fact are needed by Metis, cf. example in "Clausify.thy") -- the solution reintroduces old code removed in a6725f293377
blanchet
parents: 42341
diff changeset
   688
(* It is normally sufficient to apply "assume_tac" to unify the conclusion with
6babd86a54a4 handle case where the same Skolem name is given different types in different subgoals in the new Skolemizer (this can happen if several type-instances of the same fact are needed by Metis, cf. example in "Clausify.thy") -- the solution reintroduces old code removed in a6725f293377
blanchet
parents: 42341
diff changeset
   689
   one of the premises. Unfortunately, this sometimes yields "Variable
6babd86a54a4 handle case where the same Skolem name is given different types in different subgoals in the new Skolemizer (this can happen if several type-instances of the same fact are needed by Metis, cf. example in "Clausify.thy") -- the solution reintroduces old code removed in a6725f293377
blanchet
parents: 42341
diff changeset
   690
   ?SK_a_b_c_x has two distinct types" errors. To avoid this, we instantiate the
6babd86a54a4 handle case where the same Skolem name is given different types in different subgoals in the new Skolemizer (this can happen if several type-instances of the same fact are needed by Metis, cf. example in "Clausify.thy") -- the solution reintroduces old code removed in a6725f293377
blanchet
parents: 42341
diff changeset
   691
   variables before applying "assume_tac". Typical constraints are of the form
6babd86a54a4 handle case where the same Skolem name is given different types in different subgoals in the new Skolemizer (this can happen if several type-instances of the same fact are needed by Metis, cf. example in "Clausify.thy") -- the solution reintroduces old code removed in a6725f293377
blanchet
parents: 42341
diff changeset
   692
     ?SK_a_b_c_x SK_d_e_f_y ... SK_a_b_c_x ... SK_g_h_i_z =?= SK_a_b_c_x,
6babd86a54a4 handle case where the same Skolem name is given different types in different subgoals in the new Skolemizer (this can happen if several type-instances of the same fact are needed by Metis, cf. example in "Clausify.thy") -- the solution reintroduces old code removed in a6725f293377
blanchet
parents: 42341
diff changeset
   693
   where the nonvariables are goal parameters. *)
6babd86a54a4 handle case where the same Skolem name is given different types in different subgoals in the new Skolemizer (this can happen if several type-instances of the same fact are needed by Metis, cf. example in "Clausify.thy") -- the solution reintroduces old code removed in a6725f293377
blanchet
parents: 42341
diff changeset
   694
fun unify_first_prem_with_concl thy i th =
6babd86a54a4 handle case where the same Skolem name is given different types in different subgoals in the new Skolemizer (this can happen if several type-instances of the same fact are needed by Metis, cf. example in "Clausify.thy") -- the solution reintroduces old code removed in a6725f293377
blanchet
parents: 42341
diff changeset
   695
  let
6babd86a54a4 handle case where the same Skolem name is given different types in different subgoals in the new Skolemizer (this can happen if several type-instances of the same fact are needed by Metis, cf. example in "Clausify.thy") -- the solution reintroduces old code removed in a6725f293377
blanchet
parents: 42341
diff changeset
   696
    val goal = Logic.get_goal (prop_of th) i |> Envir.beta_eta_contract
6babd86a54a4 handle case where the same Skolem name is given different types in different subgoals in the new Skolemizer (this can happen if several type-instances of the same fact are needed by Metis, cf. example in "Clausify.thy") -- the solution reintroduces old code removed in a6725f293377
blanchet
parents: 42341
diff changeset
   697
    val prem = goal |> Logic.strip_assums_hyp |> hd
6babd86a54a4 handle case where the same Skolem name is given different types in different subgoals in the new Skolemizer (this can happen if several type-instances of the same fact are needed by Metis, cf. example in "Clausify.thy") -- the solution reintroduces old code removed in a6725f293377
blanchet
parents: 42341
diff changeset
   698
    val concl = goal |> Logic.strip_assums_concl
6babd86a54a4 handle case where the same Skolem name is given different types in different subgoals in the new Skolemizer (this can happen if several type-instances of the same fact are needed by Metis, cf. example in "Clausify.thy") -- the solution reintroduces old code removed in a6725f293377
blanchet
parents: 42341
diff changeset
   699
    fun pair_untyped_aconv (t1, t2) (u1, u2) =
43134
0c82e00ba63e make sure no warnings are given for polymorphic facts where we use a monomorphic instance
blanchet
parents: 43130
diff changeset
   700
      untyped_aconv (t1, u1) andalso untyped_aconv (t2, u2)
42342
6babd86a54a4 handle case where the same Skolem name is given different types in different subgoals in the new Skolemizer (this can happen if several type-instances of the same fact are needed by Metis, cf. example in "Clausify.thy") -- the solution reintroduces old code removed in a6725f293377
blanchet
parents: 42341
diff changeset
   701
    fun add_terms tp inst =
6babd86a54a4 handle case where the same Skolem name is given different types in different subgoals in the new Skolemizer (this can happen if several type-instances of the same fact are needed by Metis, cf. example in "Clausify.thy") -- the solution reintroduces old code removed in a6725f293377
blanchet
parents: 42341
diff changeset
   702
      if exists (pair_untyped_aconv tp) inst then inst
6babd86a54a4 handle case where the same Skolem name is given different types in different subgoals in the new Skolemizer (this can happen if several type-instances of the same fact are needed by Metis, cf. example in "Clausify.thy") -- the solution reintroduces old code removed in a6725f293377
blanchet
parents: 42341
diff changeset
   703
      else tp :: map (apsnd (subst_atomic [tp])) inst
6babd86a54a4 handle case where the same Skolem name is given different types in different subgoals in the new Skolemizer (this can happen if several type-instances of the same fact are needed by Metis, cf. example in "Clausify.thy") -- the solution reintroduces old code removed in a6725f293377
blanchet
parents: 42341
diff changeset
   704
    fun is_flex t =
6babd86a54a4 handle case where the same Skolem name is given different types in different subgoals in the new Skolemizer (this can happen if several type-instances of the same fact are needed by Metis, cf. example in "Clausify.thy") -- the solution reintroduces old code removed in a6725f293377
blanchet
parents: 42341
diff changeset
   705
      case strip_comb t of
6babd86a54a4 handle case where the same Skolem name is given different types in different subgoals in the new Skolemizer (this can happen if several type-instances of the same fact are needed by Metis, cf. example in "Clausify.thy") -- the solution reintroduces old code removed in a6725f293377
blanchet
parents: 42341
diff changeset
   706
        (Var _, args) => forall is_Bound args
6babd86a54a4 handle case where the same Skolem name is given different types in different subgoals in the new Skolemizer (this can happen if several type-instances of the same fact are needed by Metis, cf. example in "Clausify.thy") -- the solution reintroduces old code removed in a6725f293377
blanchet
parents: 42341
diff changeset
   707
      | _ => false
6babd86a54a4 handle case where the same Skolem name is given different types in different subgoals in the new Skolemizer (this can happen if several type-instances of the same fact are needed by Metis, cf. example in "Clausify.thy") -- the solution reintroduces old code removed in a6725f293377
blanchet
parents: 42341
diff changeset
   708
    fun unify_flex flex rigid =
6babd86a54a4 handle case where the same Skolem name is given different types in different subgoals in the new Skolemizer (this can happen if several type-instances of the same fact are needed by Metis, cf. example in "Clausify.thy") -- the solution reintroduces old code removed in a6725f293377
blanchet
parents: 42341
diff changeset
   709
      case strip_comb flex of
6babd86a54a4 handle case where the same Skolem name is given different types in different subgoals in the new Skolemizer (this can happen if several type-instances of the same fact are needed by Metis, cf. example in "Clausify.thy") -- the solution reintroduces old code removed in a6725f293377
blanchet
parents: 42341
diff changeset
   710
        (Var (z as (_, T)), args) =>
6babd86a54a4 handle case where the same Skolem name is given different types in different subgoals in the new Skolemizer (this can happen if several type-instances of the same fact are needed by Metis, cf. example in "Clausify.thy") -- the solution reintroduces old code removed in a6725f293377
blanchet
parents: 42341
diff changeset
   711
        add_terms (Var z,
6babd86a54a4 handle case where the same Skolem name is given different types in different subgoals in the new Skolemizer (this can happen if several type-instances of the same fact are needed by Metis, cf. example in "Clausify.thy") -- the solution reintroduces old code removed in a6725f293377
blanchet
parents: 42341
diff changeset
   712
          fold_rev (curry absdummy) (take (length args) (binder_types T)) rigid)
6babd86a54a4 handle case where the same Skolem name is given different types in different subgoals in the new Skolemizer (this can happen if several type-instances of the same fact are needed by Metis, cf. example in "Clausify.thy") -- the solution reintroduces old code removed in a6725f293377
blanchet
parents: 42341
diff changeset
   713
      | _ => I
6babd86a54a4 handle case where the same Skolem name is given different types in different subgoals in the new Skolemizer (this can happen if several type-instances of the same fact are needed by Metis, cf. example in "Clausify.thy") -- the solution reintroduces old code removed in a6725f293377
blanchet
parents: 42341
diff changeset
   714
    fun unify_potential_flex comb atom =
6babd86a54a4 handle case where the same Skolem name is given different types in different subgoals in the new Skolemizer (this can happen if several type-instances of the same fact are needed by Metis, cf. example in "Clausify.thy") -- the solution reintroduces old code removed in a6725f293377
blanchet
parents: 42341
diff changeset
   715
      if is_flex comb then unify_flex comb atom
6babd86a54a4 handle case where the same Skolem name is given different types in different subgoals in the new Skolemizer (this can happen if several type-instances of the same fact are needed by Metis, cf. example in "Clausify.thy") -- the solution reintroduces old code removed in a6725f293377
blanchet
parents: 42341
diff changeset
   716
      else if is_Var atom then add_terms (atom, comb)
6babd86a54a4 handle case where the same Skolem name is given different types in different subgoals in the new Skolemizer (this can happen if several type-instances of the same fact are needed by Metis, cf. example in "Clausify.thy") -- the solution reintroduces old code removed in a6725f293377
blanchet
parents: 42341
diff changeset
   717
      else I
6babd86a54a4 handle case where the same Skolem name is given different types in different subgoals in the new Skolemizer (this can happen if several type-instances of the same fact are needed by Metis, cf. example in "Clausify.thy") -- the solution reintroduces old code removed in a6725f293377
blanchet
parents: 42341
diff changeset
   718
    fun unify_terms (t, u) =
6babd86a54a4 handle case where the same Skolem name is given different types in different subgoals in the new Skolemizer (this can happen if several type-instances of the same fact are needed by Metis, cf. example in "Clausify.thy") -- the solution reintroduces old code removed in a6725f293377
blanchet
parents: 42341
diff changeset
   719
      case (t, u) of
6babd86a54a4 handle case where the same Skolem name is given different types in different subgoals in the new Skolemizer (this can happen if several type-instances of the same fact are needed by Metis, cf. example in "Clausify.thy") -- the solution reintroduces old code removed in a6725f293377
blanchet
parents: 42341
diff changeset
   720
        (t1 $ t2, u1 $ u2) =>
6babd86a54a4 handle case where the same Skolem name is given different types in different subgoals in the new Skolemizer (this can happen if several type-instances of the same fact are needed by Metis, cf. example in "Clausify.thy") -- the solution reintroduces old code removed in a6725f293377
blanchet
parents: 42341
diff changeset
   721
        if is_flex t then unify_flex t u
6babd86a54a4 handle case where the same Skolem name is given different types in different subgoals in the new Skolemizer (this can happen if several type-instances of the same fact are needed by Metis, cf. example in "Clausify.thy") -- the solution reintroduces old code removed in a6725f293377
blanchet
parents: 42341
diff changeset
   722
        else if is_flex u then unify_flex u t
6babd86a54a4 handle case where the same Skolem name is given different types in different subgoals in the new Skolemizer (this can happen if several type-instances of the same fact are needed by Metis, cf. example in "Clausify.thy") -- the solution reintroduces old code removed in a6725f293377
blanchet
parents: 42341
diff changeset
   723
        else fold unify_terms [(t1, u1), (t2, u2)]
6babd86a54a4 handle case where the same Skolem name is given different types in different subgoals in the new Skolemizer (this can happen if several type-instances of the same fact are needed by Metis, cf. example in "Clausify.thy") -- the solution reintroduces old code removed in a6725f293377
blanchet
parents: 42341
diff changeset
   724
      | (_ $ _, _) => unify_potential_flex t u
6babd86a54a4 handle case where the same Skolem name is given different types in different subgoals in the new Skolemizer (this can happen if several type-instances of the same fact are needed by Metis, cf. example in "Clausify.thy") -- the solution reintroduces old code removed in a6725f293377
blanchet
parents: 42341
diff changeset
   725
      | (_, _ $ _) => unify_potential_flex u t
6babd86a54a4 handle case where the same Skolem name is given different types in different subgoals in the new Skolemizer (this can happen if several type-instances of the same fact are needed by Metis, cf. example in "Clausify.thy") -- the solution reintroduces old code removed in a6725f293377
blanchet
parents: 42341
diff changeset
   726
      | (Var _, _) => add_terms (t, u)
6babd86a54a4 handle case where the same Skolem name is given different types in different subgoals in the new Skolemizer (this can happen if several type-instances of the same fact are needed by Metis, cf. example in "Clausify.thy") -- the solution reintroduces old code removed in a6725f293377
blanchet
parents: 42341
diff changeset
   727
      | (_, Var _) => add_terms (u, t)
6babd86a54a4 handle case where the same Skolem name is given different types in different subgoals in the new Skolemizer (this can happen if several type-instances of the same fact are needed by Metis, cf. example in "Clausify.thy") -- the solution reintroduces old code removed in a6725f293377
blanchet
parents: 42341
diff changeset
   728
      | _ => I
42344
4a58173ffb99 "unify_first_prem_with_concl" (cf. 9ceb585c097a) sometimes throws an exception, but it is very rarely needed -- catch the exception for now
blanchet
parents: 42342
diff changeset
   729
    val t_inst =
4a58173ffb99 "unify_first_prem_with_concl" (cf. 9ceb585c097a) sometimes throws an exception, but it is very rarely needed -- catch the exception for now
blanchet
parents: 42342
diff changeset
   730
      [] |> try (unify_terms (prem, concl) #> map (pairself (cterm_of thy)))
4a58173ffb99 "unify_first_prem_with_concl" (cf. 9ceb585c097a) sometimes throws an exception, but it is very rarely needed -- catch the exception for now
blanchet
parents: 42342
diff changeset
   731
         |> the_default [] (* FIXME *)
42342
6babd86a54a4 handle case where the same Skolem name is given different types in different subgoals in the new Skolemizer (this can happen if several type-instances of the same fact are needed by Metis, cf. example in "Clausify.thy") -- the solution reintroduces old code removed in a6725f293377
blanchet
parents: 42341
diff changeset
   732
  in th |> cterm_instantiate t_inst end
39964
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   733
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   734
val copy_prem = @{lemma "P ==> (P ==> P ==> Q) ==> Q" by fast}
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   735
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   736
fun copy_prems_tac [] ns i =
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   737
    if forall (curry (op =) 1) ns then all_tac else copy_prems_tac (rev ns) [] i
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   738
  | copy_prems_tac (1 :: ms) ns i =
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   739
    rotate_tac 1 i THEN copy_prems_tac ms (1 :: ns) i
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   740
  | copy_prems_tac (m :: ms) ns i =
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   741
    etac copy_prem i THEN copy_prems_tac ms (m div 2 :: (m + 1) div 2 :: ns) i
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   742
42271
7d08265f181d further development of new Skolemizer -- make sure constructed terms have correct types and fixed a few bugs where the goal was out of sync with what we had in mind
blanchet
parents: 42270
diff changeset
   743
(* Metis generates variables of the form _nnn. *)
7d08265f181d further development of new Skolemizer -- make sure constructed terms have correct types and fixed a few bugs where the goal was out of sync with what we had in mind
blanchet
parents: 42270
diff changeset
   744
val is_metis_fresh_variable = String.isPrefix "_"
7d08265f181d further development of new Skolemizer -- make sure constructed terms have correct types and fixed a few bugs where the goal was out of sync with what we had in mind
blanchet
parents: 42270
diff changeset
   745
40258
2c0d8fe36c21 make handling of parameters more robust, by querying the goal
blanchet
parents: 40221
diff changeset
   746
fun instantiate_forall_tac thy t i st =
39964
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   747
  let
40258
2c0d8fe36c21 make handling of parameters more robust, by querying the goal
blanchet
parents: 40221
diff changeset
   748
    val params = Logic.strip_params (Logic.get_goal (prop_of st) i) |> rev
39964
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   749
    fun repair (t as (Var ((s, _), _))) =
40258
2c0d8fe36c21 make handling of parameters more robust, by querying the goal
blanchet
parents: 40221
diff changeset
   750
        (case find_index (fn (s', _) => s' = s) params of
39964
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   751
           ~1 => t
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   752
         | j => Bound j)
40261
7a02144874f3 more work on new Skolemizer without Hilbert_Choice
blanchet
parents: 40259
diff changeset
   753
      | repair (t $ u) =
7a02144874f3 more work on new Skolemizer without Hilbert_Choice
blanchet
parents: 40259
diff changeset
   754
        (case (repair t, repair u) of
7a02144874f3 more work on new Skolemizer without Hilbert_Choice
blanchet
parents: 40259
diff changeset
   755
           (t as Bound j, u as Bound k) =>
7a02144874f3 more work on new Skolemizer without Hilbert_Choice
blanchet
parents: 40259
diff changeset
   756
           (* This is a rather subtle trick to repair the discrepancy between
7a02144874f3 more work on new Skolemizer without Hilbert_Choice
blanchet
parents: 40259
diff changeset
   757
              the fully skolemized term that MESON gives us (where existentials
7a02144874f3 more work on new Skolemizer without Hilbert_Choice
blanchet
parents: 40259
diff changeset
   758
              were pulled out) and the reality. *)
7a02144874f3 more work on new Skolemizer without Hilbert_Choice
blanchet
parents: 40259
diff changeset
   759
           if k > j then t else t $ u
7a02144874f3 more work on new Skolemizer without Hilbert_Choice
blanchet
parents: 40259
diff changeset
   760
         | (t, u) => t $ u)
39964
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   761
      | repair t = t
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   762
    val t' = t |> repair |> fold (curry absdummy) (map snd params)
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   763
    fun do_instantiate th =
42270
5f2960582e45 make new Skolemizer more robust
blanchet
parents: 42107
diff changeset
   764
      case Term.add_vars (prop_of th) []
42271
7d08265f181d further development of new Skolemizer -- make sure constructed terms have correct types and fixed a few bugs where the goal was out of sync with what we had in mind
blanchet
parents: 42270
diff changeset
   765
           |> filter_out ((Meson_Clausify.is_zapped_var_name orf
7d08265f181d further development of new Skolemizer -- make sure constructed terms have correct types and fixed a few bugs where the goal was out of sync with what we had in mind
blanchet
parents: 42270
diff changeset
   766
                           is_metis_fresh_variable) o fst o fst) of
42270
5f2960582e45 make new Skolemizer more robust
blanchet
parents: 42107
diff changeset
   767
        [] => th
42271
7d08265f181d further development of new Skolemizer -- make sure constructed terms have correct types and fixed a few bugs where the goal was out of sync with what we had in mind
blanchet
parents: 42270
diff changeset
   768
      | [var as (_, T)] =>
7d08265f181d further development of new Skolemizer -- make sure constructed terms have correct types and fixed a few bugs where the goal was out of sync with what we had in mind
blanchet
parents: 42270
diff changeset
   769
        let
7d08265f181d further development of new Skolemizer -- make sure constructed terms have correct types and fixed a few bugs where the goal was out of sync with what we had in mind
blanchet
parents: 42270
diff changeset
   770
          val var_binder_Ts = T |> binder_types |> take (length params) |> rev
7d08265f181d further development of new Skolemizer -- make sure constructed terms have correct types and fixed a few bugs where the goal was out of sync with what we had in mind
blanchet
parents: 42270
diff changeset
   771
          val var_body_T = T |> funpow (length params) range_type
7d08265f181d further development of new Skolemizer -- make sure constructed terms have correct types and fixed a few bugs where the goal was out of sync with what we had in mind
blanchet
parents: 42270
diff changeset
   772
          val tyenv =
7d08265f181d further development of new Skolemizer -- make sure constructed terms have correct types and fixed a few bugs where the goal was out of sync with what we had in mind
blanchet
parents: 42270
diff changeset
   773
            Vartab.empty |> Type.raw_unifys (fastype_of t :: map snd params,
7d08265f181d further development of new Skolemizer -- make sure constructed terms have correct types and fixed a few bugs where the goal was out of sync with what we had in mind
blanchet
parents: 42270
diff changeset
   774
                                             var_body_T :: var_binder_Ts)
7d08265f181d further development of new Skolemizer -- make sure constructed terms have correct types and fixed a few bugs where the goal was out of sync with what we had in mind
blanchet
parents: 42270
diff changeset
   775
          val env =
7d08265f181d further development of new Skolemizer -- make sure constructed terms have correct types and fixed a few bugs where the goal was out of sync with what we had in mind
blanchet
parents: 42270
diff changeset
   776
            Envir.Envir {maxidx = Vartab.fold (Integer.max o snd o fst) tyenv 0,
7d08265f181d further development of new Skolemizer -- make sure constructed terms have correct types and fixed a few bugs where the goal was out of sync with what we had in mind
blanchet
parents: 42270
diff changeset
   777
                         tenv = Vartab.empty, tyenv = tyenv}
7d08265f181d further development of new Skolemizer -- make sure constructed terms have correct types and fixed a few bugs where the goal was out of sync with what we had in mind
blanchet
parents: 42270
diff changeset
   778
          val ty_inst =
7d08265f181d further development of new Skolemizer -- make sure constructed terms have correct types and fixed a few bugs where the goal was out of sync with what we had in mind
blanchet
parents: 42270
diff changeset
   779
            Vartab.fold (fn (x, (S, T)) =>
7d08265f181d further development of new Skolemizer -- make sure constructed terms have correct types and fixed a few bugs where the goal was out of sync with what we had in mind
blanchet
parents: 42270
diff changeset
   780
                            cons (pairself (ctyp_of thy) (TVar (x, S), T)))
7d08265f181d further development of new Skolemizer -- make sure constructed terms have correct types and fixed a few bugs where the goal was out of sync with what we had in mind
blanchet
parents: 42270
diff changeset
   781
                        tyenv []
7d08265f181d further development of new Skolemizer -- make sure constructed terms have correct types and fixed a few bugs where the goal was out of sync with what we had in mind
blanchet
parents: 42270
diff changeset
   782
          val t_inst =
7d08265f181d further development of new Skolemizer -- make sure constructed terms have correct types and fixed a few bugs where the goal was out of sync with what we had in mind
blanchet
parents: 42270
diff changeset
   783
            [pairself (cterm_of thy o Envir.norm_term env) (Var var, t')]
42342
6babd86a54a4 handle case where the same Skolem name is given different types in different subgoals in the new Skolemizer (this can happen if several type-instances of the same fact are needed by Metis, cf. example in "Clausify.thy") -- the solution reintroduces old code removed in a6725f293377
blanchet
parents: 42341
diff changeset
   784
        in th |> instantiate (ty_inst, t_inst) end
42271
7d08265f181d further development of new Skolemizer -- make sure constructed terms have correct types and fixed a few bugs where the goal was out of sync with what we had in mind
blanchet
parents: 42270
diff changeset
   785
      | _ => raise Fail "expected a single non-zapped, non-Metis Var"
39964
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   786
  in
42271
7d08265f181d further development of new Skolemizer -- make sure constructed terms have correct types and fixed a few bugs where the goal was out of sync with what we had in mind
blanchet
parents: 42270
diff changeset
   787
    (DETERM (etac @{thm allE} i THEN rotate_tac ~1 i)
40258
2c0d8fe36c21 make handling of parameters more robust, by querying the goal
blanchet
parents: 40221
diff changeset
   788
     THEN PRIMITIVE do_instantiate) st
39964
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   789
  end
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   790
41135
8c5d44c7e8af tuning: unused var
blanchet
parents: 40868
diff changeset
   791
fun fix_exists_tac t =
40261
7a02144874f3 more work on new Skolemizer without Hilbert_Choice
blanchet
parents: 40259
diff changeset
   792
  etac @{thm exE}
7a02144874f3 more work on new Skolemizer without Hilbert_Choice
blanchet
parents: 40259
diff changeset
   793
  THEN' rename_tac [t |> dest_Var |> fst |> fst]
7a02144874f3 more work on new Skolemizer without Hilbert_Choice
blanchet
parents: 40259
diff changeset
   794
7a02144874f3 more work on new Skolemizer without Hilbert_Choice
blanchet
parents: 40259
diff changeset
   795
fun release_quantifier_tac thy (skolem, t) =
41135
8c5d44c7e8af tuning: unused var
blanchet
parents: 40868
diff changeset
   796
  (if skolem then fix_exists_tac else instantiate_forall_tac thy) t
40261
7a02144874f3 more work on new Skolemizer without Hilbert_Choice
blanchet
parents: 40259
diff changeset
   797
40258
2c0d8fe36c21 make handling of parameters more robust, by querying the goal
blanchet
parents: 40221
diff changeset
   798
fun release_clusters_tac _ _ _ [] = K all_tac
2c0d8fe36c21 make handling of parameters more robust, by querying the goal
blanchet
parents: 40221
diff changeset
   799
  | release_clusters_tac thy ax_counts substs
39964
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   800
                         ((ax_no, cluster_no) :: clusters) =
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   801
    let
40261
7a02144874f3 more work on new Skolemizer without Hilbert_Choice
blanchet
parents: 40259
diff changeset
   802
      val cluster_of_var =
7a02144874f3 more work on new Skolemizer without Hilbert_Choice
blanchet
parents: 40259
diff changeset
   803
        Meson_Clausify.cluster_of_zapped_var_name o fst o fst o dest_Var
7a02144874f3 more work on new Skolemizer without Hilbert_Choice
blanchet
parents: 40259
diff changeset
   804
      fun in_right_cluster ((_, (cluster_no', _)), _) = cluster_no' = cluster_no
39964
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   805
      val cluster_substs =
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   806
        substs
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   807
        |> map_filter (fn (ax_no', (_, (_, tsubst))) =>
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   808
                          if ax_no' = ax_no then
40261
7a02144874f3 more work on new Skolemizer without Hilbert_Choice
blanchet
parents: 40259
diff changeset
   809
                            tsubst |> map (apfst cluster_of_var)
7a02144874f3 more work on new Skolemizer without Hilbert_Choice
blanchet
parents: 40259
diff changeset
   810
                                   |> filter (in_right_cluster o fst)
7a02144874f3 more work on new Skolemizer without Hilbert_Choice
blanchet
parents: 40259
diff changeset
   811
                                   |> map (apfst snd)
7a02144874f3 more work on new Skolemizer without Hilbert_Choice
blanchet
parents: 40259
diff changeset
   812
                                   |> SOME
7a02144874f3 more work on new Skolemizer without Hilbert_Choice
blanchet
parents: 40259
diff changeset
   813
                          else
7a02144874f3 more work on new Skolemizer without Hilbert_Choice
blanchet
parents: 40259
diff changeset
   814
                            NONE)
39964
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   815
      fun do_cluster_subst cluster_subst =
40261
7a02144874f3 more work on new Skolemizer without Hilbert_Choice
blanchet
parents: 40259
diff changeset
   816
        map (release_quantifier_tac thy) cluster_subst @ [rotate_tac 1]
39964
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   817
      val first_prem = find_index (fn (ax_no', _) => ax_no' = ax_no) substs
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   818
    in
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   819
      rotate_tac first_prem
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   820
      THEN' (EVERY' (maps do_cluster_subst cluster_substs))
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   821
      THEN' rotate_tac (~ first_prem - length cluster_substs)
40258
2c0d8fe36c21 make handling of parameters more robust, by querying the goal
blanchet
parents: 40221
diff changeset
   822
      THEN' release_clusters_tac thy ax_counts substs clusters
39964
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   823
    end
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   824
40264
b91e2e16d994 fixed order of quantifier instantiation in new Skolemizer
blanchet
parents: 40261
diff changeset
   825
fun cluster_key ((ax_no, (cluster_no, index_no)), skolem) =
b91e2e16d994 fixed order of quantifier instantiation in new Skolemizer
blanchet
parents: 40261
diff changeset
   826
  (ax_no, (cluster_no, (skolem, index_no)))
b91e2e16d994 fixed order of quantifier instantiation in new Skolemizer
blanchet
parents: 40261
diff changeset
   827
fun cluster_ord p =
b91e2e16d994 fixed order of quantifier instantiation in new Skolemizer
blanchet
parents: 40261
diff changeset
   828
  prod_ord int_ord (prod_ord int_ord (prod_ord bool_ord int_ord))
b91e2e16d994 fixed order of quantifier instantiation in new Skolemizer
blanchet
parents: 40261
diff changeset
   829
           (pairself cluster_key p)
39964
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   830
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   831
val tysubst_ord =
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   832
  list_ord (prod_ord Term_Ord.fast_indexname_ord
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   833
                     (prod_ord Term_Ord.sort_ord Term_Ord.typ_ord))
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   834
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   835
structure Int_Tysubst_Table =
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   836
  Table(type key = int * (indexname * (sort * typ)) list
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   837
        val ord = prod_ord int_ord tysubst_ord)
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   838
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   839
structure Int_Pair_Graph =
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   840
  Graph(type key = int * int val ord = prod_ord int_ord int_ord)
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   841
42271
7d08265f181d further development of new Skolemizer -- make sure constructed terms have correct types and fixed a few bugs where the goal was out of sync with what we had in mind
blanchet
parents: 42270
diff changeset
   842
fun shuffle_key (((axiom_no, (_, index_no)), _), _) = (axiom_no, index_no)
40258
2c0d8fe36c21 make handling of parameters more robust, by querying the goal
blanchet
parents: 40221
diff changeset
   843
fun shuffle_ord p = prod_ord int_ord int_ord (pairself shuffle_key p)
2c0d8fe36c21 make handling of parameters more robust, by querying the goal
blanchet
parents: 40221
diff changeset
   844
39964
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   845
(* Attempts to derive the theorem "False" from a theorem of the form
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   846
   "P1 ==> ... ==> Pn ==> False", where the "Pi"s are to be discharged using the
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   847
   specified axioms. The axioms have leading "All" and "Ex" quantifiers, which
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   848
   must be eliminated first. *)
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   849
fun discharge_skolem_premises ctxt axioms prems_imp_false =
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   850
  if prop_of prems_imp_false aconv @{prop False} then
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   851
    prems_imp_false
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   852
  else
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   853
    let
42361
23f352990944 modernized structure Proof_Context;
wenzelm
parents: 42354
diff changeset
   854
      val thy = Proof_Context.theory_of ctxt
39964
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   855
      fun match_term p =
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   856
        let
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   857
          val (tyenv, tenv) =
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   858
            Pattern.first_order_match thy p (Vartab.empty, Vartab.empty)
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   859
          val tsubst =
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   860
            tenv |> Vartab.dest
42099
447fa058ab22 avoid evil "export_without_context", which breaks if there are local "fixes"
blanchet
parents: 42098
diff changeset
   861
                 |> filter (Meson_Clausify.is_zapped_var_name o fst o fst)
39964
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   862
                 |> sort (cluster_ord
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   863
                          o pairself (Meson_Clausify.cluster_of_zapped_var_name
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   864
                                      o fst o fst))
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   865
                 |> map (Meson.term_pair_of
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   866
                         #> pairself (Envir.subst_term_types tyenv))
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   867
          val tysubst = tyenv |> Vartab.dest
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   868
        in (tysubst, tsubst) end
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   869
      fun subst_info_for_prem subgoal_no prem =
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   870
        case prem of
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   871
          _ $ (Const (@{const_name Meson.skolem}, _) $ (_ $ t $ num)) =>
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   872
          let val ax_no = HOLogic.dest_nat num in
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   873
            (ax_no, (subgoal_no,
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   874
                     match_term (nth axioms ax_no |> the |> snd, t)))
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   875
          end
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   876
        | _ => raise TERM ("discharge_skolem_premises: Malformed premise",
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   877
                           [prem])
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   878
      fun cluster_of_var_name skolem s =
42098
f978caf60bbe more robust handling of variables in new Skolemizer
blanchet
parents: 41491
diff changeset
   879
        case try Meson_Clausify.cluster_of_zapped_var_name s of
f978caf60bbe more robust handling of variables in new Skolemizer
blanchet
parents: 41491
diff changeset
   880
          NONE => NONE
f978caf60bbe more robust handling of variables in new Skolemizer
blanchet
parents: 41491
diff changeset
   881
        | SOME ((ax_no, (cluster_no, _)), skolem') =>
39964
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   882
          if skolem' = skolem andalso cluster_no > 0 then
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   883
            SOME (ax_no, cluster_no)
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   884
          else
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   885
            NONE
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   886
      fun clusters_in_term skolem t =
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   887
        Term.add_var_names t [] |> map_filter (cluster_of_var_name skolem o fst)
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   888
      fun deps_for_term_subst (var, t) =
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   889
        case clusters_in_term false var of
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   890
          [] => NONE
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   891
        | [(ax_no, cluster_no)] =>
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   892
          SOME ((ax_no, cluster_no),
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   893
                clusters_in_term true t
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   894
                |> cluster_no > 1 ? cons (ax_no, cluster_no - 1))
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   895
        | _ => raise TERM ("discharge_skolem_premises: Expected Var", [var])
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   896
      val prems = Logic.strip_imp_prems (prop_of prems_imp_false)
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   897
      val substs = prems |> map2 subst_info_for_prem (1 upto length prems)
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   898
                         |> sort (int_ord o pairself fst)
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   899
      val depss = maps (map_filter deps_for_term_subst o snd o snd o snd) substs
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   900
      val clusters = maps (op ::) depss
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   901
      val ordered_clusters =
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   902
        Int_Pair_Graph.empty
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   903
        |> fold Int_Pair_Graph.default_node (map (rpair ()) clusters)
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   904
        |> fold Int_Pair_Graph.add_deps_acyclic depss
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   905
        |> Int_Pair_Graph.topological_order
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   906
        handle Int_Pair_Graph.CYCLES _ =>
40158
a88d6073b190 clearer error messages
blanchet
parents: 39978
diff changeset
   907
               error "Cannot replay Metis proof in Isabelle without \
a88d6073b190 clearer error messages
blanchet
parents: 39978
diff changeset
   908
                     \\"Hilbert_Choice\"."
39964
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   909
      val ax_counts =
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   910
        Int_Tysubst_Table.empty
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   911
        |> fold (fn (ax_no, (_, (tysubst, _))) =>
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   912
                    Int_Tysubst_Table.map_default ((ax_no, tysubst), 0)
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   913
                                                  (Integer.add 1)) substs
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   914
        |> Int_Tysubst_Table.dest
42339
0e5d1e5e1177 use the list of actually used axioms to (correctly) precompute the "outer params", not all axioms
blanchet
parents: 42337
diff changeset
   915
      val needed_axiom_props =
0e5d1e5e1177 use the list of actually used axioms to (correctly) precompute the "outer params", not all axioms
blanchet
parents: 42337
diff changeset
   916
        0 upto length axioms - 1 ~~ axioms
0e5d1e5e1177 use the list of actually used axioms to (correctly) precompute the "outer params", not all axioms
blanchet
parents: 42337
diff changeset
   917
        |> map_filter (fn (_, NONE) => NONE
0e5d1e5e1177 use the list of actually used axioms to (correctly) precompute the "outer params", not all axioms
blanchet
parents: 42337
diff changeset
   918
                        | (ax_no, SOME (_, t)) =>
0e5d1e5e1177 use the list of actually used axioms to (correctly) precompute the "outer params", not all axioms
blanchet
parents: 42337
diff changeset
   919
                          if exists (fn ((ax_no', _), n) =>
0e5d1e5e1177 use the list of actually used axioms to (correctly) precompute the "outer params", not all axioms
blanchet
parents: 42337
diff changeset
   920
                                        ax_no' = ax_no andalso n > 0)
0e5d1e5e1177 use the list of actually used axioms to (correctly) precompute the "outer params", not all axioms
blanchet
parents: 42337
diff changeset
   921
                                    ax_counts then
0e5d1e5e1177 use the list of actually used axioms to (correctly) precompute the "outer params", not all axioms
blanchet
parents: 42337
diff changeset
   922
                            SOME t
0e5d1e5e1177 use the list of actually used axioms to (correctly) precompute the "outer params", not all axioms
blanchet
parents: 42337
diff changeset
   923
                          else
0e5d1e5e1177 use the list of actually used axioms to (correctly) precompute the "outer params", not all axioms
blanchet
parents: 42337
diff changeset
   924
                            NONE)
0e5d1e5e1177 use the list of actually used axioms to (correctly) precompute the "outer params", not all axioms
blanchet
parents: 42337
diff changeset
   925
      val outer_param_names =
0e5d1e5e1177 use the list of actually used axioms to (correctly) precompute the "outer params", not all axioms
blanchet
parents: 42337
diff changeset
   926
        [] |> fold Term.add_var_names needed_axiom_props
0e5d1e5e1177 use the list of actually used axioms to (correctly) precompute the "outer params", not all axioms
blanchet
parents: 42337
diff changeset
   927
           |> filter (Meson_Clausify.is_zapped_var_name o fst)
0e5d1e5e1177 use the list of actually used axioms to (correctly) precompute the "outer params", not all axioms
blanchet
parents: 42337
diff changeset
   928
           |> map (`(Meson_Clausify.cluster_of_zapped_var_name o fst))
0e5d1e5e1177 use the list of actually used axioms to (correctly) precompute the "outer params", not all axioms
blanchet
parents: 42337
diff changeset
   929
           |> filter (fn (((_, (cluster_no, _)), skolem), _) =>
0e5d1e5e1177 use the list of actually used axioms to (correctly) precompute the "outer params", not all axioms
blanchet
parents: 42337
diff changeset
   930
                         cluster_no = 0 andalso skolem)
0e5d1e5e1177 use the list of actually used axioms to (correctly) precompute the "outer params", not all axioms
blanchet
parents: 42337
diff changeset
   931
           |> sort shuffle_ord |> map (fst o snd)
42270
5f2960582e45 make new Skolemizer more robust
blanchet
parents: 42107
diff changeset
   932
(* for debugging only:
39964
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   933
      fun string_for_subst_info (ax_no, (subgoal_no, (tysubst, tsubst))) =
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   934
        "ax: " ^ string_of_int ax_no ^ "; asm: " ^ string_of_int subgoal_no ^
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   935
        "; tysubst: " ^ PolyML.makestring tysubst ^ "; tsubst: {" ^
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   936
        commas (map ((fn (s, t) => s ^ " |-> " ^ t)
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   937
                     o pairself (Syntax.string_of_term ctxt)) tsubst) ^ "}"
40264
b91e2e16d994 fixed order of quantifier instantiation in new Skolemizer
blanchet
parents: 40261
diff changeset
   938
      val _ = tracing ("ORDERED CLUSTERS: " ^ PolyML.makestring ordered_clusters)
b91e2e16d994 fixed order of quantifier instantiation in new Skolemizer
blanchet
parents: 40261
diff changeset
   939
      val _ = tracing ("AXIOM COUNTS: " ^ PolyML.makestring ax_counts)
42339
0e5d1e5e1177 use the list of actually used axioms to (correctly) precompute the "outer params", not all axioms
blanchet
parents: 42337
diff changeset
   940
      val _ = tracing ("OUTER PARAMS: " ^ PolyML.makestring outer_param_names)
39964
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   941
      val _ = tracing ("SUBSTS (" ^ string_of_int (length substs) ^ "):\n" ^
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   942
                       cat_lines (map string_for_subst_info substs))
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   943
*)
42271
7d08265f181d further development of new Skolemizer -- make sure constructed terms have correct types and fixed a few bugs where the goal was out of sync with what we had in mind
blanchet
parents: 42270
diff changeset
   944
      fun cut_and_ex_tac axiom =
7d08265f181d further development of new Skolemizer -- make sure constructed terms have correct types and fixed a few bugs where the goal was out of sync with what we had in mind
blanchet
parents: 42270
diff changeset
   945
        cut_rules_tac [axiom] 1
7d08265f181d further development of new Skolemizer -- make sure constructed terms have correct types and fixed a few bugs where the goal was out of sync with what we had in mind
blanchet
parents: 42270
diff changeset
   946
        THEN TRY (REPEAT_ALL_NEW (etac @{thm exE}) 1)
39964
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   947
      fun rotation_for_subgoal i =
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   948
        find_index (fn (_, (subgoal_no, _)) => subgoal_no = i) substs
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   949
    in
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   950
      Goal.prove ctxt [] [] @{prop False}
42271
7d08265f181d further development of new Skolemizer -- make sure constructed terms have correct types and fixed a few bugs where the goal was out of sync with what we had in mind
blanchet
parents: 42270
diff changeset
   951
          (K (DETERM (EVERY (map (cut_and_ex_tac o fst o the o nth axioms o fst
7d08265f181d further development of new Skolemizer -- make sure constructed terms have correct types and fixed a few bugs where the goal was out of sync with what we had in mind
blanchet
parents: 42270
diff changeset
   952
                                  o fst) ax_counts)
7d08265f181d further development of new Skolemizer -- make sure constructed terms have correct types and fixed a few bugs where the goal was out of sync with what we had in mind
blanchet
parents: 42270
diff changeset
   953
                      THEN rename_tac outer_param_names 1
7d08265f181d further development of new Skolemizer -- make sure constructed terms have correct types and fixed a few bugs where the goal was out of sync with what we had in mind
blanchet
parents: 42270
diff changeset
   954
                      THEN copy_prems_tac (map snd ax_counts) [] 1)
40258
2c0d8fe36c21 make handling of parameters more robust, by querying the goal
blanchet
parents: 40221
diff changeset
   955
              THEN release_clusters_tac thy ax_counts substs ordered_clusters 1
39964
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   956
              THEN match_tac [prems_imp_false] 1
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   957
              THEN ALLGOALS (fn i =>
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   958
                       rtac @{thm Meson.skolem_COMBK_I} i
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   959
                       THEN rotate_tac (rotation_for_subgoal i) i
42342
6babd86a54a4 handle case where the same Skolem name is given different types in different subgoals in the new Skolemizer (this can happen if several type-instances of the same fact are needed by Metis, cf. example in "Clausify.thy") -- the solution reintroduces old code removed in a6725f293377
blanchet
parents: 42341
diff changeset
   960
                       THEN PRIMITIVE (unify_first_prem_with_concl thy i)
42271
7d08265f181d further development of new Skolemizer -- make sure constructed terms have correct types and fixed a few bugs where the goal was out of sync with what we had in mind
blanchet
parents: 42270
diff changeset
   961
                       THEN assume_tac i
42270
5f2960582e45 make new Skolemizer more robust
blanchet
parents: 42107
diff changeset
   962
                       THEN flexflex_tac)))
40158
a88d6073b190 clearer error messages
blanchet
parents: 39978
diff changeset
   963
      handle ERROR _ =>
a88d6073b190 clearer error messages
blanchet
parents: 39978
diff changeset
   964
             error ("Cannot replay Metis proof in Isabelle:\n\
a88d6073b190 clearer error messages
blanchet
parents: 39978
diff changeset
   965
                    \Error when discharging Skolem assumptions.")
39964
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   966
    end
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   967
39495
bb4fb9ffe2d1 added new "Metis_Reconstruct" module, temporarily empty
blanchet
parents:
diff changeset
   968
end;