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