src/HOL/Tools/SMT/smt_datatypes.ML
author blanchet
Wed Jun 11 11:28:46 2014 +0200 (2014-06-11)
changeset 57213 9daec42f6784
parent 43385 9cd4b4ecb4dd
child 57226 c22ad39c3b4b
permissions -rw-r--r--
fixed unsoundness in SMT(2) as oracle: don't register typedef Abs_x as constructor unless it is known to be injective
boehmes@41426
     1
(*  Title:      HOL/Tools/SMT/smt_datatypes.ML
boehmes@41426
     2
    Author:     Sascha Boehme, TU Muenchen
boehmes@41426
     3
boehmes@41426
     4
Collector functions for common type declarations and their representation
boehmes@41426
     5
as algebraic datatypes.
boehmes@41426
     6
*)
boehmes@41426
     7
boehmes@41426
     8
signature SMT_DATATYPES =
boehmes@41426
     9
sig
boehmes@41426
    10
  val add_decls: typ ->
boehmes@41426
    11
    (typ * (term * term list) list) list list * Proof.context ->
boehmes@41426
    12
    (typ * (term * term list) list) list list * Proof.context
boehmes@41426
    13
end
boehmes@41426
    14
boehmes@41426
    15
structure SMT_Datatypes: SMT_DATATYPES =
boehmes@41426
    16
struct
boehmes@41426
    17
boehmes@41426
    18
val lhs_head_of = Term.head_of o fst o Logic.dest_equals o Thm.prop_of
boehmes@41426
    19
boehmes@41426
    20
fun mk_selectors T Ts ctxt =
boehmes@41426
    21
  let
