src/HOL/Tools/record_package.ML
author schirmer
Sun, 18 Jul 2004 12:01:08 +0200
changeset 15059 66ded85a6749
parent 15058 cc8f1de3f86c
child 15203 4481ada46cbb
permissions -rw-r--r--
tuned
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4867
9be2bf0ce909 package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff changeset
     1
(*  Title:      HOL/Tools/record_package.ML
9be2bf0ce909 package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff changeset
     2
    ID:         $Id$
14579
wenzelm
parents: 14427
diff changeset
     3
    Author:     Wolfgang Naraschewski, Norbert Schirmer and Markus Wenzel, TU Muenchen
4867
9be2bf0ce909 package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff changeset
     4
9be2bf0ce909 package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff changeset
     5
Extensible records with structural subtyping in HOL.
5698
2b5d9bdec5af field types: datatype;
wenzelm
parents: 5290
diff changeset
     6
*)
4867
9be2bf0ce909 package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff changeset
     7
5698
2b5d9bdec5af field types: datatype;
wenzelm
parents: 5290
diff changeset
     8
signature BASIC_RECORD_PACKAGE =
2b5d9bdec5af field types: datatype;
wenzelm
parents: 5290
diff changeset
     9
sig
7178
50b9849cf6ad record_simproc for sel-upd (by Sebastian Nanz);
wenzelm
parents: 6851
diff changeset
    10
  val record_simproc: simproc
14079
1c22e5499eeb - record_split_tac now also works for object-level universal quantifier
berghofe
parents: 13904
diff changeset
    11
  val record_eq_simproc: simproc
14700
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
    12
  val record_upd_simproc: simproc
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
    13
  val record_split_simproc: (term -> bool) -> simproc
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
    14
  val record_ex_sel_eq_simproc: simproc
5698
2b5d9bdec5af field types: datatype;
wenzelm
parents: 5290
diff changeset
    15
  val record_split_tac: int -> tactic
14700
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
    16
  val record_split_simp_tac: (term -> bool) -> int -> tactic
5713
27d4fcf5fe5f fixed field_injects;
wenzelm
parents: 5707
diff changeset
    17
  val record_split_name: string
5698
2b5d9bdec5af field types: datatype;
wenzelm
parents: 5290
diff changeset
    18
  val record_split_wrapper: string * wrapper
14700
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
    19
  val print_record_type_abbr: bool ref
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
    20
  val print_record_type_as_fields: bool ref 
5698
2b5d9bdec5af field types: datatype;
wenzelm
parents: 5290
diff changeset
    21
end;
4867
9be2bf0ce909 package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff changeset
    22
9be2bf0ce909 package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff changeset
    23
signature RECORD_PACKAGE =
9be2bf0ce909 package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff changeset
    24
sig
5698
2b5d9bdec5af field types: datatype;
wenzelm
parents: 5290
diff changeset
    25
  include BASIC_RECORD_PACKAGE
2b5d9bdec5af field types: datatype;
wenzelm
parents: 5290
diff changeset
    26
  val quiet_mode: bool ref
15015
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
    27
  val record_quick_and_dirty_sensitive: bool ref
8574
bed3b994ab26 export updateN;
wenzelm
parents: 8428
diff changeset
    28
  val updateN: string
14700
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
    29
  val ext_typeN: string
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
    30
  val last_extT: typ -> (string * typ list) option
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
    31
  val dest_recTs : typ -> (string * typ list) list
15059
schirmer
parents: 15058
diff changeset
    32
  val get_extT_fields:  Sign.sg -> typ -> ((string * typ) list * (string * typ))
schirmer
parents: 15058
diff changeset
    33
  val get_recT_fields:  Sign.sg -> typ -> ((string * typ) list * (string * typ))
14700
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
    34
  val get_extension: Sign.sg -> Symtab.key -> (string * typ list) option
15015
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
    35
  val get_extinjects: Sign.sg -> thm list
15058
cc8f1de3f86c added: get_extT_fields and
schirmer
parents: 15015
diff changeset
    36
  val get_simpset: Sign.sg -> simpset
4867
9be2bf0ce909 package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff changeset
    37
  val print_records: theory -> unit
14700
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
    38
  val add_record: string list * string -> string option -> (string * string * mixfix) list 
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
    39
                  -> theory -> theory
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
    40
  val add_record_i: string list * string -> (typ list * string) option 
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
    41
                    -> (string * typ * mixfix) list -> theory -> theory
4867
9be2bf0ce909 package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff changeset
    42
  val setup: (theory -> theory) list
9be2bf0ce909 package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff changeset
    43
end;
9be2bf0ce909 package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff changeset
    44
14700
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
    45
14709
d01983034ded tuned HOL/record package; enabled record_upd_simproc by default.
schirmer
parents: 14702
diff changeset
    46
structure RecordPackage :RECORD_PACKAGE =     
4867
9be2bf0ce909 package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff changeset
    47
struct
9be2bf0ce909 package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff changeset
    48
14700
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
    49
val rec_UNIV_I = thm "rec_UNIV_I";
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
    50
val rec_True_simp = thm "rec_True_simp";
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
    51
val Pair_eq = thm "Product_Type.Pair_eq";
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
    52
val atomize_all = thm "HOL.atomize_all";
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
    53
val atomize_imp = thm "HOL.atomize_imp";
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
    54
val triv_goal   = thm "triv_goal";
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
    55
val prop_subst  = thm "prop_subst";
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
    56
val Pair_sel_convs = [fst_conv,snd_conv];
11832
8fca3665d1ee use abstract product type instead of datatype;
wenzelm
parents: 11739
diff changeset
    57
8fca3665d1ee use abstract product type instead of datatype;
wenzelm
parents: 11739
diff changeset
    58
8fca3665d1ee use abstract product type instead of datatype;
wenzelm
parents: 11739
diff changeset
    59
14700
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
    60
(** name components **)
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
    61
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
    62
val rN = "r";
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
    63
val moreN = "more";
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
    64
val schemeN = "_scheme";
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
    65
val ext_typeN = "_ext_type"; 
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
    66
val extN ="_ext";
14709
d01983034ded tuned HOL/record package; enabled record_upd_simproc by default.
schirmer
parents: 14702
diff changeset
    67
val ext_dest = "_sel";
14700
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
    68
val updateN = "_update";
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
    69
val schemeN = "_scheme";
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
    70
val makeN = "make";
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
    71
val fields_selN = "fields";
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
    72
val extendN = "extend";
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
    73
val truncateN = "truncate";
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
    74
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
    75
(*see typedef_package.ML*)
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
    76
val RepN = "Rep_";
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
    77
val AbsN = "Abs_";
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
    78
4894
32187e0b8b48 'more' selector;
wenzelm
parents: 4890
diff changeset
    79
(*** utilities ***)
4867
9be2bf0ce909 package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff changeset
    80
14709
d01983034ded tuned HOL/record package; enabled record_upd_simproc by default.
schirmer
parents: 14702
diff changeset
    81
fun but_last xs = fst (split_last xs);
15015
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
    82
fun list None = []
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
    83
  | list (Some x) = [x]
14700
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
    84
5698
2b5d9bdec5af field types: datatype;
wenzelm
parents: 5290
diff changeset
    85
(* messages *)
2b5d9bdec5af field types: datatype;
wenzelm
parents: 5290
diff changeset
    86
2b5d9bdec5af field types: datatype;
wenzelm
parents: 5290
diff changeset
    87
val quiet_mode = ref false;
2b5d9bdec5af field types: datatype;
wenzelm
parents: 5290
diff changeset
    88
fun message s = if ! quiet_mode then () else writeln s;
2b5d9bdec5af field types: datatype;
wenzelm
parents: 5290
diff changeset
    89
15012
28fa57b57209 Added reference record_definition_quick_and_dirty_sensitive, to
schirmer
parents: 14981
diff changeset
    90
(* timing *)
28fa57b57209 Added reference record_definition_quick_and_dirty_sensitive, to
schirmer
parents: 14981
diff changeset
    91
28fa57b57209 Added reference record_definition_quick_and_dirty_sensitive, to
schirmer
parents: 14981
diff changeset
    92
fun timeit_msg s x = if !timing then (warning s; timeit x) else x ();
28fa57b57209 Added reference record_definition_quick_and_dirty_sensitive, to
schirmer
parents: 14981
diff changeset
    93
 
12255
wenzelm
parents: 12247
diff changeset
    94
(* syntax *)
4867
9be2bf0ce909 package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff changeset
    95
12247
9b029789aff6 derive cases/induct rules for ``more'' parts;
wenzelm
parents: 12129
diff changeset
    96
fun prune n xs = Library.drop (n, xs);
11832
8fca3665d1ee use abstract product type instead of datatype;
wenzelm
parents: 11739
diff changeset
    97
fun prefix_base s = NameSpace.map_base (fn bname => s ^ bname);
8fca3665d1ee use abstract product type instead of datatype;
wenzelm
parents: 11739
diff changeset
    98
11927
96f267adc029 provodes induct/cases for use with corresponding Isar methods;
wenzelm
parents: 11923
diff changeset
    99
val Trueprop = HOLogic.mk_Trueprop;
96f267adc029 provodes induct/cases for use with corresponding Isar methods;
wenzelm
parents: 11923
diff changeset
   100
fun All xs t = Term.list_all_free (xs, t);
4894
32187e0b8b48 'more' selector;
wenzelm
parents: 4890
diff changeset
   101
11934
6c1bf72430b6 derived operations are now: make, extend, truncate (cf. derived_defs);
wenzelm
parents: 11927
diff changeset
   102
infix 9 $$;
6c1bf72430b6 derived operations are now: make, extend, truncate (cf. derived_defs);
wenzelm
parents: 11927
diff changeset
   103
infix 0 :== ===;
6c1bf72430b6 derived operations are now: make, extend, truncate (cf. derived_defs);
wenzelm
parents: 11927
diff changeset
   104
infixr 0 ==>;
6c1bf72430b6 derived operations are now: make, extend, truncate (cf. derived_defs);
wenzelm
parents: 11927
diff changeset
   105
6c1bf72430b6 derived operations are now: make, extend, truncate (cf. derived_defs);
wenzelm
parents: 11927
diff changeset
   106
val (op $$) = Term.list_comb;
11927
96f267adc029 provodes induct/cases for use with corresponding Isar methods;
wenzelm
parents: 11923
diff changeset
   107
val (op :==) = Logic.mk_defpair;
96f267adc029 provodes induct/cases for use with corresponding Isar methods;
wenzelm
parents: 11923
diff changeset
   108
val (op ===) = Trueprop o HOLogic.mk_eq;
96f267adc029 provodes induct/cases for use with corresponding Isar methods;
wenzelm
parents: 11923
diff changeset
   109
val (op ==>) = Logic.mk_implies;
96f267adc029 provodes induct/cases for use with corresponding Isar methods;
wenzelm
parents: 11923
diff changeset
   110
11832
8fca3665d1ee use abstract product type instead of datatype;
wenzelm
parents: 11739
diff changeset
   111
(* morphisms *)
8fca3665d1ee use abstract product type instead of datatype;
wenzelm
parents: 11739
diff changeset
   112
14700
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   113
fun mk_RepN name = suffix ext_typeN (prefix_base RepN name);
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   114
fun mk_AbsN name = suffix ext_typeN (prefix_base AbsN name);
11832
8fca3665d1ee use abstract product type instead of datatype;
wenzelm
parents: 11739
diff changeset
   115
14700
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   116
fun mk_Rep name repT absT  =
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   117
  Const (suffix ext_typeN (prefix_base RepN name),absT --> repT);
11832
8fca3665d1ee use abstract product type instead of datatype;
wenzelm
parents: 11739
diff changeset
   118
14700
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   119
fun mk_Abs name repT absT =
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   120
  Const (mk_AbsN name,repT --> absT);
4867
9be2bf0ce909 package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff changeset
   121
14700
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   122
(* constructor *)
4867
9be2bf0ce909 package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff changeset
   123
