src/HOL/Tools/record_package.ML
author wenzelm
Fri Jun 05 14:33:18 1998 +0200 (1998-06-05)
changeset 5001 9de7fda0a6df
parent 4970 8b65444edbb0
child 5006 cdc86a914e63
permissions -rw-r--r--
accomodate tuned version of theory data;
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@5001
   259
(* data kind 'records' *)
wenzelm@4867
   260
wenzelm@4867
   261
local
wenzelm@5001
   262
  val recordsK = Object.kind "HOL/records";
wenzelm@5001
   263
  exception Records of record_info Symtab.table;
wenzelm@5001
   264
wenzelm@5001
   265
wenzelm@4867
   266
  val empty = Records Symtab.empty;
wenzelm@4867
   267
wenzelm@4867
   268
  fun prep_ext (x as Records _) = x;
wenzelm@4867
   269
wenzelm@4867
   270
  fun merge (Records tab1, Records tab2) =
wenzelm@4867
   271
    Records (Symtab.merge (K true) (tab1, tab2));
wenzelm@4867
   272
wenzelm@4867
   273
  fun print sg (Records tab) =
wenzelm@4867
   274
    let
wenzelm@4867
   275
      val prt_typ = Sign.pretty_typ sg;
wenzelm@4867
   276
      val ext_const = Sign.cond_extern sg Sign.constK;
wenzelm@4867
   277
wenzelm@4867
   278
      fun pretty_parent None = []
wenzelm@4867
   279
        | pretty_parent (Some (Ts, name)) =
wenzelm@4867
   280
            [Pretty.block [prt_typ (Type (name, Ts)), Pretty.str " +"]];
wenzelm@4867
   281
wenzelm@4867
   282
      fun pretty_field (c, T) = Pretty.block
wenzelm@4867
   283
        [Pretty.str (ext_const c), Pretty.str " ::", Pretty.brk 1, Pretty.quote (prt_typ T)];
wenzelm@4867
   284
wenzelm@4895
   285
      fun pretty_record (name, {args, parent, fields, simps = _}) = Pretty.block (Pretty.fbreaks
wenzelm@4867
   286
        (Pretty.block [prt_typ (Type (name, map TFree args)), Pretty.str " = "] ::
wenzelm@4867
   287
          pretty_parent parent @ map pretty_field fields));
wenzelm@4867
   288
    in
wenzelm@4867
   289
      seq (Pretty.writeln o pretty_record) (Symtab.dest tab)
wenzelm@4867
   290
    end;
wenzelm@4867
   291
in
wenzelm@5001
   292
  val init_records = Theory.init_data recordsK (empty, prep_ext, merge, print);
wenzelm@5001
   293
  val print_records = Theory.print_data recordsK;
wenzelm@5001
   294
  val get_records = Theory.get_data recordsK (fn Records x => x);
wenzelm@5001
   295
  val put_records = Theory.put_data recordsK Records;
wenzelm@4867
   296
end;
wenzelm@4867
   297
wenzelm@4867
   298
wenzelm@4867
   299
(* get and put records *)
wenzelm@4867
   300
wenzelm@4867
   301
fun get_record thy name = Symtab.lookup (get_records thy, name);
wenzelm@4867
   302
wenzelm@4890
   303
fun put_record name info thy =
wenzelm@4895
   304
  put_records (Symtab.update ((name, info), get_records thy)) thy;
wenzelm@4867
   305
wenzelm@4867
   306
wenzelm@4867
   307
(* parent records *)
wenzelm@4867
   308
wenzelm@4867
   309
fun inst_record thy (types, name) =
wenzelm@4867
   310
  let
wenzelm@4867
   311
    val sign = Theory.sign_of thy;
wenzelm@4867
   312
    fun err msg = error (msg ^ " parent record " ^ quote name);
wenzelm@4867
   313
wenzelm@4895
   314
    val {args, parent, fields, simps} =
wenzelm@4867
   315
      (case get_record thy name of Some info => info | None => err "Unknown");
wenzelm@4895
   316
    val _ = if length types <> length args then err "Bad number of arguments for" else ();
wenzelm@4867
   317
wenzelm@4867
   318
    fun bad_inst ((x, S), T) =
wenzelm@4867
   319
      if Sign.of_sort sign (T, S) then None else Some x
