src/HOL/Tools/record_package.ML
author wenzelm
Tue May 05 13:27:18 1998 +0200 (1998-05-05)
changeset 4895 0fad2acb45fb
parent 4894 32187e0b8b48
child 4967 c9c481bc0216
permissions -rw-r--r--
misc tuning;
wenzelm@4867
     1
(*  Title:      HOL/Tools/record_package.ML
wenzelm@4867
     2
    ID:         $Id$
wenzelm@4867
     3
    Author:     Wolfgang Naraschewski and Markus Wenzel, TU Muenchen
wenzelm@4867
     4
wenzelm@4867
     5
Extensible records with structural subtyping in HOL.
wenzelm@4867
     6
wenzelm@4867
     7
TODO:
wenzelm@4890
     8
  - field types: typedef;
wenzelm@4867
     9
  - trfuns for record types;
wenzelm@4894
    10
  - operations and theorems: split, split_all/ex, ...;
wenzelm@4895
    11
  - field constructor: more specific type for snd component;
wenzelm@4894
    12
  - update_more operation;
wenzelm@4895
    13
  - field syntax: "..." for "... = more", "?..." for "?... = ?more";
wenzelm@4867
    14
*)
wenzelm@4867
    15
wenzelm@4867
    16
signature RECORD_PACKAGE =
wenzelm@4867
    17
sig
wenzelm@4890
    18
  val moreS: sort
wenzelm@4890
    19
  val mk_fieldT: (string * typ) * typ -> typ
wenzelm@4890
    20
  val dest_fieldT: typ -> (string * typ) * typ
wenzelm@4890
    21
  val mk_field: (string * term) * term -> term
wenzelm@4890
    22
  val mk_fst: term -> term
wenzelm@4890
    23
  val mk_snd: term -> term
wenzelm@4890
    24
  val mk_recordT: (string * typ) list * typ -> typ
wenzelm@4890
    25
  val dest_recordT: typ -> (string * typ) list * typ
wenzelm@4890
    26
  val mk_record: (string * term) list * term -> term
wenzelm@4890
    27
  val mk_sel: term -> string -> term
wenzelm@4890
    28
  val mk_update: term -> string * term -> term
wenzelm@4867
    29
  val print_records: theory -> unit
wenzelm@4867
    30
  val add_record: (string list * bstring) -> string option
wenzelm@4867
    31
    -> (bstring * string) list -> theory -> theory
wenzelm@4867
    32
  val add_record_i: (string list * bstring) -> (typ list * string) option
wenzelm@4867
    33
    -> (bstring * typ) list -> theory -> theory
wenzelm@4867
    34
  val setup: (theory -> theory) list
wenzelm@4867
    35
end;
wenzelm@4867
    36
wenzelm@4867
    37
structure RecordPackage: RECORD_PACKAGE =
wenzelm@4867
    38
struct
wenzelm@4867
    39
wenzelm@4867
    40
wenzelm@4894
    41
(*** utilities ***)
wenzelm@4867
    42
wenzelm@4894
    43
(* string suffixes *)
wenzelm@4867
    44
wenzelm@4867
    45
fun suffix sfx s = s ^ sfx;
wenzelm@4867
    46
wenzelm@4867
    47
fun unsuffix sfx s =
wenzelm@4867
    48
  let
wenzelm@4867
    49
    val cs = explode s;
wenzelm@4867
    50
    val prfx_len = size s - size sfx;
wenzelm@4867
    51
  in
wenzelm@4867
    52
    if prfx_len >= 0 andalso implode (drop (prfx_len, cs)) = sfx then
wenzelm@4867
    53
      implode (take (prfx_len, cs))
wenzelm@4867
    54
    else raise LIST "unsuffix"
wenzelm@4867
    55
  end;
wenzelm@4867
    56
wenzelm@4867
    57
wenzelm@4894
    58
(* definitions and equations *)
wenzelm@4867
    59
wenzelm@4894
    60
infix 0 :== === ;
wenzelm@4894
    61
wenzelm@4894
    62
val (op :==) = Logic.mk_defpair;
wenzelm@4894
    63
val (op ===) = HOLogic.mk_Trueprop o HOLogic.mk_eq;
wenzelm@4894
    64
wenzelm@4894
    65
fun get_defs thy specs = map (PureThy.get_tthm thy o fst) specs;
wenzelm@4894
    66
wenzelm@4894
    67
wenzelm@4894
    68
(* proof by simplification *)
wenzelm@4890
    69
wenzelm@4895
    70
fun prove_simp thy simps =
wenzelm@4894
    71
  let
wenzelm@4895
    72
    val sign = Theory.sign_of thy;
wenzelm@4895
    73
    val ss = Simplifier.addsimps (HOL_basic_ss, map Attribute.thm_of simps);
wenzelm@4895
    74
wenzelm@4895
    75
    fun prove goal =
wenzelm@4894
    76
      Attribute.tthm_of
wenzelm@4895
    77
        (Goals.prove_goalw_cterm [] (Thm.cterm_of sign goal)
wenzelm@4894
    78
          (K [ALLGOALS (Simplifier.simp_tac ss)])
wenzelm@4894
    79
        handle ERROR => error ("The error(s) above occurred while trying to prove "
wenzelm@4895
    80
          ^ quote (Sign.string_of_term sign goal)));
wenzelm@4894
    81
  in prove end;
wenzelm@4894
    82
wenzelm@4894
    83
wenzelm@4894
    84
wenzelm@4894
    85
(*** syntax operations ***)
wenzelm@4894
    86
wenzelm@4894
    87
(** name components **)
wenzelm@4894
    88
wenzelm@4894
    89
val moreN = "more";
wenzelm@4894
    90
