src/HOL/Tools/Metis/metis_reconstruct.ML
author haftmann
Thu, 19 Jun 2025 17:15:40 +0200
changeset 82734 89347c0cc6a3
parent 81254 d3c0734059ee
permissions -rw-r--r--
treat map_filter similar to list_all, list_ex, list_ex1
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
46320
0b8b73b49848 renamed two files to make room for a new file
blanchet
parents: 45569
diff changeset
    12
  type type_enc = ATP_Problem_Generate.type_enc
44492
a330c0608da8 avoid using ":" for anything but systematic type tag annotations, because Hurd's Metis gives it that special semantics
blanchet
parents: 44241
diff changeset
    13
50875
bfb626265782 less brutal Metis failure -- the brutality was accidentally introduced by df8ae0590be2
blanchet
parents: 48132
diff changeset
    14
  exception METIS_RECONSTRUCT of string * string
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
    15
81254
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents: 80910
diff changeset
    16
  val hol_term_of_metis : Proof.context -> type_enc -> int Symtab.table ->
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents: 80910
diff changeset
    17
    Metis_Term.term -> term
57408
39b3562e9edc tuned whitespace and parentheses
blanchet
parents: 57400
diff changeset
    18
  val hol_clause_of_metis : Proof.context -> type_enc -> int Symtab.table ->
