src/HOL/Tools/SMT2/smtlib2_isar.ML
author fleury
Wed, 30 Jul 2014 14:03:12 +0200
changeset 57708 4b52c1b319ce
parent 57705 5da48dae7d03
child 57710 323a57d7455c
permissions -rw-r--r--
veriT changes for lifted terms, and ite_elim rules.
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
57708
4b52c1b319ce veriT changes for lifted terms, and ite_elim rules.
fleury
parents: 57705
diff changeset
    16
  val unskolemize_names: term -> term
57704
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    17
end;
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    18
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    19
structure SMTLIB2_Isar: SMTLIB2_ISAR =
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    20
struct
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    21
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    22
open ATP_Problem
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    23
open ATP_Util
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    24
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    25
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
    26
    let val t' = simplify_bool t in
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    27
      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
    28
    end
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    29
  | simplify_bool (@{const Not} $ t) = s_not (simplify_bool t)
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    30
  | 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
    31
  | 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
    32
  | 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
    33
  | 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
    34
  | simplify_bool (t as Const (@{const_name HOL.eq}, _) $ u $ v) =
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    35
    if u aconv v then @{const True} else t
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    36
  | simplify_bool (t $ u) = simplify_bool t $ simplify_bool u
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    37
  | simplify_bool (Abs (s, T, t)) = Abs (s, T, simplify_bool t)
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    38
  | simplify_bool t = t
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    39
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    40
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
    41
  | strip_alls t = ([], t)
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    42
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    43
fun push_skolem_all_under_iff t =
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    44
  (case strip_alls t of
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    45
    (qs as _ :: _,
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    46
     (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
    47
    t0 $ HOLogic.list_all (qs, t1) $ HOLogic.list_all (qs, t2)
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    48
  | _ => t)
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    49
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    50
(* 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
    51
val unskolemize_names =
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    52
  Term.map_abs_vars (perhaps (try Name.dest_skolem))
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    53
  #> 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
    54
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    55
fun unlift_term ll_defs =
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    56
  let
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    57
    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
    58
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    59
    fun un_free (t as Free (s, _)) =
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    60
       (case AList.lookup (op =) lifted s of
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    61
         SOME t => un_term t
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    62
       | NONE => t)
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    63
     | un_free t = t
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    64
    and un_term t = map_aterms un_free t
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    65
  in un_term end
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    66
57708
4b52c1b319ce veriT changes for lifted terms, and ite_elim rules.
fleury
parents: 57705
diff changeset
    67
(* It is not entirely clear if this is necessary for abstractions variables. *)
4b52c1b319ce veriT changes for lifted terms, and ite_elim rules.
fleury
parents: 57705
diff changeset
    68
val unskolemize_names =
4b52c1b319ce veriT changes for lifted terms, and ite_elim rules.
fleury
parents: 57705
diff changeset
    69
  Term.map_abs_vars (perhaps (try Name.dest_skolem))
4b52c1b319ce veriT changes for lifted terms, and ite_elim rules.
fleury
parents: 57705
diff changeset
    70
  #> Term.map_aterms (perhaps (try (fn Free (s, T) => Free (Name.dest_skolem s, T))))
4b52c1b319ce veriT changes for lifted terms, and ite_elim rules.
fleury
parents: 57705
diff changeset
    71
57704
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    72
fun postprocess_step_conclusion concl thy rewrite_rules ll_defs =
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    73
  concl
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    74
  |> Raw_Simplifier.rewrite_term thy rewrite_rules []
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    75
  |> Object_Logic.atomize_term thy
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    76
  |> simplify_bool
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    77
  |> not (null ll_defs) ? unlift_term ll_defs
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    78
  |> unskolemize_names
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    79
  |> push_skolem_all_under_iff
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    80
  |> HOLogic.mk_Trueprop
57708
4b52c1b319ce veriT changes for lifted terms, and ite_elim rules.
fleury
parents: 57705
diff changeset
    81
  |> unskolemize_names
57704
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    82
57705
5da48dae7d03 Subproofs for the SMT solver veriT.
fleury
parents: 57704
diff changeset
    83
fun normalizing_prems ctxt concl0 =
57704
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    84
  SMT2_Normalize.case_bool_entry :: SMT2_Normalize.special_quant_table @
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    85
  SMT2_Normalize.abs_min_max_table
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    86
  |> map_filter (fn (c, th) =>
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    87
    if exists_Const (curry (op =) c o fst) concl0 then
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    88
      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
    89
    else
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    90
      NONE)
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    91
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    92
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
    93
    t =
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    94
  (case ss of
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    95
    [s] => (Axiom, the (AList.lookup (op =) fact_helper_ts s))
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    96
  | _ =>
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    97
    if id = conjecture_id then
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    98
      (Conjecture, concl_t)
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
    99
    else
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
   100
      (Hypothesis,
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
   101
       (case find_index (curry (op =) id) prem_ids of
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
   102
         ~1 => t
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
   103
       | i => nth hyp_ts i)))
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
   104
c0da3fc313e3 Basic support for the SMT prover veriT.
fleury
parents:
diff changeset
   105
end;