src/HOL/Tools/SMT/smt_datatypes.ML
author blanchet
Mon Dec 15 07:20:48 2014 +0100 (2014-12-15)
changeset 59142 705f8aea8d60
parent 59020 a86683d6c97e
child 59143 15c342a9a8e0
permissions -rw-r--r--
avoid generating selectors with function types -- this produce arity inconsistencies
     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 =
    40       if has_duplicates (op aconv) (flat selss0) orelse
    41          exists (exists (can (dest_funT o range_type o fastype_of))) selss0 then
    42         []
    43       else
    44         selss0
    45   in
    46     @{fold_map 2} mk_constr ctrs0 (Ctr_Sugar_Util.pad_list [] (length ctrs0) selss) ctxt
    47     |>> (pair T #> single)
    48   end
    49 
    50 
    51 (* typedef declarations *)
    52 
    53 fun get_typedef_decl (({Abs_name, Rep_name, abs_type, rep_type, ...}, {Abs_inverse, ...})
    54     : Typedef.info) T Ts =
    55   if can (curry (op RS) @{thm UNIV_I}) Abs_inverse then
    56     let
    57       val env = snd (Term.dest_Type abs_type) ~~ Ts
    58       val instT = Term.map_atyps (perhaps (AList.lookup (op =) env))
    59 
    60       val constr = Const (Abs_name, instT (rep_type --> abs_type))
    61       val select = Const (Rep_name, instT (abs_type --> rep_type))
    62     in [(T, [(constr, [select])])] end
    63   else
    64     []
    65 
    66 
    67 (* collection of declarations *)
    68 
    69 val extN = "_ext" (* cf. "HOL/Tools/typedef.ML" *)
    70 
    71 fun get_decls fps T n Ts ctxt =
    72   let
    73     fun maybe_typedef () =
    74       (case Typedef.get_info ctxt n of
    75         [] => ([], ctxt)
    76       | info :: _ => (map (pair (hd fps)) (get_typedef_decl info T Ts), ctxt))
    77   in
    78     (case BNF_FP_Def_Sugar.fp_sugar_of ctxt n of
    79       SOME {fp, fp_res = {Ts = fp_Ts, ...}, fp_ctr_sugar = {ctr_sugar, ...}, ...} =>
    80       if member (op =) fps fp then
    81         let
    82           val ns = map (fst o dest_Type) fp_Ts
    83           val mutual_fp_sugars = map_filter (BNF_FP_Def_Sugar.fp_sugar_of ctxt) ns
    84           val Xs = map #X mutual_fp_sugars
    85           val ctrXs_Tsss = map (#ctrXs_Tss o #fp_ctr_sugar) mutual_fp_sugars
    86 
    87           (* Datatypes nested through datatypes and codatatypes nested through codatatypes are
    88              allowed. So are mutually (co)recursive (co)datatypes. *)
    89           fun is_same_fp s =
    90             (case BNF_FP_Def_Sugar.fp_sugar_of ctxt s of
    91               SOME {fp = fp', ...} => fp' = fp
    92             | NONE => false)
    93           fun is_homogenously_nested_co_recursive (Type (s, Ts)) =
    94               forall (if is_same_fp s then is_homogenously_nested_co_recursive
    95                 else not o BNF_FP_Rec_Sugar_Util.exists_subtype_in Xs) Ts
    96             | is_homogenously_nested_co_recursive _ = true
    97 
    98           val Type (_, As) :: _ = fp_Ts
    99           val substAs = Term.typ_subst_atomic (As ~~ Ts);
   100         in
   101           (* TODO/FIXME: The "bool" check is there to work around a CVC4 bug
   102              (http://church.cims.nyu.edu/bugzilla3/show_bug.cgi?id=597). It should be removed once
   103              the bug is fixed. *)
   104           if forall (forall (forall (is_homogenously_nested_co_recursive))) ctrXs_Tsss andalso
   105              forall (forall (forall (curry (op <>) @{typ bool})))
   106                (map (map (map substAs)) ctrXs_Tsss) then
   107             get_ctr_sugar_decl ctr_sugar T Ts ctxt |>> map (pair fp)
   108           else
   109             maybe_typedef ()
   110         end
   111       else
   112         ([], ctxt)
   113     | NONE =>
   114       if String.isSuffix extN n then
   115         (* for records (FIXME: hack) *)
   116         (case Ctr_Sugar.ctr_sugar_of ctxt n of
   117           SOME ctr_sugar =>
   118           get_ctr_sugar_decl ctr_sugar T Ts ctxt |>> map (pair (hd fps))
   119         | NONE => maybe_typedef ())
   120       else
   121         maybe_typedef ())
   122   end
   123 
   124 fun add_decls fps T (declss, ctxt) =
   125   let
   126     fun declared T = exists (exists (equal T o fst o snd))
   127     fun declared' T = exists (exists (equal T o fst o snd) o snd)
   128     fun depends ds = exists (member (op =) (map (fst o snd) ds))
   129 
   130     fun add (TFree _) = I
   131       | add (TVar _) = I
   132       | add (T as Type (@{type_name fun}, _)) =
   133           fold add (Term.body_type T :: Term.binder_types T)
   134       | add @{typ bool} = I
   135       | add (T as Type (n, Ts)) = (fn (dss, ctxt1) =>
   136           if declared T declss orelse declared' T dss then
   137             (dss, ctxt1)
   138           else if SMT_Builtin.is_builtin_typ_ext ctxt1 T then
   139             (dss, ctxt1)
   140           else
   141             (case get_decls fps T n Ts ctxt1 of
   142               ([], _) => (dss, ctxt1)
   143             | (ds, ctxt2) =>
   144                 let
   145                   val constrTs = maps (map (snd o Term.dest_Const o fst) o snd o snd) ds
   146                   val Us = fold (union (op =) o Term.binder_types) constrTs []
   147 
   148                   fun ins [] = [(Us, ds)]
   149                     | ins ((Uds as (Us', _)) :: Udss) =
   150                         if depends ds Us' then (Us, ds) :: Uds :: Udss else Uds :: ins Udss
   151             in fold add Us (ins dss, ctxt2) end))
   152   in add T ([], ctxt) |>> append declss o map snd end
   153 
   154 end;