src/HOL/Tools/SMT/smt_translate.ML
author blanchet
Wed Sep 17 16:53:39 2014 +0200 (2014-09-17)
changeset 58360 dee1fd1cc631
parent 58061 3d060f43accb
child 58361 7f2b3b6f6ad1
permissions -rw-r--r--
added interface for CVC4 extensions
blanchet@58061
     1
(*  Title:      HOL/Tools/SMT/smt_translate.ML
blanchet@56078
     2
    Author:     Sascha Boehme, TU Muenchen
blanchet@56078
     3
blanchet@56078
     4
Translate theorems into an SMT intermediate format and serialize them.
blanchet@56078
     5
*)
blanchet@56078
     6
blanchet@58061
     7
signature SMT_TRANSLATE =
blanchet@56078
     8
sig
blanchet@56078
     9
  (*intermediate term structure*)
blanchet@56078
    10
  datatype squant = SForall | SExists
blanchet@56078
    11
  datatype 'a spattern = SPat of 'a list | SNoPat of 'a list
blanchet@56078
    12
  datatype sterm =
blanchet@56078
    13
    SVar of int |
blanchet@56078
    14
    SApp of string * sterm list |
blanchet@56078
    15
    SLet of string * sterm * sterm |
blanchet@57165
    16
    SQua of squant * string list * sterm spattern list * sterm
blanchet@56078
    17
blanchet@56078
    18
  (*translation configuration*)
blanchet@56078
    19
  type sign = {
blanchet@57238
    20
    logic: string,
blanchet@56078
    21
    sorts: string list,
blanchet@56078
    22
    dtyps: (string * (string * (string * string) list) list) list list,
blanchet@56078
    23
    funcs: (string * (string list * string)) list }
blanchet@56078
    24
  type config = {
blanchet@57238
    25
    logic: term list -> string,
blanchet@58360
    26
    fp_kinds: BNF_Util.fp_kind list,
blanchet@57239
    27
    serialize: (string * string) list -> string list -> sign -> sterm list -> string }
blanchet@56078
    28
  type replay_data = {
blanchet@56078
    29
    context: Proof.context,
blanchet@56078
    30
    typs: typ Symtab.table,
blanchet@56078
    31
    terms: term Symtab.table,
blanchet@57541
    32
    ll_defs: term list,
blanchet@56078
    33
    rewrite_rules: thm list,
blanchet@56078
    34
    assms: (int * thm) list }
blanchet@56078
    35
blanchet@56078
    36
  (*translation*)
blanchet@58061
    37
  val add_config: SMT_Util.class * (Proof.context -> config) -> Context.generic -> Context.generic
blanchet@57239
    38
  val translate: Proof.context -> (string * string) list -> string list -> (int * thm) list ->
blanchet@57239
    39
    string * replay_data
blanchet@57229
    40
end;
blanchet@56078
    41
blanchet@58061
    42
structure SMT_Translate: SMT_TRANSLATE =
blanchet@56078
    43
struct
blanchet@56078
    44
blanchet@56078
    45
blanchet@56078
    46
(* intermediate term structure *)
blanchet@56078
    47
blanchet@56078
    48
datatype squant = SForall | SExists
blanchet@56078
    49
blanchet@56078
    50
datatype 'a spattern = SPat of 'a list | SNoPat of 'a list
blanchet@56078
    51
blanchet@56078
    52
datatype sterm =
blanchet@56078
    53
  SVar of int |
blanchet@56078
    54
  SApp of string * sterm list |
blanchet@56078
    55
  SLet of string * sterm * sterm |
blanchet@57165
    56
  SQua of squant * string list * sterm spattern list * sterm
blanchet@56078
    57
blanchet@56078
    58
blanchet@56078
    59
(* translation configuration *)
blanchet@56078
    60
blanchet@56078
    61
type sign = {
blanchet@57238
    62
  logic: string,
blanchet@56078
    63
  sorts: string list,
blanchet@56078
    64
  dtyps: (string * (string * (string * string) list) list) list list,
blanchet@56078
    65
  funcs: (string * (string list * string)) list }
blanchet@56078
    66
blanchet@56078
    67
type config = {
blanchet@57238
    68
  logic: term list -> string,
blanchet@58360
    69
  fp_kinds: BNF_Util.fp_kind list,
blanchet@57239
    70
  serialize: (string * string) list -> string list -> sign -> sterm list -> string }
blanchet@56078
    71
blanchet@56078
    72
type replay_data = {
blanchet@56078
    73
  context: Proof.context,
blanchet@56078
    74
  typs: typ Symtab.table,
blanchet@56078
    75
  terms: term Symtab.table,
blanchet@57541
    76
  ll_defs: term list,
blanchet@56078
    77
  rewrite_rules: thm list,
blanchet@56078
    78
  assms: (int * thm) list }
