src/HOL/Tools/record_package.ML
author wenzelm
Thu Oct 18 21:22:40 2001 +0200 (2001-10-18)
changeset 11833 475f772ab643
parent 11832 8fca3665d1ee
child 11923 929d97ed8c0f
permissions -rw-r--r--
tuned;
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@9230
     4
    License:    GPL (GNU GENERAL PUBLIC LICENSE)
wenzelm@4867
     5
wenzelm@4867
     6
Extensible records with structural subtyping in HOL.
wenzelm@5698
     7
*)
wenzelm@4867
     8
wenzelm@5698
     9
signature BASIC_RECORD_PACKAGE =
wenzelm@5698
    10
sig
wenzelm@7178
    11
  val record_simproc: simproc
wenzelm@5698
    12
  val record_split_tac: int -> tactic
wenzelm@5713
    13
  val record_split_name: string
wenzelm@5698
    14
  val record_split_wrapper: string * wrapper
wenzelm@5698
    15
end;
wenzelm@4867
    16
wenzelm@4867
    17
signature RECORD_PACKAGE =
wenzelm@4867
    18
sig
wenzelm@5698
    19
  include BASIC_RECORD_PACKAGE
wenzelm@5698
    20
  val quiet_mode: bool ref
wenzelm@8574
    21
  val updateN: string
wenzelm@4890
    22
  val moreS: sort
wenzelm@4890
    23
  val mk_fieldT: (string * typ) * typ -> typ
wenzelm@4890
    24
  val dest_fieldT: typ -> (string * typ) * typ
wenzelm@4890
    25
  val mk_field: (string * term) * term -> term
wenzelm@4890
    26
  val mk_fst: term -> term
wenzelm@4890
    27
  val mk_snd: term -> term
wenzelm@4890
    28
  val mk_recordT: (string * typ) list * typ -> typ
wenzelm@4890
    29
  val dest_recordT: typ -> (string * typ) list * typ
wenzelm@4890
    30
  val mk_record: (string * term) list * term -> term
wenzelm@4890
    31
  val mk_sel: term -> string -> term
wenzelm@4890
    32
  val mk_update: term -> string * term -> term
wenzelm@4867
    33
  val print_records: theory -> unit
wenzelm@4867
    34
  val add_record: (string list * bstring) -> string option
wenzelm@6519
    35
    -> (bstring * string) list -> theory -> theory * {simps: thm list, iffs: thm list}
wenzelm@4867
    36
  val add_record_i: (string list * bstring) -> (typ list * string) option
wenzelm@6519
    37
    -> (bstring * typ) list -> theory -> theory * {simps: thm list, iffs: thm list}
wenzelm@4867
    38
  val setup: (theory -> theory) list
wenzelm@4867
    39
end;
wenzelm@4867
    40
wenzelm@4867
    41
structure RecordPackage: RECORD_PACKAGE =
wenzelm@4867
    42
struct
wenzelm@4867
    43
wenzelm@4867
    44
wenzelm@11832
    45
(*** theory context references ***)
wenzelm@11832
    46
wenzelm@11832
    47
val product_typeN = "Record.product_type";
wenzelm@11832
    48
wenzelm@11832
    49
val product_typeI = thm "product_typeI";
wenzelm@11832
    50
val product_type_inject = thm "product_type_inject";
wenzelm@11832
    51
val product_type_conv1 = thm "product_type_conv1";
wenzelm@11832
    52
val product_type_conv2 = thm "product_type_conv2";
wenzelm@11832
    53
val product_type_split_paired_all = thm "product_type_split_paired_all";
wenzelm@11832
    54
wenzelm@11832
    55
wenzelm@11832
    56
wenzelm@4894
    57
(*** utilities ***)
wenzelm@4867
    58
wenzelm@5698
    59
(* messages *)
wenzelm@5698
    60
wenzelm@5698
    61
val quiet_mode = ref false;
wenzelm@5698
    62
fun message s = if ! quiet_mode then () else writeln s;
wenzelm@5698
    63
wenzelm@5698
    64
wenzelm@11832
    65
(* fundamental syntax *)
wenzelm@4867
    66
wenzelm@5698
    67
infix 0 :== ===;
wenzelm@4894
    68
wenzelm@4894
    69
val (op :==) = Logic.mk_defpair;
wenzelm@4894
    70
val (op ===) = HOLogic.mk_Trueprop o HOLogic.mk_eq;
wenzelm@4894
    71
wenzelm@11832
    72
fun lambda v t =
wenzelm@11832
    73
  let val (x, T) = Term.dest_Free v
wenzelm@11832
    74
  in Abs (x, T, abstract_over (v, t)) end;
wenzelm@11832
    75
wenzelm@11832
    76
fun prefix_base s = NameSpace.map_base (fn bname => s ^ bname);
wenzelm@11832
    77
wenzelm@4894
    78
wenzelm@4894
    79
(* proof by simplification *)
wenzelm@4890
    80
wenzelm@7178
    81
fun prove_simp sign ss tacs simps =
wenzelm@4894
    82
  let
wenzelm@7178
    83
    val ss' = Simplifier.addsimps (ss, simps);
wenzelm@4895
    84
wenzelm@4895
    85
    fun prove goal =
wenzelm@6092
    86
      Goals.prove_goalw_cterm [] (Thm.cterm_of sign goal)