wenzelm@4867
   320
    val bads = mapfilter bad_inst (args ~~ types);
wenzelm@4867
   321
wenzelm@4867
   322
    val inst = map fst args ~~ types;
wenzelm@4867
   323
    val subst = Term.map_type_tfree (fn (x, _) => the (assoc (inst, x)));
wenzelm@4867
   324
  in
wenzelm@4895
   325
    if not (null bads) then
wenzelm@4867
   326
      err ("Ill-sorted instantiation of " ^ commas bads ^ " in")
wenzelm@4895
   327
    else (apsome (apfst (map subst)) parent, map (apsnd subst) fields, simps)
wenzelm@4867
   328
  end;
wenzelm@4867
   329
wenzelm@4867
   330
fun add_parents thy (None, parents) = parents
wenzelm@4867
   331
  | add_parents thy (Some (types, name), parents) =
wenzelm@4895
   332
      let val (pparent, pfields, psimps) = inst_record thy (types, name)
wenzelm@4895
   333
      in add_parents thy (pparent, {name = name, fields = pfields, simps = psimps} :: parents) end;
wenzelm@4867
   334
wenzelm@4867
   335
wenzelm@4867
   336
wenzelm@4890
   337
(** internal theory extenders **)
wenzelm@4867
   338
wenzelm@4894
   339
(* field_definitions *)
wenzelm@4867
   340
wenzelm@4894
   341
(*theorems from Prod.thy*)
wenzelm@4890
   342
val prod_convs = map Attribute.tthm_of [fst_conv, snd_conv];
wenzelm@4890
   343
wenzelm@4890
   344
wenzelm@4894
   345
fun field_definitions fields names zeta moreT more vars named_vars thy =
wenzelm@4890
   346
  let
wenzelm@4890
   347
    val base = Sign.base_name;
wenzelm@4867
   348
wenzelm@4867
   349
wenzelm@4890
   350
    (* prepare declarations and definitions *)
wenzelm@4867
   351
wenzelm@4890
   352
    (*field types*)
wenzelm@4890
   353
    fun mk_fieldT_spec c =
wenzelm@4890
   354
      (suffix field_typeN c, ["'a", zeta],
wenzelm@4890
   355
        HOLogic.mk_prodT (TFree ("'a", HOLogic.termS), moreT), Syntax.NoSyn);
wenzelm@4890
   356
    val fieldT_specs = map (mk_fieldT_spec o base) names;
wenzelm@4890
   357
wenzelm@4890
   358
    (*field declarations*)
wenzelm@4890
   359
    val field_decls = map (mk_fieldC moreT) fields;
wenzelm@4890
   360
    val dest_decls = map (mk_fstC moreT) fields @ map (mk_sndC moreT) fields;
wenzelm@4890
   361
wenzelm@4890
   362
    (*field constructors*)
wenzelm@4890
   363
    fun mk_field_spec (c, v) =
wenzelm@4894
   364
      mk_field ((c, v), more) :== HOLogic.mk_prod (v, more);
wenzelm@4894
   365
    val field_specs = map mk_field_spec named_vars;
wenzelm@4890
   366
wenzelm@4890
   367
    (*field destructors*)
wenzelm@4890
   368
    fun mk_dest_spec dest dest' (c, T) =
wenzelm@4890
   369
      let
wenzelm@4895
   370
        val p = Free ("p", mk_fieldT ((c, T), moreT));
wenzelm@4895
   371
        val p' = Free ("p", HOLogic.mk_prodT (T, moreT));
wenzelm@4894
   372
          (*note: field types are just abbreviations*)
wenzelm@4894
   373
      in dest p :== dest' p' end;
wenzelm@4890
   374
    val dest_specs =
wenzelm@4890
   375
      map (mk_dest_spec mk_fst HOLogic.mk_fst) fields @
wenzelm@4890
   376
      map (mk_dest_spec mk_snd HOLogic.mk_snd) fields;
wenzelm@4890
   377
wenzelm@4890
   378
wenzelm@4890
   379
    (* prepare theorems *)
wenzelm@4894
   380
wenzelm@4890
   381
    fun mk_dest_prop dest dest' (c, v) =
wenzelm@4894
   382
      dest (mk_field ((c, v), more)) === dest' (v, more);
