src/HOL/SMT/Tools/smt_translate.ML
changeset 33017 4fb8a33f74d6
parent 32618 42865636d006
child 33299 73af7831ba1e
equal deleted inserted replaced
33016:b73b74fe23c3 33017:4fb8a33f74d6
    32   val bv_rotate: (int -> string) -> builtin_fun
    32   val bv_rotate: (int -> string) -> builtin_fun
    33   val bv_extend: (int -> string) -> builtin_fun
    33   val bv_extend: (int -> string) -> builtin_fun
    34   val bv_extract: (int -> int -> string) -> builtin_fun
    34   val bv_extract: (int -> int -> string) -> builtin_fun
    35 
    35 
    36   (* configuration options *)
    36   (* configuration options *)
    37   datatype prefixes = Prefixes of {
    37   type prefixes = {
    38     var_prefix: string,
    38     var_prefix: string,
    39     typ_prefix: string,
    39     typ_prefix: string,
    40     fun_prefix: string,
    40     fun_prefix: string,
    41     pred_prefix: string }
    41     pred_prefix: string }
    42   datatype markers = Markers of {
    42   type markers = {
    43     term_marker: string,
    43     term_marker: string,
    44     formula_marker: string }
    44     formula_marker: string }
    45   datatype builtins = Builtins of {
    45   type builtins = {
    46     builtin_typ: typ -> string option,
    46     builtin_typ: typ -> string option,
    47     builtin_num: int * typ -> string option,
    47     builtin_num: int * typ -> string option,
    48     builtin_fun: bool -> builtin_table }
    48     builtin_fun: bool -> builtin_table }
    49   datatype sign = Sign of {
    49   type sign = {
    50     typs: string list,
    50     typs: string list,
    51     funs: (string * (string list * string)) list,
    51     funs: (string * (string list * string)) list,
    52     preds: (string * string list) list }
    52     preds: (string * string list) list }
    53   datatype config = Config of {
    53   type config = {
    54     strict: bool,
    54     strict: bool,
    55     prefixes: prefixes,
    55     prefixes: prefixes,
    56     markers: markers,
    56     markers: markers,
    57     builtins: builtins,
    57     builtins: builtins,
    58     serialize: sign -> (string, string) sterm list -> TextIO.outstream -> unit}
    58     serialize: sign -> (string, string) sterm list -> TextIO.outstream -> unit}
    59   datatype recon = Recon of {typs: typ Symtab.table, terms: term Symtab.table}
    59   type recon = {typs: typ Symtab.table, terms: term Symtab.table}
    60 
    60 
    61   val translate: config -> theory -> thm list -> TextIO.outstream ->
    61   val translate: config -> theory -> thm list -> TextIO.outstream ->
    62     recon * thm list
    62     recon * thm list
    63 
    63 
    64   val dest_binT: typ -> int
    64   val dest_binT: typ -> int
   157 end
   157 end
   158 
   158 
   159 
   159 
   160 (* Configuration options *)
   160 (* Configuration options *)
   161 
   161 
   162 datatype prefixes = Prefixes of {
   162 type prefixes = {
   163   var_prefix: string,
   163   var_prefix: string,
   164   typ_prefix: string,
   164   typ_prefix: string,
   165   fun_prefix: string,
   165   fun_prefix: string,
   166   pred_prefix: string }
   166   pred_prefix: string }
   167 datatype markers = Markers of {
   167 type markers = {
   168   term_marker: string,
   168   term_marker: string,
   169   formula_marker: string }
   169   formula_marker: string }
   170 datatype builtins = Builtins of {
   170 type builtins = {
   171   builtin_typ: typ -> string option,
   171   builtin_typ: typ -> string option,
   172   builtin_num: int * typ -> string option,
   172   builtin_num: int * typ -> string option,
   173   builtin_fun: bool -> builtin_table }
   173   builtin_fun: bool -> builtin_table }
   174 datatype sign = Sign of {
   174 type sign = {
   175   typs: string list,
   175   typs: string list,
   176   funs: (string * (string list * string)) list,
   176   funs: (string * (string list * string)) list,
   177   preds: (string * string list) list }
   177   preds: (string * string list) list }
   178 datatype config = Config of {
   178 type config = {
   179   strict: bool,
   179   strict: bool,
   180   prefixes: prefixes,
   180   prefixes: prefixes,
   181   markers: markers,
   181   markers: markers,
   182   builtins: builtins,
   182   builtins: builtins,
   183   serialize: sign -> (string, string) sterm list -> TextIO.outstream -> unit}
   183   serialize: sign -> (string, string) sterm list -> TextIO.outstream -> unit}
   184 datatype recon = Recon of {typs: typ Symtab.table, terms: term Symtab.table}
   184 type recon = {typs: typ Symtab.table, terms: term Symtab.table}
   185 
   185 
   186 
   186 
   187 (* Translate Isabelle/HOL terms into SMT intermediate terms.
   187 (* Translate Isabelle/HOL terms into SMT intermediate terms.
   188    We assume that lambda-lifting has been performed before, i.e., lambda
   188    We assume that lambda-lifting has been performed before, i.e., lambda
   189    abstractions occur only at quantifiers and let expressions.
   189    abstractions occur only at quantifiers and let expressions.
   407   fun lookup_fun true (_, funs, _) = Termtab.lookup funs
   407   fun lookup_fun true (_, funs, _) = Termtab.lookup funs
   408     | lookup_fun false (_, _, preds) = Termtab.lookup preds
   408     | lookup_fun false (_, _, preds) = Termtab.lookup preds
   409   fun add_typ x (typs, funs, preds) = (Typtab.update x typs, funs, preds)
   409   fun add_typ x (typs, funs, preds) = (Typtab.update x typs, funs, preds)
   410   fun add_fun true x (typs, funs, preds) = (typs, Termtab.update x funs, preds)
   410   fun add_fun true x (typs, funs, preds) = (typs, Termtab.update x funs, preds)
   411     | add_fun false x (typs, funs, preds) = (typs, funs, Termtab.update x preds)
   411     | add_fun false x (typs, funs, preds) = (typs, funs, Termtab.update x preds)
   412   fun make_sign (typs, funs, preds) = Sign {
   412   fun make_sign (typs, funs, preds) = {
   413     typs = map snd (Typtab.dest typs),
   413     typs = map snd (Typtab.dest typs),
   414     funs = map snd (Termtab.dest funs),
   414     funs = map snd (Termtab.dest funs),
   415     preds = map (apsnd fst o snd) (Termtab.dest preds) }
   415     preds = map (apsnd fst o snd) (Termtab.dest preds) }
   416   fun make_rtab (typs, funs, preds) =
   416   fun make_rtab (typs, funs, preds) =
   417     let
   417     let
   418       val rTs = Typtab.dest typs |> map swap |> Symtab.make
   418       val rTs = Typtab.dest typs |> map swap |> Symtab.make
   419       val rts = Termtab.dest funs @ Termtab.dest preds
   419       val rts = Termtab.dest funs @ Termtab.dest preds
   420         |> map (apfst fst o swap) |> Symtab.make
   420         |> map (apfst fst o swap) |> Symtab.make
   421     in Recon {typs=rTs, terms=rts} end
   421     in {typs=rTs, terms=rts} end
   422 
   422 
   423   fun either f g x = (case f x of NONE => g x | y => y)
   423   fun either f g x = (case f x of NONE => g x | y => y)
   424 
   424 
   425   fun rep_typ (Builtins {builtin_typ, ...}) T (st as (vars, ns, sgn)) =
   425   fun rep_typ ({builtin_typ, ...} : builtins) T (st as (vars, ns, sgn)) =
   426     (case either builtin_typ (lookup_typ sgn) T of
   426     (case either builtin_typ (lookup_typ sgn) T of
   427       SOME n => (n, st)
   427       SOME n => (n, st)
   428     | NONE =>
   428     | NONE =>
   429         let val (n, ns') = fresh_typ ns
   429         let val (n, ns') = fresh_typ ns
   430         in (n, (vars, ns', add_typ (T, n) sgn)) end)
   430         in (n, (vars, ns', add_typ (T, n) sgn)) end)
   442           val (uns, (vars, ns, sgn)) =
   442           val (uns, (vars, ns, sgn)) =
   443             st |> fold_map (rep_typ bs) Us ||>> rep_typ bs U
   443             st |> fold_map (rep_typ bs) Us ||>> rep_typ bs U
   444           val (n, ns') = fresh_fun loc ns
   444           val (n, ns') = fresh_fun loc ns
   445         in (n, (vars, ns', add_fun loc (t, (n, uns)) sgn)) end)
   445         in (n, (vars, ns', add_fun loc (t, (n, uns)) sgn)) end)
   446 
   446 
   447   fun rep_num (bs as Builtins {builtin_num, ...}) (i, T) st =
   447   fun rep_num (bs as {builtin_num, ...} : builtins) (i, T) st =
   448     (case builtin_num (i, T) of
   448     (case builtin_num (i, T) of
   449       SOME n => (n, st)
   449       SOME n => (n, st)
   450     | NONE => rep_fun bs true (HOLogic.mk_number T i) T 0 st)
   450     | NONE => rep_fun bs true (HOLogic.mk_number T i) T 0 st)
   451 in
   451 in
   452 fun signature_of prefixes markers builtins thy ts =
   452 fun signature_of prefixes markers builtins thy ts =
   453   let
   453   let
   454     val Prefixes {var_prefix, typ_prefix, fun_prefix, pred_prefix} = prefixes
   454     val {var_prefix, typ_prefix, fun_prefix, pred_prefix} = prefixes
   455     val Markers {formula_marker, term_marker} = markers
   455     val {formula_marker, term_marker} = markers
   456     val Builtins {builtin_fun, ...} = builtins
   456     val {builtin_fun, ...} = builtins
   457 
   457 
   458     fun sign loc t =
   458     fun sign loc t =
   459       (case t of
   459       (case t of
   460         SVar i => pair (SVar i)
   460         SVar i => pair (SVar i)
   461       | SApp (c as SConst (@{const_name term}, _), [u]) =>
   461       | SApp (c as SConst (@{const_name term}, _), [u]) =>
   491 
   491 
   492 
   492 
   493 (* Combination of all translation functions and invocation of serialization. *)
   493 (* Combination of all translation functions and invocation of serialization. *)
   494 
   494 
   495 fun translate config thy thms stream =
   495 fun translate config thy thms stream =
   496   let val Config {strict, prefixes, markers, builtins, serialize} = config
   496   let val {strict, prefixes, markers, builtins, serialize} = config
   497   in
   497   in
   498     map Thm.prop_of thms
   498     map Thm.prop_of thms
   499     |> SMT_Monomorph.monomorph thy
   499     |> SMT_Monomorph.monomorph thy
   500     |> intermediate
   500     |> intermediate
   501     |> (if strict then separate thy else pair [])
   501     |> (if strict then separate thy else pair [])