src/HOL/Tools/SMT2/z3_new_isar.ML
changeset 57704 c0da3fc313e3
parent 57541 147e3f1e0459
child 57705 5da48dae7d03
equal deleted inserted replaced
57703:fefe3ea75289 57704:c0da3fc313e3
    16 
    16 
    17 open ATP_Util
    17 open ATP_Util
    18 open ATP_Problem
    18 open ATP_Problem
    19 open ATP_Proof
    19 open ATP_Proof
    20 open ATP_Proof_Reconstruct
    20 open ATP_Proof_Reconstruct
       
    21 open SMTLIB2_Isar
    21 
    22 
    22 val z3_apply_def_rule = Z3_New_Proof.string_of_rule Z3_New_Proof.Apply_Def
    23 val z3_apply_def_rule = Z3_New_Proof.string_of_rule Z3_New_Proof.Apply_Def
    23 val z3_hypothesis_rule = Z3_New_Proof.string_of_rule Z3_New_Proof.Hypothesis
    24 val z3_hypothesis_rule = Z3_New_Proof.string_of_rule Z3_New_Proof.Hypothesis
    24 val z3_intro_def_rule = Z3_New_Proof.string_of_rule Z3_New_Proof.Intro_Def
    25 val z3_intro_def_rule = Z3_New_Proof.string_of_rule Z3_New_Proof.Intro_Def
    25 val z3_lemma_rule = Z3_New_Proof.string_of_rule Z3_New_Proof.Lemma
    26 val z3_lemma_rule = Z3_New_Proof.string_of_rule Z3_New_Proof.Lemma
    60           in
    61           in
    61             (name, role, t', rule, deps') :: inline_z3_hypotheses hyp_names hyps' lines
    62             (name, role, t', rule, deps') :: inline_z3_hypotheses hyp_names hyps' lines
    62           end
    63           end
    63       end
    64       end
    64 
    65 
    65 fun simplify_bool ((all as Const (@{const_name All}, _)) $ Abs (s, T, t)) =
       
    66     let val t' = simplify_bool t in
       
    67       if loose_bvar1 (t', 0) then all $ Abs (s, T, t') else t'
       
    68     end
       
    69   | simplify_bool (@{const Not} $ t) = s_not (simplify_bool t)
       
    70   | simplify_bool (@{const conj} $ t $ u) = s_conj (simplify_bool t, simplify_bool u)
       
    71   | simplify_bool (@{const disj} $ t $ u) = s_disj (simplify_bool t, simplify_bool u)
       
    72   | simplify_bool (@{const implies} $ t $ u) = s_imp (simplify_bool t, simplify_bool u)
       
    73   | simplify_bool (@{const HOL.eq (bool)} $ t $ u) = s_iff (simplify_bool t, simplify_bool u)
       
    74   | simplify_bool (t as Const (@{const_name HOL.eq}, _) $ u $ v) =
       
    75     if u aconv v then @{const True} else t
       
    76   | simplify_bool (t $ u) = simplify_bool t $ simplify_bool u
       
    77   | simplify_bool (Abs (s, T, t)) = Abs (s, T, simplify_bool t)
       
    78   | simplify_bool t = t
       
    79 
       
    80 (* It is not entirely clear why this should be necessary, especially for abstractions variables. *)
       
    81 val unskolemize_names =
       
    82   Term.map_abs_vars (perhaps (try Name.dest_skolem))
       
    83   #> Term.map_aterms (perhaps (try (fn Free (s, T) => Free (Name.dest_skolem s, T))))
       
    84 
       
    85 fun strip_alls (Const (@{const_name All}, _) $ Abs (s, T, body)) = strip_alls body |>> cons (s, T)
       
    86   | strip_alls t = ([], t)
       
    87 
       
    88 fun push_skolem_all_under_iff t =
       
    89   (case strip_alls t of
       
    90     (qs as _ :: _,
       
    91      (t0 as Const (@{const_name HOL.eq}, _)) $ (t1 as Const (@{const_name Ex}, _) $ _) $ t2) =>
       
    92     t0 $ HOLogic.list_all (qs, t1) $ HOLogic.list_all (qs, t2)
       
    93   | _ => t)
       
    94 
       
    95 fun unlift_term ll_defs =
       
    96   let
       
    97     val lifted = map (ATP_Util.extract_lambda_def dest_Free o ATP_Util.hol_open_form I) ll_defs
       
    98 
       
    99     fun un_free (t as Free (s, _)) =
       
   100        (case AList.lookup (op =) lifted s of
       
   101          SOME t => un_term t
       
   102        | NONE => t)
       
   103      | un_free t = t
       
   104     and un_term t = map_aterms un_free t
       
   105   in un_term end
       
   106 
       
   107 fun atp_proof_of_z3_proof ctxt ll_defs rewrite_rules hyp_ts concl_t fact_helper_ts prem_ids
    66 fun atp_proof_of_z3_proof ctxt ll_defs rewrite_rules hyp_ts concl_t fact_helper_ts prem_ids
   108     conjecture_id fact_helper_ids proof =
    67     conjecture_id fact_helper_ids proof =
   109   let
    68   let
   110     val thy = Proof_Context.theory_of ctxt
    69     val thy = Proof_Context.theory_of ctxt
   111 
    70 
   112     val unlift_term = unlift_term ll_defs
       
   113 
       
   114     fun steps_of (Z3_New_Proof.Z3_Step {id, rule, prems, concl, ...}) =
    71     fun steps_of (Z3_New_Proof.Z3_Step {id, rule, prems, concl, ...}) =
   115       let
    72       let
   116         val sid = string_of_int id
    73         val sid = string_of_int id
   117 
    74 
   118         val concl' =
    75         val concl' = postprocess_step_conclusion concl thy rewrite_rules ll_defs
   119           concl
       
   120           |> Raw_Simplifier.rewrite_term thy rewrite_rules []
       
   121           |> Object_Logic.atomize_term thy
       
   122           |> simplify_bool
       
   123           |> not (null ll_defs) ? unlift_term
       
   124           |> unskolemize_names
       
   125           |> push_skolem_all_under_iff
       
   126           |> HOLogic.mk_Trueprop
       
   127 
       
   128         fun standard_step role =
    76         fun standard_step role =
   129           ((sid, []), role, concl', Z3_New_Proof.string_of_rule rule,
    77           ((sid, []), role, concl', Z3_New_Proof.string_of_rule rule,
   130            map (fn id => (string_of_int id, [])) prems)
    78            map (fn id => (string_of_int id, [])) prems)
   131       in
    79       in
   132         (case rule of
    80         (case rule of
   134           let
    82           let
   135             val ss = the_list (AList.lookup (op =) fact_helper_ids id)
    83             val ss = the_list (AList.lookup (op =) fact_helper_ids id)
   136             val name0 = (sid ^ "a", ss)
    84             val name0 = (sid ^ "a", ss)
   137 
    85 
   138             val (role0, concl0) =
    86             val (role0, concl0) =
   139               (case ss of
    87               distinguish_conjecture_and_hypothesis ss id conjecture_id prem_ids fact_helper_ts
   140                 [s] => (Axiom, the (AList.lookup (op =) fact_helper_ts s))
    88               hyp_ts concl_t concl
   141               | _ =>
       
   142                 if id = conjecture_id then
       
   143                   (Conjecture, concl_t)
       
   144                 else
       
   145                   (Hypothesis,
       
   146                    (case find_index (curry (op =) id) prem_ids of
       
   147                      ~1 => concl
       
   148                    | i => nth hyp_ts i)))
       
   149 
    89 
   150             val normalize_prems =
    90             val normalizing_prems = normalize_prems ctxt concl0
   151               SMT2_Normalize.case_bool_entry :: SMT2_Normalize.special_quant_table @
       
   152               SMT2_Normalize.abs_min_max_table
       
   153               |> map_filter (fn (c, th) =>
       
   154                 if exists_Const (curry (op =) c o fst) concl0 then
       
   155                   let val s = short_thm_name ctxt th in SOME (s, [s]) end
       
   156                 else
       
   157                   NONE)
       
   158           in
    91           in
   159             (if role0 = Axiom then []
    92             (if role0 = Axiom then []
   160              else [(name0, role0, concl0, Z3_New_Proof.string_of_rule rule, [])]) @
    93              else [(name0, role0, concl0, Z3_New_Proof.string_of_rule rule, [])]) @
   161             [((sid, []), Plain, concl', Z3_New_Proof.string_of_rule Z3_New_Proof.Rewrite,
    94             [((sid, []), Plain, concl', Z3_New_Proof.string_of_rule Z3_New_Proof.Rewrite,
   162               name0 :: normalize_prems)]
    95               name0 :: normalizing_prems)]
   163           end
    96           end
   164         | Z3_New_Proof.Rewrite => [standard_step Lemma]
    97         | Z3_New_Proof.Rewrite => [standard_step Lemma]
   165         | Z3_New_Proof.Rewrite_Star => [standard_step Lemma]
    98         | Z3_New_Proof.Rewrite_Star => [standard_step Lemma]
   166         | Z3_New_Proof.Skolemize => [standard_step Lemma]
    99         | Z3_New_Proof.Skolemize => [standard_step Lemma]
   167         | Z3_New_Proof.Th_Lemma _ => [standard_step Lemma]
   100         | Z3_New_Proof.Th_Lemma _ => [standard_step Lemma]