src/HOL/Tools/Datatype/rep_datatype.ML
author wenzelm
Thu Dec 15 17:37:14 2011 +0100 (2011-12-15)
changeset 45890 5f70aaecae26
child 45891 d73605c829cc
permissions -rw-r--r--
separate rep_datatype.ML;
tuned signature;
wenzelm@45890
     1
(*  Title:      HOL/Tools/Datatype/rep_datatype.ML
wenzelm@45890
     2
    Author:     Stefan Berghofer, TU Muenchen
wenzelm@45890
     3
wenzelm@45890
     4
Representation of existing types as datatypes.
wenzelm@45890
     5
*)
wenzelm@45890
     6
wenzelm@45890
     7
signature REP_DATATYPE =
wenzelm@45890
     8
sig
wenzelm@45890
     9
  val derive_datatype_props : Datatype_Aux.config -> string list -> Datatype_Aux.descr list ->
wenzelm@45890
    10
    thm -> thm list list -> thm list list -> theory -> string list * theory
wenzelm@45890
    11
  val rep_datatype : Datatype_Aux.config -> (string list -> Proof.context -> Proof.context) ->
wenzelm@45890
    12
    term list -> theory -> Proof.state
wenzelm@45890
    13
  val rep_datatype_cmd : Datatype_Aux.config -> (string list -> Proof.context -> Proof.context) ->
wenzelm@45890
    14
    string list -> theory -> Proof.state
wenzelm@45890
    15
end;
wenzelm@45890
    16
wenzelm@45890
    17
structure Rep_Datatype: REP_DATATYPE =
wenzelm@45890
    18
struct
wenzelm@45890
    19
wenzelm@45890
    20
fun make_dt_info descr induct inducts rec_names rec_rewrites
wenzelm@45890
    21
    (index, (((((((((((_, (tname, _, _))), inject), distinct),
wenzelm@45890
    22
      exhaust), nchotomy), case_name), case_rewrites), case_cong), weak_case_cong),
wenzelm@45890
    23
        (split, split_asm))) =
wenzelm@45890
    24
  (tname,
wenzelm@45890
    25
   {index = index,
wenzelm@45890
    26
    descr = descr,
wenzelm@45890
    27
    inject = inject,
wenzelm@45890
    28
    distinct = distinct,
wenzelm@45890
    29
    induct = induct,
wenzelm@45890
    30
    inducts = inducts,
wenzelm@45890
    31
    exhaust = exhaust,
wenzelm@45890
    32
    nchotomy = nchotomy,
wenzelm@45890
    33
    rec_names = rec_names,
wenzelm@45890
    34
    rec_rewrites = rec_rewrites,
wenzelm@45890
    35
    case_name = case_name,
wenzelm@45890
    36
    case_rewrites = case_rewrites,
wenzelm@45890
    37
    case_cong = case_cong,
wenzelm@45890
    38
    weak_case_cong = weak_case_cong,
wenzelm@45890
    39
    split = split,
wenzelm@45890
    40
    split_asm = split_asm});
wenzelm@45890
    41
wenzelm@45890
    42
fun derive_datatype_props config dt_names descr induct inject distinct thy1 =
wenzelm@45890
    43
  let
wenzelm@45890
    44
    val thy2 = thy1 |> Theory.checkpoint;
wenzelm@45890
    45
    val flat_descr = flat descr;
wenzelm@45890
    46
    val new_type_names = map Long_Name.base_name dt_names;
wenzelm@45890
    47
    val _ =
wenzelm@45890
    48
      Datatype_Aux.message config
wenzelm@45890
    49
        ("Deriving properties for datatype(s) " ^ commas_quote new_type_names);
wenzelm@45890
    50
wenzelm@45890
    51
    val (exhaust, thy3) = thy2
wenzelm@45890
    52
      |> Datatype_Abs_Proofs.prove_casedist_thms config new_type_names
wenzelm@45890
    53
        descr induct (Datatype_Data.mk_case_names_exhausts flat_descr dt_names);
wenzelm@45890
    54
    val (nchotomys, thy4) = thy3
wenzelm@45890
    55
      |> Datatype_Abs_Proofs.prove_nchotomys config new_type_names descr exhaust;
