src/HOL/Tools/Sledgehammer/sledgehammer_reconstruct.ML
author blanchet
Thu, 18 Oct 2012 13:19:44 +0200
changeset 49914 23e36a4d28f1
parent 49913 2e7d0655b176
child 49915 e88a864fa35c
permissions -rw-r--r--
refactor code
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
49883
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
     1
(*  Title:      HOL/Tools/Sledgehammer/sledgehammer_reconstruct.ML
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
     2
    Author:     Jasmin Blanchette, TU Muenchen
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
     3
    Author:     Steffen Juilf Smolka, TU Muenchen
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
     4
49914
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
     5
Isar proof reconstruction from ATP proofs.
49883
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
     6
*)
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
     7
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
     8
signature SLEDGEHAMMER_PROOF_RECONSTRUCT =
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
     9
sig
49914
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    10
  type 'a proof = 'a ATP_Proof.proof
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    11
  type stature = ATP_Problem_Generate.stature
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    12
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    13
  datatype reconstructor =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    14
    Metis of string * string |
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    15
    SMT
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    16
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    17
  datatype play =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    18
    Played of reconstructor * Time.time |
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    19
    Trust_Playable of reconstructor * Time.time option |
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    20
    Failed_to_Play of reconstructor
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    21
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    22
  type minimize_command = string list -> string
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    23
  type one_line_params =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    24
    play * string * (string * stature) list * minimize_command * int * int
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    25
  type isar_params =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    26
    bool * int * string Symtab.table * (string * stature) list vector
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    27
    * int Symtab.table * string proof * thm
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    28
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    29
  val smtN : string
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    30
  val string_for_reconstructor : reconstructor -> string
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    31
  val lam_trans_from_atp_proof : string proof -> string -> string
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    32
  val is_typed_helper_used_in_atp_proof : string proof -> bool
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    33
  val used_facts_in_atp_proof :
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    34
    Proof.context -> (string * stature) list vector -> string proof ->
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    35
    (string * stature) list
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    36
  val used_facts_in_unsound_atp_proof :
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    37
    Proof.context -> (string * stature) list vector -> 'a proof ->
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    38
    string list option
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    39
  val one_line_proof_text : int -> one_line_params -> string
49883
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
    40
  val isar_proof_text :
49913
blanchet
parents: 49883
diff changeset
    41
    Proof.context -> bool -> isar_params -> one_line_params -> string
49883
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
    42
  val proof_text :
49913
blanchet
parents: 49883
diff changeset
    43
    Proof.context -> bool -> isar_params -> int -> one_line_params -> string
49883
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
    44
end;
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
    45
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
    46
structure Sledgehammer_Reconstruct : SLEDGEHAMMER_PROOF_RECONSTRUCT =
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
    47
struct
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
    48
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
    49
open ATP_Util
49914
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    50
open ATP_Problem
49883
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
    51
open ATP_Proof
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
    52
open ATP_Problem_Generate
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
    53
