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