val schemeN = "_scheme";
wenzelm@4894
    91
val fieldN = "_field";
wenzelm@4894
    92
val field_typeN = "_field_type";
wenzelm@4894
    93
val fstN = "_fst";
wenzelm@4894
    94
val sndN = "_snd";
wenzelm@4894
    95
val updateN = "_update";
wenzelm@4894
    96
val makeN = "make";
wenzelm@4894
    97
val make_schemeN = "make_scheme";
wenzelm@4890
    98
wenzelm@4890
    99
wenzelm@4890
   100
wenzelm@4867
   101
(** tuple operations **)
wenzelm@4867
   102
wenzelm@4867
   103
(* more type class *)
wenzelm@4867
   104
wenzelm@4867
   105
val moreS = ["more"];
wenzelm@4867
   106
wenzelm@4867
   107
wenzelm@4867
   108
(* types *)
wenzelm@4867
   109
wenzelm@4867
   110
fun mk_fieldT ((c, T), U) = Type (suffix field_typeN c, [T, U]);
wenzelm@4867
   111
wenzelm@4867
   112
fun dest_fieldT (typ as Type (c_field_type, [T, U])) =
wenzelm@4867
   113
      (case try (unsuffix field_typeN) c_field_type of
wenzelm@4867
   114
        None => raise TYPE ("dest_fieldT", [typ], [])
wenzelm@4867
   115
      | Some c => ((c, T), U))
wenzelm@4867
   116
  | dest_fieldT typ = raise TYPE ("dest_fieldT", [typ], []);
wenzelm@4867
   117
wenzelm@4867
   118
wenzelm@4867
   119
(* constructors *)
wenzelm@4867
   120
wenzelm@4867
   121
fun mk_fieldC U (c, T) = (suffix fieldN c, T --> U --> mk_fieldT ((c, T), U));
wenzelm@4867
   122
wenzelm@4867
   123
fun mk_field ((c, t), u) =
wenzelm@4867
   124
  let val T = fastype_of t and U = fastype_of u
wenzelm@4867
   125
  in Const (suffix fieldN c, [T, U] ---> mk_fieldT ((c, T), U)) $ t $ u end;
wenzelm@4867
   126
wenzelm@4867
   127
wenzelm@4867
   128
(* destructors *)
wenzelm@4867
   129
wenzelm@4867
   130
fun mk_fstC U (c, T) = (suffix fstN c, mk_fieldT ((c, T), U) --> T);
wenzelm@4867
   131
fun mk_sndC U (c, T) = (suffix sndN c, mk_fieldT ((c, T), U) --> U);
wenzelm@4867
   132
wenzelm@4867
   133
fun dest_field fst_or_snd p =
wenzelm@4867
   134
  let
wenzelm@4867
   135
    val pT = fastype_of p;
wenzelm@4867
   136
    val ((c, T), U) = dest_fieldT pT;
wenzelm@4867
   137
    val (destN, destT) = if fst_or_snd then (fstN, T) else (sndN, U);
wenzelm@4867
   138
  in Const (suffix destN c, pT --> destT) $ p end;
wenzelm@4867
   139
wenzelm@4867
   140
val mk_fst = dest_field true;
wenzelm@4867
   141
val mk_snd = dest_field false;
wenzelm@4867
   142
wenzelm@4867
   143
wenzelm@4867
   144
wenzelm@4867
   145
(** record operations **)
wenzelm@4867
   146
wenzelm@4867
   147
(* types *)
wenzelm@4867
   148
wenzelm@4867
   149
val mk_recordT = foldr mk_fieldT;
wenzelm@4867
   150
wenzelm@4867
   151
fun dest_recordT T =
wenzelm@4867
   152
  (case try dest_fieldT T of
wenzelm@4867
   153
    None => ([], T)
wenzelm@4867
   154
  | Some (c_T, U) => apfst (cons c_T) (dest_recordT U));
wenzelm@4867
   155
wenzelm@4867
   156
fun find_fieldT c rT =
wenzelm@4867
   157
  (case assoc (fst (dest_recordT rT), c) of
wenzelm@4867
   158
    None => raise TYPE ("find_field: " ^ c, [rT], [])
wenzelm@4867
   159
  | Some T => T);
wenzelm@4867
   160
wenzelm@4867
   161
wenzelm@4867
   162
(* constructors *)
wenzelm@4867
   163
wenzelm@4867
   164
val mk_record = foldr mk_field;
wenzelm@4867
   165
wenzelm@4867
   166
wenzelm@4867
   167
(* selectors *)
wenzelm@4867
   168
wenzelm@4867
   169
fun mk_selC rT (c, T) = (c, rT --> T);
wenzelm@4867
   170
wenzelm@4890
   171
fun mk_sel r c =
wenzelm@4867
   172
  let val rT = fastype_of r
wenzelm@4867
   173
  in Const (mk_selC rT (c, find_fieldT c rT)) $ r end;
wenzelm@4867
   174
wenzelm@4894
   175
val mk_moreC = mk_selC;
wenzelm@4894
   176
wenzelm@4894
   177
fun mk_more r c =
wenzelm@4894
   178
  let val rT = fastype_of r
wenzelm@4894
   179
  in Const (mk_moreC rT (c, snd (dest_recordT rT))) $ r end;
wenzelm@4894
   180
wenzelm@4867
   181
wenzelm@4867
   182
(* updates *)
wenzelm@4867
   183
wenzelm@4867
   184
fun mk_updateC rT (c, T) = (suffix updateN c, T --> rT --> rT);
wenzelm@4867
   185
wenzelm@4890
   186
fun mk_update r (c, x) =
wenzelm@4867
   187
  let val rT = fastype_of r
