src/HOL/Tools/record_package.ML
author wenzelm
Mon Jul 27 11:29:33 1998 +0200 (1998-07-27)
changeset 5201 fac6fea3b782
parent 5197 69c77ed95ba3
child 5210 54aaa779b6b4
permissions -rw-r--r--
tuned;
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@4867
     4
wenzelm@4867
     5
Extensible records with structural subtyping in HOL.
wenzelm@4867
     6
wenzelm@4867
     7
TODO:
wenzelm@4890
     8
  - field types: typedef;
wenzelm@4894
     9
  - operations and theorems: split, split_all/ex, ...;
wenzelm@5197
    10
  - field constructor: more specific type for snd component (x_more etc. classes);
wenzelm@4867
    11
*)
wenzelm@4867
    12
wenzelm@4867
    13
signature RECORD_PACKAGE =
wenzelm@4867
    14
sig
wenzelm@4890
    15
  val moreS: sort
wenzelm@4890
    16
  val mk_fieldT: (string * typ) * typ -> typ
wenzelm@4890
    17
  val dest_fieldT: typ -> (string * typ) * typ
wenzelm@4890
    18
  val mk_field: (string * term) * term -> term
wenzelm@4890
    19
  val mk_fst: term -> term
wenzelm@4890
    20
  val mk_snd: term -> term
wenzelm@4890
    21
  val mk_recordT: (string * typ) list * typ -> typ
wenzelm@4890
    22
  val dest_recordT: typ -> (string * typ) list * typ
wenzelm@4890
    23
  val mk_record: (string * term) list * term -> term
wenzelm@4890
    24
  val mk_sel: term -> string -> term
wenzelm@4890
    25
  val mk_update: term -> string * term -> term
wenzelm@4867
    26
  val print_records: theory -> unit
wenzelm@4867
    27
  val add_record: (string list * bstring) -> string option
wenzelm@4867
    28
    -> (bstring * string) list -> theory -> theory
wenzelm@4867
    29
  val add_record_i: (string list * bstring) -> (typ list * string) option
wenzelm@4867
    30
    -> (bstring * typ) list -> theory -> theory
wenzelm@4867
    31
  val setup: (theory -> theory) list
wenzelm@4867
    32
end;
wenzelm@4867
    33
wenzelm@4867
    34
structure RecordPackage: RECORD_PACKAGE =
wenzelm@4867
    35
struct
wenzelm@4867
    36
wenzelm@4867
    37
wenzelm@4894
    38
(*** utilities ***)
wenzelm@4867
    39
wenzelm@4894
    40
(* string suffixes *)
wenzelm@4867
    41
wenzelm@4867
    42
fun suffix sfx s = s ^ sfx;
wenzelm@4867
    43
wenzelm@4867
    44
fun unsuffix sfx s =
wenzelm@4867
    45
  let
wenzelm@4867
    46
    val cs = explode s;
wenzelm@4867
    47
    val prfx_len = size s - size sfx;
wenzelm@4867
    48
  in
wenzelm@4867
    49
    if prfx_len >= 0 andalso implode (drop (prfx_len, cs)) = sfx then
wenzelm@4867
    50
      implode (take (prfx_len, cs))
wenzelm@4867
    51
    else raise LIST "unsuffix"
wenzelm@4867
    52
  end;
wenzelm@4867
    53
wenzelm@4867
    54
wenzelm@4894
    55
(* definitions and equations *)
wenzelm@4867
    56
wenzelm@4894
    57
infix 0 :== === ;
wenzelm@4894
    58
wenzelm@4894
    59
val (op :==) = Logic.mk_defpair;
wenzelm@4894
    60
val (op ===) = HOLogic.mk_Trueprop o HOLogic.mk_eq;
wenzelm@4894
    61
wenzelm@4894
    62
fun get_defs thy specs = map (PureThy.get_tthm thy o fst) specs;
wenzelm@4894
    63
wenzelm@4894
    64
wenzelm@4894
    65
(* proof by simplification *)
wenzelm@4890
    66
wenzelm@4895
    67
fun prove_simp thy simps =
wenzelm@4894
    68
  let
wenzelm@4895
    69
    val sign = Theory.sign_of thy;
wenzelm@4895
    70
    val ss = Simplifier.addsimps (HOL_basic_ss, map Attribute.thm_of simps);
wenzelm@4895
    71
wenzelm@4895
    72
    fun prove goal =
wenzelm@4894
    73
      Attribute.tthm_of
wenzelm@4895
    74
        (Goals.prove_goalw_cterm [] (Thm.cterm_of sign goal)
wenzelm@4894
    75
          (K [ALLGOALS (Simplifier.simp_tac ss)])
wenzelm@4894
    76
        handle ERROR => error ("The error(s) above occurred while trying to prove "
wenzelm@4895
    77
          ^ quote (Sign.string_of_term sign goal)));
wenzelm@4894
    78
  in prove end;
wenzelm@4894
    79
wenzelm@4894
    80
wenzelm@4894
    81
wenzelm@4894
    82
(*** syntax operations ***)
wenzelm@4894
    83
wenzelm@4894
    84
(** name components **)
wenzelm@4894
    85
wenzelm@4894
    86
val moreN = "more";
wenzelm@4894
    87
val schemeN = "_scheme";
wenzelm@4894
    88
val fieldN = "_field";
wenzelm@4894
    89
val field_typeN = "_field_type";
wenzelm@4894
    90
val fstN = "_fst";
wenzelm@4894
    91
val sndN = "_snd";
wenzelm@4894
    92
val updateN = "_update";
wenzelm@4894
    93
val makeN = "make";
wenzelm@4894
    94
val make_schemeN = "make_scheme";
wenzelm@4890
    95
wenzelm@4890
    96
wenzelm@4890
    97
wenzelm@4867
    98
(** tuple operations **)
wenzelm@4867
    99
wenzelm@4867
   100
(* more type class *)
wenzelm@4867
   101
wenzelm@4867
   102
val moreS = ["more"];
wenzelm@4867
   103
wenzelm@4867
   104
wenzelm@4867
   105
