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