src/HOL/Tools/SMT/smt_datatypes.ML
author wenzelm
Wed Oct 08 17:09:07 2014 +0200 (2014-10-08)
changeset 58634 9f10d82e8188
parent 58460 a88eb33058f7
child 59020 a86683d6c97e
permissions -rw-r--r--
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
     1 (*  Title:      HOL/Tools/SMT/smt_datatypes.ML
     2     Author:     Sascha Boehme, TU Muenchen
     3 
     4 Collector functions for common type declarations and their representation
     5 as (co)algebraic datatypes.
     6 *)
     7 
     8 signature SMT_DATATYPES =
     9 sig
    10   val add_decls: BNF_Util.fp_kind list -> typ ->
    11     (BNF_Util.fp_kind * (typ * (term * term list) list)) list list * Proof.context ->
    12     (BNF_Util.fp_kind * (typ * (term * term list) list)) list list * Proof.context
    13 end;
    14 
    15 structure SMT_Datatypes: SMT_DATATYPES =
    16 struct
    17 
    18 fun mk_selectors T Ts sels =
    19   if null sels then
    20     Variable.variant_fixes (replicate (length Ts) "select")
    21     #>> map2 (fn U => fn n => Free (n, T --> U)) Ts
    22   else
    23     pair sels
    24 
    25 
    26 (* free constructor type declarations *)
    27 
    28 fun get_ctr_sugar_decl ({ctrs = ctrs0, selss = selss0, ...} : Ctr_Sugar.ctr_sugar) T Ts ctxt =
    29   let
    30     fun mk_constr ctr0 sels0 =
    31       let
    32         val sels = map (Ctr_Sugar.mk_disc_or_sel Ts) sels0
    33         val ctr = Ctr_Sugar.mk_ctr Ts ctr0
    34         val binder_Ts = binder_types (fastype_of ctr)
    35       in
    36         mk_selectors T binder_Ts sels #>> pair ctr
    37       end
    38 
    39     val selss = if has_duplicates (op aconv) (flat selss0) then [] else selss0
    40   in
    41     @{fold_map 2} mk_constr ctrs0 (Ctr_Sugar_Util.pad_list [] (length ctrs0) selss) ctxt
    42     |>> (pair T #> single)
    43   end
    44 
    45 
    46 (* typedef declarations *)
    47 
    48 fun get_typedef_decl (({Abs_name, Rep_name, abs_type, rep_type, ...}, {Abs_inverse, ...})
    49     : Typedef.info) T Ts =
    50   if can (curry (op RS) @{thm UNIV_I}) Abs_inverse then
    51     let
    52       val env = snd (Term.dest_Type abs_type) ~~ Ts
    53       val instT = Term.map_atyps (perhaps (AList.lookup (op =) env))
    54 
    55       val constr = Const (Abs_name, instT (rep_type --> abs_type))
    56       val select = Const (Rep_name, instT (abs_type --> rep_type))
    57     in [(T, [(constr, [select])])] end
    58   else
    59     []
    60 
    61 
    62 (* collection of declarations *)
    63 
    64 val extN = "_ext" (* cf. "HOL/Tools/typedef.ML" *)
    65 
    66 fun get_decls fps T n Ts ctxt =
    67   let
    68     fun maybe_typedef () =
    69       (case Typedef.get_info ctxt n of
    70         [] => ([], ctxt)
    71       | info :: _ => (map (pair (hd fps)) (get_typedef_decl info T Ts), ctxt))
    72   in
    73     (case BNF_FP_Def_Sugar.fp_sugar_of ctxt n of
    74       SOME {fp, fp_res = {Ts = fp_Ts, ...}, fp_ctr_sugar = {ctr_sugar, ...}, ...} =>
    75       if member (op =) fps fp then
    76         let
    77           val ns = map (fst o dest_Type) fp_Ts
    78           val mutual_fp_sugars = map_filter (BNF_FP_Def_Sugar.fp_sugar_of ctxt) ns
    79           val Xs = map #X mutual_fp_sugars
    80           val ctrXs_Tsss = map (#ctrXs_Tss o #fp_ctr_sugar) mutual_fp_sugars
    81 
    82           (* Datatypes nested through datatypes and codatatypes nested through codatatypes are
    83              allowed. So are mutually (co)recursive (co)datatypes. *)
    84           fun is_same_fp s =
    85             (case BNF_FP_Def_Sugar.fp_sugar_of ctxt s of
    86               SOME {fp = fp', ...} => fp' = fp
    87             | NONE => false)
    88           fun is_homogenously_nested_co_recursive (Type (s, Ts)) =
    89               forall (if is_same_fp s then is_homogenously_nested_co_recursive
    90                 else not o BNF_FP_Rec_Sugar_Util.exists_subtype_in Xs) Ts
    91             | is_homogenously_nested_co_recursive _ = true
    92         in
    93           if forall (forall (forall is_homogenously_nested_co_recursive)) ctrXs_Tsss then
    94             get_ctr_sugar_decl ctr_sugar T Ts ctxt |>> map (pair fp)
    95           else
    96             maybe_typedef ()
    97         end
    98       else
    99         ([], ctxt)
   100     | NONE =>
   101       if String.isSuffix extN n then
   102         (* for records (FIXME: hack) *)
   103         (case Ctr_Sugar.ctr_sugar_of ctxt n of
   104           SOME ctr_sugar =>
   105           get_ctr_sugar_decl ctr_sugar T Ts ctxt |>> map (pair (hd fps))
   106         | NONE => maybe_typedef ())
   107       else
   108         maybe_typedef ())
   109   end
   110 
   111 fun add_decls fps T (declss, ctxt) =
   112   let
   113     fun declared T = exists (exists (equal T o fst o snd))
   114     fun declared' T = exists (exists (equal T o fst o snd) o snd)
   115     fun depends ds = exists (member (op =) (map (fst o snd) ds))
   116 
   117     fun add (TFree _) = I
   118       | add (TVar _) = I
   119       | add (T as Type (@{type_name fun}, _)) =
   120           fold add (Term.body_type T :: Term.binder_types T)
   121       | add @{typ bool} = I
   122       | add (T as Type (n, Ts)) = (fn (dss, ctxt1) =>
   123           if declared T declss orelse declared' T dss then
   124             (dss, ctxt1)
   125           else if SMT_Builtin.is_builtin_typ_ext ctxt1 T then
   126             (dss, ctxt1)
   127           else
   128             (case get_decls fps T n Ts ctxt1 of
   129               ([], _) => (dss, ctxt1)
   130             | (ds, ctxt2) =>
   131                 let
   132                   val constrTs = maps (map (snd o Term.dest_Const o fst) o snd o snd) ds
   133                   val Us = fold (union (op =) o Term.binder_types) constrTs []
   134 
   135                   fun ins [] = [(Us, ds)]
   136                     | ins ((Uds as (Us', _)) :: Udss) =
   137                         if depends ds Us' then (Us, ds) :: Uds :: Udss else Uds :: ins Udss
   138             in fold add Us (ins dss, ctxt2) end))
   139   in add T ([], ctxt) |>> append declss o map snd end
   140 
   141 end;