src/HOL/Tools/SMT/smt_datatypes.ML
author blanchet
Wed Sep 24 15:46:25 2014 +0200 (2014-09-24)
changeset 58429 0b94858325a5
parent 58428 e4e34dfc3e68
child 58430 73df5884edcf
permissions -rw-r--r--
interleave (co)datatypes in the right order w.r.t. dependencies
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@58429
    10
  val add_decls: BNF_Util.fp_kind list -> typ ->
blanchet@58429
    11
    (BNF_Util.fp_kind * (typ * (term * term list) list)) list list * Proof.context ->
blanchet@58429
    12
    (BNF_Util.fp_kind * (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@58427
    64
val extN = "_ext" (* cf. "HOL/Tools/typedef.ML" *)
blanchet@58361
    65
blanchet@58429
    66
fun get_decls fps T n Ts ctxt =
blanchet@58361
    67
  let
blanchet@58427
    68
    fun maybe_typedef () =
blanchet@57226
    69
      (case Typedef.get_info ctxt n of
blanchet@57226
    70
        [] => ([], ctxt)
blanchet@58429
    71
      | info :: _ => (map (pair (hd fps)) (get_typedef_decl info T Ts), ctxt))
blanchet@58361
    72
  in
blanchet@58427
    73
    (case BNF_FP_Def_Sugar.fp_sugar_of ctxt n of
blanchet@58429
    74
      SOME {fp, fp_res = {Ts = fp_Ts, ...}, ctr_sugar, ...} =>
blanchet@58429
    75
      if member (op =) fps fp then
blanchet@58428
    76
        let
blanchet@58428
    77
          val ns = map (fst o dest_Type) fp_Ts
blanchet@58428
    78
          val mutual_fp_sugars = map_filter (BNF_FP_Def_Sugar.fp_sugar_of ctxt) ns
blanchet@58428
    79
          val Xs = map #X mutual_fp_sugars
blanchet@58428
    80
          val ctrXs_Tsss = map #ctrXs_Tss mutual_fp_sugars
blanchet@58428
    81
blanchet@58429
    82
          (* FIXME: allow nested recursion to same FP kind *)
blanchet@58429
    83
          fun is_nested_co_recursive (T as Type _) = BNF_FP_Rec_Sugar_Util.exists_subtype_in Xs T
blanchet@58428
    84
            | is_nested_co_recursive _ = false
blanchet@58428
    85
        in
blanchet@58428
    86
          if exists (exists (exists is_nested_co_recursive)) ctrXs_Tsss then maybe_typedef ()
blanchet@58429
    87
          else get_ctr_sugar_decl ctr_sugar T Ts ctxt |>> map (pair fp)
blanchet@58428
    88
        end
blanchet@58428
    89
      else
blanchet@58428
    90
        ([], ctxt)
blanchet@58427
    91
    | NONE =>
blanchet@58429
    92
      if String.isSuffix extN n then
blanchet@58429
    93
        (* for records (FIXME: hack) *)
blanchet@58429
    94
        (case Ctr_Sugar.ctr_sugar_of ctxt n of
blanchet@58429
    95
          SOME ctr_sugar =>
blanchet@58429
    96
          get_ctr_sugar_decl ctr_sugar T Ts ctxt |>> map (pair (hd fps))
blanchet@58429
    97
        | NONE => maybe_typedef ())
blanchet@58427
    98
      else
blanchet@58429
    99
        maybe_typedef ())
blanchet@58361
   100
  end
blanchet@56078
   101
blanchet@58429
   102
fun add_decls fps T (declss, ctxt) =
blanchet@56078
   103
  let
blanchet@58429
   104
    fun declared T = exists (exists (equal T o fst o snd))
blanchet@58429
   105
    fun declared' T = exists (exists (equal T o fst o snd) o snd)
blanchet@58429
   106
    fun depends ds = exists (member (op =) (map (fst o snd) ds))
blanchet@56078
   107
blanchet@56078
   108
    fun add (TFree _) = I
blanchet@56078
   109
      | add (TVar _) = I
blanchet@56078
   110
      | add (T as Type (@{type_name fun}, _)) =
blanchet@56078
   111
          fold add (Term.body_type T :: Term.binder_types T)
blanchet@56078
   112
      | add @{typ bool} = I
blanchet@56078
   113
      | add (T as Type (n, Ts)) = (fn (dss, ctxt1) =>
blanchet@58429
   114
          if declared T declss orelse declared' T dss then
blanchet@58429
   115
            (dss, ctxt1)
blanchet@58429
   116
          else if SMT_Builtin.is_builtin_typ_ext ctxt1 T then
blanchet@58429
   117
            (dss, ctxt1)
blanchet@56078
   118
          else
blanchet@58429
   119
            (case get_decls fps T n Ts ctxt1 of
blanchet@56078
   120
              ([], _) => (dss, ctxt1)
blanchet@56078
   121
            | (ds, ctxt2) =>
blanchet@56078
   122
                let
blanchet@58429
   123
                  val constrTs = maps (map (snd o Term.dest_Const o fst) o snd 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@58364
   128
                        if depends ds Us' then (Us, ds) :: Uds :: Udss else Uds :: ins Udss
blanchet@56078
   129
            in fold add Us (ins dss, ctxt2) end))
blanchet@56078
   130
  in add T ([], ctxt) |>> append declss o map snd end
blanchet@56078
   131
blanchet@57229
   132
end;