src/HOL/Tools/SMT/verit_replay.ML
author desharna
Fri, 11 Mar 2022 09:22:13 +0100
changeset 75274 e89709b80b6e
parent 72513 75f5c63f6cfa
child 75299 da591621d6ae
child 75365 83940294cc67
permissions -rw-r--r--
used more descriptive assert names in SMT-Lib output
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
69205
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
     1
(*  Title:      HOL/Tools/SMT/verit_replay.ML
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
     2
    Author:     Mathias Fleury, MPII
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
     3
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
     4
VeriT proof parsing and replay.
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
     5
*)
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
     6
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
     7
signature VERIT_REPLAY =
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
     8
sig
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
     9
  val replay: Proof.context -> SMT_Translate.replay_data -> string list -> thm
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
    10
end;
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
    11
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
    12
structure Verit_Replay: VERIT_REPLAY =
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
    13
struct
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
    14
72458
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
    15
fun subst_only_free pairs =
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
    16
  let
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
    17
     fun substf u =
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
    18
        (case Termtab.lookup pairs u of
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
    19
          SOME u' => u'
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
    20
        | NONE => 
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
    21
          (case u of
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
    22
            (Abs(a,T,t)) => Abs(a, T, substf t)
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
    23
          | (t$u') => substf t $ substf u'
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
    24
          | u => u))
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
    25
  in substf end;
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
    26
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
    27
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
    28
fun under_fixes f unchanged_prems (prems, nthms) names args insts decls (concl, ctxt) =
69205
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
    29
  let
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
    30
    val thms1 = unchanged_prems @ map (SMT_Replay.varify ctxt) prems
72458
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
    31
    val _ =  SMT_Config.verit_msg ctxt (fn () => \<^print>  ("names =", names))
69205
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
    32
    val thms2 = map snd nthms
72458
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
    33
    val _ = SMT_Config.verit_msg ctxt (fn () => \<^print> ("prems=", prems))
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
    34
    val _ = SMT_Config.verit_msg ctxt (fn () => \<^print> ("nthms=", nthms))
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
    35
    val _ = SMT_Config.verit_msg ctxt (fn () => \<^print> ("thms1=", thms1))
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
    36
    val _ = SMT_Config.verit_msg ctxt (fn () => \<^print> ("thms2=", thms2))
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
    37
  in (f ctxt (thms1 @ thms2) args insts decls concl) end
69205
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
    38
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
    39
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
    40
(** Replaying **)
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
    41
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
    42
fun replay_thm method_for rewrite_rules ll_defs ctxt assumed unchanged_prems prems nthms
72458
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
    43
    concl_transformation global_transformation args insts
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
    44
    (Verit_Proof.VeriT_Replay_Node {id, rule, concl, bounds, declarations = decls, ...}) =
69205
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
    45
  let
72458
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
    46
    val _ = SMT_Config.verit_msg ctxt (fn () => \<^print> id)
69205
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
    47
    val rewrite = let val thy = Proof_Context.theory_of (empty_simpset ctxt) in
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
    48
        Raw_Simplifier.rewrite_term thy rewrite_rules []
72458
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
    49
        #> not (null ll_defs andalso Verit_Proof.keep_raw_lifting rule) ? SMTLIB_Isar.unlift_term ll_defs
69205
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
    50
      end
72458
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
    51
    val rewrite_concl = if Verit_Proof.keep_app_symbols rule then
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
    52
          filter (curry Term.could_unify (Thm.concl_of @{thm SMT.fun_app_def}) o Thm.concl_of) rewrite_rules
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
    53
        else rewrite_rules
69205
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
    54
    val post = let val thy = Proof_Context.theory_of (empty_simpset ctxt) in
72458
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
    55
        Raw_Simplifier.rewrite_term thy rewrite_concl []
69205
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
    56
        #> Object_Logic.atomize_term ctxt
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
    57
        #> not (null ll_defs) ? SMTLIB_Isar.unlift_term ll_defs
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
    58
        #> SMTLIB_Isar.unskolemize_names ctxt
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
    59
        #> HOLogic.mk_Trueprop
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
    60
      end
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
    61
    val concl = concl
72458
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
    62
      |> Term.subst_free concl_transformation
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
    63
      |> subst_only_free global_transformation
69205
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
    64
      |> post
72458
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
    65
  in
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
    66
    if rule = Verit_Proof.input_rule then
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
    67
      (case Symtab.lookup assumed id of
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
    68
        SOME (_, thm) => thm
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
    69
      | _ => raise Fail ("assumption " ^ @{make_string} id ^ " not found"))
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
    70
    else
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
    71
      under_fixes (method_for rule) unchanged_prems
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
    72
        (prems, nthms) (map fst bounds)
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
    73
        (map rewrite args)
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
    74
        (Symtab.map (K rewrite) insts)
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
    75
        decls
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
    76
        (concl, ctxt)
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
    77
      |> Simplifier.simplify (empty_simpset ctxt addsimps rewrite_rules)
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
    78
  end
69205
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
    79
72458
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
    80
fun add_used_asserts_in_step (Verit_Proof.VeriT_Replay_Node {prems,
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
    81
    subproof = (_, _, _, subproof), ...}) =
75274
e89709b80b6e used more descriptive assert names in SMT-Lib output
desharna
parents: 72513
diff changeset
    82
  union (op =) (map_filter (try (snd o SMTLIB_Interface.role_and_index_of_assert_name)) prems @
69205
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
    83
     flat (map (fn x => add_used_asserts_in_step x []) subproof))
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
    84
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
    85
fun remove_rewrite_rules_from_rules n =
72458
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
    86
  (fn (step as Verit_Proof.VeriT_Replay_Node {id, ...}) =>
75274
e89709b80b6e used more descriptive assert names in SMT-Lib output
desharna
parents: 72513
diff changeset
    87
    (case try (snd o SMTLIB_Interface.role_and_index_of_assert_name) id of
69205
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
    88
      NONE => SOME step
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
    89
    | SOME a => if a < n then NONE else SOME step))
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
    90
72458
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
    91
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
    92
fun replay_theorem_step rewrite_rules ll_defs assumed inputs proof_prems
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
    93
  (step as Verit_Proof.VeriT_Replay_Node {id, rule, prems, bounds, args, insts,
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
    94
     subproof = (fixes, assms, input, subproof), concl, ...}) state =
69205
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
    95
  let
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
    96
    val (proofs, stats, ctxt, concl_tranformation, global_transformation) = state
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
    97
    val (_, ctxt) = Variable.variant_fixes (map fst bounds) ctxt
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
    98
      |> (fn (names, ctxt) => (names,
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
    99
        fold Variable.declare_term [SMTLIB_Isar.unskolemize_names ctxt concl] ctxt))
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
   100
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
   101
    val (names, sub_ctxt) = Variable.variant_fixes (map fst fixes) ctxt
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
   102
       ||> fold Variable.declare_term (map Free fixes)
72458
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   103
    val export_vars = concl_tranformation @
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   104
       (ListPair.zip (map Free fixes, map Free (ListPair.zip (names, map snd fixes))))
69205
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
   105
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
   106
    val post = let val thy = Proof_Context.theory_of (empty_simpset ctxt) in
72458
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   107
        Raw_Simplifier.rewrite_term thy ((if Verit_Proof.keep_raw_lifting rule then tl rewrite_rules else rewrite_rules)) []
69205
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
   108
        #> Object_Logic.atomize_term ctxt
72458
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   109
        #> not (null ll_defs andalso Verit_Proof.keep_raw_lifting rule) ? SMTLIB_Isar.unlift_term ll_defs
69205
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
   110
        #> SMTLIB_Isar.unskolemize_names ctxt
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
   111
        #> HOLogic.mk_Trueprop
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
   112
      end
72458
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   113
    val assms = map (subst_only_free global_transformation o Term.subst_free (export_vars) o post) assms
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   114
    val input = map (subst_only_free global_transformation o Term.subst_free (export_vars) o post) input
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   115
    val (all_proof_prems', sub_ctxt2) = Assumption.add_assumes (map (Thm.cterm_of sub_ctxt) (assms @ input))
69205
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
   116
      sub_ctxt
72458
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   117
    fun is_refl thm = Thm.concl_of thm |> (fn (_ $ t) => t) |> HOLogic.dest_eq |> (op =) handle TERM _=> false
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   118
    val all_proof_prems' =
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   119
        all_proof_prems'
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   120
        |> filter_out is_refl
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   121
    val proof_prems' = take (length assms) all_proof_prems'
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   122
    val input = drop (length assms) all_proof_prems'
69205
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
   123
    val all_proof_prems = proof_prems @ proof_prems'
72458
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   124
    val replay = replay_theorem_step rewrite_rules ll_defs assumed (input @ inputs) all_proof_prems
69205
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
   125
    val (proofs', stats, _, _, sub_global_rew) =
72458
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   126
       fold replay subproof (proofs, stats, sub_ctxt2, export_vars, global_transformation)
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   127
69205
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
   128
    val export_thm = singleton (Proof_Context.export sub_ctxt2 ctxt)
72458
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   129
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   130
    (*for sko_ex and sko_forall, assumptions are in proofs',  but the definition of the skolem
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   131
       function is in proofs *)
69205
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
   132
    val nthms = prems
72458
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   133
      |> map (apsnd export_thm) o map_filter (Symtab.lookup (if (null subproof) then proofs else proofs'))
72513
75f5c63f6cfa better handling of skolemization for Isar reconstruction in Sledgehammer for veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 72459
diff changeset
   134
    val nthms' = (if Verit_Proof.is_skolemization rule
72458
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   135
         then prems else [])
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   136
      |> map_filter (Symtab.lookup proofs)
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   137
    val args = map (Term.subst_free concl_tranformation o subst_only_free global_transformation) args
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   138
    val insts = Symtab.map (K (Term.subst_free concl_tranformation o subst_only_free global_transformation)) insts
69205
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
   139
    val proof_prems =
72459
15fc6320da68 reconstruction of veriT proofs in NEWS
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 72458
diff changeset
   140
       if Verit_Replay_Methods.requires_subproof_assms prems rule then proof_prems else []
72458
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   141
    val local_inputs =
72459
15fc6320da68 reconstruction of veriT proofs in NEWS
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 72458
diff changeset
   142
       if Verit_Replay_Methods.requires_local_input prems rule then input @ inputs else []
15fc6320da68 reconstruction of veriT proofs in NEWS
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 72458
diff changeset
   143
    val replay = Timing.timing (replay_thm Verit_Replay_Methods.method_for rewrite_rules ll_defs
72458
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   144
       ctxt assumed [] (proof_prems @ local_inputs) (nthms @ nthms') concl_tranformation
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   145
       global_transformation args insts)
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   146
69205
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
   147
    val ({elapsed, ...}, thm) =
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
   148
      SMT_Config.with_time_limit ctxt SMT_Config.reconstruction_step_timeout replay step
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
   149
        handle Timeout.TIMEOUT _ => raise SMT_Failure.SMT SMT_Failure.Time_Out
72458
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   150
    val stats' = Symtab.cons_list (rule, Time.toNanoseconds elapsed) stats
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   151
(*     val _ = ((Time.toMilliseconds elapsed > 10 andalso (rule = "cong")) ? @{print})
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   152
        ("WARNING slow " ^ id ^ @{make_string} rule ^ ": " ^ string_of_int (Time.toMilliseconds elapsed) ^ " "
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   153
         ^ @{make_string} (proof_prems @ local_inputs))
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   154
    val _ = ((Time.toMilliseconds elapsed > 10 andalso (rule = "cong")) ? @{print})
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   155
        ( (proof_prems @ local_inputs))
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   156
    val _ = ((Time.toMilliseconds elapsed > 10 andalso (rule = "cong")) ? @{print})
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   157
        thm
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   158
    val _ = ((Time.toMilliseconds elapsed > 40) ? @{print})
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   159
        ("WARNING slow " ^ id ^ @{make_string} rule ^ ": " ^ string_of_int (Time.toMilliseconds elapsed)) *)
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   160
    val proofs = Symtab.update (id, (map fst bounds, thm)) proofs
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   161
  in (proofs, stats', ctxt,
69205
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
   162
       concl_tranformation, sub_global_rew) end
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
   163
72458
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   164
fun replay_definition_step rewrite_rules ll_defs _ _ _
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   165
  (Verit_Proof.VeriT_Replay_Node {id, declarations = raw_declarations, subproof = (_, _, _, subproof), ...}) state =
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   166
  let
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   167
    val _ = if null subproof then ()
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   168
          else raise (Fail ("unrecognized veriT proof, definition has a subproof"))
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   169
    val (proofs, stats, ctxt, concl_tranformation, global_transformation) = state
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   170
    val global_transformer = subst_only_free global_transformation
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   171
    val rewrite = let val thy = Proof_Context.theory_of ctxt in
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   172
        Raw_Simplifier.rewrite_term thy (rewrite_rules) []
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   173
        #> not (null ll_defs) ? SMTLIB_Isar.unlift_term ll_defs
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   174
      end
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   175
    val start0 = Timing.start ()
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   176
    val declaration = map (apsnd (rewrite o global_transformer)) raw_declarations
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   177
    val (names, ctxt) = Variable.variant_fixes (map fst declaration) ctxt
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   178
       ||> fold Variable.declare_term (map Free (map (apsnd fastype_of) declaration))
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   179
    val old_names = map Free (map (fn (a, b) => (a, fastype_of b)) declaration)
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   180
    val new_names = map Free (ListPair.zip (names, map (fastype_of o snd) declaration))
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   181
    fun update_mapping (a, b) tab = 
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   182
          if a <> b andalso Termtab.lookup tab a = NONE
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   183
          then Termtab.update_new (a, b) tab else tab
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   184
    val global_transformation = global_transformation 
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   185
     |> fold update_mapping (ListPair.zip (old_names, new_names))
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   186
    val global_transformer = subst_only_free global_transformation
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   187
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   188
    val generate_definition =
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   189
      (fn (name, term) => (HOLogic.mk_Trueprop
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   190
        (Const(\<^const_name>\<open>HOL.eq\<close>, fastype_of term --> fastype_of term --> @{typ bool}) $
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   191
            Free (name, fastype_of term) $ term)))
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   192
      #> global_transformer
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   193
      #> Thm.cterm_of ctxt
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   194
    val decls = map generate_definition declaration
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   195
    val (defs, ctxt) = Assumption.add_assumes decls ctxt
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   196
    val thms_with_old_name = ListPair.zip (map fst declaration, defs)
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   197
    val proofs = fold
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   198
      (fn (name, thm) => Symtab.update (id, ([name], @{thm sym} OF [thm])))
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   199
      thms_with_old_name proofs
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   200
    val total = Time.toNanoseconds (#elapsed (Timing.result start0))
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   201
    val stats = Symtab.cons_list ("choice", total) stats
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   202
  in (proofs, stats, ctxt, concl_tranformation, global_transformation) end
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   203
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   204
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   205
fun replay_assumed assms ll_defs rewrite_rules stats ctxt term =
69205
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
   206
  let
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
   207
    val rewrite = let val thy = Proof_Context.theory_of (empty_simpset ctxt) in
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
   208
        Raw_Simplifier.rewrite_term thy rewrite_rules []
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
   209
        #> not (null ll_defs) ? SMTLIB_Isar.unlift_term ll_defs
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
   210
      end
72458
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   211
    val replay = Timing.timing (SMT_Replay_Methods.prove ctxt (rewrite term))
69205
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
   212
    val ({elapsed, ...}, thm) =
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
   213
      SMT_Config.with_time_limit ctxt SMT_Config.reconstruction_step_timeout replay
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
   214
         (fn _ => Method.insert_tac ctxt (map snd assms) THEN' Classical.fast_tac ctxt)
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
   215
        handle Timeout.TIMEOUT _ => raise SMT_Failure.SMT SMT_Failure.Time_Out
72458
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   216
    val stats' = Symtab.cons_list (Verit_Proof.input_rule, Time.toNanoseconds elapsed) stats
69205
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
   217
  in
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
   218
    (thm, stats')
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
   219
  end
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
   220
72458
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   221
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   222
fun replay_step rewrite_rules ll_defs assumed inputs proof_prems
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   223
  (step as Verit_Proof.VeriT_Replay_Node {rule, ...}) state =
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   224
  if rule = Verit_Proof.veriT_def
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   225
  then replay_definition_step rewrite_rules ll_defs assumed inputs proof_prems step state
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   226
  else replay_theorem_step rewrite_rules ll_defs assumed inputs proof_prems step state
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   227
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   228
69205
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
   229
fun replay outer_ctxt
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
   230
    ({context = ctxt, typs, terms, rewrite_rules, assms, ll_defs, ...} : SMT_Translate.replay_data)
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
   231
     output =
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
   232
  let
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
   233
    val rewrite_rules =
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
   234
      filter_out (fn thm => Term.could_unify (Thm.prop_of @{thm verit_eq_true_simplify},
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
   235
          Thm.prop_of thm))
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
   236
        rewrite_rules
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
   237
    val num_ll_defs = length ll_defs
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
   238
    val index_of_id = Integer.add (~ num_ll_defs)
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
   239
    val id_of_index = Integer.add num_ll_defs
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
   240
72458
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   241
    val start0 = Timing.start ()
69205
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
   242
    val (actual_steps, ctxt2) =
72458
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   243
      Verit_Proof.parse_replay typs terms output ctxt
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   244
    val parsing_time = Time.toNanoseconds (#elapsed (Timing.result start0))
69205
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
   245
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
   246
    fun step_of_assume (j, (_, th)) =
72458
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   247
      Verit_Proof.VeriT_Replay_Node {
75274
e89709b80b6e used more descriptive assert names in SMT-Lib output
desharna
parents: 72513
diff changeset
   248
        id = SMTLIB_Interface.assert_name_of_role_and_index SMT_Util.Axiom (id_of_index j),
72458
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   249
        rule = Verit_Proof.input_rule,
69205
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
   250
        args = [],
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
   251
        prems = [],
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
   252
        proof_ctxt = [],
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
   253
        concl = Thm.prop_of th
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
   254
          |> Raw_Simplifier.rewrite_term (Proof_Context.theory_of
72458
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   255
               (empty_simpset ctxt addsimps rewrite_rules)) rewrite_rules [],
69205
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
   256
        bounds = [],
72458
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   257
        insts = Symtab.empty,
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   258
        declarations = [],
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   259
        subproof = ([], [], [], [])}
69205
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
   260
    val used_assert_ids = fold add_used_asserts_in_step actual_steps []
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
   261
    fun normalize_tac ctxt = let val thy = Proof_Context.theory_of (empty_simpset ctxt) in
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
   262
      Raw_Simplifier.rewrite_term thy rewrite_rules [] end
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
   263
    val used_assm_js =
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
   264
      map_filter (fn id => let val i = index_of_id id in if i >= 0 then SOME (i, nth assms i)
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
   265
          else NONE end)
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
   266
        used_assert_ids
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
   267
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
   268
    val assm_steps = map step_of_assume used_assm_js
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
   269
72458
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   270
    fun extract (Verit_Proof.VeriT_Replay_Node {id, rule, concl, bounds, ...}) =
69205
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
   271
         (id, rule, concl, map fst bounds)
72458
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   272
    fun cond rule = rule = Verit_Proof.input_rule
69205
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
   273
    val add_asssert = SMT_Replay.add_asserted Symtab.update Symtab.empty extract cond
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
   274
    val ((_, _), (ctxt3, assumed)) =
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
   275
      add_asssert outer_ctxt rewrite_rules assms
72458
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   276
        (map_filter (remove_rewrite_rules_from_rules num_ll_defs) assm_steps) ctxt2
69205
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
   277
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
   278
    val used_rew_js =
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
   279
      map_filter (fn id => let val i = index_of_id id in if i < 0
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
   280
          then SOME (id, normalize_tac ctxt (nth ll_defs id)) else NONE end)
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
   281
        used_assert_ids
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
   282
    val (assumed, stats) = fold (fn ((id, thm)) => fn (assumed, stats) =>
75274
e89709b80b6e used more descriptive assert names in SMT-Lib output
desharna
parents: 72513
diff changeset
   283
      let
e89709b80b6e used more descriptive assert names in SMT-Lib output
desharna
parents: 72513
diff changeset
   284
        val (thm, stats) = replay_assumed assms ll_defs rewrite_rules stats ctxt thm
e89709b80b6e used more descriptive assert names in SMT-Lib output
desharna
parents: 72513
diff changeset
   285
        val name = SMTLIB_Interface.assert_name_of_role_and_index SMT_Util.Axiom id
e89709b80b6e used more descriptive assert names in SMT-Lib output
desharna
parents: 72513
diff changeset
   286
      in
e89709b80b6e used more descriptive assert names in SMT-Lib output
desharna
parents: 72513
diff changeset
   287
        (Symtab.update (name, ([], thm)) assumed, stats)
e89709b80b6e used more descriptive assert names in SMT-Lib output
desharna
parents: 72513
diff changeset
   288
      end)
e89709b80b6e used more descriptive assert names in SMT-Lib output
desharna
parents: 72513
diff changeset
   289
      used_rew_js (assumed, Symtab.cons_list ("parsing", parsing_time) Symtab.empty)
69205
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
   290
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
   291
    val ctxt4 =
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
   292
      ctxt3
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
   293
      |> put_simpset (SMT_Replay.make_simpset ctxt3 [])
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
   294
      |> Config.put SAT.solver (Config.get ctxt3 SMT_Config.sat_solver)
72458
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   295
    val len = Verit_Proof.number_of_steps actual_steps
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   296
    fun steps_with_depth _ [] = []
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   297
      | steps_with_depth i (p :: ps) = (i +  Verit_Proof.number_of_steps [p], p) ::
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   298
          steps_with_depth (i +  Verit_Proof.number_of_steps [p]) ps
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   299
    val actual_steps = steps_with_depth 0 actual_steps
69205
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
   300
    val start = Timing.start ()
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
   301
    val print_runtime_statistics = SMT_Replay.intermediate_statistics ctxt4 start len
72458
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   302
    fun blockwise f (i, x) (next, y) =
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   303
      (if i > next then print_runtime_statistics i else ();
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   304
       (if i > next then i + 10 else next, f x y))
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   305
    val global_transformation : term Termtab.table = Termtab.empty
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   306
    val (_, (proofs, stats, ctxt5, _, _)) =
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   307
      fold (blockwise (replay_step rewrite_rules ll_defs assumed [] [])) actual_steps
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   308
        (1, (assumed, stats, ctxt4, [], global_transformation))
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   309
    val total = Time.toMilliseconds (#elapsed (Timing.result start))
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   310
    val (_, (_, Verit_Proof.VeriT_Replay_Node {id, ...})) = split_last actual_steps
69205
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
   311
    val _ = print_runtime_statistics len
72458
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   312
    val thm_with_defs = Symtab.lookup proofs id |> the |> snd
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   313
      |> singleton (Proof_Context.export ctxt5 outer_ctxt)
69205
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
   314
    val _ = SMT_Config.statistics_msg ctxt5
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
   315
      (Pretty.string_of o SMT_Replay.pretty_statistics "veriT" total) stats
72458
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   316
    val _ = SMT_Replay.spying (SMT_Config.spy_verit ctxt) ctxt
b44e894796d5 add reconstruction for the SMT solver veriT
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 69593
diff changeset
   317
      (fn () => SMT_Replay.print_stats (Symtab.dest stats)) "spy_verit"
69205
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
   318
  in
72459
15fc6320da68 reconstruction of veriT proofs in NEWS
Mathias Fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents: 72458
diff changeset
   319
    Verit_Replay_Methods.discharge ctxt [thm_with_defs] @{term False}
69205
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
   320
  end
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
   321
8050734eee3e add reconstruction by veriT in method smt
fleury <Mathias.Fleury@mpi-inf.mpg.de>
parents:
diff changeset
   322
end