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