src/Pure/consts.ML
author wenzelm
Mon, 11 Dec 2023 11:46:12 +0100
changeset 79237 f97fe7ad62a7
parent 77979 a12c48fbf10f
child 79449 e6fb110d6e44
permissions -rw-r--r--
proper ZTerm.lift_proof (amending 4a1a25bdf81d);
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
18060
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/consts.ML
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
     3
18935
f22be3d61ed5 added abbreviations;
wenzelm
parents: 18163
diff changeset
     4
Polymorphic constants: declarations, abbreviations, additional type
f22be3d61ed5 added abbreviations;
wenzelm
parents: 18163
diff changeset
     5
constraints.
18060
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
     6
*)
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
     7
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
     8
signature CONSTS =
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
     9
sig
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    10
  type T
30424
692279df7cc2 Consts.eq_const is back again (cf. 907da436f8a9) -- required in ProofContext.transfer_syntax to prevent expensive merges of local_consts/global_consts;
wenzelm
parents: 30364
diff changeset
    11
  val eq_consts: T * T -> bool
56056
4d46d53566e6 more efficient local theory operations, by imposing a linear change discipline on the main types/consts tables, in order to speed-up Proof_Context.transfer_syntax required for Local_Theory.raw_theory_result;
wenzelm
parents: 56025
diff changeset
    12
  val change_base: bool -> T -> T
56139
b7add947a6ef more frugal recording of changes: join merely requires information from one side;
wenzelm
parents: 56062
diff changeset
    13
  val change_ignore: T -> T
30566
9643f54c4184 reverted abbreviations: improved performance via Item_Net.T;
wenzelm
parents: 30466
diff changeset
    14
  val retrieve_abbrevs: T -> string list -> term -> (term * term) list
18935
f22be3d61ed5 added abbreviations;
wenzelm
parents: 18163
diff changeset
    15
  val dest: T ->
56025
d74fed45fa8b abstract type Name_Space.table;
wenzelm
parents: 56008
diff changeset
    16
   {const_space: Name_Space.T,
56062
8ae2965ddc80 tuned signature;
wenzelm
parents: 56056
diff changeset
    17
    constants: (string * (typ * term option)) list,
8ae2965ddc80 tuned signature;
wenzelm
parents: 56056
diff changeset
    18
    constraints: (string * typ) list}
43794
49cbbe2768a8 sub-structural sharing after Syntax.check phase, with global interning of logical entities (the latter is relevant when bypassing default parsing via YXML);
wenzelm
parents: 43552
diff changeset
    19
  val the_const: T -> string -> string * typ                   (*exception TYPE*)
25048
5a94a87af697 the_abbreviation: return plain rhs only;
wenzelm
parents: 25041
diff changeset
    20
  val the_abbreviation: T -> string -> typ * term              (*exception TYPE*)
5a94a87af697 the_abbreviation: return plain rhs only;
wenzelm
parents: 25041
diff changeset
    21
  val type_scheme: T -> string -> typ                          (*exception TYPE*)
74291
b83fa8f3a271 ML antiquotations for type constructors and term constants;
wenzelm
parents: 74266
diff changeset
    22
  val type_arguments: T -> string -> int list list             (*exception TYPE*)
25048
5a94a87af697 the_abbreviation: return plain rhs only;
wenzelm
parents: 25041
diff changeset
    23
  val is_monomorphic: T -> string -> bool                      (*exception TYPE*)
5a94a87af697 the_abbreviation: return plain rhs only;
wenzelm
parents: 25041
diff changeset
    24
  val the_constraint: T -> string -> typ                       (*exception TYPE*)
33095
bbd52d2f8696 renamed NameSpace to Name_Space -- also to emphasize its subtle change in semantics;
wenzelm
parents: 33092
diff changeset
    25
  val space_of: T -> Name_Space.T
35680
897740382442 aliases for class/type/const;
wenzelm
parents: 35554
diff changeset
    26
  val alias: Name_Space.naming -> binding -> string -> T -> T
33158
6e3dc0ba2b06 conceal consts via name space, not tags;
wenzelm
parents: 33097
diff changeset
    27
  val is_concealed: T -> string -> bool
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
    28
  val intern: T -> xstring -> string
35255
2cb27605301f authentic syntax for *all* term constants;
wenzelm
parents: 33373
diff changeset
    29
  val intern_syntax: T -> xstring -> string
55956
94d384d621b0 reject internal term names outright, and complete consts instead;
wenzelm
parents: 55950
diff changeset
    30
  val check_const: Context.generic -> T -> xstring * Position.T list -> term * Position.report list
61262
7bd1eb4b056e tuned signature: eliminated pointless type Context.pretty;
wenzelm
parents: 56139
diff changeset
    31
  val certify: Context.generic -> Type.tsig -> bool -> T -> term -> term  (*exception TYPE*)
18146
47463b1825c6 uncurried Consts.typargs;
wenzelm
parents: 18117
diff changeset
    32
  val typargs: T -> string * typ -> typ list
18163
9b729737bf14 added instance;
wenzelm
parents: 18146
diff changeset
    33
  val instance: T -> string * typ list -> typ
70785
edaeb8feb4d0 proper replacement for (map_types (K dummyT));
wenzelm
parents: 63632
diff changeset
    34
  val dummy_types: T -> term -> term
47005
421760a1efe7 maintain generic context naming in structure Name_Space (NB: empty = default_naming, init = local_naming);
wenzelm
parents: 45666
diff changeset
    35
  val declare: Context.generic -> binding * typ -> T -> T
19098
fc736dbbe333 constrain: assert const declaration, optional type (i.e. may delete constraints);
wenzelm
parents: 19027
diff changeset
    36
  val constrain: string * typ option -> T -> T
47005
421760a1efe7 maintain generic context naming in structure Name_Space (NB: empty = default_naming, init = local_naming);
wenzelm
parents: 45666
diff changeset
    37
  val abbreviate: Context.generic -> Type.tsig -> string -> binding * term -> T -> (term * term) * T
25048
5a94a87af697 the_abbreviation: return plain rhs only;
wenzelm
parents: 25041
diff changeset
    38
  val revert_abbrev: string -> string -> T -> T
18060
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    39
  val hide: bool -> string -> T -> T
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    40
  val empty: T
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    41
  val merge: T * T -> T
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
    42
end;
18060
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    43
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    44
structure Consts: CONSTS =
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    45
struct
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    46
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
    47