open ATP_Proof_Reconstruct
49914
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    54
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    55
structure String_Redirect = ATP_Proof_Redirect(
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    56
  type key = step_name
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    57
  val ord = fn ((s, _ : string list), (s', _)) => fast_string_ord (s, s')
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    58
  val string_of = fst)
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    59
49883
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
    60
open String_Redirect
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
    61
49914
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    62
(** reconstructors **)
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    63
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    64
datatype reconstructor =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    65
  Metis of string * string |
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    66
  SMT
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    67
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    68
datatype play =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    69
  Played of reconstructor * Time.time |
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    70
  Trust_Playable of reconstructor * Time.time option |
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    71
  Failed_to_Play of reconstructor
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    72
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    73
val smtN = "smt"
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    74
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    75
fun string_for_reconstructor (Metis (type_enc, lam_trans)) =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    76
    metis_call type_enc lam_trans
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    77
  | string_for_reconstructor SMT = smtN
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    78
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    79
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    80
(** fact extraction from ATP proofs **)
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    81
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    82
fun find_first_in_list_vector vec key =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    83
  Vector.foldl (fn (ps, NONE) => AList.lookup (op =) ps key
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    84
                 | (_, value) => value) NONE vec
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    85
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    86
val unprefix_fact_number = space_implode "_" o tl o space_explode "_"
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    87
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    88
fun resolve_one_named_fact fact_names s =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    89
  case try (unprefix fact_prefix) s of
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    90
    SOME s' =>
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    91
    let val s' = s' |> unprefix_fact_number |> unascii_of in
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    92
      s' |> find_first_in_list_vector fact_names |> Option.map (pair s')
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    93
    end
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    94
  | NONE => NONE
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    95
fun resolve_fact fact_names = map_filter (resolve_one_named_fact fact_names)
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    96
fun is_fact fact_names = not o null o resolve_fact fact_names
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    97
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    98
fun resolve_one_named_conjecture s =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
    99
  case try (unprefix conjecture_prefix) s of
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   100
    SOME s' => Int.fromString s'
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   101
  | NONE => NONE
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   102
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   103
val resolve_conjecture = map_filter resolve_one_named_conjecture
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   104
val is_conjecture = not o null o resolve_conjecture
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   105
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   106
val ascii_of_lam_fact_prefix = ascii_of lam_fact_prefix
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   107
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   108
(* overapproximation (good enough) *)
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   109
fun is_lam_lifted s =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   110
  String.isPrefix fact_prefix s andalso
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   111
  String.isSubstring ascii_of_lam_fact_prefix s
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   112
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   113
val is_combinator_def = String.isPrefix (helper_prefix ^ combinator_prefix)
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   114
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   115
fun is_axiom_used_in_proof pred =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   116
  exists (fn Inference_Step ((_, ss), _, _, []) => exists pred ss | _ => false)
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   117
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   118
fun lam_trans_from_atp_proof atp_proof default =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   119
  case (is_axiom_used_in_proof is_combinator_def atp_proof,
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   120
        is_axiom_used_in_proof is_lam_lifted atp_proof) of
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   121
    (false, false) => default
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   122
  | (false, true) => liftingN
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   123
(*  | (true, true) => combs_and_liftingN -- not supported by "metis" *)
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   124
  | (true, _) => combsN
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   125
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   126
val is_typed_helper_name =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   127
  String.isPrefix helper_prefix andf String.isSuffix typed_helper_suffix
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   128
fun is_typed_helper_used_in_atp_proof atp_proof =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   129
  is_axiom_used_in_proof is_typed_helper_name atp_proof
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   130
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   131
fun add_non_rec_defs fact_names accum =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   132
  Vector.foldl (fn (facts, facts') =>
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   133
      union (op =) (filter (fn (_, (_, status)) => status = Non_Rec_Def) facts)
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   134
            facts')
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   135
    accum fact_names
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   136
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   137
val isa_ext = Thm.get_name_hint @{thm ext}
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   138
val isa_short_ext = Long_Name.base_name isa_ext
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   139
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   140
fun ext_name ctxt =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   141
  if Thm.eq_thm_prop (@{thm ext},
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   142
         singleton (Attrib.eval_thms ctxt) (Facts.named isa_short_ext, [])) then
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   143
    isa_short_ext
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   144
  else
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   145
    isa_ext
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   146
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   147
val leo2_ext = "extcnf_equal_neg"
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   148
val leo2_unfold_def = "unfold_def"
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   149
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   150
fun add_fact ctxt fact_names (Inference_Step ((_, ss), _, rule, deps)) =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   151
    (if rule = leo2_ext then
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   152
       insert (op =) (ext_name ctxt, (Global, General))
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   153
     else if rule = leo2_unfold_def then
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   154
       (* LEO 1.3.3 does not record definitions properly, leading to missing
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   155
         dependencies in the TSTP proof. Remove the next line once this is
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   156
         fixed. *)
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   157
       add_non_rec_defs fact_names
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   158
     else if rule = satallax_coreN then
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   159
       (fn [] =>
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   160
           (* Satallax doesn't include definitions in its unsatisfiable cores,
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   161
              so we assume the worst and include them all here. *)
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   162
           [(ext_name ctxt, (Global, General))] |> add_non_rec_defs fact_names
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   163
         | facts => facts)
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   164
     else
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   165
       I)
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   166
    #> (if null deps then union (op =) (resolve_fact fact_names ss)
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   167
        else I)
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   168
  | add_fact _ _ _ = I
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   169
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   170
fun used_facts_in_atp_proof ctxt fact_names atp_proof =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   171
  if null atp_proof then Vector.foldl (uncurry (union (op =))) [] fact_names
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   172
  else fold (add_fact ctxt fact_names) atp_proof []
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   173
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   174
fun used_facts_in_unsound_atp_proof _ _ [] = NONE
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   175
  | used_facts_in_unsound_atp_proof ctxt fact_names atp_proof =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   176
    let val used_facts = used_facts_in_atp_proof ctxt fact_names atp_proof in
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   177
      if forall (fn (_, (sc, _)) => sc = Global) used_facts andalso
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   178
         not (is_axiom_used_in_proof (is_conjecture o single) atp_proof) then
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   179
        SOME (map fst used_facts)
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   180
      else
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   181
        NONE
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   182
    end
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   183
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   184
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   185
(** one-liner reconstructor proofs **)
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   186
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   187
fun string_for_label (s, num) = s ^ string_of_int num
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   188
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   189
fun show_time NONE = ""
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   190
  | show_time (SOME ext_time) = " (" ^ string_from_ext_time ext_time ^ ")"
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   191
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   192
fun unusing_chained_facts _ 0 = ""
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   193
  | unusing_chained_facts used_chaineds num_chained =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   194
    if length used_chaineds = num_chained then ""
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   195
    else if null used_chaineds then "(* using no facts *) "
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   196
    else "(* using only " ^ space_implode " " used_chaineds ^ " *) "
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   197
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   198
fun apply_on_subgoal _ 1 = "by "
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   199
  | apply_on_subgoal 1 _ = "apply "
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   200
  | apply_on_subgoal i n =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   201
    "prefer " ^ string_of_int i ^ " " ^ apply_on_subgoal 1 n
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   202
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   203
fun using_labels [] = ""
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   204
  | using_labels ls =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   205
    "using " ^ space_implode " " (map string_for_label ls) ^ " "
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   206
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   207
fun command_call name [] =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   208
    name |> not (Lexicon.is_identifier name) ? enclose "(" ")"
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   209
  | command_call name args = "(" ^ name ^ " " ^ space_implode " " args ^ ")"
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   210
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   211
fun reconstructor_command reconstr i n used_chaineds num_chained (ls, ss) =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   212
  unusing_chained_facts used_chaineds num_chained ^
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   213
  using_labels ls ^ apply_on_subgoal i n ^
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   214
  command_call (string_for_reconstructor reconstr) ss
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   215
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   216
fun try_command_line banner time command =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   217
  banner ^ ": " ^ Markup.markup Isabelle_Markup.sendback command ^
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   218
  show_time time ^ "."
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   219
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   220
fun minimize_line _ [] = ""
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   221
  | minimize_line minimize_command ss =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   222
    case minimize_command ss of
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   223
      "" => ""
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   224
    | command =>
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   225
      "\nTo minimize: " ^ Markup.markup Isabelle_Markup.sendback command ^ "."
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   226
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   227
fun split_used_facts facts =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   228
  facts |> List.partition (fn (_, (sc, _)) => sc = Chained)
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   229
        |> pairself (sort_distinct (string_ord o pairself fst))
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   230
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   231
type minimize_command = string list -> string
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   232
type one_line_params =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   233
  play * string * (string * stature) list * minimize_command * int * int
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   234
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   235
fun one_line_proof_text num_chained
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   236
        (preplay, banner, used_facts, minimize_command, subgoal,
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   237
         subgoal_count) =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   238
  let
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   239
    val (chained, extra) = split_used_facts used_facts
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   240
    val (failed, reconstr, ext_time) =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   241
      case preplay of
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   242
        Played (reconstr, time) => (false, reconstr, (SOME (false, time)))
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   243
      | Trust_Playable (reconstr, time) =>
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   244
        (false, reconstr,
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   245
         case time of
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   246
           NONE => NONE
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   247
         | SOME time =>
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   248
           if time = Time.zeroTime then NONE else SOME (true, time))
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   249
      | Failed_to_Play reconstr => (true, reconstr, NONE)
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   250
    val try_line =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   251
      ([], map fst extra)
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   252
      |> reconstructor_command reconstr subgoal subgoal_count (map fst chained)
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   253
                               num_chained
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   254
      |> (if failed then
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   255
            enclose "One-line proof reconstruction failed: "
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   256
                     ".\n(Invoking \"sledgehammer\" with \"[strict]\" might \
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   257
                     \solve this.)"
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   258
          else
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   259
            try_command_line banner ext_time)
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   260
  in try_line ^ minimize_line minimize_command (map fst (extra @ chained)) end
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   261
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   262
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   263
(** Isar proof construction and manipulation **)
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   264
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   265
type label = string * int
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   266
type facts = label list * string list
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   267
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   268
datatype isar_qualifier = Show | Then | Moreover | Ultimately
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   269
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   270
datatype isar_step =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   271
  Fix of (string * typ) list |
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   272
  Let of term * term |
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   273
  Assume of label * term |
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   274
  Prove of isar_qualifier list * label * term * byline
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   275
and byline =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   276
  By_Metis of facts |
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   277
  Case_Split of isar_step list list * facts
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   278
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   279
val assum_prefix = "a"
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   280
val have_prefix = "f"
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   281
val raw_prefix = "x"
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   282
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   283
fun raw_label_for_name (num, ss) =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   284
  case resolve_conjecture ss of
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   285
    [j] => (conjecture_prefix, j)
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   286
  | _ => (raw_prefix ^ ascii_of num, 0)
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   287
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   288
fun add_fact_from_dependency fact_names (name as (_, ss)) =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   289
  if is_fact fact_names ss then
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   290
    apsnd (union (op =) (map fst (resolve_fact fact_names ss)))
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   291
  else
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   292
    apfst (insert (op =) (raw_label_for_name name))
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   293
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   294
fun repair_name "$true" = "c_True"
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   295
  | repair_name "$false" = "c_False"
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   296
  | repair_name "$$e" = tptp_equal (* seen in Vampire proofs *)
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   297
  | repair_name s =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   298
    if is_tptp_equal s orelse
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   299
       (* seen in Vampire proofs *)
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   300
       (String.isPrefix "sQ" s andalso String.isSuffix "_eqProxy" s) then
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   301
      tptp_equal
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   302
    else
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   303
      s
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   304
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   305
fun unvarify_term (Var ((s, 0), T)) = Free (s, T)
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   306
  | unvarify_term t = raise TERM ("unvarify_term: non-Var", [t])
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   307
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   308
fun infer_formula_types ctxt =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   309
  Type.constraint HOLogic.boolT
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   310
  #> Syntax.check_term
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   311
         (Proof_Context.set_mode Proof_Context.mode_schematic ctxt)
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   312
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   313
val combinator_table =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   314
  [(@{const_name Meson.COMBI}, @{thm Meson.COMBI_def [abs_def]}),
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   315
   (@{const_name Meson.COMBK}, @{thm Meson.COMBK_def [abs_def]}),
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   316
   (@{const_name Meson.COMBB}, @{thm Meson.COMBB_def [abs_def]}),
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   317
   (@{const_name Meson.COMBC}, @{thm Meson.COMBC_def [abs_def]}),
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   318
   (@{const_name Meson.COMBS}, @{thm Meson.COMBS_def [abs_def]})]
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   319
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   320
fun uncombine_term thy =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   321
  let
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   322
    fun aux (t1 $ t2) = betapply (pairself aux (t1, t2))
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   323
      | aux (Abs (s, T, t')) = Abs (s, T, aux t')
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   324
      | aux (t as Const (x as (s, _))) =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   325
        (case AList.lookup (op =) combinator_table s of
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   326
           SOME thm => thm |> prop_of |> specialize_type thy x
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   327
                           |> Logic.dest_equals |> snd
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   328
         | NONE => t)
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   329
      | aux t = t
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   330
  in aux end
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   331
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   332
fun decode_line sym_tab (Definition_Step (name, phi1, phi2)) ctxt =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   333
    let
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   334
      val thy = Proof_Context.theory_of ctxt
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   335
      val t1 = prop_from_atp ctxt true sym_tab phi1
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   336
      val vars = snd (strip_comb t1)
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   337
      val frees = map unvarify_term vars
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   338
      val unvarify_args = subst_atomic (vars ~~ frees)
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   339
      val t2 = prop_from_atp ctxt true sym_tab phi2
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   340
      val (t1, t2) =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   341
        HOLogic.eq_const HOLogic.typeT $ t1 $ t2
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   342
        |> unvarify_args |> uncombine_term thy |> infer_formula_types ctxt
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   343
        |> HOLogic.dest_eq
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   344
    in
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   345
      (Definition_Step (name, t1, t2),
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   346
       fold Variable.declare_term (maps Misc_Legacy.term_frees [t1, t2]) ctxt)
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   347
    end
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   348
  | decode_line sym_tab (Inference_Step (name, u, rule, deps)) ctxt =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   349
    let
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   350
      val thy = Proof_Context.theory_of ctxt
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   351
      val t = u |> prop_from_atp ctxt true sym_tab
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   352
                |> uncombine_term thy |> infer_formula_types ctxt
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   353
    in
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   354
      (Inference_Step (name, t, rule, deps),
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   355
       fold Variable.declare_term (Misc_Legacy.term_frees t) ctxt)
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   356
    end
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   357
fun decode_lines ctxt sym_tab lines =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   358
  fst (fold_map (decode_line sym_tab) lines ctxt)
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   359
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   360
fun replace_one_dependency (old, new) dep =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   361
  if is_same_atp_step dep old then new else [dep]
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   362
fun replace_dependencies_in_line _ (line as Definition_Step _) = line
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   363
  | replace_dependencies_in_line p (Inference_Step (name, t, rule, deps)) =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   364
    Inference_Step (name, t, rule,
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   365
                    fold (union (op =) o replace_one_dependency p) deps [])
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   366
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   367
(* No "real" literals means only type information (tfree_tcs, clsrel, or
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   368
   clsarity). *)
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   369
fun is_only_type_information t = t aconv @{term True}
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   370
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   371
fun is_same_inference _ (Definition_Step _) = false
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   372
  | is_same_inference t (Inference_Step (_, t', _, _)) = t aconv t'
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   373
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   374
(* Discard facts; consolidate adjacent lines that prove the same formula, since
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   375
   they differ only in type information.*)
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   376
fun add_line _ (line as Definition_Step _) lines = line :: lines
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   377
  | add_line fact_names (Inference_Step (name as (_, ss), t, rule, [])) lines =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   378
    (* No dependencies: fact, conjecture, or (for Vampire) internal facts or
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   379
       definitions. *)
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   380
    if is_fact fact_names ss then
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   381
      (* Facts are not proof lines. *)
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   382
      if is_only_type_information t then
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   383
        map (replace_dependencies_in_line (name, [])) lines
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   384
      (* Is there a repetition? If so, replace later line by earlier one. *)
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   385
      else case take_prefix (not o is_same_inference t) lines of
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   386
        (_, []) => lines (* no repetition of proof line *)
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   387
      | (pre, Inference_Step (name', _, _, _) :: post) =>
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   388
        pre @ map (replace_dependencies_in_line (name', [name])) post
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   389
      | _ => raise Fail "unexpected inference"
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   390
    else if is_conjecture ss then
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   391
      Inference_Step (name, t, rule, []) :: lines
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   392
    else
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   393
      map (replace_dependencies_in_line (name, [])) lines
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   394
  | add_line _ (Inference_Step (name, t, rule, deps)) lines =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   395
    (* Type information will be deleted later; skip repetition test. *)
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   396
    if is_only_type_information t then
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   397
      Inference_Step (name, t, rule, deps) :: lines
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   398
    (* Is there a repetition? If so, replace later line by earlier one. *)
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   399
    else case take_prefix (not o is_same_inference t) lines of
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   400
      (* FIXME: Doesn't this code risk conflating proofs involving different
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   401
         types? *)
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   402
       (_, []) => Inference_Step (name, t, rule, deps) :: lines
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   403
     | (pre, Inference_Step (name', t', rule, _) :: post) =>
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   404
       Inference_Step (name, t', rule, deps) ::
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   405
       pre @ map (replace_dependencies_in_line (name', [name])) post
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   406
     | _ => raise Fail "unexpected inference"
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   407
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   408
val waldmeister_conjecture_num = "1.0.0.0"
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   409
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   410
val repair_waldmeister_endgame =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   411
  let
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   412
    fun do_tail (Inference_Step (name, t, rule, deps)) =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   413
        Inference_Step (name, s_not t, rule, deps)
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   414
      | do_tail line = line
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   415
    fun do_body [] = []
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   416
      | do_body ((line as Inference_Step ((num, _), _, _, _)) :: lines) =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   417
        if num = waldmeister_conjecture_num then map do_tail (line :: lines)
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   418
        else line :: do_body lines
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   419
      | do_body (line :: lines) = line :: do_body lines
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   420
  in do_body end
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   421
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   422
(* Recursively delete empty lines (type information) from the proof. *)
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   423
fun add_nontrivial_line (line as Inference_Step (name, t, _, [])) lines =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   424
    if is_only_type_information t then delete_dependency name lines
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   425
    else line :: lines
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   426
  | add_nontrivial_line line lines = line :: lines
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   427
and delete_dependency name lines =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   428
  fold_rev add_nontrivial_line
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   429
           (map (replace_dependencies_in_line (name, [])) lines) []
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   430
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   431
(* ATPs sometimes reuse free variable names in the strangest ways. Removing
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   432
   offending lines often does the trick. *)
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   433
fun is_bad_free frees (Free x) = not (member (op =) frees x)
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   434
  | is_bad_free _ _ = false
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   435
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   436
fun add_desired_line _ _ _ (line as Definition_Step (name, _, _)) (j, lines) =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   437
    (j, line :: map (replace_dependencies_in_line (name, [])) lines)
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   438
  | add_desired_line isar_shrink_factor fact_names frees
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   439
        (Inference_Step (name as (_, ss), t, rule, deps)) (j, lines) =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   440
    (j + 1,
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   441
     if is_fact fact_names ss orelse
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   442
        is_conjecture ss orelse
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   443
        (* the last line must be kept *)
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   444
        j = 0 orelse
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   445
        (not (is_only_type_information t) andalso
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   446
         null (Term.add_tvars t []) andalso
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   447
         not (exists_subterm (is_bad_free frees) t) andalso
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   448
         length deps >= 2 andalso j mod isar_shrink_factor = 0 andalso
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   449
         (* kill next to last line, which usually results in a trivial step *)
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   450
         j <> 1) then
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   451
       Inference_Step (name, t, rule, deps) :: lines  (* keep line *)
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   452
     else
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   453
       map (replace_dependencies_in_line (name, deps)) lines)  (* drop line *)
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   454
49883
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   455
(** Type annotations **)
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   456
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   457
fun post_traverse_term_type' f _ (t as Const (_, T)) s = f t T s
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   458
  | post_traverse_term_type' f _ (t as Free (_, T)) s = f t T s
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   459
  | post_traverse_term_type' f _ (t as Var (_, T)) s = f t T s
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   460
  | post_traverse_term_type' f env (t as Bound i) s = f t (nth env i) s
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   461
  | post_traverse_term_type' f env (Abs (x, T1, b)) s =
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   462
    let
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   463
      val ((b', s'), T2) = post_traverse_term_type' f (T1 :: env) b s
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   464
    in f (Abs (x, T1, b')) (T1 --> T2) s' end
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   465
  | post_traverse_term_type' f env (u $ v) s =
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   466
    let
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   467
      val ((u', s'), Type (_, [_, T])) = post_traverse_term_type' f env u s
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   468
      val ((v', s''), _) = post_traverse_term_type' f env v s'
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   469
    in f (u' $ v') T s'' end
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   470
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   471
fun post_traverse_term_type f s t =
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   472
  post_traverse_term_type' (fn t => fn T => fn s => (f t T s, T)) [] t s |> fst
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   473
fun post_fold_term_type f s t =
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   474
  post_traverse_term_type (fn t => fn T => fn s => (t, f t T s)) s t |> snd
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   475
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   476
(* Data structures, orders *)
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   477
val cost_ord = prod_ord int_ord (prod_ord int_ord int_ord)
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   478
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   479
structure Var_Set_Tab = Table(
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   480
  type key = indexname list
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   481
  val ord = list_ord Term_Ord.fast_indexname_ord)
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   482
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   483
(* (1) Generalize Types *)
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   484
fun generalize_types ctxt t =
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   485
  t |> map_types (fn _ => dummyT)
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   486
    |> Syntax.check_term
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   487
         (Proof_Context.set_mode Proof_Context.mode_pattern ctxt)
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   488
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   489
(* (2) Typing-spot Table *)
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   490
local
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   491
fun key_of_atype (TVar (idxn, _)) =
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   492
    Ord_List.insert Term_Ord.fast_indexname_ord idxn
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   493
  | key_of_atype _ = I
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   494
fun key_of_type T = fold_atyps key_of_atype T []
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   495
fun update_tab t T (tab, pos) =
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   496
  (case key_of_type T of
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   497
     [] => tab
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   498
   | key =>
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   499
     let val cost = (size_of_typ T, (size_of_term t, pos)) in
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   500
       case Var_Set_Tab.lookup tab key of
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   501
         NONE => Var_Set_Tab.update_new (key, cost) tab
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   502
       | SOME old_cost =>
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   503
         (case cost_ord (cost, old_cost) of
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   504
            LESS => Var_Set_Tab.update (key, cost) tab
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   505
          | _ => tab)
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   506
     end,
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   507
   pos + 1)
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   508
in
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   509
val typing_spot_table =
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   510
  post_fold_term_type update_tab (Var_Set_Tab.empty, 0) #> fst
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   511
end
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   512
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   513
(* (3) Reverse-Greedy *)
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   514
fun reverse_greedy typing_spot_tab =
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   515
  let
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   516
    fun update_count z =
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   517
      fold (fn tvar => fn tab =>
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   518
        let val c = Vartab.lookup tab tvar |> the_default 0 in
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   519
          Vartab.update (tvar, c + z) tab
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   520
        end)
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   521
    fun superfluous tcount =
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   522
      forall (fn tvar => the (Vartab.lookup tcount tvar) > 1)
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   523
    fun drop_superfluous (tvars, (_, (_, spot))) (spots, tcount) =
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   524
      if superfluous tcount tvars then (spots, update_count ~1 tvars tcount)
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   525
      else (spot :: spots, tcount)
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   526
    val (typing_spots, tvar_count_tab) =
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   527
      Var_Set_Tab.fold
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   528
        (fn kv as (k, _) => apfst (cons kv) #> apsnd (update_count 1 k))
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   529
        typing_spot_tab ([], Vartab.empty)
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   530
      |>> sort_distinct (rev_order o cost_ord o pairself snd)
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   531
  in fold drop_superfluous typing_spots ([], tvar_count_tab) |> fst end
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   532
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   533
(* (4) Introduce Annotations *)
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   534
fun introduce_annotations thy spots t t' =
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   535
  let
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   536
    val get_types = post_fold_term_type (K cons) []
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   537
    fun match_types tp =
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   538
      fold (Sign.typ_match thy) (op ~~ (pairself get_types tp)) Vartab.empty
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   539
    fun unica' b x [] = if b then [x] else []
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   540
      | unica' b x (y :: ys) =
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   541
        if x = y then unica' false x ys
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   542
        else unica' true y ys |> b ? cons x
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   543
    fun unica ord xs =
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   544
      case sort ord xs of x :: ys => unica' true x ys | [] => []
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   545
    val add_all_tfree_namesT = fold_atyps (fn TFree (x, _) => cons x | _ => I)
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   546
    fun erase_unica_tfrees env =
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   547
      let
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   548
        val unica =
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   549
          Vartab.fold (add_all_tfree_namesT o snd o snd) env []
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   550
          |> unica fast_string_ord
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   551
        val erase_unica = map_atyps
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   552
          (fn T as TFree (s, _) =>
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   553
              if Ord_List.member fast_string_ord unica s then dummyT else T
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   554
            | T => T)
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   555
      in Vartab.map (K (apsnd erase_unica)) env end
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   556
    val env = match_types (t', t) |> erase_unica_tfrees
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   557
    fun get_annot env (TFree _) = (false, (env, dummyT))
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   558
      | get_annot env (T as TVar (v, S)) =
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   559
        let val T' = Envir.subst_type env T in
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   560
          if T' = dummyT then (false, (env, dummyT))
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   561
          else (true, (Vartab.update (v, (S, dummyT)) env, T'))
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   562
        end
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   563
      | get_annot env (Type (S, Ts)) =
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   564
        (case fold_rev (fn T => fn (b, (env, Ts)) =>
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   565
                  let
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   566
                    val (b', (env', T)) = get_annot env T
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   567
                  in (b orelse b', (env', T :: Ts)) end)
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   568
                Ts (false, (env, [])) of
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   569
           (true, (env', Ts)) => (true, (env', Type (S, Ts)))
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   570
         | (false, (env', _)) => (false, (env', dummyT)))
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   571
    fun post1 _ T (env, cp, ps as p :: ps', annots) =
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   572
        if p <> cp then
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   573
          (env, cp + 1, ps, annots)
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   574
        else
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   575
          let val (_, (env', T')) = get_annot env T in
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   576
            (env', cp + 1, ps', (p, T') :: annots)
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   577
          end
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   578
      | post1 _ _ accum = accum
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   579
    val (_, _, _, annots) = post_fold_term_type post1 (env, 0, spots, []) t'
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   580
    fun post2 t _ (cp, annots as (p, T) :: annots') =
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   581
        if p <> cp then (t, (cp + 1, annots))
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   582
        else (Type.constraint T t, (cp + 1, annots'))
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   583
      | post2 t _ x = (t, x)
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   584
  in post_traverse_term_type post2 (0, rev annots) t |> fst end
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   585
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   586
(* (5) Annotate *)
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   587
fun annotate_types ctxt t =
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   588
  let
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   589
    val thy = Proof_Context.theory_of ctxt
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   590
    val t' = generalize_types ctxt t
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   591
    val typing_spots =
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   592
      t' |> typing_spot_table
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   593
         |> reverse_greedy
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   594
         |> sort int_ord
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   595
  in introduce_annotations thy typing_spots t t' end
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   596
49914
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   597
val indent_size = 2
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   598
val no_label = ("", ~1)
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   599
49883
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   600
fun string_for_proof ctxt type_enc lam_trans i n =
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   601
  let
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   602
    fun fix_print_mode f x =
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   603
      Print_Mode.setmp (filter (curry (op =) Symbol.xsymbolsN)
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   604
                               (print_mode_value ())) f x
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   605
    fun do_indent ind = replicate_string (ind * indent_size) " "
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   606
    fun do_free (s, T) =
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   607
      maybe_quote s ^ " :: " ^
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   608
      maybe_quote (fix_print_mode (Syntax.string_of_typ ctxt) T)
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   609
    fun do_label l = if l = no_label then "" else string_for_label l ^ ": "
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   610
    fun do_have qs =
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   611
      (if member (op =) qs Moreover then "moreover " else "") ^
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   612
      (if member (op =) qs Ultimately then "ultimately " else "") ^
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   613
      (if member (op =) qs Then then
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   614
         if member (op =) qs Show then "thus" else "hence"
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   615
       else
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   616
         if member (op =) qs Show then "show" else "have")
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   617
    val do_term =
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   618
      maybe_quote o fix_print_mode (Syntax.string_of_term ctxt)
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   619
      o annotate_types ctxt
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   620
    val reconstr = Metis (type_enc, lam_trans)
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   621
    fun do_facts (ls, ss) =
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   622
      reconstructor_command reconstr 1 1 [] 0
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   623
          (ls |> sort_distinct (prod_ord string_ord int_ord),
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   624
           ss |> sort_distinct string_ord)
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   625
    and do_step ind (Fix xs) =
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   626
        do_indent ind ^ "fix " ^ space_implode " and " (map do_free xs) ^ "\n"
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   627
      | do_step ind (Let (t1, t2)) =
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   628
        do_indent ind ^ "let " ^ do_term t1 ^ " = " ^ do_term t2 ^ "\n"
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   629
      | do_step ind (Assume (l, t)) =
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   630
        do_indent ind ^ "assume " ^ do_label l ^ do_term t ^ "\n"
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   631
      | do_step ind (Prove (qs, l, t, By_Metis facts)) =
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   632
        do_indent ind ^ do_have qs ^ " " ^
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   633
        do_label l ^ do_term t ^ " " ^ do_facts facts ^ "\n"
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   634
      | do_step ind (Prove (qs, l, t, Case_Split (proofs, facts))) =
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   635
        implode (map (prefix (do_indent ind ^ "moreover\n") o do_block ind)
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   636
                     proofs) ^
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   637
        do_indent ind ^ do_have qs ^ " " ^ do_label l ^ do_term t ^ " " ^
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   638
        do_facts facts ^ "\n"
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   639
    and do_steps prefix suffix ind steps =
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   640
      let val s = implode (map (do_step ind) steps) in
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   641
        replicate_string (ind * indent_size - size prefix) " " ^ prefix ^
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   642
        String.extract (s, ind * indent_size,
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   643
                        SOME (size s - ind * indent_size - 1)) ^
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   644
        suffix ^ "\n"
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   645
      end
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   646
    and do_block ind proof = do_steps "{ " " }" (ind + 1) proof
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   647
    (* One-step proofs are pointless; better use the Metis one-liner
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   648
       directly. *)
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   649
    and do_proof [Prove (_, _, _, By_Metis _)] = ""
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   650
      | do_proof proof =
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   651
        (if i <> 1 then "prefer " ^ string_of_int i ^ "\n" else "") ^
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   652
        do_indent 0 ^ "proof -\n" ^ do_steps "" "" 1 proof ^ do_indent 0 ^
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   653
        (if n <> 1 then "next" else "qed")
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   654
  in do_proof end
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   655
49914
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   656
(* FIXME: Still needed? Try with SPASS proofs perhaps. *)
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   657
val kill_duplicate_assumptions_in_proof =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   658
  let
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   659
    fun relabel_facts subst =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   660
      apfst (map (fn l => AList.lookup (op =) subst l |> the_default l))
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   661
    fun do_step (step as Assume (l, t)) (proof, subst, assums) =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   662
        (case AList.lookup (op aconv) assums t of
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   663
           SOME l' => (proof, (l, l') :: subst, assums)
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   664
         | NONE => (step :: proof, subst, (t, l) :: assums))
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   665
      | do_step (Prove (qs, l, t, by)) (proof, subst, assums) =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   666
        (Prove (qs, l, t,
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   667
                case by of
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   668
                  By_Metis facts => By_Metis (relabel_facts subst facts)
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   669
                | Case_Split (proofs, facts) =>
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   670
                  Case_Split (map do_proof proofs,
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   671
                              relabel_facts subst facts)) ::
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   672
         proof, subst, assums)
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   673
      | do_step step (proof, subst, assums) = (step :: proof, subst, assums)
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   674
    and do_proof proof = fold do_step proof ([], [], []) |> #1 |> rev
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   675
  in do_proof end
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   676
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   677
fun used_labels_of_step (Prove (_, _, _, by)) =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   678
    (case by of
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   679
       By_Metis (ls, _) => ls
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   680
     | Case_Split (proofs, (ls, _)) =>
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   681
       fold (union (op =) o used_labels_of) proofs ls)
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   682
  | used_labels_of_step _ = []
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   683
and used_labels_of proof = fold (union (op =) o used_labels_of_step) proof []
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   684
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   685
fun kill_useless_labels_in_proof proof =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   686
  let
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   687
    val used_ls = used_labels_of proof
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   688
    fun do_label l = if member (op =) used_ls l then l else no_label
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   689
    fun do_step (Assume (l, t)) = Assume (do_label l, t)
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   690
      | do_step (Prove (qs, l, t, by)) =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   691
        Prove (qs, do_label l, t,
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   692
               case by of
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   693
                 Case_Split (proofs, facts) =>
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   694
                 Case_Split (map (map do_step) proofs, facts)
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   695
               | _ => by)
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   696
      | do_step step = step
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   697
  in map do_step proof end
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   698
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   699
fun prefix_for_depth n = replicate_string (n + 1)
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   700
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   701
val relabel_proof =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   702
  let
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   703
    fun aux _ _ _ [] = []
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   704
      | aux subst depth (next_assum, next_fact) (Assume (l, t) :: proof) =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   705
        if l = no_label then
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   706
          Assume (l, t) :: aux subst depth (next_assum, next_fact) proof
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   707
        else
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   708
          let val l' = (prefix_for_depth depth assum_prefix, next_assum) in
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   709
            Assume (l', t) ::
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   710
            aux ((l, l') :: subst) depth (next_assum + 1, next_fact) proof
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   711
          end
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   712
      | aux subst depth (next_assum, next_fact)
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   713
            (Prove (qs, l, t, by) :: proof) =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   714
        let
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   715
          val (l', subst, next_fact) =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   716
            if l = no_label then
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   717
              (l, subst, next_fact)
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   718
            else
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   719
              let
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   720
                val l' = (prefix_for_depth depth have_prefix, next_fact)
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   721
              in (l', (l, l') :: subst, next_fact + 1) end
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   722
          val relabel_facts =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   723
            apfst (maps (the_list o AList.lookup (op =) subst))
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   724
          val by =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   725
            case by of
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   726
              By_Metis facts => By_Metis (relabel_facts facts)
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   727
            | Case_Split (proofs, facts) =>
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   728
              Case_Split (map (aux subst (depth + 1) (1, 1)) proofs,
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   729
                          relabel_facts facts)
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   730
        in
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   731
          Prove (qs, l', t, by) :: aux subst depth (next_assum, next_fact) proof
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   732
        end
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   733
      | aux subst depth nextp (step :: proof) =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   734
        step :: aux subst depth nextp proof
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   735
  in aux [] 0 (1, 1) end
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   736
49913
blanchet
parents: 49883
diff changeset
   737
fun minimize_locally ctxt type_enc lam_trans proof =
49883
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   738
  let
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   739
    (* Merging spots, greedy algorithm *)
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   740
    fun cost (Prove (_, _ , t, _)) = Term.size_of_term t
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   741
      | cost _ = ~1
49913
blanchet
parents: 49883
diff changeset
   742
    fun can_merge (Prove (_, lbl, _, By_Metis _))
blanchet
parents: 49883
diff changeset
   743
                  (Prove (_, _, _, By_Metis _)) =
blanchet
parents: 49883
diff changeset
   744
        (lbl = no_label)
49883
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   745
      | can_merge _ _ = false
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   746
    val merge_spots = 
49913
blanchet
parents: 49883
diff changeset
   747
      fold_index (fn (i, s2) => fn (s1, pile) =>
blanchet
parents: 49883
diff changeset
   748
          (s2, pile |> can_merge s1 s2 ? cons (i, cost s1)))
49883
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   749
        (tl proof) (hd proof, [])
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   750
    |> snd |> sort (rev_order o int_ord o pairself snd) |> map fst
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   751
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   752
    (* Enrich context with facts *)
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   753
    val thy = Proof_Context.theory_of ctxt
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   754
    fun sorry t = Skip_Proof.make_thm thy (HOLogic.mk_Trueprop t) (* FIXME: mk_Trueprop always ok? *)
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   755
    fun enrich_ctxt' (Prove (_, lbl, t, _)) ctxt = 
49913
blanchet
parents: 49883
diff changeset
   756
        ctxt |> lbl <> no_label
blanchet
parents: 49883
diff changeset
   757
          ? Proof_Context.put_thms false (string_for_label lbl, SOME [sorry t])
49883
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   758
      | enrich_ctxt' _ ctxt = ctxt
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   759
    val rich_ctxt = fold enrich_ctxt' proof ctxt
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   760
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   761
    (* Timing *)
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   762
    fun take_time tac arg =
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   763
      let
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   764
        val t_start = Timing.start ()
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   765
      in
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   766
        (tac arg ; Timing.result t_start |> #cpu)
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   767
      end
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   768
    fun try_metis (Prove (qs, _, t, By_Metis fact_names)) s0 =
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   769
      let
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   770
        fun thmify (Prove (_, _, t, _)) = sorry t
49913
blanchet
parents: 49883
diff changeset
   771
        val facts =
blanchet
parents: 49883
diff changeset
   772
          fact_names
blanchet
parents: 49883
diff changeset
   773
          |>> map string_for_label
blanchet
parents: 49883
diff changeset
   774
          |> op @
blanchet
parents: 49883
diff changeset
   775
          |> map (Proof_Context.get_thm rich_ctxt)
blanchet
parents: 49883
diff changeset
   776
          |> (if member (op =) qs Then then cons (the s0 |> thmify) else I)
49883
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   777
        val goal = Goal.prove ctxt [] [] (HOLogic.mk_Trueprop t) (* FIXME: mk_Trueprop always ok? *)
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   778
        fun tac {context = ctxt, prems = _} =
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   779
          Metis_Tactic.metis_tac [type_enc] lam_trans ctxt facts 1
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   780
      in
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   781
        take_time (fn () => goal tac)
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   782
      end
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   783
  
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   784
    (* Merging *)
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   785
    fun merge (Prove (qs1, _, _, By_Metis (ls1, ss1))) 
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   786
              (Prove (qs2, lbl , t, By_Metis (ls2, ss2))) =
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   787
      let
49913
blanchet
parents: 49883
diff changeset
   788
        val qs =
blanchet
parents: 49883
diff changeset
   789
          inter (op =) qs1 qs2 (* FIXME: Is this correct? *)
blanchet
parents: 49883
diff changeset
   790
          |> member (op =) (union (op =) qs1 qs2) Ultimately ? cons Ultimately
blanchet
parents: 49883
diff changeset
   791
          |> member (op =) qs2 Show ? cons Show
blanchet
parents: 49883
diff changeset
   792
      in Prove (qs, lbl, t, By_Metis (ls1 @ ls2, ss1 @ ss2)) end
49883
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   793
    fun try_merge proof i =
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   794
      let
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   795
        val (front, s0, s1, s2, tail) = 
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   796
          case (proof, i) of
49913
blanchet
parents: 49883
diff changeset
   797
            ((s1 :: s2 :: proof), 0) => ([], NONE, s1, s2, proof)
blanchet
parents: 49883
diff changeset
   798
          | _ =>
blanchet
parents: 49883
diff changeset
   799
            let val (front, s0 :: s1 :: s2 :: tail) = chop (i - 1) proof in
blanchet
parents: 49883
diff changeset
   800
              (front, SOME s0, s1, s2, tail)
blanchet
parents: 49883
diff changeset
   801
            end
49883
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   802
        val s12 = merge s1 s2
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   803
        val t1 = try_metis s1 s0 ()
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   804
        val t2 = try_metis s2 (SOME s1) ()
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   805
        val tlimit = t1 + t2 |> Time.toReal |> curry Real.* 1.2 |> Time.fromReal
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   806
      in
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   807
        (TimeLimit.timeLimit tlimit (try_metis s12 s0) ();
49913
blanchet
parents: 49883
diff changeset
   808
         SOME (front @ (case s0 of
blanchet
parents: 49883
diff changeset
   809
                          NONE => s12 :: tail
blanchet
parents: 49883
diff changeset
   810
                        | SOME s => s :: s12 :: tail)))
49883
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   811
        handle _ => NONE
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   812
      end
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   813
    fun merge_steps proof [] = proof
49913
blanchet
parents: 49883
diff changeset
   814
      | merge_steps proof (i :: is) = 
49883
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   815
        case try_merge proof i of 
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   816
          NONE => merge_steps proof is
49913
blanchet
parents: 49883
diff changeset
   817
        | SOME proof' =>
blanchet
parents: 49883
diff changeset
   818
          merge_steps proof' (map (fn j => if j > i then j - 1 else j) is)
49883
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   819
  in merge_steps proof merge_spots end
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   820
49914
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   821
type isar_params =
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   822
  bool * int * string Symtab.table * (string * stature) list vector
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   823
  * int Symtab.table * string proof * thm
23e36a4d28f1 refactor code
blanchet
parents: 49913
diff changeset
   824
49883
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   825
fun isar_proof_text ctxt isar_proof_requested
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   826
        (debug, isar_shrink_factor, pool, fact_names, sym_tab, atp_proof, goal)
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   827
        (one_line_params as (_, _, _, _, subgoal, subgoal_count)) =
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   828
  let
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   829
    val isar_shrink_factor =
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   830
      (if isar_proof_requested then 1 else 2) * isar_shrink_factor
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   831
    val (params, hyp_ts, concl_t) = strip_subgoal ctxt goal subgoal
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   832
    val frees = fold Term.add_frees (concl_t :: hyp_ts) []
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   833
    val one_line_proof = one_line_proof_text 0 one_line_params
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   834
    val type_enc =
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   835
      if is_typed_helper_used_in_atp_proof atp_proof then full_typesN
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   836
      else partial_typesN
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   837
    val lam_trans = lam_trans_from_atp_proof atp_proof metis_default_lam_trans
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   838
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   839
    fun isar_proof_of () =
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   840
      let
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   841
        val atp_proof =
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   842
          atp_proof
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   843
          |> clean_up_atp_proof_dependencies
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   844
          |> nasty_atp_proof pool
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   845
          |> map_term_names_in_atp_proof repair_name
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   846
          |> decode_lines ctxt sym_tab
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   847
          |> rpair [] |-> fold_rev (add_line fact_names)
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   848
          |> repair_waldmeister_endgame
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   849
          |> rpair [] |-> fold_rev add_nontrivial_line
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   850
          |> rpair (0, [])
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   851
          |-> fold_rev (add_desired_line isar_shrink_factor fact_names frees)
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   852
          |> snd
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   853
        val conj_name = conjecture_prefix ^ string_of_int (length hyp_ts)
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   854
        val conjs =
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   855
          atp_proof
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   856
          |> map_filter (fn Inference_Step (name as (_, ss), _, _, []) =>
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   857
                            if member (op =) ss conj_name then SOME name else NONE
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   858
                          | _ => NONE)
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   859
        fun dep_of_step (Definition_Step _) = NONE
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   860
          | dep_of_step (Inference_Step (name, _, _, from)) = SOME (from, name)
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   861
        val ref_graph = atp_proof |> map_filter dep_of_step |> make_ref_graph
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   862
        val axioms = axioms_of_ref_graph ref_graph conjs
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   863
        val tainted = tainted_atoms_of_ref_graph ref_graph conjs
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   864
        val props =
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   865
          Symtab.empty
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   866
          |> fold (fn Definition_Step _ => I (* FIXME *)
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   867
                    | Inference_Step ((s, _), t, _, _) =>
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   868
                      Symtab.update_new (s,
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   869
                          t |> fold forall_of (map Var (Term.add_vars t []))
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   870
                            |> member (op = o apsnd fst) tainted s ? s_not))
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   871
                  atp_proof
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   872
        fun prop_of_clause c =
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   873
          fold (curry s_disj) (map_filter (Symtab.lookup props o fst) c)
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   874
               @{term False}
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   875
        fun label_of_clause [name] = raw_label_for_name name
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   876
          | label_of_clause c = (space_implode "___" (map fst c), 0)
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   877
        fun maybe_show outer c =
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   878
          (outer andalso length c = 1 andalso subset (op =) (c, conjs))
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   879
          ? cons Show
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   880
        fun do_have outer qs (gamma, c) =
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   881
          Prove (maybe_show outer c qs, label_of_clause c, prop_of_clause c,
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   882
                 By_Metis (fold (add_fact_from_dependency fact_names
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   883
                                 o the_single) gamma ([], [])))
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   884
        fun do_inf outer (Have z) = do_have outer [] z
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   885
          | do_inf outer (Hence z) = do_have outer [Then] z
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   886
          | do_inf outer (Cases cases) =
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   887
            let val c = succedent_of_cases cases in
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   888
              Prove (maybe_show outer c [Ultimately], label_of_clause c,
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   889
                     prop_of_clause c,
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   890
                     Case_Split (map (do_case false) cases, ([], [])))
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   891
            end
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   892
        and do_case outer (c, infs) =
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   893
          Assume (label_of_clause c, prop_of_clause c) ::
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   894
          map (do_inf outer) infs
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   895
        val isar_proof =
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   896
          (if null params then [] else [Fix params]) @
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   897
           (ref_graph
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   898
           |> redirect_graph axioms tainted
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   899
           |> chain_direct_proof
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   900
           |> map (do_inf true)
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   901
           |> kill_duplicate_assumptions_in_proof
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   902
           |> kill_useless_labels_in_proof
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   903
           |> relabel_proof
49913
blanchet
parents: 49883
diff changeset
   904
           |> minimize_locally ctxt type_enc lam_trans)
49883
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   905
          |> string_for_proof ctxt type_enc lam_trans subgoal subgoal_count
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   906
      in
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   907
        case isar_proof of
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   908
          "" =>
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   909
          if isar_proof_requested then
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   910
            "\nNo structured proof available (proof too short)."
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   911
          else
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   912
            ""
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   913
        | _ =>
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   914
          "\n\n" ^ (if isar_proof_requested then "Structured proof"
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   915
                    else "Perhaps this will work") ^
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   916
          ":\n" ^ Markup.markup Isabelle_Markup.sendback isar_proof
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   917
      end
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   918
    val isar_proof =
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   919
      if debug then
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   920
        isar_proof_of ()
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   921
      else case try isar_proof_of () of
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   922
        SOME s => s
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   923
      | NONE => if isar_proof_requested then
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   924
                  "\nWarning: The Isar proof construction failed."
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   925
                else
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   926
                  ""
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   927
  in one_line_proof ^ isar_proof end
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   928
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   929
fun proof_text ctxt isar_proof isar_params num_chained
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   930
               (one_line_params as (preplay, _, _, _, _, _)) =
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   931
  (if case preplay of Failed_to_Play _ => true | _ => isar_proof then
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   932
     isar_proof_text ctxt isar_proof isar_params
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   933
   else
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   934
     one_line_proof_text num_chained) one_line_params
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   935
a6ebdaf8e267 added missing file
blanchet
parents:
diff changeset
   936
end;