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