src/HOL/Tools/record_package.ML
author wenzelm
Wed Apr 29 11:40:37 1998 +0200 (1998-04-29)
changeset 4867 9be2bf0ce909
child 4890 f0a24bad990a
permissions -rw-r--r--
package extensible records with structural subtyping in HOL -- still
experimental version;
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@4867
     8
  - record_info: tr' funs;
wenzelm@4867
     9
  - trfuns for record types;
wenzelm@4867
    10
  - field types: typedef;
wenzelm@4867
    11
  - make selector types as general as possible (no!?);
wenzelm@4867
    12
*)
wenzelm@4867
    13
wenzelm@4867
    14
signature RECORD_PACKAGE =
wenzelm@4867
    15
sig
wenzelm@4867
    16
  val print_records: theory -> unit
wenzelm@4867
    17
  val add_record: (string list * bstring) -> string option
wenzelm@4867
    18
    -> (bstring * string) list -> theory -> theory
wenzelm@4867
    19
  val add_record_i: (string list * bstring) -> (typ list * string) option
wenzelm@4867
    20
    -> (bstring * typ) list -> theory -> theory
wenzelm@4867
    21
  val setup: (theory -> theory) list
wenzelm@4867
    22
end;
wenzelm@4867
    23
wenzelm@4867
    24
structure RecordPackage: RECORD_PACKAGE =
wenzelm@4867
    25
struct
wenzelm@4867
    26
wenzelm@4867
    27
wenzelm@4867
    28
(*** syntax operations ***)
wenzelm@4867
    29
wenzelm@4867
    30
(** names **)
wenzelm@4867
    31
wenzelm@4867
    32
(* name components *)
wenzelm@4867
    33
wenzelm@4867
    34
val moreN = "more";
wenzelm@4867
    35
val schemeN = "_scheme";
wenzelm@4867
    36
val fieldN = "_field";
wenzelm@4867
    37
val field_typeN = "_field_type";
wenzelm@4867
    38
val fstN = "_val";
wenzelm@4867
    39
val sndN = "_more";
wenzelm@4867
    40
val updateN = "_update";
wenzelm@4867
    41
val makeN = "make";
wenzelm@4867
    42
val make_schemeN = "make_scheme";
wenzelm@4867
    43
wenzelm@4867
    44
wenzelm@4867
    45
(* suffixes *)
wenzelm@4867
    46
wenzelm@4867
    47
fun suffix sfx s = s ^ sfx;
wenzelm@4867
    48
wenzelm@4867
    49
fun unsuffix sfx s =
wenzelm@4867
    50
  let
wenzelm@4867
    51
    val cs = explode s;
wenzelm@4867
    52
    val prfx_len = size s - size sfx;
wenzelm@4867
    53
  in
wenzelm@4867
    54
    if prfx_len >= 0 andalso implode (drop (prfx_len, cs)) = sfx then
wenzelm@4867
    55
      implode (take (prfx_len, cs))
wenzelm@4867
    56
    else raise LIST "unsuffix"
wenzelm@4867
    57
  end;
wenzelm@4867
    58
wenzelm@4867
    59
wenzelm@4867
    60
wenzelm@4867
    61
(** tuple operations **)
wenzelm@4867
    62
wenzelm@4867
    63
(* more type class *)
wenzelm@4867
    64
wenzelm@4867
    65
val moreS = ["more"];
wenzelm@4867
    66
wenzelm@4867
    67
wenzelm@4867
    68
(* types *)
wenzelm@4867
    69
wenzelm@4867
    70
fun mk_fieldT ((c, T), U) = Type (suffix field_typeN c, [T, U]);
wenzelm@4867
    71
wenzelm@4867
    72
fun dest_fieldT (typ as Type (c_field_type, [T, U])) =
wenzelm@4867
    73
      (case try (unsuffix field_typeN) c_field_type of
wenzelm@4867
    74
        None => raise TYPE ("dest_fieldT", [typ], [])
wenzelm@4867
    75
      | Some c => ((c, T), U))
wenzelm@4867
    76
  | dest_fieldT typ = raise TYPE ("dest_fieldT", [typ], []);
wenzelm@4867
    77
wenzelm@4867
    78
wenzelm@4867
    79
(* constructors *)
wenzelm@4867
    80
wenzelm@4867
    81
fun mk_fieldC U (c, T) = (suffix fieldN c, T --> U --> mk_fieldT ((c, T), U));
wenzelm@4867
    82