wenzelm@9626
    87
        (K (tacs @ [ALLGOALS (Simplifier.asm_full_simp_tac ss')]))
wenzelm@6092
    88
      handle ERROR => error ("The error(s) above occurred while trying to prove "
wenzelm@6092
    89
        ^ quote (Sign.string_of_term sign goal));
wenzelm@4894
    90
  in prove end;
wenzelm@4894
    91
wenzelm@4894
    92
wenzelm@4894
    93
wenzelm@4894
    94
(*** syntax operations ***)
wenzelm@4894
    95
wenzelm@4894
    96
(** name components **)
wenzelm@4894
    97
wenzelm@8294
    98
val recordN = "record";
wenzelm@4894
    99
val moreN = "more";
wenzelm@4894
   100
val schemeN = "_scheme";
wenzelm@11832
   101
val field_typeN = "_field_type";
wenzelm@4894
   102
val fieldN = "_field";
wenzelm@5698
   103
val fstN = "_val";
wenzelm@5698
   104
val sndN = "_more";
wenzelm@4894
   105
val updateN = "_update";
wenzelm@4894
   106
val makeN = "make";
wenzelm@4894
   107
val make_schemeN = "make_scheme";
wenzelm@4890
   108
wenzelm@11832
   109
(*see typedef_package.ML*)
wenzelm@11832
   110
val RepN = "Rep_";
wenzelm@11832
   111
val AbsN = "Abs_";
wenzelm@5698
   112
wenzelm@5698
   113
wenzelm@5713
   114
wenzelm@4867
   115
(** tuple operations **)
wenzelm@4867
   116
wenzelm@4867
   117
(* more type class *)
wenzelm@4867
   118
wenzelm@5210
   119
val moreS = ["Record.more"];
wenzelm@4867
   120
wenzelm@4867
   121
wenzelm@4867
   122
(* types *)
wenzelm@4867
   123
wenzelm@4867
   124
fun mk_fieldT ((c, T), U) = Type (suffix field_typeN c, [T, U]);
wenzelm@4867
   125
wenzelm@4867
   126
fun dest_fieldT (typ as Type (c_field_type, [T, U])) =
wenzelm@4867
   127
      (case try (unsuffix field_typeN) c_field_type of
wenzelm@4867
   128
        None => raise TYPE ("dest_fieldT", [typ], [])
wenzelm@4867
   129
      | Some c => ((c, T), U))
wenzelm@4867
   130
  | dest_fieldT typ = raise TYPE ("dest_fieldT", [typ], []);
wenzelm@4867
   131
wenzelm@4867
   132
wenzelm@11832
   133
(* morphisms *)
wenzelm@11832
   134
wenzelm@11832
   135
fun mk_Rep U (c, T) =
wenzelm@11832
   136
  Const (suffix field_typeN (prefix_base RepN c),
wenzelm@11832
   137
    mk_fieldT ((c, T), U) --> HOLogic.mk_prodT (T, U));
wenzelm@11832
   138
wenzelm@11832
   139
fun mk_Abs U (c, T) =
wenzelm@11832
   140
  Const (suffix field_typeN (prefix_base AbsN c),
wenzelm@11832
   141
    HOLogic.mk_prodT (T, U) --> mk_fieldT ((c, T), U));
wenzelm@11833
   142
wenzelm@11832
   143
wenzelm@4867
   144
(* constructors *)
wenzelm@4867
   145
wenzelm@4867
   146
fun mk_fieldC U (c, T) = (suffix fieldN c, T --> U --> mk_fieldT ((c, T), U));
wenzelm@4867
   147
wenzelm@11832
   148
fun mk_field ((c, t), u) =
wenzelm@4867
   149
  let val T = fastype_of t and U = fastype_of u
wenzelm@11832
   150
  in Const (suffix fieldN c, [T, U] ---> mk_fieldT ((c, T), U)) $ t $ u end;
wenzelm@4867
   151
wenzelm@4867
   152
wenzelm@4867
   153
(* destructors *)
wenzelm@4867
   154
wenzelm@4867
   155
fun mk_fstC U (c, T) = (suffix fstN c, mk_fieldT ((c, T), U) --> T);
wenzelm@4867
   156
fun mk_sndC U (c, T) = (suffix sndN c, mk_fieldT ((c, T), U) --> U);
wenzelm@4867
   157
wenzelm@4867
   158
fun dest_field fst_or_snd p =
wenzelm@4867
   159
  let
wenzelm@4867
   160
    val pT = fastype_of p;
wenzelm@4867
   161
    val ((c, T), U) = dest_fieldT pT;
wenzelm@4867
   162
    val (destN, destT) = if fst_or_snd then (fstN, T) else (sndN, U);
wenzelm@4867
   163
  in Const (suffix destN c, pT --> destT) $ p end;
wenzelm@4867
   164
wenzelm@4867
   165
val mk_fst = dest_field true;
wenzelm@4867
   166
val mk_snd = dest_field false;
wenzelm@4867
   167
wenzelm@4867
   168
wenzelm@4867
   169
wenzelm@4867
   170
(** record operations **)
wenzelm@4867
   171
wenzelm@4867
   172
(* types *)
wenzelm@4867
   173
wenzelm@4867
   174
val mk_recordT = foldr mk_fieldT;
wenzelm@4867
   175
wenzelm@4867
   176
fun dest_recordT T =
wenzelm@4867
   177
  (case try dest_fieldT T of
wenzelm@4867
   178
    None => ([], T)
wenzelm@4867
   179
  | Some (c_T, U) => apfst (cons c_T) (dest_recordT U));
wenzelm@4867
   180
wenzelm@4867
   181
fun find_fieldT c rT =
wenzelm@4867
   182
  (case assoc (fst (dest_recordT rT), c) of
wenzelm@4867
   183
    None => raise TYPE ("find_field: " ^ c, [rT], [])
wenzelm@4867
   184
  | Some T => T);
wenzelm@4867
   185
wenzelm@4867
   186
wenzelm@4867
   187
(* constructors *)
wenzelm@4867
   188
wenzelm@4867
   189
val mk_record = foldr mk_field;
wenzelm@4867
   190
wenzelm@4867
   191
wenzelm@4867
   192
(* selectors *)
wenzelm@4867
   193
wenzelm@4867
   194
fun mk_selC rT (c, T) = (c, rT --> T);
wenzelm@4867
   195
wenzelm@4890
   196
fun mk_sel r c =
wenzelm@4867
   197
  let val rT = fastype_of r
wenzelm@4867
   198
  in Const (mk_selC rT (c, find_fieldT c rT)) $ r end;
wenzelm@4867
   199
wenzelm@4894
   200
val mk_moreC = mk_selC;
wenzelm@4894
   201
wenzelm@4894
   202
fun mk_more r c =
wenzelm@4894
   203
  let val rT = fastype_of r
wenzelm@4894
   204
  in Const (mk_moreC rT (c, snd (dest_recordT rT))) $ r end;
wenzelm@4894
   205
wenzelm@4867
   206
wenzelm@4867
   207
(* updates *)
wenzelm@4867
   208
wenzelm@4867
   209
fun mk_updateC rT (c, T) = (suffix updateN c, T --> rT --> rT);
wenzelm@4867
   210
wenzelm@4890
   211
fun mk_update r (c, x) =
wenzelm@4867
   212
  let val rT = fastype_of r
wenzelm@4867
   213
  in Const (mk_updateC rT (c, find_fieldT c rT)) $ x $ r end;
wenzelm@4867
   214
wenzelm@5197
   215
val mk_more_updateC = mk_updateC;
wenzelm@5197
   216
wenzelm@5197
   217
fun mk_more_update r (c, x) =
wenzelm@5197
   218
  let val rT = fastype_of r
wenzelm@5197
   219
  in Const (mk_more_updateC rT (c, snd (dest_recordT rT))) $ x $ r end;
wenzelm@5197
   220
wenzelm@4867
   221
wenzelm@4890
   222
(* make *)
wenzelm@4890
   223
wenzelm@4890
   224
fun mk_makeC rT (c, Ts) = (c, Ts ---> rT);
wenzelm@4890
   225
wenzelm@4890
   226
wenzelm@4867
   227
wenzelm@4867
   228
(** concrete syntax for records **)
wenzelm@4867
   229
wenzelm@4867
   230
(* parse translations *)
wenzelm@4867
   231
wenzelm@11473
   232
fun gen_field_tr mark sfx (t as Const (c, _) $ Const (name, _) $ arg) =
wenzelm@5197
   233
      if c = mark then Syntax.const (suffix sfx name) $ arg
wenzelm@5197
   234
      else raise TERM ("gen_field_tr: " ^ mark, [t])
wenzelm@5197
   235
  | gen_field_tr mark _ t = raise TERM ("gen_field_tr: " ^ mark, [t]);
wenzelm@4867
   236
wenzelm@5197
   237
fun gen_fields_tr sep mark sfx (tm as Const (c, _) $ t $ u) =
wenzelm@5197
   238
      if c = sep then gen_field_tr mark sfx t :: gen_fields_tr sep mark sfx u
wenzelm@5201
   239
      else [gen_field_tr mark sfx tm]
wenzelm@5201
   240
  | gen_fields_tr _ mark sfx tm = [gen_field_tr mark sfx tm];
wenzelm@5197
   241
wenzelm@5197
   242
fun gen_record_tr sep mark sfx unit [t] = foldr (op $) (gen_fields_tr sep mark sfx t, unit)
wenzelm@5201
   243
  | gen_record_tr _ _ _ _ ts = raise TERM ("gen_record_tr", ts);
wenzelm@5197
   244
wenzelm@5197
   245
fun gen_record_scheme_tr sep mark sfx [t, more] = foldr (op $) (gen_fields_tr sep mark sfx t, more)
wenzelm@5201
   246
  | gen_record_scheme_tr _ _ _ ts = raise TERM ("gen_record_scheme_tr", ts);
wenzelm@5197
   247
wenzelm@4867
   248
wenzelm@5197
   249
val record_type_tr = gen_record_tr "_field_types" "_field_type" field_typeN (Syntax.const "unit");
wenzelm@5197
   250
val record_type_scheme_tr = gen_record_scheme_tr "_field_types" "_field_type" field_typeN;
wenzelm@5197
   251
wenzelm@5197
   252
val record_tr = gen_record_tr "_fields" "_field" fieldN HOLogic.unit;
wenzelm@5197
   253
val record_scheme_tr = gen_record_scheme_tr "_fields" "_field" fieldN;
wenzelm@4867
   254
wenzelm@5197
   255
fun record_update_tr [t, u] =
wenzelm@5197
   256
      foldr (op $) (rev (gen_fields_tr "_updates" "_update" updateN u), t)
wenzelm@5197
   257
  | record_update_tr ts = raise TERM ("record_update_tr", ts);
wenzelm@5197
   258
wenzelm@5197
   259
wenzelm@11833
   260
fun update_name_tr (Free (x, T) :: ts) = Term.list_comb (Free (suffix updateN x, T), ts)
wenzelm@11833
   261
  | update_name_tr (Const (x, T) :: ts) = Term.list_comb (Const (suffix updateN x, T), ts)
wenzelm@11833
   262
  | update_name_tr (((c as Const ("_constrain", _)) $ t $ ty) :: ts) =
wenzelm@11833
   263
      Term.list_comb (c $ update_name_tr [t] $
wenzelm@11833
   264
        (Syntax.const "fun" $ ty $ Syntax.const "dummy"), ts)
wenzelm@11833
   265
  | update_name_tr ts = raise TERM ("update_name_tr", ts);
wenzelm@11833
   266
wenzelm@11833
   267
wenzelm@5197
   268
val parse_translation =
wenzelm@5197
   269
 [("_record_type", record_type_tr),
wenzelm@5197
   270
  ("_record_type_scheme", record_type_scheme_tr),
wenzelm@5197
   271
  ("_record", record_tr),
wenzelm@5197
   272
  ("_record_scheme", record_scheme_tr),
wenzelm@11833
   273
  ("_record_update", record_update_tr),
wenzelm@11833
   274
  ("_update_name", update_name_tr)];
wenzelm@4867
   275
wenzelm@4867
   276
wenzelm@4890
   277
(* print translations *)
wenzelm@4867
   278
wenzelm@5197
   279
fun gen_fields_tr' mark sfx (tm as Const (name_field, _) $ t $ u) =
wenzelm@5197
   280
      (case try (unsuffix sfx) name_field of
wenzelm@5197
   281
        Some name =>
wenzelm@5197
   282
          apfst (cons (Syntax.const mark $ Syntax.free name $ t)) (gen_fields_tr' mark sfx u)
wenzelm@5197
   283
      | None => ([], tm))
wenzelm@5197
   284
  | gen_fields_tr' _ _ tm = ([], tm);
wenzelm@5197
   285
wenzelm@5197
   286
fun gen_record_tr' sep mark sfx is_unit record record_scheme tm =
wenzelm@4867
   287
  let
wenzelm@5197
   288
    val (ts, u) = gen_fields_tr' mark sfx tm;
wenzelm@5197
   289
    val t' = foldr1 (fn (v, w) => Syntax.const sep $ v $ w) ts;
wenzelm@4867
   290
  in
wenzelm@5197
   291
    if is_unit u then Syntax.const record $ t'
wenzelm@5197
   292
    else Syntax.const record_scheme $ t' $ u
wenzelm@4867
   293
  end;
wenzelm@4867
   294
wenzelm@5197
   295
wenzelm@5197
   296
val record_type_tr' =
wenzelm@5197
   297
  gen_record_tr' "_field_types" "_field_type" field_typeN
wenzelm@5197
   298
    (fn Const ("unit", _) => true | _ => false) "_record_type" "_record_type_scheme";
wenzelm@5197
   299
wenzelm@5197
   300
val record_tr' =
wenzelm@5197
   301
  gen_record_tr' "_fields" "_field" fieldN HOLogic.is_unit "_record" "_record_scheme";
wenzelm@5197
   302
wenzelm@5197
   303
fun record_update_tr' tm =
wenzelm@5197
   304
  let val (ts, u) = gen_fields_tr' "_update" updateN tm in
wenzelm@5197
   305
    Syntax.const "_record_update" $ u $
wenzelm@5197
   306
      foldr1 (fn (v, w) => Syntax.const "_updates" $ v $ w) (rev ts)
wenzelm@5197
   307
  end;
wenzelm@5197
   308
wenzelm@5197
   309
wenzelm@5201
   310
fun gen_field_tr' sfx tr' name =
wenzelm@5201
   311
  let val name_sfx = suffix sfx name
wenzelm@5201
   312
  in (name_sfx, fn [t, u] => tr' (Syntax.const name_sfx $ t $ u) | _ => raise Match) end;
wenzelm@5201
   313
wenzelm@5197
   314
fun print_translation names =
wenzelm@5197
   315
  map (gen_field_tr' field_typeN record_type_tr') names @
wenzelm@5197
   316
  map (gen_field_tr' fieldN record_tr') names @
wenzelm@5197
   317
  map (gen_field_tr' updateN record_update_tr') names;
wenzelm@4867
   318
wenzelm@4867
   319
wenzelm@4867
   320
wenzelm@4867
   321
(*** extend theory by record definition ***)
wenzelm@4867
   322
wenzelm@4867
   323
(** record info **)
wenzelm@4867
   324
wenzelm@4867
   325
(* type record_info and parent_info *)
wenzelm@4867
   326
wenzelm@4867
   327
type record_info =
wenzelm@4867
   328
 {args: (string * sort) list,
wenzelm@4867
   329
  parent: (typ list * string) option,
wenzelm@4867
   330
  fields: (string * typ) list,
wenzelm@6092
   331
  simps: thm list};
wenzelm@4867
   332
wenzelm@4867
   333
type parent_info =
wenzelm@4867
   334
 {name: string,
wenzelm@4867
   335
  fields: (string * typ) list,
wenzelm@6092
   336
  simps: thm list};
wenzelm@4867
   337
wenzelm@4867
   338
wenzelm@5052
   339
(* data kind 'HOL/records' *)
wenzelm@5001
   340
wenzelm@7178
   341
type record_data =
wenzelm@7178
   342
 {records: record_info Symtab.table,
wenzelm@7178
   343
  sel_upd:
wenzelm@7178
   344
   {selectors: unit Symtab.table,
wenzelm@7178
   345
    updates: string Symtab.table,
wenzelm@7178
   346
    simpset: Simplifier.simpset},
wenzelm@7178
   347
  field_splits:
wenzelm@7178
   348
   {fields: unit Symtab.table,
wenzelm@7178
   349
    simpset: Simplifier.simpset}};
wenzelm@7178
   350
wenzelm@7178
   351
fun make_record_data records sel_upd field_splits =
wenzelm@7178
   352
 {records = records, sel_upd = sel_upd, field_splits = field_splits}: record_data;
wenzelm@7178
   353
wenzelm@5006
   354
structure RecordsArgs =
wenzelm@5006
   355
struct
wenzelm@5006
   356
  val name = "HOL/records";
wenzelm@7178
   357
  type T = record_data;
wenzelm@4867
   358
wenzelm@7178
   359
  val empty =
wenzelm@7178
   360
    make_record_data Symtab.empty
wenzelm@7178
   361
      {selectors = Symtab.empty, updates = Symtab.empty, simpset = HOL_basic_ss}
wenzelm@7178
   362
      {fields = Symtab.empty, simpset = HOL_basic_ss};
wenzelm@7178
   363
wenzelm@6556
   364
  val copy = I;
wenzelm@5006
   365
  val prep_ext = I;
wenzelm@7178
   366
  fun merge
wenzelm@7178
   367
   ({records = recs1,
wenzelm@7178
   368
     sel_upd = {selectors = sels1, updates = upds1, simpset = ss1},
wenzelm@7178
   369
     field_splits = {fields = flds1, simpset = fld_ss1}},
wenzelm@7178
   370
    {records = recs2,
wenzelm@7178
   371
     sel_upd = {selectors = sels2, updates = upds2, simpset = ss2},
wenzelm@7178
   372
     field_splits = {fields = flds2, simpset = fld_ss2}}) =
wenzelm@7178
   373
    make_record_data
wenzelm@7178
   374
      (Symtab.merge (K true) (recs1, recs2))
wenzelm@7178
   375
      {selectors = Symtab.merge (K true) (sels1, sels2),
wenzelm@7178
   376
        updates = Symtab.merge (K true) (upds1, upds2),
wenzelm@7178
   377
        simpset = Simplifier.merge_ss (ss1, ss2)}
wenzelm@7178
   378
      {fields = Symtab.merge (K true) (flds1, flds2),
wenzelm@7178
   379
        simpset = Simplifier.merge_ss (fld_ss1, fld_ss2)};
wenzelm@4867
   380
wenzelm@7178
   381
  fun print sg ({records = recs, ...}: record_data) =
wenzelm@4867
   382
    let
wenzelm@4867
   383
      val prt_typ = Sign.pretty_typ sg;
wenzelm@4867
   384
      val ext_const = Sign.cond_extern sg Sign.constK;
wenzelm@4867
   385
wenzelm@4867
   386
      fun pretty_parent None = []
wenzelm@4867
   387
        | pretty_parent (Some (Ts, name)) =
wenzelm@4867
   388
            [Pretty.block [prt_typ (Type (name, Ts)), Pretty.str " +"]];
wenzelm@4867
   389
wenzelm@4867
   390
      fun pretty_field (c, T) = Pretty.block
wenzelm@4867
   391
        [Pretty.str (ext_const c), Pretty.str " ::", Pretty.brk 1, Pretty.quote (prt_typ T)];
wenzelm@4867
   392
wenzelm@4895
   393
      fun pretty_record (name, {args, parent, fields, simps = _}) = Pretty.block (Pretty.fbreaks
wenzelm@4867
   394
        (Pretty.block [prt_typ (Type (name, map TFree args)), Pretty.str " = "] ::
wenzelm@4867
   395
          pretty_parent parent @ map pretty_field fields));
wenzelm@8720
   396
    in
wenzelm@8720
   397
      map pretty_record (Sign.cond_extern_table sg Sign.typeK recs)
wenzelm@8720
   398
      |> Pretty.chunks |> Pretty.writeln
wenzelm@8720
   399
    end;
wenzelm@4867
   400
end;
wenzelm@4867
   401
wenzelm@5006
   402
structure RecordsData = TheoryDataFun(RecordsArgs);
wenzelm@5006
   403
val print_records = RecordsData.print;
wenzelm@5006
   404
wenzelm@4867
   405
wenzelm@7178
   406
(* access 'records' *)
wenzelm@4867
   407
wenzelm@7178
   408
fun get_record thy name = Symtab.lookup (#records (RecordsData.get thy), name);
wenzelm@4867
   409
wenzelm@4890
   410
fun put_record name info thy =
wenzelm@7178
   411
  let
wenzelm@7178
   412
    val {records, sel_upd, field_splits} = RecordsData.get thy;
wenzelm@7178
   413
    val data = make_record_data (Symtab.update ((name, info), records)) sel_upd field_splits;
wenzelm@7178
   414
  in RecordsData.put data thy end;
wenzelm@7178
   415
wenzelm@7178
   416
wenzelm@7178
   417
(* access 'sel_upd' *)
wenzelm@7178
   418
wenzelm@7178
   419
fun get_sel_upd sg = #sel_upd (RecordsData.get_sg sg);
wenzelm@7178
   420
wenzelm@7178
   421
fun get_selectors sg name = Symtab.lookup (#selectors (get_sel_upd sg), name);
wenzelm@7178
   422
fun get_updates sg name = Symtab.lookup (#updates (get_sel_upd sg), name);
wenzelm@7178
   423
fun get_simpset sg = #simpset (get_sel_upd sg);
wenzelm@7178
   424
wenzelm@7178
   425
wenzelm@7178
   426
fun put_sel_upd names simps thy =
wenzelm@7178
   427
  let
wenzelm@7178
   428
    val sels = map (rpair ()) names;
wenzelm@7178
   429
    val upds = map (suffix updateN) names ~~ names;
wenzelm@7178
   430
wenzelm@7178
   431
    val {records, sel_upd = {selectors, updates, simpset}, field_splits} = RecordsData.get thy;
wenzelm@7178
   432
    val data = make_record_data records
wenzelm@7178
   433
      {selectors = Symtab.extend (selectors, sels),
wenzelm@7178
   434
        updates = Symtab.extend (updates, upds),
wenzelm@7178
   435
        simpset = Simplifier.addsimps (simpset, simps)}
wenzelm@7178
   436
      field_splits;
wenzelm@7178
   437
  in RecordsData.put data thy end;
wenzelm@7178
   438
wenzelm@7178
   439
wenzelm@7178
   440
(* access 'field_splits' *)
wenzelm@5698
   441
wenzelm@5698
   442
fun add_record_splits splits thy =
wenzelm@5698
   443
  let
wenzelm@7178
   444
    val {records, sel_upd, field_splits = {fields, simpset}} = RecordsData.get thy;
wenzelm@7178
   445
    val flds = map (rpair () o fst) splits;
wenzelm@7178
   446
    val simps = map snd splits;
wenzelm@7178
   447
    val data = make_record_data records sel_upd
wenzelm@7178
   448
      {fields = Symtab.extend (fields, flds), simpset = Simplifier.addsimps (simpset, simps)};
wenzelm@7178
   449
  in RecordsData.put data thy end;
wenzelm@4867
   450
wenzelm@4867
   451
wenzelm@4867
   452
(* parent records *)
wenzelm@4867
   453
wenzelm@4867
   454
fun inst_record thy (types, name) =
wenzelm@4867
   455
  let
wenzelm@4867
   456
    val sign = Theory.sign_of thy;
wenzelm@4867
   457
    fun err msg = error (msg ^ " parent record " ^ quote name);
wenzelm@4867
   458
wenzelm@4895
   459
    val {args, parent, fields, simps} =
wenzelm@4867
   460
      (case get_record thy name of Some info => info | None => err "Unknown");
wenzelm@4895
   461
    val _ = if length types <> length args then err "Bad number of arguments for" else ();
wenzelm@4867
   462
wenzelm@4867
   463
    fun bad_inst ((x, S), T) =
wenzelm@4867
   464
      if Sign.of_sort sign (T, S) then None else Some x
wenzelm@4867
   465
    val bads = mapfilter bad_inst (args ~~ types);
wenzelm@4867
   466
wenzelm@4867
   467
    val inst = map fst args ~~ types;
wenzelm@4867
   468
    val subst = Term.map_type_tfree (fn (x, _) => the (assoc (inst, x)));
wenzelm@4867
   469
  in
wenzelm@4895
   470
    if not (null bads) then
wenzelm@4867
   471
      err ("Ill-sorted instantiation of " ^ commas bads ^ " in")
wenzelm@4895
   472
    else (apsome (apfst (map subst)) parent, map (apsnd subst) fields, simps)
wenzelm@4867
   473
  end;
wenzelm@4867
   474
wenzelm@4867
   475
fun add_parents thy (None, parents) = parents
wenzelm@4867
   476
  | add_parents thy (Some (types, name), parents) =
wenzelm@4895
   477
      let val (pparent, pfields, psimps) = inst_record thy (types, name)
wenzelm@4895
   478
      in add_parents thy (pparent, {name = name, fields = pfields, simps = psimps} :: parents) end;
wenzelm@4867
   479
wenzelm@4867
   480
wenzelm@4867
   481
wenzelm@7178
   482
(** record simproc **)
wenzelm@7178
   483
wenzelm@7178
   484
local
wenzelm@7178
   485
wenzelm@8100
   486
val sel_upd_pat = [Thm.read_cterm (Theory.sign_of HOL.thy) ("s (u k r)", HOLogic.termT)];
wenzelm@7178
   487
wenzelm@7178
   488
fun proc sg _ t =
wenzelm@7178
   489
  (case t of (sel as Const (s, _)) $ ((upd as Const (u, _)) $ k $ r) =>
wenzelm@7178
   490
    (case get_selectors sg s of Some () =>
wenzelm@7178
   491
      (case get_updates sg u of Some u_name =>
wenzelm@7178
   492
        let
wenzelm@7178
   493
          fun atomize x t = Free (x, fastype_of t);
wenzelm@7178
   494
          val k' = atomize "k" k;
wenzelm@7178
   495
          val r' = atomize "r" r;
wenzelm@7178
   496
          val t' = sel $ (upd $ k' $ r');
wenzelm@7178
   497
          val prove = mk_meta_eq o prove_simp sg (get_simpset sg) [] [];
wenzelm@7178
   498
        in
wenzelm@7178
   499
          if u_name = s then Some (prove (t' === k'))
wenzelm@7178
   500
          else Some (prove (t' === sel $ r'))
wenzelm@7178
   501
        end
wenzelm@7178
   502
      | None => None)
wenzelm@7178
   503
    | None => None)
wenzelm@7178
   504
  | _ => None);
wenzelm@7178
   505
wenzelm@7178
   506
in
wenzelm@7178
   507
wenzelm@7178
   508
val record_simproc = Simplifier.mk_simproc "record_simp" sel_upd_pat proc;
wenzelm@7178
   509
wenzelm@7178
   510
end;
wenzelm@7178
   511
wenzelm@7178
   512
wenzelm@7178
   513
wenzelm@5698
   514
(** record field splitting **)
wenzelm@5698
   515
wenzelm@6358
   516
(* tactic *)
wenzelm@6358
   517
wenzelm@5698
   518
fun record_split_tac i st =
wenzelm@5698
   519
  let
wenzelm@7178
   520
    val {field_splits = {fields, simpset}, ...} = RecordsData.get_sg (Thm.sign_of_thm st);
wenzelm@5698
   521
wenzelm@7178
   522
    fun is_fieldT (_, Type (a, [_, _])) = is_some (Symtab.lookup (fields, a))
wenzelm@5698
   523
      | is_fieldT _ = false;
wenzelm@5698
   524
    val params = Logic.strip_params (Library.nth_elem (i - 1, Thm.prems_of st));
wenzelm@5698
   525
  in
wenzelm@7178
   526
    if exists is_fieldT params then Simplifier.full_simp_tac simpset i st
wenzelm@5698
   527
    else Seq.empty
wenzelm@5698
   528
  end handle Library.LIST _ => Seq.empty;
wenzelm@5698
   529
wenzelm@6358
   530
wenzelm@6358
   531
(* wrapper *)
wenzelm@6358
   532
wenzelm@5707
   533
val record_split_name = "record_split_tac";
wenzelm@5707
   534
val record_split_wrapper = (record_split_name, fn tac => record_split_tac ORELSE' tac);
wenzelm@5698
   535
wenzelm@5698
   536
wenzelm@6358
   537
(* method *)
wenzelm@6358
   538
wenzelm@6358
   539
val record_split_method =
wenzelm@9705
   540
  ("record_split", Method.no_args (Method.SIMPLE_METHOD' HEADGOAL record_split_tac),
wenzelm@6358
   541
    "split record fields");
wenzelm@6358
   542
wenzelm@6358
   543
wenzelm@4890
   544
(** internal theory extenders **)
wenzelm@4867
   545
wenzelm@11832
   546
(* field_typedefs *)
wenzelm@5698
   547
wenzelm@11832
   548
fun field_typedefs zeta moreT names theory =
wenzelm@5698
   549
  let
wenzelm@11832
   550
    val alpha = "'a";
wenzelm@11832
   551
    val aT = TFree (alpha, HOLogic.termS);
wenzelm@11832
   552
    val UNIV = HOLogic.mk_UNIV (HOLogic.mk_prodT (aT, moreT));
wenzelm@5698
   553
wenzelm@11832
   554
    fun type_def (thy, name) =
wenzelm@11832
   555
      let val (thy', {type_definition, set_def = Some def, ...}) = thy
wenzelm@11832
   556
        |> setmp TypedefPackage.quiet_mode true
wenzelm@11832
   557
          (TypedefPackage.add_typedef_i true None
wenzelm@11832
   558
            (suffix field_typeN (Sign.base_name name), [alpha, zeta], Syntax.NoSyn) UNIV None
wenzelm@11832
   559
          (Tactic.rtac UNIV_witness 1))
wenzelm@11832
   560
        |>> setmp AxClass.quiet_mode true (AxClass.add_inst_arity_i
wenzelm@11832
   561
            (suffix field_typeN name, [HOLogic.termS, moreS], moreS) all_tac)
wenzelm@11832
   562
      in (thy', Tactic.rewrite_rule [def] type_definition) end
wenzelm@11832
   563
  in foldl_map type_def (theory, names) end;
wenzelm@5698
   564
wenzelm@5698
   565
wenzelm@4894
   566
(* field_definitions *)
wenzelm@4867
   567
wenzelm@11832
   568
fun field_definitions fields names xs alphas zeta moreT more vars named_vars thy =
wenzelm@4890
   569
  let
wenzelm@5698
   570
    val sign = Theory.sign_of thy;
wenzelm@4890
   571
    val base = Sign.base_name;
wenzelm@5698
   572
    val full_path = Sign.full_name_path sign;
wenzelm@4867
   573
wenzelm@11832
   574
    val xT = TFree (variant alphas "'x", HOLogic.termS);
wenzelm@11832
   575
wenzelm@4867
   576
wenzelm@4890
   577
    (* prepare declarations and definitions *)
wenzelm@4867
   578
wenzelm@5698
   579
    (*field constructors*)
wenzelm@4890
   580
    val field_decls = map (mk_fieldC moreT) fields;
wenzelm@4890
   581
wenzelm@11832
   582
    fun mk_field_spec ((c, T), v) =
wenzelm@11832
   583
      Term.head_of (mk_field ((c, v), more)) :==
wenzelm@11832
   584
        lambda v (lambda more (mk_Abs moreT (c, T) $ (HOLogic.mk_prod (v, more))));
wenzelm@11832
   585
    val field_specs = map mk_field_spec (fields ~~ vars);
wenzelm@4890
   586
wenzelm@4890
   587
    (*field destructors*)
wenzelm@5698
   588
    val dest_decls = map (mk_fstC moreT) fields @ map (mk_sndC moreT) fields;
wenzelm@5698
   589
wenzelm@11832
   590
    fun mk_dest_spec dest sel (c, T) =
wenzelm@5698
   591
      let val p = Free ("p", mk_fieldT ((c, T), moreT));
wenzelm@11832
   592
      in Term.head_of (dest p) :== lambda p (sel (mk_Rep moreT (c, T) $ p)) end;
wenzelm@11832
   593
    val dest_specs1 = map (mk_dest_spec mk_fst HOLogic.mk_fst) fields;
wenzelm@11832
   594
    val dest_specs2 = map (mk_dest_spec mk_snd HOLogic.mk_snd) fields;
wenzelm@4890
   595
wenzelm@4890
   596
wenzelm@11832
   597
    (* 1st stage: defs_thy *)
wenzelm@5713
   598
wenzelm@11832
   599
    val (defs_thy, (((typedefs, field_defs), dest_defs1), dest_defs2)) =
wenzelm@11832
   600
      thy
wenzelm@11832
   601
      |> field_typedefs zeta moreT names
wenzelm@11832
   602
      |>> (Theory.add_consts_i o map (Syntax.no_syn o apfst base)) (field_decls @ dest_decls)
wenzelm@11832
   603
      |>>> (PureThy.add_defs_i false o map Thm.no_attributes) field_specs
wenzelm@11832
   604
      |>>> (PureThy.add_defs_i false o map Thm.no_attributes) dest_specs1
wenzelm@11832
   605
      |>>> (PureThy.add_defs_i false o map Thm.no_attributes) dest_specs2;
wenzelm@4890
   606
wenzelm@11832
   607
    val prod_types = map (fn (((a, b), c), d) => product_typeI OF [a, b, c, d])
wenzelm@11832
   608
      (typedefs ~~ field_defs ~~ dest_defs1 ~~ dest_defs2);
wenzelm@4890
   609
wenzelm@5698
   610
wenzelm@11832
   611
    (* 2nd stage: thms_thy *)
wenzelm@5698
   612
wenzelm@11832
   613
    fun make th = map (fn prod_type => Drule.standard (th OF [prod_type])) prod_types;
wenzelm@4890
   614
wenzelm@11832
   615
    val field_injects = make product_type_inject;
wenzelm@11832
   616
    val dest_convs = make product_type_conv1 @ make product_type_conv2;
wenzelm@11832
   617
    val field_splits = make product_type_split_paired_all;
wenzelm@4894
   618
wenzelm@4890
   619
    val thms_thy =
wenzelm@4890
   620
      defs_thy
wenzelm@8428
   621
      |> (#1 oo (PureThy.add_thmss o map Thm.no_attributes))
wenzelm@4890
   622
        [("field_defs", field_defs),
wenzelm@11832
   623
          ("dest_defs", dest_defs1 @ dest_defs2),
wenzelm@5698
   624
          ("dest_convs", dest_convs),
wenzelm@5713
   625
          ("field_splits", field_splits)];
wenzelm@4890
   626
wenzelm@5713
   627
  in (thms_thy, dest_convs, field_injects, field_splits) end;
wenzelm@4890
   628
wenzelm@4890
   629
wenzelm@4890
   630
(* record_definition *)
wenzelm@4867
   631
wenzelm@4867
   632
fun record_definition (args, bname) parent (parents: parent_info list) bfields thy =
wenzelm@4867
   633
  let
wenzelm@4867
   634
    val sign = Theory.sign_of thy;
wenzelm@4867
   635
    val full = Sign.full_name_path sign bname;
wenzelm@4890
   636
    val base = Sign.base_name;
wenzelm@4867
   637
wenzelm@4867
   638
wenzelm@4890
   639
    (* basic components *)
wenzelm@4867
   640
wenzelm@4867
   641
    val alphas = map fst args;
wenzelm@5197
   642
    val name = Sign.full_name sign bname;       (*not made part of record name space!*)
wenzelm@4890
   643
wenzelm@4867
   644
    val parent_fields = flat (map #fields parents);
wenzelm@4890
   645
    val parent_names = map fst parent_fields;
wenzelm@4890
   646
    val parent_types = map snd parent_fields;
wenzelm@4890
   647
    val parent_len = length parent_fields;
wenzelm@8294
   648
    val parent_xs = variantlist (map (base o fst) parent_fields, [moreN, recordN]);
wenzelm@4890
   649
    val parent_vars = ListPair.map Free (parent_xs, parent_types);
wenzelm@4894
   650
    val parent_named_vars = parent_names ~~ parent_vars;
wenzelm@4890
   651
wenzelm@4867
   652
    val fields = map (apfst full) bfields;
wenzelm@4890
   653
    val names = map fst fields;
wenzelm@4890
   654
    val types = map snd fields;
wenzelm@4890
   655
    val len = length fields;
wenzelm@8294
   656
    val xs = variantlist (map fst bfields, moreN :: recordN :: parent_xs);
wenzelm@4890
   657
    val vars = ListPair.map Free (xs, types);
wenzelm@4894
   658
    val named_vars = names ~~ vars;
wenzelm@4867
   659
wenzelm@4867
   660
    val all_fields = parent_fields @ fields;
wenzelm@4890
   661
    val all_names = parent_names @ names;
wenzelm@4890
   662
    val all_types = parent_types @ types;
wenzelm@4890
   663
    val all_len = parent_len + len;
wenzelm@4890
   664
    val all_xs = parent_xs @ xs;
wenzelm@4890
   665
    val all_vars = parent_vars @ vars;
wenzelm@4894
   666
    val all_named_vars = parent_named_vars @ named_vars;
wenzelm@4867
   667
wenzelm@4867
   668
    val zeta = variant alphas "'z";
wenzelm@4867
   669
    val moreT = TFree (zeta, moreS);
wenzelm@4895
   670
    val more = Free (moreN, moreT);
wenzelm@5197
   671
    val full_moreN = full moreN;
wenzelm@5197
   672
    fun more_part t = mk_more t full_moreN;
wenzelm@5197
   673
    fun more_part_update t x = mk_more_update t (full_moreN, x);
wenzelm@4894
   674
wenzelm@4894
   675
    val parent_more = funpow parent_len mk_snd;
wenzelm@4894
   676
    val idxs = 0 upto (len - 1);
wenzelm@4867
   677
wenzelm@4867
   678
    val rec_schemeT = mk_recordT (all_fields, moreT);
wenzelm@4894
   679
    val rec_scheme = mk_record (all_named_vars, more);
wenzelm@8294
   680
    val r = Free (recordN, rec_schemeT);
wenzelm@4894
   681
    val recT = mk_recordT (all_fields, HOLogic.unitT);
wenzelm@4867
   682
wenzelm@4867
   683
wenzelm@4890
   684
    (* prepare print translation functions *)
wenzelm@4867
   685
wenzelm@5698
   686
    val field_tr's =
wenzelm@5698
   687
      print_translation (distinct (flat (map NameSpace.accesses (full_moreN :: names))));
wenzelm@4867
   688
wenzelm@4867
   689
wenzelm@4867
   690
    (* prepare declarations *)
wenzelm@4867
   691
wenzelm@5197
   692
    val sel_decls = map (mk_selC rec_schemeT) bfields @
wenzelm@5197
   693
      [mk_moreC rec_schemeT (moreN, moreT)];
wenzelm@5197
   694
    val update_decls = map (mk_updateC rec_schemeT) bfields @
wenzelm@5197
   695
      [mk_more_updateC rec_schemeT (moreN, moreT)];
wenzelm@4890
   696
    val make_decls =
wenzelm@4890
   697
      [(mk_makeC rec_schemeT (make_schemeN, all_types @ [moreT])),
wenzelm@4890
   698
       (mk_makeC recT (makeN, all_types))];
wenzelm@4867
   699
wenzelm@4867
   700
wenzelm@4867
   701
    (* prepare definitions *)
wenzelm@4867
   702
wenzelm@4895
   703
    (*record (scheme) type abbreviation*)
wenzelm@4890
   704
    val recordT_specs =
wenzelm@4890
   705
      [(suffix schemeN bname, alphas @ [zeta], rec_schemeT, Syntax.NoSyn),
wenzelm@4890
   706
        (bname, alphas, recT, Syntax.NoSyn)];
wenzelm@4867
   707
wenzelm@4894
   708
    (*selectors*)
wenzelm@4890
   709
    fun mk_sel_spec (i, c) =
wenzelm@4894
   710
      mk_sel r c :== mk_fst (funpow i mk_snd (parent_more r));
wenzelm@4894
   711
    val sel_specs =
wenzelm@4894
   712
      ListPair.map mk_sel_spec (idxs, names) @
wenzelm@4894
   713
        [more_part r :== funpow len mk_snd (parent_more r)];
wenzelm@4890
   714
wenzelm@4890
   715
    (*updates*)
wenzelm@4894
   716
    val all_sels = all_names ~~ map (mk_sel r) all_names;
wenzelm@4890
   717
    fun mk_upd_spec (i, (c, x)) =
wenzelm@4894
   718
      mk_update r (c, x) :==
wenzelm@4894
   719
        mk_record (nth_update (c, x) (parent_len + i, all_sels), more_part r)
wenzelm@5197
   720
    val update_specs =
wenzelm@5197
   721
      ListPair.map mk_upd_spec (idxs, named_vars) @
wenzelm@5197
   722
        [more_part_update r more :== mk_record (all_sels, more)];
wenzelm@4867
   723
wenzelm@4867
   724
    (*makes*)
wenzelm@4890
   725
    val make_scheme = Const (mk_makeC rec_schemeT (full make_schemeN, all_types @ [moreT]));
wenzelm@4890
   726
    val make = Const (mk_makeC recT (full makeN, all_types));
wenzelm@4867
   727
    val make_specs =
wenzelm@4894
   728
      [list_comb (make_scheme, all_vars) $ more :== rec_scheme,
wenzelm@4894
   729
        list_comb (make, all_vars) :== mk_record (all_named_vars, HOLogic.unit)];
wenzelm@4894
   730
wenzelm@4894
   731
wenzelm@4894
   732
    (* prepare propositions *)
wenzelm@4894
   733
wenzelm@4894
   734
    (*selectors*)
wenzelm@4894
   735
    val sel_props =
wenzelm@4894
   736
      map (fn (c, x) => mk_sel rec_scheme c === x) named_vars @
wenzelm@4894
   737
        [more_part rec_scheme === more];
wenzelm@4894
   738
wenzelm@4894
   739
    (*updates*)
wenzelm@4894
   740
    fun mk_upd_prop (i, (c, T)) =
wenzelm@4894
   741
      let val x' = Free (variant all_xs (base c ^ "'"), T) in
wenzelm@4894
   742
        mk_update rec_scheme (c, x') ===
wenzelm@4894
   743
          mk_record (nth_update (c, x') (parent_len + i, all_named_vars), more)
wenzelm@4894
   744
      end;
wenzelm@5197
   745
    val update_props =
wenzelm@5197
   746
      ListPair.map mk_upd_prop (idxs, fields) @
wenzelm@5197
   747
        let val more' = Free (variant all_xs (moreN ^ "'"), moreT)
wenzelm@5197
   748
        in [more_part_update rec_scheme more' === mk_record (all_named_vars, more')] end;
wenzelm@4867
   749
wenzelm@9626
   750
    (*equality*)
wenzelm@9626
   751
    fun mk_sel_eq (t, T) =
wenzelm@9626
   752
      let val t' = Term.abstract_over (r, t)
wenzelm@9626
   753
      in HOLogic.mk_Trueprop (HOLogic.eq_const T $ Term.incr_boundvars 1 t' $ t') end;
wenzelm@9626
   754
    val sel_eqs = map2 mk_sel_eq (map (mk_sel r) all_names @ [more_part r], all_types @ [moreT]);
wenzelm@9626
   755
    val equality_prop =
wenzelm@9626
   756
      Term.all rec_schemeT $ (Abs ("r", rec_schemeT,
wenzelm@9626
   757
        Term.all rec_schemeT $ (Abs ("r'", rec_schemeT,
wenzelm@9626
   758
          Logic.list_implies (sel_eqs,
wenzelm@9626
   759
            HOLogic.mk_Trueprop (HOLogic.eq_const rec_schemeT $ Bound 1 $ Bound 0))))));
wenzelm@9626
   760
wenzelm@4867
   761
wenzelm@4890
   762
    (* 1st stage: fields_thy *)
wenzelm@4867
   763
wenzelm@5713
   764
    val (fields_thy, field_simps, field_injects, field_splits) =
wenzelm@4867
   765
      thy
wenzelm@4867
   766
      |> Theory.add_path bname
wenzelm@11832
   767
      |> field_definitions fields names xs alphas zeta moreT more vars named_vars;
wenzelm@4867
   768
wenzelm@6092
   769
    val named_splits = map2 (fn (c, th) => (suffix field_typeN c, th)) (names, field_splits);
wenzelm@5698
   770
wenzelm@4867
   771
wenzelm@4890
   772
    (* 2nd stage: defs_thy *)
wenzelm@4890
   773
wenzelm@8428
   774
    val (defs_thy, ((sel_defs, update_defs), make_defs)) =
wenzelm@4890
   775
      fields_thy
wenzelm@9626
   776
      |> add_record_splits named_splits
wenzelm@4890
   777
      |> Theory.parent_path
wenzelm@5197
   778
      |> Theory.add_tyabbrs_i recordT_specs     (*not made part of record name space!*)
wenzelm@4890
   779
      |> Theory.add_path bname
wenzelm@5197
   780
      |> Theory.add_trfuns ([], [], field_tr's, [])
wenzelm@4894
   781
      |> (Theory.add_consts_i o map Syntax.no_syn)
wenzelm@4894
   782
        (sel_decls @ update_decls @ make_decls)
wenzelm@11832
   783
      |> (PureThy.add_defs_i false o map Thm.no_attributes) sel_specs
wenzelm@11832
   784
      |>>> (PureThy.add_defs_i false o map Thm.no_attributes) update_specs
wenzelm@9315
   785
      |>>> (PureThy.add_defs_i false o map Thm.no_attributes) make_specs;
wenzelm@4890
   786
wenzelm@4890
   787
wenzelm@4890
   788
    (* 3rd stage: thms_thy *)
wenzelm@4890
   789
wenzelm@4895
   790
    val parent_simps = flat (map #simps parents);
wenzelm@7178
   791
    val prove = prove_simp (Theory.sign_of defs_thy) HOL_basic_ss [];
wenzelm@9626
   792
    val prove' = prove_simp (Theory.sign_of defs_thy) HOL_ss;
wenzelm@4890
   793
wenzelm@4895
   794
    val sel_convs = map (prove (parent_simps @ sel_defs @ field_simps)) sel_props;
wenzelm@4895
   795
    val update_convs = map (prove (parent_simps @ update_defs @ sel_convs)) update_props;
wenzelm@9626
   796
    val equality =
wenzelm@9626
   797
      prove' [ALLGOALS record_split_tac] (parent_simps @ sel_convs @ field_injects) equality_prop;
wenzelm@4894
   798
wenzelm@9626
   799
    val simps = field_simps @ sel_convs @ update_convs @ make_defs @ [equality];
wenzelm@6519
   800
    val iffs = field_injects;
wenzelm@4867
   801
wenzelm@4867
   802
    val thms_thy =
wenzelm@4867
   803
      defs_thy
wenzelm@8428
   804
      |> (#1 oo (PureThy.add_thmss o map Thm.no_attributes))
wenzelm@4895
   805
        [("select_defs", sel_defs),
wenzelm@4890
   806
          ("update_defs", update_defs),
wenzelm@4894
   807
          ("make_defs", make_defs),
wenzelm@4895
   808
          ("select_convs", sel_convs),
wenzelm@4894
   809
          ("update_convs", update_convs)]
wenzelm@10008
   810
      |> (#1 oo PureThy.add_thms)
wenzelm@10008
   811
          [(("equality", equality), [Classical.xtra_intro_global])]
wenzelm@8428
   812
      |> (#1 oo PureThy.add_thmss)
wenzelm@5707
   813
        [(("simps", simps), [Simplifier.simp_add_global]),
wenzelm@6519
   814
         (("iffs", iffs), [iff_add_global])];
wenzelm@4867
   815
wenzelm@4867
   816
wenzelm@4890
   817
    (* 4th stage: final_thy *)
wenzelm@4867
   818
wenzelm@4867
   819
    val final_thy =
wenzelm@4867
   820
      thms_thy
wenzelm@4895
   821
      |> put_record name {args = args, parent = parent, fields = fields, simps = simps}
wenzelm@9626
   822
      |> put_sel_upd (names @ [full_moreN]) (field_simps @ sel_defs @ update_defs)
wenzelm@4867
   823
      |> Theory.parent_path;
wenzelm@4867
   824
wenzelm@6519
   825
  in (final_thy, {simps = simps, iffs = iffs}) end;
wenzelm@4867
   826
wenzelm@4867
   827
wenzelm@4867
   828
wenzelm@4867
   829
(** theory extender interface **)
wenzelm@4867
   830
wenzelm@4867
   831
(* prepare arguments *)
wenzelm@4867
   832
wenzelm@4894
   833
(*note: read_raw_typ avoids expanding type abbreviations*)
wenzelm@4867
   834
fun read_raw_parent sign s =
wenzelm@4867
   835
  (case Sign.read_raw_typ (sign, K None) s handle TYPE (msg, _, _) => error msg of
wenzelm@4867
   836
    Type (name, Ts) => (Ts, name)
wenzelm@4867
   837
  | _ => error ("Bad parent record specification: " ^ quote s));
wenzelm@4867
   838
wenzelm@4867
   839
fun read_typ sign (env, s) =
wenzelm@4867
   840
  let
wenzelm@5060
   841
    fun def_sort (x, ~1) = assoc (env, x)
wenzelm@5060
   842
      | def_sort _ = None;
wenzelm@5060
   843
    val T = Type.no_tvars (Sign.read_typ (sign, def_sort) s) handle TYPE (msg, _, _) => error msg;
wenzelm@4867
   844
  in (Term.add_typ_tfrees (T, env), T) end;
wenzelm@4867
   845
wenzelm@4867
   846
fun cert_typ sign (env, raw_T) =
wenzelm@4867
   847
  let val T = Type.no_tvars (Sign.certify_typ sign raw_T) handle TYPE (msg, _, _) => error msg
wenzelm@4867
   848
  in (Term.add_typ_tfrees (T, env), T) end;
wenzelm@4867
   849
wenzelm@4867
   850
wenzelm@4867
   851
(* add_record *)
wenzelm@4867
   852
wenzelm@4895
   853
(*we do all preparations and error checks here, deferring the real
wenzelm@4895
   854
  work to record_definition*)
wenzelm@4890
   855
wenzelm@4867
   856
fun gen_add_record prep_typ prep_raw_parent (params, bname) raw_parent raw_fields thy =
wenzelm@4867
   857
  let
paulson@4970
   858
    val _ = Theory.requires thy "Record" "record definitions";
wenzelm@4867
   859
    val sign = Theory.sign_of thy;
wenzelm@5698
   860
    val _ = message ("Defining record " ^ quote bname ^ " ...");
wenzelm@4867
   861
wenzelm@4867
   862
wenzelm@4867
   863
    (* parents *)
wenzelm@4867
   864
wenzelm@4867
   865
    fun prep_inst T = snd (cert_typ sign ([], T));
wenzelm@4867
   866
wenzelm@4867
   867
    val parent = apsome (apfst (map prep_inst) o prep_raw_parent sign) raw_parent
wenzelm@4867
   868
      handle ERROR => error ("The error(s) above in parent record specification");
wenzelm@4867
   869
    val parents = add_parents thy (parent, []);
wenzelm@4867
   870
wenzelm@4867
   871
    val init_env =
wenzelm@4867
   872
      (case parent of
wenzelm@4867
   873
        None => []
wenzelm@4867
   874
      | Some (types, _) => foldr Term.add_typ_tfrees (types, []));
wenzelm@4867
   875
wenzelm@4867
   876
wenzelm@4867
   877
    (* fields *)
wenzelm@4867
   878
wenzelm@4967
   879
    fun prep_field (env, (c, raw_T)) =
wenzelm@4967
   880
      let val (env', T) = prep_typ sign (env, raw_T) handle ERROR =>
wenzelm@4967
   881
        error ("The error(s) above occured in field " ^ quote c)
wenzelm@4967
   882
      in (env', (c, T)) end;
wenzelm@4867
   883
wenzelm@4967
   884
    val (envir, bfields) = foldl_map prep_field (init_env, raw_fields);
wenzelm@4867
   885
    val envir_names = map fst envir;
wenzelm@4867
   886
wenzelm@4867
   887
wenzelm@4867
   888
    (* args *)
wenzelm@4867
   889
wenzelm@4867
   890
    val defaultS = Sign.defaultS sign;
wenzelm@4867
   891
    val args = map (fn x => (x, if_none (assoc (envir, x)) defaultS)) params;
wenzelm@4867
   892
wenzelm@4867
   893
wenzelm@4867
   894
    (* errors *)
wenzelm@4867
   895
wenzelm@4890
   896
    val name = Sign.full_name sign bname;
wenzelm@4890
   897
    val err_dup_record =
wenzelm@4890
   898
      if is_none (get_record thy name) then []
wenzelm@4890
   899
      else ["Duplicate definition of record " ^ quote name];
wenzelm@4890
   900
wenzelm@4867
   901
    val err_dup_parms =
wenzelm@4867
   902
      (case duplicates params of
wenzelm@4867
   903
        [] => []
wenzelm@4890
   904
      | dups => ["Duplicate parameter(s) " ^ commas dups]);
wenzelm@4867
   905
wenzelm@4867
   906
    val err_extra_frees =
wenzelm@4867
   907
      (case gen_rems (op =) (envir_names, params) of
wenzelm@4867
   908
        [] => []
wenzelm@4890
   909
      | extras => ["Extra free type variable(s) " ^ commas extras]);
wenzelm@4867
   910
wenzelm@4890
   911
    val err_no_fields = if null bfields then ["No fields present"] else [];
wenzelm@4867
   912
wenzelm@4867
   913
    val err_dup_fields =
wenzelm@4867
   914
      (case duplicates (map fst bfields) of
wenzelm@4867
   915
        [] => []
wenzelm@4890
   916
      | dups => ["Duplicate field(s) " ^ commas_quote dups]);
wenzelm@4890
   917
wenzelm@4890
   918
    val err_bad_fields =
wenzelm@4890
   919
      if forall (not_equal moreN o fst) bfields then []
wenzelm@4890
   920
      else ["Illegal field name " ^ quote moreN];
wenzelm@4867
   921
wenzelm@4867
   922
    val err_dup_sorts =
wenzelm@4867
   923
      (case duplicates envir_names of
wenzelm@4867
   924
        [] => []
wenzelm@4867
   925
      | dups => ["Inconsistent sort constraints for " ^ commas dups]);
wenzelm@4867
   926
wenzelm@4867
   927
    val errs =
wenzelm@4890
   928
      err_dup_record @ err_dup_parms @ err_extra_frees @ err_no_fields @
wenzelm@4890
   929
      err_dup_fields @ err_bad_fields @ err_dup_sorts;
wenzelm@4867
   930
  in
wenzelm@4890
   931
    if null errs then () else error (cat_lines errs);
wenzelm@4867
   932
    thy |> record_definition (args, bname) parent parents bfields
wenzelm@4867
   933
  end
wenzelm@4867
   934
  handle ERROR => error ("Failed to define record " ^ quote bname);
wenzelm@4867
   935
wenzelm@4867
   936
val add_record = gen_add_record read_typ read_raw_parent;
wenzelm@4867
   937
val add_record_i = gen_add_record cert_typ (K I);
wenzelm@4867
   938
wenzelm@4867
   939
wenzelm@4867
   940
wenzelm@6358
   941
(** package setup **)
wenzelm@6358
   942
wenzelm@6358
   943
(* setup theory *)
wenzelm@4867
   944
wenzelm@4867
   945
val setup =
wenzelm@5006
   946
 [RecordsData.init,
wenzelm@11490
   947
  Theory.add_trfuns ([], parse_translation, [], []),
wenzelm@6358
   948
  Method.add_methods [record_split_method],
wenzelm@7178
   949
  Simplifier.change_simpset_of Simplifier.addsimprocs [record_simproc]];
wenzelm@4867
   950
wenzelm@4867
   951
wenzelm@6358
   952
(* outer syntax *)
wenzelm@6358
   953
wenzelm@6723
   954
local structure P = OuterParse and K = OuterSyntax.Keyword in
wenzelm@6358
   955
wenzelm@6358
   956
val record_decl =
wenzelm@6723
   957
  P.type_args -- P.name -- (P.$$$ "=" |-- Scan.option (P.typ --| P.$$$ "+")
wenzelm@6729
   958
    -- Scan.repeat1 (P.name -- (P.$$$ "::" |-- P.typ) --| P.marg_comment));
wenzelm@6358
   959
wenzelm@6358
   960
val recordP =
wenzelm@6723
   961
  OuterSyntax.command "record" "define extensible record" K.thy_decl
wenzelm@6519
   962
    (record_decl >> (fn (x, (y, z)) => Toplevel.theory (#1 o add_record x y z)));
wenzelm@6358
   963
wenzelm@6358
   964
val _ = OuterSyntax.add_parsers [recordP];
wenzelm@6358
   965
wenzelm@4867
   966
end;
wenzelm@5698
   967
wenzelm@5698
   968
wenzelm@6384
   969
end;
wenzelm@6384
   970
wenzelm@5698
   971
structure BasicRecordPackage: BASIC_RECORD_PACKAGE = RecordPackage;
wenzelm@5698
   972
open BasicRecordPackage;