src/HOL/Tools/SMT/smt_translate.ML
changeset 36898 8e55aa1306c5
child 36899 bcd6fce5bf06
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/HOL/Tools/SMT/smt_translate.ML	Wed May 12 23:54:02 2010 +0200
     1.3 @@ -0,0 +1,344 @@
     1.4 +(*  Title:      HOL/Tools/SMT/smt_translate.ML
     1.5 +    Author:     Sascha Boehme, TU Muenchen
     1.6 +
     1.7 +Translate theorems into an SMT intermediate format and serialize them.
     1.8 +*)
     1.9 +
    1.10 +signature SMT_TRANSLATE =
    1.11 +sig
    1.12 +  (* intermediate term structure *)
    1.13 +  datatype squant = SForall | SExists
    1.14 +  datatype 'a spattern = SPat of 'a list | SNoPat of 'a list
    1.15 +  datatype sterm =
    1.16 +    SVar of int |
    1.17 +    SApp of string * sterm list |
    1.18 +    SLet of string * sterm * sterm |
    1.19 +    SQua of squant * string list * sterm spattern list * sterm
    1.20 +
    1.21 +  (* configuration options *)
    1.22 +  type prefixes = {sort_prefix: string, func_prefix: string}
    1.23 +  type strict = {
    1.24 +    is_builtin_conn: string * typ -> bool,
    1.25 +    is_builtin_pred: string * typ -> bool,
    1.26 +    is_builtin_distinct: bool}
    1.27 +  type builtins = {
    1.28 +    builtin_typ: typ -> string option,
    1.29 +    builtin_num: typ -> int -> string option,
    1.30 +    builtin_fun: string * typ -> term list -> (string * term list) option }
    1.31 +  datatype smt_theory = Integer | Real | Bitvector
    1.32 +  type sign = {
    1.33 +    theories: smt_theory list,
    1.34 +    sorts: string list,
    1.35 +    funcs: (string * (string list * string)) list }
    1.36 +  type config = {
    1.37 +    prefixes: prefixes,
    1.38 +    strict: strict option,
    1.39 +    builtins: builtins,
    1.40 +    serialize: string list -> sign -> sterm list -> string }
    1.41 +  type recon = {
    1.42 +    typs: typ Symtab.table,
    1.43 +    terms: term Symtab.table,
    1.44 +    unfolds: thm list,
    1.45 +    assms: thm list option }
    1.46 +
    1.47 +  val translate: config -> Proof.context -> string list -> thm list ->
    1.48 +    string * recon
    1.49 +end
    1.50 +
    1.51 +structure SMT_Translate: SMT_TRANSLATE =
    1.52 +struct
    1.53 +
    1.54 +(* intermediate term structure *)
    1.55 +
    1.56 +datatype squant = SForall | SExists
    1.57 +
    1.58 +datatype 'a spattern = SPat of 'a list | SNoPat of 'a list
    1.59 +
    1.60 +datatype sterm =
    1.61 +  SVar of int |
    1.62 +  SApp of string * sterm list |
    1.63 +  SLet of string * sterm * sterm |
    1.64 +  SQua of squant * string list * sterm spattern list * sterm
    1.65 +
    1.66 +
    1.67 +
    1.68 +(* configuration options *)
    1.69 +
    1.70 +type prefixes = {sort_prefix: string, func_prefix: string}
    1.71 +
    1.72 +type strict = {
    1.73 +  is_builtin_conn: string * typ -> bool,
    1.74 +  is_builtin_pred: string * typ -> bool,
    1.75 +  is_builtin_distinct: bool}
    1.76 +
    1.77 +type builtins = {
    1.78 +  builtin_typ: typ -> string option,
    1.79 +  builtin_num: typ -> int -> string option,
    1.80 +  builtin_fun: string * typ -> term list -> (string * term list) option }
    1.81 +
    1.82 +datatype smt_theory = Integer | Real | Bitvector
    1.83 +
    1.84 +type sign = {
    1.85 +  theories: smt_theory list,
    1.86 +  sorts: string list,
    1.87 +  funcs: (string * (string list * string)) list }
    1.88 +
    1.89 +type config = {
    1.90 +  prefixes: prefixes,
    1.91 +  strict: strict option,
    1.92 +  builtins: builtins,
    1.93 +  serialize: string list -> sign -> sterm list -> string }
    1.94 +
    1.95 +type recon = {
    1.96 +  typs: typ Symtab.table,
    1.97 +  terms: term Symtab.table,
    1.98 +  unfolds: thm list,
    1.99 +  assms: thm list option }
   1.100 +
   1.101 +
   1.102 +
   1.103 +(* utility functions *)
   1.104 +
   1.105 +val dest_funT =
   1.106 +  let
   1.107 +    fun dest Ts 0 T = (rev Ts, T)
   1.108 +      | dest Ts i (Type ("fun", [T, U])) = dest (T::Ts) (i-1) U
   1.109 +      | dest _ _ T = raise TYPE ("dest_funT", [T], [])
   1.110 +  in dest [] end
   1.111 +
   1.112 +val quantifier = (fn
   1.113 +    @{const_name All} => SOME SForall
   1.114 +  | @{const_name Ex} => SOME SExists
   1.115 +  | _ => NONE)
   1.116 +
   1.117 +fun group_quant qname Ts (t as Const (q, _) $ Abs (_, T, u)) =
   1.118 +      if q = qname then group_quant qname (T :: Ts) u else (Ts, t)
   1.119 +  | group_quant _ Ts t = (Ts, t)
   1.120 +
   1.121 +fun dest_pat ts (Const (@{const_name pat}, _) $ t) = SPat (rev (t :: ts))
   1.122 +  | dest_pat ts (Const (@{const_name nopat}, _) $ t) = SNoPat (rev (t :: ts))
   1.123 +  | dest_pat ts (Const (@{const_name andpat}, _) $ p $ t) = dest_pat (t::ts) p
   1.124 +  | dest_pat _ t = raise TERM ("dest_pat", [t])
   1.125 +
   1.126 +fun dest_trigger (@{term trigger} $ tl $ t) =
   1.127 +      (map (dest_pat []) (HOLogic.dest_list tl), t)
   1.128 +  | dest_trigger t = ([], t)
   1.129 +
   1.130 +fun dest_quant qn T t = quantifier qn |> Option.map (fn q =>
   1.131 +  let
   1.132 +    val (Ts, u) = group_quant qn [T] t
   1.133 +    val (ps, b) = dest_trigger u
   1.134 +  in (q, rev Ts, ps, b) end)
   1.135 +
   1.136 +fun fold_map_pat f (SPat ts) = fold_map f ts #>> SPat
   1.137 +  | fold_map_pat f (SNoPat ts) = fold_map f ts #>> SNoPat
   1.138 +
   1.139 +fun prop_of thm = HOLogic.dest_Trueprop (Thm.prop_of thm)
   1.140 +
   1.141 +
   1.142 +
   1.143 +(* enforce a strict separation between formulas and terms *)
   1.144 +
   1.145 +val term_eq_rewr = @{lemma "x term_eq y == x = y" by (simp add: term_eq_def)}
   1.146 +
   1.147 +val term_bool = @{lemma "~(True term_eq False)" by (simp add: term_eq_def)}
   1.148 +val term_bool' = Simplifier.rewrite_rule [term_eq_rewr] term_bool
   1.149 +
   1.150 +
   1.151 +val needs_rewrite = Thm.prop_of #> Term.exists_subterm (fn
   1.152 +    Const (@{const_name Let}, _) => true
   1.153 +  | @{term "op = :: bool => _"} $ _ $ @{term True} => true
   1.154 +  | Const (@{const_name If}, _) $ _ $ @{term True} $ @{term False} => true
   1.155 +  | _ => false)
   1.156 +
   1.157 +val rewrite_rules = [
   1.158 +  Let_def,
   1.159 +  @{lemma "P = True == P" by (rule eq_reflection) simp},
   1.160 +  @{lemma "if P then True else False == P" by (rule eq_reflection) simp}]
   1.161 +
   1.162 +fun rewrite ctxt = Simplifier.full_rewrite
   1.163 +  (Simplifier.context ctxt empty_ss addsimps rewrite_rules)
   1.164 +
   1.165 +fun normalize ctxt thm =
   1.166 +  if needs_rewrite thm then Conv.fconv_rule (rewrite ctxt) thm else thm
   1.167 +
   1.168 +val unfold_rules = term_eq_rewr :: rewrite_rules
   1.169 +
   1.170 +
   1.171 +val revert_types =
   1.172 +  let
   1.173 +    fun revert @{typ prop} = @{typ bool}
   1.174 +      | revert (Type (n, Ts)) = Type (n, map revert Ts)
   1.175 +      | revert T = T
   1.176 +  in Term.map_types revert end
   1.177 +
   1.178 +
   1.179 +fun strictify {is_builtin_conn, is_builtin_pred, is_builtin_distinct} ctxt =
   1.180 +  let
   1.181 +
   1.182 +    fun is_builtin_conn' (@{const_name True}, _) = false
   1.183 +      | is_builtin_conn' (@{const_name False}, _) = false
   1.184 +      | is_builtin_conn' c = is_builtin_conn c
   1.185 +
   1.186 +    val propT = @{typ prop} and boolT = @{typ bool}
   1.187 +    val as_propT = (fn @{typ bool} => propT | T => T)
   1.188 +    fun mapTs f g = Term.strip_type #> (fn (Ts, T) => map f Ts ---> g T)
   1.189 +    fun conn (n, T) = (n, mapTs as_propT as_propT T)
   1.190 +    fun pred (n, T) = (n, mapTs I as_propT T)
   1.191 +
   1.192 +    val term_eq = @{term "op = :: bool => _"} |> Term.dest_Const |> pred
   1.193 +    fun as_term t = Const term_eq $ t $ @{term True}
   1.194 +
   1.195 +    val if_term = Const (@{const_name If}, [propT, boolT, boolT] ---> boolT)
   1.196 +    fun wrap_in_if t = if_term $ t $ @{term True} $ @{term False}
   1.197 +
   1.198 +    fun in_list T f t = HOLogic.mk_list T (map f (HOLogic.dest_list t))
   1.199 +
   1.200 +    fun in_term t =
   1.201 +      (case Term.strip_comb t of
   1.202 +        (c as Const (@{const_name If}, _), [t1, t2, t3]) =>
   1.203 +          c $ in_form t1 $ in_term t2 $ in_term t3
   1.204 +      | (h as Const c, ts) =>
   1.205 +          if is_builtin_conn' (conn c) orelse is_builtin_pred (pred c)
   1.206 +          then wrap_in_if (in_form t)
   1.207 +          else Term.list_comb (h, map in_term ts)
   1.208 +      | (h as Free _, ts) => Term.list_comb (h, map in_term ts)
   1.209 +      | _ => t)
   1.210 +
   1.211 +    and in_pat ((c as Const (@{const_name pat}, _)) $ t) = c $ in_term t
   1.212 +      | in_pat ((c as Const (@{const_name nopat}, _)) $ t) = c $ in_term t
   1.213 +      | in_pat ((c as Const (@{const_name andpat}, _)) $ p $ t) =
   1.214 +          c $ in_pat p $ in_term t
   1.215 +      | in_pat t = raise TERM ("in_pat", [t])
   1.216 +
   1.217 +    and in_pats p = in_list @{typ pattern} in_pat p
   1.218 +
   1.219 +    and in_trig ((c as @{term trigger}) $ p $ t) = c $ in_pats p $ in_form t
   1.220 +      | in_trig t = in_form t
   1.221 +
   1.222 +    and in_form t =
   1.223 +      (case Term.strip_comb t of
   1.224 +        (q as Const (qn, _), [Abs (n, T, t')]) =>
   1.225 +          if is_some (quantifier qn) then q $ Abs (n, T, in_trig t')
   1.226 +          else as_term (in_term t)
   1.227 +      | (Const (c as (@{const_name distinct}, T)), [t']) =>
   1.228 +          if is_builtin_distinct then Const (pred c) $ in_list T in_term t'
   1.229 +          else as_term (in_term t)
   1.230 +      | (Const c, ts) =>
   1.231 +          if is_builtin_conn (conn c)
   1.232 +          then Term.list_comb (Const (conn c), map in_form ts)
   1.233 +          else if is_builtin_pred (pred c)
   1.234 +          then Term.list_comb (Const (pred c), map in_term ts)
   1.235 +          else as_term (in_term t)
   1.236 +      | _ => as_term (in_term t))
   1.237 +  in
   1.238 +    map (normalize ctxt) #> (fn thms => ((unfold_rules, term_bool' :: thms),
   1.239 +    map (in_form o prop_of) (term_bool :: thms)))
   1.240 +  end
   1.241 +
   1.242 +
   1.243 +
   1.244 +(* translation from Isabelle terms into SMT intermediate terms *)
   1.245 +
   1.246 +val empty_context = (1, Typtab.empty, 1, Termtab.empty, [])
   1.247 +
   1.248 +fun make_sign (_, typs, _, terms, thys) = {
   1.249 +  theories = thys,
   1.250 +  sorts = Typtab.fold (cons o snd) typs [],
   1.251 +  funcs = Termtab.fold (cons o snd) terms [] }
   1.252 +
   1.253 +fun make_recon (unfolds, assms) (_, typs, _, terms, _) = {
   1.254 +  typs = Symtab.make (map swap (Typtab.dest typs)),
   1.255 +  terms = Symtab.make (map (fn (t, (n, _)) => (n, t)) (Termtab.dest terms)),
   1.256 +  unfolds = unfolds,
   1.257 +  assms = SOME assms }
   1.258 +
   1.259 +fun string_of_index pre i = pre ^ string_of_int i
   1.260 +
   1.261 +fun add_theory T (Tidx, typs, idx, terms, thys) =
   1.262 +  let
   1.263 +    fun add @{typ int} = insert (op =) Integer
   1.264 +      | add @{typ real} = insert (op =) Real
   1.265 +      | add (Type (@{type_name word}, _)) = insert (op =) Bitvector
   1.266 +      | add (Type (_, Ts)) = fold add Ts
   1.267 +      | add _ = I
   1.268 +  in (Tidx, typs, idx, terms, add T thys) end
   1.269 +
   1.270 +fun fresh_typ sort_prefix T (cx as (Tidx, typs, idx, terms, thys)) =
   1.271 +  (case Typtab.lookup typs T of
   1.272 +    SOME s => (s, cx)
   1.273 +  | NONE =>
   1.274 +      let
   1.275 +        val s = string_of_index sort_prefix Tidx
   1.276 +        val typs' = Typtab.update (T, s) typs
   1.277 +      in (s, (Tidx+1, typs', idx, terms, thys)) end)
   1.278 +
   1.279 +fun fresh_fun func_prefix t ss (cx as (Tidx, typs, idx, terms, thys)) =
   1.280 +  (case Termtab.lookup terms t of
   1.281 +    SOME (f, _) => (f, cx)
   1.282 +  | NONE =>
   1.283 +      let
   1.284 +        val f = string_of_index func_prefix idx
   1.285 +        val terms' = Termtab.update (revert_types t, (f, ss)) terms
   1.286 +      in (f, (Tidx, typs, idx+1, terms', thys)) end)
   1.287 +
   1.288 +fun relaxed thms = (([], thms), map prop_of thms)
   1.289 +
   1.290 +fun with_context f (ths, ts) =
   1.291 +  let val (us, context) = fold_map f ts empty_context
   1.292 +  in ((make_sign context, us), make_recon ths context) end
   1.293 +
   1.294 +
   1.295 +fun translate {prefixes, strict, builtins, serialize} ctxt comments =
   1.296 +  let
   1.297 +    val {sort_prefix, func_prefix} = prefixes
   1.298 +    val {builtin_typ, builtin_num, builtin_fun} = builtins
   1.299 +
   1.300 +    fun transT T = add_theory T #>
   1.301 +      (case builtin_typ T of
   1.302 +        SOME n => pair n
   1.303 +      | NONE => fresh_typ sort_prefix T)
   1.304 +
   1.305 +    fun app n ts = SApp (n, ts)
   1.306 +
   1.307 +    fun trans t =
   1.308 +      (case Term.strip_comb t of
   1.309 +        (Const (qn, _), [Abs (_, T, t1)]) =>
   1.310 +          (case dest_quant qn T t1 of
   1.311 +            SOME (q, Ts, ps, b) =>
   1.312 +              fold_map transT Ts ##>> fold_map (fold_map_pat trans) ps ##>>
   1.313 +              trans b #>> (fn ((Ts', ps'), b') => SQua (q, Ts', ps', b'))
   1.314 +          | NONE => raise TERM ("intermediate", [t]))
   1.315 +      | (Const (@{const_name Let}, _), [t1, Abs (_, T, t2)]) =>
   1.316 +          transT T ##>> trans t1 ##>> trans t2 #>>
   1.317 +          (fn ((U, u1), u2) => SLet (U, u1, u2))
   1.318 +      | (h as Const (c as (@{const_name distinct}, T)), [t1]) =>
   1.319 +          (case builtin_fun c (HOLogic.dest_list t1) of
   1.320 +            SOME (n, ts) => add_theory T #> fold_map trans ts #>> app n
   1.321 +          | NONE => transs h T [t1])
   1.322 +      | (h as Const (c as (_, T)), ts) =>
   1.323 +          (case try HOLogic.dest_number t of
   1.324 +            SOME (T, i) =>
   1.325 +              (case builtin_num T i of
   1.326 +                SOME n => add_theory T #> pair (SApp (n, []))
   1.327 +              | NONE => transs t T [])
   1.328 +          | NONE =>
   1.329 +              (case builtin_fun c ts of
   1.330 +                SOME (n, ts') => add_theory T #> fold_map trans ts' #>> app n
   1.331 +              | NONE => transs h T ts))
   1.332 +      | (h as Free (_, T), ts) => transs h T ts
   1.333 +      | (Bound i, []) => pair (SVar i)
   1.334 +      | _ => raise TERM ("intermediate", [t]))
   1.335 +
   1.336 +    and transs t T ts =
   1.337 +      let val (Us, U) = dest_funT (length ts) T
   1.338 +      in
   1.339 +        fold_map transT Us ##>> transT U #-> (fn Up =>
   1.340 +        fresh_fun func_prefix t Up ##>> fold_map trans ts #>> SApp)
   1.341 +      end
   1.342 +  in
   1.343 +    (if is_some strict then strictify (the strict) ctxt else relaxed) #>
   1.344 +    with_context trans #>> uncurry (serialize comments)
   1.345 +  end
   1.346 +
   1.347 +end