wenzelm@4867
    83
fun mk_field ((c, t), u) =
wenzelm@4867
    84
  let val T = fastype_of t and U = fastype_of u
wenzelm@4867
    85
  in Const (suffix fieldN c, [T, U] ---> mk_fieldT ((c, T), U)) $ t $ u end;
wenzelm@4867
    86
wenzelm@4867
    87
wenzelm@4867
    88
(* destructors *)
wenzelm@4867
    89
wenzelm@4867
    90
fun mk_fstC U (c, T) = (suffix fstN c, mk_fieldT ((c, T), U) --> T);
wenzelm@4867
    91
fun mk_sndC U (c, T) = (suffix sndN c, mk_fieldT ((c, T), U) --> U);
wenzelm@4867
    92
wenzelm@4867
    93
fun dest_field fst_or_snd p =
wenzelm@4867
    94
  let
wenzelm@4867
    95
    val pT = fastype_of p;
wenzelm@4867
    96
    val ((c, T), U) = dest_fieldT pT;
wenzelm@4867
    97
    val (destN, destT) = if fst_or_snd then (fstN, T) else (sndN, U);
wenzelm@4867
    98
  in Const (suffix destN c, pT --> destT) $ p end;
wenzelm@4867
    99
wenzelm@4867
   100
val mk_fst = dest_field true;
wenzelm@4867
   101
val mk_snd = dest_field false;
wenzelm@4867
   102
wenzelm@4867
   103
wenzelm@4867
   104
wenzelm@4867
   105
(** record operations **)
wenzelm@4867
   106
wenzelm@4867
   107
(* types *)
wenzelm@4867
   108
wenzelm@4867
   109
val mk_recordT = foldr mk_fieldT;
wenzelm@4867
   110
wenzelm@4867
   111
fun dest_recordT T =
wenzelm@4867
   112
  (case try dest_fieldT T of
wenzelm@4867
   113
    None => ([], T)
wenzelm@4867
   114
  | Some (c_T, U) => apfst (cons c_T) (dest_recordT U));
wenzelm@4867
   115
wenzelm@4867
   116
fun find_fieldT c rT =
wenzelm@4867
   117
  (case assoc (fst (dest_recordT rT), c) of
wenzelm@4867
   118
    None => raise TYPE ("find_field: " ^ c, [rT], [])
wenzelm@4867
   119
  | Some T => T);
wenzelm@4867
   120
wenzelm@4867
   121
wenzelm@4867
   122
(* constructors *)
wenzelm@4867
   123
wenzelm@4867
   124
val mk_record = foldr mk_field;
wenzelm@4867
   125
wenzelm@4867
   126
wenzelm@4867
   127
(* selectors *)
wenzelm@4867
   128
wenzelm@4867
   129
fun mk_selC rT (c, T) = (c, rT --> T);
wenzelm@4867
   130
wenzelm@4867
   131
fun mk_sel c r =
wenzelm@4867
   132
  let val rT = fastype_of r
wenzelm@4867
   133
  in Const (mk_selC rT (c, find_fieldT c rT)) $ r end;
wenzelm@4867
   134
wenzelm@4867
   135
wenzelm@4867
   136
(* updates *)
wenzelm@4867
   137
wenzelm@4867
   138
fun mk_updateC rT (c, T) = (suffix updateN c, T --> rT --> rT);
wenzelm@4867
   139
wenzelm@4867
   140
fun mk_update c x r =
wenzelm@4867
   141
  let val rT = fastype_of r
wenzelm@4867
   142
  in Const (mk_updateC rT (c, find_fieldT c rT)) $ x $ r end;
wenzelm@4867
   143
wenzelm@4867
   144
wenzelm@4867
   145
wenzelm@4867
   146
(** concrete syntax for records **)
wenzelm@4867
   147
wenzelm@4867
   148
(* parse translations *)
wenzelm@4867
   149
wenzelm@4867
   150
fun field_tr (Const ("_field", _) $ Free (name, _) $ arg) =
wenzelm@4867
   151
      Syntax.const (suffix fieldN name) $ arg
wenzelm@4867
   152
  | field_tr t = raise TERM ("field_tr", [t]);
wenzelm@4867
   153
wenzelm@4867
   154
fun fields_tr (Const ("_fields", _) $ field $ fields) =
wenzelm@4867
   155
      field_tr field :: fields_tr fields
wenzelm@4867
   156
  | fields_tr field = [field_tr field];
wenzelm@4867
   157