(** consts type **)
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
    48
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
    49
(* datatype T *)
18060
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    50
35255
2cb27605301f authentic syntax for *all* term constants;
wenzelm
parents: 33373
diff changeset
    51
type decl = {T: typ, typargs: int list list};
24909
afae05eb1f1c maintain typargs for abbrevs as well;
wenzelm
parents: 24772
diff changeset
    52
type abbrev = {rhs: term, normal_rhs: term, force_expand: bool};
18935
f22be3d61ed5 added abbreviations;
wenzelm
parents: 18163
diff changeset
    53
18060
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    54
datatype T = Consts of
33095
bbd52d2f8696 renamed NameSpace to Name_Space -- also to emphasize its subtle change in semantics;
wenzelm
parents: 33092
diff changeset
    55
 {decls: (decl * abbrev option) Name_Space.table,
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
    56
  constraints: typ Symtab.table,
30566
9643f54c4184 reverted abbreviations: improved performance via Item_Net.T;
wenzelm
parents: 30466
diff changeset
    57
  rev_abbrevs: (term * term) Item_Net.T Symtab.table};
18060
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    58
30424
692279df7cc2 Consts.eq_const is back again (cf. 907da436f8a9) -- required in ProofContext.transfer_syntax to prevent expensive merges of local_consts/global_consts;
wenzelm
parents: 30364
diff changeset
    59
fun eq_consts
692279df7cc2 Consts.eq_const is back again (cf. 907da436f8a9) -- required in ProofContext.transfer_syntax to prevent expensive merges of local_consts/global_consts;
wenzelm
parents: 30364
diff changeset
    60
   (Consts {decls = decls1, constraints = constraints1, rev_abbrevs = rev_abbrevs1},
692279df7cc2 Consts.eq_const is back again (cf. 907da436f8a9) -- required in ProofContext.transfer_syntax to prevent expensive merges of local_consts/global_consts;
wenzelm
parents: 30364
diff changeset
    61
    Consts {decls = decls2, constraints = constraints2, rev_abbrevs = rev_abbrevs2}) =
692279df7cc2 Consts.eq_const is back again (cf. 907da436f8a9) -- required in ProofContext.transfer_syntax to prevent expensive merges of local_consts/global_consts;
wenzelm
parents: 30364
diff changeset
    62
  pointer_eq (decls1, decls2) andalso
692279df7cc2 Consts.eq_const is back again (cf. 907da436f8a9) -- required in ProofContext.transfer_syntax to prevent expensive merges of local_consts/global_consts;
wenzelm
parents: 30364
diff changeset
    63
  pointer_eq (constraints1, constraints2) andalso
692279df7cc2 Consts.eq_const is back again (cf. 907da436f8a9) -- required in ProofContext.transfer_syntax to prevent expensive merges of local_consts/global_consts;
wenzelm
parents: 30364
diff changeset
    64
  pointer_eq (rev_abbrevs1, rev_abbrevs2);
692279df7cc2 Consts.eq_const is back again (cf. 907da436f8a9) -- required in ProofContext.transfer_syntax to prevent expensive merges of local_consts/global_consts;
wenzelm
parents: 30364
diff changeset
    65
24673
62b75508eb66 certify: do_expand as explicit argument, actually certify type of abstractions;
wenzelm
parents: 23655
diff changeset
    66
fun make_consts (decls, constraints, rev_abbrevs) =
30284
907da436f8a9 eliminated Consts.eq_consts tuning -- this is built into tables and name spaces already;
wenzelm
parents: 30280
diff changeset
    67
  Consts {decls = decls, constraints = constraints, rev_abbrevs = rev_abbrevs};
18060
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    68
30284
907da436f8a9 eliminated Consts.eq_consts tuning -- this is built into tables and name spaces already;
wenzelm
parents: 30280
diff changeset
    69
fun map_consts f (Consts {decls, constraints, rev_abbrevs}) =
24673
62b75508eb66 certify: do_expand as explicit argument, actually certify type of abstractions;
wenzelm
parents: 23655
diff changeset
    70
  make_consts (f (decls, constraints, rev_abbrevs));
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
    71
56056
4d46d53566e6 more efficient local theory operations, by imposing a linear change discipline on the main types/consts tables, in order to speed-up Proof_Context.transfer_syntax required for Local_Theory.raw_theory_result;
wenzelm
parents: 56025
diff changeset
    72
fun change_base begin = map_consts (fn (decls, constraints, rev_abbrevs) =>
4d46d53566e6 more efficient local theory operations, by imposing a linear change discipline on the main types/consts tables, in order to speed-up Proof_Context.transfer_syntax required for Local_Theory.raw_theory_result;
wenzelm
parents: 56025
diff changeset
    73
  (Name_Space.change_base begin decls, constraints, rev_abbrevs));
4d46d53566e6 more efficient local theory operations, by imposing a linear change discipline on the main types/consts tables, in order to speed-up Proof_Context.transfer_syntax required for Local_Theory.raw_theory_result;
wenzelm
parents: 56025
diff changeset
    74
56139
b7add947a6ef more frugal recording of changes: join merely requires information from one side;
wenzelm
parents: 56062
diff changeset
    75
val change_ignore = map_consts (fn (decls, constraints, rev_abbrevs) =>
b7add947a6ef more frugal recording of changes: join merely requires information from one side;
wenzelm
parents: 56062
diff changeset
    76
  (Name_Space.change_ignore decls, constraints, rev_abbrevs));
b7add947a6ef more frugal recording of changes: join merely requires information from one side;
wenzelm
parents: 56062
diff changeset
    77
30566
9643f54c4184 reverted abbreviations: improved performance via Item_Net.T;
wenzelm
parents: 30466
diff changeset
    78
9643f54c4184 reverted abbreviations: improved performance via Item_Net.T;
wenzelm
parents: 30466
diff changeset
    79
(* reverted abbrevs *)
9643f54c4184 reverted abbreviations: improved performance via Item_Net.T;
wenzelm
parents: 30466
diff changeset
    80
33173
b8ca12f6681a eliminated obsolete tags for types/consts -- now handled via name space, in strongly typed fashion;
wenzelm
parents: 33158
diff changeset
    81
