src/HOL/Tools/SMT/smt_translate.ML
author blanchet
Wed Sep 24 15:46:25 2014 +0200 (2014-09-24)
changeset 58429 0b94858325a5
parent 58361 7f2b3b6f6ad1
child 61782 7d754aca6a75
permissions -rw-r--r--
interleave (co)datatypes in the right order w.r.t. dependencies
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@58429
    22
    dtyps: (BNF_Util.fp_kind * (string * (string * (string * string) 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@58429
    64
  dtyps: (BNF_Util.fp_kind * (string * (string * (string * string) 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@58429
   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@58429
   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@58361
   138
(** (co)datatype declarations **)
blanchet@56078
   139
blanchet@58361
   140
fun collect_co_datatypes fp_kinds (tr_context, ctxt) ts =
blanchet@56078
   141
  let
blanchet@58429
   142
    val (fp_decls, ctxt') =
blanchet@58361
   143
      ([], ctxt)
blanchet@58429
   144
      |> fold (Term.fold_types (SMT_Datatypes.add_decls fp_kinds)) ts
blanchet@58429
   145
      |>> flat
blanchet@56078
   146
blanchet@58429
   147
    fun is_decl_typ T = exists (equal T o fst o snd) fp_decls
blanchet@56078
   148
blanchet@56078
   149
    fun add_typ' T proper =
blanchet@58429
   150
      (case SMT_Builtin.dest_builtin_typ ctxt' T of
blanchet@56078
   151
        SOME n => pair n
blanchet@56078
   152
      | NONE => add_typ T proper)
blanchet@56078
   153
blanchet@56078
   154
    fun tr_select sel =
blanchet@56078
   155
      let val T = Term.range_type (Term.fastype_of sel)
blanchet@56078
   156
      in add_fun sel NONE ##>> add_typ' T (not (is_decl_typ T)) end
blanchet@56078
   157
    fun tr_constr (constr, selects) =
blanchet@56078
   158
      add_fun constr NONE ##>> fold_map tr_select selects
blanchet@58429
   159
    fun tr_typ (fp, (T, cases)) =
blanchet@58429
   160
      add_typ' T false ##>> fold_map tr_constr cases #>> pair fp
blanchet@58361
   161
blanchet@58429
   162
    val (fp_decls', tr_context') = fold_map tr_typ fp_decls tr_context
blanchet@56078
   163
blanchet@56078
   164
    fun add (constr, selects) =
blanchet@56078
   165
      Termtab.update (constr, length selects) #>
blanchet@56078
   166
      fold (Termtab.update o rpair 1) selects
blanchet@58361
   167
blanchet@58429
   168
    val funcs = fold (fold add o snd o snd) fp_decls Termtab.empty
blanchet@58429
   169
blanchet@58429
   170
  in ((funcs, fp_decls', tr_context', ctxt'), ts) end
blanchet@58361
   171
    (* FIXME: also return necessary (co)datatype theorems *)
blanchet@56078
   172
blanchet@56078
   173
blanchet@56078
   174
(** eta-expand quantifiers, let expressions and built-ins *)
blanchet@56078
   175
blanchet@56078
   176
local
blanchet@56078
   177
  fun eta f T t = Abs (Name.uu, T, f (Term.incr_boundvars 1 t $ Bound 0))
blanchet@56078
   178
blanchet@56078
   179
  fun exp f T = eta f (Term.domain_type (Term.domain_type T))
blanchet@56078
   180
blanchet@56078
   181
  fun exp2 T q =
blanchet@56078
   182
    let val U = Term.domain_type T
blanchet@56078
   183
    in Abs (Name.uu, U, q $ eta I (Term.domain_type U) (Bound 0)) end
blanchet@56078
   184
blanchet@56078
   185
  fun expf k i T t =
blanchet@58061
   186
    let val Ts = drop i (fst (SMT_Util.dest_funT k T))
blanchet@56078
   187
    in
blanchet@56078
   188
      Term.incr_boundvars (length Ts) t
blanchet@56078
   189
      |> fold_rev (fn i => fn u => u $ Bound i) (0 upto length Ts - 1)
blanchet@56078
   190
      |> fold_rev (fn T => fn u => Abs (Name.uu, T, u)) Ts
blanchet@56078
   191
    end
blanchet@56078
   192
in
blanchet@56078
   193
blanchet@56078
   194
fun eta_expand ctxt funcs =
blanchet@56078
   195
  let
blanchet@56078
   196
    fun exp_func t T ts =
blanchet@56078
   197
      (case Termtab.lookup funcs t of
blanchet@56078
   198
        SOME k => Term.list_comb (t, ts) |> k <> length ts ? expf k (length ts) T
blanchet@56078
   199
      | NONE => Term.list_comb (t, ts))
blanchet@56078
   200
blanchet@56078
   201
    fun expand ((q as Const (@{const_name All}, _)) $ Abs a) = q $ abs_expand a
blanchet@56078
   202
      | expand ((q as Const (@{const_name All}, T)) $ t) = q $ exp expand T t
blanchet@56078
   203
      | expand (q as Const (@{const_name All}, T)) = exp2 T q
blanchet@56078
   204
      | expand ((q as Const (@{const_name Ex}, _)) $ Abs a) = q $ abs_expand a
blanchet@56078
   205
      | expand ((q as Const (@{const_name Ex}, T)) $ t) = q $ exp expand T t
blanchet@56078
   206
      | expand (q as Const (@{const_name Ex}, T)) = exp2 T q
blanchet@57165
   207
      | expand (Const (@{const_name Let}, _) $ t $ u) = expand (Term.betapply (u, t))
blanchet@57165
   208
      | expand (Const (@{const_name Let}, T) $ t) =
blanchet@56078
   209
          let val U = Term.domain_type (Term.range_type T)
blanchet@56078
   210
          in Abs (Name.uu, U, Bound 0 $ Term.incr_boundvars 1 t) end
blanchet@56078
   211
      | expand (Const (@{const_name Let}, T)) =
blanchet@56078
   212
          let val U = Term.domain_type (Term.range_type T)
blanchet@56078
   213
          in Abs (Name.uu, Term.domain_type T, Abs (Name.uu, U, Bound 0 $ Bound 1)) end
blanchet@56078
   214
      | expand t =
blanchet@56078
   215
          (case Term.strip_comb t of
blanchet@56078
   216
            (u as Const (c as (_, T)), ts) =>
blanchet@58061
   217
              (case SMT_Builtin.dest_builtin ctxt c ts of
blanchet@56078
   218
                SOME (_, k, us, mk) =>
blanchet@56078
   219
                  if k = length us then mk (map expand us)
blanchet@56078
   220
                  else if k < length us then chop k (map expand us) |>> mk |> Term.list_comb
blanchet@56078
   221
                  else expf k (length ts) T (mk (map expand us))
blanchet@56078
   222
              | NONE => exp_func u T (map expand ts))
blanchet@56078
   223
          | (u as Free (_, T), ts) => exp_func u T (map expand ts)
blanchet@56078
   224
          | (Abs a, ts) => Term.list_comb (abs_expand a, map expand ts)
blanchet@56078
   225
          | (u, ts) => Term.list_comb (u, map expand ts))
blanchet@56078
   226
blanchet@56078
   227
    and abs_expand (n, T, t) = Abs (n, T, expand t)
blanchet@57230
   228
blanchet@56078
   229
  in map expand end
blanchet@56078
   230
blanchet@56078
   231
end
blanchet@56078
   232
blanchet@56078
   233
blanchet@56078
   234
(** introduce explicit applications **)
blanchet@56078
   235
blanchet@56078
   236
local
blanchet@56078
   237
  (*
blanchet@56078
   238
    Make application explicit for functions with varying number of arguments.
blanchet@56078
   239
  *)
blanchet@56078
   240
blanchet@56078
   241
  fun add t i = apfst (Termtab.map_default (t, i) (Integer.min i))
blanchet@56078
   242
  fun add_type T = apsnd (Typtab.update (T, ()))
blanchet@56078
   243
blanchet@56078
   244
  fun min_arities t =
blanchet@56078
   245
    (case Term.strip_comb t of
blanchet@56078
   246
      (u as Const _, ts) => add u (length ts) #> fold min_arities ts
blanchet@56078
   247
    | (u as Free _, ts) => add u (length ts) #> fold min_arities ts
blanchet@56078
   248
    | (Abs (_, T, u), ts) => (can dest_funT T ? add_type T) #> min_arities u #> fold min_arities ts
blanchet@56078
   249
    | (_, ts) => fold min_arities ts)
blanchet@56078
   250
blanchet@56078
   251
  fun minimize types t i =
blanchet@56078
   252
    let
blanchet@56078
   253
      fun find_min j [] _ = j
blanchet@56078
   254
        | find_min j (U :: Us) T =
blanchet@56078
   255
            if Typtab.defined types T then j else find_min (j + 1) Us (U --> T)
blanchet@56078
   256
blanchet@56078
   257
      val (Ts, T) = Term.strip_type (Term.type_of t)
blanchet@56078
   258
    in find_min 0 (take i (rev Ts)) T end
blanchet@56078
   259
blanchet@57230
   260
  fun app u (t, T) = (Const (@{const_name fun_app}, T --> T) $ t $ u, Term.range_type T)
blanchet@56078
   261
blanchet@56078
   262
  fun apply i t T ts =
blanchet@56078
   263
    let
blanchet@56078
   264
      val (ts1, ts2) = chop i ts
blanchet@58061
   265
      val (_, U) = SMT_Util.dest_funT i T
blanchet@56078
   266
    in fst (fold app ts2 (Term.list_comb (t, ts1), U)) end
blanchet@56078
   267
in
blanchet@56078
   268
blanchet@56078
   269
fun intro_explicit_application ctxt funcs ts =
blanchet@56078
   270
  let
blanchet@56078
   271
    val (arities, types) = fold min_arities ts (Termtab.empty, Typtab.empty)
blanchet@56078
   272
    val arities' = Termtab.map (minimize types) arities (* FIXME: highly suspicious *)
blanchet@56078
   273
blanchet@56078
   274
    fun app_func t T ts =
blanchet@56078
   275
      if is_some (Termtab.lookup funcs t) then Term.list_comb (t, ts)
blanchet@56078
   276
      else apply (the (Termtab.lookup arities' t)) t T ts
blanchet@56078
   277
blanchet@58061
   278
    fun in_list T f t = SMT_Util.mk_symb_list T (map f (SMT_Util.dest_symb_list t))
blanchet@56078
   279
blanchet@56078
   280
    fun traverse Ts t =
blanchet@56078
   281
      (case Term.strip_comb t of
blanchet@56078
   282
        (q as Const (@{const_name All}, _), [Abs (x, T, u)]) =>
blanchet@56078
   283
          q $ Abs (x, T, in_trigger (T :: Ts) u)
blanchet@56078
   284
      | (q as Const (@{const_name Ex}, _), [Abs (x, T, u)]) =>
blanchet@56078
   285
          q $ Abs (x, T, in_trigger (T :: Ts) u)
blanchet@56078
   286
      | (q as Const (@{const_name Let}, _), [u1, u2 as Abs _]) =>
blanchet@56078
   287
          q $ traverse Ts u1 $ traverse Ts u2
blanchet@56078
   288
      | (u as Const (c as (_, T)), ts) =>
blanchet@58061
   289
          (case SMT_Builtin.dest_builtin ctxt c ts of
blanchet@56078
   290
            SOME (_, k, us, mk) =>
blanchet@56078
   291
              let
blanchet@56078
   292
                val (ts1, ts2) = chop k (map (traverse Ts) us)
blanchet@56078
   293
                val U = Term.strip_type T |>> snd o chop k |> (op --->)
blanchet@56078
   294
              in apply 0 (mk ts1) U ts2 end
blanchet@56078
   295
          | NONE => app_func u T (map (traverse Ts) ts))
blanchet@56078
   296
      | (u as Free (_, T), ts) => app_func u T (map (traverse Ts) ts)
blanchet@56078
   297
      | (u as Bound i, ts) => apply 0 u (nth Ts i) (map (traverse Ts) ts)
blanchet@56078
   298
      | (Abs (n, T, u), ts) => traverses Ts (Abs (n, T, traverse (T::Ts) u)) ts
blanchet@56078
   299
      | (u, ts) => traverses Ts u ts)
blanchet@57230
   300
    and in_trigger Ts ((c as @{const trigger}) $ p $ t) = c $ in_pats Ts p $ traverse Ts t
blanchet@57165
   301
      | in_trigger Ts t = traverse Ts t
blanchet@56078
   302
    and in_pats Ts ps =
blanchet@57230
   303
      in_list @{typ "pattern symb_list"} (in_list @{typ pattern} (in_pat Ts)) ps
blanchet@57230
   304
    and in_pat Ts ((p as Const (@{const_name pat}, _)) $ t) = p $ traverse Ts t
blanchet@57230
   305
      | in_pat Ts ((p as Const (@{const_name nopat}, _)) $ t) = p $ traverse Ts t
blanchet@56078
   306
      | in_pat _ t = raise TERM ("bad pattern", [t])
blanchet@56078
   307
    and traverses Ts t ts = Term.list_comb (t, map (traverse Ts) ts)
blanchet@56078
   308
  in map (traverse []) ts end
blanchet@56078
   309
blanchet@57230
   310
val fun_app_eq = mk_meta_eq @{thm fun_app_def}
blanchet@56078
   311
blanchet@56078
   312
end
blanchet@56078
   313
blanchet@56078
   314
blanchet@56078
   315
(** map HOL formulas to FOL formulas (i.e., separate formulas froms terms) **)
blanchet@56078
   316
blanchet@56078
   317
local
blanchet@56078
   318
  val is_quant = member (op =) [@{const_name All}, @{const_name Ex}]
blanchet@56078
   319
blanchet@56078
   320
  val fol_rules = [
blanchet@56078
   321
    Let_def,
blanchet@56078
   322
    @{lemma "P = True == P" by (rule eq_reflection) simp},
blanchet@56078
   323
    @{lemma "if P then True else False == P" by (rule eq_reflection) simp}]
blanchet@56078
   324
blanchet@56078
   325
  exception BAD_PATTERN of unit
blanchet@56078
   326
blanchet@56078
   327
  fun wrap_in_if pat t =
blanchet@56104
   328
    if pat then raise BAD_PATTERN () else @{const If (bool)} $ t $ @{const True} $ @{const False}
blanchet@56078
   329
blanchet@56078
   330
  fun is_builtin_conn_or_pred ctxt c ts =
blanchet@58061
   331
    is_some (SMT_Builtin.dest_builtin_conn ctxt c ts) orelse
blanchet@58061
   332
    is_some (SMT_Builtin.dest_builtin_pred ctxt c ts)
blanchet@56078
   333
in
blanchet@56078
   334
blanchet@56078
   335
fun folify ctxt =
blanchet@56078
   336
  let
blanchet@58061
   337
    fun in_list T f t = SMT_Util.mk_symb_list T (map_filter f (SMT_Util.dest_symb_list t))
blanchet@56078
   338
blanchet@56078
   339
    fun in_term pat t =
blanchet@56078
   340
      (case Term.strip_comb t of
blanchet@56078
   341
        (@{const True}, []) => t
blanchet@56078
   342
      | (@{const False}, []) => t
blanchet@56078
   343
      | (u as Const (@{const_name If}, _), [t1, t2, t3]) =>
blanchet@56104
   344
          if pat then raise BAD_PATTERN () else u $ in_form t1 $ in_term pat t2 $ in_term pat t3
blanchet@56078
   345
      | (Const (c as (n, _)), ts) =>
blanchet@56078
   346
          if is_builtin_conn_or_pred ctxt c ts then wrap_in_if pat (in_form t)
blanchet@56078
   347
          else if is_quant n then wrap_in_if pat (in_form t)
blanchet@56078
   348
          else Term.list_comb (Const c, map (in_term pat) ts)
blanchet@56078
   349
      | (Free c, ts) => Term.list_comb (Free c, map (in_term pat) ts)
blanchet@56078
   350
      | _ => t)
blanchet@56078
   351
blanchet@57230
   352
    and in_pat ((p as Const (@{const_name pat}, _)) $ t) =
blanchet@56078
   353
          p $ in_term true t
blanchet@57230
   354
      | in_pat ((p as Const (@{const_name nopat}, _)) $ t) =
blanchet@56078
   355
          p $ in_term true t
blanchet@56078
   356
      | in_pat t = raise TERM ("bad pattern", [t])
blanchet@56078
   357
blanchet@56078
   358
    and in_pats ps =
blanchet@57230
   359
      in_list @{typ "pattern symb_list"} (SOME o in_list @{typ pattern} (try in_pat)) ps
blanchet@56078
   360
blanchet@57230
   361
    and in_trigger ((c as @{const trigger}) $ p $ t) = c $ in_pats p $ in_form t
blanchet@57165
   362
      | in_trigger t = in_form t
blanchet@56078
   363
blanchet@56078
   364
    and in_form t =
blanchet@56078
   365
      (case Term.strip_comb t of
blanchet@56078
   366
        (q as Const (qn, _), [Abs (n, T, u)]) =>
blanchet@56078
   367
          if is_quant qn then q $ Abs (n, T, in_trigger u)
blanchet@56078
   368
          else in_term false t
blanchet@56078
   369
      | (Const c, ts) =>
blanchet@58061
   370
          (case SMT_Builtin.dest_builtin_conn ctxt c ts of
blanchet@56078
   371
            SOME (_, _, us, mk) => mk (map in_form us)
blanchet@56078
   372
          | NONE =>
blanchet@58061
   373
              (case SMT_Builtin.dest_builtin_pred ctxt c ts of
blanchet@56078
   374
                SOME (_, _, us, mk) => mk (map (in_term false) us)
blanchet@56078
   375
              | NONE => in_term false t))
blanchet@56078
   376
      | _ => in_term false t)
blanchet@56078
   377
  in
blanchet@56078
   378
    map in_form #>
blanchet@56078
   379
    pair (fol_rules, I)
blanchet@56078
   380
  end
blanchet@56078
   381
blanchet@56078
   382
end
blanchet@56078
   383
blanchet@56078
   384
blanchet@56078
   385
(* translation into intermediate format *)
blanchet@56078
   386
blanchet@56078
   387
(** utility functions **)
blanchet@56078
   388
blanchet@56078
   389
val quantifier = (fn
blanchet@56078
   390
    @{const_name All} => SOME SForall
blanchet@56078
   391
  | @{const_name Ex} => SOME SExists
blanchet@56078
   392
  | _ => NONE)
blanchet@56078
   393
blanchet@56078
   394
fun group_quant qname Ts (t as Const (q, _) $ Abs (_, T, u)) =
blanchet@56078
   395
      if q = qname then group_quant qname (T :: Ts) u else (Ts, t)
blanchet@56078
   396
  | group_quant _ Ts t = (Ts, t)
blanchet@56078
   397
blanchet@57230
   398
fun dest_pat (Const (@{const_name pat}, _) $ t) = (t, true)
blanchet@57230
   399
  | dest_pat (Const (@{const_name nopat}, _) $ t) = (t, false)
blanchet@56078
   400
  | dest_pat t = raise TERM ("bad pattern", [t])
blanchet@56078
   401
blanchet@56078
   402
fun dest_pats [] = I
blanchet@56078
   403
  | dest_pats ts =
blanchet@56078
   404
      (case map dest_pat ts |> split_list ||> distinct (op =) of
blanchet@56078
   405
        (ps, [true]) => cons (SPat ps)
blanchet@56078
   406
      | (ps, [false]) => cons (SNoPat ps)
blanchet@56078
   407
      | _ => raise TERM ("bad multi-pattern", ts))
blanchet@56078
   408
blanchet@57230
   409
fun dest_trigger (@{const trigger} $ tl $ t) =
blanchet@58061
   410
      (rev (fold (dest_pats o SMT_Util.dest_symb_list) (SMT_Util.dest_symb_list tl) []), t)
blanchet@56078
   411
  | dest_trigger t = ([], t)
blanchet@56078
   412
blanchet@56078
   413
fun dest_quant qn T t = quantifier qn |> Option.map (fn q =>
blanchet@56078
   414
  let
blanchet@56078
   415
    val (Ts, u) = group_quant qn [T] t
blanchet@56078
   416
    val (ps, p) = dest_trigger u
blanchet@57165
   417
  in (q, rev Ts, ps, p) end)
blanchet@56078
   418
blanchet@56078
   419
fun fold_map_pat f (SPat ts) = fold_map f ts #>> SPat
blanchet@56078
   420
  | fold_map_pat f (SNoPat ts) = fold_map f ts #>> SNoPat
blanchet@56078
   421
blanchet@56078
   422
blanchet@56078
   423
(** translation from Isabelle terms into SMT intermediate terms **)
blanchet@56078
   424
blanchet@58429
   425
fun intermediate logic dtyps builtin ctxt ts trx =
blanchet@56078
   426
  let
blanchet@56078
   427
    fun transT (T as TFree _) = add_typ T true
blanchet@56078
   428
      | transT (T as TVar _) = (fn _ => raise TYPE ("bad SMT type", [T], []))
blanchet@56078
   429
      | transT (T as Type _) =
blanchet@58061
   430
          (case SMT_Builtin.dest_builtin_typ ctxt T of
blanchet@56078
   431
            SOME n => pair n
blanchet@56078
   432
          | NONE => add_typ T true)
blanchet@56078
   433
blanchet@56078
   434
    fun app n ts = SApp (n, ts)
blanchet@56078
   435
blanchet@56078
   436
    fun trans t =
blanchet@56078
   437
      (case Term.strip_comb t of
blanchet@56078
   438
        (Const (qn, _), [Abs (_, T, t1)]) =>
blanchet@56078
   439
          (case dest_quant qn T t1 of
blanchet@57165
   440
            SOME (q, Ts, ps, b) =>
blanchet@56078
   441
              fold_map transT Ts ##>> fold_map (fold_map_pat trans) ps ##>>
blanchet@57165
   442
              trans b #>> (fn ((Ts', ps'), b') => SQua (q, Ts', ps', b'))
blanchet@56078
   443
          | NONE => raise TERM ("unsupported quantifier", [t]))
blanchet@56078
   444
      | (Const (@{const_name Let}, _), [t1, Abs (_, T, t2)]) =>
blanchet@56078
   445
          transT T ##>> trans t1 ##>> trans t2 #>> (fn ((U, u1), u2) => SLet (U, u1, u2))
blanchet@56078
   446
      | (u as Const (c as (_, T)), ts) =>
blanchet@56078
   447
          (case builtin ctxt c ts of
blanchet@56078
   448
            SOME (n, _, us, _) => fold_map trans us #>> app n
blanchet@56078
   449
          | NONE => transs u T ts)
blanchet@56078
   450
      | (u as Free (_, T), ts) => transs u T ts
blanchet@56078
   451
      | (Bound i, []) => pair (SVar i)
blanchet@56078
   452
      | _ => raise TERM ("bad SMT term", [t]))
blanchet@57230
   453
blanchet@56078
   454
    and transs t T ts =
blanchet@58061
   455
      let val (Us, U) = SMT_Util.dest_funT (length ts) T
blanchet@56078
   456
      in
blanchet@56078
   457
        fold_map transT Us ##>> transT U #-> (fn Up =>
blanchet@56104
   458
          add_fun t (SOME Up) ##>> fold_map trans ts #>> SApp)
blanchet@56078
   459
      end
blanchet@56078
   460
blanchet@56078
   461
    val (us, trx') = fold_map trans ts trx
blanchet@58429
   462
  in ((sign_of (logic ts) dtyps trx', us), trx') end
blanchet@56078
   463
blanchet@56078
   464
blanchet@56078
   465
(* translation *)
blanchet@56078
   466
blanchet@56078
   467
structure Configs = Generic_Data
blanchet@56078
   468
(
blanchet@58061
   469
  type T = (Proof.context -> config) SMT_Util.dict
blanchet@56078
   470
  val empty = []
blanchet@56078
   471
  val extend = I
blanchet@58061
   472
  fun merge data = SMT_Util.dict_merge fst data
blanchet@56078
   473
)
blanchet@56078
   474
blanchet@58061
   475
fun add_config (cs, cfg) = Configs.map (SMT_Util.dict_update (cs, cfg))
blanchet@56078
   476
blanchet@57230
   477
fun get_config ctxt =
blanchet@58061
   478
  let val cs = SMT_Config.solver_class_of ctxt
blanchet@56078
   479
  in
blanchet@58061
   480
    (case SMT_Util.dict_get (Configs.get (Context.Proof ctxt)) cs of
blanchet@56078
   481
      SOME cfg => cfg ctxt
blanchet@56078
   482
    | NONE => error ("SMT: no translation configuration found " ^
blanchet@58061
   483
        "for solver class " ^ quote (SMT_Util.string_of_class cs)))
blanchet@56078
   484
  end
blanchet@56078
   485
blanchet@57239
   486
fun translate ctxt smt_options comments ithms =
blanchet@56078
   487
  let
blanchet@58360
   488
    val {logic, fp_kinds, serialize} = get_config ctxt
blanchet@56078
   489
blanchet@56078
   490
    fun no_dtyps (tr_context, ctxt) ts =
blanchet@58429
   491
      ((Termtab.empty, [], tr_context, ctxt), ts)
blanchet@56078
   492
blanchet@58061
   493
    val ts1 = map (Envir.beta_eta_contract o SMT_Util.prop_of o snd) ithms
blanchet@56078
   494
blanchet@58429
   495
    val ((funcs, dtyps, tr_context, ctxt1), ts2) =
blanchet@56078
   496
      ((empty_tr_context, ctxt), ts1)
blanchet@58361
   497
      |-> (if null fp_kinds then no_dtyps else collect_co_datatypes fp_kinds)
blanchet@56078
   498
blanchet@56078
   499
    fun is_binder (Const (@{const_name Let}, _) $ _) = true
blanchet@56078
   500
      | is_binder t = Lambda_Lifting.is_quantifier t
blanchet@56078
   501
blanchet@56078
   502
    fun mk_trigger ((q as Const (@{const_name All}, _)) $ Abs (n, T, t)) =
blanchet@56078
   503
          q $ Abs (n, T, mk_trigger t)
blanchet@56078
   504
      | mk_trigger (eq as (Const (@{const_name HOL.eq}, T) $ lhs $ _)) =
blanchet@57230
   505
          Term.domain_type T --> @{typ pattern}
blanchet@57230
   506
          |> (fn T => Const (@{const_name pat}, T) $ lhs)
blanchet@58061
   507
          |> SMT_Util.mk_symb_list @{typ pattern} o single
blanchet@58061
   508
          |> SMT_Util.mk_symb_list @{typ "pattern symb_list"} o single
blanchet@57230
   509
          |> (fn t => @{const trigger} $ t $ eq)
blanchet@56078
   510
      | mk_trigger t = t
blanchet@56078
   511
blanchet@57541
   512
    val (ctxt2, (ts3, ll_defs)) =
blanchet@56078
   513
      ts2
blanchet@56078
   514
      |> eta_expand ctxt1 funcs
blanchet@56078
   515
      |> rpair ctxt1
blanchet@56078
   516
      |-> Lambda_Lifting.lift_lambdas NONE is_binder
blanchet@57541
   517
      |-> (fn (ts', ll_defs) => fn ctxt' =>
blanchet@57541
   518
          (ctxt', (intro_explicit_application ctxt' funcs (map mk_trigger ll_defs @ ts'), ll_defs)))
blanchet@56078
   519
blanchet@56078
   520
    val ((rewrite_rules, builtin), ts4) = folify ctxt2 ts3
blanchet@56104
   521
      |>> apfst (cons fun_app_eq)
blanchet@58429
   522
val _ = dtyps : (BNF_Util.fp_kind * (string * (string * (string * string) list) list)) list (*###*)
blanchet@56078
   523
  in
blanchet@56078
   524
    (ts4, tr_context)
blanchet@58429
   525
    |-> intermediate logic dtyps (builtin SMT_Builtin.dest_builtin) ctxt2
blanchet@57239
   526
    |>> uncurry (serialize smt_options comments)
blanchet@57541
   527
    ||> replay_data_of ctxt2 ll_defs rewrite_rules ithms
blanchet@56078
   528
  end
blanchet@56078
   529
blanchet@57229
   530
end;