src/HOL/Tools/SMT/smt_datatypes.ML
author blanchet
Wed Sep 17 23:45:57 2014 +0200 (2014-09-17)
changeset 58364 efc56d935728
parent 58362 cf32eb8001b8
child 58427 cc1bab5558b0
permissions -rw-r--r--
tuning
     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 -> typ ->
    11     (typ * (term * term list) list) list list * Proof.context ->
    12     (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     Ctr_Sugar_Util.fold_map2 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 (* Simplification: We assume that every type that is not a codatatype is a datatype (or a
    65    record). *)
    66 fun fp_kind_of ctxt n =
    67   (case BNF_FP_Def_Sugar.fp_sugar_of ctxt n of
    68     SOME {fp, ...} => fp
    69   | NONE => BNF_Util.Least_FP)
    70 
    71 fun get_decls fp T n Ts ctxt =
    72   let
    73     fun fallback () =
    74       (case Typedef.get_info ctxt n of
    75         [] => ([], ctxt)
    76       | info :: _ => (get_typedef_decl info T Ts, ctxt))
    77   in
    78     (case Ctr_Sugar.ctr_sugar_of ctxt n of
    79       SOME (ctr_sugar as {T = U as Type (_, Us), ...}) =>
    80       if fp_kind_of ctxt n = fp then get_ctr_sugar_decl ctr_sugar T Ts ctxt else fallback ()
    81     | NONE => fallback ())
    82   end
    83 
    84 fun add_decls fp T (declss, ctxt) =
    85   let
    86     fun declared T = exists (exists (equal T o fst))
    87     fun declared' T = exists (exists (equal T o fst) o snd)
    88     fun depends ds = exists (member (op =) (map fst ds))
    89 
    90     fun add (TFree _) = I
    91       | add (TVar _) = I
    92       | add (T as Type (@{type_name fun}, _)) =
    93           fold add (Term.body_type T :: Term.binder_types T)
    94       | add @{typ bool} = I
    95       | add (T as Type (n, Ts)) = (fn (dss, ctxt1) =>
    96           if declared T declss orelse declared' T dss then (dss, ctxt1)
    97           else if SMT_Builtin.is_builtin_typ_ext ctxt1 T then (dss, ctxt1)
    98           else
    99             (case get_decls fp T n Ts ctxt1 of
   100               ([], _) => (dss, ctxt1)
   101             | (ds, ctxt2) =>
   102                 let
   103                   val constrTs = maps (map (snd o Term.dest_Const o fst) o snd) ds
   104                   val Us = fold (union (op =) o Term.binder_types) constrTs []
   105 
   106                   fun ins [] = [(Us, ds)]
   107                     | ins ((Uds as (Us', _)) :: Udss) =
   108                         if depends ds Us' then (Us, ds) :: Uds :: Udss else Uds :: ins Udss
   109             in fold add Us (ins dss, ctxt2) end))
   110   in add T ([], ctxt) |>> append declss o map snd end
   111 
   112 end;