src/HOL/Tools/SMT2/z3_new_isar.ML
author blanchet
Fri Aug 01 14:43:57 2014 +0200 (2014-08-01)
changeset 57745 c65c116553b5
parent 57730 d39815cdb7ba
child 57748 31f5781fa9cd
permissions -rw-r--r--
more rational unskolemizing of names
blanchet@56078
     1
(*  Title:      HOL/Tools/SMT2/z3_new_isar.ML
blanchet@56078
     2
    Author:     Jasmin Blanchette, TU Muenchen
blanchet@56078
     3
blanchet@56078
     4
Z3 proofs as generic ATP proofs for Isar proof reconstruction.
blanchet@56078
     5
*)
blanchet@56078
     6
blanchet@56078
     7
signature Z3_NEW_ISAR =
blanchet@56078
     8
sig
blanchet@57541
     9
  val atp_proof_of_z3_proof: Proof.context -> term list -> thm list -> term list -> term ->
blanchet@56985
    10
    (string * term) list -> int list -> int -> (int * string) list -> Z3_New_Proof.z3_step list ->
blanchet@57159
    11
    (term, string) ATP_Proof.atp_step list
blanchet@56078
    12
end;
blanchet@56078
    13
blanchet@56078
    14
structure Z3_New_Isar: Z3_NEW_ISAR =
blanchet@56078
    15
struct
blanchet@56078
    16
blanchet@56078
    17
open ATP_Util
blanchet@56078
    18
open ATP_Problem
blanchet@56078
    19
open ATP_Proof
blanchet@56078
    20
open ATP_Proof_Reconstruct
fleury@57704
    21
open SMTLIB2_Isar
blanchet@56078
    22
blanchet@56078
    23
val z3_apply_def_rule = Z3_New_Proof.string_of_rule Z3_New_Proof.Apply_Def
blanchet@56078
    24
val z3_hypothesis_rule = Z3_New_Proof.string_of_rule Z3_New_Proof.Hypothesis
blanchet@56078
    25
val z3_intro_def_rule = Z3_New_Proof.string_of_rule Z3_New_Proof.Intro_Def
blanchet@56078
    26
val z3_lemma_rule = Z3_New_Proof.string_of_rule Z3_New_Proof.Lemma
blanchet@56078
    27
blanchet@56078
    28
fun inline_z3_defs _ [] = []
blanchet@56078
    29
  | inline_z3_defs defs ((name, role, t, rule, deps) :: lines) =
blanchet@56078
    30
    if rule = z3_intro_def_rule then
blanchet@56078
    31
      let val def = t |> HOLogic.dest_Trueprop |> HOLogic.dest_eq |> swap in
blanchet@56078
    32
        inline_z3_defs (insert (op =) def defs)
blanchet@56078
    33
          (map (replace_dependencies_in_line (name, [])) lines)
blanchet@56078
    34
      end
blanchet@56078
    35
    else if rule = z3_apply_def_rule then
blanchet@56078
    36
      inline_z3_defs defs (map (replace_dependencies_in_line (name, [])) lines)
blanchet@56078
    37
    else
blanchet@56078
    38
      (name, role, Term.subst_atomic defs t, rule, deps) :: inline_z3_defs defs lines
blanchet@56078
    39
blanchet@56078
    40
fun add_z3_hypotheses [] = I
blanchet@56078
    41
  | add_z3_hypotheses hyps =
blanchet@56078
    42
    HOLogic.dest_Trueprop
blanchet@56078
    43
    #> curry s_imp (Library.foldr1 s_conj (map HOLogic.dest_Trueprop hyps))
blanchet@56078
    44
    #> HOLogic.mk_Trueprop
blanchet@56078
    45
blanchet@56078
    46
fun inline_z3_hypotheses _ _ [] = []
blanchet@56078
    47
  | inline_z3_hypotheses hyp_names hyps ((name, role, t, rule, deps) :: lines) =
blanchet@56078
    48
    if rule = z3_hypothesis_rule then
blanchet@56078
    49
      inline_z3_hypotheses (name :: hyp_names) (AList.map_default (op =) (t, []) (cons name) hyps)
blanchet@56078
    50
        lines
blanchet@56078
    51
    else