(* types *)
wenzelm@4867
   106
wenzelm@4867
   107
fun mk_fieldT ((c, T), U) = Type (suffix field_typeN c, [T, U]);
wenzelm@4867
   108
wenzelm@4867
   109
fun dest_fieldT (typ as Type (c_field_type, [T, U])) =
wenzelm@4867
   110
      (case try (unsuffix field_typeN) c_field_type of
wenzelm@4867
   111
        None => raise TYPE ("dest_fieldT", [typ], [])
wenzelm@4867
   112
      | Some c => ((c, T), U))
wenzelm@4867
   113
  | dest_fieldT typ = raise TYPE ("dest_fieldT", [typ], []);
wenzelm@4867
   114
wenzelm@4867
   115
wenzelm@4867
   116
(* constructors *)
wenzelm@4867
   117
wenzelm@4867
   118
fun mk_fieldC U (c, T) = (suffix fieldN c, T --> U --> mk_fieldT ((c, T), U));
wenzelm@4867
   119
wenzelm@4867
   120
fun mk_field ((c, t), u) =
wenzelm@4867
   121
  let val T = fastype_of t and U = fastype_of u
wenzelm@4867
   122
  in Const (suffix fieldN c, [T, U] ---> mk_fieldT ((c, T), U)) $ t $ u end;
wenzelm@4867
   123
wenzelm@4867
   124
wenzelm@4867
   125
(* destructors *)
wenzelm@4867
   126
wenzelm@4867
   127
fun mk_fstC U (c, T) = (suffix fstN c, mk_fieldT ((c, T), U) --> T);
wenzelm@4867
   128
fun mk_sndC U (c, T) = (suffix sndN c, mk_fieldT ((c, T), U) --> U);
wenzelm@4867
   129
wenzelm@4867
   130
fun dest_field fst_or_snd p =
wenzelm@4867
   131
  let
wenzelm@4867
   132
    val pT = fastype_of p;
wenzelm@4867
   133
    val ((c, T), U) = dest_fieldT pT;
wenzelm@4867
   134
    val (destN, destT) = if fst_or_snd then (fstN, T) else (sndN, U);
wenzelm@4867
   135
  in Const (suffix destN c, pT --> destT) $ p end;
wenzelm@4867
   136
wenzelm@4867
   137
val mk_fst = dest_field true;
wenzelm@4867
   138
val mk_snd = dest_field false;
wenzelm@4867
   139
wenzelm@4867
   140
wenzelm@4867
   141
wenzelm@4867
   142
(** record operations **)
wenzelm@4867
   143
wenzelm@4867
   144
(* types *)
wenzelm@4867
   145
wenzelm@4867
   146
val mk_recordT = foldr mk_fieldT;
wenzelm@4867
   147
wenzelm@4867
   148
fun dest_recordT T =
wenzelm@4867
   149
  (case try dest_fieldT T of
wenzelm@4867
   150
    None => ([], T)
wenzelm@4867
   151
  | Some (c_T, U) => apfst (cons c_T) (dest_recordT U));
wenzelm@4867
   152
wenzelm@4867
   153
fun find_fieldT c rT =
wenzelm@4867
   154
  (case assoc (fst (dest_recordT rT), c) of
wenzelm@4867
   155
    None => raise TYPE ("find_field: " ^ c, [rT], [])
wenzelm@4867
   156
  | Some T => T);
wenzelm@4867
   157
wenzelm@4867
   158
wenzelm@4867
   159
(* constructors *)
wenzelm@4867
   160
wenzelm@4867
   161
val mk_record = foldr mk_field;
wenzelm@4867
   162
wenzelm@4867
   163
wenzelm@4867
   164
(* selectors *)
wenzelm@4867
   165
wenzelm@4867
   166
fun mk_selC rT (c, T) = (c, rT --> T);
wenzelm@4867
   167
wenzelm@4890
   168
fun mk_sel r c =
wenzelm@4867
   169
  let val rT = fastype_of r
wenzelm@4867
   170
  in Const (mk_selC rT (c, find_fieldT c rT)) $ r end;
wenzelm@4867
   171
wenzelm@4894
   172
val mk_moreC = mk_selC;
wenzelm@4894
   173
wenzelm@4894
   174
fun mk_more r c =
wenzelm@4894
   175
  let val rT = fastype_of r
wenzelm@4894
   176
  in Const (mk_moreC rT (c, snd (dest_recordT rT))) $ r end;
wenzelm@4894
   177
wenzelm@4867
   178
wenzelm@4867
   179
(* updates *)
wenzelm@4867
   180
wenzelm@4867
   181
fun mk_updateC rT (c, T) = (suffix updateN c, T --> rT --> rT);
wenzelm@4867
   182
wenzelm@4890
   183
fun mk_update r (c, x) =
wenzelm@4867
   184
  let val rT = fastype_of r
wenzelm@4867
   185
  in Const (mk_updateC rT (c, find_fieldT c rT)) $ x $ r end;
wenzelm@4867
   186
wenzelm@5197
   187
val mk_more_updateC = mk_updateC;
wenzelm@5197
   188
wenzelm@5197
   189
fun mk_more_update r (c, x) =
wenzelm@5197
   190
  let val rT = fastype_of r
wenzelm@5197
   191
  in Const (mk_more_updateC rT (c, snd (dest_recordT rT))) $ x $ r end;
wenzelm@5197
   192
wenzelm@4867
   193
wenzelm@4890
   194
(* make *)
wenzelm@4890
   195
wenzelm@4890
   196
fun mk_makeC rT (c, Ts) = (c, Ts ---> rT);
wenzelm@4890
   197
wenzelm@4890
   198
wenzelm@4867
   199
wenzelm@4867
   200
(** concrete syntax for records **)
wenzelm@4867
   201
wenzelm@4867
   202
(* parse translations *)
wenzelm@4867
   203
wenzelm@5197
   204
fun gen_field_tr mark sfx (t as Const (c, _) $ Free (name, _) $ arg) =
wenzelm@5197
   205
      if c = mark then Syntax.const (suffix sfx name) $ arg
