src/HOL/Tools/SMT2/z3_new_isar.ML
author blanchet
Thu Jul 10 18:08:21 2014 +0200 (2014-07-10)
changeset 57541 147e3f1e0459
parent 57289 5483868da0d8
child 57704 c0da3fc313e3
permissions -rw-r--r--
lambda-lifting for Z3 Isar proofs
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
blanchet@56078
    21
blanchet@56078
    22
val z3_apply_def_rule = Z3_New_Proof.string_of_rule Z3_New_Proof.Apply_Def
blanchet@56078
    23
val z3_hypothesis_rule = Z3_New_Proof.string_of_rule Z3_New_Proof.Hypothesis
blanchet@56078
    24
val z3_intro_def_rule = Z3_New_Proof.string_of_rule Z3_New_Proof.Intro_Def
blanchet@56078
    25
val z3_lemma_rule = Z3_New_Proof.string_of_rule Z3_New_Proof.Lemma
blanchet@56078
    26
blanchet@56078
    27
fun inline_z3_defs _ [] = []
blanchet@56078
    28
  | inline_z3_defs defs ((name, role, t, rule, deps) :: lines) =
blanchet@56078
    29
    if rule = z3_intro_def_rule then
blanchet@56078
    30
      let val def = t |> HOLogic.dest_Trueprop |> HOLogic.dest_eq |> swap in
blanchet@56078
    31
        inline_z3_defs (insert (op =) def defs)
blanchet@56078
    32
          (map (replace_dependencies_in_line (name, [])) lines)
blanchet@56078
    33
      end
blanchet@56078
    34
    else if rule = z3_apply_def_rule then
blanchet@56078
    35
      inline_z3_defs defs (map (replace_dependencies_in_line (name, [])) lines)
blanchet@56078
    36
    else
blanchet@56078
    37
      (name, role, Term.subst_atomic defs t, rule, deps) :: inline_z3_defs defs lines
blanchet@56078
    38
blanchet@56078
    39
fun add_z3_hypotheses [] = I
blanchet@56078
    40
  | add_z3_hypotheses hyps =
blanchet@56078
    41
    HOLogic.dest_Trueprop
blanchet@56078
    42
    #> curry s_imp (Library.foldr1 s_conj (map HOLogic.dest_Trueprop hyps))
blanchet@56078
    43
    #> HOLogic.mk_Trueprop
blanchet@56078
    44
blanchet@56078
    45
fun inline_z3_hypotheses _ _ [] = []
blanchet@56078
    46
  | inline_z3_hypotheses hyp_names hyps ((name, role, t, rule, deps) :: lines) =
blanchet@56078
    47
    if rule = z3_hypothesis_rule then
blanchet@56078
    48
      inline_z3_hypotheses (name :: hyp_names) (AList.map_default (op =) (t, []) (cons name) hyps)
blanchet@56078
    49
        lines
blanchet@56078
    50
    else
blanchet@56078
    51
      let val deps' = subtract (op =) hyp_names deps in
blanchet@56078
    52
        if rule = z3_lemma_rule then
