src/HOL/Tools/SMT/smt_datatypes.ML
author blanchet
Wed Sep 17 21:35:58 2014 +0200 (2014-09-17)
changeset 58362 cf32eb8001b8
parent 58361 7f2b3b6f6ad1
child 58364 efc56d935728
permissions -rw-r--r--
register Isabelle selectors as SMT selectors when possible
blanchet@58061
     1
(*  Title:      HOL/Tools/SMT/smt_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@58361
     5
as (co)algebraic datatypes.
blanchet@56078
     6
*)
blanchet@56078
     7
blanchet@58061
     8
signature SMT_DATATYPES =
blanchet@56078
     9
sig
blanchet@58360
    10
  val add_decls: BNF_Util.fp_kind -> 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@57229
    13
end;
blanchet@56078
    14
blanchet@58061
    15
structure SMT_Datatypes: SMT_DATATYPES =
blanchet@56078
    16
struct
blanchet@56078
    17
blanchet@58362
    18
fun mk_selectors T Ts sels =
blanchet@58362
    19
  if null sels then
blanchet@58362
    20
    Variable.variant_fixes (replicate (length Ts) "select")
blanchet@58362
    21
    #>> map2 (fn U => fn n => Free (n, T --> U)) Ts
blanchet@58362
    22
  else
blanchet@58362
    23
    pair sels
blanchet@56078
    24
blanchet@56078
    25
blanchet@57226
    26
(* free constructor type declarations *)
blanchet@56078
    27
blanchet@58362
    28
fun get_ctr_sugar_decl ({ctrs = ctrs0, selss = selss0, ...} : Ctr_Sugar.ctr_sugar) T Ts ctxt =
blanchet@56078
    29
  let
blanchet@58362
    30
    fun mk_constr ctr0 sels0 =
blanchet@58362
    31
      let
blanchet@58362
    32
        val sels = map (Ctr_Sugar.mk_disc_or_sel Ts) sels0
blanchet@58362
    33
        val ctr = Ctr_Sugar.mk_ctr Ts ctr0
blanchet@58362
    34
        val binder_Ts = binder_types (fastype_of ctr)
blanchet@58362
    35
      in
blanchet@58362
    36
        mk_selectors T binder_Ts sels #>> pair ctr
blanchet@57226
    37
      end
blanchet@58362
    38
blanchet@58362
    39
    val selss = if has_duplicates (op aconv) (flat selss0) then [] else selss0
blanchet@57226
    40
  in
blanchet@58362
    41
    Ctr_Sugar_Util.fold_map2 mk_constr ctrs0 (Ctr_Sugar_Util.pad_list [] (length ctrs0) selss) ctxt
blanchet@57226
    42
    |>> (pair T #> single)
blanchet@57226
    43
  end
blanchet@56078
    44
blanchet@56078
    45
blanchet@56078
    46
(* typedef declarations *)
blanchet@56078
    47
blanchet@57213
    48
fun get_typedef_decl (({Abs_name, Rep_name, abs_type, rep_type, ...}, {Abs_inverse, ...})
blanchet@57213
    49
    : Typedef.info) T Ts =
blanchet@57213
    50
  if can (curry (op RS) @{thm UNIV_I}) Abs_inverse then
blanchet@57213
    51
    let
blanchet@57213
    52
      val env = snd (Term.dest_Type abs_type) ~~ Ts
blanchet@57213
    53
      val instT = Term.map_atyps (perhaps (AList.lookup (op =) env))
blanchet@56078
    54
blanchet@57213
    55
      val constr = Const (Abs_name, instT (rep_type --> abs_type))
blanchet@57213
    56
      val select = Const (Rep_name, instT (abs_type --> rep_type))
blanchet@57213
    57
    in [(T, [(constr, [select])])] end
blanchet@57213
    58
  else
blanchet@57213
    59
    []
blanchet@56078
    60
blanchet@56078
    61
blanchet@56078
    62
(* collection of declarations *)
blanchet@56078
    63
blanchet@56078
    64
fun declared declss T = exists (exists (equal T o fst)) declss
blanchet@56078
    65
fun declared' dss T = exists (exists (equal T o fst) o snd) dss
blanchet@56078
    66
blanchet@58361
    67
(* Simplification: We assume that every type that is not a codatatype is a datatype (or a
blanchet@58361
    68
   record). *)
blanchet@58361
    69
fun fp_kind_of ctxt n =
blanchet@58361
    70
  (case BNF_FP_Def_Sugar.fp_sugar_of ctxt n of
blanchet@58361
    71
    SOME {fp, ...} => fp
blanchet@58361
    72
  | NONE => BNF_Util.Least_FP)
blanchet@58361
    73
blanchet@58361
    74
fun get_decls fp T n Ts ctxt =
blanchet@58361
    75
  let
blanchet@58361
    76
    fun fallback () =
blanchet@57226
    77
      (case Typedef.get_info ctxt n of
blanchet@57226
    78
        [] => ([], ctxt)
blanchet@58361
    79
      | info :: _ => (get_typedef_decl info T Ts, ctxt))
blanchet@58361
    80
  in
blanchet@58361
    81
    (case Ctr_Sugar.ctr_sugar_of ctxt n of
blanchet@58361
    82
      SOME ctr_sugar =>
blanchet@58361
    83
      if fp_kind_of ctxt n = fp then get_ctr_sugar_decl ctr_sugar T Ts ctxt else fallback ()
blanchet@58361
    84
    | NONE => fallback ())
blanchet@58361
    85
  end
blanchet@56078
    86
blanchet@58360
    87
fun add_decls fp T (declss, ctxt) =
blanchet@56078
    88
  let
blanchet@56078
    89
    fun depends Ts ds = exists (member (op =) (map fst ds)) Ts
blanchet@56078
    90
blanchet@56078
    91
    fun add (TFree _) = I
blanchet@56078
    92
      | add (TVar _) = I
blanchet@56078
    93
      | add (T as Type (@{type_name fun}, _)) =
blanchet@56078
    94
          fold add (Term.body_type T :: Term.binder_types T)
blanchet@56078
    95
      | add @{typ bool} = I
blanchet@56078
    96
      | add (T as Type (n, Ts)) = (fn (dss, ctxt1) =>
blanchet@56078
    97
          if declared declss T orelse declared' dss T then (dss, ctxt1)
blanchet@58061
    98
          else if SMT_Builtin.is_builtin_typ_ext ctxt1 T then (dss, ctxt1)
blanchet@56078
    99
          else
blanchet@58361
   100
            (case get_decls fp T n Ts ctxt1 of
blanchet@56078
   101
              ([], _) => (dss, ctxt1)
blanchet@56078
   102
            | (ds, ctxt2) =>
blanchet@56078
   103
                let
blanchet@57226
   104
                  val constrTs = maps (map (snd o Term.dest_Const o fst) o snd) ds
blanchet@56078
   105
                  val Us = fold (union (op =) o Term.binder_types) constrTs []
blanchet@56078
   106
blanchet@56078
   107
                  fun ins [] = [(Us, ds)]
blanchet@56078
   108
                    | ins ((Uds as (Us', _)) :: Udss) =
blanchet@56078
   109
                        if depends Us' ds then (Us, ds) :: Uds :: Udss
blanchet@56078
   110
                        else Uds :: ins Udss
blanchet@56078
   111
            in fold add Us (ins dss, ctxt2) end))
blanchet@56078
   112
  in add T ([], ctxt) |>> append declss o map snd end
blanchet@56078
   113
blanchet@57229
   114
end;