wenzelm@4890
   383
    val dest_props =
wenzelm@4895
   384
      map (mk_dest_prop mk_fst fst) named_vars @
wenzelm@4895
   385
      map (mk_dest_prop mk_snd snd) named_vars;
wenzelm@4890
   386
wenzelm@4890
   387
wenzelm@4890
   388
    (* 1st stage: defs_thy *)
wenzelm@4890
   389
wenzelm@4890
   390
    val defs_thy =
wenzelm@4890
   391
      thy
wenzelm@4890
   392
      |> Theory.add_tyabbrs_i fieldT_specs
wenzelm@4890
   393
      |> (Theory.add_consts_i o map (Syntax.no_syn o apfst base))
wenzelm@4890
   394
        (field_decls @ dest_decls)
wenzelm@4890
   395
      |> (PureThy.add_defs_i o map Attribute.none)
wenzelm@4890
   396
        (field_specs @ dest_specs);
wenzelm@4890
   397
wenzelm@4890
   398
    val field_defs = get_defs defs_thy field_specs;
wenzelm@4890
   399
    val dest_defs = get_defs defs_thy dest_specs;
wenzelm@4890
   400
wenzelm@4890
   401
wenzelm@4890
   402
    (* 2nd stage: thms_thy *)
wenzelm@4890
   403
wenzelm@4894
   404
    val dest_convs =
wenzelm@4895
   405
      map (prove_simp defs_thy (field_defs @ dest_defs @ prod_convs)) dest_props;
wenzelm@4894
   406
wenzelm@4890
   407
    val thms_thy =
wenzelm@4890
   408
      defs_thy
wenzelm@4890
   409
      |> (PureThy.add_tthmss o map Attribute.none)
wenzelm@4890
   410
        [("field_defs", field_defs),
wenzelm@4890
   411
          ("dest_defs", dest_defs),
wenzelm@4890
   412
          ("dest_convs", dest_convs)];
wenzelm@4890
   413
wenzelm@4890
   414
  in (thms_thy, dest_convs) end;
wenzelm@4890
   415
wenzelm@4890
   416
wenzelm@4890
   417
(* record_definition *)
wenzelm@4867
   418
wenzelm@4867
   419
fun record_definition (args, bname) parent (parents: parent_info list) bfields thy =
wenzelm@4867
   420
  let
wenzelm@4867
   421
    val sign = Theory.sign_of thy;
wenzelm@4867
   422
    val full = Sign.full_name_path sign bname;
wenzelm@4890
   423
    val base = Sign.base_name;
wenzelm@4867
   424
wenzelm@4867
   425
wenzelm@4890
   426
    (* basic components *)
wenzelm@4867
   427
wenzelm@4867
   428
    val alphas = map fst args;
wenzelm@4890
   429
    val name = Sign.full_name sign bname;	(*not made part of record name space!*)
wenzelm@4890
   430
