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