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