39b3562e9edc tuned whitespace and parentheses
blanchet
parents: 57400
diff changeset
    19
    (string * term) list * (string * term) list -> Metis_Thm.thm -> term
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
    20
  val lookth : (Metis_Thm.thm * 'a) list -> Metis_Thm.thm -> 'a
57408
39b3562e9edc tuned whitespace and parentheses
blanchet
parents: 57400
diff changeset
    21
  val replay_one_inference : Proof.context -> type_enc ->
39b3562e9edc tuned whitespace and parentheses
blanchet
parents: 57400
diff changeset
    22
    (string * term) list * (string * term) list -> int Symtab.table ->
39b3562e9edc tuned whitespace and parentheses
blanchet
parents: 57400
diff changeset
    23
    Metis_Thm.thm * Metis_Proof.inference -> (Metis_Thm.thm * thm) list ->
39b3562e9edc tuned whitespace and parentheses
blanchet
parents: 57400
diff changeset
    24
    (Metis_Thm.thm * thm) list
39b3562e9edc tuned whitespace and parentheses
blanchet
parents: 57400
diff changeset
    25
  val discharge_skolem_premises : Proof.context -> (thm * term) option list -> thm -> thm
39495
bb4fb9ffe2d1 added new "Metis_Reconstruct" module, temporarily empty
blanchet
parents:
diff changeset
    26
end;
bb4fb9ffe2d1 added new "Metis_Reconstruct" module, temporarily empty
blanchet
parents:
diff changeset
    27
bb4fb9ffe2d1 added new "Metis_Reconstruct" module, temporarily empty
blanchet
parents:
diff changeset
    28
structure Metis_Reconstruct : METIS_RECONSTRUCT =
bb4fb9ffe2d1 added new "Metis_Reconstruct" module, temporarily empty
blanchet
parents:
diff changeset
    29
struct
bb4fb9ffe2d1 added new "Metis_Reconstruct" module, temporarily empty
blanchet
parents:
diff changeset
    30
43094
269300fb83d0 more work on new Metis
blanchet
parents: 43093
diff changeset
    31
open ATP_Problem
46320
0b8b73b49848 renamed two files to make room for a new file
blanchet
parents: 45569
diff changeset
    32
open ATP_Problem_Generate
0b8b73b49848 renamed two files to make room for a new file
blanchet
parents: 45569
diff changeset
    33
open ATP_Proof_Reconstruct
0b8b73b49848 renamed two files to make room for a new file
blanchet
parents: 45569
diff changeset
    34
open Metis_Generate
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
    35
50875
bfb626265782 less brutal Metis failure -- the brutality was accidentally introduced by df8ae0590be2
blanchet
parents: 48132
diff changeset
    36
exception METIS_RECONSTRUCT of string * string
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
    37
57400
13b06c626163 resolution modulo double negation
blanchet
parents: 57255
diff changeset
    38
val meta_not_not = @{thms not_not[THEN eq_reflection]}
13b06c626163 resolution modulo double negation
blanchet
parents: 57255
diff changeset
    39
52031
9a9238342963 tuning -- renamed '_from_' to '_of_' in Sledgehammer
blanchet
parents: 51998
diff changeset
    40
fun atp_name_of_metis type_enc s =
57408
39b3562e9edc tuned whitespace and parentheses
blanchet
parents: 57400
diff changeset
    41
  (case find_first (fn (_, (f, _)) => f type_enc = s) metis_name_table of
43104
81d1b15aa0ae use ":" for type information (looks good in Metis's output) and handle it in new path finder
blanchet
parents: 43103
diff changeset
    42
    SOME ((s, _), (_, swap)) => (s, swap)
57408
39b3562e9edc tuned whitespace and parentheses
blanchet
parents: 57400
diff changeset
    43
  | _ => (s, false))
39b3562e9edc tuned whitespace and parentheses
blanchet
parents: 57400
diff changeset
    44
52031
9a9238342963 tuning -- renamed '_from_' to '_of_' in Sledgehammer
blanchet
parents: 51998
diff changeset
    45
fun atp_term_of_metis type_enc (Metis_Term.Fn (s, tms)) =
70505
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
    46
      let val (s, swap) = atp_name_of_metis type_enc (Metis_Name.toString s)
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
    47
      in ATerm ((s, []), tms |> map (atp_term_of_metis type_enc) |> swap ? rev) end
52031
9a9238342963 tuning -- renamed '_from_' to '_of_' in Sledgehammer
blanchet
parents: 51998
diff changeset
    48
  | atp_term_of_metis _ (Metis_Term.Var s) =
70505
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
    49
      ATerm ((Metis_Name.toString s, []), [])
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
    50
52031
9a9238342963 tuning -- renamed '_from_' to '_of_' in Sledgehammer
blanchet
parents: 51998
diff changeset
    51
fun hol_term_of_metis ctxt type_enc sym_tab =
57255
488046fdda59 add support for Isar reconstruction for thf1 ATP provers like Leo-II.
fleury
parents: 55523
diff changeset
    52
  atp_term_of_metis type_enc #> term_of_atp ctxt ATP_Problem.CNF type_enc false sym_tab NONE
43094
269300fb83d0 more work on new Metis
blanchet
parents: 43093
diff changeset
    53
52031
9a9238342963 tuning -- renamed '_from_' to '_of_' in Sledgehammer
blanchet
parents: 51998
diff changeset
    54
fun atp_literal_of_metis type_enc (pos, atom) =
9a9238342963 tuning -- renamed '_from_' to '_of_' in Sledgehammer
blanchet
parents: 51998
diff changeset
    55
  atom |> Metis_Term.Fn |> atp_term_of_metis type_enc
44492
a330c0608da8 avoid using ":" for anything but systematic type tag annotations, because Hurd's Metis gives it that special semantics
blanchet
parents: 44241
diff changeset
    56
       |> AAtom |> not pos ? mk_anot
57408
39b3562e9edc tuned whitespace and parentheses
blanchet
parents: 57400
diff changeset
    57
52031
9a9238342963 tuning -- renamed '_from_' to '_of_' in Sledgehammer
blanchet
parents: 51998
diff changeset
    58
fun atp_clause_of_metis _ [] = AAtom (ATerm ((tptp_false, []), []))
9a9238342963 tuning -- renamed '_from_' to '_of_' in Sledgehammer
blanchet
parents: 51998
diff changeset
    59
  | atp_clause_of_metis type_enc lits =
9a9238342963 tuning -- renamed '_from_' to '_of_' in Sledgehammer
blanchet
parents: 51998
diff changeset
    60
    lits |> map (atp_literal_of_metis type_enc) |> mk_aconns AOr
43136
cf5cda219058 handle lightweight tags sym theorems gracefully in the presence of TVars with interesting type classes
blanchet
parents: 43135
diff changeset
    61
45508
b216dc1b3630 started implementing lambda-lifting in Metis
blanchet
parents: 44492
diff changeset
    62
fun polish_hol_terms ctxt (lifted, old_skolems) =
80666
cdae621613da more robust: only type inference with its finish/fixate phase (on contrast to dc387e3999ec), e.g. avoid accidental "improvement" of type class operations (free vs. const);
wenzelm
parents: 74904
diff changeset
    63
  map (reveal_lam_lifted lifted #> reveal_old_skolem_terms old_skolems) #>
cdae621613da more robust: only type inference with its finish/fixate phase (on contrast to dc387e3999ec), e.g. avoid accidental "improvement" of type class operations (free vs. const);
wenzelm
parents: 74904
diff changeset
    64
  Type_Infer_Context.infer_types_finished (Proof_Context.set_mode Proof_Context.mode_pattern ctxt)
43184
b16693484c5d reveal Skolems in new Metis
blanchet
parents: 43177
diff changeset
    65
52031
9a9238342963 tuning -- renamed '_from_' to '_of_' in Sledgehammer
blanchet
parents: 51998
diff changeset
    66
fun hol_clause_of_metis ctxt type_enc sym_tab concealed =
43159
29b55f292e0b added support for helpers in new Metis, so far only for polymorphic type encodings
blanchet
parents: 43136
diff changeset
    67
  Metis_Thm.clause
29b55f292e0b added support for helpers in new Metis, so far only for polymorphic type encodings
blanchet
parents: 43136
diff changeset
    68
  #> Metis_LiteralSet.toList
52031
9a9238342963 tuning -- renamed '_from_' to '_of_' in Sledgehammer
blanchet
parents: 51998
diff changeset
    69
  #> atp_clause_of_metis type_enc
57255
488046fdda59 add support for Isar reconstruction for thf1 ATP provers like Leo-II.
fleury
parents: 55523
diff changeset
    70
  #> prop_of_atp ctxt ATP_Problem.CNF type_enc false sym_tab
45508
b216dc1b3630 started implementing lambda-lifting in Metis
blanchet
parents: 44492
diff changeset
    71
  #> singleton (polish_hol_terms ctxt concealed)
43136
cf5cda219058 handle lightweight tags sym theorems gracefully in the presence of TVars with interesting type classes
blanchet
parents: 43135
diff changeset
    72
52031
9a9238342963 tuning -- renamed '_from_' to '_of_' in Sledgehammer
blanchet
parents: 51998
diff changeset
    73
fun hol_terms_of_metis ctxt type_enc concealed sym_tab fol_tms =
57408
39b3562e9edc tuned whitespace and parentheses
blanchet
parents: 57400
diff changeset
    74
  let
39b3562e9edc tuned whitespace and parentheses
blanchet
parents: 57400
diff changeset
    75
    val ts = map (hol_term_of_metis ctxt type_enc sym_tab) fol_tms
39b3562e9edc tuned whitespace and parentheses
blanchet
parents: 57400
diff changeset
    76
    val _ = trace_msg ctxt (fn () => "  calling type inference:")
67379
c2dfc510a38c prefer qualified names;
wenzelm
parents: 67091
diff changeset
    77
    val _ = List.app (fn t => trace_msg ctxt (fn () => Syntax.string_of_term ctxt t)) ts
57408
39b3562e9edc tuned whitespace and parentheses
blanchet
parents: 57400
diff changeset
    78
    val ts' = ts |> polish_hol_terms ctxt concealed
67379
c2dfc510a38c prefer qualified names;
wenzelm
parents: 67091
diff changeset
    79
    val _ = List.app (fn t => trace_msg ctxt
57408
39b3562e9edc tuned whitespace and parentheses
blanchet
parents: 57400
diff changeset
    80
                  (fn () => "  final term: " ^ Syntax.string_of_term ctxt t ^
39b3562e9edc tuned whitespace and parentheses
blanchet
parents: 57400
diff changeset
    81
                            " of type " ^ Syntax.string_of_typ ctxt (type_of t))) ts'
39b3562e9edc tuned whitespace and parentheses
blanchet
parents: 57400
diff changeset
    82
  in ts' end
81254
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents: 80910
diff changeset
    83
  handle ERROR msg => raise METIS_RECONSTRUCT ("hol_terms_of_metis", msg)
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
    84
70505
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
    85
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
    86
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
    87
(** FOL step Inference Rules **)
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
    88
70506
wenzelm
parents: 70505
diff changeset
    89
fun lookth th_pairs fol_th =
wenzelm
parents: 70505
diff changeset
    90
  (case AList.lookup (uncurry Metis_Thm.equal) th_pairs fol_th of
wenzelm
parents: 70505
diff changeset
    91
    SOME th => th
wenzelm
parents: 70505
diff changeset
    92
  | NONE => raise Fail ("Failed to find Metis theorem " ^ Metis_Thm.toString fol_th))
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
    93
59632
5980e75a204e clarified context;
wenzelm
parents: 59621
diff changeset
    94
fun cterm_incr_types ctxt idx =
5980e75a204e clarified context;
wenzelm
parents: 59621
diff changeset
    95
  Thm.cterm_of ctxt o map_types (Logic.incr_tvar idx)
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
    96
70505
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
    97
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
    98
(* INFERENCE RULE: AXIOM *)
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
    99
70505
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
   100
(*This causes variables to have an index of 1 by default. See also
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
   101
  "term_of_atp" in "ATP_Proof_Reconstruct".*)
43212
050a03afe024 Metis code cleanup
blanchet
parents: 43209
diff changeset
   102
val axiom_inference = Thm.incr_indexes 1 oo lookth
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   103
70505
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
   104
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   105
(* INFERENCE RULE: ASSUME *)
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   106
74611
98e7930e6d15 clarified antiquotations, assuming that Drule.instantiate_normalize was merely a historical relic;
wenzelm
parents: 74347
diff changeset
   107
fun excluded_middle P =
98e7930e6d15 clarified antiquotations, assuming that Drule.instantiate_normalize was merely a historical relic;
wenzelm
parents: 74347
diff changeset
   108
  \<^instantiate>\<open>P in lemma (open) \<open>P \<Longrightarrow> \<not> P \<Longrightarrow> False\<close> by (rule notE)\<close>
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   109
45508
b216dc1b3630 started implementing lambda-lifting in Metis
blanchet
parents: 44492
diff changeset
   110
fun assume_inference ctxt type_enc concealed sym_tab atom =
70508
wenzelm
parents: 70506
diff changeset
   111
  singleton (hol_terms_of_metis ctxt type_enc concealed sym_tab) (Metis_Term.Fn atom)
74611
98e7930e6d15 clarified antiquotations, assuming that Drule.instantiate_normalize was merely a historical relic;
wenzelm
parents: 74347
diff changeset
   112
  |> Thm.cterm_of ctxt |> excluded_middle
70508
wenzelm
parents: 70506
diff changeset
   113
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   114
70508
wenzelm
parents: 70506
diff changeset
   115
(* INFERENCE RULE: INSTANTIATE (SUBST). *)
wenzelm
parents: 70506
diff changeset
   116
wenzelm
parents: 70506
diff changeset
   117
(*Type instantiations are ignored. Trying to reconstruct them admits new
wenzelm
parents: 70506
diff changeset
   118
  possibilities of errors, e.g. concerning sorts. Instead we try to arrange
wenzelm
parents: 70506
diff changeset
   119
  hat new TVars are distinct and that types can be inferred from terms.*)
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   120
45508
b216dc1b3630 started implementing lambda-lifting in Metis
blanchet
parents: 44492
diff changeset
   121
fun inst_inference ctxt type_enc concealed sym_tab th_pairs fsubst th =
57408
39b3562e9edc tuned whitespace and parentheses
blanchet
parents: 57400
diff changeset
   122
  let
39b3562e9edc tuned whitespace and parentheses
blanchet
parents: 57400
diff changeset
   123
    val i_th = lookth th_pairs th
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59498
diff changeset
   124
    val i_th_vars = Term.add_vars (Thm.prop_of i_th) []
57408
39b3562e9edc tuned whitespace and parentheses
blanchet
parents: 57400
diff changeset
   125
39b3562e9edc tuned whitespace and parentheses
blanchet
parents: 57400
diff changeset
   126
    fun find_var x = the (List.find (fn ((a,_),_) => a=x) i_th_vars)
39b3562e9edc tuned whitespace and parentheses
blanchet
parents: 57400
diff changeset
   127
    fun subst_translation (x,y) =
39b3562e9edc tuned whitespace and parentheses
blanchet
parents: 57400
diff changeset
   128
      let
39b3562e9edc tuned whitespace and parentheses
blanchet
parents: 57400
diff changeset
   129
        val v = find_var x
70505
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
   130
        (*We call "polish_hol_terms" below.*)
57408
39b3562e9edc tuned whitespace and parentheses
blanchet
parents: 57400
diff changeset
   131
        val t = hol_term_of_metis ctxt type_enc sym_tab y
39b3562e9edc tuned whitespace and parentheses
blanchet
parents: 57400
diff changeset
   132
      in
59632
5980e75a204e clarified context;
wenzelm
parents: 59621
diff changeset
   133
        SOME (Thm.cterm_of ctxt (Var v), t)
57408
39b3562e9edc tuned whitespace and parentheses
blanchet
parents: 57400
diff changeset
   134
      end
39b3562e9edc tuned whitespace and parentheses
blanchet
parents: 57400
diff changeset
   135
      handle Option.Option =>
39b3562e9edc tuned whitespace and parentheses
blanchet
parents: 57400
diff changeset
   136
             (trace_msg ctxt (fn () =>
61268
abe08fb15a12 moved remaining display.ML to more_thm.ML;
wenzelm
parents: 60949
diff changeset
   137
                "\"find_var\" failed for " ^ x ^ " in " ^ Thm.string_of_thm ctxt i_th);
57408
39b3562e9edc tuned whitespace and parentheses
blanchet
parents: 57400
diff changeset
   138
              NONE)
39b3562e9edc tuned whitespace and parentheses
blanchet
parents: 57400
diff changeset
   139
           | TYPE _ =>
39b3562e9edc tuned whitespace and parentheses
blanchet
parents: 57400
diff changeset
   140
             (trace_msg ctxt (fn () =>
61268
abe08fb15a12 moved remaining display.ML to more_thm.ML;
wenzelm
parents: 60949
diff changeset
   141
                "\"hol_term_of_metis\" failed for " ^ x ^ " in " ^ Thm.string_of_thm ctxt i_th);
57408
39b3562e9edc tuned whitespace and parentheses
blanchet
parents: 57400
diff changeset
   142
              NONE)
39b3562e9edc tuned whitespace and parentheses
blanchet
parents: 57400
diff changeset
   143
    fun remove_typeinst (a, t) =
39b3562e9edc tuned whitespace and parentheses
blanchet
parents: 57400
diff changeset
   144
      let val a = Metis_Name.toString a in
39b3562e9edc tuned whitespace and parentheses
blanchet
parents: 57400
diff changeset
   145
        (case unprefix_and_unascii schematic_var_prefix a of
39b3562e9edc tuned whitespace and parentheses
blanchet
parents: 57400
diff changeset
   146
          SOME b => SOME (b, t)
39b3562e9edc tuned whitespace and parentheses
blanchet
parents: 57400
diff changeset
   147
        | NONE =>
39b3562e9edc tuned whitespace and parentheses
blanchet
parents: 57400
diff changeset
   148
          (case unprefix_and_unascii tvar_prefix a of
70505
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
   149
            SOME _ => NONE (*type instantiations are forbidden*)
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
   150
          | NONE => SOME (a, t) (*internal Metis var?*)))
57408
39b3562e9edc tuned whitespace and parentheses
blanchet
parents: 57400
diff changeset
   151
      end
61268
abe08fb15a12 moved remaining display.ML to more_thm.ML;
wenzelm
parents: 60949
diff changeset
   152
    val _ = trace_msg ctxt (fn () => "  isa th: " ^ Thm.string_of_thm ctxt i_th)
57408
39b3562e9edc tuned whitespace and parentheses
blanchet
parents: 57400
diff changeset
   153
    val substs = map_filter remove_typeinst (Metis_Subst.toList fsubst)
39b3562e9edc tuned whitespace and parentheses
blanchet
parents: 57400
diff changeset
   154
    val (vars, tms) =
39b3562e9edc tuned whitespace and parentheses
blanchet
parents: 57400
diff changeset
   155
      ListPair.unzip (map_filter subst_translation substs)
39b3562e9edc tuned whitespace and parentheses
blanchet
parents: 57400
diff changeset
   156
      ||> polish_hol_terms ctxt concealed
70515
wenzelm
parents: 70512
diff changeset
   157
    val ctm_of = cterm_incr_types ctxt (Thm.maxidx_of i_th + 1)
57408
39b3562e9edc tuned whitespace and parentheses
blanchet
parents: 57400
diff changeset
   158
    val substs' = ListPair.zip (vars, map ctm_of tms)
39b3562e9edc tuned whitespace and parentheses
blanchet
parents: 57400
diff changeset
   159
    val _ = trace_msg ctxt (fn () =>
39b3562e9edc tuned whitespace and parentheses
blanchet
parents: 57400
diff changeset
   160
      cat_lines ("subst_translations:" ::
39b3562e9edc tuned whitespace and parentheses
blanchet
parents: 57400
diff changeset
   161
        (substs' |> map (fn (x, y) =>
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59498
diff changeset
   162
          Syntax.string_of_term ctxt (Thm.term_of x) ^ " |-> " ^
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59498
diff changeset
   163
          Syntax.string_of_term ctxt (Thm.term_of y)))))
57408
39b3562e9edc tuned whitespace and parentheses
blanchet
parents: 57400
diff changeset
   164
  in
60795
c24fa03f4c71 tuned signature;
wenzelm
parents: 60794
diff changeset
   165
    infer_instantiate_types ctxt (map (apfst (dest_Var o Thm.term_of)) substs') i_th
57408
39b3562e9edc tuned whitespace and parentheses
blanchet
parents: 57400
diff changeset
   166
  end
50875
bfb626265782 less brutal Metis failure -- the brutality was accidentally introduced by df8ae0590be2
blanchet
parents: 48132
diff changeset
   167
  handle THM (msg, _, _) => raise METIS_RECONSTRUCT ("inst_inference", msg)
bfb626265782 less brutal Metis failure -- the brutality was accidentally introduced by df8ae0590be2
blanchet
parents: 48132
diff changeset
   168
       | ERROR msg => raise METIS_RECONSTRUCT ("inst_inference", msg)
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   169
70505
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
   170
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   171
(* INFERENCE RULE: RESOLVE *)
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   172
43330
c6bbeca3ee06 clarified special incr_type_indexes;
wenzelm
parents: 43301
diff changeset
   173
(*Increment the indexes of only the type variables*)
60363
5568b16aa477 clarified context;
wenzelm
parents: 59632
diff changeset
   174
fun incr_type_indexes ctxt inc th =
57408
39b3562e9edc tuned whitespace and parentheses
blanchet
parents: 57400
diff changeset
   175
  let
39b3562e9edc tuned whitespace and parentheses
blanchet
parents: 57400
diff changeset
   176
    val tvs = Term.add_tvars (Thm.full_prop_of th) []
60642
48dd1cefb4ae simplified Thm.instantiate and derivatives: the LHS refers to non-certified variables -- this merely serves as index into already certified structures (or is ignored);
wenzelm
parents: 60363
diff changeset
   177
    fun inc_tvar ((a, i), s) = (((a, i), s), Thm.ctyp_of ctxt (TVar ((a, i + inc), s)))
57408
39b3562e9edc tuned whitespace and parentheses
blanchet
parents: 57400
diff changeset
   178
  in
74282
c2ee8d993d6a clarified signature: more scalable operations;
wenzelm
parents: 70518
diff changeset
   179
    Thm.instantiate (TVars.make (map inc_tvar tvs), Vars.empty) th
57408
39b3562e9edc tuned whitespace and parentheses
blanchet
parents: 57400
diff changeset
   180
  end
43330
c6bbeca3ee06 clarified special incr_type_indexes;
wenzelm
parents: 43301
diff changeset
   181
70505
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
   182
(*Like RSN, but we rename apart only the type variables. Vars here typically
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
   183
  have an index of 1, and the use of RSN would increase this typically to 3.
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
   184
  Instantiations of those Vars could then fail.*)
70516
60005f96d232 tuned -- avoid shadowing of ML names;
wenzelm
parents: 70515
diff changeset
   185
fun resolve_inc_tyvars ctxt th1 i th2 =
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   186
  let
70516
60005f96d232 tuned -- avoid shadowing of ML names;
wenzelm
parents: 70515
diff changeset
   187
    val th1' = incr_type_indexes ctxt (Thm.maxidx_of th2 + 1) th1
57400
13b06c626163 resolution modulo double negation
blanchet
parents: 57255
diff changeset
   188
    fun res (tha, thb) =
58950
d07464875dd4 optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents: 58839
diff changeset
   189
      (case Thm.bicompose (SOME ctxt) {flatten = true, match = false, incremented = true}
70518
bf5724694ce5 more compact proof terms;
wenzelm
parents: 70516
diff changeset
   190
            (false, Thm.close_derivation \<^here> tha, Thm.nprems_of tha) i thb
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   191
           |> Seq.list_of |> distinct Thm.eq_thm of
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   192
        [th] => th
57400
13b06c626163 resolution modulo double negation
blanchet
parents: 57255
diff changeset
   193
      | _ =>
13b06c626163 resolution modulo double negation
blanchet
parents: 57255
diff changeset
   194
        let
13b06c626163 resolution modulo double negation
blanchet
parents: 57255
diff changeset
   195
          val thaa'bb' as [(tha', _), (thb', _)] =
63170
eae6549dbea2 tuned proofs, to allow unfold_abs_def;
wenzelm
parents: 61268
diff changeset
   196
            map (`(Local_Defs.unfold0 ctxt meta_not_not)) [tha, thb]
57400
13b06c626163 resolution modulo double negation
blanchet
parents: 57255
diff changeset
   197
        in
13b06c626163 resolution modulo double negation
blanchet
parents: 57255
diff changeset
   198
          if forall Thm.eq_thm_prop thaa'bb' then
13b06c626163 resolution modulo double negation
blanchet
parents: 57255
diff changeset
   199
            raise THM ("resolve_inc_tyvars: unique result expected", i, [tha, thb])
13b06c626163 resolution modulo double negation
blanchet
parents: 57255
diff changeset
   200
          else
13b06c626163 resolution modulo double negation
blanchet
parents: 57255
diff changeset
   201
            res (tha', thb')
57408
39b3562e9edc tuned whitespace and parentheses
blanchet
parents: 57400
diff changeset
   202
        end)
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   203
  in
70516
60005f96d232 tuned -- avoid shadowing of ML names;
wenzelm
parents: 70515
diff changeset
   204
    res (th1', th2)
52225
568b2cd65d50 resolve_inc_tyvars: back to old behavior before 0fa3b456a267 where types of equal Vars are *not* unified -- recover last example in src/HOL/Metis_Examples/Clausification.thy;
wenzelm
parents: 52223
diff changeset
   205
    handle TERM z =>
57400
13b06c626163 resolution modulo double negation
blanchet
parents: 57255
diff changeset
   206
      let
81254
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents: 80910
diff changeset
   207
        val tyenv = []
70516
60005f96d232 tuned -- avoid shadowing of ML names;
wenzelm
parents: 70515
diff changeset
   208
          |> fold (Term.add_vars o Thm.prop_of) [th1', th2]
57400
13b06c626163 resolution modulo double negation
blanchet
parents: 57255
diff changeset
   209
          |> AList.group (op =)
13b06c626163 resolution modulo double negation
blanchet
parents: 57255
diff changeset
   210
          |> maps (fn ((s, _), T :: Ts) => map (fn T' => (Free (s, T), Free (s, T'))) Ts)
63615
wenzelm
parents: 63170
diff changeset
   211
          |> rpair Envir.init
58950
d07464875dd4 optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents: 58839
diff changeset
   212
          |-> fold (Pattern.unify (Context.Proof ctxt))
81254
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents: 80910
diff changeset
   213
          |> Envir.type_env
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents: 80910
diff changeset
   214
        val instT =
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents: 80910
diff changeset
   215
          TVars.build (tyenv |> Vartab.fold (fn (x, (S, T)) =>
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents: 80910
diff changeset
   216
            TVars.add ((x, S), Thm.ctyp_of ctxt (Envir.norm_type tyenv T))))
57400
13b06c626163 resolution modulo double negation
blanchet
parents: 57255
diff changeset
   217
      in
70505
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
   218
        (*The unifier, which is invoked from "Thm.bicompose", will sometimes refuse to unify
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
   219
          "?a::?'a" with "?a::?'b" or "?a::nat" and throw a "TERM" exception (with "add_ffpair" as
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
   220
          first argument). We then perform unification of the types of variables by hand and try
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
   221
          again. We could do this the first time around but this error occurs seldom and we don't
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
   222
          want to break existing proofs in subtle ways or slow them down.*)
81254
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents: 80910
diff changeset
   223
        if TVars.is_empty instT then raise TERM z
d3c0734059ee variable instantiation in Sledgehammer and Metis
blanchet
parents: 80910
diff changeset
   224
        else res (apply2 (Drule.instantiate_normalize (instT, Vars.empty)) (th1', th2))
57400
13b06c626163 resolution modulo double negation
blanchet
parents: 57255
diff changeset
   225
      end
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   226
  end
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   227
74347
f984d30cd0c3 clarified antiquotations;
wenzelm
parents: 74290
diff changeset
   228
fun s_not \<^Const_>\<open>Not for t\<close> = t
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
   229
  | s_not t = HOLogic.mk_not t
74347
f984d30cd0c3 clarified antiquotations;
wenzelm
parents: 74290
diff changeset
   230
fun simp_not_not \<^Const_>\<open>Trueprop for t\<close> = \<^Const>\<open>Trueprop for \<open>simp_not_not t\<close>\<close>
f984d30cd0c3 clarified antiquotations;
wenzelm
parents: 74290
diff changeset
   231
  | simp_not_not \<^Const_>\<open>Not for t\<close> = s_not (simp_not_not t)
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
   232
  | simp_not_not t = t
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   233
43195
6dc58b3b73b5 improved correctness of handling of higher-order occurrences of "Not" in new Metis (and probably in old Metis)
blanchet
parents: 43187
diff changeset
   234
val normalize_literal = simp_not_not o Envir.eta_contract
6dc58b3b73b5 improved correctness of handling of higher-order occurrences of "Not" in new Metis (and probably in old Metis)
blanchet
parents: 43187
diff changeset
   235
70505
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
   236
(*Find the relative location of an untyped term within a list of terms as a
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
   237
  1-based index. Returns 0 in case of failure.*)
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
   238
fun index_of_literal lit haystack =
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   239
  let
43195
6dc58b3b73b5 improved correctness of handling of higher-order occurrences of "Not" in new Metis (and probably in old Metis)
blanchet
parents: 43187
diff changeset
   240
    fun match_lit normalize =
43134
0c82e00ba63e make sure no warnings are given for polymorphic facts where we use a monomorphic instance
blanchet
parents: 43130
diff changeset
   241
      HOLogic.dest_Trueprop #> normalize
43301
8d7fc4a5b502 removed needless function that duplicated standard functionality, with a little unnecessary twist
blanchet
parents: 43300
diff changeset
   242
      #> curry Term.aconv_untyped (lit |> normalize)
43195
6dc58b3b73b5 improved correctness of handling of higher-order occurrences of "Not" in new Metis (and probably in old Metis)
blanchet
parents: 43187
diff changeset
   243
  in
6dc58b3b73b5 improved correctness of handling of higher-order occurrences of "Not" in new Metis (and probably in old Metis)
blanchet
parents: 43187
diff changeset
   244
    (case find_index (match_lit I) haystack of
6dc58b3b73b5 improved correctness of handling of higher-order occurrences of "Not" in new Metis (and probably in old Metis)
blanchet
parents: 43187
diff changeset
   245
       ~1 => find_index (match_lit (simp_not_not o Envir.eta_contract)) haystack
6dc58b3b73b5 improved correctness of handling of higher-order occurrences of "Not" in new Metis (and probably in old Metis)
blanchet
parents: 43187
diff changeset
   246
     | j => j) + 1
6dc58b3b73b5 improved correctness of handling of higher-order occurrences of "Not" in new Metis (and probably in old Metis)
blanchet
parents: 43187
diff changeset
   247
  end
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   248
70505
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
   249
(*Permute a rule's premises to move the i-th premise to the last position.*)
39893
25a339e1ff9b move functions closer to where they're used
blanchet
parents: 39887
diff changeset
   250
fun make_last i th =
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59498
diff changeset
   251
  let val n = Thm.nprems_of th in
54756
blanchet
parents: 54742
diff changeset
   252
    if i >= 1 andalso i <= n then Thm.permute_prems (i - 1) 1 th
blanchet
parents: 54742
diff changeset
   253
    else raise THM ("select_literal", i, [th])
57408
39b3562e9edc tuned whitespace and parentheses
blanchet
parents: 57400
diff changeset
   254
  end
39893
25a339e1ff9b move functions closer to where they're used
blanchet
parents: 39887
diff changeset
   255
70505
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
   256
(*Maps a rule that ends "... ==> P ==> False" to "... ==> ~ P" while avoiding
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
   257
  to create double negations. The "select" wrapper is a trick to ensure that
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
   258
  "P ==> ~ False ==> False" is rewritten to "P ==> False", not to "~ P". We
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
   259
  don't use this trick in general because it makes the proof object uglier than
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
   260
  necessary. FIXME.*)
54742
7a86358a3c0b proper context for basic Simplifier operations: rewrite_rule, rewrite_goals_rule, rewrite_goals_tac etc.;
wenzelm
parents: 54501
diff changeset
   261
fun negate_head ctxt th =
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 67725
diff changeset
   262
  if exists (fn t => t aconv \<^prop>\<open>\<not> False\<close>) (Thm.prems_of th) then
42349
721e85fd2db3 make 48170228f562 work also with "HO_Reas" examples
blanchet
parents: 42348
diff changeset
   263
    (th RS @{thm select_FalseI})
54756
blanchet
parents: 54742
diff changeset
   264
    |> fold (rewrite_rule ctxt o single) @{thms not_atomize_select atomize_not_select}
42349
721e85fd2db3 make 48170228f562 work also with "HO_Reas" examples
blanchet
parents: 42348
diff changeset
   265
  else
54742
7a86358a3c0b proper context for basic Simplifier operations: rewrite_rule, rewrite_goals_rule, rewrite_goals_tac etc.;
wenzelm
parents: 54501
diff changeset
   266
    th |> fold (rewrite_rule ctxt o single) @{thms not_atomize atomize_not}
39893
25a339e1ff9b move functions closer to where they're used
blanchet
parents: 39887
diff changeset
   267
25a339e1ff9b move functions closer to where they're used
blanchet
parents: 39887
diff changeset
   268
(* Maps the clause  [P1,...Pn]==>False to [P1,...,P(i-1),P(i+1),...Pn] ==> ~P *)
54742
7a86358a3c0b proper context for basic Simplifier operations: rewrite_rule, rewrite_goals_rule, rewrite_goals_tac etc.;
wenzelm
parents: 54501
diff changeset
   269
fun select_literal ctxt = negate_head ctxt oo make_last
39893
25a339e1ff9b move functions closer to where they're used
blanchet
parents: 39887
diff changeset
   270
45508
b216dc1b3630 started implementing lambda-lifting in Metis
blanchet
parents: 44492
diff changeset
   271
fun resolve_inference ctxt type_enc concealed sym_tab th_pairs atom th1 th2 =
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   272
  let
59058
a78612c67ec0 renamed "pairself" to "apply2", in accordance to @{apply 2};
wenzelm
parents: 58963
diff changeset
   273
    val (i_th1, i_th2) = apply2 (lookth th_pairs) (th1, th2)
43187
95bd1ef1331a make resolution replay more robust, in case Metis distinguishes between two literals that are merged in Isabelle (e.g. because they carry more or less type annotations in Metis)
blanchet
parents: 43186
diff changeset
   274
    val _ = trace_msg ctxt (fn () =>
61268
abe08fb15a12 moved remaining display.ML to more_thm.ML;
wenzelm
parents: 60949
diff changeset
   275
        "  isa th1 (pos): " ^ Thm.string_of_thm ctxt i_th1 ^ "\n\
abe08fb15a12 moved remaining display.ML to more_thm.ML;
wenzelm
parents: 60949
diff changeset
   276
        \  isa th2 (neg): " ^ Thm.string_of_thm ctxt i_th2)
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   277
  in
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   278
    (* Trivial cases where one operand is type info *)
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   279
    if Thm.eq_thm (TrueI, i_th1) then
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   280
      i_th2
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   281
    else if Thm.eq_thm (TrueI, i_th2) then
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   282
      i_th1
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   283
    else
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   284
      let
43187
95bd1ef1331a make resolution replay more robust, in case Metis distinguishes between two literals that are merged in Isabelle (e.g. because they carry more or less type annotations in Metis)
blanchet
parents: 43186
diff changeset
   285
        val i_atom =
54756
blanchet
parents: 54742
diff changeset
   286
          singleton (hol_terms_of_metis ctxt type_enc concealed sym_tab) (Metis_Term.Fn atom)
blanchet
parents: 54742
diff changeset
   287
        val _ = trace_msg ctxt (fn () => "  atom: " ^ Syntax.string_of_term ctxt i_atom)
43187
95bd1ef1331a make resolution replay more robust, in case Metis distinguishes between two literals that are merged in Isabelle (e.g. because they carry more or less type annotations in Metis)
blanchet
parents: 43186
diff changeset
   288
      in
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59498
diff changeset
   289
        (case index_of_literal (s_not i_atom) (Thm.prems_of i_th1) of
54756
blanchet
parents: 54742
diff changeset
   290
          0 => (trace_msg ctxt (fn () => "Failed to find literal in \"th1\""); i_th1)
43187
95bd1ef1331a make resolution replay more robust, in case Metis distinguishes between two literals that are merged in Isabelle (e.g. because they carry more or less type annotations in Metis)
blanchet
parents: 43186
diff changeset
   291
        | j1 =>
95bd1ef1331a make resolution replay more robust, in case Metis distinguishes between two literals that are merged in Isabelle (e.g. because they carry more or less type annotations in Metis)
blanchet
parents: 43186
diff changeset
   292
          (trace_msg ctxt (fn () => "  index th1: " ^ string_of_int j1);
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59498
diff changeset
   293
           (case index_of_literal i_atom (Thm.prems_of i_th2) of
54756
blanchet
parents: 54742
diff changeset
   294
             0 => (trace_msg ctxt (fn () => "Failed to find literal in \"th2\""); i_th2)
43187
95bd1ef1331a make resolution replay more robust, in case Metis distinguishes between two literals that are merged in Isabelle (e.g. because they carry more or less type annotations in Metis)
blanchet
parents: 43186
diff changeset
   295
           | j2 =>
95bd1ef1331a make resolution replay more robust, in case Metis distinguishes between two literals that are merged in Isabelle (e.g. because they carry more or less type annotations in Metis)
blanchet
parents: 43186
diff changeset
   296
             (trace_msg ctxt (fn () => "  index th2: " ^ string_of_int j2);
57400
13b06c626163 resolution modulo double negation
blanchet
parents: 57255
diff changeset
   297
              resolve_inc_tyvars ctxt (select_literal ctxt j1 i_th1) j2 i_th2
57408
39b3562e9edc tuned whitespace and parentheses
blanchet
parents: 57400
diff changeset
   298
              handle TERM (s, _) => raise METIS_RECONSTRUCT ("resolve_inference", s)))))
43187
95bd1ef1331a make resolution replay more robust, in case Metis distinguishes between two literals that are merged in Isabelle (e.g. because they carry more or less type annotations in Metis)
blanchet
parents: 43186
diff changeset
   299
      end
95bd1ef1331a make resolution replay more robust, in case Metis distinguishes between two literals that are merged in Isabelle (e.g. because they carry more or less type annotations in Metis)
blanchet
parents: 43186
diff changeset
   300
  end
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   301
70505
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
   302
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   303
(* INFERENCE RULE: REFL *)
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   304
70508
wenzelm
parents: 70506
diff changeset
   305
val REFL_THM = Thm.incr_indexes 2 @{lemma "x \<noteq> x \<Longrightarrow> False" by (drule notE) (rule refl)}
70487
wenzelm
parents: 70485
diff changeset
   306
val [refl_x] = Term.add_vars (Thm.prop_of REFL_THM) [];
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   307
45508
b216dc1b3630 started implementing lambda-lifting in Metis
blanchet
parents: 44492
diff changeset
   308
fun refl_inference ctxt type_enc concealed sym_tab t =
43094
269300fb83d0 more work on new Metis
blanchet
parents: 43093
diff changeset
   309
  let
54756
blanchet
parents: 54742
diff changeset
   310
    val i_t = singleton (hol_terms_of_metis ctxt type_enc concealed sym_tab) t
43094
269300fb83d0 more work on new Metis
blanchet
parents: 43093
diff changeset
   311
    val _ = trace_msg ctxt (fn () => "  term: " ^ Syntax.string_of_term ctxt i_t)
70515
wenzelm
parents: 70512
diff changeset
   312
    val c_t = cterm_incr_types ctxt (Thm.maxidx_of REFL_THM + 1) i_t
70487
wenzelm
parents: 70485
diff changeset
   313
  in infer_instantiate_types ctxt [(refl_x, c_t)] REFL_THM end
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   314
70505
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
   315
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   316
(* INFERENCE RULE: EQUALITY *)
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   317
70485
b203aaf373cf more compact proofterms;
wenzelm
parents: 69597
diff changeset
   318
val subst_em = @{lemma "s = t \<Longrightarrow> P s \<Longrightarrow> \<not> P t \<Longrightarrow> False" by (erule notE) (erule subst)}
b203aaf373cf more compact proofterms;
wenzelm
parents: 69597
diff changeset
   319
val ssubst_em = @{lemma "s = t \<Longrightarrow> P t \<Longrightarrow> \<not> P s \<Longrightarrow> False" by (erule notE) (erule ssubst)}
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   320
45508
b216dc1b3630 started implementing lambda-lifting in Metis
blanchet
parents: 44492
diff changeset
   321
fun equality_inference ctxt type_enc concealed sym_tab (pos, atom) fp fr =
70505
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
   322
  let
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
   323
    val m_tm = Metis_Term.Fn atom
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
   324
    val [i_atom, i_tm] = hol_terms_of_metis ctxt type_enc concealed sym_tab [m_tm, fr]
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
   325
    val _ = trace_msg ctxt (fn () => "sign of the literal: " ^ Bool.toString pos)
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
   326
    fun replace_item_list lx 0 (_::ls) = lx::ls
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
   327
      | replace_item_list lx i (l::ls) = l :: replace_item_list lx (i-1) ls
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
   328
    fun path_finder_fail tm ps t =
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
   329
      raise METIS_RECONSTRUCT ("equality_inference (path_finder)",
80910
406a85a25189 clarified signature: more explicit operations;
wenzelm
parents: 80666
diff changeset
   330
                "path = " ^ implode_space (map string_of_int ps) ^
70505
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
   331
                " isa-term: " ^ Syntax.string_of_term ctxt tm ^
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
   332
                (case t of
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
   333
                  SOME t => " fol-term: " ^ Metis_Term.toString t
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
   334
                | NONE => ""))
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
   335
    fun path_finder tm [] _ = (tm, Bound 0)
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
   336
      | path_finder tm (p :: ps) (t as Metis_Term.Fn (s, ts)) =
43177
5017d436a572 properly unmangle names in path finder
blanchet
parents: 43174
diff changeset
   337
          let
43268
c0eaa8b9bff5 removed yet another hack in "make_metis" script -- respect opacity of "Metis_Name.name"
blanchet
parents: 43262
diff changeset
   338
            val s = s |> Metis_Name.toString
45511
9b0f8ca4388e continued implementation of lambda-lifting in Metis
blanchet
parents: 45508
diff changeset
   339
                      |> perhaps (try (unprefix_and_unascii const_prefix
46392
676a4b4b6e73 implemented partial application aliases (for SPASS mainly)
blanchet
parents: 46320
diff changeset
   340
                                       #> the #> unmangled_const_name #> hd))
43177
5017d436a572 properly unmangle names in path finder
blanchet
parents: 43174
diff changeset
   341
          in
5017d436a572 properly unmangle names in path finder
blanchet
parents: 43174
diff changeset
   342
            if s = metis_predicator orelse s = predicator_name orelse
44492
a330c0608da8 avoid using ":" for anything but systematic type tag annotations, because Hurd's Metis gives it that special semantics
blanchet
parents: 44241
diff changeset
   343
               s = metis_systematic_type_tag orelse s = metis_ad_hoc_type_tag
a330c0608da8 avoid using ":" for anything but systematic type tag annotations, because Hurd's Metis gives it that special semantics
blanchet
parents: 44241
diff changeset
   344
               orelse s = type_tag_name then
43212
050a03afe024 Metis code cleanup
blanchet
parents: 43209
diff changeset
   345
              path_finder tm ps (nth ts p)
43177
5017d436a572 properly unmangle names in path finder
blanchet
parents: 43174
diff changeset
   346
            else if s = metis_app_op orelse s = app_op_name then
43130
d73fc2e55308 implemented missing hAPP and ti cases of new path finder
blanchet
parents: 43128
diff changeset
   347
              let
d73fc2e55308 implemented missing hAPP and ti cases of new path finder
blanchet
parents: 43128
diff changeset
   348
                val (tm1, tm2) = dest_comb tm
d73fc2e55308 implemented missing hAPP and ti cases of new path finder
blanchet
parents: 43128
diff changeset
   349
                val p' = p - (length ts - 2)
d73fc2e55308 implemented missing hAPP and ti cases of new path finder
blanchet
parents: 43128
diff changeset
   350
              in
54756
blanchet
parents: 54742
diff changeset
   351
                if p' = 0 then path_finder tm1 ps (nth ts p) ||> (fn y => y $ tm2)
blanchet
parents: 54742
diff changeset
   352
                else path_finder tm2 ps (nth ts p) ||> (fn y => tm1 $ y)
43130
d73fc2e55308 implemented missing hAPP and ti cases of new path finder
blanchet
parents: 43128
diff changeset
   353
              end
d73fc2e55308 implemented missing hAPP and ti cases of new path finder
blanchet
parents: 43128
diff changeset
   354
            else
d73fc2e55308 implemented missing hAPP and ti cases of new path finder
blanchet
parents: 43128
diff changeset
   355
              let
d73fc2e55308 implemented missing hAPP and ti cases of new path finder
blanchet
parents: 43128
diff changeset
   356
                val (tm1, args) = strip_comb tm
d73fc2e55308 implemented missing hAPP and ti cases of new path finder
blanchet
parents: 43128
diff changeset
   357
                val adjustment = length ts - length args
d73fc2e55308 implemented missing hAPP and ti cases of new path finder
blanchet
parents: 43128
diff changeset
   358
                val p' = if adjustment > p then p else p - adjustment
54756
blanchet
parents: 54742
diff changeset
   359
                val tm_p = nth args p'
43278
1fbdcebb364b more robust exception pattern General.Subscript;
wenzelm
parents: 43268
diff changeset
   360
                  handle General.Subscript => path_finder_fail tm (p :: ps) (SOME t)
43130
d73fc2e55308 implemented missing hAPP and ti cases of new path finder
blanchet
parents: 43128
diff changeset
   361
                val _ = trace_msg ctxt (fn () =>
d73fc2e55308 implemented missing hAPP and ti cases of new path finder
blanchet
parents: 43128
diff changeset
   362
                    "path_finder: " ^ string_of_int p ^ "  " ^
d73fc2e55308 implemented missing hAPP and ti cases of new path finder
blanchet
parents: 43128
diff changeset
   363
                    Syntax.string_of_term ctxt tm_p)
43212
050a03afe024 Metis code cleanup
blanchet
parents: 43209
diff changeset
   364
                val (r, t) = path_finder tm_p ps (nth ts p)
43130
d73fc2e55308 implemented missing hAPP and ti cases of new path finder
blanchet
parents: 43128
diff changeset
   365
              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
   366
          end
70505
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
   367
      | path_finder tm ps t = path_finder_fail tm ps (SOME t)
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
   368
    val (tm_subst, body) = path_finder i_atom fp m_tm
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
   369
    val tm_abs = Abs ("x", type_of tm_subst, body)
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
   370
    val _ = trace_msg ctxt (fn () => "abstraction: " ^ Syntax.string_of_term ctxt tm_abs)
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
   371
    val _ = trace_msg ctxt (fn () => "i_tm: " ^ Syntax.string_of_term ctxt i_tm)
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
   372
    val _ = trace_msg ctxt (fn () => "located term: " ^ Syntax.string_of_term ctxt tm_subst)
70515
wenzelm
parents: 70512
diff changeset
   373
    val maxidx = fold Term.maxidx_term [i_tm, tm_abs, tm_subst] ~1
wenzelm
parents: 70512
diff changeset
   374
    val subst' = Thm.incr_indexes (maxidx + 1) (if pos then subst_em else ssubst_em)
70505
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
   375
    val _ = trace_msg ctxt (fn () => "subst' " ^ Thm.string_of_thm ctxt subst')
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
   376
    val eq_terms =
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
   377
      map (apply2 (Thm.cterm_of ctxt))
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
   378
        (ListPair.zip (Misc_Legacy.term_vars (Thm.prop_of subst'), [tm_abs, tm_subst, i_tm]))
57408
39b3562e9edc tuned whitespace and parentheses
blanchet
parents: 57400
diff changeset
   379
  in
60795
c24fa03f4c71 tuned signature;
wenzelm
parents: 60794
diff changeset
   380
    infer_instantiate_types ctxt (map (apfst (dest_Var o Thm.term_of)) eq_terms) subst'
57408
39b3562e9edc tuned whitespace and parentheses
blanchet
parents: 57400
diff changeset
   381
  end
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   382
43094
269300fb83d0 more work on new Metis
blanchet
parents: 43093
diff changeset
   383
val factor = Seq.hd o distinct_subgoals_tac
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   384
70512
wenzelm
parents: 70508
diff changeset
   385
fun one_step ctxt type_enc concealed sym_tab th_pairs (fol_th, inference) =
wenzelm
parents: 70508
diff changeset
   386
  (case inference of
wenzelm
parents: 70508
diff changeset
   387
    Metis_Proof.Axiom _ =>
wenzelm
parents: 70508
diff changeset
   388
      axiom_inference th_pairs fol_th |> factor
wenzelm
parents: 70508
diff changeset
   389
  | Metis_Proof.Assume atom =>
wenzelm
parents: 70508
diff changeset
   390
      assume_inference ctxt type_enc concealed sym_tab atom
wenzelm
parents: 70508
diff changeset
   391
  | Metis_Proof.Metis_Subst (subst, th1) =>
wenzelm
parents: 70508
diff changeset
   392
      inst_inference ctxt type_enc concealed sym_tab th_pairs subst th1 |> factor
wenzelm
parents: 70508
diff changeset
   393
  | Metis_Proof.Resolve (atom, th1, th2) =>
wenzelm
parents: 70508
diff changeset
   394
      resolve_inference ctxt type_enc concealed sym_tab th_pairs atom th1 th2 |> factor
wenzelm
parents: 70508
diff changeset
   395
  | Metis_Proof.Refl tm =>
wenzelm
parents: 70508
diff changeset
   396
      refl_inference ctxt type_enc concealed sym_tab tm
wenzelm
parents: 70508
diff changeset
   397
  | Metis_Proof.Equality (lit, p, r) =>
wenzelm
parents: 70508
diff changeset
   398
      equality_inference ctxt type_enc concealed sym_tab lit p r)
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   399
60363
5568b16aa477 clarified context;
wenzelm
parents: 59632
diff changeset
   400
fun flexflex_first_order ctxt th =
54756
blanchet
parents: 54742
diff changeset
   401
  (case Thm.tpairs_of th of
blanchet
parents: 54742
diff changeset
   402
    [] => th
blanchet
parents: 54742
diff changeset
   403
  | pairs =>
59617
b60e65ad13df tuned -- more explicit use of context;
wenzelm
parents: 59582
diff changeset
   404
      let
60363
5568b16aa477 clarified context;
wenzelm
parents: 59632
diff changeset
   405
        val thy = Proof_Context.theory_of ctxt
59617
b60e65ad13df tuned -- more explicit use of context;
wenzelm
parents: 59582
diff changeset
   406
        val (tyenv, tenv) = fold (Pattern.first_order_match thy) pairs (Vartab.empty, Vartab.empty)
70505
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
   407
60642
48dd1cefb4ae simplified Thm.instantiate and derivatives: the LHS refers to non-certified variables -- this merely serves as index into already certified structures (or is ignored);
wenzelm
parents: 60363
diff changeset
   408
        fun mkT (v, (S, T)) = ((v, S), Thm.ctyp_of ctxt T)
48dd1cefb4ae simplified Thm.instantiate and derivatives: the LHS refers to non-certified variables -- this merely serves as index into already certified structures (or is ignored);
wenzelm
parents: 60363
diff changeset
   409
        fun mk (v, (T, t)) = ((v, Envir.subst_type tyenv T), Thm.cterm_of ctxt t)
70505
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
   410
59617
b60e65ad13df tuned -- more explicit use of context;
wenzelm
parents: 59582
diff changeset
   411
        val instsT = Vartab.fold (cons o mkT) tyenv []
b60e65ad13df tuned -- more explicit use of context;
wenzelm
parents: 59582
diff changeset
   412
        val insts = Vartab.fold (cons o mk) tenv []
b60e65ad13df tuned -- more explicit use of context;
wenzelm
parents: 59582
diff changeset
   413
      in
74282
c2ee8d993d6a clarified signature: more scalable operations;
wenzelm
parents: 70518
diff changeset
   414
        Thm.instantiate (TVars.make instsT, Vars.make insts) th
59617
b60e65ad13df tuned -- more explicit use of context;
wenzelm
parents: 59582
diff changeset
   415
      end
b60e65ad13df tuned -- more explicit use of context;
wenzelm
parents: 59582
diff changeset
   416
      handle THM _ => th)
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   417
43268
c0eaa8b9bff5 removed yet another hack in "make_metis" script -- respect opacity of "Metis_Name.name"
blanchet
parents: 43262
diff changeset
   418
fun is_metis_literal_genuine (_, (s, _)) =
c0eaa8b9bff5 removed yet another hack in "make_metis" script -- respect opacity of "Metis_Name.name"
blanchet
parents: 43262
diff changeset
   419
  not (String.isPrefix class_prefix (Metis_Name.toString s))
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
   420
fun is_isabelle_literal_genuine t =
74347
f984d30cd0c3 clarified antiquotations;
wenzelm
parents: 74290
diff changeset
   421
  (case t of _ $ \<^Const_>\<open>Meson.skolem _ for _\<close> => 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
   422
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
   423
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
   424
70505
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
   425
(*Seldomly needed hack. A Metis clause is represented as a set, so duplicate
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
   426
  disjuncts are impossible. In the Isabelle proof, in spite of efforts to
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
   427
  eliminate them, duplicates sometimes appear with slightly different (but
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
   428
  unifiable) types.*)
42333
23bb0784b5d0 try to repair out-of-sync situations in Metis
blanchet
parents: 42271
diff changeset
   429
fun resynchronize ctxt fol_th th =
23bb0784b5d0 try to repair out-of-sync situations in Metis
blanchet
parents: 42271
diff changeset
   430
  let
23bb0784b5d0 try to repair out-of-sync situations in Metis
blanchet
parents: 42271
diff changeset
   431
    val num_metis_lits =
54756
blanchet
parents: 54742
diff changeset
   432
      count is_metis_literal_genuine (Metis_LiteralSet.toList (Metis_Thm.clause fol_th))
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59498
diff changeset
   433
    val num_isabelle_lits = count is_isabelle_literal_genuine (Thm.prems_of th)
42333
23bb0784b5d0 try to repair out-of-sync situations in Metis
blanchet
parents: 42271
diff changeset
   434
  in
23bb0784b5d0 try to repair out-of-sync situations in Metis
blanchet
parents: 42271
diff changeset
   435
    if num_metis_lits >= num_isabelle_lits then
23bb0784b5d0 try to repair out-of-sync situations in Metis
blanchet
parents: 42271
diff changeset
   436
      th
23bb0784b5d0 try to repair out-of-sync situations in Metis
blanchet
parents: 42271
diff changeset
   437
    else
23bb0784b5d0 try to repair out-of-sync situations in Metis
blanchet
parents: 42271
diff changeset
   438
      let
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59498
diff changeset
   439
        val (prems0, concl) = th |> Thm.prop_of |> Logic.strip_horn
54756
blanchet
parents: 54742
diff changeset
   440
        val prems = prems0 |> map normalize_literal |> distinct Term.aconv_untyped
42333
23bb0784b5d0 try to repair out-of-sync situations in Metis
blanchet
parents: 42271
diff changeset
   441
        val goal = Logic.list_implies (prems, concl)
60949
ccbf9379e355 added Thm.chyps_of;
wenzelm
parents: 60795
diff changeset
   442
        val ctxt' = fold Thm.declare_hyps (Thm.chyps_of th) ctxt
58963
26bf09b95dda proper context for assume_tac (atac remains as fall-back without context);
wenzelm
parents: 58957
diff changeset
   443
        val tac =
26bf09b95dda proper context for assume_tac (atac remains as fall-back without context);
wenzelm
parents: 58957
diff changeset
   444
          cut_tac th 1 THEN
26bf09b95dda proper context for assume_tac (atac remains as fall-back without context);
wenzelm
parents: 58957
diff changeset
   445
          rewrite_goals_tac ctxt' meta_not_not THEN
26bf09b95dda proper context for assume_tac (atac remains as fall-back without context);
wenzelm
parents: 58957
diff changeset
   446
          ALLGOALS (assume_tac ctxt')
42333
23bb0784b5d0 try to repair out-of-sync situations in Metis
blanchet
parents: 42271
diff changeset
   447
      in
23bb0784b5d0 try to repair out-of-sync situations in Metis
blanchet
parents: 42271
diff changeset
   448
        if length prems = length prems0 then
50875
bfb626265782 less brutal Metis failure -- the brutality was accidentally introduced by df8ae0590be2
blanchet
parents: 48132
diff changeset
   449
          raise METIS_RECONSTRUCT ("resynchronize", "Out of sync")
42333
23bb0784b5d0 try to repair out-of-sync situations in Metis
blanchet
parents: 42271
diff changeset
   450
        else
54984
da70ab8531f4 more elementary management of declared hyps, below structure Assumption;
wenzelm
parents: 54756
diff changeset
   451
          Goal.prove ctxt' [] [] goal (K tac)
da70ab8531f4 more elementary management of declared hyps, below structure Assumption;
wenzelm
parents: 54756
diff changeset
   452
          |> resynchronize ctxt' fol_th
42333
23bb0784b5d0 try to repair out-of-sync situations in Metis
blanchet
parents: 42271
diff changeset
   453
      end
23bb0784b5d0 try to repair out-of-sync situations in Metis
blanchet
parents: 42271
diff changeset
   454
  end
23bb0784b5d0 try to repair out-of-sync situations in Metis
blanchet
parents: 42271
diff changeset
   455
70505
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
   456
fun replay_one_inference ctxt type_enc concealed sym_tab (fol_th, inf) th_pairs =
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 67725
diff changeset
   457
  if not (null th_pairs) andalso Thm.prop_of (snd (hd th_pairs)) aconv \<^prop>\<open>False\<close> then
70505
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
   458
    (*Isabelle sometimes identifies literals (premises) that are distinct in
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
   459
      Metis (e.g., because of type variables). We give the Isabelle proof the
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
   460
      benefice of the doubt.*)
43094
269300fb83d0 more work on new Metis
blanchet
parents: 43093
diff changeset
   461
    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
   462
  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
   463
    let
54756
blanchet
parents: 54742
diff changeset
   464
      val _ = trace_msg ctxt (fn () => "=============================================")
blanchet
parents: 54742
diff changeset
   465
      val _ = trace_msg ctxt (fn () => "METIS THM: " ^ Metis_Thm.toString fol_th)
blanchet
parents: 54742
diff changeset
   466
      val _ = trace_msg ctxt (fn () => "INFERENCE: " ^ Metis_Proof.inferenceToString inf)
45508
b216dc1b3630 started implementing lambda-lifting in Metis
blanchet
parents: 44492
diff changeset
   467
      val th = one_step ctxt type_enc concealed sym_tab th_pairs (fol_th, inf)
60363
5568b16aa477 clarified context;
wenzelm
parents: 59632
diff changeset
   468
        |> flexflex_first_order ctxt
54756
blanchet
parents: 54742
diff changeset
   469
        |> resynchronize ctxt fol_th
61268
abe08fb15a12 moved remaining display.ML to more_thm.ML;
wenzelm
parents: 60949
diff changeset
   470
      val _ = trace_msg ctxt (fn () => "ISABELLE THM: " ^ Thm.string_of_thm ctxt th)
54756
blanchet
parents: 54742
diff changeset
   471
      val _ = trace_msg ctxt (fn () => "=============================================")
blanchet
parents: 54742
diff changeset
   472
    in
blanchet
parents: 54742
diff changeset
   473
      (fol_th, th) :: th_pairs
blanchet
parents: 54742
diff changeset
   474
    end
39497
fa16349939b7 complete refactoring of Metis along the lines of Sledgehammer
blanchet
parents: 39495
diff changeset
   475
70505
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
   476
(*It is normally sufficient to apply "assume_tac" to unify the conclusion with
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
   477
  one of the premises. Unfortunately, this sometimes yields "Variable
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
   478
  has two distinct types" errors. To avoid this, we instantiate the
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
   479
  variables before applying "assume_tac". Typical constraints are of the form
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
   480
    ?SK_a_b_c_x SK_d_e_f_y ... SK_a_b_c_x ... SK_g_h_i_z \<equiv>\<^sup>? SK_a_b_c_x,
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
   481
  where the nonvariables are goal parameters.*)
59632
5980e75a204e clarified context;
wenzelm
parents: 59621
diff changeset
   482
fun unify_first_prem_with_concl ctxt i th =
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
   483
  let
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59498
diff changeset
   484
    val goal = Logic.get_goal (Thm.prop_of th) i |> Envir.beta_eta_contract
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
   485
    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
   486
    val concl = goal |> Logic.strip_assums_concl
54756
blanchet
parents: 54742
diff changeset
   487
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
   488
    fun pair_untyped_aconv (t1, t2) (u1, u2) =
43301
8d7fc4a5b502 removed needless function that duplicated standard functionality, with a little unnecessary twist
blanchet
parents: 43300
diff changeset
   489
      Term.aconv_untyped (t1, u1) andalso Term.aconv_untyped (t2, u2)
54756
blanchet
parents: 54742
diff changeset
   490
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
   491
    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
   492
      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
   493
      else tp :: map (apsnd (subst_atomic [tp])) inst
54756
blanchet
parents: 54742
diff changeset
   494
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
   495
    fun is_flex t =
54756
blanchet
parents: 54742
diff changeset
   496
      (case strip_comb t of
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
   497
        (Var _, args) => forall is_Bound args
54756
blanchet
parents: 54742
diff changeset
   498
      | _ => false)
blanchet
parents: 54742
diff changeset
   499
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
   500
    fun unify_flex flex rigid =
54756
blanchet
parents: 54742
diff changeset
   501
      (case strip_comb flex of
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
   502
        (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
   503
        add_terms (Var z,
44241
7943b69f0188 modernized signature of Term.absfree/absdummy;
wenzelm
parents: 44121
diff changeset
   504
          fold_rev absdummy (take (length args) (binder_types T)) rigid)
54756
blanchet
parents: 54742
diff changeset
   505
      | _ => I)
blanchet
parents: 54742
diff changeset
   506
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
   507
    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
   508
      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
   509
      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
   510
      else I
54756
blanchet
parents: 54742
diff changeset
   511
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
   512
    fun unify_terms (t, u) =
54756
blanchet
parents: 54742
diff changeset
   513
      (case (t, u) of
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
   514
        (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
   515
        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
   516
        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
   517
        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
   518
      | (_ $ _, _) => 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
   519
      | (_, _ $ _) => 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
   520
      | (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
   521
      | (_, Var _) => add_terms (u, t)
54756
blanchet
parents: 54742
diff changeset
   522
      | _ => I)
blanchet
parents: 54742
diff changeset
   523
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
   524
    val t_inst =
59632
5980e75a204e clarified context;
wenzelm
parents: 59621
diff changeset
   525
      [] |> try (unify_terms (prem, concl) #> map (apply2 (Thm.cterm_of ctxt)))
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
   526
         |> the_default [] (* FIXME *)
54756
blanchet
parents: 54742
diff changeset
   527
  in
60795
c24fa03f4c71 tuned signature;
wenzelm
parents: 60794
diff changeset
   528
    infer_instantiate_types ctxt (map (apfst (dest_Var o Thm.term_of)) t_inst) th
54756
blanchet
parents: 54742
diff changeset
   529
  end
39964
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   530
70505
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
   531
val copy_prem = @{lemma "P \<Longrightarrow> (P \<Longrightarrow> P \<Longrightarrow> Q) \<Longrightarrow> Q" by assumption}
39964
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   532
59498
50b60f501b05 proper context for resolve_tac, eresolve_tac, dresolve_tac, forward_tac etc.;
wenzelm
parents: 59058
diff changeset
   533
fun copy_prems_tac ctxt [] ns i =
70505
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
   534
      if forall (curry (op =) 1) ns then all_tac else copy_prems_tac ctxt (rev ns) [] i
59498
50b60f501b05 proper context for resolve_tac, eresolve_tac, dresolve_tac, forward_tac etc.;
wenzelm
parents: 59058
diff changeset
   535
  | copy_prems_tac ctxt (1 :: ms) ns i = rotate_tac 1 i THEN copy_prems_tac ctxt ms (1 :: ns) i
50b60f501b05 proper context for resolve_tac, eresolve_tac, dresolve_tac, forward_tac etc.;
wenzelm
parents: 59058
diff changeset
   536
  | copy_prems_tac ctxt (m :: ms) ns i =
70505
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
   537
      eresolve_tac ctxt [copy_prem] i THEN copy_prems_tac ctxt ms (m div 2 :: (m + 1) div 2 :: ns) i
39964
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   538
70505
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
   539
(*Metis generates variables of the form _nnn.*)
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
   540
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
   541
59498
50b60f501b05 proper context for resolve_tac, eresolve_tac, dresolve_tac, forward_tac etc.;
wenzelm
parents: 59058
diff changeset
   542
fun instantiate_forall_tac ctxt t i st =
39964
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   543
  let
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59498
diff changeset
   544
    val params = Logic.strip_params (Logic.get_goal (Thm.prop_of st) i) |> rev
54756
blanchet
parents: 54742
diff changeset
   545
39964
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   546
    fun repair (t as (Var ((s, _), _))) =
70505
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
   547
          (case find_index (fn (s', _) => s' = s) params of
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
   548
            ~1 => t
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
   549
          | j => Bound j)
40261
7a02144874f3 more work on new Skolemizer without Hilbert_Choice
blanchet
parents: 40259
diff changeset
   550
      | repair (t $ u) =
70505
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
   551
          (case (repair t, repair u) of
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
   552
            (t as Bound j, u as Bound k) =>
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
   553
            (*This is a trick to repair the discrepancy between the fully skolemized term that MESON
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
   554
              gives us (where existentials were pulled out) and the reality.*)
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
   555
            if k > j then t else t $ u
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
   556
          | (t, u) => t $ u)
39964
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   557
      | repair t = t
54756
blanchet
parents: 54742
diff changeset
   558
44241
7943b69f0188 modernized signature of Term.absfree/absdummy;
wenzelm
parents: 44121
diff changeset
   559
    val t' = t |> repair |> fold (absdummy o snd) params
54756
blanchet
parents: 54742
diff changeset
   560
39964
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   561
    fun do_instantiate th =
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59498
diff changeset
   562
      (case Term.add_vars (Thm.prop_of th) []
54756
blanchet
parents: 54742
diff changeset
   563
            |> filter_out ((Meson_Clausify.is_zapped_var_name orf is_metis_fresh_variable) o fst
blanchet
parents: 54742
diff changeset
   564
              o fst) of
42270
5f2960582e45 make new Skolemizer more robust
blanchet
parents: 42107
diff changeset
   565
        [] => 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
   566
      | [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
   567
        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
   568
          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
   569
          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
   570
          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
   571
            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
   572
                                             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
   573
          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
   574
            Envir.Envir {maxidx = Vartab.fold (Integer.max o snd o fst) tyenv 0,
54756
blanchet
parents: 54742
diff changeset
   575
              tenv = Vartab.empty, tyenv = tyenv}
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
   576
          val ty_inst =
60642
48dd1cefb4ae simplified Thm.instantiate and derivatives: the LHS refers to non-certified variables -- this merely serves as index into already certified structures (or is ignored);
wenzelm
parents: 60363
diff changeset
   577
            Vartab.fold (fn (x, (S, T)) => cons (((x, S), Thm.ctyp_of ctxt T)))
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59498
diff changeset
   578
              tyenv []
59632
5980e75a204e clarified context;
wenzelm
parents: 59621
diff changeset
   579
          val t_inst = [apply2 (Thm.cterm_of ctxt o Envir.norm_term env) (Var var, t')]
54756
blanchet
parents: 54742
diff changeset
   580
        in
74282
c2ee8d993d6a clarified signature: more scalable operations;
wenzelm
parents: 70518
diff changeset
   581
          Drule.instantiate_normalize
c2ee8d993d6a clarified signature: more scalable operations;
wenzelm
parents: 70518
diff changeset
   582
            (TVars.make ty_inst, Vars.make (map (apfst (dest_Var o Thm.term_of)) t_inst)) th
54756
blanchet
parents: 54742
diff changeset
   583
        end
blanchet
parents: 54742
diff changeset
   584
      | _ => 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
   585
  in
59498
50b60f501b05 proper context for resolve_tac, eresolve_tac, dresolve_tac, forward_tac etc.;
wenzelm
parents: 59058
diff changeset
   586
    (DETERM (eresolve_tac ctxt @{thms allE} i THEN rotate_tac ~1 i) THEN PRIMITIVE do_instantiate) st
39964
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   587
  end
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   588
59498
50b60f501b05 proper context for resolve_tac, eresolve_tac, dresolve_tac, forward_tac etc.;
wenzelm
parents: 59058
diff changeset
   589
fun fix_exists_tac ctxt t = eresolve_tac ctxt [exE] THEN' rename_tac [t |> dest_Var |> fst |> fst]
40261
7a02144874f3 more work on new Skolemizer without Hilbert_Choice
blanchet
parents: 40259
diff changeset
   590
59498
50b60f501b05 proper context for resolve_tac, eresolve_tac, dresolve_tac, forward_tac etc.;
wenzelm
parents: 59058
diff changeset
   591
fun release_quantifier_tac ctxt (skolem, t) =
50b60f501b05 proper context for resolve_tac, eresolve_tac, dresolve_tac, forward_tac etc.;
wenzelm
parents: 59058
diff changeset
   592
  (if skolem then fix_exists_tac ctxt else instantiate_forall_tac ctxt) t
40261
7a02144874f3 more work on new Skolemizer without Hilbert_Choice
blanchet
parents: 40259
diff changeset
   593
40258
2c0d8fe36c21 make handling of parameters more robust, by querying the goal
blanchet
parents: 40221
diff changeset
   594
fun release_clusters_tac _ _ _ [] = K all_tac
59498
50b60f501b05 proper context for resolve_tac, eresolve_tac, dresolve_tac, forward_tac etc.;
wenzelm
parents: 59058
diff changeset
   595
  | release_clusters_tac ctxt ax_counts substs ((ax_no, cluster_no) :: clusters) =
39964
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   596
    let
54756
blanchet
parents: 54742
diff changeset
   597
      val cluster_of_var = Meson_Clausify.cluster_of_zapped_var_name o fst o fst o dest_Var
40261
7a02144874f3 more work on new Skolemizer without Hilbert_Choice
blanchet
parents: 40259
diff changeset
   598
      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
   599
      val cluster_substs =
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   600
        substs
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   601
        |> map_filter (fn (ax_no', (_, (_, tsubst))) =>
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   602
                          if ax_no' = ax_no then
40261
7a02144874f3 more work on new Skolemizer without Hilbert_Choice
blanchet
parents: 40259
diff changeset
   603
                            tsubst |> map (apfst cluster_of_var)
7a02144874f3 more work on new Skolemizer without Hilbert_Choice
blanchet
parents: 40259
diff changeset
   604
                                   |> filter (in_right_cluster o fst)
7a02144874f3 more work on new Skolemizer without Hilbert_Choice
blanchet
parents: 40259
diff changeset
   605
                                   |> map (apfst snd)
7a02144874f3 more work on new Skolemizer without Hilbert_Choice
blanchet
parents: 40259
diff changeset
   606
                                   |> SOME
7a02144874f3 more work on new Skolemizer without Hilbert_Choice
blanchet
parents: 40259
diff changeset
   607
                          else
7a02144874f3 more work on new Skolemizer without Hilbert_Choice
blanchet
parents: 40259
diff changeset
   608
                            NONE)
39964
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   609
      fun do_cluster_subst cluster_subst =
59498
50b60f501b05 proper context for resolve_tac, eresolve_tac, dresolve_tac, forward_tac etc.;
wenzelm
parents: 59058
diff changeset
   610
        map (release_quantifier_tac ctxt) cluster_subst @ [rotate_tac 1]
39964
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   611
      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
   612
    in
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   613
      rotate_tac first_prem
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   614
      THEN' (EVERY' (maps do_cluster_subst cluster_substs))
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   615
      THEN' rotate_tac (~ first_prem - length cluster_substs)
59498
50b60f501b05 proper context for resolve_tac, eresolve_tac, dresolve_tac, forward_tac etc.;
wenzelm
parents: 59058
diff changeset
   616
      THEN' release_clusters_tac ctxt ax_counts substs clusters
39964
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   617
    end
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   618
40264
b91e2e16d994 fixed order of quantifier instantiation in new Skolemizer
blanchet
parents: 40261
diff changeset
   619
fun cluster_key ((ax_no, (cluster_no, index_no)), skolem) =
b91e2e16d994 fixed order of quantifier instantiation in new Skolemizer
blanchet
parents: 40261
diff changeset
   620
  (ax_no, (cluster_no, (skolem, index_no)))
b91e2e16d994 fixed order of quantifier instantiation in new Skolemizer
blanchet
parents: 40261
diff changeset
   621
fun cluster_ord p =
59058
a78612c67ec0 renamed "pairself" to "apply2", in accordance to @{apply 2};
wenzelm
parents: 58963
diff changeset
   622
  prod_ord int_ord (prod_ord int_ord (prod_ord bool_ord int_ord)) (apply2 cluster_key p)
39964
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   623
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   624
val tysubst_ord =
54756
blanchet
parents: 54742
diff changeset
   625
  list_ord (prod_ord Term_Ord.fast_indexname_ord (prod_ord Term_Ord.sort_ord Term_Ord.typ_ord))
39964
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   626
70505
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
   627
structure Int_Tysubst_Table = Table
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
   628
(
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
   629
  type key = int * (indexname * (sort * typ)) list
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
   630
  val ord = prod_ord int_ord tysubst_ord
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
   631
)
39964
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   632
70505
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
   633
structure Int_Pair_Graph = Graph(
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
   634
  type key = int * int
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
   635
  val ord = prod_ord int_ord int_ord
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
   636
)
39964
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   637
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
   638
fun shuffle_key (((axiom_no, (_, index_no)), _), _) = (axiom_no, index_no)
59058
a78612c67ec0 renamed "pairself" to "apply2", in accordance to @{apply 2};
wenzelm
parents: 58963
diff changeset
   639
fun shuffle_ord p = prod_ord int_ord int_ord (apply2 shuffle_key p)
40258
2c0d8fe36c21 make handling of parameters more robust, by querying the goal
blanchet
parents: 40221
diff changeset
   640
70505
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
   641
(*Attempts to derive the theorem "False" from a theorem of the form
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
   642
  "P1 ==> ... ==> Pn ==> False", where the "Pi"s are to be discharged using the
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
   643
  specified axioms. The axioms have leading "All" and "Ex" quantifiers, which
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
   644
  must be eliminated first.*)
39964
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   645
fun discharge_skolem_premises ctxt axioms prems_imp_false =
70505
1881fb38a1ef misc tuning -- slightly more readable;
wenzelm
parents: 70487
diff changeset
   646
  if Thm.prop_of prems_imp_false aconv \<^prop>\<open>False\<close> then prems_imp_false
39964
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   647
  else
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   648
    let
42361
23f352990944 modernized structure Proof_Context;
wenzelm
parents: 42354
diff changeset
   649
      val thy = Proof_Context.theory_of ctxt
57408
39b3562e9edc tuned whitespace and parentheses
blanchet
parents: 57400
diff changeset
   650
39964
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   651
      fun match_term p =
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   652
        let
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   653
          val (tyenv, tenv) =
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   654
            Pattern.first_order_match thy p (Vartab.empty, Vartab.empty)
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   655
          val tsubst =
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   656
            tenv |> Vartab.dest
42099
447fa058ab22 avoid evil "export_without_context", which breaks if there are local "fixes"
blanchet
parents: 42098
diff changeset
   657
                 |> 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
   658
                 |> sort (cluster_ord
59058
a78612c67ec0 renamed "pairself" to "apply2", in accordance to @{apply 2};
wenzelm
parents: 58963
diff changeset
   659
                          o apply2 (Meson_Clausify.cluster_of_zapped_var_name
39964
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   660
                                      o fst o fst))
60781
2da59cdf531c updated to infer_instantiate;
wenzelm
parents: 60642
diff changeset
   661
                 |> map (fn (xi, (T, t)) => apply2 (Envir.subst_term_types tyenv) (Var (xi, T), t))
39964
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   662
          val tysubst = tyenv |> Vartab.dest
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   663
        in (tysubst, tsubst) end
57408
39b3562e9edc tuned whitespace and parentheses
blanchet
parents: 57400
diff changeset
   664
51998
f732a674db1b renamed Sledgehammer functions with 'for' in their names to 'of'
blanchet
parents: 51951
diff changeset
   665
      fun subst_info_of_prem subgoal_no prem =
57408
39b3562e9edc tuned whitespace and parentheses
blanchet
parents: 57400
diff changeset
   666
        (case prem of
74347
f984d30cd0c3 clarified antiquotations;
wenzelm
parents: 74290
diff changeset
   667
          _ $ \<^Const_>\<open>Meson.skolem _ for \<open>_ $ t $ num\<close>\<close> =>
f984d30cd0c3 clarified antiquotations;
wenzelm
parents: 74290
diff changeset
   668
            let val ax_no = HOLogic.dest_nat num in
f984d30cd0c3 clarified antiquotations;
wenzelm
parents: 74290
diff changeset
   669
              (ax_no, (subgoal_no,
f984d30cd0c3 clarified antiquotations;
wenzelm
parents: 74290
diff changeset
   670
                       match_term (nth axioms ax_no |> the |> snd, t)))
f984d30cd0c3 clarified antiquotations;
wenzelm
parents: 74290
diff changeset
   671
            end
57408
39b3562e9edc tuned whitespace and parentheses
blanchet
parents: 57400
diff changeset
   672
        | _ => raise TERM ("discharge_skolem_premises: Malformed premise", [prem]))
39b3562e9edc tuned whitespace and parentheses
blanchet
parents: 57400
diff changeset
   673
39964
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   674
      fun cluster_of_var_name skolem s =
57408
39b3562e9edc tuned whitespace and parentheses
blanchet
parents: 57400
diff changeset
   675
        (case try Meson_Clausify.cluster_of_zapped_var_name s of
42098
f978caf60bbe more robust handling of variables in new Skolemizer
blanchet
parents: 41491
diff changeset
   676
          NONE => NONE
f978caf60bbe more robust handling of variables in new Skolemizer
blanchet
parents: 41491
diff changeset
   677
        | SOME ((ax_no, (cluster_no, _)), skolem') =>
57408
39b3562e9edc tuned whitespace and parentheses
blanchet
parents: 57400
diff changeset
   678
          if skolem' = skolem andalso cluster_no > 0 then SOME (ax_no, cluster_no) else NONE)
39b3562e9edc tuned whitespace and parentheses
blanchet
parents: 57400
diff changeset
   679
39964
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   680
      fun clusters_in_term skolem t =
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   681
        Term.add_var_names t [] |> map_filter (cluster_of_var_name skolem o fst)
57408
39b3562e9edc tuned whitespace and parentheses
blanchet
parents: 57400
diff changeset
   682
51998
f732a674db1b renamed Sledgehammer functions with 'for' in their names to 'of'
blanchet
parents: 51951
diff changeset
   683
      fun deps_of_term_subst (var, t) =
57408
39b3562e9edc tuned whitespace and parentheses
blanchet
parents: 57400
diff changeset
   684
        (case clusters_in_term false var of
39964
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   685
          [] => NONE
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   686
        | [(ax_no, cluster_no)] =>
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   687
          SOME ((ax_no, cluster_no),
57408
39b3562e9edc tuned whitespace and parentheses
blanchet
parents: 57400
diff changeset
   688
            clusters_in_term true t |> cluster_no > 1 ? cons (ax_no, cluster_no - 1))
39b3562e9edc tuned whitespace and parentheses
blanchet
parents: 57400
diff changeset
   689
        | _ => raise TERM ("discharge_skolem_premises: Expected Var", [var]))
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59498
diff changeset
   690
      val prems = Logic.strip_imp_prems (Thm.prop_of prems_imp_false)
74904
cab76af373e7 tuned metis to use map_index
desharna
parents: 74611
diff changeset
   691
      val substs =
cab76af373e7 tuned metis to use map_index
desharna
parents: 74611
diff changeset
   692
        map_index (fn (i, prem) => subst_info_of_prem (i + 1) prem) prems
cab76af373e7 tuned metis to use map_index
desharna
parents: 74611
diff changeset
   693
        |> sort (int_ord o apply2 fst)
51998
f732a674db1b renamed Sledgehammer functions with 'for' in their names to 'of'
blanchet
parents: 51951
diff changeset
   694
      val depss = maps (map_filter deps_of_term_subst o snd o snd o snd) substs
39964
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   695
      val clusters = maps (op ::) depss
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   696
      val ordered_clusters =
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   697
        Int_Pair_Graph.empty
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   698
        |> fold Int_Pair_Graph.default_node (map (rpair ()) clusters)
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   699
        |> fold Int_Pair_Graph.add_deps_acyclic depss
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   700
        |> Int_Pair_Graph.topological_order
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   701
        handle Int_Pair_Graph.CYCLES _ =>
55523
9429e7b5b827 removed final periods in messages for proof methods
blanchet
parents: 55234
diff changeset
   702
               error "Cannot replay Metis proof in Isabelle without \"Hilbert_Choice\""
39964
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   703
      val ax_counts =
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   704
        Int_Tysubst_Table.empty
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   705
        |> fold (fn (ax_no, (_, (tysubst, _))) =>
43262
547a02d889f5 removed experimental code submitted by mistake
blanchet
parents: 43259
diff changeset
   706
                    Int_Tysubst_Table.map_default ((ax_no, tysubst), 0)
39964
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   707
                                                  (Integer.add 1)) substs
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   708
        |> 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
   709
      val needed_axiom_props =
74904
cab76af373e7 tuned metis to use map_index
desharna
parents: 74611
diff changeset
   710
        map_index I axioms
42339
0e5d1e5e1177 use the list of actually used axioms to (correctly) precompute the "outer params", not all axioms
blanchet
parents: 42337
diff changeset
   711
        |> 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
   712
                        | (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
   713
                          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
   714
                                        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
   715
                                    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
   716
                            SOME t
0e5d1e5e1177 use the list of actually used axioms to (correctly) precompute the "outer params", not all axioms
blanchet
parents: 42337
diff changeset
   717
                          else
0e5d1e5e1177 use the list of actually used axioms to (correctly) precompute the "outer params", not all axioms
blanchet
parents: 42337
diff changeset
   718
                            NONE)
0e5d1e5e1177 use the list of actually used axioms to (correctly) precompute the "outer params", not all axioms
blanchet
parents: 42337
diff changeset
   719
      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
   720
        [] |> 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
   721
           |> 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
   722
           |> 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
   723
           |> 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
   724
                         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
   725
           |> sort shuffle_ord |> map (fst o snd)
57408
39b3562e9edc tuned whitespace and parentheses
blanchet
parents: 57400
diff changeset
   726
42270
5f2960582e45 make new Skolemizer more robust
blanchet
parents: 42107
diff changeset
   727
(* for debugging only:
51998
f732a674db1b renamed Sledgehammer functions with 'for' in their names to 'of'
blanchet
parents: 51951
diff changeset
   728
      fun string_of_subst_info (ax_no, (subgoal_no, (tysubst, tsubst))) =
39964
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   729
        "ax: " ^ string_of_int ax_no ^ "; asm: " ^ string_of_int subgoal_no ^
51929
5e8a0b8bb070 avoid PolyML.makestring, even in dead code;
wenzelm
parents: 51701
diff changeset
   730
        "; tysubst: " ^ @{make_string} tysubst ^ "; tsubst: {" ^
39964
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   731
        commas (map ((fn (s, t) => s ^ " |-> " ^ t)
59058
a78612c67ec0 renamed "pairself" to "apply2", in accordance to @{apply 2};
wenzelm
parents: 58963
diff changeset
   732
                     o apply2 (Syntax.string_of_term ctxt)) tsubst) ^ "}"
51929
5e8a0b8bb070 avoid PolyML.makestring, even in dead code;
wenzelm
parents: 51701
diff changeset
   733
      val _ = tracing ("ORDERED CLUSTERS: " ^ @{make_string} ordered_clusters)
5e8a0b8bb070 avoid PolyML.makestring, even in dead code;
wenzelm
parents: 51701
diff changeset
   734
      val _ = tracing ("AXIOM COUNTS: " ^ @{make_string} ax_counts)
5e8a0b8bb070 avoid PolyML.makestring, even in dead code;
wenzelm
parents: 51701
diff changeset
   735
      val _ = tracing ("OUTER PARAMS: " ^ @{make_string} outer_param_names)
39964
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   736
      val _ = tracing ("SUBSTS (" ^ string_of_int (length substs) ^ "):\n" ^
51998
f732a674db1b renamed Sledgehammer functions with 'for' in their names to 'of'
blanchet
parents: 51951
diff changeset
   737
                       cat_lines (map string_of_subst_info substs))
39964
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   738
*)
60949
ccbf9379e355 added Thm.chyps_of;
wenzelm
parents: 60795
diff changeset
   739
      val ctxt' = fold Thm.declare_hyps (Thm.chyps_of prems_imp_false) ctxt
57408
39b3562e9edc tuned whitespace and parentheses
blanchet
parents: 57400
diff changeset
   740
58839
ccda99401bc8 eliminated aliases;
wenzelm
parents: 58070
diff changeset
   741
      fun cut_and_ex_tac axiom =
59498
50b60f501b05 proper context for resolve_tac, eresolve_tac, dresolve_tac, forward_tac etc.;
wenzelm
parents: 59058
diff changeset
   742
        cut_tac axiom 1 THEN TRY (REPEAT_ALL_NEW (eresolve_tac ctxt' @{thms exE}) 1)
51998
f732a674db1b renamed Sledgehammer functions with 'for' in their names to 'of'
blanchet
parents: 51951
diff changeset
   743
      fun rotation_of_subgoal i =
39964
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   744
        find_index (fn (_, (subgoal_no, _)) => subgoal_no = i) substs
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   745
    in
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 67725
diff changeset
   746
      Goal.prove ctxt' [] [] \<^prop>\<open>False\<close>
57408
39b3562e9edc tuned whitespace and parentheses
blanchet
parents: 57400
diff changeset
   747
        (K (DETERM (EVERY (map (cut_and_ex_tac o fst o the o nth axioms o fst o fst) ax_counts)
39b3562e9edc tuned whitespace and parentheses
blanchet
parents: 57400
diff changeset
   748
              THEN rename_tac outer_param_names 1
59498
50b60f501b05 proper context for resolve_tac, eresolve_tac, dresolve_tac, forward_tac etc.;
wenzelm
parents: 59058
diff changeset
   749
              THEN copy_prems_tac ctxt' (map snd ax_counts) [] 1)
50b60f501b05 proper context for resolve_tac, eresolve_tac, dresolve_tac, forward_tac etc.;
wenzelm
parents: 59058
diff changeset
   750
            THEN release_clusters_tac ctxt' ax_counts substs ordered_clusters 1
58957
c9e744ea8a38 proper context for match_tac etc.;
wenzelm
parents: 58950
diff changeset
   751
            THEN match_tac ctxt' [prems_imp_false] 1
59498
50b60f501b05 proper context for resolve_tac, eresolve_tac, dresolve_tac, forward_tac etc.;
wenzelm
parents: 59058
diff changeset
   752
            THEN ALLGOALS (fn i => resolve_tac ctxt' @{thms Meson.skolem_COMBK_I} i
57408
39b3562e9edc tuned whitespace and parentheses
blanchet
parents: 57400
diff changeset
   753
              THEN rotate_tac (rotation_of_subgoal i) i
59632
5980e75a204e clarified context;
wenzelm
parents: 59621
diff changeset
   754
              THEN PRIMITIVE (unify_first_prem_with_concl ctxt' i)
58963
26bf09b95dda proper context for assume_tac (atac remains as fall-back without context);
wenzelm
parents: 58957
diff changeset
   755
              THEN assume_tac ctxt' i
58950
d07464875dd4 optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents: 58839
diff changeset
   756
              THEN flexflex_tac ctxt')))
54984
da70ab8531f4 more elementary management of declared hyps, below structure Assumption;
wenzelm
parents: 54756
diff changeset
   757
      handle ERROR msg =>
da70ab8531f4 more elementary management of declared hyps, below structure Assumption;
wenzelm
parents: 54756
diff changeset
   758
        cat_error msg
55523
9429e7b5b827 removed final periods in messages for proof methods
blanchet
parents: 55234
diff changeset
   759
          "Cannot replay Metis proof in Isabelle: error when discharging Skolem assumptions"
39964
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   760
    end
8ca95d819c7c move code from "Metis_Tactics" to "Metis_Reconstruct"
blanchet
parents: 39958
diff changeset
   761
39495
bb4fb9ffe2d1 added new "Metis_Reconstruct" module, temporarily empty
blanchet
parents:
diff changeset
   762
end;