wenzelm@5197
   206
      else raise TERM ("gen_field_tr: " ^ mark, [t])
wenzelm@5197
   207
  | gen_field_tr mark _ t = raise TERM ("gen_field_tr: " ^ mark, [t]);
wenzelm@4867
   208
wenzelm@5197
   209
fun gen_fields_tr sep mark sfx (tm as Const (c, _) $ t $ u) =
wenzelm@5197
   210
      if c = sep then gen_field_tr mark sfx t :: gen_fields_tr sep mark sfx u
wenzelm@5201
   211
      else [gen_field_tr mark sfx tm]
wenzelm@5201
   212
  | gen_fields_tr _ mark sfx tm = [gen_field_tr mark sfx tm];
wenzelm@5197
   213
wenzelm@5197
   214
fun gen_record_tr sep mark sfx unit [t] = foldr (op $) (gen_fields_tr sep mark sfx t, unit)
wenzelm@5201
   215
  | gen_record_tr _ _ _ _ ts = raise TERM ("gen_record_tr", ts);
wenzelm@5197
   216
wenzelm@5197
   217
fun gen_record_scheme_tr sep mark sfx [t, more] = foldr (op $) (gen_fields_tr sep mark sfx t, more)
wenzelm@5201
   218
  | gen_record_scheme_tr _ _ _ ts = raise TERM ("gen_record_scheme_tr", ts);
wenzelm@5197
   219
wenzelm@4867
   220
wenzelm@5197
   221
val record_type_tr = gen_record_tr "_field_types" "_field_type" field_typeN (Syntax.const "unit");
wenzelm@5197
   222
val record_type_scheme_tr = gen_record_scheme_tr "_field_types" "_field_type" field_typeN;
wenzelm@5197
   223
wenzelm@5197
   224
val record_tr = gen_record_tr "_fields" "_field" fieldN HOLogic.unit;
wenzelm@5197
   225
val record_scheme_tr = gen_record_scheme_tr "_fields" "_field" fieldN;
wenzelm@4867
   226
wenzelm@5197
   227
fun record_update_tr [t, u] =
wenzelm@5197
   228
      foldr (op $) (rev (gen_fields_tr "_updates" "_update" updateN u), t)
wenzelm@5197
   229
  | record_update_tr ts = raise TERM ("record_update_tr", ts);
wenzelm@5197
   230
wenzelm@5197
   231
wenzelm@5197
   232
val parse_translation =
wenzelm@5197
   233
 [("_record_type", record_type_tr),
wenzelm@5197
   234
  ("_record_type_scheme", record_type_scheme_tr),
wenzelm@5197
   235
  ("_record", record_tr),
wenzelm@5197
   236
  ("_record_scheme", record_scheme_tr),
wenzelm@5197
   237
  ("_record_update", record_update_tr)];
wenzelm@4867
   238
wenzelm@4867
   239
wenzelm@4890
   240
(* print translations *)
wenzelm@4867
   241
wenzelm@5197
   242