blanchet@56078
    53
          (name, role, t, rule, deps') :: inline_z3_hypotheses hyp_names hyps lines
blanchet@56078
    54
        else
blanchet@56078
    55
          let
blanchet@56078
    56
            val add_hyps = filter_out (null o inter (op =) deps o snd) hyps
blanchet@56078
    57
            val t' = add_z3_hypotheses (map fst add_hyps) t
blanchet@56078
    58
            val deps' = subtract (op =) hyp_names deps
blanchet@56078
    59
            val hyps' = fold (AList.update (op =) o apsnd (insert (op =) name)) add_hyps hyps
blanchet@56078
    60
          in
blanchet@56078
    61
            (name, role, t', rule, deps') :: inline_z3_hypotheses hyp_names hyps' lines
blanchet@56078
    62
          end
blanchet@56078
    63
      end
blanchet@56078
    64
blanchet@56855
    65
fun simplify_bool ((all as Const (@{const_name All}, _)) $ Abs (s, T, t)) =
blanchet@56855
    66
    let val t' = simplify_bool t in
blanchet@56855
    67
      if loose_bvar1 (t', 0) then all $ Abs (s, T, t') else t'
blanchet@56855
    68
    end
blanchet@56855
    69
  | simplify_bool (@{const Not} $ t) = s_not (simplify_bool t)
blanchet@56129
    70
  | simplify_bool (@{const conj} $ t $ u) = s_conj (simplify_bool t, simplify_bool u)
blanchet@56129
    71
  | simplify_bool (@{const disj} $ t $ u) = s_disj (simplify_bool t, simplify_bool u)
blanchet@56129
    72
  | simplify_bool (@{const implies} $ t $ u) = s_imp (simplify_bool t, simplify_bool u)
blanchet@56129
    73
  | simplify_bool (@{const HOL.eq (bool)} $ t $ u) = s_iff (simplify_bool t, simplify_bool u)
blanchet@56129
    74
  | simplify_bool (t as Const (@{const_name HOL.eq}, _) $ u $ v) =
blanchet@56126
    75
    if u aconv v then @{const True} else t
blanchet@56129
    76
  | simplify_bool (t $ u) = simplify_bool t $ simplify_bool u
blanchet@56129
    77
  | simplify_bool (Abs (s, T, t)) = Abs (s, T, simplify_bool t)
blanchet@56129
    78
  | simplify_bool t = t
blanchet@56078
    79
blanchet@56129
    80
(* It is not entirely clear why this should be necessary, especially for abstractions variables. *)
blanchet@56129
    81
val unskolemize_names =
blanchet@56129
    82
  Term.map_abs_vars (perhaps (try Name.dest_skolem))
blanchet@56129
    83
  #> Term.map_aterms (perhaps (try (fn Free (s, T) => Free (Name.dest_skolem s, T))))
blanchet@56078
    84
blanchet@57289
    85
fun strip_alls (Const (@{const_name All}, _) $ Abs (s, T, body)) = strip_alls body |>> cons (s, T)
blanchet@57289
    86
  | strip_alls t = ([], t)
blanchet@57289
    87
blanchet@57289
    88
fun push_skolem_all_under_iff t =
blanchet@57289
    89
  (case strip_alls t of
blanchet@57289
    90
    (qs as _ :: _,
blanchet@57289
    91
     (t0 as Const (@{const_name HOL.eq}, _)) $ (t1 as Const (@{const_name Ex}, _) $ _) $ t2) =>
blanchet@57289
    92
    t0 $ HOLogic.list_all (qs, t1) $ HOLogic.list_all (qs, t2)
blanchet@57289
    93
  | _ => t)
blanchet@57289
    94
blanchet@57541
    95
fun unlift_term ll_defs =
blanchet@57541
    96
  let
blanchet@57541
    97
    val lifted = map (ATP_Util.extract_lambda_def dest_Free o ATP_Util.hol_open_form I) ll_defs
blanchet@57541
    98
blanchet@57541
    99
    fun un_free (t as Free (s, _)) =
blanchet@57541
   100
       (case AList.lookup (op =) lifted s of
blanchet@57541
   101
         SOME t => un_term t
blanchet@57541
   102
       | NONE => t)
blanchet@57541
   103
     | un_free t = t
blanchet@57541
   104
    and un_term t = map_aterms un_free t
blanchet@57541
   105
  in un_term end
blanchet@57541
   106
blanchet@57541
   107
fun atp_proof_of_z3_proof ctxt ll_defs rewrite_rules hyp_ts concl_t fact_helper_ts prem_ids
blanchet@57541
   108
    conjecture_id fact_helper_ids proof =
blanchet@56078
   109
  let
blanchet@56981
   110
    val thy = Proof_Context.theory_of ctxt
blanchet@56981
   111
blanchet@57541
   112
    val unlift_term = unlift_term ll_defs
blanchet@57541
   113
blanchet@56981
   114
    fun steps_of (Z3_New_Proof.Z3_Step {id, rule, prems, concl, ...}) =
blanchet@56078
   115
      let
blanchet@56985
   116
        val sid = string_of_int id
blanchet@56128
   117
blanchet@56981
   118
        val concl' =
blanchet@56981
   119
          concl
blanchet@56128
   120
          |> Raw_Simplifier.rewrite_term thy rewrite_rules []
blanchet@56128
   121
          |> Object_Logic.atomize_term thy
blanchet@56129
   122
          |> simplify_bool
blanchet@57541
   123
          |> not (null ll_defs) ? unlift_term
blanchet@56129
   124
          |> unskolemize_names
blanchet@57289
   125
          |> push_skolem_all_under_iff
blanchet@56128
   126
          |> HOLogic.mk_Trueprop
blanchet@56981
   127
blanchet@56981
   128
        fun standard_step role =
blanchet@56985
   129
          ((sid, []), role, concl', Z3_New_Proof.string_of_rule rule,
blanchet@56985
   130
           map (fn id => (string_of_int id, [])) prems)
blanchet@56078
   131
      in
blanchet@56981
   132
        (case rule of
blanchet@56981
   133
          Z3_New_Proof.Asserted =>
blanchet@56981
   134
          let
blanchet@57056
   135
            val ss = the_list (AList.lookup (op =) fact_helper_ids id)
blanchet@56981
   136
            val name0 = (sid ^ "a", ss)
blanchet@56985
   137
blanchet@56981
   138
            val (role0, concl0) =
blanchet@56985
   139
              (case ss of
blanchet@57056
   140
                [s] => (Axiom, the (AList.lookup (op =) fact_helper_ts s))
blanchet@56985
   141
              | _ =>
blanchet@56985
   142
                if id = conjecture_id then
blanchet@56985
   143
                  (Conjecture, concl_t)
blanchet@56985
   144
                else
blanchet@56985
   145
                  (Hypothesis,
blanchet@56985
   146
                   (case find_index (curry (op =) id) prem_ids of
blanchet@56985
   147
                     ~1 => concl
blanchet@56985
   148
                   | i => nth hyp_ts i)))
blanchet@56981
   149
blanchet@56981
   150
            val normalize_prems =
blanchet@56981
   151
              SMT2_Normalize.case_bool_entry :: SMT2_Normalize.special_quant_table @
blanchet@56981
   152
              SMT2_Normalize.abs_min_max_table
blanchet@56981
   153
              |> map_filter (fn (c, th) =>
blanchet@56981
   154
                if exists_Const (curry (op =) c o fst) concl0 then
blanchet@56981
   155
                  let val s = short_thm_name ctxt th in SOME (s, [s]) end
blanchet@56981
   156
                else
blanchet@56981
   157
                  NONE)
blanchet@56981
   158
          in
blanchet@57218
   159
            (if role0 = Axiom then []
blanchet@57218
   160
             else [(name0, role0, concl0, Z3_New_Proof.string_of_rule rule, [])]) @
blanchet@57218
   161
            [((sid, []), Plain, concl', Z3_New_Proof.string_of_rule Z3_New_Proof.Rewrite,
blanchet@56985
   162
              name0 :: normalize_prems)]
blanchet@56981
   163
          end
blanchet@56981
   164
        | Z3_New_Proof.Rewrite => [standard_step Lemma]
blanchet@56981
   165
        | Z3_New_Proof.Rewrite_Star => [standard_step Lemma]
blanchet@56981
   166
        | Z3_New_Proof.Skolemize => [standard_step Lemma]
blanchet@56981
   167
        | Z3_New_Proof.Th_Lemma _ => [standard_step Lemma]
blanchet@56981
   168
        | _ => [standard_step Plain])
blanchet@56078
   169
      end
blanchet@56078
   170
  in
blanchet@56078
   171
    proof
blanchet@56981
   172
    |> maps steps_of
blanchet@56078
   173
    |> inline_z3_defs []
blanchet@56078
   174
    |> inline_z3_hypotheses [] []
blanchet@56078
   175
  end
blanchet@56078
   176
blanchet@56078
   177
end;