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