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