src/HOL/Library/Old_SMT/old_smt_datatypes.ML
author blanchet
Wed Sep 24 15:45:55 2014 +0200 (2014-09-24)
changeset 58425 246985c6b20b
parent 58058 1a0b18176548
permissions -rw-r--r--
simpler proof
blanchet@58058
     1
(*  Title:      HOL/Library/Old_SMT/old_smt_datatypes.ML
boehmes@41426
     2
    Author:     Sascha Boehme, TU Muenchen
boehmes@41426
     3
boehmes@41426
     4
Collector functions for common type declarations and their representation
boehmes@41426
     5
as algebraic datatypes.
boehmes@41426
     6
*)
boehmes@41426
     7
blanchet@58058
     8
signature OLD_SMT_DATATYPES =
boehmes@41426
     9
sig
boehmes@41426
    10
  val add_decls: typ ->
boehmes@41426
    11
    (typ * (term * term list) list) list list * Proof.context ->
boehmes@41426
    12
    (typ * (term * term list) list) list list * Proof.context
boehmes@41426
    13
end
boehmes@41426
    14
blanchet@58058
    15
structure Old_SMT_Datatypes: OLD_SMT_DATATYPES =
boehmes@41426
    16
struct
boehmes@41426
    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
boehmes@41426
    21
boehmes@41426
    22
blanchet@57226
    23
(* free constructor type declarations *)
boehmes@41426
    24
blanchet@57226
    25
fun get_ctr_sugar_decl ({ctrs, ...} : Ctr_Sugar.ctr_sugar) T Ts ctxt =
boehmes@41426
    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
boehmes@41426
    35
boehmes@41426
    36
boehmes@41426
    37
(* typedef declarations *)
boehmes@41426
    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))
boehmes@41426
    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
    []
boehmes@41426
    51
boehmes@41426
    52
boehmes@41426
    53
(* collection of declarations *)
boehmes@41426
    54
boehmes@41426
    55
fun declared declss T = exists (exists (equal T o fst)) declss
boehmes@43385
    56
fun declared' dss T = exists (exists (equal T o fst) o snd) dss
boehmes@41426
    57
boehmes@41426
    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)))
boehmes@41426
    65
boehmes@41426
    66
fun add_decls T (declss, ctxt) =
boehmes@41426
    67
  let
boehmes@43385
    68
    fun depends Ts ds = exists (member (op =) (map fst ds)) Ts
boehmes@43385
    69
boehmes@41426
    70
    fun add (TFree _) = I
boehmes@41426
    71
      | add (TVar _) = I
boehmes@41426
    72
      | add (T as Type (@{type_name fun}, _)) =
boehmes@41426
    73
          fold add (Term.body_type T :: Term.binder_types T)
boehmes@41426
    74
      | add @{typ bool} = I
boehmes@41426
    75
      | add (T as Type (n, Ts)) = (fn (dss, ctxt1) =>
boehmes@43385
    76
          if declared declss T orelse declared' dss T then (dss, ctxt1)
blanchet@58058
    77
          else if Old_SMT_Builtin.is_builtin_typ_ext ctxt1 T then (dss, ctxt1)
boehmes@41426
    78
          else
boehmes@41426
    79
            (case get_decls T n Ts ctxt1 of
boehmes@41426
    80
              ([], _) => (dss, ctxt1)
boehmes@41426
    81
            | (ds, ctxt2) =>
boehmes@41426
    82
                let
boehmes@41426
    83
                  val constrTs =
boehmes@41426
    84
                    maps (map (snd o Term.dest_Const o fst) o snd) ds
boehmes@41426
    85
                  val Us = fold (union (op =) o Term.binder_types) constrTs []
boehmes@43385
    86
boehmes@43385
    87
                  fun ins [] = [(Us, ds)]
boehmes@43385
    88
                    | ins ((Uds as (Us', _)) :: Udss) =
boehmes@43385
    89
                        if depends Us' ds then (Us, ds) :: Uds :: Udss
boehmes@43385
    90
                        else Uds :: ins Udss
boehmes@43385
    91
            in fold add Us (ins dss, ctxt2) end))
boehmes@43385
    92
  in add T ([], ctxt) |>> append declss o map snd end
boehmes@43385
    93
boehmes@41426
    94
end