wenzelm@4867
   431
    val parent_fields = flat (map #fields parents);
wenzelm@4890
   432
    val parent_names = map fst parent_fields;
wenzelm@4890
   433
    val parent_types = map snd parent_fields;
wenzelm@4890
   434
    val parent_len = length parent_fields;
wenzelm@4890
   435
    val parent_xs = variantlist (map (base o fst) parent_fields, [moreN]);
wenzelm@4890
   436
    val parent_vars = ListPair.map Free (parent_xs, parent_types);
wenzelm@4894
   437
    val parent_named_vars = parent_names ~~ parent_vars;
wenzelm@4890
   438
wenzelm@4867
   439
    val fields = map (apfst full) bfields;
wenzelm@4890
   440
    val names = map fst fields;
wenzelm@4890
   441
    val types = map snd fields;
wenzelm@4890
   442
    val len = length fields;
wenzelm@4890
   443
    val xs = variantlist (map fst bfields, moreN :: parent_xs);
wenzelm@4890
   444
    val vars = ListPair.map Free (xs, types);
wenzelm@4894
   445
    val named_vars = names ~~ vars;
wenzelm@4867
   446
wenzelm@4867
   447
    val all_fields = parent_fields @ fields;
wenzelm@4890
   448
    val all_names = parent_names @ names;
wenzelm@4890
   449
    val all_types = parent_types @ types;
wenzelm@4890
   450
    val all_len = parent_len + len;
wenzelm@4890
   451
    val all_xs = parent_xs @ xs;
wenzelm@4890
   452
    val all_vars = parent_vars @ vars;
wenzelm@4894
   453
    val all_named_vars = parent_named_vars @ named_vars;
wenzelm@4867
   454
wenzelm@4867
   455
    val zeta = variant alphas "'z";
wenzelm@4867
   456
    val moreT = TFree (zeta, moreS);
wenzelm@4895
   457
    val more = Free (moreN, moreT);
wenzelm@4894
   458
    fun more_part t = mk_more t (full moreN);
wenzelm@4894
   459
wenzelm@4894
   460
    val parent_more = funpow parent_len mk_snd;
wenzelm@4894
   461
    val idxs = 0 upto (len - 1);
wenzelm@4867
   462
wenzelm@4867
   463
    val rec_schemeT = mk_recordT (all_fields, moreT);
wenzelm@4894
   464
    val rec_scheme = mk_record (all_named_vars, more);
wenzelm@4890
   465
    val r = Free ("r", rec_schemeT);
wenzelm@4894
   466
    val recT = mk_recordT (all_fields, HOLogic.unitT);
wenzelm@4867
   467
wenzelm@4867
   468
wenzelm@4890
   469
    (* prepare print translation functions *)
wenzelm@4867
   470
wenzelm@4890
   471
    val field_tr'_names =
wenzelm@4890
   472
      distinct (flat (map (NameSpace.accesses o suffix fieldN) names)) \\
wenzelm@4890
   473
        #3 (Syntax.trfun_names (Theory.syn_of thy));
wenzelm@4890
   474
    val field_trfuns = ([], [], field_tr'_names ~~ map field_tr' field_tr'_names, []);
wenzelm@4867
   475
wenzelm@4867
   476
wenzelm@4867
   477
    (* prepare declarations *)
wenzelm@4867
   478
wenzelm@4894
   479
    val sel_decls = map (mk_selC rec_schemeT) bfields @ [mk_moreC rec_schemeT (moreN, moreT)];
wenzelm@4894
   480
    val update_decls = map (mk_updateC rec_schemeT) bfields;
wenzelm@4890
   481
    val make_decls =
wenzelm@4890
   482
      [(mk_makeC rec_schemeT (make_schemeN, all_types @ [moreT])),
wenzelm@4890
   483
       (mk_makeC recT (makeN, all_types))];
wenzelm@4867
   484
wenzelm@4867
   485
wenzelm@4867
   486
    (* prepare definitions *)
wenzelm@4867
   487
wenzelm@4895
   488
    (*record (scheme) type abbreviation*)
wenzelm@4890
   489
    val recordT_specs =
wenzelm@4890
   490
      [(suffix schemeN bname, alphas @ [zeta], rec_schemeT, Syntax.NoSyn),
wenzelm@4890
   491
        (bname, alphas, recT, Syntax.NoSyn)];
wenzelm@4867
   492
wenzelm@4894
   493
    (*selectors*)
wenzelm@4890
   494
    fun mk_sel_spec (i, c) =
wenzelm@4894
   495
      mk_sel r c :== mk_fst (funpow i mk_snd (parent_more r));
wenzelm@4894
   496
    val sel_specs =
wenzelm@4894
   497
      ListPair.map mk_sel_spec (idxs, names) @
wenzelm@4894
   498
        [more_part r :== funpow len mk_snd (parent_more r)];
wenzelm@4890
   499
wenzelm@4890
   500
    (*updates*)
wenzelm@4894
   501
    val all_sels = all_names ~~ map (mk_sel r) all_names;
wenzelm@4890
   502
    fun mk_upd_spec (i, (c, x)) =
wenzelm@4894
   503
      mk_update r (c, x) :==
wenzelm@4894
   504
        mk_record (nth_update (c, x) (parent_len + i, all_sels), more_part r)
wenzelm@4894
   505
    val update_specs = ListPair.map mk_upd_spec (idxs, named_vars);
wenzelm@4867
   506
wenzelm@4867
   507
    (*makes*)
wenzelm@4890
   508
    val make_scheme = Const (mk_makeC rec_schemeT (full make_schemeN, all_types @ [moreT]));
wenzelm@4890
   509
    val make = Const (mk_makeC recT (full makeN, all_types));
wenzelm@4867
   510
    val make_specs =
wenzelm@4894
   511
      [list_comb (make_scheme, all_vars) $ more :== rec_scheme,
wenzelm@4894
   512
        list_comb (make, all_vars) :== mk_record (all_named_vars, HOLogic.unit)];
wenzelm@4894
   513
wenzelm@4894
   514
wenzelm@4894
   515
    (* prepare propositions *)
wenzelm@4894
   516
wenzelm@4894
   517
    (*selectors*)
wenzelm@4894
   518
    val sel_props =
wenzelm@4894
   519
      map (fn (c, x) => mk_sel rec_scheme c === x) named_vars @
wenzelm@4894
   520
        [more_part rec_scheme === more];
wenzelm@4894
   521
wenzelm@4894
   522
    (*updates*)
wenzelm@4894
   523
    fun mk_upd_prop (i, (c, T)) =
wenzelm@4894
   524
      let val x' = Free (variant all_xs (base c ^ "'"), T) in
wenzelm@4894
   525
        mk_update rec_scheme (c, x') ===
wenzelm@4894
   526
          mk_record (nth_update (c, x') (parent_len + i, all_named_vars), more)
wenzelm@4894
   527
      end;
wenzelm@4894
   528
    val update_props = ListPair.map mk_upd_prop (idxs, fields);
wenzelm@4867
   529
wenzelm@4867
   530
wenzelm@4890
   531
    (* 1st stage: fields_thy *)
wenzelm@4867
   532
wenzelm@4890
   533
    val (fields_thy, field_simps) =
wenzelm@4867
   534
      thy
wenzelm@4867
   535
      |> Theory.add_path bname
wenzelm@4894
   536
      |> field_definitions fields names zeta moreT more vars named_vars;
wenzelm@4867
   537
wenzelm@4867
   538
wenzelm@4890
   539
    (* 2nd stage: defs_thy *)
wenzelm@4890
   540
wenzelm@4890
   541
    val defs_thy =
wenzelm@4890
   542
      fields_thy
wenzelm@4890
   543
      |> Theory.parent_path
wenzelm@4890
   544
      |> Theory.add_tyabbrs_i recordT_specs	(*not made part of record name space!*)
wenzelm@4890
   545
      |> Theory.add_path bname
wenzelm@4890
   546
      |> Theory.add_trfuns field_trfuns
wenzelm@4894
   547
      |> (Theory.add_consts_i o map Syntax.no_syn)
wenzelm@4894
   548
        (sel_decls @ update_decls @ make_decls)
wenzelm@4890
   549
      |> (PureThy.add_defs_i o map Attribute.none)
wenzelm@4894
   550
        (sel_specs @ update_specs @ make_specs);
wenzelm@4890
   551
wenzelm@4890
   552
    val sel_defs = get_defs defs_thy sel_specs;
wenzelm@4890
   553
    val update_defs = get_defs defs_thy update_specs;
wenzelm@4890
   554
    val make_defs = get_defs defs_thy make_specs;
wenzelm@4890
   555
wenzelm@4890
   556
wenzelm@4890
   557
    (* 3rd stage: thms_thy *)
wenzelm@4890
   558
wenzelm@4895
   559
    val parent_simps = flat (map #simps parents);
wenzelm@4895
   560
    val prove = prove_simp defs_thy;
wenzelm@4890
   561
wenzelm@4895
   562
    val sel_convs = map (prove (parent_simps @ sel_defs @ field_simps)) sel_props;
wenzelm@4895
   563
    val update_convs = map (prove (parent_simps @ update_defs @ sel_convs)) update_props;
wenzelm@4894
   564
wenzelm@4894
   565
    val simps = field_simps @ sel_convs @ update_convs @ make_defs;
wenzelm@4867
   566
wenzelm@4867
   567
    val thms_thy =
wenzelm@4867
   568
      defs_thy
wenzelm@4867
   569
      |> (PureThy.add_tthmss o map Attribute.none)
wenzelm@4895
   570
        [("select_defs", sel_defs),
wenzelm@4890
   571
          ("update_defs", update_defs),
wenzelm@4894
   572
          ("make_defs", make_defs),
wenzelm@4895
   573
          ("select_convs", sel_convs),
wenzelm@4894
   574
          ("update_convs", update_convs)]
wenzelm@4894
   575
      |> PureThy.add_tthmss [(("simps", simps), [Simplifier.simp_add_global])];
wenzelm@4867
   576
wenzelm@4867
   577
wenzelm@4890
   578
    (* 4th stage: final_thy *)
wenzelm@4867
   579
wenzelm@4867
   580
    val final_thy =
wenzelm@4867
   581
      thms_thy
wenzelm@4895
   582
      |> put_record name {args = args, parent = parent, fields = fields, simps = simps}
wenzelm@4867
   583
      |> Theory.parent_path;
wenzelm@4867
   584
wenzelm@4867
   585
  in final_thy end;
wenzelm@4867
   586
wenzelm@4867
   587
wenzelm@4867
   588
wenzelm@4867
   589
(** theory extender interface **)
wenzelm@4867
   590
wenzelm@4867
   591
(* prepare arguments *)
wenzelm@4867
   592
wenzelm@4894
   593
(*note: read_raw_typ avoids expanding type abbreviations*)
wenzelm@4867
   594
fun read_raw_parent sign s =
wenzelm@4867
   595
  (case Sign.read_raw_typ (sign, K None) s handle TYPE (msg, _, _) => error msg of
wenzelm@4867
   596
    Type (name, Ts) => (Ts, name)
wenzelm@4867
   597
  | _ => error ("Bad parent record specification: " ^ quote s));
wenzelm@4867
   598
wenzelm@4867
   599
fun read_typ sign (env, s) =
wenzelm@4867
   600
  let
wenzelm@4867
   601
    fun def_type (x, ~1) = assoc (env, x)
wenzelm@4867
   602
      | def_type _ = None;
wenzelm@4867
   603
    val T = Type.no_tvars (Sign.read_typ (sign, def_type) s) handle TYPE (msg, _, _) => error msg;
wenzelm@4867
   604
  in (Term.add_typ_tfrees (T, env), T) end;
wenzelm@4867
   605
wenzelm@4867
   606
fun cert_typ sign (env, raw_T) =
wenzelm@4867
   607
  let val T = Type.no_tvars (Sign.certify_typ sign raw_T) handle TYPE (msg, _, _) => error msg
wenzelm@4867
   608
  in (Term.add_typ_tfrees (T, env), T) end;
wenzelm@4867
   609
wenzelm@4867
   610
wenzelm@4867
   611
(* add_record *)
wenzelm@4867
   612
wenzelm@4895
   613
(*we do all preparations and error checks here, deferring the real
wenzelm@4895
   614
  work to record_definition*)
wenzelm@4890
   615
wenzelm@4867
   616
fun gen_add_record prep_typ prep_raw_parent (params, bname) raw_parent raw_fields thy =
wenzelm@4867
   617
  let
paulson@4970
   618
    val _ = Theory.requires thy "Record" "record definitions";
wenzelm@4867
   619
    val sign = Theory.sign_of thy;
wenzelm@4895
   620
    val _ = writeln ("Defining record " ^ quote bname ^ " ...");
wenzelm@4867
   621
wenzelm@4867
   622
wenzelm@4867
   623
    (* parents *)
wenzelm@4867
   624
wenzelm@4867
   625
    fun prep_inst T = snd (cert_typ sign ([], T));
wenzelm@4867
   626
wenzelm@4867
   627
    val parent = apsome (apfst (map prep_inst) o prep_raw_parent sign) raw_parent
wenzelm@4867
   628
      handle ERROR => error ("The error(s) above in parent record specification");
wenzelm@4867
   629
    val parents = add_parents thy (parent, []);
wenzelm@4867
   630
wenzelm@4867
   631
    val init_env =
wenzelm@4867
   632
      (case parent of
wenzelm@4867
   633
        None => []
wenzelm@4867
   634
      | Some (types, _) => foldr Term.add_typ_tfrees (types, []));
wenzelm@4867
   635
wenzelm@4867
   636
wenzelm@4867
   637
    (* fields *)
wenzelm@4867
   638
wenzelm@4967
   639
    fun prep_field (env, (c, raw_T)) =
wenzelm@4967
   640
      let val (env', T) = prep_typ sign (env, raw_T) handle ERROR =>
wenzelm@4967
   641
        error ("The error(s) above occured in field " ^ quote c)
wenzelm@4967
   642
      in (env', (c, T)) end;
wenzelm@4867
   643
wenzelm@4967
   644
    val (envir, bfields) = foldl_map prep_field (init_env, raw_fields);
wenzelm@4867
   645
    val envir_names = map fst envir;
wenzelm@4867
   646
wenzelm@4867
   647
wenzelm@4867
   648
    (* args *)
wenzelm@4867
   649
wenzelm@4867
   650
    val defaultS = Sign.defaultS sign;
wenzelm@4867
   651
    val args = map (fn x => (x, if_none (assoc (envir, x)) defaultS)) params;
wenzelm@4867
   652
wenzelm@4867
   653
wenzelm@4867
   654
    (* errors *)
wenzelm@4867
   655
wenzelm@4890
   656
    val name = Sign.full_name sign bname;
wenzelm@4890
   657
    val err_dup_record =
wenzelm@4890
   658
      if is_none (get_record thy name) then []
wenzelm@4890
   659
      else ["Duplicate definition of record " ^ quote name];
wenzelm@4890
   660
wenzelm@4867
   661
    val err_dup_parms =
wenzelm@4867
   662
      (case duplicates params of
wenzelm@4867
   663
        [] => []
wenzelm@4890
   664
      | dups => ["Duplicate parameter(s) " ^ commas dups]);
wenzelm@4867
   665
wenzelm@4867
   666
    val err_extra_frees =
wenzelm@4867
   667
      (case gen_rems (op =) (envir_names, params) of
wenzelm@4867
   668
        [] => []
wenzelm@4890
   669
      | extras => ["Extra free type variable(s) " ^ commas extras]);
wenzelm@4867
   670
wenzelm@4890
   671
    val err_no_fields = if null bfields then ["No fields present"] else [];
wenzelm@4867
   672
wenzelm@4867
   673
    val err_dup_fields =
wenzelm@4867
   674
      (case duplicates (map fst bfields) of
wenzelm@4867
   675
        [] => []
wenzelm@4890
   676
      | dups => ["Duplicate field(s) " ^ commas_quote dups]);
wenzelm@4890
   677
wenzelm@4890
   678
    val err_bad_fields =
wenzelm@4890
   679
      if forall (not_equal moreN o fst) bfields then []
wenzelm@4890
   680
      else ["Illegal field name " ^ quote moreN];
wenzelm@4867
   681
wenzelm@4867
   682
    val err_dup_sorts =
wenzelm@4867
   683
      (case duplicates envir_names of
wenzelm@4867
   684
        [] => []
wenzelm@4867
   685
      | dups => ["Inconsistent sort constraints for " ^ commas dups]);
wenzelm@4867
   686
wenzelm@4867
   687
    val errs =
wenzelm@4890
   688
      err_dup_record @ err_dup_parms @ err_extra_frees @ err_no_fields @
wenzelm@4890
   689
      err_dup_fields @ err_bad_fields @ err_dup_sorts;
wenzelm@4867
   690
  in
wenzelm@4890
   691
    if null errs then () else error (cat_lines errs);
wenzelm@4867
   692
    thy |> record_definition (args, bname) parent parents bfields
wenzelm@4867
   693
  end
wenzelm@4867
   694
  handle ERROR => error ("Failed to define record " ^ quote bname);
wenzelm@4867
   695
wenzelm@4867
   696
val add_record = gen_add_record read_typ read_raw_parent;
wenzelm@4867
   697
val add_record_i = gen_add_record cert_typ (K I);
wenzelm@4867
   698
wenzelm@4867
   699
wenzelm@4867
   700
wenzelm@4867
   701
(** setup theory **)
wenzelm@4867
   702
wenzelm@4867
   703
val setup =
wenzelm@5001
   704
 [init_records,
wenzelm@4867
   705
  Theory.add_trfuns
wenzelm@4867
   706
    ([], [("_record", record_tr), ("_record_scheme", record_scheme_tr)], [], [])];
wenzelm@4867
   707
wenzelm@4867
   708
wenzelm@4867
   709
end;