boehmes@41426
    22
    val (sels, ctxt') =
boehmes@41426
    23
      Variable.variant_fixes (replicate (length Ts) "select") ctxt
boehmes@41426
    24
  in (map2 (fn n => fn U => Free (n, T --> U)) sels Ts, ctxt') end
boehmes@41426
    25
boehmes@41426
    26
boehmes@41426
    27
(* datatype declarations *)
boehmes@41426
    28
boehmes@41426
    29
fun get_datatype_decl ({descr, ...} : Datatype.info) n Ts ctxt =
boehmes@41426
    30
  let
boehmes@41426
    31
    fun get_vars (_, (m, vs, _)) = if m = n then SOME vs else NONE
boehmes@41426
    32
    val vars = the (get_first get_vars descr) ~~ Ts
boehmes@41426
    33
    val lookup_var = the o AList.lookup (op =) vars
boehmes@41426
    34
boehmes@41426
    35
    fun typ_of (dt as Datatype.DtTFree _) = lookup_var dt
boehmes@43385
    36
      | typ_of (Datatype.DtType (m, dts)) = Type (m, map typ_of dts)
boehmes@43385
    37
      | typ_of (Datatype.DtRec i) =
boehmes@43385
    38
          the (AList.lookup (op =) descr i)
boehmes@43385
    39
          |> (fn (m, dts, _) => Type (m, map typ_of dts))
boehmes@41426
    40
boehmes@41426
    41
    fun mk_constr T (m, dts) ctxt =
boehmes@41426
    42
      let
boehmes@41426
    43
        val Ts = map typ_of dts
boehmes@41426
    44
        val constr = Const (m, Ts ---> T)
boehmes@41426
    45
        val (selects, ctxt') = mk_selectors T Ts ctxt
boehmes@41426
    46
      in ((constr, selects), ctxt') end
boehmes@41426
    47
boehmes@41426
    48
    fun mk_decl (i, (_, _, constrs)) ctxt =
boehmes@41426
    49
      let
boehmes@43385
    50
        val T = typ_of (Datatype.DtRec i)
boehmes@41426
    51
        val (css, ctxt') = fold_map (mk_constr T) constrs ctxt
boehmes@41426
    52
      in ((T, css), ctxt') end
boehmes@41426
    53
boehmes@41426
    54
  in fold_map mk_decl descr ctxt end
boehmes@41426
    55
boehmes@41426
    56
boehmes@41426
    57
(* record declarations *)
boehmes@41426
    58
boehmes@41426
    59
val record_name_of = Long_Name.implode o fst o split_last o Long_Name.explode
boehmes@41426
    60
boehmes@41426
    61
fun get_record_decl ({ext_def, ...} : Record.info) T ctxt =
boehmes@41426
    62
  let
boehmes@41426
    63
    val (con, _) = Term.dest_Const (lhs_head_of ext_def)
wenzelm@42361
    64
    val (fields, more) = Record.get_extT_fields (Proof_Context.theory_of ctxt) T
boehmes@41426
    65
    val fieldTs = map snd fields @ [snd more]
boehmes@41426
    66
boehmes@41426
    67
    val constr = Const (con, fieldTs ---> T)
boehmes@41426
    68
    val (selects, ctxt') = mk_selectors T fieldTs ctxt
boehmes@41426
    69
  in ((T, [(constr, selects)]), ctxt') end
boehmes@41426
    70
boehmes@41426
    71
boehmes@41426
    72
(* typedef declarations *)
boehmes@41426
    73
blanchet@57213
    74
fun get_typedef_decl (({Abs_name, Rep_name, abs_type, rep_type, ...}, {Abs_inverse, ...})
blanchet@57213
    75
    : Typedef.info) T Ts =
blanchet@57213
    76
  if can (curry (op RS) @{thm UNIV_I}) Abs_inverse then
blanchet@57213
    77
    let
blanchet@57213
    78
      val env = snd (Term.dest_Type abs_type) ~~ Ts
blanchet@57213
    79
      val instT = Term.map_atyps (perhaps (AList.lookup (op =) env))
boehmes@41426
    80
blanchet@57213
    81
      val constr = Const (Abs_name, instT (rep_type --> abs_type))
blanchet@57213
    82
      val select = Const (Rep_name, instT (abs_type --> rep_type))
blanchet@57213
    83
    in [(T, [(constr, [select])])] end
blanchet@57213
    84
  else
blanchet@57213
    85
    []
boehmes@41426
    86
boehmes@41426
    87
boehmes@41426
    88
(* collection of declarations *)
boehmes@41426
    89
boehmes@41426
    90
fun declared declss T = exists (exists (equal T o fst)) declss
boehmes@43385
    91
fun declared' dss T = exists (exists (equal T o fst) o snd) dss
boehmes@41426
    92
boehmes@41426
    93
fun get_decls T n Ts ctxt =
wenzelm@42361
    94
  let val thy = Proof_Context.theory_of ctxt
boehmes@41426
    95
  in
boehmes@41426
    96
    (case Datatype.get_info thy n of
boehmes@41426
    97
      SOME info => get_datatype_decl info n Ts ctxt
boehmes@41426
    98
    | NONE =>
boehmes@41426
    99
        (case Record.get_info thy (record_name_of n) of
boehmes@41426
   100
          SOME info => get_record_decl info T ctxt |>> single
boehmes@41426
   101
        | NONE =>
boehmes@41426
   102
            (case Typedef.get_info ctxt n of
boehmes@41426
   103
              [] => ([], ctxt)
blanchet@57213
   104
            | info :: _ => (get_typedef_decl info T Ts, ctxt))))
boehmes@41426
   105
  end
boehmes@41426
   106
boehmes@41426
   107
fun add_decls T (declss, ctxt) =
boehmes@41426
   108
  let
boehmes@43385
   109
    fun depends Ts ds = exists (member (op =) (map fst ds)) Ts
boehmes@43385
   110
boehmes@41426
   111
    fun add (TFree _) = I
boehmes@41426
   112
      | add (TVar _) = I
boehmes@41426
   113
      | add (T as Type (@{type_name fun}, _)) =
boehmes@41426
   114
          fold add (Term.body_type T :: Term.binder_types T)
boehmes@41426
   115
      | add @{typ bool} = I
boehmes@41426
   116
      | add (T as Type (n, Ts)) = (fn (dss, ctxt1) =>
boehmes@43385
   117
          if declared declss T orelse declared' dss T then (dss, ctxt1)
boehmes@41426
   118
          else if SMT_Builtin.is_builtin_typ_ext ctxt1 T then (dss, ctxt1)
boehmes@41426
   119
          else
boehmes@41426
   120
            (case get_decls T n Ts ctxt1 of
boehmes@41426
   121
              ([], _) => (dss, ctxt1)
boehmes@41426
   122
            | (ds, ctxt2) =>
boehmes@41426
   123
                let
boehmes@41426
   124
                  val constrTs =
boehmes@41426
   125
                    maps (map (snd o Term.dest_Const o fst) o snd) ds
boehmes@41426
   126
                  val Us = fold (union (op =) o Term.binder_types) constrTs []
boehmes@43385
   127
boehmes@43385
   128
                  fun ins [] = [(Us, ds)]
boehmes@43385
   129
                    | ins ((Uds as (Us', _)) :: Udss) =
boehmes@43385
   130
                        if depends Us' ds then (Us, ds) :: Uds :: Udss
boehmes@43385
   131
                        else Uds :: ins Udss
boehmes@43385
   132
            in fold add Us (ins dss, ctxt2) end))
boehmes@43385
   133
  in add T ([], ctxt) |>> append declss o map snd end
boehmes@43385
   134
boehmes@41426
   135
boehmes@41426
   136
end