blanchet@56078
    79
blanchet@56078
    80
blanchet@56078
    81
(* translation context *)
blanchet@56078
    82
blanchet@56078
    83
fun add_components_of_typ (Type (s, Ts)) =
blanchet@56078
    84
    cons (Long_Name.base_name s) #> fold_rev add_components_of_typ Ts
blanchet@56078
    85
  | add_components_of_typ (TFree (s, _)) = cons (perhaps (try (unprefix "'")) s)
blanchet@56078
    86
  | add_components_of_typ _ = I;
blanchet@56078
    87
blanchet@56078
    88
fun suggested_name_of_typ T = space_implode "_" (add_components_of_typ T []);
blanchet@56078
    89
blanchet@56078
    90
fun suggested_name_of_term (Const (s, _)) = Long_Name.base_name s
blanchet@56078
    91
  | suggested_name_of_term (Free (s, _)) = s
blanchet@56078
    92
  | suggested_name_of_term _ = Name.uu
blanchet@56078
    93
blanchet@56078
    94
val empty_tr_context = (Name.context, Typtab.empty, Termtab.empty)
blanchet@56096
    95
val safe_suffix = "$"
blanchet@56078
    96
blanchet@56078
    97
fun add_typ T proper (cx as (names, typs, terms)) =
blanchet@56078
    98
  (case Typtab.lookup typs T of
blanchet@56078
    99
    SOME (name, _) => (name, cx)
blanchet@56078
   100
  | NONE =>
blanchet@56078
   101
      let
haftmann@56811
   102
        val sugg = Name.desymbolize (SOME true) (suggested_name_of_typ T) ^ safe_suffix
blanchet@56078
   103
        val (name, names') = Name.variant sugg names
blanchet@56078
   104
        val typs' = Typtab.update (T, (name, proper)) typs
blanchet@56078
   105
      in (name, (names', typs', terms)) end)
blanchet@56078
   106
blanchet@56078
   107
fun add_fun t sort (cx as (names, typs, terms)) =
blanchet@56078
   108
  (case Termtab.lookup terms t of
blanchet@56078
   109
    SOME (name, _) => (name, cx)
blanchet@57230
   110
  | NONE =>
blanchet@56078
   111
      let
haftmann@56811
   112
        val sugg = Name.desymbolize (SOME false) (suggested_name_of_term t) ^ safe_suffix
blanchet@56078
   113
        val (name, names') = Name.variant sugg names
blanchet@56078
   114
        val terms' = Termtab.update (t, (name, sort)) terms
blanchet@56078
   115
      in (name, (names', typs, terms')) end)
blanchet@56078
   116
blanchet@57238
   117
fun sign_of logic dtyps (_, typs, terms) = {
blanchet@57238
   118
  logic = logic,
blanchet@56078
   119
  sorts = Typtab.fold (fn (_, (n, true)) => cons n | _ => I) typs [],
blanchet@56078
   120
  dtyps = dtyps,
blanchet@56078
   121
  funcs = Termtab.fold (fn (_, (n, SOME ss)) => cons (n,ss) | _ => I) terms []}
blanchet@56078
   122
blanchet@57541
   123
fun replay_data_of ctxt ll_defs rules assms (_, typs, terms) =
blanchet@56078
   124
  let
blanchet@56078
   125
    fun add_typ (T, (n, _)) = Symtab.update (n, T)
blanchet@56078
   126
    val typs' = Typtab.fold add_typ typs Symtab.empty
blanchet@56078
   127
blanchet@56078
   128
    fun add_fun (t, (n, _)) = Symtab.update (n, t)
blanchet@56078
   129
    val terms' = Termtab.fold add_fun terms Symtab.empty
blanchet@56078
   130
  in
blanchet@57541
   131
    {context = ctxt, typs = typs', terms = terms', ll_defs = ll_defs, rewrite_rules = rules,
blanchet@57541
   132
     assms = assms}
blanchet@56078
   133
  end
blanchet@56078
   134
blanchet@56078
   135
blanchet@56078
   136
(* preprocessing *)
blanchet@56078
   137
blanchet@56078
   138
(** datatype declarations **)
blanchet@56078
   139
blanchet@56078
   140
fun collect_datatypes_and_records (tr_context, ctxt) ts =
blanchet@56078
   141
  let
blanchet@58360
   142
    val (declss, ctxt') =
blanchet@58360
   143
      fold (Term.fold_types (SMT_Datatypes.add_decls BNF_Util.Least_FP)) ts ([], ctxt)
blanchet@56078
   144
blanchet@56078
   145
    fun is_decl_typ T = exists (exists (equal T o fst)) declss
blanchet@56078
   146
blanchet@56078
   147
    fun add_typ' T proper =
blanchet@58061
   148
      (case SMT_Builtin.dest_builtin_typ ctxt' T of
blanchet@56078
   149
        SOME n => pair n
blanchet@56078
   150
      | NONE => add_typ T proper)
blanchet@56078
   151
blanchet@56078
   152
    fun tr_select sel =
blanchet@56078
   153
      let val T = Term.range_type (Term.fastype_of sel)
blanchet@56078
   154
      in add_fun sel NONE ##>> add_typ' T (not (is_decl_typ T)) end
blanchet@56078
   155
    fun tr_constr (constr, selects) =
blanchet@56078
   156
      add_fun constr NONE ##>> fold_map tr_select selects
blanchet@56078
   157
    fun tr_typ (T, cases) = add_typ' T false ##>> fold_map tr_constr cases
blanchet@56078
   158
    val (declss', tr_context') = fold_map (fold_map tr_typ) declss tr_context
blanchet@56078
   159
blanchet@56078
   160
    fun add (constr, selects) =
blanchet@56078
   161
      Termtab.update (constr, length selects) #>
blanchet@56078
   162
      fold (Termtab.update o rpair 1) selects
blanchet@56078
   163
    val funcs = fold (fold (fold add o snd)) declss Termtab.empty
blanchet@56078
   164
  in ((funcs, declss', tr_context', ctxt'), ts) end
blanchet@56078
   165
    (* FIXME: also return necessary datatype and record theorems *)
blanchet@56078
   166
blanchet@56078
   167
blanchet@56078
   168
(** eta-expand quantifiers, let expressions and built-ins *)
blanchet@56078
   169
blanchet@56078
   170
local
blanchet@56078
   171
  fun eta f T t = Abs (Name.uu, T, f (Term.incr_boundvars 1 t $ Bound 0))
blanchet@56078
   172
blanchet@56078
   173
  fun exp f T = eta f (Term.domain_type (Term.domain_type T))
blanchet@56078
   174
blanchet@56078
   175
  fun exp2 T q =
blanchet@56078
   176
    let val U = Term.domain_type T
blanchet@56078
   177
    in Abs (Name.uu, U, q $ eta I (Term.domain_type U) (Bound 0)) end
blanchet@56078
   178
blanchet@56078
   179
  fun expf k i T t =
blanchet@58061
   180
    let val Ts = drop i (fst (SMT_Util.dest_funT k T))
blanchet@56078
   181
    in
blanchet@56078
   182
      Term.incr_boundvars (length Ts) t
blanchet@56078
   183
      |> fold_rev (fn i => fn u => u $ Bound i) (0 upto length Ts - 1)
blanchet@56078
   184
      |> fold_rev (fn T => fn u => Abs (Name.uu, T, u)) Ts
blanchet@56078
   185
    end
blanchet@56078
   186
in
blanchet@56078
   187
blanchet@56078
   188
fun eta_expand ctxt funcs =
blanchet@56078
   189
  let
blanchet@56078
   190
    fun exp_func t T ts =
blanchet@56078
   191
      (case Termtab.lookup funcs t of
blanchet@56078
   192
        SOME k => Term.list_comb (t, ts) |> k <> length ts ? expf k (length ts) T
blanchet@56078
   193
      | NONE => Term.list_comb (t, ts))
blanchet@56078
   194
blanchet@56078
   195
    fun expand ((q as Const (@{const_name All}, _)) $ Abs a) = q $ abs_expand a
blanchet@56078
   196
      | expand ((q as Const (@{const_name All}, T)) $ t) = q $ exp expand T t
blanchet@56078
   197
      | expand (q as Const (@{const_name All}, T)) = exp2 T q
blanchet@56078
   198
      | expand ((q as Const (@{const_name Ex}, _)) $ Abs a) = q $ abs_expand a
blanchet@56078
   199
      | expand ((q as Const (@{const_name Ex}, T)) $ t) = q $ exp expand T t
blanchet@56078
   200
      | expand (q as Const (@{const_name Ex}, T)) = exp2 T q
blanchet@57165
   201
      | expand (Const (@{const_name Let}, _) $ t $ u) = expand (Term.betapply (u, t))
blanchet@57165
   202
      | expand (Const (@{const_name Let}, T) $ t) =
blanchet@56078
   203
          let val U = Term.domain_type (Term.range_type T)
blanchet@56078
   204
          in Abs (Name.uu, U, Bound 0 $ Term.incr_boundvars 1 t) end
blanchet@56078
   205
      | expand (Const (@{const_name Let}, T)) =
blanchet@56078
   206
          let val U = Term.domain_type (Term.range_type T)
blanchet@56078
   207
          in Abs (Name.uu, Term.domain_type T, Abs (Name.uu, U, Bound 0 $ Bound 1)) end
blanchet@56078
   208
      | expand t =
blanchet@56078
   209
          (case Term.strip_comb t of
blanchet@56078
   210
            (u as Const (c as (_, T)), ts) =>
blanchet@58061
   211
              (case SMT_Builtin.dest_builtin ctxt c ts of
blanchet@56078
   212
                SOME (_, k, us, mk) =>
blanchet@56078
   213
                  if k = length us then mk (map expand us)
blanchet@56078
   214
                  else if k < length us then chop k (map expand us) |>> mk |> Term.list_comb
blanchet@56078
   215
                  else expf k (length ts) T (mk (map expand us))
blanchet@56078
   216
              | NONE => exp_func u T (map expand ts))
blanchet@56078
   217
          | (u as Free (_, T), ts) => exp_func u T (map expand ts)
blanchet@56078
   218
          | (Abs a, ts) => Term.list_comb (abs_expand a, map expand ts)
blanchet@56078
   219
          | (u, ts) => Term.list_comb (u, map expand ts))
blanchet@56078
   220
blanchet@56078
   221
    and abs_expand (n, T, t) = Abs (n, T, expand t)
blanchet@57230
   222
blanchet@56078
   223
  in map expand end
blanchet@56078
   224
blanchet@56078
   225
end
blanchet@56078
   226
blanchet@56078
   227
blanchet@56078
   228
(** introduce explicit applications **)
blanchet@56078
   229
blanchet@56078
   230
local
blanchet@56078
   231
  (*
blanchet@56078
   232
    Make application explicit for functions with varying number of arguments.
blanchet@56078
   233
  *)
blanchet@56078
   234
blanchet@56078
   235
  fun add t i = apfst (Termtab.map_default (t, i) (Integer.min i))
blanchet@56078
   236
  fun add_type T = apsnd (Typtab.update (T, ()))
blanchet@56078
   237
blanchet@56078
   238
  fun min_arities t =
blanchet@56078
   239
    (case Term.strip_comb t of
blanchet@56078
   240
      (u as Const _, ts) => add u (length ts) #> fold min_arities ts
blanchet@56078
   241
    | (u as Free _, ts) => add u (length ts) #> fold min_arities ts
blanchet@56078
   242
    | (Abs (_, T, u), ts) => (can dest_funT T ? add_type T) #> min_arities u #> fold min_arities ts
blanchet@56078
   243
    | (_, ts) => fold min_arities ts)
blanchet@56078
   244
blanchet@56078
   245
  fun minimize types t i =
blanchet@56078
   246
    let
blanchet@56078
   247
      fun find_min j [] _ = j
blanchet@56078
   248
        | find_min j (U :: Us) T =
blanchet@56078
   249
            if Typtab.defined types T then j else find_min (j + 1) Us (U --> T)
blanchet@56078
   250
blanchet@56078
   251
      val (Ts, T) = Term.strip_type (Term.type_of t)
blanchet@56078
   252
    in find_min 0 (take i (rev Ts)) T end
blanchet@56078
   253
blanchet@57230
   254
  fun app u (t, T) = (Const (@{const_name fun_app}, T --> T) $ t $ u, Term.range_type T)
blanchet@56078
   255
blanchet@56078
   256
  fun apply i t T ts =
blanchet@56078
   257
    let
blanchet@56078
   258
      val (ts1, ts2) = chop i ts
blanchet@58061
   259
      val (_, U) = SMT_Util.dest_funT i T
blanchet@56078
   260
    in fst (fold app ts2 (Term.list_comb (t, ts1), U)) end
blanchet@56078
   261
in
blanchet@56078
   262
blanchet@56078
   263
fun intro_explicit_application ctxt funcs ts =
blanchet@56078
   264
  let
blanchet@56078
   265
    val (arities, types) = fold min_arities ts (Termtab.empty, Typtab.empty)
blanchet@56078
   266
    val arities' = Termtab.map (minimize types) arities (* FIXME: highly suspicious *)
blanchet@56078
   267
blanchet@56078
   268
    fun app_func t T ts =
blanchet@56078
   269
      if is_some (Termtab.lookup funcs t) then Term.list_comb (t, ts)
blanchet@56078
   270
      else apply (the (Termtab.lookup arities' t)) t T ts
blanchet@56078
   271
blanchet@58061
   272
    fun in_list T f t = SMT_Util.mk_symb_list T (map f (SMT_Util.dest_symb_list t))
blanchet@56078
   273
blanchet@56078
   274
    fun traverse Ts t =
blanchet@56078
   275
      (case Term.strip_comb t of
blanchet@56078
   276
        (q as Const (@{const_name All}, _), [Abs (x, T, u)]) =>
blanchet@56078
   277
          q $ Abs (x, T, in_trigger (T :: Ts) u)
blanchet@56078
   278
      | (q as Const (@{const_name Ex}, _), [Abs (x, T, u)]) =>
blanchet@56078
   279
          q $ Abs (x, T, in_trigger (T :: Ts) u)
blanchet@56078
   280
      | (q as Const (@{const_name Let}, _), [u1, u2 as Abs _]) =>
blanchet@56078
   281
          q $ traverse Ts u1 $ traverse Ts u2
blanchet@56078
   282
      | (u as Const (c as (_, T)), ts) =>
blanchet@58061
   283
          (case SMT_Builtin.dest_builtin ctxt c ts of
blanchet@56078
   284
            SOME (_, k, us, mk) =>
blanchet@56078
   285
              let
blanchet@56078
   286
                val (ts1, ts2) = chop k (map (traverse Ts) us)
blanchet@56078
   287
                val U = Term.strip_type T |>> snd o chop k |> (op --->)
blanchet@56078
   288
              in apply 0 (mk ts1) U ts2 end
blanchet@56078
   289
          | NONE => app_func u T (map (traverse Ts) ts))
blanchet@56078
   290
      | (u as Free (_, T), ts) => app_func u T (map (traverse Ts) ts)
blanchet@56078
   291
      | (u as Bound i, ts) => apply 0 u (nth Ts i) (map (traverse Ts) ts)
blanchet@56078
   292
      | (Abs (n, T, u), ts) => traverses Ts (Abs (n, T, traverse (T::Ts) u)) ts
blanchet@56078
   293
      | (u, ts) => traverses Ts u ts)
blanchet@57230
   294
    and in_trigger Ts ((c as @{const trigger}) $ p $ t) = c $ in_pats Ts p $ traverse Ts t
blanchet@57165
   295
      | in_trigger Ts t = traverse Ts t
blanchet@56078
   296
    and in_pats Ts ps =
blanchet@57230
   297
      in_list @{typ "pattern symb_list"} (in_list @{typ pattern} (in_pat Ts)) ps
blanchet@57230
   298
    and in_pat Ts ((p as Const (@{const_name pat}, _)) $ t) = p $ traverse Ts t
blanchet@57230
   299
      | in_pat Ts ((p as Const (@{const_name nopat}, _)) $ t) = p $ traverse Ts t
blanchet@56078
   300
      | in_pat _ t = raise TERM ("bad pattern", [t])
blanchet@56078
   301
    and traverses Ts t ts = Term.list_comb (t, map (traverse Ts) ts)
blanchet@56078
   302
  in map (traverse []) ts end
blanchet@56078
   303
blanchet@57230
   304
val fun_app_eq = mk_meta_eq @{thm fun_app_def}
blanchet@56078
   305
blanchet@56078
   306
end
blanchet@56078
   307
blanchet@56078
   308
blanchet@56078
   309
(** map HOL formulas to FOL formulas (i.e., separate formulas froms terms) **)
blanchet@56078
   310
blanchet@56078
   311
local
blanchet@56078
   312
  val is_quant = member (op =) [@{const_name All}, @{const_name Ex}]
blanchet@56078
   313
blanchet@56078
   314
  val fol_rules = [
blanchet@56078
   315
    Let_def,
blanchet@56078
   316
    @{lemma "P = True == P" by (rule eq_reflection) simp},
blanchet@56078
   317
    @{lemma "if P then True else False == P" by (rule eq_reflection) simp}]
blanchet@56078
   318
blanchet@56078
   319
  exception BAD_PATTERN of unit
blanchet@56078
   320
blanchet@56078
   321
  fun wrap_in_if pat t =
blanchet@56104
   322
    if pat then raise BAD_PATTERN () else @{const If (bool)} $ t $ @{const True} $ @{const False}
blanchet@56078
   323
blanchet@56078
   324
  fun is_builtin_conn_or_pred ctxt c ts =
blanchet@58061
   325
    is_some (SMT_Builtin.dest_builtin_conn ctxt c ts) orelse
blanchet@58061
   326
    is_some (SMT_Builtin.dest_builtin_pred ctxt c ts)
blanchet@56078
   327
in
blanchet@56078
   328
blanchet@56078
   329
fun folify ctxt =
blanchet@56078
   330
  let
blanchet@58061
   331
    fun in_list T f t = SMT_Util.mk_symb_list T (map_filter f (SMT_Util.dest_symb_list t))
blanchet@56078
   332
blanchet@56078
   333
    fun in_term pat t =
blanchet@56078
   334
      (case Term.strip_comb t of
blanchet@56078
   335
        (@{const True}, []) => t
blanchet@56078
   336
      | (@{const False}, []) => t
blanchet@56078
   337
      | (u as Const (@{const_name If}, _), [t1, t2, t3]) =>
blanchet@56104
   338
          if pat then raise BAD_PATTERN () else u $ in_form t1 $ in_term pat t2 $ in_term pat t3
blanchet@56078
   339
      | (Const (c as (n, _)), ts) =>
blanchet@56078
   340
          if is_builtin_conn_or_pred ctxt c ts then wrap_in_if pat (in_form t)
blanchet@56078
   341
          else if is_quant n then wrap_in_if pat (in_form t)
blanchet@56078
   342
          else Term.list_comb (Const c, map (in_term pat) ts)
blanchet@56078
   343
      | (Free c, ts) => Term.list_comb (Free c, map (in_term pat) ts)
blanchet@56078
   344
      | _ => t)
blanchet@56078
   345
blanchet@57230
   346
    and in_pat ((p as Const (@{const_name pat}, _)) $ t) =
blanchet@56078
   347
          p $ in_term true t
blanchet@57230
   348
      | in_pat ((p as Const (@{const_name nopat}, _)) $ t) =
blanchet@56078
   349
          p $ in_term true t
blanchet@56078
   350
      | in_pat t = raise TERM ("bad pattern", [t])
blanchet@56078
   351
blanchet@56078
   352
    and in_pats ps =
blanchet@57230
   353
      in_list @{typ "pattern symb_list"} (SOME o in_list @{typ pattern} (try in_pat)) ps
blanchet@56078
   354
blanchet@57230
   355
    and in_trigger ((c as @{const trigger}) $ p $ t) = c $ in_pats p $ in_form t
blanchet@57165
   356
      | in_trigger t = in_form t
blanchet@56078
   357
blanchet@56078
   358
    and in_form t =
blanchet@56078
   359
      (case Term.strip_comb t of
blanchet@56078
   360
        (q as Const (qn, _), [Abs (n, T, u)]) =>
blanchet@56078
   361
          if is_quant qn then q $ Abs (n, T, in_trigger u)
blanchet@56078
   362
          else in_term false t
blanchet@56078
   363
      | (Const c, ts) =>
blanchet@58061
   364
          (case SMT_Builtin.dest_builtin_conn ctxt c ts of
blanchet@56078
   365
            SOME (_, _, us, mk) => mk (map in_form us)
blanchet@56078
   366
          | NONE =>
blanchet@58061
   367
              (case SMT_Builtin.dest_builtin_pred ctxt c ts of
blanchet@56078
   368
                SOME (_, _, us, mk) => mk (map (in_term false) us)
blanchet@56078
   369
              | NONE => in_term false t))
blanchet@56078
   370
      | _ => in_term false t)
blanchet@56078
   371
  in
blanchet@56078
   372
    map in_form #>
blanchet@56078
   373
    pair (fol_rules, I)
blanchet@56078
   374
  end
blanchet@56078
   375
blanchet@56078
   376
end
blanchet@56078
   377
blanchet@56078
   378
blanchet@56078
   379
(* translation into intermediate format *)
blanchet@56078
   380
blanchet@56078
   381
(** utility functions **)
blanchet@56078
   382
blanchet@56078
   383
val quantifier = (fn
blanchet@56078
   384
    @{const_name All} => SOME SForall
blanchet@56078
   385
  | @{const_name Ex} => SOME SExists
blanchet@56078
   386
  | _ => NONE)
blanchet@56078
   387
blanchet@56078
   388
fun group_quant qname Ts (t as Const (q, _) $ Abs (_, T, u)) =
blanchet@56078
   389
      if q = qname then group_quant qname (T :: Ts) u else (Ts, t)
blanchet@56078
   390
  | group_quant _ Ts t = (Ts, t)
blanchet@56078
   391
blanchet@57230
   392
fun dest_pat (Const (@{const_name pat}, _) $ t) = (t, true)
blanchet@57230
   393
  | dest_pat (Const (@{const_name nopat}, _) $ t) = (t, false)
blanchet@56078
   394
  | dest_pat t = raise TERM ("bad pattern", [t])
blanchet@56078
   395
blanchet@56078
   396
fun dest_pats [] = I
blanchet@56078
   397
  | dest_pats ts =
blanchet@56078
   398
      (case map dest_pat ts |> split_list ||> distinct (op =) of
blanchet@56078
   399
        (ps, [true]) => cons (SPat ps)
blanchet@56078
   400
      | (ps, [false]) => cons (SNoPat ps)
blanchet@56078
   401
      | _ => raise TERM ("bad multi-pattern", ts))
blanchet@56078
   402
blanchet@57230
   403
fun dest_trigger (@{const trigger} $ tl $ t) =
blanchet@58061
   404
      (rev (fold (dest_pats o SMT_Util.dest_symb_list) (SMT_Util.dest_symb_list tl) []), t)
blanchet@56078
   405
  | dest_trigger t = ([], t)
blanchet@56078
   406
blanchet@56078
   407
fun dest_quant qn T t = quantifier qn |> Option.map (fn q =>
blanchet@56078
   408
  let
blanchet@56078
   409
    val (Ts, u) = group_quant qn [T] t
blanchet@56078
   410
    val (ps, p) = dest_trigger u
blanchet@57165
   411
  in (q, rev Ts, ps, p) end)
blanchet@56078
   412
blanchet@56078
   413
fun fold_map_pat f (SPat ts) = fold_map f ts #>> SPat
blanchet@56078
   414
  | fold_map_pat f (SNoPat ts) = fold_map f ts #>> SNoPat
blanchet@56078
   415
blanchet@56078
   416
blanchet@56078
   417
(** translation from Isabelle terms into SMT intermediate terms **)
blanchet@56078
   418
blanchet@57238
   419
fun intermediate logic dtyps builtin ctxt ts trx =
blanchet@56078
   420
  let
blanchet@56078
   421
    fun transT (T as TFree _) = add_typ T true
blanchet@56078
   422
      | transT (T as TVar _) = (fn _ => raise TYPE ("bad SMT type", [T], []))
blanchet@56078
   423
      | transT (T as Type _) =
blanchet@58061
   424
          (case SMT_Builtin.dest_builtin_typ ctxt T of
blanchet@56078
   425
            SOME n => pair n
blanchet@56078
   426
          | NONE => add_typ T true)
blanchet@56078
   427
blanchet@56078
   428
    fun app n ts = SApp (n, ts)
blanchet@56078
   429
blanchet@56078
   430
    fun trans t =
blanchet@56078
   431
      (case Term.strip_comb t of
blanchet@56078
   432
        (Const (qn, _), [Abs (_, T, t1)]) =>
blanchet@56078
   433
          (case dest_quant qn T t1 of
blanchet@57165
   434
            SOME (q, Ts, ps, b) =>
blanchet@56078
   435
              fold_map transT Ts ##>> fold_map (fold_map_pat trans) ps ##>>
blanchet@57165
   436
              trans b #>> (fn ((Ts', ps'), b') => SQua (q, Ts', ps', b'))
blanchet@56078
   437
          | NONE => raise TERM ("unsupported quantifier", [t]))
blanchet@56078
   438
      | (Const (@{const_name Let}, _), [t1, Abs (_, T, t2)]) =>
blanchet@56078
   439
          transT T ##>> trans t1 ##>> trans t2 #>> (fn ((U, u1), u2) => SLet (U, u1, u2))
blanchet@56078
   440
      | (u as Const (c as (_, T)), ts) =>
blanchet@56078
   441
          (case builtin ctxt c ts of
blanchet@56078
   442
            SOME (n, _, us, _) => fold_map trans us #>> app n
blanchet@56078
   443
          | NONE => transs u T ts)
blanchet@56078
   444
      | (u as Free (_, T), ts) => transs u T ts
blanchet@56078
   445
      | (Bound i, []) => pair (SVar i)
blanchet@56078
   446
      | _ => raise TERM ("bad SMT term", [t]))
blanchet@57230
   447
blanchet@56078
   448
    and transs t T ts =
blanchet@58061
   449
      let val (Us, U) = SMT_Util.dest_funT (length ts) T
blanchet@56078
   450
      in
blanchet@56078
   451
        fold_map transT Us ##>> transT U #-> (fn Up =>
blanchet@56104
   452
          add_fun t (SOME Up) ##>> fold_map trans ts #>> SApp)
blanchet@56078
   453
      end
blanchet@56078
   454
blanchet@56078
   455
    val (us, trx') = fold_map trans ts trx
blanchet@57238
   456
  in ((sign_of (logic ts) dtyps trx', us), trx') end
blanchet@56078
   457
blanchet@56078
   458
blanchet@56078
   459
(* translation *)
blanchet@56078
   460
blanchet@56078
   461
structure Configs = Generic_Data
blanchet@56078
   462
(
blanchet@58061
   463
  type T = (Proof.context -> config) SMT_Util.dict
blanchet@56078
   464
  val empty = []
blanchet@56078
   465
  val extend = I
blanchet@58061
   466
  fun merge data = SMT_Util.dict_merge fst data
blanchet@56078
   467
)
blanchet@56078
   468
blanchet@58061
   469
fun add_config (cs, cfg) = Configs.map (SMT_Util.dict_update (cs, cfg))
blanchet@56078
   470
blanchet@57230
   471
fun get_config ctxt =
blanchet@58061
   472
  let val cs = SMT_Config.solver_class_of ctxt
blanchet@56078
   473
  in
blanchet@58061
   474
    (case SMT_Util.dict_get (Configs.get (Context.Proof ctxt)) cs of
blanchet@56078
   475
      SOME cfg => cfg ctxt
blanchet@56078
   476
    | NONE => error ("SMT: no translation configuration found " ^
blanchet@58061
   477
        "for solver class " ^ quote (SMT_Util.string_of_class cs)))
blanchet@56078
   478
  end
blanchet@56078
   479
blanchet@57239
   480
fun translate ctxt smt_options comments ithms =
blanchet@56078
   481
  let
blanchet@58360
   482
    val {logic, fp_kinds, serialize} = get_config ctxt
blanchet@56078
   483
blanchet@56078
   484
    fun no_dtyps (tr_context, ctxt) ts =
blanchet@56078
   485
      ((Termtab.empty, [], tr_context, ctxt), ts)
blanchet@56078
   486
blanchet@58061
   487
    val ts1 = map (Envir.beta_eta_contract o SMT_Util.prop_of o snd) ithms
blanchet@56078
   488
blanchet@56078
   489
    val ((funcs, dtyps, tr_context, ctxt1), ts2) =
blanchet@56078
   490
      ((empty_tr_context, ctxt), ts1)
blanchet@58360
   491
      |-> (if member (op =) fp_kinds BNF_Util.Least_FP then collect_datatypes_and_records
blanchet@58360
   492
        else no_dtyps)
blanchet@56078
   493
blanchet@56078
   494
    fun is_binder (Const (@{const_name Let}, _) $ _) = true
blanchet@56078
   495
      | is_binder t = Lambda_Lifting.is_quantifier t
blanchet@56078
   496
blanchet@56078
   497
    fun mk_trigger ((q as Const (@{const_name All}, _)) $ Abs (n, T, t)) =
blanchet@56078
   498
          q $ Abs (n, T, mk_trigger t)
blanchet@56078
   499
      | mk_trigger (eq as (Const (@{const_name HOL.eq}, T) $ lhs $ _)) =
blanchet@57230
   500
          Term.domain_type T --> @{typ pattern}
blanchet@57230
   501
          |> (fn T => Const (@{const_name pat}, T) $ lhs)
blanchet@58061
   502
          |> SMT_Util.mk_symb_list @{typ pattern} o single
blanchet@58061
   503
          |> SMT_Util.mk_symb_list @{typ "pattern symb_list"} o single
blanchet@57230
   504
          |> (fn t => @{const trigger} $ t $ eq)
blanchet@56078
   505
      | mk_trigger t = t
blanchet@56078
   506
blanchet@57541
   507
    val (ctxt2, (ts3, ll_defs)) =
blanchet@56078
   508
      ts2
blanchet@56078
   509
      |> eta_expand ctxt1 funcs
blanchet@56078
   510
      |> rpair ctxt1
blanchet@56078
   511
      |-> Lambda_Lifting.lift_lambdas NONE is_binder
blanchet@57541
   512
      |-> (fn (ts', ll_defs) => fn ctxt' =>
blanchet@57541
   513
          (ctxt', (intro_explicit_application ctxt' funcs (map mk_trigger ll_defs @ ts'), ll_defs)))
blanchet@56078
   514
blanchet@56078
   515
    val ((rewrite_rules, builtin), ts4) = folify ctxt2 ts3
blanchet@56104
   516
      |>> apfst (cons fun_app_eq)
blanchet@56078
   517
  in
blanchet@56078
   518
    (ts4, tr_context)
blanchet@58061
   519
    |-> intermediate logic dtyps (builtin SMT_Builtin.dest_builtin) ctxt2
blanchet@57239
   520
    |>> uncurry (serialize smt_options comments)
blanchet@57541
   521
    ||> replay_data_of ctxt2 ll_defs rewrite_rules ithms
blanchet@56078
   522
  end
blanchet@56078
   523
blanchet@57229
   524
end;