wenzelm@4867
   188
  in Const (mk_updateC rT (c, find_fieldT c rT)) $ x $ r end;
wenzelm@4867
   189
wenzelm@4867
   190
wenzelm@4890
   191
(* make *)
wenzelm@4890
   192
wenzelm@4890
   193
fun mk_makeC rT (c, Ts) = (c, Ts ---> rT);
wenzelm@4890
   194
wenzelm@4890
   195
wenzelm@4867
   196
wenzelm@4867
   197
(** concrete syntax for records **)
wenzelm@4867
   198
wenzelm@4867
   199
(* parse translations *)
wenzelm@4867
   200
wenzelm@4867
   201
fun field_tr (Const ("_field", _) $ Free (name, _) $ arg) =
wenzelm@4867
   202
      Syntax.const (suffix fieldN name) $ arg
wenzelm@4867
   203
  | field_tr t = raise TERM ("field_tr", [t]);
wenzelm@4867
   204
wenzelm@4867
   205
fun fields_tr (Const ("_fields", _) $ field $ fields) =
wenzelm@4867
   206
      field_tr field :: fields_tr fields
wenzelm@4867
   207
  | fields_tr field = [field_tr field];
wenzelm@4867
   208
wenzelm@4867
   209
fun record_tr (*"_record"*) [fields] =
wenzelm@4867
   210
      foldr (op $) (fields_tr fields, HOLogic.unit)
wenzelm@4867
   211
  | record_tr (*"_record"*) ts = raise TERM ("record_tr", ts);
wenzelm@4867
   212
wenzelm@4867
   213
fun record_scheme_tr (*"_record_scheme"*) [fields, more] =
wenzelm@4867
   214
      foldr (op $) (fields_tr fields, more)
wenzelm@4867
   215
  | record_scheme_tr (*"_record_scheme"*) ts = raise TERM ("record_scheme_tr", ts);
wenzelm@4867
   216
wenzelm@4867
   217
wenzelm@4890
   218
(* print translations *)
wenzelm@4867
   219
wenzelm@4867
   220