wenzelm@4867
   158
fun record_tr (*"_record"*) [fields] =
wenzelm@4867
   159
      foldr (op $) (fields_tr fields, HOLogic.unit)
wenzelm@4867
   160
  | record_tr (*"_record"*) ts = raise TERM ("record_tr", ts);
wenzelm@4867
   161
wenzelm@4867
   162
fun record_scheme_tr (*"_record_scheme"*) [fields, more] =
wenzelm@4867
   163
      foldr (op $) (fields_tr fields, more)
wenzelm@4867
   164
  | record_scheme_tr (*"_record_scheme"*) ts = raise TERM ("record_scheme_tr", ts);
wenzelm@4867
   165
wenzelm@4867
   166
wenzelm@4867
   167
(* print translations *)		(* FIXME tune, activate *)
wenzelm@4867
   168
wenzelm@4867
   169
(* FIXME ... :: tms *)
wenzelm@4867
   170
fun fields_tr' (tm as Const (name_field, _) $ arg $ more) =
wenzelm@4867
   171
      (case try (unsuffix fieldN) name_field of
wenzelm@4867
   172
        Some name =>
wenzelm@4867
   173
          apfst (cons (Syntax.const "_field" $ Syntax.free name $ arg)) (fields_tr' more)
wenzelm@4867
   174
      | None => ([], tm))
wenzelm@4867
   175
  | fields_tr' tm = ([], tm);
wenzelm@4867
   176
wenzelm@4867
   177
fun record_tr' tm =
wenzelm@4867
   178
  let
wenzelm@4867
   179
    val mk_fields = foldr (fn (field, fields) => Syntax.const "_fields" $ field $ fields);
wenzelm@4867
   180
    val (fields, more) = fields_tr' tm;
wenzelm@4867
   181
  in
wenzelm@4867
   182
    if HOLogic.is_unit more then
wenzelm@4867
   183
      Syntax.const "_record" $ mk_fields (split_last fields)
wenzelm@4867
   184
    else Syntax.const "_record_scheme" $ mk_fields (fields, more)
wenzelm@4867
   185
  end;
wenzelm@4867
   186
wenzelm@4867
   187
fun field_tr' name [arg, more] = record_tr' (Syntax.const name $ arg $ more)
wenzelm@4867
   188
  | field_tr' _ _ = raise Match;
wenzelm@4867
   189
wenzelm@4867
   190
wenzelm@4867
   191
wenzelm@4867
   192
(*** extend theory by record definition ***)
wenzelm@4867
   193
wenzelm@4867
   194
(** record info **)
wenzelm@4867
   195
wenzelm@4867
   196
(* type record_info and parent_info *)
wenzelm@4867
   197
wenzelm@4867
   198
type record_info =
wenzelm@4867
   199
 {args: (string * sort) list,
wenzelm@4867
   200
  parent: (typ list * string) option,
wenzelm@4867
   201
  fields: (string * typ) list,
wenzelm@4867
   202
  simps: tthm list};
wenzelm@4867
   203
wenzelm@4867
   204
type parent_info =
wenzelm@4867
   205
 {name: string,
wenzelm@4867
   206
  fields: (string * typ) list,
wenzelm@4867
   207
  simps: tthm list};
wenzelm@4867
   208
wenzelm@4867
   209
wenzelm@4867
   210
(* theory data *)
wenzelm@4867
   211
wenzelm@4867
   212
val recordsK = "HOL/records";
wenzelm@4867
   213
exception Records of record_info Symtab.table;
wenzelm@4867
   214
wenzelm@4867
   215
fun print_records thy = Display.print_data thy recordsK;
wenzelm@4867
   216
wenzelm@4867
   217
local
wenzelm@4867
   218
  val empty = Records Symtab.empty;
wenzelm@4867
   219
wenzelm@4867
   220
  fun prep_ext (x as Records _) = x;
wenzelm@4867
   221
wenzelm@4867
   222
  fun merge (Records tab1, Records tab2) =
wenzelm@4867
   223
    Records (Symtab.merge (K true) (tab1, tab2));
wenzelm@4867
   224
wenzelm@4867
   225
  fun print sg (Records tab) =
wenzelm@4867
   226
    let
wenzelm@4867
   227
      val prt_typ = Sign.pretty_typ sg;
wenzelm@4867
   228
      val ext_const = Sign.cond_extern sg Sign.constK;
wenzelm@4867
   229
wenzelm@4867
   230
      fun pretty_parent None = []
wenzelm@4867
   231
        | pretty_parent (Some (Ts, name)) =
wenzelm@4867
   232
            [Pretty.block [prt_typ (Type (name, Ts)), Pretty.str " +"]];
wenzelm@4867
   233
wenzelm@4867
   234
      fun pretty_field (c, T) = Pretty.block
wenzelm@4867
   235
        [Pretty.str (ext_const c), Pretty.str " ::", Pretty.brk 1, Pretty.quote (prt_typ T)];
wenzelm@4867
   236
wenzelm@4867
   237
      fun pretty_record (name, {args, parent, fields, simps = _}) = Pretty.block (Pretty.fbreaks
wenzelm@4867
   238
        (Pretty.block [prt_typ (Type (name, map TFree args)), Pretty.str " = "] ::
wenzelm@4867
   239
          pretty_parent parent @ map pretty_field fields));
wenzelm@4867
   240
    in
wenzelm@4867
   241
      seq (Pretty.writeln o pretty_record) (Symtab.dest tab)
wenzelm@4867
   242
    end;
wenzelm@4867
   243
in
wenzelm@4867
   244
  val record_thy_data = (recordsK, (empty, prep_ext, merge, print));
wenzelm@4867
   245
end;
wenzelm@4867
   246
wenzelm@4867
   247
wenzelm@4867
   248
(* get and put records *)
wenzelm@4867
   249
wenzelm@4867
   250
fun get_records thy =
wenzelm@4867
   251
  (case Theory.get_data thy recordsK of
wenzelm@4867
   252
    Records tab => tab
wenzelm@4867
   253
  | _ => type_error recordsK);
wenzelm@4867
   254
wenzelm@4867
   255
fun get_record thy name = Symtab.lookup (get_records thy, name);
wenzelm@4867
   256
wenzelm@4867
   257
wenzelm@4867
   258
fun put_records tab thy =
wenzelm@4867
   259
  Theory.put_data (recordsK, Records tab) thy;
wenzelm@4867
   260
wenzelm@4867
   261
fun put_new_record name info thy =
wenzelm@4867
   262
  thy |> put_records
wenzelm@4867
   263
    (Symtab.update_new ((name, info), get_records thy)
wenzelm@4867
   264
      handle Symtab.DUP _ => error ("Duplicate definition of record " ^ quote name));
wenzelm@4867
   265
wenzelm@4867
   266
wenzelm@4867
   267
(* parent records *)
wenzelm@4867
   268
wenzelm@4867
   269
fun inst_record thy (types, name) =
wenzelm@4867
   270
  let
wenzelm@4867
   271
    val sign = Theory.sign_of thy;
wenzelm@4867
   272
    fun err msg = error (msg ^ " parent record " ^ quote name);
wenzelm@4867
   273
wenzelm@4867
   274
    val {args, parent, fields, simps} =
wenzelm@4867
   275
      (case get_record thy name of Some info => info | None => err "Unknown");
wenzelm@4867
   276
wenzelm@4867
   277
    fun bad_inst ((x, S), T) =
wenzelm@4867
   278
      if Sign.of_sort sign (T, S) then None else Some x
wenzelm@4867
   279
    val bads = mapfilter bad_inst (args ~~ types);
wenzelm@4867
   280
wenzelm@4867
   281
    val inst = map fst args ~~ types;
wenzelm@4867
   282
    val subst = Term.map_type_tfree (fn (x, _) => the (assoc (inst, x)));
wenzelm@4867
   283
  in
wenzelm@4867
   284
    if length types <> length args then
wenzelm@4867
   285
      err "Bad number of arguments for"
wenzelm@4867
   286
    else if not (null bads) then
wenzelm@4867
   287
      err ("Ill-sorted instantiation of " ^ commas bads ^ " in")
wenzelm@4867
   288
    else (apsome (apfst (map subst)) parent, map (apsnd subst) fields, simps)
wenzelm@4867
   289
  end;
wenzelm@4867
   290
wenzelm@4867
   291
fun add_parents thy (None, parents) = parents
wenzelm@4867
   292
  | add_parents thy (Some (types, name), parents) =
wenzelm@4867
   293
      let val (pparent, pfields, psimps) = inst_record thy (types, name)
wenzelm@4867
   294
      in add_parents thy (pparent, {name = name, fields = pfields, simps = psimps} :: parents) end;
wenzelm@4867
   295
wenzelm@4867
   296
wenzelm@4867
   297
wenzelm@4867
   298
(** record theorems **)
wenzelm@4867
   299
wenzelm@4867
   300
(* proof by simplification *)
wenzelm@4867
   301
wenzelm@4867
   302
fun prove_simp thy opt_ss simps =
wenzelm@4867
   303
  let val ss = if_none opt_ss HOL_basic_ss addsimps simps in
wenzelm@4867
   304
    fn goal => Goals.prove_goalw_cterm [] (Thm.cterm_of (sign_of thy) goal)
wenzelm@4867
   305
      (K [ALLGOALS (Simplifier.simp_tac ss)])
wenzelm@4867
   306
  end;
wenzelm@4867
   307
wenzelm@4867
   308
wenzelm@4867
   309
wenzelm@4867
   310
(** internal theory extender **)
wenzelm@4867
   311
wenzelm@4867
   312
(*do the actual record definition, assuming that all arguments are
wenzelm@4867
   313
  well-formed*)
wenzelm@4867
   314
wenzelm@4867
   315
fun record_definition (args, bname) parent (parents: parent_info list) bfields thy =
wenzelm@4867
   316
  let
wenzelm@4867
   317
    val sign = Theory.sign_of thy;
wenzelm@4867
   318
    val full = Sign.full_name_path sign bname;
wenzelm@4867
   319
wenzelm@4867
   320
wenzelm@4867
   321
    (* input *)
wenzelm@4867
   322
wenzelm@4867
   323
    val alphas = map fst args;
wenzelm@4867
   324
    val name = Sign.full_name sign bname;		(* FIXME !? *)
wenzelm@4867
   325
    val parent_fields = flat (map #fields parents);
wenzelm@4867
   326
    val fields = map (apfst full) bfields;
wenzelm@4867
   327
wenzelm@4867
   328
    val all_fields = parent_fields @ fields;
wenzelm@4867
   329
    val all_types = map snd all_fields;
wenzelm@4867
   330
wenzelm@4867
   331
wenzelm@4867
   332
    (* term / type components *)
wenzelm@4867
   333
wenzelm@4867
   334
    val zeta = variant alphas "'z";
wenzelm@4867
   335
    val moreT = TFree (zeta, moreS);
wenzelm@4867
   336
wenzelm@4867
   337
    val xs = variantlist (map fst bfields, []);
wenzelm@4867
   338
    val vars = map2 Free (xs, map snd fields);
wenzelm@4867
   339
    val more = Free (variant xs moreN, moreT);
wenzelm@4867
   340
wenzelm@4867
   341
    val rec_schemeT = mk_recordT (all_fields, moreT);
wenzelm@4867
   342
    val recT = mk_recordT (all_fields, HOLogic.unitT);
wenzelm@4867
   343
wenzelm@4867
   344
    (* FIXME tune *)
wenzelm@4867
   345
    val make_schemeT = all_types ---> moreT --> rec_schemeT;
wenzelm@4867
   346
    val make_scheme = Const (full make_schemeN, make_schemeT);
wenzelm@4867
   347
    val makeT = all_types ---> recT;
wenzelm@4867
   348
    val make = Const (full makeN, makeT);
wenzelm@4867
   349
wenzelm@4867
   350
    val parent_more = funpow (length parent_fields) mk_snd;
wenzelm@4867
   351
wenzelm@4867
   352
wenzelm@4867
   353
    (* prepare type definitions *)
wenzelm@4867
   354
wenzelm@4867
   355
    (*field types*)
wenzelm@4867
   356
    fun mk_fieldT_spec ((c, T), a) =
wenzelm@4867
   357
      (suffix field_typeN c, [a, zeta],
wenzelm@4867
   358
        HOLogic.mk_prodT (TFree (a, HOLogic.termS), moreT), Syntax.NoSyn);
wenzelm@4867
   359
    val fieldT_specs = map2 mk_fieldT_spec (bfields, alphas);
wenzelm@4867
   360
wenzelm@4867
   361
    (*record types*)
wenzelm@4867
   362
    val recordT_specs =
wenzelm@4867
   363
      [(suffix schemeN bname, alphas @ [zeta], rec_schemeT, Syntax.NoSyn),
wenzelm@4867
   364
        (bname, alphas, recT, Syntax.NoSyn)];
wenzelm@4867
   365
wenzelm@4867
   366
wenzelm@4867
   367
    (* prepare declarations *)
wenzelm@4867
   368
wenzelm@4867
   369
    val field_decls = map (mk_fieldC moreT) fields;
wenzelm@4867
   370
    val dest_decls = map (mk_fstC moreT) fields @ map (mk_sndC moreT) fields;
wenzelm@4867
   371
    val sel_decls = map (mk_selC rec_schemeT) fields;
wenzelm@4867
   372
    val update_decls = map (mk_updateC rec_schemeT) fields;
wenzelm@4867
   373
    val make_decls = [(make_schemeN, make_schemeT), (makeN, makeT)];
wenzelm@4867
   374
wenzelm@4867
   375
wenzelm@4867
   376
    (* prepare definitions *)
wenzelm@4867
   377
wenzelm@4867
   378
    (*field constructors*)
wenzelm@4867
   379
    fun mk_field_spec ((c, _), v) =
wenzelm@4867
   380
      Logic.mk_defpair (mk_field ((c, v), more), HOLogic.mk_prod (v, more));
wenzelm@4867
   381
    val field_specs = map2 mk_field_spec (fields, vars);
wenzelm@4867
   382
wenzelm@4867
   383
    (*field destructors*)
wenzelm@4867
   384
    fun mk_dest_spec dest dest' (c, T) =
wenzelm@4867
   385
      let
wenzelm@4867
   386
        val p = Free ("p",  mk_fieldT ((c, T), moreT));
wenzelm@4867
   387
        val p' = Free ("p",  HOLogic.mk_prodT (T, moreT));  (*Note: field types are abbreviations*)
wenzelm@4867
   388
      in Logic.mk_defpair (dest p, dest' p') end;
wenzelm@4867
   389
    val dest_specs =
wenzelm@4867
   390
      map (mk_dest_spec mk_fst HOLogic.mk_fst) fields @
wenzelm@4867
   391
      map (mk_dest_spec mk_snd HOLogic.mk_snd) fields;
wenzelm@4867
   392
wenzelm@4867
   393
    (*field selectors*)		(* FIXME tune *)
wenzelm@4867
   394
    fun mk_sel_specs _ [] specs = rev specs
wenzelm@4867
   395
      | mk_sel_specs prfx ((c, T) :: fs) specs =
wenzelm@4867
   396
          let
wenzelm@4867
   397
            val prfx' = prfx @ [(c, T)];
wenzelm@4867
   398
            val r = Free ("r", mk_recordT (prfx' @ fs, moreT));
wenzelm@4867
   399
            val spec = Logic.mk_defpair (mk_sel c r, mk_fst (funpow (length prfx) mk_snd r));
wenzelm@4867
   400
          in mk_sel_specs prfx' fs (spec :: specs) end;
wenzelm@4867
   401
    val sel_specs = mk_sel_specs parent_fields fields [];
wenzelm@4867
   402
wenzelm@4867
   403
    (*updates*)
wenzelm@4867
   404
    val update_specs = [];	(* FIXME *)
wenzelm@4867
   405
wenzelm@4867
   406
    (*makes*)
wenzelm@4867
   407
    val make_specs =
wenzelm@4867
   408
      map Logic.mk_defpair
wenzelm@4867
   409
        [(list_comb (make_scheme, vars) $ more, mk_record (map fst fields ~~ vars, more)),
wenzelm@4867
   410
          (list_comb (make, vars), mk_record (map fst fields ~~ vars, HOLogic.unit))];
wenzelm@4867
   411
wenzelm@4867
   412
wenzelm@4867
   413
    (* 1st stage: defs_thy *)
wenzelm@4867
   414
wenzelm@4867
   415
    val defs_thy =
wenzelm@4867
   416
      thy
wenzelm@4867
   417
      |> Theory.add_path bname
wenzelm@4867
   418
      |> Theory.add_tyabbrs_i (fieldT_specs @ recordT_specs)
wenzelm@4867
   419
      |> (Theory.add_consts_i o map (Syntax.no_syn o apfst Sign.base_name))
wenzelm@4867
   420
        (field_decls @ dest_decls @ sel_decls @ update_decls @ make_decls)
wenzelm@4867
   421
      |> (PureThy.add_defs_i o map Attribute.none)
wenzelm@4867
   422
        (field_specs @ dest_specs @ sel_specs @ update_specs @ make_specs);
wenzelm@4867
   423
wenzelm@4867
   424
    local fun get_defs specs = map (PureThy.get_tthm defs_thy o fst) specs in
wenzelm@4867
   425
      val make_defs = get_defs make_specs;
wenzelm@4867
   426
      val field_defs = get_defs field_specs;
wenzelm@4867
   427
      val sel_defs = get_defs sel_specs;
wenzelm@4867
   428
      val update_defs = get_defs update_specs;
wenzelm@4867
   429
    end;
wenzelm@4867
   430
wenzelm@4867
   431
wenzelm@4867
   432
    (* 2nd stage: thms_thy *)
wenzelm@4867
   433
wenzelm@4867
   434
    val thms_thy =
wenzelm@4867
   435
      defs_thy
wenzelm@4867
   436
      |> (PureThy.add_tthmss o map Attribute.none)
wenzelm@4867
   437
        [("make_defs", make_defs),
wenzelm@4867
   438
          ("field_defs", field_defs),
wenzelm@4867
   439
          ("sel_defs", sel_defs),
wenzelm@4867
   440
          ("update_defs", update_defs)]
wenzelm@4867
   441
(*    |> record_theorems FIXME *)
wenzelm@4867
   442
wenzelm@4867
   443
wenzelm@4867
   444
    (* 3rd stage: final_thy *)
wenzelm@4867
   445
wenzelm@4867
   446
    val final_thy =
wenzelm@4867
   447
      thms_thy
wenzelm@4867
   448
      |> put_new_record name
wenzelm@4867
   449
        {args = args, parent = parent, fields = fields, simps = [] (* FIXME *)}
wenzelm@4867
   450
      |> Theory.parent_path;
wenzelm@4867
   451
wenzelm@4867
   452
  in final_thy end;
wenzelm@4867
   453
wenzelm@4867
   454
wenzelm@4867
   455
wenzelm@4867
   456
(** theory extender interface **)
wenzelm@4867
   457
wenzelm@4867
   458
(*do all preparations and error checks here, deferring the real work
wenzelm@4867
   459
  to record_definition above*)
wenzelm@4867
   460
wenzelm@4867
   461
wenzelm@4867
   462
(* prepare arguments *)
wenzelm@4867
   463
wenzelm@4867
   464
(*Note: read_raw_typ avoids expanding type abbreviations*)
wenzelm@4867
   465
fun read_raw_parent sign s =
wenzelm@4867
   466
  (case Sign.read_raw_typ (sign, K None) s handle TYPE (msg, _, _) => error msg of
wenzelm@4867
   467
    Type (name, Ts) => (Ts, name)
wenzelm@4867
   468
  | _ => error ("Bad parent record specification: " ^ quote s));
wenzelm@4867
   469
wenzelm@4867
   470
fun read_typ sign (env, s) =
wenzelm@4867
   471
  let
wenzelm@4867
   472
    fun def_type (x, ~1) = assoc (env, x)
wenzelm@4867
   473
      | def_type _ = None;
wenzelm@4867
   474
    val T = Type.no_tvars (Sign.read_typ (sign, def_type) s) handle TYPE (msg, _, _) => error msg;
wenzelm@4867
   475
  in (Term.add_typ_tfrees (T, env), T) end;
wenzelm@4867
   476
wenzelm@4867
   477
fun cert_typ sign (env, raw_T) =
wenzelm@4867
   478
  let val T = Type.no_tvars (Sign.certify_typ sign raw_T) handle TYPE (msg, _, _) => error msg
wenzelm@4867
   479
  in (Term.add_typ_tfrees (T, env), T) end;
wenzelm@4867
   480
wenzelm@4867
   481
wenzelm@4867
   482
(* add_record *)
wenzelm@4867
   483
wenzelm@4867
   484
fun gen_add_record prep_typ prep_raw_parent (params, bname) raw_parent raw_fields thy =
wenzelm@4867
   485
  let
wenzelm@4867
   486
    val _ = Theory.require thy "Record" "record definitions";
wenzelm@4867
   487
    val sign = Theory.sign_of thy;
wenzelm@4867
   488
wenzelm@4867
   489
wenzelm@4867
   490
    (* parents *)
wenzelm@4867
   491
wenzelm@4867
   492
    fun prep_inst T = snd (cert_typ sign ([], T));
wenzelm@4867
   493
wenzelm@4867
   494
    val parent = apsome (apfst (map prep_inst) o prep_raw_parent sign) raw_parent
wenzelm@4867
   495
      handle ERROR => error ("The error(s) above in parent record specification");
wenzelm@4867
   496
    val parents = add_parents thy (parent, []);
wenzelm@4867
   497
wenzelm@4867
   498
    val init_env =
wenzelm@4867
   499
      (case parent of
wenzelm@4867
   500
        None => []
wenzelm@4867
   501
      | Some (types, _) => foldr Term.add_typ_tfrees (types, []));
wenzelm@4867
   502
wenzelm@4867
   503
wenzelm@4867
   504
    (* fields *)
wenzelm@4867
   505
wenzelm@4867
   506
    fun prep_fields (env, []) = (env, [])
wenzelm@4867
   507
      | prep_fields (env, (c, raw_T) :: fs) =
wenzelm@4867
   508
          let
wenzelm@4867
   509
            val (env', T) = prep_typ sign (env, raw_T) handle ERROR =>
wenzelm@4867
   510
              error ("The error(s) above occured in field " ^ quote c);
wenzelm@4867
   511
            val (env'', fs') = prep_fields (env', fs);
wenzelm@4867
   512
      in (env'', (c, T) :: fs') end;
wenzelm@4867
   513
wenzelm@4867
   514
    val (envir, bfields) = prep_fields (init_env, raw_fields);
wenzelm@4867
   515
    val envir_names = map fst envir;
wenzelm@4867
   516
wenzelm@4867
   517
wenzelm@4867
   518
    (* args *)
wenzelm@4867
   519
wenzelm@4867
   520
    val defaultS = Sign.defaultS sign;
wenzelm@4867
   521
    val args = map (fn x => (x, if_none (assoc (envir, x)) defaultS)) params;
wenzelm@4867
   522
wenzelm@4867
   523
wenzelm@4867
   524
    (* errors *)
wenzelm@4867
   525
wenzelm@4867
   526
    val err_dup_parms =
wenzelm@4867
   527
      (case duplicates params of
wenzelm@4867
   528
        [] => []
wenzelm@4867
   529
      | dups => ["Duplicate parameters " ^ commas params]);
wenzelm@4867
   530
wenzelm@4867
   531
    val err_extra_frees =
wenzelm@4867
   532
      (case gen_rems (op =) (envir_names, params) of
wenzelm@4867
   533
        [] => []
wenzelm@4867
   534
      | extras => ["Extraneous free type variables " ^ commas extras]);
wenzelm@4867
   535
wenzelm@4867
   536
    val err_no_fields = if null bfields then ["No fields"] else [];
wenzelm@4867
   537
wenzelm@4867
   538
    val err_dup_fields =
wenzelm@4867
   539
      (case duplicates (map fst bfields) of
wenzelm@4867
   540
        [] => []
wenzelm@4867
   541
      | dups => ["Duplicate fields " ^ commas_quote dups]);
wenzelm@4867
   542
wenzelm@4867
   543
    val err_dup_sorts =
wenzelm@4867
   544
      (case duplicates envir_names of
wenzelm@4867
   545
        [] => []
wenzelm@4867
   546
      | dups => ["Inconsistent sort constraints for " ^ commas dups]);
wenzelm@4867
   547
wenzelm@4867
   548
    val errs =
wenzelm@4867
   549
      err_dup_parms @ err_extra_frees @ err_no_fields @ err_dup_fields @ err_dup_sorts;
wenzelm@4867
   550
  in
wenzelm@4867
   551
    if null errs then ()
wenzelm@4867
   552
    else error (cat_lines errs);
wenzelm@4867
   553
wenzelm@4867
   554
    writeln ("Defining record " ^ quote bname ^ " ...");
wenzelm@4867
   555
    thy |> record_definition (args, bname) parent parents bfields
wenzelm@4867
   556
  end
wenzelm@4867
   557
  handle ERROR => error ("Failed to define record " ^ quote bname);
wenzelm@4867
   558
wenzelm@4867
   559
val add_record = gen_add_record read_typ read_raw_parent;
wenzelm@4867
   560
val add_record_i = gen_add_record cert_typ (K I);
wenzelm@4867
   561
wenzelm@4867
   562
wenzelm@4867
   563
wenzelm@4867
   564
(** setup theory **)
wenzelm@4867
   565
wenzelm@4867
   566
val setup =
wenzelm@4867
   567
 [Theory.init_data [record_thy_data],
wenzelm@4867
   568
  Theory.add_trfuns
wenzelm@4867
   569
    ([], [("_record", record_tr), ("_record_scheme", record_scheme_tr)], [], [])];
wenzelm@4867
   570
wenzelm@4867
   571
wenzelm@4867
   572
end;