blanchet@56078
    52
      let val deps' = subtract (op =) hyp_names deps in
blanchet@56078
    53
        if rule = z3_lemma_rule then
blanchet@56078
    54
          (name, role, t, rule, deps') :: inline_z3_hypotheses hyp_names hyps lines
blanchet@56078
    55
        else
blanchet@56078
    56
          let
blanchet@56078
    57
            val add_hyps = filter_out (null o inter (op =) deps o snd) hyps
blanchet@56078
    58
            val t' = add_z3_hypotheses (map fst add_hyps) t
blanchet@56078
    59
            val deps' = subtract (op =) hyp_names deps
blanchet@56078
    60
            val hyps' = fold (AList.update (op =) o apsnd (insert (op =) name)) add_hyps hyps
blanchet@56078
    61
          in
blanchet@56078
    62
            (name, role, t', rule, deps') :: inline_z3_hypotheses hyp_names hyps' lines
blanchet@56078
    63
          end
blanchet@56078
    64
      end
blanchet@56078
    65
blanchet@57541
    66
fun atp_proof_of_z3_proof ctxt ll_defs rewrite_rules hyp_ts concl_t fact_helper_ts prem_ids
blanchet@57541
    67
    conjecture_id fact_helper_ids proof =
blanchet@56078
    68
  let
blanchet@56981
    69
    val thy = Proof_Context.theory_of ctxt
blanchet@56981
    70
blanchet@56981
    71
    fun steps_of (Z3_New_Proof.Z3_Step {id, rule, prems, concl, ...}) =
blanchet@56078
    72
      let
blanchet@56985
    73
        val sid = string_of_int id
blanchet@56128
    74
fleury@57704
    75
        val concl' = postprocess_step_conclusion concl thy rewrite_rules ll_defs
blanchet@56981
    76
        fun standard_step role =
blanchet@56985
    77
          ((sid, []), role, concl', Z3_New_Proof.string_of_rule rule,
blanchet@56985
    78
           map (fn id => (string_of_int id, [])) prems)
blanchet@56078
    79
      in
blanchet@56981
    80
        (case rule of
blanchet@56981
    81
          Z3_New_Proof.Asserted =>
blanchet@57730
    82
          let val ss = the_list (AList.lookup (op =) fact_helper_ids id) in
blanchet@57730
    83
            (case distinguish_conjecture_and_hypothesis ss id conjecture_id prem_ids fact_helper_ts
blanchet@57730
    84
                hyp_ts concl_t concl of
blanchet@57730
    85
              NONE => []
blanchet@57745
    86
            | SOME (role0, concl00) =>
blanchet@57745
    87
              let
blanchet@57745
    88
                val name0 = (sid ^ "a", ss)
blanchet@57745
    89
                val concl0 = unskolemize_names concl00
blanchet@57745
    90
              in
blanchet@57730
    91
                (if role0 = Axiom then []
blanchet@57730
    92
                 else [(name0, role0, concl0, Z3_New_Proof.string_of_rule rule, [])]) @
blanchet@57730
    93
                [((sid, []), Plain, concl', Z3_New_Proof.string_of_rule Z3_New_Proof.Rewrite,
blanchet@57730
    94
                  name0 :: normalizing_prems ctxt concl0)]
blanchet@57730
    95
              end)
blanchet@56981
    96
          end
blanchet@56981
    97
        | Z3_New_Proof.Rewrite => [standard_step Lemma]
blanchet@56981
    98
        | Z3_New_Proof.Rewrite_Star => [standard_step Lemma]
blanchet@56981
    99
        | Z3_New_Proof.Skolemize => [standard_step Lemma]
blanchet@56981
   100
        | Z3_New_Proof.Th_Lemma _ => [standard_step Lemma]
blanchet@56981
   101
        | _ => [standard_step Plain])
blanchet@56078
   102
      end
blanchet@56078
   103
  in
blanchet@56078
   104
    proof
blanchet@56981
   105
    |> maps steps_of
blanchet@56078
   106
    |> inline_z3_defs []
blanchet@56078
   107
    |> inline_z3_hypotheses [] []
blanchet@56078
   108
  end
blanchet@56078
   109
blanchet@56078
   110
end;