src/HOL/Tools/SMT/smt_datatypes.ML
author wenzelm
Sat Apr 16 16:15:37 2011 +0200 (2011-04-16)
changeset 42361 23f352990944
parent 41426 09615ed31f04
child 43385 9cd4b4ecb4dd
permissions -rw-r--r--
modernized structure Proof_Context;
     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 algebraic datatypes.
     6 *)
     7 
     8 signature SMT_DATATYPES =
     9 sig
    10   val add_decls: 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 val lhs_head_of = Term.head_of o fst o Logic.dest_equals o Thm.prop_of
    19 
    20 fun mk_selectors T Ts ctxt =
    21   let
    22     val (sels, ctxt') =
    23       Variable.variant_fixes (replicate (length Ts) "select") ctxt
    24   in (map2 (fn n => fn U => Free (n, T --> U)) sels Ts, ctxt') end
    25 
    26 
    27 (* datatype declarations *)
    28 
    29 fun get_datatype_decl ({descr, ...} : Datatype.info) n Ts ctxt =
    30   let
    31     fun get_vars (_, (m, vs, _)) = if m = n then SOME vs else NONE
    32     val vars = the (get_first get_vars descr) ~~ Ts
    33     val lookup_var = the o AList.lookup (op =) vars
    34 
    35     val dTs = map (apsnd (fn (m, vs, _) => Type (m, map lookup_var vs))) descr
    36     val lookup_typ = the o AList.lookup (op =) dTs
    37 
    38     fun typ_of (dt as Datatype.DtTFree _) = lookup_var dt
    39       | typ_of (Datatype.DtType (n, dts)) = Type (n, map typ_of dts)
    40       | typ_of (Datatype.DtRec i) = lookup_typ i
    41 
    42     fun mk_constr T (m, dts) ctxt =
    43       let
    44         val Ts = map typ_of dts
    45         val constr = Const (m, Ts ---> T)
    46         val (selects, ctxt') = mk_selectors T Ts ctxt
    47       in ((constr, selects), ctxt') end
    48 
    49     fun mk_decl (i, (_, _, constrs)) ctxt =
    50       let
    51         val T = lookup_typ i
    52         val (css, ctxt') = fold_map (mk_constr T) constrs ctxt
    53       in ((T, css), ctxt') end
    54 
    55   in fold_map mk_decl descr ctxt end
    56 
    57 
    58 (* record declarations *)
    59 
    60 val record_name_of = Long_Name.implode o fst o split_last o Long_Name.explode
    61 
    62 fun get_record_decl ({ext_def, ...} : Record.info) T ctxt =
    63   let
    64     val (con, _) = Term.dest_Const (lhs_head_of ext_def)
    65     val (fields, more) = Record.get_extT_fields (Proof_Context.theory_of ctxt) T
    66     val fieldTs = map snd fields @ [snd more]
    67 
    68     val constr = Const (con, fieldTs ---> T)
    69     val (selects, ctxt') = mk_selectors T fieldTs ctxt
    70   in ((T, [(constr, selects)]), ctxt') end
    71 
    72 
    73 (* typedef declarations *)
    74 
    75 fun get_typedef_decl (info : Typedef.info) T Ts =
    76   let
    77     val ({Abs_name, Rep_name, abs_type, rep_type, ...}, _) = info
    78 
    79     val env = snd (Term.dest_Type abs_type) ~~ Ts
    80     val instT = Term.map_atyps (perhaps (AList.lookup (op =) env))
    81 
    82     val constr = Const (Abs_name, instT (rep_type --> abs_type))
    83     val select = Const (Rep_name, instT (abs_type --> rep_type))
    84   in (T, [(constr, [select])]) end
    85 
    86 
    87 (* collection of declarations *)
    88 
    89 fun declared declss T = exists (exists (equal T o fst)) declss
    90 
    91 fun get_decls T n Ts ctxt =
    92   let val thy = Proof_Context.theory_of ctxt
    93   in
    94     (case Datatype.get_info thy n of
    95       SOME info => get_datatype_decl info n Ts ctxt
    96     | NONE =>
    97         (case Record.get_info thy (record_name_of n) of
    98           SOME info => get_record_decl info T ctxt |>> single
    99         | NONE =>
   100             (case Typedef.get_info ctxt n of
   101               [] => ([], ctxt)
   102             | info :: _ => ([get_typedef_decl info T Ts], ctxt))))
   103   end
   104 
   105 fun add_decls T (declss, ctxt) =
   106   let
   107     fun add (TFree _) = I
   108       | add (TVar _) = I
   109       | add (T as Type (@{type_name fun}, _)) =
   110           fold add (Term.body_type T :: Term.binder_types T)
   111       | add @{typ bool} = I
   112       | add (T as Type (n, Ts)) = (fn (dss, ctxt1) =>
   113           if declared declss T orelse declared dss T then (dss, ctxt1)
   114           else if SMT_Builtin.is_builtin_typ_ext ctxt1 T then (dss, ctxt1)
   115           else
   116             (case get_decls T n Ts ctxt1 of
   117               ([], _) => (dss, ctxt1)
   118             | (ds, ctxt2) =>
   119                 let
   120                   val constrTs =
   121                     maps (map (snd o Term.dest_Const o fst) o snd) ds
   122                   val Us = fold (union (op =) o Term.binder_types) constrTs []
   123             in fold add Us (ds :: dss, ctxt2) end))
   124   in add T ([], ctxt) |>> append declss end
   125 
   126 end