val empty_abbrevs =
33373
674df68d4df0 adapted Item_Net;
wenzelm
parents: 33173
diff changeset
    82
  Item_Net.init (fn ((t, u), (t', u')) => t aconv t' andalso u aconv u') (single o #1);
30566
9643f54c4184 reverted abbreviations: improved performance via Item_Net.T;
wenzelm
parents: 30466
diff changeset
    83
33373
674df68d4df0 adapted Item_Net;
wenzelm
parents: 33173
diff changeset
    84
fun update_abbrevs mode abbrs =
674df68d4df0 adapted Item_Net;
wenzelm
parents: 33173
diff changeset
    85
  Symtab.map_default (mode, empty_abbrevs) (Item_Net.update abbrs);
30566
9643f54c4184 reverted abbreviations: improved performance via Item_Net.T;
wenzelm
parents: 30466
diff changeset
    86
9643f54c4184 reverted abbreviations: improved performance via Item_Net.T;
wenzelm
parents: 30466
diff changeset
    87
fun retrieve_abbrevs (Consts {rev_abbrevs, ...}) modes =
63632
a59d9b81be24 Item_Net.retrieve_matching requires beta-eta normal form (amending 8976c5bc9e97);
wenzelm
parents: 63573
diff changeset
    88
  let val nets = map_filter (Symtab.lookup rev_abbrevs) modes in
a59d9b81be24 Item_Net.retrieve_matching requires beta-eta normal form (amending 8976c5bc9e97);
wenzelm
parents: 63573
diff changeset
    89
    fn t =>
a59d9b81be24 Item_Net.retrieve_matching requires beta-eta normal form (amending 8976c5bc9e97);
wenzelm
parents: 63573
diff changeset
    90
      let
a59d9b81be24 Item_Net.retrieve_matching requires beta-eta normal form (amending 8976c5bc9e97);
wenzelm
parents: 63573
diff changeset
    91
        val retrieve =
a59d9b81be24 Item_Net.retrieve_matching requires beta-eta normal form (amending 8976c5bc9e97);
wenzelm
parents: 63573
diff changeset
    92
          if Term.could_beta_eta_contract t
a59d9b81be24 Item_Net.retrieve_matching requires beta-eta normal form (amending 8976c5bc9e97);
wenzelm
parents: 63573
diff changeset
    93
          then Item_Net.retrieve
a59d9b81be24 Item_Net.retrieve_matching requires beta-eta normal form (amending 8976c5bc9e97);
wenzelm
parents: 63573
diff changeset
    94
          else Item_Net.retrieve_matching
a59d9b81be24 Item_Net.retrieve_matching requires beta-eta normal form (amending 8976c5bc9e97);
wenzelm
parents: 63573
diff changeset
    95
      in maps (fn net => retrieve net t) nets end
a59d9b81be24 Item_Net.retrieve_matching requires beta-eta normal form (amending 8976c5bc9e97);
wenzelm
parents: 63573
diff changeset
    96
  end;
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
    97
18965
3b76383e3ab3 renamed space to space_of;
wenzelm
parents: 18935
diff changeset
    98
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
    99
(* dest consts *)
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   100
56025
d74fed45fa8b abstract type Name_Space.table;
wenzelm
parents: 56008
diff changeset
   101
fun dest (Consts {decls, constraints, ...}) =
d74fed45fa8b abstract type Name_Space.table;
wenzelm
parents: 56008
diff changeset
   102
 {const_space = Name_Space.space_of_table decls,
d74fed45fa8b abstract type Name_Space.table;
wenzelm
parents: 56008
diff changeset
   103
  constants =
d74fed45fa8b abstract type Name_Space.table;
wenzelm
parents: 56008
diff changeset
   104
    Name_Space.fold_table (fn (c, ({T, ...}, abbr)) =>
56062
8ae2965ddc80 tuned signature;
wenzelm
parents: 56056
diff changeset
   105
      cons (c, (T, Option.map #rhs abbr))) decls [],
8ae2965ddc80 tuned signature;
wenzelm
parents: 56056
diff changeset
   106
  constraints = Symtab.dest constraints};
18060
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   107
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   108
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   109
(* lookup consts *)
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   110
56025
d74fed45fa8b abstract type Name_Space.table;
wenzelm
parents: 56008
diff changeset
   111
fun the_entry (Consts {decls, ...}) c =
d74fed45fa8b abstract type Name_Space.table;
wenzelm
parents: 56008
diff changeset
   112
  (case Name_Space.lookup_key decls c of
43794
49cbbe2768a8 sub-structural sharing after Syntax.check phase, with global interning of logical entities (the latter is relevant when bypassing default parsing via YXML);
wenzelm
parents: 43552
diff changeset
   113
    SOME entry => entry
25041
c1efae25ee76 renamed the_declaration to the_type;
wenzelm
parents: 25037
diff changeset
   114
  | NONE => raise TYPE ("Unknown constant: " ^ quote c, [], []));
18935
f22be3d61ed5 added abbreviations;
wenzelm
parents: 18163
diff changeset
   115
43794
49cbbe2768a8 sub-structural sharing after Syntax.check phase, with global interning of logical entities (the latter is relevant when bypassing default parsing via YXML);
wenzelm
parents: 43552
diff changeset
   116
fun the_const consts c =
49cbbe2768a8 sub-structural sharing after Syntax.check phase, with global interning of logical entities (the latter is relevant when bypassing default parsing via YXML);
wenzelm
parents: 43552
diff changeset
   117
  (case the_entry consts c of
49cbbe2768a8 sub-structural sharing after Syntax.check phase, with global interning of logical entities (the latter is relevant when bypassing default parsing via YXML);
wenzelm
parents: 43552
diff changeset
   118
    (c', ({T, ...}, NONE)) => (c', T)
21720
059e6b8cee8e abbreviate: always authentic, force expansion of internal abbreviations;
wenzelm
parents: 21694
diff changeset
   119
  | _ => raise TYPE ("Not a logical constant: " ^ quote c, [], []));
18060
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   120
21720
059e6b8cee8e abbreviate: always authentic, force expansion of internal abbreviations;
wenzelm
parents: 21694
diff changeset
   121
fun the_abbreviation consts c =
43794
49cbbe2768a8 sub-structural sharing after Syntax.check phase, with global interning of logical entities (the latter is relevant when bypassing default parsing via YXML);
wenzelm
parents: 43552
diff changeset
   122
  (case the_entry consts c of
49cbbe2768a8 sub-structural sharing after Syntax.check phase, with global interning of logical entities (the latter is relevant when bypassing default parsing via YXML);
wenzelm
parents: 43552
diff changeset
   123
    (_, ({T, ...}, SOME {rhs, ...})) => (T, rhs)
21720
059e6b8cee8e abbreviate: always authentic, force expansion of internal abbreviations;
wenzelm
parents: 21694
diff changeset
   124
  | _ => raise TYPE ("Not an abbreviated constant: " ^ quote c, [], []));
059e6b8cee8e abbreviate: always authentic, force expansion of internal abbreviations;
wenzelm
parents: 21694
diff changeset
   125
43794
49cbbe2768a8 sub-structural sharing after Syntax.check phase, with global interning of logical entities (the latter is relevant when bypassing default parsing via YXML);
wenzelm
parents: 43552
diff changeset
   126
fun the_decl consts = #1 o #2 o the_entry consts;
25041
c1efae25ee76 renamed the_declaration to the_type;
wenzelm
parents: 25037
diff changeset
   127
val type_scheme = #T oo the_decl;
c1efae25ee76 renamed the_declaration to the_type;
wenzelm
parents: 25037
diff changeset
   128
val type_arguments = #typargs oo the_decl;
c1efae25ee76 renamed the_declaration to the_type;
wenzelm
parents: 25037
diff changeset
   129
21720
059e6b8cee8e abbreviate: always authentic, force expansion of internal abbreviations;
wenzelm
parents: 21694
diff changeset
   130
val is_monomorphic = null oo type_arguments;
18935
f22be3d61ed5 added abbreviations;
wenzelm
parents: 18163
diff changeset
   131
30284
907da436f8a9 eliminated Consts.eq_consts tuning -- this is built into tables and name spaces already;
wenzelm
parents: 30280
diff changeset
   132
fun the_constraint (consts as Consts {constraints, ...}) c =
18060
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   133
  (case Symtab.lookup constraints c of
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   134
    SOME T => T
25041
c1efae25ee76 renamed the_declaration to the_type;
wenzelm
parents: 25037
diff changeset
   135
  | NONE => type_scheme consts c);
18935
f22be3d61ed5 added abbreviations;
wenzelm
parents: 18163
diff changeset
   136
f22be3d61ed5 added abbreviations;
wenzelm
parents: 18163
diff changeset
   137
19657
25eaa3660123 added syntax interface;
wenzelm
parents: 19576
diff changeset
   138
(* name space and syntax *)
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   139
56025
d74fed45fa8b abstract type Name_Space.table;
wenzelm
parents: 56008
diff changeset
   140
fun space_of (Consts {decls, ...}) = Name_Space.space_of_table decls;
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   141
56025
d74fed45fa8b abstract type Name_Space.table;
wenzelm
parents: 56008
diff changeset
   142
fun alias naming binding name = map_consts (fn (decls, constraints, rev_abbrevs) =>
77979
a12c48fbf10f back to more elementary concept of aliases as adhoc change of accesses, but now with "suppress" information (see also 31ea5c1f874d);
wenzelm
parents: 77970
diff changeset
   143
  ((Name_Space.alias_table naming binding name decls), constraints, rev_abbrevs));
35680
897740382442 aliases for class/type/const;
wenzelm
parents: 35554
diff changeset
   144
33158
6e3dc0ba2b06 conceal consts via name space, not tags;
wenzelm
parents: 33097
diff changeset
   145
val is_concealed = Name_Space.is_concealed o space_of;
6e3dc0ba2b06 conceal consts via name space, not tags;
wenzelm
parents: 33097
diff changeset
   146
33095
bbd52d2f8696 renamed NameSpace to Name_Space -- also to emphasize its subtle change in semantics;
wenzelm
parents: 33092
diff changeset
   147
val intern = Name_Space.intern o space_of;
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   148
35554
1e05ea0a5cd7 added extern_syntax;
wenzelm
parents: 35262
diff changeset
   149
fun intern_syntax consts s =
42290
b1f544c84040 discontinued special treatment of structure Lexicon;
wenzelm
parents: 42083
diff changeset
   150
  (case try Lexicon.unmark_const s of
35255
2cb27605301f authentic syntax for *all* term constants;
wenzelm
parents: 33373
diff changeset
   151
    SOME c => c
35554
1e05ea0a5cd7 added extern_syntax;
wenzelm
parents: 35262
diff changeset
   152
  | NONE => intern consts s);
1e05ea0a5cd7 added extern_syntax;
wenzelm
parents: 35262
diff changeset
   153
18060
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   154
55843
3fa61f39d1f2 prefer Name_Space.check with its builtin reports (including completion);
wenzelm
parents: 50768
diff changeset
   155
(* check_const *)
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   156
55956
94d384d621b0 reject internal term names outright, and complete consts instead;
wenzelm
parents: 55950
diff changeset
   157
fun check_const context consts (xname, ps) =
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   158
  let
55843
3fa61f39d1f2 prefer Name_Space.check with its builtin reports (including completion);
wenzelm
parents: 50768
diff changeset
   159
    val Consts {decls, ...} = consts;
55956
94d384d621b0 reject internal term names outright, and complete consts instead;
wenzelm
parents: 55950
diff changeset
   160
    val ((c, reports), _) = Name_Space.check_reports context decls (xname, ps);
94d384d621b0 reject internal term names outright, and complete consts instead;
wenzelm
parents: 55950
diff changeset
   161
    val T = type_scheme consts c handle TYPE (msg, _, _) => error (msg ^ Position.here_list ps);
55950
3bb5c7179234 clarified treatment of consts -- prefer value-oriented reports;
wenzelm
parents: 55843
diff changeset
   162
  in (Const (c, T), reports) end;
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   163
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   164
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   165
(* certify *)
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   166
61262
7bd1eb4b056e tuned signature: eliminated pointless type Context.pretty;
wenzelm
parents: 56139
diff changeset
   167
fun certify context tsig do_expand consts =
18965
3b76383e3ab3 renamed space to space_of;
wenzelm
parents: 18935
diff changeset
   168
  let
3b76383e3ab3 renamed space to space_of;
wenzelm
parents: 18935
diff changeset
   169
    fun err msg (c, T) =
42383
0ae4ad40d7b5 simplified pretty printing context, which is only required for certain kernel operations;
wenzelm
parents: 42381
diff changeset
   170
      raise TYPE (msg ^ " " ^ quote c ^ " :: " ^
61262
7bd1eb4b056e tuned signature: eliminated pointless type Context.pretty;
wenzelm
parents: 56139
diff changeset
   171
        Syntax.string_of_typ (Syntax.init_pretty context) T, [], []);
24673
62b75508eb66 certify: do_expand as explicit argument, actually certify type of abstractions;
wenzelm
parents: 23655
diff changeset
   172
    val certT = Type.cert_typ tsig;
18965
3b76383e3ab3 renamed space to space_of;
wenzelm
parents: 18935
diff changeset
   173
    fun cert tm =
3b76383e3ab3 renamed space to space_of;
wenzelm
parents: 18935
diff changeset
   174
      let
3b76383e3ab3 renamed space to space_of;
wenzelm
parents: 18935
diff changeset
   175
        val (head, args) = Term.strip_comb tm;
21694
wenzelm
parents: 21680
diff changeset
   176
        val args' = map cert args;
wenzelm
parents: 21680
diff changeset
   177
        fun comb head' = Term.list_comb (head', args');
18965
3b76383e3ab3 renamed space to space_of;
wenzelm
parents: 18935
diff changeset
   178
      in
3b76383e3ab3 renamed space to space_of;
wenzelm
parents: 18935
diff changeset
   179
        (case head of
24673
62b75508eb66 certify: do_expand as explicit argument, actually certify type of abstractions;
wenzelm
parents: 23655
diff changeset
   180
          Abs (x, T, t) => comb (Abs (x, certT T, cert t))
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   181
        | Const (c, T) =>
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   182
            let
24673
62b75508eb66 certify: do_expand as explicit argument, actually certify type of abstractions;
wenzelm
parents: 23655
diff changeset
   183
              val T' = certT T;
43794
49cbbe2768a8 sub-structural sharing after Syntax.check phase, with global interning of logical entities (the latter is relevant when bypassing default parsing via YXML);
wenzelm
parents: 43552
diff changeset
   184
              val (_, ({T = U, ...}, abbr)) = the_entry consts c;
25048
5a94a87af697 the_abbreviation: return plain rhs only;
wenzelm
parents: 25041
diff changeset
   185
              fun expand u =
5a94a87af697 the_abbreviation: return plain rhs only;
wenzelm
parents: 25041
diff changeset
   186
                Term.betapplys (Envir.expand_atom T' (U, u) handle TYPE _ =>
5a94a87af697 the_abbreviation: return plain rhs only;
wenzelm
parents: 25041
diff changeset
   187
                  err "Illegal type for abbreviation" (c, T), args');
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   188
            in
19433
c7a2b7a8c4cb certify: ignore sort constraints of declarations (MAJOR CHANGE);
wenzelm
parents: 19364
diff changeset
   189
              if not (Type.raw_instance (T', U)) then
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   190
                err "Illegal type for constant" (c, T)
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   191
              else
24909
afae05eb1f1c maintain typargs for abbrevs as well;
wenzelm
parents: 24772
diff changeset
   192
                (case abbr of
25048
5a94a87af697 the_abbreviation: return plain rhs only;
wenzelm
parents: 25041
diff changeset
   193
                  SOME {rhs, normal_rhs, force_expand} =>
5a94a87af697 the_abbreviation: return plain rhs only;
wenzelm
parents: 25041
diff changeset
   194
                    if do_expand then expand normal_rhs
5a94a87af697 the_abbreviation: return plain rhs only;
wenzelm
parents: 25041
diff changeset
   195
                    else if force_expand then expand rhs
21720
059e6b8cee8e abbreviate: always authentic, force expansion of internal abbreviations;
wenzelm
parents: 21694
diff changeset
   196
                    else comb head
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   197
                | _ => comb head)
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   198
            end
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   199
        | _ => comb head)
18965
3b76383e3ab3 renamed space to space_of;
wenzelm
parents: 18935
diff changeset
   200
      end;
3b76383e3ab3 renamed space to space_of;
wenzelm
parents: 18935
diff changeset
   201
  in cert end;
3b76383e3ab3 renamed space to space_of;
wenzelm
parents: 18935
diff changeset
   202
3b76383e3ab3 renamed space to space_of;
wenzelm
parents: 18935
diff changeset
   203
18060
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   204
(* typargs -- view actual const type as instance of declaration *)
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   205
24909
afae05eb1f1c maintain typargs for abbrevs as well;
wenzelm
parents: 24772
diff changeset
   206
local
afae05eb1f1c maintain typargs for abbrevs as well;
wenzelm
parents: 24772
diff changeset
   207
afae05eb1f1c maintain typargs for abbrevs as well;
wenzelm
parents: 24772
diff changeset
   208
fun args_of (Type (_, Ts)) pos = args_of_list Ts 0 pos
afae05eb1f1c maintain typargs for abbrevs as well;
wenzelm
parents: 24772
diff changeset
   209
  | args_of (TVar v) pos = insert (eq_fst op =) (v, rev pos)
afae05eb1f1c maintain typargs for abbrevs as well;
wenzelm
parents: 24772
diff changeset
   210
  | args_of (TFree _) _ = I
afae05eb1f1c maintain typargs for abbrevs as well;
wenzelm
parents: 24772
diff changeset
   211
and args_of_list (T :: Ts) i is = args_of T (i :: is) #> args_of_list Ts (i + 1) is
afae05eb1f1c maintain typargs for abbrevs as well;
wenzelm
parents: 24772
diff changeset
   212
  | args_of_list [] _ _ = I;
afae05eb1f1c maintain typargs for abbrevs as well;
wenzelm
parents: 24772
diff changeset
   213
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   214
fun subscript (Type (_, Ts)) (i :: is) = subscript (nth Ts i) is
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   215
  | subscript T [] = T
32784
1a5dde5079ac eliminated redundant bindings;
wenzelm
parents: 31977
diff changeset
   216
  | subscript _ _ = raise Subscript;
18060
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   217
24909
afae05eb1f1c maintain typargs for abbrevs as well;
wenzelm
parents: 24772
diff changeset
   218
in
afae05eb1f1c maintain typargs for abbrevs as well;
wenzelm
parents: 24772
diff changeset
   219
afae05eb1f1c maintain typargs for abbrevs as well;
wenzelm
parents: 24772
diff changeset
   220
fun typargs_of T = map #2 (rev (args_of T [] []));
afae05eb1f1c maintain typargs for abbrevs as well;
wenzelm
parents: 24772
diff changeset
   221
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   222
fun typargs consts (c, T) = map (subscript T) (type_arguments consts c);
18060
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   223
24909
afae05eb1f1c maintain typargs for abbrevs as well;
wenzelm
parents: 24772
diff changeset
   224
end;
afae05eb1f1c maintain typargs for abbrevs as well;
wenzelm
parents: 24772
diff changeset
   225
18163
9b729737bf14 added instance;
wenzelm
parents: 18146
diff changeset
   226
fun instance consts (c, Ts) =
9b729737bf14 added instance;
wenzelm
parents: 18146
diff changeset
   227
  let
25041
c1efae25ee76 renamed the_declaration to the_type;
wenzelm
parents: 25037
diff changeset
   228
    val declT = type_scheme consts c;
74220
c49134ee16c1 more scalable data structure (but: rarely used many arguments);
wenzelm
parents: 70785
diff changeset
   229
    val args = typargs consts (c, declT);
c49134ee16c1 more scalable data structure (but: rarely used many arguments);
wenzelm
parents: 70785
diff changeset
   230
    val inst =
74266
612b7e0d6721 clarified signature;
wenzelm
parents: 74232
diff changeset
   231
      TVars.build (fold2 (fn a => fn T => TVars.add (Term.dest_TVar a, T)) args Ts)
612b7e0d6721 clarified signature;
wenzelm
parents: 74232
diff changeset
   232
        handle ListPair.UnequalLengths => raise TYPE ("Consts.instance", Ts, [Const (c, dummyT)]);
31977
e03059ae2d82 renamed structure TermSubst to Term_Subst;
wenzelm
parents: 30568
diff changeset
   233
  in declT |> Term_Subst.instantiateT inst end;
18163
9b729737bf14 added instance;
wenzelm
parents: 18146
diff changeset
   234
70785
edaeb8feb4d0 proper replacement for (map_types (K dummyT));
wenzelm
parents: 63632
diff changeset
   235
fun dummy_types consts =
edaeb8feb4d0 proper replacement for (map_types (K dummyT));
wenzelm
parents: 63632
diff changeset
   236
  let
edaeb8feb4d0 proper replacement for (map_types (K dummyT));
wenzelm
parents: 63632
diff changeset
   237
    fun dummy (Const (c, T)) =
edaeb8feb4d0 proper replacement for (map_types (K dummyT));
wenzelm
parents: 63632
diff changeset
   238
          Const (c, instance consts (c, replicate (length (typargs consts (c, T))) dummyT))
edaeb8feb4d0 proper replacement for (map_types (K dummyT));
wenzelm
parents: 63632
diff changeset
   239
      | dummy (Free (x, _)) = Free (x, dummyT)
edaeb8feb4d0 proper replacement for (map_types (K dummyT));
wenzelm
parents: 63632
diff changeset
   240
      | dummy (Var (xi, _)) = Var (xi, dummyT)
edaeb8feb4d0 proper replacement for (map_types (K dummyT));
wenzelm
parents: 63632
diff changeset
   241
      | dummy (b as Bound _) = b
edaeb8feb4d0 proper replacement for (map_types (K dummyT));
wenzelm
parents: 63632
diff changeset
   242
      | dummy (t $ u) = dummy t $ dummy u
edaeb8feb4d0 proper replacement for (map_types (K dummyT));
wenzelm
parents: 63632
diff changeset
   243
      | dummy (Abs (a, _, b)) = Abs (a, dummyT, dummy b);
edaeb8feb4d0 proper replacement for (map_types (K dummyT));
wenzelm
parents: 63632
diff changeset
   244
  in dummy end;
edaeb8feb4d0 proper replacement for (map_types (K dummyT));
wenzelm
parents: 63632
diff changeset
   245
18060
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   246
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   247
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   248
(** build consts **)
18060
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   249
18935
f22be3d61ed5 added abbreviations;
wenzelm
parents: 18163
diff changeset
   250
(* name space *)
18060
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   251
24673
62b75508eb66 certify: do_expand as explicit argument, actually certify type of abstractions;
wenzelm
parents: 23655
diff changeset
   252
fun hide fully c = map_consts (fn (decls, constraints, rev_abbrevs) =>
56025
d74fed45fa8b abstract type Name_Space.table;
wenzelm
parents: 56008
diff changeset
   253
  (Name_Space.hide_table fully c decls, constraints, rev_abbrevs));
18935
f22be3d61ed5 added abbreviations;
wenzelm
parents: 18163
diff changeset
   254
f22be3d61ed5 added abbreviations;
wenzelm
parents: 18163
diff changeset
   255
f22be3d61ed5 added abbreviations;
wenzelm
parents: 18163
diff changeset
   256
(* declarations *)
f22be3d61ed5 added abbreviations;
wenzelm
parents: 18163
diff changeset
   257
47005
421760a1efe7 maintain generic context naming in structure Name_Space (NB: empty = default_naming, init = local_naming);
wenzelm
parents: 45666
diff changeset
   258
fun declare context (b, declT) =
33092
c859019d3ac5 eliminated separate stamp -- NameSpace.define/merge etc. ensure uniqueness already;
wenzelm
parents: 32784
diff changeset
   259
  map_consts (fn (decls, constraints, rev_abbrevs) =>
c859019d3ac5 eliminated separate stamp -- NameSpace.define/merge etc. ensure uniqueness already;
wenzelm
parents: 32784
diff changeset
   260
    let
35255
2cb27605301f authentic syntax for *all* term constants;
wenzelm
parents: 33373
diff changeset
   261
      val decl = {T = declT, typargs = typargs_of declT};
41254
78c3e472bb35 extra checking of name bindings for classes, types, consts;
wenzelm
parents: 40722
diff changeset
   262
      val _ = Binding.check b;
47005
421760a1efe7 maintain generic context naming in structure Name_Space (NB: empty = default_naming, init = local_naming);
wenzelm
parents: 45666
diff changeset
   263
      val (_, decls') = decls |> Name_Space.define context true (b, (decl, NONE));
33092
c859019d3ac5 eliminated separate stamp -- NameSpace.define/merge etc. ensure uniqueness already;
wenzelm
parents: 32784
diff changeset
   264
    in (decls', constraints, rev_abbrevs) end);
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   265
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   266
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   267
(* constraints *)
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   268
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   269
fun constrain (c, C) consts =
24673
62b75508eb66 certify: do_expand as explicit argument, actually certify type of abstractions;
wenzelm
parents: 23655
diff changeset
   270
  consts |> map_consts (fn (decls, constraints, rev_abbrevs) =>
43794
49cbbe2768a8 sub-structural sharing after Syntax.check phase, with global interning of logical entities (the latter is relevant when bypassing default parsing via YXML);
wenzelm
parents: 43552
diff changeset
   271
    (#2 (the_entry consts c) handle TYPE (msg, _, _) => error msg;
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   272
      (decls,
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   273
        constraints |> (case C of SOME T => Symtab.update (c, T) | NONE => Symtab.delete_safe c),
24673
62b75508eb66 certify: do_expand as explicit argument, actually certify type of abstractions;
wenzelm
parents: 23655
diff changeset
   274
        rev_abbrevs)));
18060
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   275
18935
f22be3d61ed5 added abbreviations;
wenzelm
parents: 18163
diff changeset
   276
f22be3d61ed5 added abbreviations;
wenzelm
parents: 18163
diff changeset
   277
(* abbreviations *)
f22be3d61ed5 added abbreviations;
wenzelm
parents: 18163
diff changeset
   278
19027
adf6fb0db28a added eq_consts;
wenzelm
parents: 18992
diff changeset
   279
local
adf6fb0db28a added eq_consts;
wenzelm
parents: 18992
diff changeset
   280
30568
e6a55291102e strip_abss: always strip abstractions as far as possible, without keeping alternatives (which appear to be redundant anyway, but cause significant slowdown since discrimination nets collapse abstractions);
wenzelm
parents: 30566
diff changeset
   281
fun strip_abss (t as Abs (x, T, b)) =
42083
e1209fc7ecdc added Term.is_open and Term.is_dependent convenience, to cover common situations of loose bounds;
wenzelm
parents: 41254
diff changeset
   282
      if Term.is_dependent b then strip_abss b |>> cons (x, T)  (* FIXME decr!? *)
30568
e6a55291102e strip_abss: always strip abstractions as far as possible, without keeping alternatives (which appear to be redundant anyway, but cause significant slowdown since discrimination nets collapse abstractions);
wenzelm
parents: 30566
diff changeset
   283
      else ([], t)
e6a55291102e strip_abss: always strip abstractions as far as possible, without keeping alternatives (which appear to be redundant anyway, but cause significant slowdown since discrimination nets collapse abstractions);
wenzelm
parents: 30566
diff changeset
   284
  | strip_abss t = ([], t);
19027
adf6fb0db28a added eq_consts;
wenzelm
parents: 18992
diff changeset
   285
21720
059e6b8cee8e abbreviate: always authentic, force expansion of internal abbreviations;
wenzelm
parents: 21694
diff changeset
   286
fun rev_abbrev lhs rhs =
18060
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   287
  let
30568
e6a55291102e strip_abss: always strip abstractions as far as possible, without keeping alternatives (which appear to be redundant anyway, but cause significant slowdown since discrimination nets collapse abstractions);
wenzelm
parents: 30566
diff changeset
   288
    val (xs, body) = strip_abss (Envir.beta_eta_contract rhs);
e6a55291102e strip_abss: always strip abstractions as far as possible, without keeping alternatives (which appear to be redundant anyway, but cause significant slowdown since discrimination nets collapse abstractions);
wenzelm
parents: 30566
diff changeset
   289
    val vars = fold (fn (x, T) => cons (Var ((x, 0), T))) (Term.rename_wrt_term body xs) [];
e6a55291102e strip_abss: always strip abstractions as far as possible, without keeping alternatives (which appear to be redundant anyway, but cause significant slowdown since discrimination nets collapse abstractions);
wenzelm
parents: 30566
diff changeset
   290
  in (Term.subst_bounds (rev vars, body), Term.list_comb (lhs, vars)) end;
19027
adf6fb0db28a added eq_consts;
wenzelm
parents: 18992
diff changeset
   291
adf6fb0db28a added eq_consts;
wenzelm
parents: 18992
diff changeset
   292
in
18965
3b76383e3ab3 renamed space to space_of;
wenzelm
parents: 18935
diff changeset
   293
47005
421760a1efe7 maintain generic context naming in structure Name_Space (NB: empty = default_naming, init = local_naming);
wenzelm
parents: 45666
diff changeset
   294
fun abbreviate context tsig mode (b, raw_rhs) consts =
18965
3b76383e3ab3 renamed space to space_of;
wenzelm
parents: 18935
diff changeset
   295
  let
61262
7bd1eb4b056e tuned signature: eliminated pointless type Context.pretty;
wenzelm
parents: 56139
diff changeset
   296
    val cert_term = certify context tsig false consts;
7bd1eb4b056e tuned signature: eliminated pointless type Context.pretty;
wenzelm
parents: 56139
diff changeset
   297
    val expand_term = certify context tsig true consts;
37146
f652333bbf8e renamed structure PrintMode to Print_Mode, keeping the old name as legacy alias for some time;
wenzelm
parents: 35680
diff changeset
   298
    val force_expand = mode = Print_Mode.internal;
21720
059e6b8cee8e abbreviate: always authentic, force expansion of internal abbreviations;
wenzelm
parents: 21694
diff changeset
   299
30286
cf89a03ee308 Consts.abbreviate: reject schematic term variables, prevent schematic type variables (hidden polymorphism) via Term.close_schematic_term -- see also 8f84a608883d;
wenzelm
parents: 30284
diff changeset
   300
    val _ = Term.exists_subterm Term.is_Var raw_rhs andalso
42381
309ec68442c6 added Binding.print convenience, which includes quote already;
wenzelm
parents: 42375
diff changeset
   301
      error ("Illegal schematic variables on rhs of abbreviation " ^ Binding.print b);
30286
cf89a03ee308 Consts.abbreviate: reject schematic term variables, prevent schematic type variables (hidden polymorphism) via Term.close_schematic_term -- see also 8f84a608883d;
wenzelm
parents: 30284
diff changeset
   302
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   303
    val rhs = raw_rhs
20548
8ef25fe585a8 renamed Term.map_term_types to Term.map_types (cf. Term.fold_types);
wenzelm
parents: 20509
diff changeset
   304
      |> Term.map_types (Type.cert_typ tsig)
30286
cf89a03ee308 Consts.abbreviate: reject schematic term variables, prevent schematic type variables (hidden polymorphism) via Term.close_schematic_term -- see also 8f84a608883d;
wenzelm
parents: 30284
diff changeset
   305
      |> cert_term
cf89a03ee308 Consts.abbreviate: reject schematic term variables, prevent schematic type variables (hidden polymorphism) via Term.close_schematic_term -- see also 8f84a608883d;
wenzelm
parents: 30284
diff changeset
   306
      |> Term.close_schematic_term;
25404
1a58d1c9fe88 simplified Consts.dest;
wenzelm
parents: 25389
diff changeset
   307
    val normal_rhs = expand_term rhs;
21794
1a9f57f1bc3a abbreviate: tuned signature;
wenzelm
parents: 21720
diff changeset
   308
    val T = Term.fastype_of rhs;
47005
421760a1efe7 maintain generic context naming in structure Name_Space (NB: empty = default_naming, init = local_naming);
wenzelm
parents: 45666
diff changeset
   309
    val lhs = Const (Name_Space.full_name (Name_Space.naming_of context) b, T);
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   310
  in
24673
62b75508eb66 certify: do_expand as explicit argument, actually certify type of abstractions;
wenzelm
parents: 23655
diff changeset
   311
    consts |> map_consts (fn (decls, constraints, rev_abbrevs) =>
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   312
      let
35255
2cb27605301f authentic syntax for *all* term constants;
wenzelm
parents: 33373
diff changeset
   313
        val decl = {T = T, typargs = typargs_of T};
25404
1a58d1c9fe88 simplified Consts.dest;
wenzelm
parents: 25389
diff changeset
   314
        val abbr = {rhs = rhs, normal_rhs = normal_rhs, force_expand = force_expand};
41254
78c3e472bb35 extra checking of name bindings for classes, types, consts;
wenzelm
parents: 40722
diff changeset
   315
        val _ = Binding.check b;
28861
f53abb0733ee using name bindings
haftmann
parents: 28017
diff changeset
   316
        val (_, decls') = decls
47005
421760a1efe7 maintain generic context naming in structure Name_Space (NB: empty = default_naming, init = local_naming);
wenzelm
parents: 45666
diff changeset
   317
          |> Name_Space.define context true (b, (decl, SOME abbr));
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   318
        val rev_abbrevs' = rev_abbrevs
33373
674df68d4df0 adapted Item_Net;
wenzelm
parents: 33173
diff changeset
   319
          |> update_abbrevs mode (rev_abbrev lhs rhs);
24673
62b75508eb66 certify: do_expand as explicit argument, actually certify type of abstractions;
wenzelm
parents: 23655
diff changeset
   320
      in (decls', constraints, rev_abbrevs') end)
21794
1a9f57f1bc3a abbreviate: tuned signature;
wenzelm
parents: 21720
diff changeset
   321
    |> pair (lhs, rhs)
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   322
  end;
18935
f22be3d61ed5 added abbreviations;
wenzelm
parents: 18163
diff changeset
   323
25048
5a94a87af697 the_abbreviation: return plain rhs only;
wenzelm
parents: 25041
diff changeset
   324
fun revert_abbrev mode c consts = consts |> map_consts (fn (decls, constraints, rev_abbrevs) =>
5a94a87af697 the_abbreviation: return plain rhs only;
wenzelm
parents: 25041
diff changeset
   325
  let
5a94a87af697 the_abbreviation: return plain rhs only;
wenzelm
parents: 25041
diff changeset
   326
    val (T, rhs) = the_abbreviation consts c;
5a94a87af697 the_abbreviation: return plain rhs only;
wenzelm
parents: 25041
diff changeset
   327
    val rev_abbrevs' = rev_abbrevs
33373
674df68d4df0 adapted Item_Net;
wenzelm
parents: 33173
diff changeset
   328
      |> update_abbrevs mode (rev_abbrev (Const (c, T)) rhs);
25048
5a94a87af697 the_abbreviation: return plain rhs only;
wenzelm
parents: 25041
diff changeset
   329
  in (decls, constraints, rev_abbrevs') end);
5a94a87af697 the_abbreviation: return plain rhs only;
wenzelm
parents: 25041
diff changeset
   330
19027
adf6fb0db28a added eq_consts;
wenzelm
parents: 18992
diff changeset
   331
end;
adf6fb0db28a added eq_consts;
wenzelm
parents: 18992
diff changeset
   332
18060
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   333
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   334
(* empty and merge *)
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   335
45666
d83797ef0d2d separate module for concrete Isabelle markup;
wenzelm
parents: 43794
diff changeset
   336
val empty =
50201
c26369c9eda6 Isabelle-specific implementation of quasi-abstract markup elements -- back to module arrangement before d83797ef0d2d;
wenzelm
parents: 48992
diff changeset
   337
  make_consts (Name_Space.empty_table Markup.constantN, Symtab.empty, Symtab.empty);
18060
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   338
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   339
fun merge
30284
907da436f8a9 eliminated Consts.eq_consts tuning -- this is built into tables and name spaces already;
wenzelm
parents: 30280
diff changeset
   340
   (Consts {decls = decls1, constraints = constraints1, rev_abbrevs = rev_abbrevs1},
907da436f8a9 eliminated Consts.eq_consts tuning -- this is built into tables and name spaces already;
wenzelm
parents: 30280
diff changeset
   341
    Consts {decls = decls2, constraints = constraints2, rev_abbrevs = rev_abbrevs2}) =
18060
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   342
  let
33095
bbd52d2f8696 renamed NameSpace to Name_Space -- also to emphasize its subtle change in semantics;
wenzelm
parents: 33092
diff changeset
   343
    val decls' = Name_Space.merge_tables (decls1, decls2);
50768
2172f82de515 tuned -- prefer high-level Table.merge with its slightly more conservative update;
wenzelm
parents: 50201
diff changeset
   344
    val constraints' = Symtab.merge (K true) (constraints1, constraints2);
30566
9643f54c4184 reverted abbreviations: improved performance via Item_Net.T;
wenzelm
parents: 30466
diff changeset
   345
    val rev_abbrevs' = Symtab.join (K Item_Net.merge) (rev_abbrevs1, rev_abbrevs2);
24673
62b75508eb66 certify: do_expand as explicit argument, actually certify type of abstractions;
wenzelm
parents: 23655
diff changeset
   346
  in make_consts (decls', constraints', rev_abbrevs') end;
18060
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   347
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   348
end;