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