fun gen_fields_tr' mark sfx (tm as Const (name_field, _) $ t $ u) =
wenzelm@5197
   243
      (case try (unsuffix sfx) name_field of
wenzelm@5197
   244
        Some name =>
wenzelm@5197
   245
          apfst (cons (Syntax.const mark $ Syntax.free name $ t)) (gen_fields_tr' mark sfx u)
wenzelm@5197
   246
      | None => ([], tm))
wenzelm@5197
   247
  | gen_fields_tr' _ _ tm = ([], tm);
wenzelm@5197
   248
wenzelm@5197
   249
fun gen_record_tr' sep mark sfx is_unit record record_scheme tm =
wenzelm@4867
   250
  let
wenzelm@5197
   251
    val (ts, u) = gen_fields_tr' mark sfx tm;
wenzelm@5197
   252
    val t' = foldr1 (fn (v, w) => Syntax.const sep $ v $ w) ts;
wenzelm@4867
   253
  in
wenzelm@5197
   254
    if is_unit u then Syntax.const record $ t'
wenzelm@5197
   255
    else Syntax.const record_scheme $ t' $ u
wenzelm@4867
   256
  end;
wenzelm@4867
   257
wenzelm@5197
   258
wenzelm@5197
   259
val record_type_tr' =
wenzelm@5197
   260
  gen_record_tr' "_field_types" "_field_type" field_typeN
wenzelm@5197
   261
    (fn Const ("unit", _) => true | _ => false) "_record_type" "_record_type_scheme";
wenzelm@5197
   262
wenzelm@5197
   263
val record_tr' =
wenzelm@5197
   264
  gen_record_tr' "_fields" "_field" fieldN HOLogic.is_unit "_record" "_record_scheme";
wenzelm@5197
   265
wenzelm@5197
   266
fun record_update_tr' tm =
wenzelm@5197
   267
  let val (ts, u) = gen_fields_tr' "_update" updateN tm in
wenzelm@5197
   268
    Syntax.const "_record_update" $ u $
wenzelm@5197
   269
      foldr1 (fn (v, w) => Syntax.const "_updates" $ v $ w) (rev ts)
wenzelm@5197
   270
  end;
wenzelm@5197
   271
wenzelm@5197
   272
wenzelm@5201
   273
fun gen_field_tr' sfx tr' name =
wenzelm@5201
   274
  let val name_sfx = suffix sfx name
wenzelm@5201
   275
  in (name_sfx, fn [t, u] => tr' (Syntax.const name_sfx $ t $ u) | _ => raise Match) end;
wenzelm@5201
   276
wenzelm@5197
   277
fun print_translation names =
wenzelm@5197
   278
  map (gen_field_tr' field_typeN record_type_tr') names @
wenzelm@5197
   279
  map (gen_field_tr' fieldN record_tr') names @
wenzelm@5197
   280
  map (gen_field_tr' updateN record_update_tr') names;
wenzelm@4867
   281
wenzelm@4867
   282
wenzelm@4867
   283
wenzelm@4867
   284
(*** extend theory by record definition ***)
wenzelm@4867
   285
wenzelm@4867
   286
(** record info **)
wenzelm@4867
   287
wenzelm@4867
   288
(* type record_info and parent_info *)
wenzelm@4867
   289
wenzelm@4867
   290
type record_info =
wenzelm@4867
   291
 {args: (string * sort) list,
wenzelm@4867
   292
  parent: (typ list * string) option,
wenzelm@4867
   293
  fields: (string * typ) list,
wenzelm@4895
   294
  simps: tthm list};
wenzelm@4867
   295
wenzelm@4867
   296
type parent_info =
wenzelm@4867
   297
 {name: string,
wenzelm@4867
   298
  fields: (string * typ) list,
wenzelm@4895
   299
  simps: tthm list};
wenzelm@4867
   300
wenzelm@4867
   301
wenzelm@5052
   302
(* data kind 'HOL/records' *)
wenzelm@5001
   303
wenzelm@5006
   304
structure RecordsArgs =
wenzelm@5006
   305
struct
wenzelm@5006
   306
  val name = "HOL/records";
wenzelm@5006
   307
  type T = record_info Symtab.table;
wenzelm@4867
   308
wenzelm@5006
   309
  val empty = Symtab.empty;
wenzelm@5006
   310
  val prep_ext = I;
wenzelm@5006
   311
  val merge: T * T -> T = Symtab.merge (K true);
wenzelm@4867
   312
wenzelm@5006
   313
  fun print sg tab =
wenzelm@4867
   314
    let
wenzelm@4867
   315
      val prt_typ = Sign.pretty_typ sg;
wenzelm@4867
   316
      val ext_const = Sign.cond_extern sg Sign.constK;
wenzelm@4867
   317
wenzelm@4867
   318
      fun pretty_parent None = []
wenzelm@4867
   319
        | pretty_parent (Some (Ts, name)) =
wenzelm@4867
   320
            [Pretty.block [prt_typ (Type (name, Ts)), Pretty.str " +"]];
wenzelm@4867
   321
wenzelm@4867
   322
      fun pretty_field (c, T) = Pretty.block
wenzelm@4867
   323
        [Pretty.str (ext_const c), Pretty.str " ::", Pretty.brk 1, Pretty.quote (prt_typ T)];
wenzelm@4867
   324
wenzelm@4895
   325
      fun pretty_record (name, {args, parent, fields, simps = _}) = Pretty.block (Pretty.fbreaks
wenzelm@4867
   326
        (Pretty.block [prt_typ (Type (name, map TFree args)), Pretty.str " = "] ::
wenzelm@4867
   327
          pretty_parent parent @ map pretty_field fields));
wenzelm@4867
   328
    in
wenzelm@4867
   329
      seq (Pretty.writeln o pretty_record) (Symtab.dest tab)
wenzelm@4867
   330
    end;
wenzelm@4867
   331
end;
wenzelm@4867
   332
wenzelm@5006
   333
structure RecordsData = TheoryDataFun(RecordsArgs);
wenzelm@5006
   334
val print_records = RecordsData.print;
wenzelm@5006
   335
wenzelm@4867
   336
wenzelm@4867
   337
(* get and put records *)
wenzelm@4867
   338
wenzelm@5006
   339
fun get_record thy name = Symtab.lookup (RecordsData.get thy, name);
wenzelm@4867
   340
wenzelm@4890
   341
fun put_record name info thy =
wenzelm@5006
   342
  RecordsData.put (Symtab.update ((name, info), RecordsData.get thy)) thy;
wenzelm@4867
   343
wenzelm@4867
   344
wenzelm@4867
   345
(* parent records *)
wenzelm@4867
   346
wenzelm@4867
   347
fun inst_record thy (types, name) =
wenzelm@4867
   348
  let
wenzelm@4867
   349
    val sign = Theory.sign_of thy;
wenzelm@4867
   350
    fun err msg = error (msg ^ " parent record " ^ quote name);
wenzelm@4867
   351
wenzelm@4895
   352
    val {args, parent, fields, simps} =
wenzelm@4867
   353
      (case get_record thy name of Some info => info | None => err "Unknown");
wenzelm@4895
   354
    val _ = if length types <> length args then err "Bad number of arguments for" else ();
wenzelm@4867
   355
wenzelm@4867
   356
    fun bad_inst ((x, S), T) =
wenzelm@4867
   357
      if Sign.of_sort sign (T, S) then None else Some x
wenzelm@4867
   358
    val bads = mapfilter bad_inst (args ~~ types);
wenzelm@4867
   359
wenzelm@4867
   360
    val inst = map fst args ~~ types;
wenzelm@4867
   361
    val subst = Term.map_type_tfree (fn (x, _) => the (assoc (inst, x)));
wenzelm@4867
   362
  in
wenzelm@4895
   363
    if not (null bads) then
wenzelm@4867
   364
      err ("Ill-sorted instantiation of " ^ commas bads ^ " in")
wenzelm@4895
   365
    else (apsome (apfst (map subst)) parent, map (apsnd subst) fields, simps)
wenzelm@4867
   366
  end;
wenzelm@4867
   367
wenzelm@4867
   368
fun add_parents thy (None, parents) = parents
wenzelm@4867
   369
  | add_parents thy (Some (types, name), parents) =
wenzelm@4895
   370
      let val (pparent, pfields, psimps) = inst_record thy (types, name)
wenzelm@4895
   371
      in add_parents thy (pparent, {name = name, fields = pfields, simps = psimps} :: parents) end;
wenzelm@4867
   372
wenzelm@4867
   373
wenzelm@4867
   374
wenzelm@4890
   375
(** internal theory extenders **)
wenzelm@4867
   376
wenzelm@4894
   377
(* field_definitions *)
wenzelm@4867
   378
wenzelm@4894
   379
(*theorems from Prod.thy*)
wenzelm@4890
   380
val prod_convs = map Attribute.tthm_of [fst_conv, snd_conv];
wenzelm@4890
   381
wenzelm@4890
   382
wenzelm@4894
   383
fun field_definitions fields names zeta moreT more vars named_vars thy =
wenzelm@4890
   384
  let
wenzelm@4890
   385
    val base = Sign.base_name;
wenzelm@4867
   386
wenzelm@4867
   387
wenzelm@4890
   388
    (* prepare declarations and definitions *)
wenzelm@4867
   389
wenzelm@4890
   390
    (*field types*)
wenzelm@4890
   391
    fun mk_fieldT_spec c =
wenzelm@4890
   392
      (suffix field_typeN c, ["'a", zeta],
wenzelm@4890
   393
        HOLogic.mk_prodT (TFree ("'a", HOLogic.termS), moreT), Syntax.NoSyn);
wenzelm@4890
   394
    val fieldT_specs = map (mk_fieldT_spec o base) names;
wenzelm@4890
   395
wenzelm@4890
   396
    (*field declarations*)
wenzelm@4890
   397
    val field_decls = map (mk_fieldC moreT) fields;
wenzelm@4890
   398
    val dest_decls = map (mk_fstC moreT) fields @ map (mk_sndC moreT) fields;
wenzelm@4890
   399
wenzelm@4890
   400
    (*field constructors*)
wenzelm@4890
   401
    fun mk_field_spec (c, v) =
wenzelm@4894
   402
      mk_field ((c, v), more) :== HOLogic.mk_prod (v, more);
wenzelm@4894
   403
    val field_specs = map mk_field_spec named_vars;
wenzelm@4890
   404
wenzelm@4890
   405
    (*field destructors*)
wenzelm@4890
   406
    fun mk_dest_spec dest dest' (c, T) =
wenzelm@4890
   407
      let
wenzelm@4895
   408
        val p = Free ("p", mk_fieldT ((c, T), moreT));
wenzelm@4895
   409
        val p' = Free ("p", HOLogic.mk_prodT (T, moreT));
wenzelm@4894
   410
          (*note: field types are just abbreviations*)
wenzelm@4894
   411
      in dest p :== dest' p' end;
wenzelm@4890
   412
    val dest_specs =
wenzelm@4890
   413
      map (mk_dest_spec mk_fst HOLogic.mk_fst) fields @
wenzelm@4890
   414
      map (mk_dest_spec mk_snd HOLogic.mk_snd) fields;
wenzelm@4890
   415
wenzelm@4890
   416
wenzelm@4890
   417
    (* prepare theorems *)
wenzelm@4894
   418
wenzelm@4890
   419
    fun mk_dest_prop dest dest' (c, v) =
wenzelm@4894
   420
      dest (mk_field ((c, v), more)) === dest' (v, more);
wenzelm@4890
   421
    val dest_props =
wenzelm@4895
   422
      map (mk_dest_prop mk_fst fst) named_vars @
wenzelm@4895
   423
      map (mk_dest_prop mk_snd snd) named_vars;
wenzelm@4890
   424
wenzelm@4890
   425
wenzelm@4890
   426
    (* 1st stage: defs_thy *)
wenzelm@4890
   427
wenzelm@4890
   428
    val defs_thy =
wenzelm@4890
   429
      thy
wenzelm@4890
   430
      |> Theory.add_tyabbrs_i fieldT_specs
wenzelm@4890
   431
      |> (Theory.add_consts_i o map (Syntax.no_syn o apfst base))
wenzelm@4890
   432
        (field_decls @ dest_decls)
wenzelm@5197
   433
      |> (PureThy.add_defs_i o map (fn x => (x, [Attribute.tag_internal])))
wenzelm@4890
   434
        (field_specs @ dest_specs);
wenzelm@4890
   435
wenzelm@4890
   436
    val field_defs = get_defs defs_thy field_specs;
wenzelm@4890
   437
    val dest_defs = get_defs defs_thy dest_specs;
wenzelm@4890
   438
wenzelm@4890
   439
wenzelm@4890
   440
    (* 2nd stage: thms_thy *)
wenzelm@4890
   441
wenzelm@4894
   442
    val dest_convs =
wenzelm@4895
   443
      map (prove_simp defs_thy (field_defs @ dest_defs @ prod_convs)) dest_props;
wenzelm@4894
   444
wenzelm@4890
   445
    val thms_thy =
wenzelm@4890
   446
      defs_thy
wenzelm@4890
   447
      |> (PureThy.add_tthmss o map Attribute.none)
wenzelm@4890
   448
        [("field_defs", field_defs),
wenzelm@4890
   449
          ("dest_defs", dest_defs),
wenzelm@4890
   450
          ("dest_convs", dest_convs)];
wenzelm@4890
   451
wenzelm@4890
   452
  in (thms_thy, dest_convs) end;
wenzelm@4890
   453
wenzelm@4890
   454
wenzelm@4890
   455
(* record_definition *)
wenzelm@4867
   456
wenzelm@4867
   457
fun record_definition (args, bname) parent (parents: parent_info list) bfields thy =
wenzelm@4867
   458
  let
wenzelm@4867
   459
    val sign = Theory.sign_of thy;
wenzelm@4867
   460
    val full = Sign.full_name_path sign bname;
wenzelm@4890
   461
    val base = Sign.base_name;
wenzelm@4867
   462
wenzelm@4867
   463
wenzelm@4890
   464
    (* basic components *)
wenzelm@4867
   465
wenzelm@4867
   466
    val alphas = map fst args;
wenzelm@5197
   467
    val name = Sign.full_name sign bname;       (*not made part of record name space!*)
wenzelm@4890
   468
wenzelm@4867
   469
    val parent_fields = flat (map #fields parents);
wenzelm@4890
   470
    val parent_names = map fst parent_fields;
wenzelm@4890
   471
    val parent_types = map snd parent_fields;
wenzelm@4890
   472
    val parent_len = length parent_fields;
wenzelm@4890
   473
    val parent_xs = variantlist (map (base o fst) parent_fields, [moreN]);
wenzelm@4890
   474
    val parent_vars = ListPair.map Free (parent_xs, parent_types);
wenzelm@4894
   475
    val parent_named_vars = parent_names ~~ parent_vars;
wenzelm@4890
   476
wenzelm@4867
   477
    val fields = map (apfst full) bfields;
wenzelm@4890
   478
    val names = map fst fields;
wenzelm@4890
   479
    val types = map snd fields;
wenzelm@4890
   480
    val len = length fields;
wenzelm@4890
   481
    val xs = variantlist (map fst bfields, moreN :: parent_xs);
wenzelm@4890
   482
    val vars = ListPair.map Free (xs, types);
wenzelm@4894
   483
    val named_vars = names ~~ vars;
wenzelm@4867
   484
wenzelm@4867
   485
    val all_fields = parent_fields @ fields;
wenzelm@4890
   486
    val all_names = parent_names @ names;
wenzelm@4890
   487
    val all_types = parent_types @ types;
wenzelm@4890
   488
    val all_len = parent_len + len;
wenzelm@4890
   489
    val all_xs = parent_xs @ xs;
wenzelm@4890
   490
    val all_vars = parent_vars @ vars;
wenzelm@4894
   491
    val all_named_vars = parent_named_vars @ named_vars;
wenzelm@4867
   492
wenzelm@4867
   493
    val zeta = variant alphas "'z";
wenzelm@4867
   494
    val moreT = TFree (zeta, moreS);
wenzelm@4895
   495
    val more = Free (moreN, moreT);
wenzelm@5197
   496
    val full_moreN = full moreN;
wenzelm@5197
   497
    fun more_part t = mk_more t full_moreN;
wenzelm@5197
   498
    fun more_part_update t x = mk_more_update t (full_moreN, x);
wenzelm@4894
   499
wenzelm@4894
   500
    val parent_more = funpow parent_len mk_snd;
wenzelm@4894
   501
    val idxs = 0 upto (len - 1);
wenzelm@4867
   502
wenzelm@4867
   503
    val rec_schemeT = mk_recordT (all_fields, moreT);
wenzelm@4894
   504
    val rec_scheme = mk_record (all_named_vars, more);
wenzelm@4890
   505
    val r = Free ("r", rec_schemeT);
wenzelm@4894
   506
    val recT = mk_recordT (all_fields, HOLogic.unitT);
wenzelm@4867
   507
wenzelm@4867
   508
wenzelm@4890
   509
    (* prepare print translation functions *)
wenzelm@4867
   510
wenzelm@5197
   511
    val accesses = distinct (flat (map NameSpace.accesses (full_moreN :: names)));
wenzelm@5197
   512
    val (_, _, tr'_names, _) = Syntax.trfun_names (Theory.syn_of thy);
wenzelm@5197
   513
    val field_tr's = filter_out (fn (c, _) => c mem tr'_names) (print_translation accesses);
wenzelm@4867
   514
wenzelm@4867
   515
wenzelm@4867
   516
    (* prepare declarations *)
wenzelm@4867
   517
wenzelm@5197
   518
    val sel_decls = map (mk_selC rec_schemeT) bfields @
wenzelm@5197
   519
      [mk_moreC rec_schemeT (moreN, moreT)];
wenzelm@5197
   520
    val update_decls = map (mk_updateC rec_schemeT) bfields @
wenzelm@5197
   521
      [mk_more_updateC rec_schemeT (moreN, moreT)];
wenzelm@4890
   522
    val make_decls =
wenzelm@4890
   523
      [(mk_makeC rec_schemeT (make_schemeN, all_types @ [moreT])),
wenzelm@4890
   524
       (mk_makeC recT (makeN, all_types))];
wenzelm@4867
   525
wenzelm@4867
   526
wenzelm@4867
   527
    (* prepare definitions *)
wenzelm@4867
   528
wenzelm@4895
   529
    (*record (scheme) type abbreviation*)
wenzelm@4890
   530
    val recordT_specs =
wenzelm@4890
   531
      [(suffix schemeN bname, alphas @ [zeta], rec_schemeT, Syntax.NoSyn),
wenzelm@4890
   532
        (bname, alphas, recT, Syntax.NoSyn)];
wenzelm@4867
   533
wenzelm@4894
   534
    (*selectors*)
wenzelm@4890
   535
    fun mk_sel_spec (i, c) =
wenzelm@4894
   536
      mk_sel r c :== mk_fst (funpow i mk_snd (parent_more r));
wenzelm@4894
   537
    val sel_specs =
wenzelm@4894
   538
      ListPair.map mk_sel_spec (idxs, names) @
wenzelm@4894
   539
        [more_part r :== funpow len mk_snd (parent_more r)];
wenzelm@4890
   540
wenzelm@4890
   541
    (*updates*)
wenzelm@4894
   542
    val all_sels = all_names ~~ map (mk_sel r) all_names;
wenzelm@4890
   543
    fun mk_upd_spec (i, (c, x)) =
wenzelm@4894
   544
      mk_update r (c, x) :==
wenzelm@4894
   545
        mk_record (nth_update (c, x) (parent_len + i, all_sels), more_part r)
wenzelm@5197
   546
    val update_specs =
wenzelm@5197
   547
      ListPair.map mk_upd_spec (idxs, named_vars) @
wenzelm@5197
   548
        [more_part_update r more :== mk_record (all_sels, more)];
wenzelm@4867
   549
wenzelm@4867
   550
    (*makes*)
wenzelm@4890
   551
    val make_scheme = Const (mk_makeC rec_schemeT (full make_schemeN, all_types @ [moreT]));
wenzelm@4890
   552
    val make = Const (mk_makeC recT (full makeN, all_types));
wenzelm@4867
   553
    val make_specs =
wenzelm@4894
   554
      [list_comb (make_scheme, all_vars) $ more :== rec_scheme,
wenzelm@4894
   555
        list_comb (make, all_vars) :== mk_record (all_named_vars, HOLogic.unit)];
wenzelm@4894
   556
wenzelm@4894
   557
wenzelm@4894
   558
    (* prepare propositions *)
wenzelm@4894
   559
wenzelm@4894
   560
    (*selectors*)
wenzelm@4894
   561
    val sel_props =
wenzelm@4894
   562
      map (fn (c, x) => mk_sel rec_scheme c === x) named_vars @
wenzelm@4894
   563
        [more_part rec_scheme === more];
wenzelm@4894
   564
wenzelm@4894
   565
    (*updates*)
wenzelm@4894
   566
    fun mk_upd_prop (i, (c, T)) =
wenzelm@4894
   567
      let val x' = Free (variant all_xs (base c ^ "'"), T) in
wenzelm@4894
   568
        mk_update rec_scheme (c, x') ===
wenzelm@4894
   569
          mk_record (nth_update (c, x') (parent_len + i, all_named_vars), more)
wenzelm@4894
   570
      end;
wenzelm@5197
   571
    val update_props =
wenzelm@5197
   572
      ListPair.map mk_upd_prop (idxs, fields) @
wenzelm@5197
   573
        let val more' = Free (variant all_xs (moreN ^ "'"), moreT)
wenzelm@5197
   574
        in [more_part_update rec_scheme more' === mk_record (all_named_vars, more')] end;
wenzelm@4867
   575
wenzelm@4867
   576
wenzelm@4890
   577
    (* 1st stage: fields_thy *)
wenzelm@4867
   578
wenzelm@4890
   579
    val (fields_thy, field_simps) =
wenzelm@4867
   580
      thy
wenzelm@4867
   581
      |> Theory.add_path bname
wenzelm@4894
   582
      |> field_definitions fields names zeta moreT more vars named_vars;
wenzelm@4867
   583
wenzelm@4867
   584
wenzelm@4890
   585
    (* 2nd stage: defs_thy *)
wenzelm@4890
   586
wenzelm@4890
   587
    val defs_thy =
wenzelm@4890
   588
      fields_thy
wenzelm@4890
   589
      |> Theory.parent_path
wenzelm@5197
   590
      |> Theory.add_tyabbrs_i recordT_specs     (*not made part of record name space!*)
wenzelm@4890
   591
      |> Theory.add_path bname
wenzelm@5197
   592
      |> Theory.add_trfuns ([], [], field_tr's, [])
wenzelm@4894
   593
      |> (Theory.add_consts_i o map Syntax.no_syn)
wenzelm@4894
   594
        (sel_decls @ update_decls @ make_decls)
wenzelm@5197
   595
      |> (PureThy.add_defs_i o map (fn x => (x, [Attribute.tag_internal])))
wenzelm@4894
   596
        (sel_specs @ update_specs @ make_specs);
wenzelm@4890
   597
wenzelm@4890
   598
    val sel_defs = get_defs defs_thy sel_specs;
wenzelm@4890
   599
    val update_defs = get_defs defs_thy update_specs;
wenzelm@4890
   600
    val make_defs = get_defs defs_thy make_specs;
wenzelm@4890
   601
wenzelm@4890
   602
wenzelm@4890
   603
    (* 3rd stage: thms_thy *)
wenzelm@4890
   604
wenzelm@4895
   605
    val parent_simps = flat (map #simps parents);
wenzelm@4895
   606
    val prove = prove_simp defs_thy;
wenzelm@4890
   607
wenzelm@4895
   608
    val sel_convs = map (prove (parent_simps @ sel_defs @ field_simps)) sel_props;
wenzelm@4895
   609
    val update_convs = map (prove (parent_simps @ update_defs @ sel_convs)) update_props;
wenzelm@4894
   610
wenzelm@4894
   611
    val simps = field_simps @ sel_convs @ update_convs @ make_defs;
wenzelm@4867
   612
wenzelm@4867
   613
    val thms_thy =
wenzelm@4867
   614
      defs_thy
wenzelm@4867
   615
      |> (PureThy.add_tthmss o map Attribute.none)
wenzelm@4895
   616
        [("select_defs", sel_defs),
wenzelm@4890
   617
          ("update_defs", update_defs),
wenzelm@4894
   618
          ("make_defs", make_defs),
wenzelm@4895
   619
          ("select_convs", sel_convs),
wenzelm@4894
   620
          ("update_convs", update_convs)]
wenzelm@4894
   621
      |> PureThy.add_tthmss [(("simps", simps), [Simplifier.simp_add_global])];
wenzelm@4867
   622
wenzelm@4867
   623
wenzelm@4890
   624
    (* 4th stage: final_thy *)
wenzelm@4867
   625
wenzelm@4867
   626
    val final_thy =
wenzelm@4867
   627
      thms_thy
wenzelm@4895
   628
      |> put_record name {args = args, parent = parent, fields = fields, simps = simps}
wenzelm@4867
   629
      |> Theory.parent_path;
wenzelm@4867
   630
wenzelm@4867
   631
  in final_thy end;
wenzelm@4867
   632
wenzelm@4867
   633
wenzelm@4867
   634
wenzelm@4867
   635
(** theory extender interface **)
wenzelm@4867
   636
wenzelm@4867
   637
(* prepare arguments *)
wenzelm@4867
   638
wenzelm@4894
   639
(*note: read_raw_typ avoids expanding type abbreviations*)
wenzelm@4867
   640
fun read_raw_parent sign s =
wenzelm@4867
   641
  (case Sign.read_raw_typ (sign, K None) s handle TYPE (msg, _, _) => error msg of
wenzelm@4867
   642
    Type (name, Ts) => (Ts, name)
wenzelm@4867
   643
  | _ => error ("Bad parent record specification: " ^ quote s));
wenzelm@4867
   644
wenzelm@4867
   645
fun read_typ sign (env, s) =
wenzelm@4867
   646
  let
wenzelm@5060
   647
    fun def_sort (x, ~1) = assoc (env, x)
wenzelm@5060
   648
      | def_sort _ = None;
wenzelm@5060
   649
    val T = Type.no_tvars (Sign.read_typ (sign, def_sort) s) handle TYPE (msg, _, _) => error msg;
wenzelm@4867
   650
  in (Term.add_typ_tfrees (T, env), T) end;
wenzelm@4867
   651
wenzelm@4867
   652
fun cert_typ sign (env, raw_T) =
wenzelm@4867
   653
  let val T = Type.no_tvars (Sign.certify_typ sign raw_T) handle TYPE (msg, _, _) => error msg
wenzelm@4867
   654
  in (Term.add_typ_tfrees (T, env), T) end;
wenzelm@4867
   655
wenzelm@4867
   656
wenzelm@4867
   657
(* add_record *)
wenzelm@4867
   658
wenzelm@4895
   659
(*we do all preparations and error checks here, deferring the real
wenzelm@4895
   660
  work to record_definition*)
wenzelm@4890
   661
wenzelm@4867
   662
fun gen_add_record prep_typ prep_raw_parent (params, bname) raw_parent raw_fields thy =
wenzelm@4867
   663
  let
paulson@4970
   664
    val _ = Theory.requires thy "Record" "record definitions";
wenzelm@4867
   665
    val sign = Theory.sign_of thy;
wenzelm@4895
   666
    val _ = writeln ("Defining record " ^ quote bname ^ " ...");
wenzelm@4867
   667
wenzelm@4867
   668
wenzelm@4867
   669
    (* parents *)
wenzelm@4867
   670
wenzelm@4867
   671
    fun prep_inst T = snd (cert_typ sign ([], T));
wenzelm@4867
   672
wenzelm@4867
   673
    val parent = apsome (apfst (map prep_inst) o prep_raw_parent sign) raw_parent
wenzelm@4867
   674
      handle ERROR => error ("The error(s) above in parent record specification");
wenzelm@4867
   675
    val parents = add_parents thy (parent, []);
wenzelm@4867
   676
wenzelm@4867
   677
    val init_env =
wenzelm@4867
   678
      (case parent of
wenzelm@4867
   679
        None => []
wenzelm@4867
   680
      | Some (types, _) => foldr Term.add_typ_tfrees (types, []));
wenzelm@4867
   681
wenzelm@4867
   682
wenzelm@4867
   683
    (* fields *)
wenzelm@4867
   684
wenzelm@4967
   685
    fun prep_field (env, (c, raw_T)) =
wenzelm@4967
   686
      let val (env', T) = prep_typ sign (env, raw_T) handle ERROR =>
wenzelm@4967
   687
        error ("The error(s) above occured in field " ^ quote c)
wenzelm@4967
   688
      in (env', (c, T)) end;
wenzelm@4867
   689
wenzelm@4967
   690
    val (envir, bfields) = foldl_map prep_field (init_env, raw_fields);
wenzelm@4867
   691
    val envir_names = map fst envir;
wenzelm@4867
   692
wenzelm@4867
   693
wenzelm@4867
   694
    (* args *)
wenzelm@4867
   695
wenzelm@4867
   696
    val defaultS = Sign.defaultS sign;
wenzelm@4867
   697
    val args = map (fn x => (x, if_none (assoc (envir, x)) defaultS)) params;
wenzelm@4867
   698
wenzelm@4867
   699
wenzelm@4867
   700
    (* errors *)
wenzelm@4867
   701
wenzelm@4890
   702
    val name = Sign.full_name sign bname;
wenzelm@4890
   703
    val err_dup_record =
wenzelm@4890
   704
      if is_none (get_record thy name) then []
wenzelm@4890
   705
      else ["Duplicate definition of record " ^ quote name];
wenzelm@4890
   706
wenzelm@4867
   707
    val err_dup_parms =
wenzelm@4867
   708
      (case duplicates params of
wenzelm@4867
   709
        [] => []
wenzelm@4890
   710
      | dups => ["Duplicate parameter(s) " ^ commas dups]);
wenzelm@4867
   711
wenzelm@4867
   712
    val err_extra_frees =
wenzelm@4867
   713
      (case gen_rems (op =) (envir_names, params) of
wenzelm@4867
   714
        [] => []
wenzelm@4890
   715
      | extras => ["Extra free type variable(s) " ^ commas extras]);
wenzelm@4867
   716
wenzelm@4890
   717
    val err_no_fields = if null bfields then ["No fields present"] else [];
wenzelm@4867
   718
wenzelm@4867
   719
    val err_dup_fields =
wenzelm@4867
   720
      (case duplicates (map fst bfields) of
wenzelm@4867
   721
        [] => []
wenzelm@4890
   722
      | dups => ["Duplicate field(s) " ^ commas_quote dups]);
wenzelm@4890
   723
wenzelm@4890
   724
    val err_bad_fields =
wenzelm@4890
   725
      if forall (not_equal moreN o fst) bfields then []
wenzelm@4890
   726
      else ["Illegal field name " ^ quote moreN];
wenzelm@4867
   727
wenzelm@4867
   728
    val err_dup_sorts =
wenzelm@4867
   729
      (case duplicates envir_names of
wenzelm@4867
   730
        [] => []
wenzelm@4867
   731
      | dups => ["Inconsistent sort constraints for " ^ commas dups]);
wenzelm@4867
   732
wenzelm@4867
   733
    val errs =
wenzelm@4890
   734
      err_dup_record @ err_dup_parms @ err_extra_frees @ err_no_fields @
wenzelm@4890
   735
      err_dup_fields @ err_bad_fields @ err_dup_sorts;
wenzelm@4867
   736
  in
wenzelm@4890
   737
    if null errs then () else error (cat_lines errs);
wenzelm@4867
   738
    thy |> record_definition (args, bname) parent parents bfields
wenzelm@4867
   739
  end
wenzelm@4867
   740
  handle ERROR => error ("Failed to define record " ^ quote bname);
wenzelm@4867
   741
wenzelm@4867
   742
val add_record = gen_add_record read_typ read_raw_parent;
wenzelm@4867
   743
val add_record_i = gen_add_record cert_typ (K I);
wenzelm@4867
   744
wenzelm@4867
   745
wenzelm@4867
   746
wenzelm@4867
   747
(** setup theory **)
wenzelm@4867
   748
wenzelm@4867
   749
val setup =
wenzelm@5006
   750
 [RecordsData.init,
wenzelm@5197
   751
  Theory.add_trfuns ([], parse_translation, [], [])];
wenzelm@4867
   752
wenzelm@4867
   753
wenzelm@4867
   754
end;