src/HOL/Tools/SMT2/smtlib2_isar.ML
author fleury
Wed, 30 Jul 2014 14:03:12 +0200
changeset 57705 5da48dae7d03
parent 57704 c0da3fc313e3
child 57708 4b52c1b319ce
permissions -rw-r--r--
Subproofs for the SMT solver veriT.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
57704
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
     1
(*  Title:      HOL/Tools/SMT2/smtlib2_isar.ML
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
     2
    Author:     Jasmin Blanchette, TU Muenchen
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
     3
    Author:     Mathias Fleury, ENS Rennes
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
     4
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
     5
General tools for Isar proof reconstruction.
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
     6
*)
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
     7
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
     8
signature SMTLIB2_ISAR =
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
     9
sig
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    10
  val simplify_bool: term -> term
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    11
  val unlift_term: term list -> term -> term
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    12
  val postprocess_step_conclusion: term -> theory -> thm list -> term list -> term
57705
5da48dae7d03 Subproofs for the SMT solver veriT.
fleury
parents: 57704
diff changeset
    13
  val normalizing_prems : Proof.context -> term -> (string * string list) list
57704
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    14
  val distinguish_conjecture_and_hypothesis : ''a list -> ''b -> ''b -> ''b list ->
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    15
    (''a * 'c) list -> 'c list -> 'c -> 'c -> ATP_Problem.atp_formula_role * 'c
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    16
end;
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    17
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    18
structure SMTLIB2_Isar: SMTLIB2_ISAR =
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    19
struct
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    20
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    21
open ATP_Problem
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    22
open ATP_Util
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    23
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    24
fun simplify_bool ((all as Const (@{const_name All}, _)) $ Abs (s, T, t)) =
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    25
    let val t' = simplify_bool t in
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    26
      if loose_bvar1 (t', 0) then all $ Abs (s, T, t') else t'
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    27
    end
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    28
  | simplify_bool (@{const Not} $ t) = s_not (simplify_bool t)
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    29
  | simplify_bool (@{const conj} $ t $ u) = s_conj (simplify_bool t, simplify_bool u)
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    30
  | simplify_bool (@{const disj} $ t $ u) = s_disj (simplify_bool t, simplify_bool u)
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    31
  | simplify_bool (@{const implies} $ t $ u) = s_imp (simplify_bool t, simplify_bool u)
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    32
  | simplify_bool (@{const HOL.eq (bool)} $ t $ u) = s_iff (simplify_bool t, simplify_bool u)
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    33
  | simplify_bool (t as Const (@{const_name HOL.eq}, _) $ u $ v) =
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    34
    if u aconv v then @{const True} else t
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    35
  | simplify_bool (t $ u) = simplify_bool t $ simplify_bool u
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    36
  | simplify_bool (Abs (s, T, t)) = Abs (s, T, simplify_bool t)
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    37
  | simplify_bool t = t
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    38
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    39
fun strip_alls (Const (@{const_name All}, _) $ Abs (s, T, body)) = strip_alls body |>> cons (s, T)
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    40
  | strip_alls t = ([], t)
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    41
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    42
fun push_skolem_all_under_iff t =
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    43
  (case strip_alls t of
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    44
    (qs as _ :: _,
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    45
     (t0 as Const (@{const_name HOL.eq}, _)) $ (t1 as Const (@{const_name Ex}, _) $ _) $ t2) =>
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    46
    t0 $ HOLogic.list_all (qs, t1) $ HOLogic.list_all (qs, t2)
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    47
  | _ => t)
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    48
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    49
(* It is not entirely clear why this should be necessary, especially for abstractions variables. *)
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    50
val unskolemize_names =
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    51
  Term.map_abs_vars (perhaps (try Name.dest_skolem))
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    52
  #> Term.map_aterms (perhaps (try (fn Free (s, T) => Free (Name.dest_skolem s, T))))
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    53
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    54
fun unlift_term ll_defs =
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    55
  let
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    56
    val lifted = map (ATP_Util.extract_lambda_def dest_Free o ATP_Util.hol_open_form I) ll_defs
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    57
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    58
    fun un_free (t as Free (s, _)) =
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    59
       (case AList.lookup (op =) lifted s of
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    60
         SOME t => un_term t
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    61
       | NONE => t)
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    62
     | un_free t = t
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    63
    and un_term t = map_aterms un_free t
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    64
  in un_term end
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    65
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    66
fun postprocess_step_conclusion concl thy rewrite_rules ll_defs =
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    67
  concl
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    68
  |> Raw_Simplifier.rewrite_term thy rewrite_rules []
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    69
  |> Object_Logic.atomize_term thy
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    70
  |> simplify_bool
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    71
  |> not (null ll_defs) ? unlift_term ll_defs
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    72
  |> unskolemize_names
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    73
  |> push_skolem_all_under_iff
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    74
  |> HOLogic.mk_Trueprop
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    75
57705
5da48dae7d03 Subproofs for the SMT solver veriT.
fleury
parents: 57704
diff changeset
    76
fun normalizing_prems ctxt concl0 =
57704
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    77
  SMT2_Normalize.case_bool_entry :: SMT2_Normalize.special_quant_table @
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    78
  SMT2_Normalize.abs_min_max_table
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    79
  |> map_filter (fn (c, th) =>
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    80
    if exists_Const (curry (op =) c o fst) concl0 then
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    81
      let val s = short_thm_name ctxt th in SOME (s, [s]) end
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    82
    else
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    83
      NONE)
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    84
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    85
fun distinguish_conjecture_and_hypothesis ss id conjecture_id prem_ids fact_helper_ts hyp_ts concl_t
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    86
    t =
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    87
  (case ss of
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    88
    [s] => (Axiom, the (AList.lookup (op =) fact_helper_ts s))
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    89
  | _ =>
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    90
    if id = conjecture_id then
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    91
      (Conjecture, concl_t)
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    92
    else
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    93
      (Hypothesis,
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    94
       (case find_index (curry (op =) id) prem_ids of
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    95
         ~1 => t
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    96
       | i => nth hyp_ts i)))
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    97
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    98
end;