14700
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   124
fun mk_extC (name,T) Ts  = (suffix extN name, Ts ---> T);
4867
9be2bf0ce909 package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff changeset
   125
14700
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   126
fun mk_ext (name,T) ts =
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   127
  let val Ts = map fastype_of ts
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   128
  in list_comb (Const (mk_extC (name,T) Ts),ts) end;
4867
9be2bf0ce909 package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff changeset
   129
14700
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   130
(* selector *)
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   131
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   132
fun mk_selC sT (c,T) = (c,sT --> T);
4867
9be2bf0ce909 package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff changeset
   133
14700
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   134
fun mk_sel s (c,T) =
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   135
  let val sT = fastype_of s
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   136
  in Const (mk_selC sT (c,T)) $ s end;
4867
9be2bf0ce909 package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff changeset
   137
14700
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   138
(* updates *)
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   139
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   140
fun mk_updC sT (c,T) = (suffix updateN c, T --> sT --> sT);
4867
9be2bf0ce909 package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff changeset
   141
14700
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   142
fun mk_upd c v s =
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   143
  let val sT = fastype_of s;
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   144
      val vT = fastype_of v;
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   145
  in Const (mk_updC sT (c, vT)) $ v $ s end;
4867
9be2bf0ce909 package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff changeset
   146
9be2bf0ce909 package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff changeset
   147
(* types *)
9be2bf0ce909 package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff changeset
   148
14700
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   149
fun dest_recT (typ as Type (c_ext_type, Ts as (T::_))) =
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   150
      (case try (unsuffix ext_typeN) c_ext_type of
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   151
        None => raise TYPE ("RecordPackage.dest_recT", [typ], [])
14709
d01983034ded tuned HOL/record package; enabled record_upd_simproc by default.
schirmer
parents: 14702
diff changeset
   152
      | Some c => ((c, Ts), last_elem Ts))
14700
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   153
  | dest_recT typ = raise TYPE ("RecordPackage.dest_recT", [typ], []);
5197
69c77ed95ba3 added more_update;
wenzelm
parents: 5060
diff changeset
   154
14700
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   155
fun is_recT T =
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   156
  (case try dest_recT T of None => false | Some _ => true); 
11833
wenzelm
parents: 11832
diff changeset
   157
14700
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   158
fun dest_recTs T =
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   159
  let val ((c, Ts), U) = dest_recT T
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   160
  in (c, Ts) :: dest_recTs U
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   161
  end handle TYPE _ => [];
14255
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
   162
14700
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   163
fun last_extT T =
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   164
  let val ((c, Ts), U) = dest_recT T
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   165
  in (case last_extT U of
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   166
        None => Some (c,Ts)
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   167
      | Some l => Some l)
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   168
  end handle TYPE _ => None
14255
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
   169
14700
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   170
fun rec_id T = foldl (fn (s,(c,T)) => s ^ c) ("",dest_recTs T);
4867
9be2bf0ce909 package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff changeset
   171
9be2bf0ce909 package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff changeset
   172
(*** extend theory by record definition ***)
9be2bf0ce909 package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff changeset
   173
9be2bf0ce909 package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff changeset
   174
(** record info **)
9be2bf0ce909 package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff changeset
   175
14255
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
   176
(* type record_info and parent_info  *)
4867
9be2bf0ce909 package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff changeset
   177
9be2bf0ce909 package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff changeset
   178
type record_info =
9be2bf0ce909 package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff changeset
   179
 {args: (string * sort) list,
9be2bf0ce909 package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff changeset
   180
  parent: (typ list * string) option,
9be2bf0ce909 package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff changeset
   181
  fields: (string * typ) list,
14700
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   182
  extension: (string * typ list),
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   183
  induct: thm
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   184
 };
11927
96f267adc029 provodes induct/cases for use with corresponding Isar methods;
wenzelm
parents: 11923
diff changeset
   185
14700
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   186
fun make_record_info args parent fields extension induct =
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   187
 {args = args, parent = parent, fields = fields, extension = extension, 
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   188
  induct = induct}: record_info;
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   189
4867
9be2bf0ce909 package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff changeset
   190
9be2bf0ce909 package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff changeset
   191
type parent_info =
9be2bf0ce909 package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff changeset
   192
 {name: string,
9be2bf0ce909 package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff changeset
   193
  fields: (string * typ) list,
14700
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   194
  extension: (string * typ list),
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   195
  induct: thm
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   196
};
11927
96f267adc029 provodes induct/cases for use with corresponding Isar methods;
wenzelm
parents: 11923
diff changeset
   197
14700
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   198
fun make_parent_info name fields extension induct =
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   199
 {name = name, fields = fields, extension = extension, induct = induct}: parent_info;
4867
9be2bf0ce909 package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff changeset
   200
14700
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   201
(* data kind 'HOL/record' *)
5001
9de7fda0a6df accomodate tuned version of theory data;
wenzelm
parents: 4970
diff changeset
   202
7178
50b9849cf6ad record_simproc for sel-upd (by Sebastian Nanz);
wenzelm
parents: 6851
diff changeset
   203
type record_data =
50b9849cf6ad record_simproc for sel-upd (by Sebastian Nanz);
wenzelm
parents: 6851
diff changeset
   204
 {records: record_info Symtab.table,
50b9849cf6ad record_simproc for sel-upd (by Sebastian Nanz);
wenzelm
parents: 6851
diff changeset
   205
  sel_upd:
50b9849cf6ad record_simproc for sel-upd (by Sebastian Nanz);
wenzelm
parents: 6851
diff changeset
   206
   {selectors: unit Symtab.table,
50b9849cf6ad record_simproc for sel-upd (by Sebastian Nanz);
wenzelm
parents: 6851
diff changeset
   207
    updates: string Symtab.table,
50b9849cf6ad record_simproc for sel-upd (by Sebastian Nanz);
wenzelm
parents: 6851
diff changeset
   208
    simpset: Simplifier.simpset},
14255
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
   209
  equalities: thm Symtab.table,
15015
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   210
  extinjects: thm list,
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   211
  extsplit: thm Symtab.table, (* maps extension name to split rule *)
14700
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   212
  splits: (thm*thm*thm*thm) Symtab.table,    (* !!,!,EX - split-equalities,induct rule *) 
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   213
  extfields: (string*typ) list Symtab.table, (* maps extension to its fields *)
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   214
  fieldext: (string*typ list) Symtab.table   (* maps field to its extension *)
14255
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
   215
};
7178
50b9849cf6ad record_simproc for sel-upd (by Sebastian Nanz);
wenzelm
parents: 6851
diff changeset
   216
15015
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   217
fun make_record_data 
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   218
      records sel_upd equalities extinjects extsplit splits extfields fieldext =
14700
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   219
 {records = records, sel_upd = sel_upd, 
15015
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   220
  equalities = equalities, extinjects=extinjects, extsplit = extsplit, splits = splits, 
14700
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   221
  extfields = extfields, fieldext = fieldext }: record_data;
7178
50b9849cf6ad record_simproc for sel-upd (by Sebastian Nanz);
wenzelm
parents: 6851
diff changeset
   222
5006
cdc86a914e63 adapted to new theory data interface;
wenzelm
parents: 5001
diff changeset
   223
structure RecordsArgs =
cdc86a914e63 adapted to new theory data interface;
wenzelm
parents: 5001
diff changeset
   224
struct
14959
schirmer
parents: 14854
diff changeset
   225
  val name = "HOL/records";     
7178
50b9849cf6ad record_simproc for sel-upd (by Sebastian Nanz);
wenzelm
parents: 6851
diff changeset
   226
  type T = record_data;
4867
9be2bf0ce909 package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff changeset
   227
7178
50b9849cf6ad record_simproc for sel-upd (by Sebastian Nanz);
wenzelm
parents: 6851
diff changeset
   228
  val empty =
50b9849cf6ad record_simproc for sel-upd (by Sebastian Nanz);
wenzelm
parents: 6851
diff changeset
   229
    make_record_data Symtab.empty
50b9849cf6ad record_simproc for sel-upd (by Sebastian Nanz);
wenzelm
parents: 6851
diff changeset
   230
      {selectors = Symtab.empty, updates = Symtab.empty, simpset = HOL_basic_ss}
15015
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   231
       Symtab.empty [] Symtab.empty Symtab.empty Symtab.empty Symtab.empty;
7178
50b9849cf6ad record_simproc for sel-upd (by Sebastian Nanz);
wenzelm
parents: 6851
diff changeset
   232
6556
daa00919502b theory data: copy;
wenzelm
parents: 6519
diff changeset
   233
  val copy = I;
5006
cdc86a914e63 adapted to new theory data interface;
wenzelm
parents: 5001
diff changeset
   234
  val prep_ext = I;
7178
50b9849cf6ad record_simproc for sel-upd (by Sebastian Nanz);
wenzelm
parents: 6851
diff changeset
   235
  fun merge
50b9849cf6ad record_simproc for sel-upd (by Sebastian Nanz);
wenzelm
parents: 6851
diff changeset
   236
   ({records = recs1,
50b9849cf6ad record_simproc for sel-upd (by Sebastian Nanz);
wenzelm
parents: 6851
diff changeset
   237
     sel_upd = {selectors = sels1, updates = upds1, simpset = ss1},
14255
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
   238
     equalities = equalities1,
15015
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   239
     extinjects = extinjects1, 
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   240
     extsplit = extsplit1,
14700
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   241
     splits = splits1,
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   242
     extfields = extfields1,
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   243
     fieldext = fieldext1},
7178
50b9849cf6ad record_simproc for sel-upd (by Sebastian Nanz);
wenzelm
parents: 6851
diff changeset
   244
    {records = recs2,
50b9849cf6ad record_simproc for sel-upd (by Sebastian Nanz);
wenzelm
parents: 6851
diff changeset
   245
     sel_upd = {selectors = sels2, updates = upds2, simpset = ss2},
15015
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   246
     equalities = equalities2,
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   247
     extinjects = extinjects2, 
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   248
     extsplit = extsplit2, 
14700
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   249
     splits = splits2,
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   250
     extfields = extfields2,
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   251
     fieldext = fieldext2}) =
14255
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
   252
    make_record_data  
7178
50b9849cf6ad record_simproc for sel-upd (by Sebastian Nanz);
wenzelm
parents: 6851
diff changeset
   253
      (Symtab.merge (K true) (recs1, recs2))
50b9849cf6ad record_simproc for sel-upd (by Sebastian Nanz);
wenzelm
parents: 6851
diff changeset
   254
      {selectors = Symtab.merge (K true) (sels1, sels2),
50b9849cf6ad record_simproc for sel-upd (by Sebastian Nanz);
wenzelm
parents: 6851
diff changeset
   255
        updates = Symtab.merge (K true) (upds1, upds2),
50b9849cf6ad record_simproc for sel-upd (by Sebastian Nanz);
wenzelm
parents: 6851
diff changeset
   256
        simpset = Simplifier.merge_ss (ss1, ss2)}
14255
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
   257
      (Symtab.merge Thm.eq_thm (equalities1, equalities2))
15015
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   258
      (extinjects1 @ extinjects2)
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   259
      (Symtab.merge Thm.eq_thm (extsplit1,extsplit2))
14255
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
   260
      (Symtab.merge (fn ((a,b,c,d),(w,x,y,z)) 
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
   261
                     => Thm.eq_thm (a,w) andalso Thm.eq_thm (b,x) andalso 
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
   262
                        Thm.eq_thm (c,y) andalso Thm.eq_thm (d,z)) 
14700
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   263
                    (splits1, splits2))
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   264
      (Symtab.merge (K true) (extfields1,extfields2))
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   265
      (Symtab.merge (K true) (fieldext1,fieldext2));
4867
9be2bf0ce909 package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff changeset
   266
7178
50b9849cf6ad record_simproc for sel-upd (by Sebastian Nanz);
wenzelm
parents: 6851
diff changeset
   267
  fun print sg ({records = recs, ...}: record_data) =