fun fields_tr' (tm as Const (name_field, _) $ arg $ more) =
wenzelm@4867
   221
      (case try (unsuffix fieldN) name_field of
wenzelm@4867
   222
        Some name =>
wenzelm@4867
   223
          apfst (cons (Syntax.const "_field" $ Syntax.free name $ arg)) (fields_tr' more)
wenzelm@4867
   224
      | None => ([], tm))
wenzelm@4867
   225
  | fields_tr' tm = ([], tm);
wenzelm@4867
   226
wenzelm@4867
   227
fun record_tr' tm =
wenzelm@4867
   228
  let
wenzelm@4867
   229
    val (fields, more) = fields_tr' tm;
wenzelm@4890
   230
    val fields' = foldr1 (fn (f, fs) => Syntax.const "_fields" $ f $ fs) fields;
wenzelm@4867
   231
  in
wenzelm@4890
   232
    if HOLogic.is_unit more then Syntax.const "_record" $ fields'
wenzelm@4890
   233
    else Syntax.const "_record_scheme" $ fields' $ more
wenzelm@4867
   234
  end;
wenzelm@4867
   235
wenzelm@4867
   236
fun field_tr' name [arg, more] = record_tr' (Syntax.const name $ arg $ more)
wenzelm@4867
   237
  | field_tr' _ _ = raise Match;
wenzelm@4867
   238
wenzelm@4867
   239
wenzelm@4867
   240
wenzelm@4867
   241
(*** extend theory by record definition ***)
wenzelm@4867
   242
wenzelm@4867
   243
(** record info **)
wenzelm@4867
   244
wenzelm@4867
   245
(* type record_info and parent_info *)
wenzelm@4867
   246
wenzelm@4867
   247
type record_info =
wenzelm@4867
   248
 {args: (string * sort) list,
wenzelm@4867
   249
  parent: (typ list * string) option,
wenzelm@4867
   250
  fields: (string * typ) list,
wenzelm@4895
   251
  simps: tthm list};
wenzelm@4867
   252
wenzelm@4867
   253
type parent_info =
wenzelm@4867
   254
 {name: string,
wenzelm@4867
   255
  fields: (string * typ) list,
wenzelm@4895
   256
  simps: tthm list};
wenzelm@4867
   257
wenzelm@4867
   258
wenzelm@4867
   259
(* theory data *)
wenzelm@4867
   260
wenzelm@4867
   261
val recordsK = "HOL/records";
wenzelm@4867
   262
exception Records of record_info Symtab.table;
wenzelm@4867
   263
wenzelm@4867
   264
fun print_records thy = Display.print_data thy recordsK;
wenzelm@4867
   265
wenzelm@4867
   266
local
wenzelm@4867
   267
  val empty = Records Symtab.empty;
wenzelm@4867
   268
wenzelm@4867
   269
  fun prep_ext (x as Records _) = x;
wenzelm@4867
   270
wenzelm@4867
   271
  fun merge (Records tab1, Records tab2) =
wenzelm@4867
   272
    Records (Symtab.merge (K true) (tab1, tab2));
wenzelm@4867
   273
wenzelm@4867
   274
  fun print sg (Records tab) =
wenzelm@4867
   275
    let
wenzelm@4867
   276
      val prt_typ = Sign.pretty_typ sg;
wenzelm@4867
   277
      val ext_const = Sign.cond_extern sg Sign.constK;
wenzelm@4867
   278
wenzelm@4867
   279
      fun pretty_parent None = []
wenzelm@4867
   280
        | pretty_parent (Some (Ts, name)) =
wenzelm@4867
   281
            [Pretty.block [prt_typ (Type (name, Ts)), Pretty.str " +"]];
wenzelm@4867
   282
wenzelm@4867
   283
      fun pretty_field (c, T) = Pretty.block
wenzelm@4867
   284
        [Pretty.str (ext_const c), Pretty.str " ::", Pretty.brk 1, Pretty.quote (prt_typ T)];
wenzelm@4867
   285
wenzelm@4895
   286
      fun pretty_record (name, {args, parent, fields, simps = _}) = Pretty.block (Pretty.fbreaks
wenzelm@4867
   287
        (Pretty.block [prt_typ (Type (name, map TFree args)), Pretty.str " = "] ::
wenzelm@4867
   288
          pretty_parent parent @ map pretty_field fields));
wenzelm@4867
   289
    in
wenzelm@4867
   290
      seq (Pretty.writeln o pretty_record) (Symtab.dest tab)
wenzelm@4867
   291
    end;
wenzelm@4867
   292
in
wenzelm@4867
   293
  val record_thy_data = (recordsK, (empty, prep_ext, merge, print));
wenzelm@4867
   294
end;
wenzelm@4867
   295
wenzelm@4867
   296
wenzelm@4867
   297
(* get and put records *)
wenzelm@4867
   298
wenzelm@4867
   299
fun get_records thy =
wenzelm@4867
   300
  (case Theory.get_data thy recordsK of
wenzelm@4867
   301
    Records tab => tab
wenzelm@4867
   302
  | _ => type_error recordsK);
wenzelm@4867
   303
wenzelm@4867
   304
fun get_record thy name = Symtab.lookup (get_records thy, name);
wenzelm@4867
   305
wenzelm@4867
   306
fun put_records tab thy =
wenzelm@4867
   307
  Theory.put_data (recordsK, Records tab) thy;
wenzelm@4867
   308
wenzelm@4890
   309
fun put_record name info thy =
wenzelm@4895
   310
  put_records (Symtab.update ((name, info), get_records thy)) thy;
wenzelm@4867
   311
wenzelm@4867
   312
wenzelm@4867
   313
(* parent records *)
wenzelm@4867
   314
wenzelm@4867
   315
fun inst_record thy (types, name) =
wenzelm@4867
   316
  let
wenzelm@4867
   317
    val sign = Theory.sign_of thy;
wenzelm@4867
   318
    fun err msg = error (msg ^ " parent record " ^ quote name);
wenzelm@4867
   319
wenzelm@4895
   320
    val {args, parent, fields, simps} =
wenzelm@4867
   321
      (case get_record thy name of Some info => info | None => err "Unknown");
wenzelm@4895
   322
    val _ = if length types <> length args then err "Bad number of arguments for" else ();
wenzelm@4867
   323
wenzelm@4867
   324
    fun bad_inst ((x, S), T) =
wenzelm@4867
   325
      if Sign.of_sort sign (T, S) then None else Some x
wenzelm@4867
   326
    val bads = mapfilter bad_inst (args ~~ types);
wenzelm@4867
   327
wenzelm@4867
   328
    val inst = map fst args ~~ types;
wenzelm@4867
   329
    val subst = Term.map_type_tfree (fn (x, _) => the (assoc (inst, x)));
wenzelm@4867
   330
  in
wenzelm@4895
   331
    if not (null bads) then
wenzelm@4867
   332
      err ("Ill-sorted instantiation of " ^ commas bads ^ " in")
wenzelm@4895
   333
    else (apsome (apfst (map subst)) parent, map (apsnd subst) fields, simps)
wenzelm@4867
   334
  end;
wenzelm@4867
   335
wenzelm@4867
   336
fun add_parents thy (None, parents) = parents
wenzelm@4867
   337
  | add_parents thy (Some (types, name), parents) =
wenzelm@4895
   338
      let val (pparent, pfields, psimps) = inst_record thy (types, name)
wenzelm@4895
   339
      in add_parents thy (pparent, {name = name, fields = pfields, simps = psimps} :: parents) end;
wenzelm@4867
   340
wenzelm@4867
   341
wenzelm@4867
   342
wenzelm@4890
   343
(** internal theory extenders **)
wenzelm@4867
   344
wenzelm@4894
   345
(* field_definitions *)
wenzelm@4867
   346
wenzelm@4894
   347
(*theorems from Prod.thy*)
wenzelm@4890
   348
val prod_convs = map Attribute.tthm_of [fst_conv, snd_conv];
wenzelm@4890
   349
wenzelm@4890
   350
wenzelm@4894
   351
fun field_definitions fields names zeta moreT more vars named_vars thy =
wenzelm@4890
   352
  let
wenzelm@4890
   353
    val base = Sign.base_name;
wenzelm@4867
   354
wenzelm@4867
   355
wenzelm@4890
   356
    (* prepare declarations and definitions *)
wenzelm@4867
   357
wenzelm@4890
   358
    (*field types*)
wenzelm@4890
   359
    fun mk_fieldT_spec c =
wenzelm@4890
   360
      (suffix field_typeN c, ["'a", zeta],
wenzelm@4890
   361
        HOLogic.mk_prodT (TFree ("'a", HOLogic.termS), moreT), Syntax.NoSyn);
wenzelm@4890
   362
    val fieldT_specs = map (mk_fieldT_spec o base) names;
wenzelm@4890
   363
wenzelm@4890
   364
    (*field declarations*)
wenzelm@4890
   365
    val field_decls = map (mk_fieldC moreT) fields;
wenzelm@4890
   366
    val dest_decls = map (mk_fstC moreT) fields @ map (mk_sndC moreT) fields;
wenzelm@4890
   367
wenzelm@4890
   368
    (*field constructors*)
wenzelm@4890
   369
    fun mk_field_spec (c, v) =
wenzelm@4894
   370
      mk_field ((c, v), more) :== HOLogic.mk_prod (v, more);
wenzelm@4894
   371
    val field_specs = map mk_field_spec named_vars;
wenzelm@4890
   372
wenzelm@4890
   373
    (*field destructors*)
wenzelm@4890
   374
    fun mk_dest_spec dest dest' (c, T) =
wenzelm@4890
   375
      let
wenzelm@4895
   376
        val p = Free ("p", mk_fieldT ((c, T), moreT));
wenzelm@4895
   377
        val p' = Free ("p", HOLogic.mk_prodT (T, moreT));
wenzelm@4894
   378
          (*note: field types are just abbreviations*)
wenzelm@4894
   379
      in dest p :== dest' p' end;
wenzelm@4890
   380
    val dest_specs =
wenzelm@4890
   381
      map (mk_dest_spec mk_fst HOLogic.mk_fst) fields @
wenzelm@4890
   382
      map (mk_dest_spec mk_snd HOLogic.mk_snd) fields;
wenzelm@4890
   383
wenzelm@4890
   384
wenzelm@4890
   385
    (* prepare theorems *)
wenzelm@4894
   386
wenzelm@4890
   387
    fun mk_dest_prop dest dest' (c, v) =
wenzelm@4894
   388
      dest (mk_field ((c, v), more)) === dest' (v, more);
wenzelm@4890
   389
    val dest_props =
wenzelm@4895
   390
      map (mk_dest_prop mk_fst fst) named_vars @
wenzelm@4895
   391
      map (mk_dest_prop mk_snd snd) named_vars;
wenzelm@4890
   392
wenzelm@4890
   393
wenzelm@4890
   394
    (* 1st stage: defs_thy *)
wenzelm@4890
   395
wenzelm@4890
   396
    val defs_thy =
wenzelm@4890
   397
      thy
wenzelm@4890
   398
      |> Theory.add_tyabbrs_i fieldT_specs
wenzelm@4890
   399
      |> (Theory.add_consts_i o map (Syntax.no_syn o apfst base))
wenzelm@4890
   400
        (field_decls @ dest_decls)
wenzelm@4890
   401
      |> (PureThy.add_defs_i o map Attribute.none)
wenzelm@4890
   402
        (field_specs @ dest_specs);
wenzelm@4890
   403
wenzelm@4890
   404
    val field_defs = get_defs defs_thy field_specs;
wenzelm@4890
   405
    val dest_defs = get_defs defs_thy dest_specs;
wenzelm@4890
   406
wenzelm@4890
   407
wenzelm@4890
   408
    (* 2nd stage: thms_thy *)
wenzelm@4890
   409
wenzelm@4894
   410
    val dest_convs =
wenzelm@4895
   411
      map (prove_simp defs_thy (field_defs @ dest_defs @ prod_convs)) dest_props;
wenzelm@4894
   412
wenzelm@4890
   413
    val thms_thy =
wenzelm@4890
   414
      defs_thy
wenzelm@4890
   415
      |> (PureThy.add_tthmss o map Attribute.none)
wenzelm@4890
   416
        [("field_defs", field_defs),
wenzelm@4890
   417
          ("dest_defs", dest_defs),
wenzelm@4890
   418
          ("dest_convs", dest_convs)];
wenzelm@4890
   419
wenzelm@4890
   420
  in (thms_thy, dest_convs) end;
wenzelm@4890
   421
wenzelm@4890
   422
wenzelm@4890
   423
(* record_definition *)
wenzelm@4867
   424
wenzelm@4867
   425
fun record_definition (args, bname) parent (parents: parent_info list) bfields thy =
wenzelm@4867
   426
  let
wenzelm@4867
   427
    val sign = Theory.sign_of thy;
wenzelm@4867
   428
    val full = Sign.full_name_path sign bname;
wenzelm@4890
   429
    val base = Sign.base_name;
wenzelm@4867
   430
wenzelm@4867
   431
wenzelm@4890
   432
    (* basic components *)
wenzelm@4867
   433
wenzelm@4867
   434
    val alphas = map fst args;
wenzelm@4890
   435
    val name = Sign.full_name sign bname;	(*not made part of record name space!*)
wenzelm@4890
   436
wenzelm@4867
   437
    val parent_fields = flat (map #fields parents);
wenzelm@4890
   438
    val parent_names = map fst parent_fields;
wenzelm@4890
   439
    val parent_types = map snd parent_fields;
wenzelm@4890
   440
    val parent_len = length parent_fields;
wenzelm@4890
   441
    val parent_xs = variantlist (map (base o fst) parent_fields, [moreN]);
wenzelm@4890
   442
    val parent_vars = ListPair.map Free (parent_xs, parent_types);
wenzelm@4894
   443
    val parent_named_vars = parent_names ~~ parent_vars;
wenzelm@4890
   444
wenzelm@4867
   445
    val fields = map (apfst full) bfields;
wenzelm@4890
   446
    val names = map fst fields;
wenzelm@4890
   447
    val types = map snd fields;
wenzelm@4890
   448
    val len = length fields;
wenzelm@4890
   449
    val xs = variantlist (map fst bfields, moreN :: parent_xs);
wenzelm@4890
   450
    val vars = ListPair.map Free (xs, types);
wenzelm@4894
   451
    val named_vars = names ~~ vars;
wenzelm@4867
   452
wenzelm@4867
   453
    val all_fields = parent_fields @ fields;
wenzelm@4890
   454
    val all_names = parent_names @ names;
wenzelm@4890
   455
    val all_types = parent_types @ types;
wenzelm@4890
   456
    val all_len = parent_len + len;
wenzelm@4890
   457
    val all_xs = parent_xs @ xs;
wenzelm@4890
   458
    val all_vars = parent_vars @ vars;
wenzelm@4894
   459
    val all_named_vars = parent_named_vars @ named_vars;
wenzelm@4867
   460
wenzelm@4867
   461
    val zeta = variant alphas "'z";
wenzelm@4867
   462
    val moreT = TFree (zeta, moreS);
wenzelm@4895
   463
    val more = Free (moreN, moreT);
wenzelm@4894
   464
    fun more_part t = mk_more t (full moreN);
wenzelm@4894
   465
wenzelm@4894
   466
    val parent_more = funpow parent_len mk_snd;
wenzelm@4894
   467
    val idxs = 0 upto (len - 1);
wenzelm@4867
   468
wenzelm@4867
   469
    val rec_schemeT = mk_recordT (all_fields, moreT);
wenzelm@4894
   470
    val rec_scheme = mk_record (all_named_vars, more);
wenzelm@4890
   471
    val r = Free ("r", rec_schemeT);
wenzelm@4894
   472
    val recT = mk_recordT (all_fields, HOLogic.unitT);
wenzelm@4867
   473
wenzelm@4867
   474
wenzelm@4890
   475
    (* prepare print translation functions *)
wenzelm@4867
   476
wenzelm@4890
   477
    val field_tr'_names =
wenzelm@4890
   478
      distinct (flat (map (NameSpace.accesses o suffix fieldN) names)) \\
wenzelm@4890
   479
        #3 (Syntax.trfun_names (Theory.syn_of thy));
wenzelm@4890
   480
    val field_trfuns = ([], [], field_tr'_names ~~ map field_tr' field_tr'_names, []);
wenzelm@4867
   481
wenzelm@4867
   482
wenzelm@4867
   483
    (* prepare declarations *)
wenzelm@4867
   484
wenzelm@4894
   485
    val sel_decls = map (mk_selC rec_schemeT) bfields @ [mk_moreC rec_schemeT (moreN, moreT)];
wenzelm@4894
   486
    val update_decls = map (mk_updateC rec_schemeT) bfields;
wenzelm@4890
   487
    val make_decls =
wenzelm@4890
   488
      [(mk_makeC rec_schemeT (make_schemeN, all_types @ [moreT])),
wenzelm@4890
   489
       (mk_makeC recT (makeN, all_types))];
wenzelm@4867
   490
wenzelm@4867
   491
wenzelm@4867
   492
    (* prepare definitions *)
wenzelm@4867
   493
wenzelm@4895
   494
    (*record (scheme) type abbreviation*)
wenzelm@4890
   495
    val recordT_specs =
wenzelm@4890
   496
      [(suffix schemeN bname, alphas @ [zeta], rec_schemeT, Syntax.NoSyn),
wenzelm@4890
   497
        (bname, alphas, recT, Syntax.NoSyn)];
wenzelm@4867
   498
wenzelm@4894
   499
    (*selectors*)
wenzelm@4890
   500
    fun mk_sel_spec (i, c) =
wenzelm@4894
   501
      mk_sel r c :== mk_fst (funpow i mk_snd (parent_more r));
wenzelm@4894
   502
    val sel_specs =
wenzelm@4894
   503
      ListPair.map mk_sel_spec (idxs, names) @
wenzelm@4894
   504
        [more_part r :== funpow len mk_snd (parent_more r)];
wenzelm@4890
   505
wenzelm@4890
   506
    (*updates*)
wenzelm@4894
   507
    val all_sels = all_names ~~ map (mk_sel r) all_names;
wenzelm@4890
   508
    fun mk_upd_spec (i, (c, x)) =
wenzelm@4894
   509
      mk_update r (c, x) :==
wenzelm@4894
   510
        mk_record (nth_update (c, x) (parent_len + i, all_sels), more_part r)
wenzelm@4894
   511
    val update_specs = ListPair.map mk_upd_spec (idxs, named_vars);
wenzelm@4867
   512
wenzelm@4867
   513
    (*makes*)
wenzelm@4890
   514
    val make_scheme = Const (mk_makeC rec_schemeT (full make_schemeN, all_types @ [moreT]));
wenzelm@4890
   515
    val make = Const (mk_makeC recT (full makeN, all_types));
wenzelm@4867
   516
    val make_specs =
wenzelm@4894
   517
      [list_comb (make_scheme, all_vars) $ more :== rec_scheme,
wenzelm@4894
   518
        list_comb (make, all_vars) :== mk_record (all_named_vars, HOLogic.unit)];
wenzelm@4894
   519
wenzelm@4894
   520
wenzelm@4894
   521
    (* prepare propositions *)
wenzelm@4894
   522
wenzelm@4894
   523
    (*selectors*)
wenzelm@4894
   524
    val sel_props =
wenzelm@4894
   525
      map (fn (c, x) => mk_sel rec_scheme c === x) named_vars @
wenzelm@4894
   526
        [more_part rec_scheme === more];
wenzelm@4894
   527
wenzelm@4894
   528
    (*updates*)
wenzelm@4894
   529
    fun mk_upd_prop (i, (c, T)) =
wenzelm@4894
   530
      let val x' = Free (variant all_xs (base c ^ "'"), T) in
wenzelm@4894
   531
        mk_update rec_scheme (c, x') ===
wenzelm@4894
   532
          mk_record (nth_update (c, x') (parent_len + i, all_named_vars), more)
wenzelm@4894
   533
      end;
wenzelm@4894
   534
    val update_props = ListPair.map mk_upd_prop (idxs, fields);
wenzelm@4867
   535
wenzelm@4867
   536
wenzelm@4890
   537
    (* 1st stage: fields_thy *)
wenzelm@4867
   538
wenzelm@4890
   539
    val (fields_thy, field_simps) =
wenzelm@4867
   540
      thy
wenzelm@4867
   541
      |> Theory.add_path bname
wenzelm@4894
   542
      |> field_definitions fields names zeta moreT more vars named_vars;
wenzelm@4867
   543
wenzelm@4867
   544
wenzelm@4890
   545
    (* 2nd stage: defs_thy *)
wenzelm@4890
   546
wenzelm@4890
   547
    val defs_thy =
wenzelm@4890
   548
      fields_thy
wenzelm@4890
   549
      |> Theory.parent_path
wenzelm@4890
   550
      |> Theory.add_tyabbrs_i recordT_specs	(*not made part of record name space!*)
wenzelm@4890
   551
      |> Theory.add_path bname
wenzelm@4890
   552
      |> Theory.add_trfuns field_trfuns
wenzelm@4894
   553
      |> (Theory.add_consts_i o map Syntax.no_syn)
wenzelm@4894
   554
        (sel_decls @ update_decls @ make_decls)
wenzelm@4890
   555
      |> (PureThy.add_defs_i o map Attribute.none)
wenzelm@4894
   556
        (sel_specs @ update_specs @ make_specs);
wenzelm@4890
   557
wenzelm@4890
   558
    val sel_defs = get_defs defs_thy sel_specs;
wenzelm@4890
   559
    val update_defs = get_defs defs_thy update_specs;
wenzelm@4890
   560
    val make_defs = get_defs defs_thy make_specs;
wenzelm@4890
   561
wenzelm@4890
   562
wenzelm@4890
   563
    (* 3rd stage: thms_thy *)
wenzelm@4890
   564
wenzelm@4895
   565
    val parent_simps = flat (map #simps parents);
wenzelm@4895
   566
    val prove = prove_simp defs_thy;
wenzelm@4890
   567
wenzelm@4895
   568
    val sel_convs = map (prove (parent_simps @ sel_defs @ field_simps)) sel_props;
wenzelm@4895
   569
    val update_convs = map (prove (parent_simps @ update_defs @ sel_convs)) update_props;
wenzelm@4894
   570
wenzelm@4894
   571
    val simps = field_simps @ sel_convs @ update_convs @ make_defs;
wenzelm@4867
   572
wenzelm@4867
   573
    val thms_thy =
wenzelm@4867
   574
      defs_thy
wenzelm@4867
   575
      |> (PureThy.add_tthmss o map Attribute.none)
wenzelm@4895
   576
        [("select_defs", sel_defs),
wenzelm@4890
   577
          ("update_defs", update_defs),
wenzelm@4894
   578
          ("make_defs", make_defs),
wenzelm@4895
   579
          ("select_convs", sel_convs),
wenzelm@4894
   580
          ("update_convs", update_convs)]
wenzelm@4894
   581
      |> PureThy.add_tthmss [(("simps", simps), [Simplifier.simp_add_global])];
wenzelm@4867
   582
wenzelm@4867
   583
wenzelm@4890
   584
    (* 4th stage: final_thy *)
wenzelm@4867
   585
wenzelm@4867
   586
    val final_thy =
wenzelm@4867
   587
      thms_thy
wenzelm@4895
   588
      |> put_record name {args = args, parent = parent, fields = fields, simps = simps}
wenzelm@4867
   589
      |> Theory.parent_path;
wenzelm@4867
   590
wenzelm@4867
   591
  in final_thy end;
wenzelm@4867
   592
wenzelm@4867
   593
wenzelm@4867
   594
wenzelm@4867
   595
(** theory extender interface **)
wenzelm@4867
   596
wenzelm@4867
   597
(* prepare arguments *)
wenzelm@4867
   598
wenzelm@4894
   599
(*note: read_raw_typ avoids expanding type abbreviations*)
wenzelm@4867
   600
fun read_raw_parent sign s =
wenzelm@4867
   601
  (case Sign.read_raw_typ (sign, K None) s handle TYPE (msg, _, _) => error msg of
wenzelm@4867
   602
    Type (name, Ts) => (Ts, name)
wenzelm@4867
   603
  | _ => error ("Bad parent record specification: " ^ quote s));
wenzelm@4867
   604
wenzelm@4867
   605
fun read_typ sign (env, s) =
wenzelm@4867
   606
  let
wenzelm@4867
   607
    fun def_type (x, ~1) = assoc (env, x)
wenzelm@4867
   608
      | def_type _ = None;
wenzelm@4867
   609
    val T = Type.no_tvars (Sign.read_typ (sign, def_type) s) handle TYPE (msg, _, _) => error msg;
wenzelm@4867
   610
  in (Term.add_typ_tfrees (T, env), T) end;
wenzelm@4867
   611
wenzelm@4867
   612
fun cert_typ sign (env, raw_T) =
wenzelm@4867
   613
  let val T = Type.no_tvars (Sign.certify_typ sign raw_T) handle TYPE (msg, _, _) => error msg
wenzelm@4867
   614
  in (Term.add_typ_tfrees (T, env), T) end;
wenzelm@4867
   615
wenzelm@4867
   616
wenzelm@4867
   617
(* add_record *)
wenzelm@4867
   618
wenzelm@4895
   619
(*we do all preparations and error checks here, deferring the real
wenzelm@4895
   620
  work to record_definition*)
wenzelm@4890
   621
wenzelm@4867
   622
fun gen_add_record prep_typ prep_raw_parent (params, bname) raw_parent raw_fields thy =
wenzelm@4867
   623
  let
wenzelm@4867
   624
    val _ = Theory.require thy "Record" "record definitions";
wenzelm@4867
   625
    val sign = Theory.sign_of thy;
wenzelm@4895
   626
    val _ = writeln ("Defining record " ^ quote bname ^ " ...");
wenzelm@4867
   627
wenzelm@4867
   628
wenzelm@4867
   629
    (* parents *)
wenzelm@4867
   630
wenzelm@4867
   631
    fun prep_inst T = snd (cert_typ sign ([], T));
wenzelm@4867
   632
wenzelm@4867
   633
    val parent = apsome (apfst (map prep_inst) o prep_raw_parent sign) raw_parent
wenzelm@4867
   634
      handle ERROR => error ("The error(s) above in parent record specification");
wenzelm@4867
   635
    val parents = add_parents thy (parent, []);
wenzelm@4867
   636
wenzelm@4867
   637
    val init_env =
wenzelm@4867
   638
      (case parent of
wenzelm@4867
   639
        None => []
wenzelm@4867
   640
      | Some (types, _) => foldr Term.add_typ_tfrees (types, []));
wenzelm@4867
   641
wenzelm@4867
   642
wenzelm@4867
   643
    (* fields *)
wenzelm@4867
   644
wenzelm@4867
   645
    fun prep_fields (env, []) = (env, [])
wenzelm@4867
   646
      | prep_fields (env, (c, raw_T) :: fs) =
wenzelm@4867
   647
          let
wenzelm@4867
   648
            val (env', T) = prep_typ sign (env, raw_T) handle ERROR =>
wenzelm@4867
   649
              error ("The error(s) above occured in field " ^ quote c);
wenzelm@4867
   650
            val (env'', fs') = prep_fields (env', fs);
wenzelm@4867
   651
      in (env'', (c, T) :: fs') end;
wenzelm@4867
   652
wenzelm@4867
   653
    val (envir, bfields) = prep_fields (init_env, raw_fields);
wenzelm@4867
   654
    val envir_names = map fst envir;
wenzelm@4867
   655
wenzelm@4867
   656
wenzelm@4867
   657
    (* args *)
wenzelm@4867
   658
wenzelm@4867
   659
    val defaultS = Sign.defaultS sign;
wenzelm@4867
   660
    val args = map (fn x => (x, if_none (assoc (envir, x)) defaultS)) params;
wenzelm@4867
   661
wenzelm@4867
   662
wenzelm@4867
   663
    (* errors *)
wenzelm@4867
   664
wenzelm@4890
   665
    val name = Sign.full_name sign bname;
wenzelm@4890
   666
    val err_dup_record =
wenzelm@4890
   667
      if is_none (get_record thy name) then []
wenzelm@4890
   668
      else ["Duplicate definition of record " ^ quote name];
wenzelm@4890
   669
wenzelm@4867
   670
    val err_dup_parms =
wenzelm@4867
   671
      (case duplicates params of
wenzelm@4867
   672
        [] => []
wenzelm@4890
   673
      | dups => ["Duplicate parameter(s) " ^ commas dups]);
wenzelm@4867
   674
wenzelm@4867
   675
    val err_extra_frees =
wenzelm@4867
   676
      (case gen_rems (op =) (envir_names, params) of
wenzelm@4867
   677
        [] => []
wenzelm@4890
   678
      | extras => ["Extra free type variable(s) " ^ commas extras]);
wenzelm@4867
   679
wenzelm@4890
   680
    val err_no_fields = if null bfields then ["No fields present"] else [];
wenzelm@4867
   681
wenzelm@4867
   682
    val err_dup_fields =
wenzelm@4867
   683
      (case duplicates (map fst bfields) of
wenzelm@4867
   684
        [] => []
wenzelm@4890
   685
      | dups => ["Duplicate field(s) " ^ commas_quote dups]);
wenzelm@4890
   686
wenzelm@4890
   687
    val err_bad_fields =
wenzelm@4890
   688
      if forall (not_equal moreN o fst) bfields then []
wenzelm@4890
   689
      else ["Illegal field name " ^ quote moreN];
wenzelm@4867
   690
wenzelm@4867
   691
    val err_dup_sorts =
wenzelm@4867
   692
      (case duplicates envir_names of
wenzelm@4867
   693
        [] => []
wenzelm@4867
   694
      | dups => ["Inconsistent sort constraints for " ^ commas dups]);
wenzelm@4867
   695
wenzelm@4867
   696
    val errs =
wenzelm@4890
   697
      err_dup_record @ err_dup_parms @ err_extra_frees @ err_no_fields @
wenzelm@4890
   698
      err_dup_fields @ err_bad_fields @ err_dup_sorts;
wenzelm@4867
   699
  in
wenzelm@4890
   700
    if null errs then () else error (cat_lines errs);
wenzelm@4867
   701
    thy |> record_definition (args, bname) parent parents bfields
wenzelm@4867
   702
  end
wenzelm@4867
   703
  handle ERROR => error ("Failed to define record " ^ quote bname);
wenzelm@4867
   704
wenzelm@4867
   705
val add_record = gen_add_record read_typ read_raw_parent;
wenzelm@4867
   706
val add_record_i = gen_add_record cert_typ (K I);
wenzelm@4867
   707
wenzelm@4867
   708
wenzelm@4867
   709
wenzelm@4867
   710
(** setup theory **)
wenzelm@4867
   711
wenzelm@4867
   712
val setup =
wenzelm@4867
   713
 [Theory.init_data [record_thy_data],
wenzelm@4867
   714
  Theory.add_trfuns
wenzelm@4867
   715
    ([], [("_record", record_tr), ("_record_scheme", record_scheme_tr)], [], [])];
wenzelm@4867
   716
wenzelm@4867
   717
wenzelm@4867
   718
end;