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