src/HOL/Tools/SMT/smt_datatypes.ML
author blanchet
Thu Aug 28 00:40:38 2014 +0200 (2014-08-28)
changeset 58061 3d060f43accb
parent 57229 src/HOL/Tools/SMT2/smt2_datatypes.ML@489083abce44
child 58360 dee1fd1cc631
permissions -rw-r--r--
renamed new SMT module from 'SMT2' to 'SMT'
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@56078
     5
as algebraic datatypes.
blanchet@56078
     6
*)
blanchet@56078
     7
blanchet@58061
     8
signature SMT_DATATYPES =
blanchet@56078
     9
sig
blanchet@56078
    10
  val add_decls: typ ->
blanchet@56078
    11
    (typ * (term * term list) list) list list * Proof.context ->
blanchet@56078
    12
    (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@57226
    18
fun mk_selectors T Ts =
blanchet@57226
    19
  Variable.variant_fixes (replicate (length Ts) "select")
blanchet@57226
    20
  #>> map2 (fn U => fn n => Free (n, T --> U)) Ts
blanchet@56078
    21
blanchet@56078
    22
blanchet@57226
    23
(* free constructor type declarations *)
blanchet@56078
    24
blanchet@57226
    25
fun get_ctr_sugar_decl ({ctrs, ...} : Ctr_Sugar.ctr_sugar) T Ts ctxt =
blanchet@56078
    26
  let
blanchet@57226
    27
    fun mk_constr ctr0 =
blanchet@57226
    28
      let val ctr = Ctr_Sugar.mk_ctr Ts ctr0 in
blanchet@57226
    29
        mk_selectors T (binder_types (fastype_of ctr)) #>> pair ctr
blanchet@57226
    30
      end
blanchet@57226
    31
  in
blanchet@57226
    32
    fold_map mk_constr ctrs ctxt
blanchet@57226
    33
    |>> (pair T #> single)
blanchet@57226
    34
  end
blanchet@56078
    35
blanchet@56078
    36
blanchet@56078
    37
(* typedef declarations *)
blanchet@56078
    38
blanchet@57213
    39
fun get_typedef_decl (({Abs_name, Rep_name, abs_type, rep_type, ...}, {Abs_inverse, ...})
blanchet@57213
    40
    : Typedef.info) T Ts =
blanchet@57213
    41
  if can (curry (op RS) @{thm UNIV_I}) Abs_inverse then
blanchet@57213
    42
    let
blanchet@57213
    43
      val env = snd (Term.dest_Type abs_type) ~~ Ts
blanchet@57213
    44
      val instT = Term.map_atyps (perhaps (AList.lookup (op =) env))
blanchet@56078
    45
blanchet@57213
    46
      val constr = Const (Abs_name, instT (rep_type --> abs_type))
blanchet@57213
    47
      val select = Const (Rep_name, instT (abs_type --> rep_type))
blanchet@57213
    48
    in [(T, [(constr, [select])])] end
blanchet@57213
    49
  else
blanchet@57213
    50
    []
blanchet@56078
    51
blanchet@56078
    52
blanchet@56078
    53
(* collection of declarations *)
blanchet@56078
    54
blanchet@56078
    55
fun declared declss T = exists (exists (equal T o fst)) declss
blanchet@56078
    56
fun declared' dss T = exists (exists (equal T o fst) o snd) dss
blanchet@56078
    57
blanchet@56078
    58
fun get_decls T n Ts ctxt =
blanchet@57226
    59
  (case Ctr_Sugar.ctr_sugar_of ctxt n of
blanchet@57226
    60
    SOME ctr_sugar => get_ctr_sugar_decl ctr_sugar T Ts ctxt
blanchet@57226
    61
  | NONE =>
blanchet@57226
    62
      (case Typedef.get_info ctxt n of
blanchet@57226
    63
        [] => ([], ctxt)
blanchet@57226
    64
      | info :: _ => (get_typedef_decl info T Ts, ctxt)))
blanchet@56078
    65
blanchet@56078
    66
fun add_decls T (declss, ctxt) =
blanchet@56078
    67
  let
blanchet@56078
    68
    fun depends Ts ds = exists (member (op =) (map fst ds)) Ts
blanchet@56078
    69
blanchet@56078
    70
    fun add (TFree _) = I
blanchet@56078
    71
      | add (TVar _) = I
blanchet@56078
    72
      | add (T as Type (@{type_name fun}, _)) =
blanchet@56078
    73
          fold add (Term.body_type T :: Term.binder_types T)
blanchet@56078
    74
      | add @{typ bool} = I
blanchet@56078
    75
      | add (T as Type (n, Ts)) = (fn (dss, ctxt1) =>
blanchet@56078
    76
          if declared declss T orelse declared' dss T then (dss, ctxt1)
blanchet@58061
    77
          else if SMT_Builtin.is_builtin_typ_ext ctxt1 T then (dss, ctxt1)
blanchet@56078
    78
          else
blanchet@56078
    79
            (case get_decls T n Ts ctxt1 of
blanchet@56078
    80
              ([], _) => (dss, ctxt1)
blanchet@56078
    81
            | (ds, ctxt2) =>
blanchet@56078
    82
                let
blanchet@57226
    83
                  val constrTs = maps (map (snd o Term.dest_Const o fst) o snd) ds
blanchet@56078
    84
                  val Us = fold (union (op =) o Term.binder_types) constrTs []
blanchet@56078
    85
blanchet@56078
    86
                  fun ins [] = [(Us, ds)]
blanchet@56078
    87
                    | ins ((Uds as (Us', _)) :: Udss) =
blanchet@56078
    88
                        if depends Us' ds then (Us, ds) :: Uds :: Udss
blanchet@56078
    89
                        else Uds :: ins Udss
blanchet@56078
    90
            in fold add Us (ins dss, ctxt2) end))
blanchet@56078
    91
  in add T ([], ctxt) |>> append declss o map snd end
blanchet@56078
    92
blanchet@57229
    93
end;