4867
9be2bf0ce909 package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff changeset
   268
    let
9be2bf0ce909 package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff changeset
   269
      val prt_typ = Sign.pretty_typ sg;
9be2bf0ce909 package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff changeset
   270
9be2bf0ce909 package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff changeset
   271
      fun pretty_parent None = []
9be2bf0ce909 package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff changeset
   272
        | pretty_parent (Some (Ts, name)) =
9be2bf0ce909 package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff changeset
   273
            [Pretty.block [prt_typ (Type (name, Ts)), Pretty.str " +"]];
9be2bf0ce909 package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff changeset
   274
9be2bf0ce909 package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff changeset
   275
      fun pretty_field (c, T) = Pretty.block
12129
964f5ffe13d0 fixed print_records;
wenzelm
parents: 12109
diff changeset
   276
        [Pretty.str (Sign.cond_extern sg Sign.constK c), Pretty.str " ::",
964f5ffe13d0 fixed print_records;
wenzelm
parents: 12109
diff changeset
   277
          Pretty.brk 1, Pretty.quote (prt_typ T)];
4867
9be2bf0ce909 package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff changeset
   278
12247
9b029789aff6 derive cases/induct rules for ``more'' parts;
wenzelm
parents: 12129
diff changeset
   279
      fun pretty_record (name, {args, parent, fields, ...}: record_info) =
11927
96f267adc029 provodes induct/cases for use with corresponding Isar methods;
wenzelm
parents: 11923
diff changeset
   280
        Pretty.block (Pretty.fbreaks (Pretty.block
96f267adc029 provodes induct/cases for use with corresponding Isar methods;
wenzelm
parents: 11923
diff changeset
   281
          [prt_typ (Type (name, map TFree args)), Pretty.str " = "] ::
4867
9be2bf0ce909 package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff changeset
   282
          pretty_parent parent @ map pretty_field fields));
12129
964f5ffe13d0 fixed print_records;
wenzelm
parents: 12109
diff changeset
   283
    in map pretty_record (Symtab.dest recs) |> Pretty.chunks |> Pretty.writeln end;
4867
9be2bf0ce909 package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff changeset
   284
end;
9be2bf0ce909 package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff changeset
   285
5006
cdc86a914e63 adapted to new theory data interface;
wenzelm
parents: 5001
diff changeset
   286
structure RecordsData = TheoryDataFun(RecordsArgs);
cdc86a914e63 adapted to new theory data interface;
wenzelm
parents: 5001
diff changeset
   287
val print_records = RecordsData.print;
cdc86a914e63 adapted to new theory data interface;
wenzelm
parents: 5001
diff changeset
   288
7178
50b9849cf6ad record_simproc for sel-upd (by Sebastian Nanz);
wenzelm
parents: 6851
diff changeset
   289
(* access 'records' *)
4867
9be2bf0ce909 package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff changeset
   290
7178
50b9849cf6ad record_simproc for sel-upd (by Sebastian Nanz);
wenzelm
parents: 6851
diff changeset
   291
