src/HOL/Tools/SMT/smt_datatypes.ML
author blanchet
Wed Sep 24 15:46:40 2014 +0200 (2014-09-24)
changeset 58430 73df5884edcf
parent 58429 0b94858325a5
child 58460 a88eb33058f7
permissions -rw-r--r--
allow homogeneous nesting for SMT (co)datatypes
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@58430
    82
          (* Datatypes nested through datatypes and codatatypes nested through codatatypes are
blanchet@58430
    83
             allowed. So are mutually (co)recursive (co)datatypes. *)
blanchet@58430
    84
          fun is_same_fp s =
blanchet@58430
    85
            (case BNF_FP_Def_Sugar.fp_sugar_of ctxt s of
blanchet@58430
    86
              SOME {fp = fp', ...} => fp' = fp
blanchet@58430
    87
            | NONE => false)
blanchet@58430
    88
          fun is_homogenously_nested_co_recursive (Type (s, Ts)) =
blanchet@58430
    89
              forall (if is_same_fp s then is_homogenously_nested_co_recursive
blanchet@58430
    90
                else not o BNF_FP_Rec_Sugar_Util.exists_subtype_in Xs) Ts
blanchet@58430
    91
            | is_homogenously_nested_co_recursive _ = true
blanchet@58428
    92
        in
blanchet@58430
    93
          if forall (forall (forall is_homogenously_nested_co_recursive)) ctrXs_Tsss then
blanchet@58430
    94
            get_ctr_sugar_decl ctr_sugar T Ts ctxt |>> map (pair fp)
blanchet@58430
    95
          else
blanchet@58430
    96
            maybe_typedef ()
blanchet@58428
    97
        end
blanchet@58428
    98
      else
blanchet@58428
    99
        ([], ctxt)
blanchet@58427
   100
    | NONE =>
blanchet@58429
   101
      if String.isSuffix extN n then
blanchet@58429
   102
        (* for records (FIXME: hack) *)
blanchet@58429
   103
        (case Ctr_Sugar.ctr_sugar_of ctxt n of
blanchet@58429
   104
          SOME ctr_sugar =>
blanchet@58429
   105
          get_ctr_sugar_decl ctr_sugar T Ts ctxt |>> map (pair (hd fps))
blanchet@58429
   106
        | NONE => maybe_typedef ())
blanchet@58427
   107
      else
blanchet@58429
   108
        maybe_typedef ())
blanchet@58361
   109
  end
blanchet@56078
   110
blanchet@58429
   111
fun add_decls fps T (declss, ctxt) =
blanchet@56078
   112
  let
blanchet@58429
   113
    fun declared T = exists (exists (equal T o fst o snd))
blanchet@58429
   114
    fun declared' T = exists (exists (equal T o fst o snd) o snd)
blanchet@58429
   115
    fun depends ds = exists (member (op =) (map (fst o snd) ds))
blanchet@56078
   116
blanchet@56078
   117
    fun add (TFree _) = I
blanchet@56078
   118
      | add (TVar _) = I
blanchet@56078
   119
      | add (T as Type (@{type_name fun}, _)) =
blanchet@56078
   120
          fold add (Term.body_type T :: Term.binder_types T)
blanchet@56078
   121
      | add @{typ bool} = I
blanchet@56078
   122
      | add (T as Type (n, Ts)) = (fn (dss, ctxt1) =>
blanchet@58429
   123
          if declared T declss orelse declared' T dss then
blanchet@58429
   124
            (dss, ctxt1)
blanchet@58429
   125
          else if SMT_Builtin.is_builtin_typ_ext ctxt1 T then
blanchet@58429
   126
            (dss, ctxt1)
blanchet@56078
   127
          else
blanchet@58429
   128
            (case get_decls fps T n Ts ctxt1 of
blanchet@56078
   129
              ([], _) => (dss, ctxt1)
blanchet@56078
   130
            | (ds, ctxt2) =>
blanchet@56078
   131
                let
blanchet@58429
   132
                  val constrTs = maps (map (snd o Term.dest_Const o fst) o snd o snd) ds
blanchet@56078
   133
                  val Us = fold (union (op =) o Term.binder_types) constrTs []
blanchet@56078
   134
blanchet@56078
   135
                  fun ins [] = [(Us, ds)]
blanchet@56078
   136
                    | ins ((Uds as (Us', _)) :: Udss) =
blanchet@58364
   137
                        if depends ds Us' then (Us, ds) :: Uds :: Udss else Uds :: ins Udss
blanchet@56078
   138
            in fold add Us (ins dss, ctxt2) end))
blanchet@56078
   139
  in add T ([], ctxt) |>> append declss o map snd end
blanchet@56078
   140
blanchet@57229
   141
end;