wenzelm@45890
    56
    val ((rec_names, rec_rewrites), thy5) = thy4
wenzelm@45890
    57
      |> Datatype_Abs_Proofs.prove_primrec_thms
wenzelm@45890
    58
        config new_type_names descr (#inject o the o Symtab.lookup (Datatype_Data.get_all thy4))
wenzelm@45890
    59
        inject (distinct, Datatype_Data.all_distincts thy2 (Datatype_Aux.get_rec_types flat_descr))
wenzelm@45890
    60
        induct;
wenzelm@45890
    61
    val ((case_rewrites, case_names), thy6) = thy5
wenzelm@45890
    62
      |> Datatype_Abs_Proofs.prove_case_thms config new_type_names descr rec_names rec_rewrites;
wenzelm@45890
    63
    val (case_congs, thy7) = thy6
wenzelm@45890
    64
      |> Datatype_Abs_Proofs.prove_case_congs new_type_names case_names descr
wenzelm@45890
    65
        nchotomys case_rewrites;
wenzelm@45890
    66
    val (weak_case_congs, thy8) = thy7
wenzelm@45890
    67
      |> Datatype_Abs_Proofs.prove_weak_case_congs new_type_names case_names descr;
wenzelm@45890
    68
    val (splits, thy9) = thy8
wenzelm@45890
    69
      |> Datatype_Abs_Proofs.prove_split_thms
wenzelm@45890
    70
        config new_type_names case_names descr inject distinct exhaust case_rewrites;
wenzelm@45890
    71
wenzelm@45890
    72
    val inducts = Project_Rule.projections (Proof_Context.init_global thy2) induct;
wenzelm@45890
    73
    val dt_infos =
wenzelm@45890
    74
      map_index
wenzelm@45890
    75
        (make_dt_info flat_descr induct inducts rec_names rec_rewrites)
wenzelm@45890
    76
        (hd descr ~~ inject ~~ distinct ~~ exhaust ~~ nchotomys ~~
wenzelm@45890
    77
          case_names ~~ case_rewrites ~~ case_congs ~~ weak_case_congs ~~ splits);
wenzelm@45890
    78
    val dt_names = map fst dt_infos;
wenzelm@45890
    79
    val prfx = Binding.qualify true (space_implode "_" new_type_names);
wenzelm@45890
    80
    val simps = flat (inject @ distinct @ case_rewrites) @ rec_rewrites;
wenzelm@45890
    81
    val named_rules = flat (map_index (fn (index, tname) =>
wenzelm@45890
    82
      [((Binding.empty, [nth inducts index]), [Induct.induct_type tname]),
wenzelm@45890
    83
       ((Binding.empty, [nth exhaust index]), [Induct.cases_type tname])]) dt_names);
wenzelm@45890
    84
    val unnamed_rules = map (fn induct =>
wenzelm@45890
    85
      ((Binding.empty, [induct]), [Rule_Cases.inner_rule, Induct.induct_type ""]))
wenzelm@45890
    86
        (drop (length dt_names) inducts);
wenzelm@45890
    87
  in
wenzelm@45890
    88
    thy9
wenzelm@45890
    89
    |> Global_Theory.add_thmss ([((prfx (Binding.name "simps"), simps), []),
wenzelm@45890
    90
        ((prfx (Binding.name "inducts"), inducts), []),
wenzelm@45890
    91
        ((prfx (Binding.name "splits"), maps (fn (x, y) => [x, y]) splits), []),
wenzelm@45890
    92
        ((Binding.empty, flat case_rewrites @ flat distinct @ rec_rewrites),
wenzelm@45890
    93
          [Simplifier.simp_add]),
wenzelm@45890
    94
        ((Binding.empty, rec_rewrites), [Code.add_default_eqn_attribute]),
wenzelm@45890
    95
        ((Binding.empty, flat inject), [iff_add]),
wenzelm@45890
    96
        ((Binding.empty, map (fn th => th RS notE) (flat distinct)),
wenzelm@45890
    97
          [Classical.safe_elim NONE]),
wenzelm@45890
    98
        ((Binding.empty, weak_case_congs), [Simplifier.cong_add]),
wenzelm@45890
    99
        ((Binding.empty, flat (distinct @ inject)), [Induct.induct_simp_add])] @
wenzelm@45890
   100
          named_rules @ unnamed_rules)
wenzelm@45890
   101
    |> snd
wenzelm@45890
   102
    |> Datatype_Data.add_case_tr' case_names
wenzelm@45890
   103
    |> Datatype_Data.register dt_infos
wenzelm@45890
   104
    |> Datatype_Data.interpretation_data (config, dt_names)
wenzelm@45890
   105
    |> pair dt_names
wenzelm@45890
   106
  end;
wenzelm@45890
   107
wenzelm@45890
   108
wenzelm@45890
   109
wenzelm@45890
   110
(** declare existing type as datatype **)
wenzelm@45890
   111
wenzelm@45890
   112
local
wenzelm@45890
   113
wenzelm@45890
   114
fun prove_rep_datatype config dt_names descr raw_inject half_distinct raw_induct thy1 =
wenzelm@45890
   115
  let
wenzelm@45890
   116
    val raw_distinct = (map o maps) (fn thm => [thm, thm RS not_sym]) half_distinct;
wenzelm@45890
   117
    val new_type_names = map Long_Name.base_name dt_names;
wenzelm@45890
   118
    val prfx = Binding.qualify true (space_implode "_" new_type_names);
wenzelm@45890
   119
    val (((inject, distinct), [induct]), thy2) =
wenzelm@45890
   120
      thy1
wenzelm@45890
   121
      |> Datatype_Aux.store_thmss "inject" new_type_names raw_inject
wenzelm@45890
   122
      ||>> Datatype_Aux.store_thmss "distinct" new_type_names raw_distinct
wenzelm@45890
   123
      ||>> Global_Theory.add_thms
wenzelm@45890
   124
        [((prfx (Binding.name "induct"), raw_induct),
wenzelm@45890
   125
          [Datatype_Data.mk_case_names_induct descr])];
wenzelm@45890
   126
  in
wenzelm@45890
   127
    thy2
wenzelm@45890
   128
    |> derive_datatype_props config dt_names [descr] induct inject distinct
wenzelm@45890
   129
 end;
wenzelm@45890
   130
wenzelm@45890
   131
fun gen_rep_datatype prep_term config after_qed raw_ts thy =
wenzelm@45890
   132
  let
wenzelm@45890
   133
    val ctxt = Proof_Context.init_global thy;
wenzelm@45890
   134
wenzelm@45890
   135
    fun constr_of_term (Const (c, T)) = (c, T)
wenzelm@45890
   136
      | constr_of_term t = error ("Not a constant: " ^ Syntax.string_of_term ctxt t);
wenzelm@45890
   137
    fun no_constr (c, T) =
wenzelm@45890
   138
      error ("Bad constructor: " ^ Proof_Context.extern_const ctxt c ^ "::" ^
wenzelm@45890
   139
        Syntax.string_of_typ ctxt T);
wenzelm@45890
   140
    fun type_of_constr (cT as (_, T)) =
wenzelm@45890
   141
      let
wenzelm@45890
   142
        val frees = Term.add_tfreesT T [];
wenzelm@45890
   143
        val (tyco, vs) = (apsnd o map) dest_TFree (dest_Type (body_type T))
wenzelm@45890
   144
          handle TYPE _ => no_constr cT
wenzelm@45890
   145
        val _ = if has_duplicates (eq_fst (op =)) vs then no_constr cT else ();
wenzelm@45890
   146
        val _ = if length frees <> length vs then no_constr cT else ();
wenzelm@45890
   147
      in (tyco, (vs, cT)) end;
wenzelm@45890
   148
wenzelm@45890
   149
    val raw_cs =
wenzelm@45890
   150
      AList.group (op =) (map (type_of_constr o constr_of_term o prep_term thy) raw_ts);
wenzelm@45890
   151
    val _ =
wenzelm@45890
   152
      (case map_filter (fn (tyco, _) =>
wenzelm@45890
   153
          if Symtab.defined (Datatype_Data.get_all thy) tyco then SOME tyco else NONE) raw_cs of
wenzelm@45890
   154
        [] => ()
wenzelm@45890
   155
      | tycos => error ("Type(s) " ^ commas_quote tycos ^ " already represented inductivly"));
wenzelm@45890
   156
    val raw_vss = maps (map (map snd o fst) o snd) raw_cs;
wenzelm@45890
   157
    val ms =
wenzelm@45890
   158
      (case distinct (op =) (map length raw_vss) of
wenzelm@45890
   159
         [n] => 0 upto n - 1
wenzelm@45890
   160
      | _ => error "Different types in given constructors");
wenzelm@45890
   161
    fun inter_sort m =
wenzelm@45890
   162
      map (fn xs => nth xs m) raw_vss
wenzelm@45890
   163
      |> foldr1 (Sorts.inter_sort (Sign.classes_of thy));
wenzelm@45890
   164
    val sorts = map inter_sort ms;
wenzelm@45890
   165
    val vs = Name.invent_names Name.context Name.aT sorts;
wenzelm@45890
   166
wenzelm@45890
   167
    fun norm_constr (raw_vs, (c, T)) =
wenzelm@45890
   168
      (c, map_atyps
wenzelm@45890
   169
        (TFree o (the o AList.lookup (op =) (map fst raw_vs ~~ vs)) o fst o dest_TFree) T);
wenzelm@45890
   170
wenzelm@45890
   171
    val cs = map (apsnd (map norm_constr)) raw_cs;
wenzelm@45890
   172
    val dtyps_of_typ = map (Datatype_Aux.dtyp_of_typ (map (rpair vs o fst) cs)) o binder_types;
wenzelm@45890
   173
    val dt_names = map fst cs;
wenzelm@45890
   174
wenzelm@45890
   175
    fun mk_spec (i, (tyco, constr)) =
wenzelm@45890
   176
      (i, (tyco, map Datatype_Aux.DtTFree vs, (map o apsnd) dtyps_of_typ constr));
wenzelm@45890
   177
    val descr = map_index mk_spec cs;
wenzelm@45890
   178
    val injs = Datatype_Prop.make_injs [descr];
wenzelm@45890
   179
    val half_distincts = Datatype_Prop.make_distincts [descr];
wenzelm@45890
   180
    val ind = Datatype_Prop.make_ind [descr];
wenzelm@45890
   181
    val rules = (map o map o map) Logic.close_form [[[ind]], injs, half_distincts];
wenzelm@45890
   182
wenzelm@45890
   183
    fun after_qed' raw_thms =
wenzelm@45890
   184
      let
wenzelm@45890
   185
        val [[[raw_induct]], raw_inject, half_distinct] =
wenzelm@45890
   186
          unflat rules (map Drule.zero_var_indexes_list raw_thms);
wenzelm@45890
   187
            (*FIXME somehow dubious*)
wenzelm@45890
   188
      in
wenzelm@45890
   189
        Proof_Context.background_theory_result  (* FIXME !? *)
wenzelm@45890
   190
          (prove_rep_datatype config dt_names descr raw_inject half_distinct raw_induct)
wenzelm@45890
   191
        #-> after_qed
wenzelm@45890
   192
      end;
wenzelm@45890
   193
  in
wenzelm@45890
   194
    ctxt
wenzelm@45890
   195
    |> Proof.theorem NONE after_qed' ((map o map) (rpair []) (flat rules))
wenzelm@45890
   196
  end;
wenzelm@45890
   197
wenzelm@45890
   198
in
wenzelm@45890
   199
wenzelm@45890
   200
val rep_datatype = gen_rep_datatype Sign.cert_term;
wenzelm@45890
   201
val rep_datatype_cmd = gen_rep_datatype Syntax.read_term_global;
wenzelm@45890
   202
wenzelm@45890
   203
end;
wenzelm@45890
   204
wenzelm@45890
   205
wenzelm@45890
   206
(* outer syntax *)
wenzelm@45890
   207
wenzelm@45890
   208
val _ =
wenzelm@45890
   209
  Outer_Syntax.command "rep_datatype" "represent existing types inductively" Keyword.thy_goal
wenzelm@45890
   210
    (Scan.repeat1 Parse.term >> (fn ts =>
wenzelm@45890
   211
      Toplevel.print o
wenzelm@45890
   212
      Toplevel.theory_to_proof (rep_datatype_cmd Datatype_Aux.default_config (K I) ts)));
wenzelm@45890
   213
wenzelm@45890
   214
end;