fun get_record thy name = Symtab.lookup (#records (RecordsData.get thy), name);
4867
9be2bf0ce909 package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff changeset
   292
4890
f0a24bad990a concrete syntax for record terms;
wenzelm
parents: 4867
diff changeset
   293
fun put_record name info thy =
7178
50b9849cf6ad record_simproc for sel-upd (by Sebastian Nanz);
wenzelm
parents: 6851
diff changeset
   294
  let
15015
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   295
    val {records, sel_upd, equalities, extinjects,extsplit,splits,extfields,fieldext} = 
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   296
          RecordsData.get thy;
14079
1c22e5499eeb - record_split_tac now also works for object-level universal quantifier
berghofe
parents: 13904
diff changeset
   297
    val data = make_record_data (Symtab.update ((name, info), records))
15015
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   298
      sel_upd equalities extinjects extsplit splits extfields fieldext;
7178
50b9849cf6ad record_simproc for sel-upd (by Sebastian Nanz);
wenzelm
parents: 6851
diff changeset
   299
  in RecordsData.put data thy end;
50b9849cf6ad record_simproc for sel-upd (by Sebastian Nanz);
wenzelm
parents: 6851
diff changeset
   300
50b9849cf6ad record_simproc for sel-upd (by Sebastian Nanz);
wenzelm
parents: 6851
diff changeset
   301
(* access 'sel_upd' *)
50b9849cf6ad record_simproc for sel-upd (by Sebastian Nanz);
wenzelm
parents: 6851
diff changeset
   302
50b9849cf6ad record_simproc for sel-upd (by Sebastian Nanz);
wenzelm
parents: 6851
diff changeset
   303
fun get_sel_upd sg = #sel_upd (RecordsData.get_sg sg);
50b9849cf6ad record_simproc for sel-upd (by Sebastian Nanz);
wenzelm
parents: 6851
diff changeset
   304
50b9849cf6ad record_simproc for sel-upd (by Sebastian Nanz);
wenzelm
parents: 6851
diff changeset
   305
fun get_selectors sg name = Symtab.lookup (#selectors (get_sel_upd sg), name);
14700
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   306
fun is_selector sg name = 
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   307
  case get_selectors sg (Sign.intern_const sg name) of 
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   308
     None => false
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   309
   | Some _ => true
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   310
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   311
                             
7178
50b9849cf6ad record_simproc for sel-upd (by Sebastian Nanz);
wenzelm
parents: 6851
diff changeset
   312
fun get_updates sg name = Symtab.lookup (#updates (get_sel_upd sg), name);
50b9849cf6ad record_simproc for sel-upd (by Sebastian Nanz);
wenzelm
parents: 6851
diff changeset
   313
fun get_simpset sg = #simpset (get_sel_upd sg);
50b9849cf6ad record_simproc for sel-upd (by Sebastian Nanz);
wenzelm
parents: 6851
diff changeset
   314
50b9849cf6ad record_simproc for sel-upd (by Sebastian Nanz);
wenzelm
parents: 6851
diff changeset
   315
fun put_sel_upd names simps thy =
50b9849cf6ad record_simproc for sel-upd (by Sebastian Nanz);
wenzelm
parents: 6851
diff changeset
   316
  let
50b9849cf6ad record_simproc for sel-upd (by Sebastian Nanz);
wenzelm
parents: 6851
diff changeset
   317
    val sels = map (rpair ()) names;
50b9849cf6ad record_simproc for sel-upd (by Sebastian Nanz);
wenzelm
parents: 6851
diff changeset
   318
    val upds = map (suffix updateN) names ~~ names;
50b9849cf6ad record_simproc for sel-upd (by Sebastian Nanz);
wenzelm
parents: 6851
diff changeset
   319
14700
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   320
    val {records, sel_upd = {selectors, updates, simpset}, 
15015
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   321
      equalities, extinjects, extsplit, splits, extfields,fieldext} = RecordsData.get thy;
7178
50b9849cf6ad record_simproc for sel-upd (by Sebastian Nanz);
wenzelm
parents: 6851
diff changeset
   322
    val data = make_record_data records
50b9849cf6ad record_simproc for sel-upd (by Sebastian Nanz);
wenzelm
parents: 6851
diff changeset
   323
      {selectors = Symtab.extend (selectors, sels),
50b9849cf6ad record_simproc for sel-upd (by Sebastian Nanz);
wenzelm
parents: 6851
diff changeset
   324
        updates = Symtab.extend (updates, upds),
50b9849cf6ad record_simproc for sel-upd (by Sebastian Nanz);
wenzelm
parents: 6851
diff changeset
   325
        simpset = Simplifier.addsimps (simpset, simps)}
15015
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   326
       equalities extinjects extsplit splits extfields fieldext;
7178
50b9849cf6ad record_simproc for sel-upd (by Sebastian Nanz);
wenzelm
parents: 6851
diff changeset
   327
  in RecordsData.put data thy end;
50b9849cf6ad record_simproc for sel-upd (by Sebastian Nanz);
wenzelm
parents: 6851
diff changeset
   328
14079
1c22e5499eeb - record_split_tac now also works for object-level universal quantifier
berghofe
parents: 13904
diff changeset
   329
(* access 'equalities' *)
1c22e5499eeb - record_split_tac now also works for object-level universal quantifier
berghofe
parents: 13904
diff changeset
   330
1c22e5499eeb - record_split_tac now also works for object-level universal quantifier
berghofe
parents: 13904
diff changeset
   331
fun add_record_equalities name thm thy =
1c22e5499eeb - record_split_tac now also works for object-level universal quantifier
berghofe
parents: 13904
diff changeset
   332
  let
15015
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   333
    val {records, sel_upd, equalities, extinjects, extsplit, splits, extfields,fieldext} = 
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   334
          RecordsData.get thy;
14700
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   335
    val data = make_record_data records sel_upd 
15015
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   336
           (Symtab.update_new ((name, thm), equalities)) extinjects extsplit 
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   337
           splits extfields fieldext;
14079
1c22e5499eeb - record_split_tac now also works for object-level universal quantifier
berghofe
parents: 13904
diff changeset
   338
  in RecordsData.put data thy end;
1c22e5499eeb - record_split_tac now also works for object-level universal quantifier
berghofe
parents: 13904
diff changeset
   339
1c22e5499eeb - record_split_tac now also works for object-level universal quantifier
berghofe
parents: 13904
diff changeset
   340
fun get_equalities sg name =
1c22e5499eeb - record_split_tac now also works for object-level universal quantifier
berghofe
parents: 13904
diff changeset
   341
  Symtab.lookup (#equalities (RecordsData.get_sg sg), name);
1c22e5499eeb - record_split_tac now also works for object-level universal quantifier
berghofe
parents: 13904
diff changeset
   342
15015
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   343
(* access 'extinjects' *)
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   344
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   345
fun add_extinjects thm thy =
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   346
  let
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   347
    val {records, sel_upd, equalities, extinjects, extsplit, splits, extfields,fieldext} = 
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   348
          RecordsData.get thy;
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   349
    val data = make_record_data records sel_upd equalities (extinjects@[thm]) extsplit  
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   350
                 splits extfields fieldext;
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   351
  in RecordsData.put data thy end;
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   352
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   353
fun get_extinjects sg = #extinjects (RecordsData.get_sg sg);
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   354
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   355
(* access 'extsplit' *)
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   356
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   357
fun add_extsplit name thm thy =
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   358
  let
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   359
    val {records, sel_upd, equalities, extinjects, extsplit, splits, extfields,fieldext} = 
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   360
          RecordsData.get thy;
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   361
    val data = make_record_data records sel_upd 
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   362
      equalities extinjects (Symtab.update_new ((name, thm), extsplit)) splits 
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   363
      extfields fieldext;
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   364
  in RecordsData.put data thy end;
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   365
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   366
fun get_extsplit sg name =
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   367
  Symtab.lookup (#extsplit (RecordsData.get_sg sg), name);
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   368
14255
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
   369
(* access 'splits' *)
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
   370
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
   371
fun add_record_splits name thmP thy =
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
   372
  let
15015
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   373
    val {records, sel_upd, equalities, extinjects, extsplit, splits, extfields,fieldext} = 
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   374
          RecordsData.get thy;
14700
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   375
    val data = make_record_data records sel_upd 
15015
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   376
      equalities extinjects extsplit (Symtab.update_new ((name, thmP), splits)) 
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   377
      extfields fieldext;
14255
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
   378
  in RecordsData.put data thy end;
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
   379
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
   380
fun get_splits sg name =
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
   381
  Symtab.lookup (#splits (RecordsData.get_sg sg), name);
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
   382
15015
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   383
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   384
14700
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   385
(* extension of a record name *)
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   386
fun get_extension sg name =
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   387
 case Symtab.lookup (#records (RecordsData.get_sg sg),name) of
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   388
        Some s => Some (#extension s)
14358
233c5bd5b539 cleaning up
schirmer
parents: 14255
diff changeset
   389
      | None => None;
14079
1c22e5499eeb - record_split_tac now also works for object-level universal quantifier
berghofe
parents: 13904
diff changeset
   390
14700
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   391
(* access 'extfields' *)
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   392
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   393
fun add_extfields name fields thy =
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   394
  let
15015
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   395
    val {records, sel_upd, equalities, extinjects, extsplit,splits, extfields, fieldext} = 
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   396
          RecordsData.get thy;
14700
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   397
    val data = make_record_data records sel_upd 
15015
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   398
         equalities extinjects extsplit splits 
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   399
         (Symtab.update_new ((name, fields), extfields)) fieldext;
14700
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   400
  in RecordsData.put data thy end;
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   401
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   402
fun get_extfields sg name =
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   403
  Symtab.lookup (#extfields (RecordsData.get_sg sg), name);
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   404
15058
cc8f1de3f86c added: get_extT_fields and
schirmer
parents: 15015
diff changeset
   405
fun get_extT_fields sg T = 
15059
schirmer
parents: 15058
diff changeset
   406
  let
schirmer
parents: 15058
diff changeset
   407
    val ((name,Ts),moreT) = dest_recT T;
schirmer
parents: 15058
diff changeset
   408
    val recname = let val (nm::recn::rst) = rev (NameSpace.unpack name) 
schirmer
parents: 15058
diff changeset
   409
                  in NameSpace.pack (rev (nm::rst)) end;
schirmer
parents: 15058
diff changeset
   410
    val midx = maxidx_of_typs (moreT::Ts);
schirmer
parents: 15058
diff changeset
   411
    fun varify (a, S) = TVar ((a, midx), S);
schirmer
parents: 15058
diff changeset
   412
    val varifyT = map_type_tfree varify;
schirmer
parents: 15058
diff changeset
   413
    val {records,extfields,...} = RecordsData.get_sg sg;
schirmer
parents: 15058
diff changeset
   414
    val (flds,(more,_)) = split_last (Symtab.lookup_multi (extfields,name));
schirmer
parents: 15058
diff changeset
   415
    val args = map varifyT (snd (#extension (the (Symtab.lookup (records,recname)))));
15058
cc8f1de3f86c added: get_extT_fields and
schirmer
parents: 15015
diff changeset
   416
15059
schirmer
parents: 15058
diff changeset
   417
    val tsig = Sign.tsig_of sg;
schirmer
parents: 15058
diff changeset
   418
    fun unify (t,env) = Type.unify tsig env t;
schirmer
parents: 15058
diff changeset
   419
    val (subst,_) = foldr unify (but_last args ~~ but_last Ts,(Vartab.empty,0));
schirmer
parents: 15058
diff changeset
   420
    val flds' = map (apsnd ((Envir.norm_type subst) o varifyT)) flds;
schirmer
parents: 15058
diff changeset
   421
  in (flds',(more,moreT)) end;
15058
cc8f1de3f86c added: get_extT_fields and
schirmer
parents: 15015
diff changeset
   422
cc8f1de3f86c added: get_extT_fields and
schirmer
parents: 15015
diff changeset
   423
fun get_recT_fields sg T = 
15059
schirmer
parents: 15058
diff changeset
   424
  let 
schirmer
parents: 15058
diff changeset
   425
    val (root_flds,(root_more,root_moreT)) = get_extT_fields sg T;
schirmer
parents: 15058
diff changeset
   426
    val (rest_flds,rest_more) = 
schirmer
parents: 15058
diff changeset
   427
	   if is_recT root_moreT then get_recT_fields sg root_moreT 
schirmer
parents: 15058
diff changeset
   428
           else ([],(root_more,root_moreT));
schirmer
parents: 15058
diff changeset
   429
  in (root_flds@rest_flds,rest_more) end;
schirmer
parents: 15058
diff changeset
   430
15058
cc8f1de3f86c added: get_extT_fields and
schirmer
parents: 15015
diff changeset
   431
14700
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   432
(* access 'fieldext' *)
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   433
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   434
fun add_fieldext extname_types fields thy =
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   435
  let
15015
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   436
    val {records, sel_upd, equalities, extinjects, extsplit, splits, extfields, fieldext} = 
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   437
           RecordsData.get thy;
14700
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   438
    val fieldext' = foldl (fn (table,field) => Symtab.update_new ((field,extname_types),table))  
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   439
                          (fieldext,fields);
15015
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   440
    val data=make_record_data records sel_upd equalities extinjects extsplit 
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   441
              splits extfields fieldext';
14700
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   442
  in RecordsData.put data thy end;
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   443
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   444
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   445
fun get_fieldext sg name =
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   446
  Symtab.lookup (#fieldext (RecordsData.get_sg sg), name);
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   447
4867
9be2bf0ce909 package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff changeset
   448
(* parent records *)
9be2bf0ce909 package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff changeset
   449
12247
9b029789aff6 derive cases/induct rules for ``more'' parts;
wenzelm
parents: 12129
diff changeset
   450
fun add_parents thy None parents = parents
9b029789aff6 derive cases/induct rules for ``more'' parts;
wenzelm
parents: 12129
diff changeset
   451
  | add_parents thy (Some (types, name)) parents =
9b029789aff6 derive cases/induct rules for ``more'' parts;
wenzelm
parents: 12129
diff changeset
   452
      let
9b029789aff6 derive cases/induct rules for ``more'' parts;
wenzelm
parents: 12129
diff changeset
   453
        val sign = Theory.sign_of thy;
9b029789aff6 derive cases/induct rules for ``more'' parts;
wenzelm
parents: 12129
diff changeset
   454
        fun err msg = error (msg ^ " parent record " ^ quote name);
12255
wenzelm
parents: 12247
diff changeset
   455
14700
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   456
        val {args, parent, fields, extension, induct} =
12247
9b029789aff6 derive cases/induct rules for ``more'' parts;
wenzelm
parents: 12129
diff changeset
   457
          (case get_record thy name of Some info => info | None => err "Unknown");
9b029789aff6 derive cases/induct rules for ``more'' parts;
wenzelm
parents: 12129
diff changeset
   458
        val _ = if length types <> length args then err "Bad number of arguments for" else ();
12255
wenzelm
parents: 12247
diff changeset
   459
12247
9b029789aff6 derive cases/induct rules for ``more'' parts;
wenzelm
parents: 12129
diff changeset
   460
        fun bad_inst ((x, S), T) =
9b029789aff6 derive cases/induct rules for ``more'' parts;
wenzelm
parents: 12129
diff changeset
   461
          if Sign.of_sort sign (T, S) then None else Some x
9b029789aff6 derive cases/induct rules for ``more'' parts;
wenzelm
parents: 12129
diff changeset
   462
        val bads = mapfilter bad_inst (args ~~ types);
12255
wenzelm
parents: 12247
diff changeset
   463
12247
9b029789aff6 derive cases/induct rules for ``more'' parts;
wenzelm
parents: 12129
diff changeset
   464
        val inst = map fst args ~~ types;
9b029789aff6 derive cases/induct rules for ``more'' parts;
wenzelm
parents: 12129
diff changeset
   465
        val subst = Term.map_type_tfree (fn (x, _) => the (assoc (inst, x)));
9b029789aff6 derive cases/induct rules for ``more'' parts;
wenzelm
parents: 12129
diff changeset
   466
        val parent' = apsome (apfst (map subst)) parent;
9b029789aff6 derive cases/induct rules for ``more'' parts;
wenzelm
parents: 12129
diff changeset
   467
        val fields' = map (apsnd subst) fields;
14700
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   468
        val extension' = apsnd (map subst) extension;
12247
9b029789aff6 derive cases/induct rules for ``more'' parts;
wenzelm
parents: 12129
diff changeset
   469
      in
12255
wenzelm
parents: 12247
diff changeset
   470
        conditional (not (null bads)) (fn () =>
wenzelm
parents: 12247
diff changeset
   471
          err ("Ill-sorted instantiation of " ^ commas bads ^ " in"));
wenzelm
parents: 12247
diff changeset
   472
        add_parents thy parent'
14700
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   473
          (make_parent_info name fields' extension' induct::parents)
12247
9b029789aff6 derive cases/induct rules for ``more'' parts;
wenzelm
parents: 12129
diff changeset
   474
      end;
4867
9be2bf0ce909 package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff changeset
   475
9be2bf0ce909 package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff changeset
   476
14700
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   477
(** concrete syntax for records **)
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   478
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   479
(* parse translations *)
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   480
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   481
fun gen_field_tr mark sfx (t as Const (c, _) $ Const (name, _) $ arg) =
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   482
      if c = mark then Syntax.const (suffix sfx name) $ arg
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   483
      else raise TERM ("gen_field_tr: " ^ mark, [t])
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   484
  | gen_field_tr mark _ t = raise TERM ("gen_field_tr: " ^ mark, [t]);
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   485
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   486
fun gen_fields_tr sep mark sfx (tm as Const (c, _) $ t $ u) =
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   487
      if c = sep then gen_field_tr mark sfx t :: gen_fields_tr sep mark sfx u
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   488
      else [gen_field_tr mark sfx tm]
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   489
  | gen_fields_tr _ mark sfx tm = [gen_field_tr mark sfx tm];
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   490
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   491
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   492
fun record_update_tr [t, u] =
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   493
      foldr (op $) (rev (gen_fields_tr "_updates" "_update" updateN u), t)
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   494
  | record_update_tr ts = raise TERM ("record_update_tr", ts);
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   495
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   496
fun update_name_tr (Free (x, T) :: ts) = Free (suffix updateN x, T) $$ ts
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   497
  | update_name_tr (Const (x, T) :: ts) = Const (suffix updateN x, T) $$ ts
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   498
  | update_name_tr (((c as Const ("_constrain", _)) $ t $ ty) :: ts) =
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   499
      (c $ update_name_tr [t] $ (Syntax.const "fun" $ ty $ Syntax.const "dummy")) $$ ts
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   500
  | update_name_tr ts = raise TERM ("update_name_tr", ts);
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   501
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   502
fun dest_ext_field mark (t as (Const (c,_) $ Const (name,_) $ arg)) =
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   503
     if c = mark then (name,arg) else raise TERM ("dest_ext_field: " ^ mark, [t])
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   504
  | dest_ext_field _ t = raise TERM ("dest_ext_field", [t])
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   505
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   506
fun dest_ext_fields sep mark (trm as (Const (c,_) $ t $ u)) =
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   507
     if c = sep then dest_ext_field mark t::dest_ext_fields sep mark u
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   508
     else [dest_ext_field mark trm]
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   509
  | dest_ext_fields _ mark t = [dest_ext_field mark t]
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   510
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   511
fun gen_ext_fields_tr sep mark sfx more sg t =
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   512
  let 
14709
d01983034ded tuned HOL/record package; enabled record_upd_simproc by default.
schirmer
parents: 14702
diff changeset
   513
    val msg = "error in record input: ";
14700
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   514
    val fieldargs = dest_ext_fields sep mark t; 
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   515
    fun splitargs (field::fields) ((name,arg)::fargs) =
14709
d01983034ded tuned HOL/record package; enabled record_upd_simproc by default.
schirmer
parents: 14702
diff changeset
   516
          if can (unsuffix name) field
14700
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   517
          then let val (args,rest) = splitargs fields fargs
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   518
               in (arg::args,rest) end
14709
d01983034ded tuned HOL/record package; enabled record_upd_simproc by default.
schirmer
parents: 14702
diff changeset
   519
          else raise TERM (msg ^ "expecting field " ^ field ^ " but got " ^ name, [t])
14700
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   520
      | splitargs [] (fargs as (_::_)) = ([],fargs)
14709
d01983034ded tuned HOL/record package; enabled record_upd_simproc by default.
schirmer
parents: 14702
diff changeset
   521
      | splitargs (_::_) [] = raise TERM (msg ^ "expecting more fields", [t])
14700
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   522
      | splitargs _ _ = ([],[]);
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   523
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   524
    fun mk_ext (fargs as (name,arg)::_) =
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   525
         (case get_fieldext sg (Sign.intern_const sg name) of
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   526
            Some (ext,_) => (case get_extfields sg ext of
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   527
                               Some flds 
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   528
                                 => let val (args,rest) = 
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   529
                                               splitargs (map fst (but_last flds)) fargs;
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   530
                                        val more' = mk_ext rest;  
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   531
                                    in list_comb (Syntax.const (suffix sfx ext),args@[more'])
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   532
                                    end
14709
d01983034ded tuned HOL/record package; enabled record_upd_simproc by default.
schirmer
parents: 14702
diff changeset
   533
                             | None => raise TERM(msg ^ "no fields defined for "
14700
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   534
                                                   ^ ext,[t]))
14709
d01983034ded tuned HOL/record package; enabled record_upd_simproc by default.
schirmer
parents: 14702
diff changeset
   535
          | None => raise TERM (msg ^ name ^" is no proper field",[t]))
14700
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   536
      | mk_ext [] = more
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   537
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   538
  in mk_ext fieldargs end;   
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   539
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   540
fun gen_ext_type_tr sep mark sfx more sg t =
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   541
  let 
14709
d01983034ded tuned HOL/record package; enabled record_upd_simproc by default.
schirmer
parents: 14702
diff changeset
   542
    val msg = "error in record-type input: ";
14700
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   543
    val fieldargs = dest_ext_fields sep mark t; 
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   544
    fun splitargs (field::fields) ((name,arg)::fargs) =
14709
d01983034ded tuned HOL/record package; enabled record_upd_simproc by default.
schirmer
parents: 14702
diff changeset
   545
          if can (unsuffix name) field
14700
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   546
          then let val (args,rest) = splitargs fields fargs
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   547
               in (arg::args,rest) end
14709
d01983034ded tuned HOL/record package; enabled record_upd_simproc by default.
schirmer
parents: 14702
diff changeset
   548
          else raise TERM (msg ^ "expecting field " ^ field ^ " but got " ^ name, [t])
14700
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   549
      | splitargs [] (fargs as (_::_)) = ([],fargs)
14709
d01983034ded tuned HOL/record package; enabled record_upd_simproc by default.
schirmer
parents: 14702
diff changeset
   550
      | splitargs (_::_) [] = raise TERM (msg ^ "expecting more fields", [t])
14700
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   551
      | splitargs _ _ = ([],[]);
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   552
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   553
    fun get_sort xs n = (case assoc (xs,n) of 
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   554
                                Some s => s 
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   555
                              | None => Sign.defaultS sg);
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   556
    fun to_type t = Sign.intern_typ sg 
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   557
                      (Syntax.typ_of_term (get_sort (Syntax.raw_term_sorts t)) I t);
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   558
 
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   559
    val tsig = Sign.tsig_of sg;
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   560
    fun unify (t,env) = Type.unify tsig env t; 
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   561
    
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   562
    fun mk_ext (fargs as (name,arg)::_) =
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   563
         (case get_fieldext sg (Sign.intern_const sg name) of
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   564
            Some (ext,alphas) => 
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   565
              (case get_extfields sg ext of
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   566
                 Some flds 
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   567
                  => (let
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   568
                       val flds' = but_last flds;
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   569
                       val types = map snd flds'; 
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   570
                       val (args,rest) = splitargs (map fst flds') fargs;
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   571
                       val vartypes = map Type.varifyT types;
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   572
                       val argtypes = map to_type args;
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   573
                       val (subst,_) = foldr unify (vartypes ~~ argtypes,(Vartab.empty,0));
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   574
                       val alphas' = map ((Syntax.term_of_typ (! Syntax.show_sorts)) o 
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   575
                                          (Envir.norm_type subst) o Type.varifyT) 
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   576
                                         (but_last alphas);
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   577
 
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   578
                       val more' = mk_ext rest;   
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   579
                     in list_comb (Syntax.const (suffix sfx ext),alphas'@[more']) 
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   580
                     end handle TUNIFY => raise 
14709
d01983034ded tuned HOL/record package; enabled record_upd_simproc by default.
schirmer
parents: 14702
diff changeset
   581
                           TERM (msg ^ "type is no proper record (extension)", [t]))
d01983034ded tuned HOL/record package; enabled record_upd_simproc by default.
schirmer
parents: 14702
diff changeset
   582
               | None => raise TERM (msg ^ "no fields defined for " ^ ext,[t]))
d01983034ded tuned HOL/record package; enabled record_upd_simproc by default.
schirmer
parents: 14702
diff changeset
   583
          | None => raise TERM (msg ^ name ^" is no proper field",[t]))
14700
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   584
      | mk_ext [] = more
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   585
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   586
  in mk_ext fieldargs end;   
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   587
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   588
fun gen_adv_record_tr sep mark sfx unit sg [t] = 
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   589
      gen_ext_fields_tr sep mark sfx unit sg t
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   590
  | gen_adv_record_tr _ _ _ _ _ ts = raise TERM ("gen_record_tr", ts);
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   591
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   592
fun gen_adv_record_scheme_tr sep mark sfx sg [t, more] = 
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   593
      gen_ext_fields_tr sep mark sfx more sg t 
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   594
  | gen_adv_record_scheme_tr _ _ _ _ ts = raise TERM ("gen_record_scheme_tr", ts);
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   595
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   596
fun gen_adv_record_type_tr sep mark sfx unit sg [t] = 
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   597
      gen_ext_type_tr sep mark sfx unit sg t
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   598
  | gen_adv_record_type_tr _ _ _ _ _ ts = raise TERM ("gen_record_tr", ts);
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   599
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   600
fun gen_adv_record_type_scheme_tr sep mark sfx sg [t, more] = 
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   601
      gen_ext_type_tr sep mark sfx more sg t 
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   602
  | gen_adv_record_type_scheme_tr _ _ _ _ ts = raise TERM ("gen_record_scheme_tr", ts);
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   603
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   604
val adv_record_tr = gen_adv_record_tr "_fields" "_field" extN HOLogic.unit;
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   605
val adv_record_scheme_tr = gen_adv_record_scheme_tr "_fields" "_field" extN;
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   606
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   607
val adv_record_type_tr = 
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   608
      gen_adv_record_type_tr "_field_types" "_field_type" ext_typeN 
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   609
        (Syntax.term_of_typ false (HOLogic.unitT));
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   610
val adv_record_type_scheme_tr = 
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   611
      gen_adv_record_type_scheme_tr "_field_types" "_field_type" ext_typeN;
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   612
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   613
val parse_translation =
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   614
 [("_record_update", record_update_tr),
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   615
  ("_update_name", update_name_tr)];
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   616
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   617
val adv_parse_translation = 
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   618
 [("_record",adv_record_tr),
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   619
  ("_record_scheme",adv_record_scheme_tr),
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   620
  ("_record_type",adv_record_type_tr),
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   621
  ("_record_type_scheme",adv_record_type_scheme_tr)];
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   622
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   623
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   624
(* print translations *)
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   625
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   626
val print_record_type_abbr = ref true;
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   627
val print_record_type_as_fields = ref true;
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   628
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   629
fun gen_field_upds_tr' mark sfx (tm as Const (name_field, _) $ t $ u) =
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   630
    (case try (unsuffix sfx) name_field of
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   631
      Some name =>
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   632
        apfst (cons (Syntax.const mark $ Syntax.free name $ t)) (gen_field_upds_tr' mark sfx u)
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   633
     | None => ([], tm))
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   634
  | gen_field_upds_tr' _ _ tm = ([], tm);
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   635
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   636
fun record_update_tr' tm =
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   637
  let val (ts, u) = gen_field_upds_tr' "_update" updateN tm in
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   638
    Syntax.const "_record_update" $ u $
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   639
      foldr1 (fn (v, w) => Syntax.const "_updates" $ v $ w) (rev ts)
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   640
  end;
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   641
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   642
fun gen_field_tr' sfx tr' name =
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   643
  let val name_sfx = suffix sfx name
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   644
  in (name_sfx, fn [t, u] => tr' (Syntax.const name_sfx $ t $ u) | _ => raise Match) end;
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   645
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   646
fun record_tr' sep mark record record_scheme unit sg t =
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   647
  let 
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   648
    fun field_lst t =
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   649
      (case strip_comb t of
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   650
        (Const (ext,_),args) 
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   651
         => (case try (unsuffix extN) (Sign.intern_const sg ext) of
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   652
               Some ext' 
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   653
               => (case get_extfields sg ext' of
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   654
                     Some flds 
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   655
                     => (let
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   656
                          val (f::fs) = but_last (map fst flds);
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   657
                          val flds' = Sign.extern sg Sign.constK f::map NameSpace.base fs; 
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   658
                          val (args',more) = split_last args; 
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   659
                         in (flds'~~args')@field_lst more end
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   660
                         handle LIST _ => [("",t)]) 
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   661
                   | None => [("",t)])
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   662
             | None => [("",t)])
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   663
       | _ => [("",t)])
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   664
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   665
    val (flds,(_,more)) = split_last (field_lst t);
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   666
    val flds' = map (fn (n,t)=>Syntax.const mark$Syntax.const n$t) flds;
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   667
    val flds'' = foldr1 (fn (x,y) => Syntax.const sep$x$y) flds';
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   668
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   669
  in if null flds then raise Match
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   670
     else if unit more  
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   671
          then Syntax.const record$flds'' 
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   672
          else Syntax.const record_scheme$flds''$more
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   673
  end
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   674
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   675
fun gen_record_tr' name = 
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   676
  let val name_sfx = suffix extN name;
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   677
      val unit = (fn Const ("Unity",_) => true | _ => false);
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   678
      fun tr' sg ts = record_tr' "_fields" "_field" "_record" "_record_scheme" unit sg 
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   679
                       (list_comb (Syntax.const name_sfx,ts))
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   680
  in (name_sfx,tr')
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   681
  end
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   682
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   683
fun print_translation names =
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   684
  map (gen_field_tr' updateN record_update_tr') names;
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   685
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   686
(* record_type_abbr_tr' tries to reconstruct the record name type abbreviation from *)
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   687
(* the (nested) extension types.                                                    *)
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   688
fun record_type_abbr_tr' default_tr' abbr alphas zeta lastExt schemeT sg tm =
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   689
  let
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   690
      (* tm is term representation of a (nested) field type. We first reconstruct the      *)
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   691
      (* type from tm so that we can continue on the type level rather then the term level.*)
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   692
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   693
      fun get_sort xs n = (case assoc (xs,n) of 
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   694
                             Some s => s 
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   695
                           | None => Sign.defaultS sg);
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   696
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   697
      val T = Sign.intern_typ sg (Syntax.typ_of_term (get_sort (Syntax.raw_term_sorts tm)) I tm) 
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   698
      val tsig = Sign.tsig_of sg
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   699
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   700
      fun mk_type_abbr subst name alphas = 
14854
61bdf2ae4dc5 removed obsolete sort 'logic';
wenzelm
parents: 14709
diff changeset
   701
          let val abbrT = Type (name, map (fn a => TVar ((a, 0), [])) alphas);
14700
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   702
          in Syntax.term_of_typ (! Syntax.show_sorts) (Envir.norm_type subst abbrT) end;    
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   703
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   704
      fun unify rT T = fst (Type.unify tsig (Vartab.empty,0) (Type.varifyT rT,T))
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   705
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   706
   in if !print_record_type_abbr
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   707
      then (case last_extT T of
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   708
             Some (name,_) 
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   709
              => if name = lastExt 
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   710
                 then
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   711
		  (let val subst = unify schemeT T 
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   712
                   in 
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   713
                    if HOLogic.is_unitT (Envir.norm_type subst (TVar((zeta,0),Sign.defaultS sg)))
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   714
                    then mk_type_abbr subst abbr alphas
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   715
                    else mk_type_abbr subst (suffix schemeN abbr) (alphas@[zeta])
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   716
		   end handle TUNIFY => default_tr' sg tm)
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   717
                 else raise Match (* give print translation of specialised record a chance *)
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   718
            | _ => raise Match)
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   719
       else default_tr' sg tm
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   720
  end
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   721
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   722
fun record_type_tr' sep mark record record_scheme sg t =
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   723
  let
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   724
    fun get_sort xs n = (case assoc (xs,n) of 
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   725
                             Some s => s 
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   726
                           | None => Sign.defaultS sg);
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   727
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   728
    val T = Sign.intern_typ sg (Syntax.typ_of_term (get_sort (Syntax.raw_term_sorts t)) I t)
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   729
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   730
    val tsig = Sign.tsig_of sg
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   731
    fun unify (t,v) = Type.unify tsig v t;
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   732
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   733
    fun term_of_type T = Syntax.term_of_typ (!Syntax.show_sorts) (Sign.extern_typ sg T);
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   734
 
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   735
    fun field_lst T =
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   736
      (case T of
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   737
        Type (ext,args) 
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   738
         => (case try (unsuffix ext_typeN) ext of
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   739
               Some ext' 
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   740
               => (case get_extfields sg ext' of
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   741
                     Some flds 
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   742
                     => (case get_fieldext sg (fst (hd flds)) of
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   743
                           Some (_,alphas) 
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   744
                           => (let
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   745
                                val (f::fs) = but_last flds;
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   746
                                val flds' = apfst (Sign.extern sg Sign.constK) f
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   747
                                            ::map (apfst NameSpace.base) fs; 
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   748
                                val (args',more) = split_last args; 
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   749
                                val alphavars = map Type.varifyT (but_last alphas); 
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   750
                                val (subst,_)= foldr unify (alphavars~~args',(Vartab.empty,0));
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   751
                                val flds'' =map (apsnd ((Envir.norm_type subst)o(Type.varifyT)))
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   752
                                                flds';
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   753
                              in flds''@field_lst more end
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   754
                              handle TUNIFY => [("",T)] 
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   755
                                   | LIST _=> [("",T)])
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   756
                         | None => [("",T)])
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   757
                   | None => [("",T)])
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   758
             | None => [("",T)]) 
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   759
        | _ => [("",T)])
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   760
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   761
    val (flds,(_,moreT)) = split_last (field_lst T);
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   762
    val flds' = map (fn (n,T)=>Syntax.const mark$Syntax.const n$term_of_type T) flds;
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   763
    val flds'' = foldr1 (fn (x,y) => Syntax.const sep$x$y) flds';
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   764
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   765
  in if not (!print_record_type_as_fields) orelse null flds then raise Match
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   766
     else if moreT = HOLogic.unitT 
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   767
          then Syntax.const record$flds'' 
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   768
          else Syntax.const record_scheme$flds''$term_of_type moreT
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   769
  end
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   770
    
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   771
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   772
fun gen_record_type_tr' name = 
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   773
  let val name_sfx = suffix ext_typeN name;
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   774
      fun tr' sg ts = record_type_tr' "_field_types" "_field_type" 
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   775
                       "_record_type" "_record_type_scheme" sg 
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   776
                       (list_comb (Syntax.const name_sfx,ts))
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   777
  in (name_sfx,tr')
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   778
  end
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   779
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   780
     
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   781
fun gen_record_type_abbr_tr' abbr alphas zeta lastExt schemeT name =
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   782
  let val name_sfx = suffix ext_typeN name;
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   783
      val default_tr' = record_type_tr' "_field_types" "_field_type" 
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   784
                               "_record_type" "_record_type_scheme" 
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   785
      fun tr' sg ts = record_type_abbr_tr' default_tr' abbr alphas zeta lastExt schemeT sg
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   786
                         (list_comb (Syntax.const name_sfx,ts))
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   787
  in (name_sfx, tr') end;
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   788
15015
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   789
(** record simprocs **)
14358
233c5bd5b539 cleaning up
schirmer
parents: 14255
diff changeset
   790
15015
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   791
val record_quick_and_dirty_sensitive = ref false;
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   792
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   793
fun quick_and_dirty_prove sg asms prop tac =
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   794
  if !record_quick_and_dirty_sensitive andalso !quick_and_dirty
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   795
  then Tactic.prove sg [] [] (Logic.list_implies (map Logic.varify asms,Logic.varify prop))
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   796
        (K (SkipProof.cheat_tac HOL.thy))
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   797
        (* standard can take quite a while for large records, thats why
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   798
         * we varify the proposition manually here.*) 
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   799
  else Tactic.prove_standard sg [] asms prop tac;
4867
9be2bf0ce909 package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff changeset
   800
14255
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
   801
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
   802
fun prove_split_simp sg T prop =
14700
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   803
  (case get_splits sg (rec_id T) of
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   804
     Some (all_thm,_,_,_) 
15015
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   805
     => let
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   806
	   val {sel_upd={simpset,...},extsplit,...} = RecordsData.get_sg sg;
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   807
           val extsplits = 
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   808
                 foldl (fn (thms,(n,_)) => (list (Symtab.lookup (extsplit,n)))@thms) 
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   809
                    ([],dest_recTs T);
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   810
           val thms = all_thm::(case extsplits of [thm] => [] | _ => extsplits);
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   811
                                     (* [thm] is the same as all_thm *)
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   812
        in (quick_and_dirty_prove sg [] prop 
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   813
            (fn _ => (simp_tac (simpset addsimps thms) 1)))
14700
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   814
        end
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   815
   | _ => error "RecordPackage.prove_split_simp:code should never been reached")
7178
50b9849cf6ad record_simproc for sel-upd (by Sebastian Nanz);
wenzelm
parents: 6851
diff changeset
   816
15015
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   817
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   818
15059
schirmer
parents: 15058
diff changeset
   819
local
15058
cc8f1de3f86c added: get_extT_fields and
schirmer
parents: 15015
diff changeset
   820
15059
schirmer
parents: 15058
diff changeset
   821
fun get_fields extfields T = 
schirmer
parents: 15058
diff changeset
   822
     foldl (fn (xs,(eN,_))=>xs@(Symtab.lookup_multi (extfields,eN)))
schirmer
parents: 15058
diff changeset
   823
             ([],(dest_recTs T));
schirmer
parents: 15058
diff changeset
   824
in
14255
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
   825
(* record_simproc *)
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
   826
(* Simplifies selections of an record update:
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
   827
 *  (1)  S (r(|S:=k|)) = k respectively
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
   828
 *  (2)  S (r(|X:=k|)) = S r
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
   829
 * The simproc skips multiple updates at once, eg:
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
   830
 *  S (r (|S:=k,X:=2,Y:=3|)) = k
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
   831
 * But be careful in (2) because of the extendibility of records.
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
   832
 * - If S is a more-selector we have to make sure that the update on component
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
   833
 *   X does not affect the selected subrecord.
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
   834
 * - If X is a more-selector we have to make sure that S is not in the updated
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
   835
 *   subrecord. 
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
   836
 *)
13462
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 13419
diff changeset
   837
val record_simproc =
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 13419
diff changeset
   838
  Simplifier.simproc (Theory.sign_of HOL.thy) "record_simp" ["s (u k r)"]
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 13419
diff changeset
   839
    (fn sg => fn _ => fn t =>
15015
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   840
      (case t of (sel as Const (s, Type (_,[domS,rangeS])))$((upd as Const (u, _)) $ k $ r)=>
13462
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 13419
diff changeset
   841
        (case get_selectors sg s of Some () =>
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 13419
diff changeset
   842
          (case get_updates sg u of Some u_name =>
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 13419
diff changeset
   843
            let
14255
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
   844
              fun mk_abs_var x t = (x, fastype_of t);
14700
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
   845
              val {sel_upd={updates,...},extfields,...} = RecordsData.get_sg sg;
15015
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   846
              
14255
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
   847
              fun mk_eq_terms ((upd as Const (u,Type(_,[updT,_]))) $ k $ r) =
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
   848
		  (case (Symtab.lookup (updates,u)) of
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
   849
                     None => None
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
   850
                   | Some u_name 
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
   851
                     => if u_name = s
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
   852
                        then let 
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
   853
                               val rv = mk_abs_var "r" r
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
   854
                               val rb = Bound 0
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
   855
                               val kv = mk_abs_var "k" k
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
   856
                               val kb = Bound 1 
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
   857
                             in Some (upd$kb$rb,kb,[kv,rv],true) end
15058
cc8f1de3f86c added: get_extT_fields and
schirmer
parents: 15015
diff changeset
   858
                        else if u_name mem (map fst (get_fields extfields rangeS))
cc8f1de3f86c added: get_extT_fields and
schirmer
parents: 15015
diff changeset
   859
                             orelse s mem (map fst (get_fields extfields updT))
14255
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
   860
                             then None
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
   861
			     else (case mk_eq_terms r of 
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
   862
                                     Some (trm,trm',vars,update_s) 
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
   863
                                     => let   
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
   864
					  val kv = mk_abs_var "k" k
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
   865
                                          val kb = Bound (length vars)
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
   866
		                        in Some (upd$kb$trm,trm',kv::vars,update_s) end
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
   867
                                   | None
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
   868
                                     => let 
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
   869
					  val rv = mk_abs_var "r" r
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
   870
                                          val rb = Bound 0
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
   871
                                          val kv = mk_abs_var "k" k
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
   872
                                          val kb = Bound 1 
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
   873
                                        in Some (upd$kb$rb,rb,[kv,rv],false) end))
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
   874
                | mk_eq_terms r = None     
13462
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 13419
diff changeset
   875
            in
14255
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
   876
	      (case mk_eq_terms (upd$k$r) of
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
   877
                 Some (trm,trm',vars,update_s) 
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
   878
                 => if update_s 
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
   879
		    then Some (prove_split_simp sg domS 
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
   880
                                 (list_all(vars,(Logic.mk_equals (sel$trm,trm')))))
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
   881
                    else Some (prove_split_simp sg domS 
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
   882
                                 (list_all(vars,(Logic.mk_equals (sel$trm,sel$trm')))))
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
   883
               | None => None)
13462
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 13419
diff changeset
   884
            end
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 13419
diff changeset
   885
          | None => None)
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 13419
diff changeset
   886
        | None => None)
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 13419
diff changeset
   887
      | _ => None));
7178
50b9849cf6ad record_simproc for sel-upd (by Sebastian Nanz);
wenzelm
parents: 6851
diff changeset
   888
15015
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   889
(* record_upd_simproc *) 
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   890
(* simplify multiple updates:
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   891
 *  (1)  "r(|M:=3,N:=1,M:=2,N:=4|) == r(|M:=2,N:=4|)"
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   892
 *  (2)  "r(|M:= M r|) = r"
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   893
 * For (2) special care of "more" updates has to be taken:
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   894
 *    r(|more := m; A := A r|)
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   895
 * If A is contained in the fields of m we cannot remove the update A := A r!
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   896
 * (But r(|more := r; A := A (r(|more := r|))|) = r(|more := r|) 
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   897
*)
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   898
val record_upd_simproc =
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   899
  Simplifier.simproc (Theory.sign_of HOL.thy) "record_upd_simp" ["(u k r)"]
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   900
    (fn sg => fn _ => fn t =>
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   901
      (case t of ((upd as Const (u, Type(_,[_,Type(_,[T,_])]))) $ k $ r) =>
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   902
 	 let datatype ('a,'b) calc = Init of 'b | Inter of 'a  
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   903
             val {sel_upd={selectors,updates,...},extfields,...} = RecordsData.get_sg sg;
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   904
             
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   905
	     fun mk_abs_var x t = (x, fastype_of t);
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   906
             fun sel_name u = NameSpace.base (unsuffix updateN u);
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   907
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   908
             fun seed s (upd as Const (more,Type(_,[mT,_]))$ k $ r) =
15058
cc8f1de3f86c added: get_extT_fields and
schirmer
parents: 15015
diff changeset
   909
                  if s mem (map fst (get_fields extfields mT)) then upd else seed s r
15015
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   910
               | seed _ r = r;
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   911
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   912
             fun grow u uT k vars (sprout,skeleton) = 
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   913
		   if sel_name u = moreN
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   914
                   then let val kv = mk_abs_var "k" k;
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   915
                            val kb = Bound (length vars);
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   916
                        in ((Const (u,uT)$k$sprout,Const (u,uT)$kb$skeleton),kv::vars) end
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   917
                   else ((sprout,skeleton),vars);
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   918
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   919
             fun is_upd_same (sprout,skeleton) u ((sel as Const (s,_))$r) =
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   920
                   if (unsuffix updateN u) = s andalso (seed s sprout) = r 
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   921
                   then Some (sel,seed s skeleton)
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   922
                   else None
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   923
               | is_upd_same _ _ _ = None
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   924
 
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   925
             fun init_seed r = ((r,Bound 0), [mk_abs_var "r" r]);
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   926
                       
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   927
             (* mk_updterm returns either
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   928
              *  - Init (orig-term, orig-term-skeleton, vars) if no optimisation can be made,
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   929
              *     where vars are the bound variables in the skeleton 
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   930
              *  - Inter (orig-term-skeleton,simplified-term-skeleton, 
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   931
              *           vars, term-sprout, skeleton-sprout)
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   932
              *     where "All vars. orig-term-skeleton = simplified-term-skeleton" is
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   933
              *     the desired simplification rule,
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   934
              *     the sprouts accumulate the "more-updates" on the way from the seed
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   935
              *     to the outermost update. It is only relevant to calculate the 
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   936
              *     possible simplification for (2) 
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   937
              * The algorithm first walks down the updates to the seed-record while
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   938
              * memorising the updates in the already-table. While walking up the
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   939
              * updates again, the optimised term is constructed.
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   940
              *)
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   941
             fun mk_updterm upds already (t as ((upd as Const (u,uT)) $ k $ r)) =
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   942
		 if is_some (Symtab.lookup (upds,u))
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   943
		 then let 
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   944
			 fun rest already = mk_updterm upds already
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   945
		      in if is_some (Symtab.lookup (already,u)) 
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   946
			 then (case (rest already r) of
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   947
				 Init ((sprout,skel),vars) => 
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   948
                                 let
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   949
	                           val kv = mk_abs_var (sel_name u) k;
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   950
                                   val kb = Bound (length vars);
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   951
                                   val (sprout',vars')= grow u uT k (kv::vars) (sprout,skel);
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   952
                                 in Inter (upd$kb$skel,skel,vars',sprout') end
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   953
                               | Inter (trm,trm',vars,sprout) => 
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   954
                                 let 
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   955
		                   val kv = mk_abs_var (sel_name u) k;
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   956
                                   val kb = Bound (length vars);
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   957
                                   val (sprout',vars') = grow u uT k (kv::vars) sprout;
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   958
                                 in Inter(upd$kb$trm,trm',kv::vars',sprout') end) 
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   959
	                 else 
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   960
                          (case rest (Symtab.update ((u,()),already)) r of 
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   961
			     Init ((sprout,skel),vars) => 
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   962
                              (case is_upd_same (sprout,skel) u k of
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   963
                                 Some (sel,skel') => 
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   964
                                 let
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   965
		                   val (sprout',vars') = grow u uT k vars (sprout,skel); 
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   966
                                  in Inter(upd$(sel$skel')$skel,skel,vars',sprout') end
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   967
                               | None =>  
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   968
                                 let
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   969
	                           val kv = mk_abs_var (sel_name u) k;
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   970
                                   val kb = Bound (length vars);
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   971
                                 in Init ((upd$k$sprout,upd$kb$skel),kv::vars) end)
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   972
		           | Inter (trm,trm',vars,sprout) => 
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   973
                               (case is_upd_same sprout u k of
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   974
                                  Some (sel,skel) =>
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   975
                                  let
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   976
                                    val (sprout',vars') = grow u uT k vars sprout
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   977
                                  in Inter(upd$(sel$skel)$trm,trm',vars',sprout') end
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   978
                                | None =>
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   979
                                  let
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   980
				    val kv = mk_abs_var (sel_name u) k
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   981
                                    val kb = Bound (length vars)
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   982
                                    val (sprout',vars') = grow u uT k (kv::vars) sprout
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   983
                                  in Inter (upd$kb$trm,upd$kb$trm',vars',sprout') end))
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   984
		      end
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   985
		 else Init (init_seed t)
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   986
	       | mk_updterm _ _ t = Init (init_seed t);
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   987
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   988
	 in (case mk_updterm updates Symtab.empty t of
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   989
	       Inter (trm,trm',vars,_)
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   990
                => Some (prove_split_simp sg T  
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   991
                          (list_all(vars,(Logic.mk_equals (trm,trm')))))
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   992
             | _ => None)
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   993
	 end
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   994
       | _ => None));
15059
schirmer
parents: 15058
diff changeset
   995
end
15015
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
   996
14255
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
   997
(* record_eq_simproc *)
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
   998
(* looks up the most specific record-equality.
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
   999
 * Note on efficiency:
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
  1000
 * Testing equality of records boils down to the test of equality of all components.
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
  1001
 * Therefore the complexity is: #components * complexity for single component.
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
  1002
 * Especially if a record has a lot of components it may be better to split up
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
  1003
 * the record first and do simplification on that (record_split_simp_tac).
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
  1004
 * e.g. r(|lots of updates|) = x
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
  1005
 *
14700
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1006
 *               record_eq_simproc       record_split_simp_tac
14255
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
  1007
 * Complexity: #components * #updates     #updates   
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
  1008
 *           
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
  1009
 *)
14079
1c22e5499eeb - record_split_tac now also works for object-level universal quantifier
berghofe
parents: 13904
diff changeset
  1010
val record_eq_simproc =
1c22e5499eeb - record_split_tac now also works for object-level universal quantifier
berghofe
parents: 13904
diff changeset
  1011
  Simplifier.simproc (Theory.sign_of HOL.thy) "record_eq_simp" ["r = s"]
1c22e5499eeb - record_split_tac now also works for object-level universal quantifier
berghofe
parents: 13904
diff changeset
  1012
    (fn sg => fn _ => fn t =>
1c22e5499eeb - record_split_tac now also works for object-level universal quantifier
berghofe
parents: 13904
diff changeset
  1013
      (case t of Const ("op =", Type (_, [T, _])) $ _ $ _ =>
14700
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1014
        (case rec_id T of
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1015
           "" => None
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1016
         | name => (case get_equalities sg name of
14255
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
  1017
                                None => None
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
  1018
                              | Some thm => Some (thm RS Eq_TrueI)))
14079
1c22e5499eeb - record_split_tac now also works for object-level universal quantifier
berghofe
parents: 13904
diff changeset
  1019
       | _ => None));
1c22e5499eeb - record_split_tac now also works for object-level universal quantifier
berghofe
parents: 13904
diff changeset
  1020
14255
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
  1021
(* record_split_simproc *)
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
  1022
(* splits quantified occurrences of records, for which P holds. P can peek on the 
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
  1023
 * subterm starting at the quantified occurrence of the record (including the quantifier)
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
  1024
 *)
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
  1025
fun record_split_simproc P =
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
  1026
  Simplifier.simproc (Theory.sign_of HOL.thy) "record_split_simp" ["(a t)"]
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
  1027
    (fn sg => fn _ => fn t =>
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
  1028
      (case t of (Const (quantifier, Type (_, [Type (_, [T, _]), _])))$trm =>
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
  1029
         if quantifier = "All" orelse quantifier = "all" orelse quantifier = "Ex"
14700
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1030
         then (case rec_id T of
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1031
                 "" => None
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1032
               | name
14255
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
  1033
                  => if P t 
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
  1034
                     then (case get_splits sg name of
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
  1035
                             None => None
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
  1036
                           | Some (all_thm, All_thm, Ex_thm,_) 
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
  1037
                              => Some (case quantifier of
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
  1038
                                         "all" => all_thm
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
  1039
                                       | "All" => All_thm RS HOL.eq_reflection
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
  1040
                                       | "Ex"  => Ex_thm RS HOL.eq_reflection
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
  1041
                                       | _     => error "record_split_simproc"))
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
  1042
                     else None)
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
  1043
         else None
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
  1044
       | _ => None))
7178
50b9849cf6ad record_simproc for sel-upd (by Sebastian Nanz);
wenzelm
parents: 6851
diff changeset
  1045
14427
cea7d2f76112 added record_ex_sel_eq_simproc
schirmer
parents: 14358
diff changeset
  1046
val record_ex_sel_eq_simproc =
cea7d2f76112 added record_ex_sel_eq_simproc
schirmer
parents: 14358
diff changeset
  1047
  Simplifier.simproc (Theory.sign_of HOL.thy) "record_ex_sel_eq_simproc" ["Ex t"]
cea7d2f76112 added record_ex_sel_eq_simproc
schirmer
parents: 14358
diff changeset
  1048
    (fn sg => fn _ => fn t =>
14959
schirmer
parents: 14854
diff changeset
  1049
       let 
15015
c5768e8c4da4 * record_upd_simproc also simplifies trivial updates:
schirmer
parents: 15012
diff changeset
  1050
         fun prove prop = (quick_and_dirty_prove sg [] prop 
14427
cea7d2f76112 added record_ex_sel_eq_simproc
schirmer
parents: 14358
diff changeset
  1051
                             (fn _ => (simp_tac ((get_simpset sg) addsimps simp_thms
14959
schirmer
parents: 14854
diff changeset
  1052
                                        addsimprocs [record_split_simproc (K true)]) 1)));
schirmer
parents: 14854
diff changeset
  1053
schirmer
parents: 14854
diff changeset
  1054
         fun mkeq (lr,Teq,(sel,Tsel),x) i =
schirmer
parents: 14854
diff changeset
  1055
              (case get_selectors sg sel of Some () =>
schirmer
parents: 14854
diff changeset
  1056
                 let val x' = if not (loose_bvar1 (x,0)) 
schirmer
parents: 14854
diff changeset
  1057
                              then Free ("x" ^ string_of_int i, range_type Tsel) 
schirmer
parents: 14854
diff changeset
  1058
                              else raise TERM ("",[x]);
schirmer
parents: 14854
diff changeset
  1059
                     val sel' = Const (sel,Tsel)$Bound 0;
schirmer
parents: 14854
diff changeset
  1060
                     val (l,r) = if lr then (sel',x') else (x',sel');
schirmer
parents: 14854
diff changeset
  1061
                  in Const ("op =",Teq)$l$r end
schirmer
parents: 14854
diff changeset
  1062
               | None => raise TERM ("",[Const (sel,Tsel)]));
schirmer
parents: 14854
diff changeset
  1063
schirmer
parents: 14854
diff changeset
  1064
         fun dest_sel_eq (Const ("op =",Teq)$(Const (sel,Tsel)$Bound 0)$X) = 
schirmer
parents: 14854
diff changeset
  1065
                           (true,Teq,(sel,Tsel),X)
schirmer
parents: 14854
diff changeset
  1066
           | dest_sel_eq (Const ("op =",Teq)$X$(Const (sel,Tsel)$Bound 0)) =
schirmer
parents: 14854
diff changeset
  1067
                           (false,Teq,(sel,Tsel),X)
schirmer
parents: 14854
diff changeset
  1068
           | dest_sel_eq _ = raise TERM ("",[]);
schirmer
parents: 14854
diff changeset
  1069
schirmer
parents: 14854
diff changeset
  1070
       in         
14427
cea7d2f76112 added record_ex_sel_eq_simproc
schirmer
parents: 14358
diff changeset
  1071
         (case t of 
14959
schirmer
parents: 14854
diff changeset
  1072
           (Const ("Ex",Tex)$Abs(s,T,t)) =>
schirmer
parents: 14854
diff changeset
  1073
             let val eq = mkeq (dest_sel_eq t) 0;
schirmer
parents: 14854
diff changeset
  1074
                 val prop = list_all ([("r",T)],
schirmer
parents: 14854
diff changeset
  1075
                              Logic.mk_equals (Const ("Ex",Tex)$Abs(s,T,eq),
schirmer
parents: 14854
diff changeset
  1076
                                               HOLogic.true_const));
schirmer
parents: 14854
diff changeset
  1077
             in Some (prove prop) end
schirmer
parents: 14854
diff changeset
  1078
             handle TERM _ => None
schirmer
parents: 14854
diff changeset
  1079
          | _ => None)                      
14427
cea7d2f76112 added record_ex_sel_eq_simproc
schirmer
parents: 14358
diff changeset
  1080
         end)
cea7d2f76112 added record_ex_sel_eq_simproc
schirmer
parents: 14358
diff changeset
  1081
5698
2b5d9bdec5af field types: datatype;
wenzelm
parents: 5290
diff changeset
  1082
14700
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1083
    
6358
92dbe243555f outer syntax for 'record';
wenzelm
parents: 6092
diff changeset
  1084
14255
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
  1085
local
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
  1086
val inductive_atomize = thms "induct_atomize";
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
  1087
val inductive_rulify1 = thms "induct_rulify1";
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
  1088
in
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
  1089
(* record_split_simp_tac *)
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
  1090
(* splits (and simplifies) all records in the goal for which P holds. 
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
  1091
 * For quantified occurrences of a record
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
  1092
 * P can peek on the whole subterm (including the quantifier); for free variables P
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
  1093
 * can only peek on the variable itself. 
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
  1094
 *)
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
  1095
fun record_split_simp_tac P i st =
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
  1096
  let
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
  1097
    val sg = Thm.sign_of_thm st;
14700
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1098
    val {sel_upd={simpset,...},...} 
14255
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
  1099
            = RecordsData.get_sg sg;
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
  1100
14700
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1101
    val has_rec = exists_Const
14255
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
  1102
      (fn (s, Type (_, [Type (_, [T, _]), _])) =>
14700
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1103
          (s = "all" orelse s = "All" orelse s = "Ex") andalso is_recT T 
14255
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
  1104
        | _ => false);
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
  1105
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
  1106
    val goal = Library.nth_elem (i - 1, Thm.prems_of st);
14700
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1107
    val frees = filter (is_recT o type_of) (term_frees goal);
14255
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
  1108
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
  1109
    fun mk_split_free_tac free induct_thm i = 
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
  1110
	let val cfree = cterm_of sg free;
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
  1111
            val (_$(_$r)) = concl_of induct_thm;
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
  1112
            val crec = cterm_of sg r;
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
  1113
            val thm  = cterm_instantiate [(crec,cfree)] induct_thm;
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
  1114
        in EVERY [simp_tac (HOL_basic_ss addsimps inductive_atomize) i,
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
  1115
                  rtac thm i,
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
  1116
                  simp_tac (HOL_basic_ss addsimps inductive_rulify1) i]
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
  1117
	end;
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
  1118
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
  1119
    fun split_free_tac P i (free as Free (n,T)) = 
14700
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1120
	(case rec_id T of
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1121
           "" => None
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1122
         | name => if P free 
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1123
                   then (case get_splits sg name of
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1124
                           None => None
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1125
                         | Some (_,_,_,induct_thm)
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1126
                             => Some (mk_split_free_tac free induct_thm i))
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1127
                   else None)
14255
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
  1128
     | split_free_tac _ _ _ = None;
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
  1129
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
  1130
    val split_frees_tacs = mapfilter (split_free_tac P i) frees;
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
  1131
   
14700
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1132
    val simprocs = if has_rec goal then [record_split_simproc P] else [];
14255
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
  1133
   
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
  1134
  in st |> (EVERY split_frees_tacs) 
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
  1135
           THEN (Simplifier.full_simp_tac (simpset addsimprocs simprocs) i)
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
  1136
  end handle Library.LIST _ => Seq.empty;
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
  1137
end;
e6e3e3f0deed Records:
schirmer
parents: 14079
diff changeset
  1138
14700
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1139
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1140
(* record_split_tac *)
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1141
(* splits all records in the goal, which are quantified by ! or !!. *)
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1142
fun record_split_tac i st =
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1143
  let
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1144
    val sg = Thm.sign_of_thm st;
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1145
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1146
    val has_rec = exists_Const
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1147
      (fn (s, Type (_, [Type (_, [T, _]), _])) =>
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1148
          (s = "all" orelse s = "All") andalso is_recT T 
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1149
        | _ => false);
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1150
 
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1151
    val goal = Library.nth_elem (i - 1, Thm.prems_of st);   
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1152
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1153
    fun is_all t =
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1154
      (case t of (Const (quantifier, _)$_) =>
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1155
         quantifier = "All" orelse quantifier = "all"
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1156
       | _ => false);
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1157
 
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1158
  in if has_rec goal 
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1159
     then Simplifier.full_simp_tac 
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1160
           (HOL_basic_ss addsimprocs [record_split_simproc is_all]) i st 
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1161
     else Seq.empty
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1162
  end handle Library.LIST _ => Seq.empty;
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1163
6358
92dbe243555f outer syntax for 'record';
wenzelm
parents: 6092
diff changeset
  1164
(* wrapper *)
92dbe243555f outer syntax for 'record';
wenzelm
parents: 6092
diff changeset
  1165
5707
b0e631634b5a field_injects [iffs];
wenzelm
parents: 5698
diff changeset
  1166
val record_split_name = "record_split_tac";
b0e631634b5a field_injects [iffs];
wenzelm
parents: 5698
diff changeset
  1167
val record_split_wrapper = (record_split_name, fn tac => record_split_tac ORELSE' tac);
5698
2b5d9bdec5af field types: datatype;
wenzelm
parents: 5290
diff changeset
  1168
4867
9be2bf0ce909 package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff changeset
  1169
(** theory extender interface **)
9be2bf0ce909 package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff changeset
  1170
9be2bf0ce909 package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff changeset
  1171
(* prepare arguments *)
9be2bf0ce909 package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff changeset
  1172
4894
32187e0b8b48 'more' selector;
wenzelm
parents: 4890
diff changeset
  1173
(*note: read_raw_typ avoids expanding type abbreviations*)
4867
9be2bf0ce909 package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff changeset
  1174
fun read_raw_parent sign s =
9be2bf0ce909 package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff changeset
  1175
  (case Sign.read_raw_typ (sign, K None) s handle TYPE (msg, _, _) => error msg of
9be2bf0ce909 package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff changeset
  1176
    Type (name, Ts) => (Ts, name)
9be2bf0ce909 package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff changeset
  1177
  | _ => error ("Bad parent record specification: " ^ quote s));
9be2bf0ce909 package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff changeset
  1178
9be2bf0ce909 package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff changeset
  1179
fun read_typ sign (env, s) =
9be2bf0ce909 package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff changeset
  1180
  let
5060
7b86df67cc1a def_sort;
wenzelm
parents: 5052
diff changeset
  1181
    fun def_sort (x, ~1) = assoc (env, x)
7b86df67cc1a def_sort;
wenzelm
parents: 5052
diff changeset
  1182
      | def_sort _ = None;
7b86df67cc1a def_sort;
wenzelm
parents: 5052
diff changeset
  1183
    val T = Type.no_tvars (Sign.read_typ (sign, def_sort) s) handle TYPE (msg, _, _) => error msg;
4867
9be2bf0ce909 package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff changeset
  1184
  in (Term.add_typ_tfrees (T, env), T) end;
9be2bf0ce909 package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff changeset
  1185
9be2bf0ce909 package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff changeset
  1186
fun cert_typ sign (env, raw_T) =
9be2bf0ce909 package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff changeset
  1187
  let val T = Type.no_tvars (Sign.certify_typ sign raw_T) handle TYPE (msg, _, _) => error msg
9be2bf0ce909 package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff changeset
  1188
  in (Term.add_typ_tfrees (T, env), T) end;
9be2bf0ce909 package extensible records with structural subtyping in HOL -- still
wenzelm
parents:
diff changeset
  1189
14700
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1190
(* attributes *)
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1191
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1192
fun case_names_fields x = RuleCases.case_names ["fields"] x;
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1193
fun induct_type_global name = [case_names_fields, InductAttrib.induct_type_global name];
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1194
fun cases_type_global name = [case_names_fields, InductAttrib.cases_type_global name];
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1195
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1196
(* tactics *)
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1197
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1198
fun simp_all_tac ss simps = ALLGOALS (Simplifier.asm_full_simp_tac (ss addsimps simps));
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1199
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1200
(* do case analysis / induction according to rule on last parameter of ith subgoal 
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1201
 * (or on s if there are no parameters); 
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1202
 * Instatiation of record variable (and predicate) in rule is calculated to
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1203
 * avoid problems with higher order unification. 
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1204
 *)
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1205
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1206
fun try_param_tac s rule i st =
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1207
  let
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1208
    val cert = cterm_of (Thm.sign_of_thm st);
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1209
    val g = nth_elem (i - 1, prems_of st);
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1210
    val params = Logic.strip_params g;
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1211
    val concl = HOLogic.dest_Trueprop (Logic.strip_assums_concl g);
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1212
    val rule' = Thm.lift_rule (st, i) rule;
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1213
    val (P, ys) = strip_comb (HOLogic.dest_Trueprop
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1214
      (Logic.strip_assums_concl (prop_of rule')));
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1215
    (* ca indicates if rule is a case analysis or induction rule *)
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1216
    val (x, ca) = (case rev (drop (length params, ys)) of
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1217
        [] => (head_of (fst (HOLogic.dest_eq (HOLogic.dest_Trueprop
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1218
          (hd (rev (Logic.strip_assums_hyp (hd (prems_of rule')))))))), true)
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1219
      | [x] => (head_of x, false));
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1220
    val rule'' = cterm_instantiate (map (pairself cert) (case (rev params) of
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1221
        [] => (case assoc (map dest_Free (term_frees (prop_of st)), s) of
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1222
          None => sys_error "try_param_tac: no such variable"
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1223
        | Some T => [(P, if ca then concl else lambda (Free (s, T)) concl),
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1224
            (x, Free (s, T))])
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1225
      | (_, T) :: _ => [(P, list_abs (params, if ca then concl
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1226
          else incr_boundvars 1 (Abs (s, T, concl)))),
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1227
        (x, list_abs (params, Bound 0))])) rule'
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1228
  in compose_tac (false, rule'', nprems_of rule) i st end;
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1229
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1230
fun extension_typedef name repT alphas thy =
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1231
  let
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1232
    val UNIV = HOLogic.mk_UNIV repT;
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1233
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1234
    val (thy',{set_def=Some def, Abs_induct = abs_induct, 
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1235
               Abs_inject=abs_inject, Abs_inverse = abs_inverse,...}) =
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1236
        thy |> setmp TypedefPackage.quiet_mode true
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1237
           (TypedefPackage.add_typedef_i true None
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1238
             (suffix ext_typeN (Sign.base_name name), alphas, Syntax.NoSyn) UNIV None
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1239
             (Tactic.rtac UNIV_witness 1))
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1240
    val rewrite_rule = Tactic.rewrite_rule [def, rec_UNIV_I, rec_True_simp];
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1241
  in (thy',map rewrite_rule [abs_inject, abs_inverse, abs_induct])
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1242
  end;
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1243
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1244
fun extension_definition full name fields names alphas zeta moreT more vars thy = 
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1245
  let  
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1246
    val base = Sign.base_name;
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1247
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1248
    val fieldTs = (map snd fields);
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1249
    val alphas_zetaTs = map (fn n => TFree (n, HOLogic.typeS)) (alphas@[zeta]);
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1250
    val extT_name = suffix ext_typeN name
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1251
    val extT = Type (extT_name, alphas_zetaTs);
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1252
    val repT = foldr1 HOLogic.mk_prodT (fieldTs@[moreT]);
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1253
    val fields_more = fields@[(full moreN,moreT)];
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1254
    val bfields_more = map (apfst base) fields_more;
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1255
    val r = Free (rN,extT)
2f885b7e5ba7 reimplementation of HOL records; only one type is created for
schirmer
parents: 14643
diff changeset
  1256
    val len = length fields;