src/Pure/consts.ML
author wenzelm
Thu, 21 Sep 2006 19:04:43 +0200
changeset 20667 953b68f4a9f3
parent 20548 8ef25fe585a8
child 20734 8aa9590bd452
permissions -rw-r--r--
serial numbers for consts; added serial_of/name_of;
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
    ID:         $Id$
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
     3
    Author:     Makarius
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
     4
18935
f22be3d61ed5 added abbreviations;
wenzelm
parents: 18163
diff changeset
     5
Polymorphic constants: declarations, abbreviations, additional type
f22be3d61ed5 added abbreviations;
wenzelm
parents: 18163
diff changeset
     6
constraints.
18060
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
     7
*)
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
     8
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
     9
signature CONSTS =
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    10
sig
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    11
  type T
19027
adf6fb0db28a added eq_consts;
wenzelm
parents: 18992
diff changeset
    12
  val eq_consts: T * T -> bool
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
    13
  val abbrevs_of: T -> string list -> (term * term) list
18935
f22be3d61ed5 added abbreviations;
wenzelm
parents: 18163
diff changeset
    14
  val dest: T ->
f22be3d61ed5 added abbreviations;
wenzelm
parents: 18163
diff changeset
    15
   {constants: (typ * term option) NameSpace.table,
f22be3d61ed5 added abbreviations;
wenzelm
parents: 18163
diff changeset
    16
    constraints: typ NameSpace.table}
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
    17
  val declaration: T -> string -> typ                               (*exception TYPE*)
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
    18
  val monomorphic: T -> string -> bool                              (*exception TYPE*)
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
    19
  val constraint: T -> string -> typ                                (*exception TYPE*)
18965
3b76383e3ab3 renamed space to space_of;
wenzelm
parents: 18935
diff changeset
    20
  val space_of: T -> NameSpace.T
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
    21
  val intern: T -> xstring -> string
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
    22
  val extern: T -> string -> xstring
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
    23
  val extern_early: T -> string -> xstring
19657
25eaa3660123 added syntax interface;
wenzelm
parents: 19576
diff changeset
    24
  val syntax: T -> string * mixfix -> string * typ * mixfix
18965
3b76383e3ab3 renamed space to space_of;
wenzelm
parents: 18935
diff changeset
    25
  val read_const: T -> string -> term
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
    26
  val certify: Pretty.pp -> Type.tsig -> T -> term -> term          (*exception TYPE*)
20667
953b68f4a9f3 serial numbers for consts;
wenzelm
parents: 20548
diff changeset
    27
  val serial_of: T -> string -> serial
953b68f4a9f3 serial numbers for consts;
wenzelm
parents: 20548
diff changeset
    28
  val name_of: T -> serial -> string
18146
47463b1825c6 uncurried Consts.typargs;
wenzelm
parents: 18117
diff changeset
    29
  val typargs: T -> string * typ -> typ list
18163
9b729737bf14 added instance;
wenzelm
parents: 18146
diff changeset
    30
  val instance: T -> string * typ list -> typ
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
    31
  val declare: NameSpace.naming -> (bstring * typ) * bool -> T -> T
19098
fc736dbbe333 constrain: assert const declaration, optional type (i.e. may delete constraints);
wenzelm
parents: 19027
diff changeset
    32
  val constrain: string * typ option -> T -> T
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
    33
  val expand_abbrevs: bool -> T -> T
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
    34
  val abbreviate: Pretty.pp -> Type.tsig -> NameSpace.naming -> string ->
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
    35
    (bstring * term) * bool -> T -> T
18060
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    36
  val hide: bool -> string -> T -> T
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    37
  val empty: T
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    38
  val merge: T * T -> T
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
    39
end;
18060
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    40
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    41
structure Consts: CONSTS =
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    42
struct
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    43
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
    44
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
    45
(** consts type **)
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
    46
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
    47
(* datatype T *)
18060
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    48
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
    49
datatype kind =
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
    50
  LogicalConst of int list list |
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
    51
  Abbreviation of term;
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
    52
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
    53
type decl =
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
    54
  (typ * kind) *
19677
9d54d6d3bc28 replaced early'' flag by inverted authentic'';
wenzelm
parents: 19673
diff changeset
    55
  bool; (*authentic syntax*)
18935
f22be3d61ed5 added abbreviations;
wenzelm
parents: 18163
diff changeset
    56
18060
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    57
datatype T = Consts of
20667
953b68f4a9f3 serial numbers for consts;
wenzelm
parents: 20548
diff changeset
    58
 {decls: (decl * serial) NameSpace.table,
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
    59
  constraints: typ Symtab.table,
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
    60
  rev_abbrevs: (term * term) list Symtab.table,
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
    61
  expand_abbrevs: bool} * stamp;
19027
adf6fb0db28a added eq_consts;
wenzelm
parents: 18992
diff changeset
    62
adf6fb0db28a added eq_consts;
wenzelm
parents: 18992
diff changeset
    63
fun eq_consts (Consts (_, s1), Consts (_, s2)) = s1 = s2;
18060
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    64
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
    65
fun make_consts (decls, constraints, rev_abbrevs, expand_abbrevs) =
19677
9d54d6d3bc28 replaced early'' flag by inverted authentic'';
wenzelm
parents: 19673
diff changeset
    66
  Consts ({decls = decls, constraints = constraints, rev_abbrevs = rev_abbrevs,
9d54d6d3bc28 replaced early'' flag by inverted authentic'';
wenzelm
parents: 19673
diff changeset
    67
    expand_abbrevs = expand_abbrevs}, stamp ());
18060
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    68
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
    69
fun map_consts f (Consts ({decls, constraints, rev_abbrevs, expand_abbrevs}, _)) =
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
    70
  make_consts (f (decls, constraints, rev_abbrevs, expand_abbrevs));
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
    71
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
    72
fun abbrevs_of (Consts ({rev_abbrevs, ...}, _)) modes =
19482
9f11af8f7ef9 tuned basic list operators (flat, maps, map_filter);
wenzelm
parents: 19433
diff changeset
    73
  maps (Symtab.lookup_list rev_abbrevs) modes;
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
    74
18965
3b76383e3ab3 renamed space to space_of;
wenzelm
parents: 18935
diff changeset
    75
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
    76
(* dest consts *)
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
    77
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
    78
fun dest_kind (LogicalConst _) = NONE
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
    79
  | dest_kind (Abbreviation t) = SOME t;
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
    80
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
    81
fun dest (Consts ({decls = (space, decls), constraints, ...}, _)) =
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
    82
 {constants = (space,
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
    83
    Symtab.fold (fn (c, (((T, kind), _), _)) =>
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
    84
      Symtab.update (c, (T, dest_kind kind))) decls Symtab.empty),
18060
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    85
  constraints = (space, constraints)};
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    86
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    87
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    88
(* lookup consts *)
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    89
19027
adf6fb0db28a added eq_consts;
wenzelm
parents: 18992
diff changeset
    90
fun the_const (Consts ({decls = (_, tab), ...}, _)) c =
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
    91
  (case Symtab.lookup tab c of
20667
953b68f4a9f3 serial numbers for consts;
wenzelm
parents: 20548
diff changeset
    92
    SOME decl => decl
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
    93
  | NONE => raise TYPE ("Undeclared constant: " ^ quote c, [], []));
18935
f22be3d61ed5 added abbreviations;
wenzelm
parents: 18163
diff changeset
    94
f22be3d61ed5 added abbreviations;
wenzelm
parents: 18163
diff changeset
    95
fun logical_const consts c =
20667
953b68f4a9f3 serial numbers for consts;
wenzelm
parents: 20548
diff changeset
    96
  (case #1 (#1 (the_const consts c)) of
953b68f4a9f3 serial numbers for consts;
wenzelm
parents: 20548
diff changeset
    97
    (T, LogicalConst ps) => (T, ps)
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
    98
  | _ => raise TYPE ("Illegal abbreviation: " ^ quote c, [], []));
18060
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    99
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   100
val declaration = #1 oo logical_const;
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   101
val type_arguments = #2 oo logical_const;
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   102
val monomorphic = null oo type_arguments;
18935
f22be3d61ed5 added abbreviations;
wenzelm
parents: 18163
diff changeset
   103
19027
adf6fb0db28a added eq_consts;
wenzelm
parents: 18992
diff changeset
   104
fun constraint (consts as Consts ({constraints, ...}, _)) c =
18060
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   105
  (case Symtab.lookup constraints c of
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   106
    SOME T => T
20667
953b68f4a9f3 serial numbers for consts;
wenzelm
parents: 20548
diff changeset
   107
  | NONE => #1 (#1 (#1 (the_const consts c))));
18935
f22be3d61ed5 added abbreviations;
wenzelm
parents: 18163
diff changeset
   108
f22be3d61ed5 added abbreviations;
wenzelm
parents: 18163
diff changeset
   109
19657
25eaa3660123 added syntax interface;
wenzelm
parents: 19576
diff changeset
   110
(* name space and syntax *)
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   111
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   112
fun space_of (Consts ({decls = (space, _), ...}, _)) = space;
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   113
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   114
val intern = NameSpace.intern o space_of;
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   115
val extern = NameSpace.extern o space_of;
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   116
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   117
fun extern_early consts c =
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   118
  (case try (the_const consts) c of
20667
953b68f4a9f3 serial numbers for consts;
wenzelm
parents: 20548
diff changeset
   119
    SOME ((_, true), _) => Syntax.constN ^ c
19576
179ad0076f75 extern_early: improved handling of undeclared constants;
wenzelm
parents: 19502
diff changeset
   120
  | _ => extern consts c);
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   121
19657
25eaa3660123 added syntax interface;
wenzelm
parents: 19576
diff changeset
   122
fun syntax consts (c, mx) =
25eaa3660123 added syntax interface;
wenzelm
parents: 19576
diff changeset
   123
  let
20667
953b68f4a9f3 serial numbers for consts;
wenzelm
parents: 20548
diff changeset
   124
    val ((T, _), authentic) = #1 (the_const consts c) handle TYPE (msg, _, _) => error msg;
19677
9d54d6d3bc28 replaced early'' flag by inverted authentic'';
wenzelm
parents: 19673
diff changeset
   125
    val c' = if authentic then Syntax.constN ^ c else NameSpace.base c;
19657
25eaa3660123 added syntax interface;
wenzelm
parents: 19576
diff changeset
   126
  in (c', T, mx) end;
25eaa3660123 added syntax interface;
wenzelm
parents: 19576
diff changeset
   127
18060
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   128
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   129
(* read_const *)
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   130
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   131
fun read_const consts raw_c =
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   132
  let
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   133
    val c = intern consts raw_c;
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   134
    val _ = the_const consts c handle TYPE (msg, _, _) => error msg;
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   135
  in Const (c, dummyT) end;
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   136
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   137
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   138
(* certify *)
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   139
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   140
fun certify pp tsig (consts as Consts ({expand_abbrevs, ...}, _)) =
18965
3b76383e3ab3 renamed space to space_of;
wenzelm
parents: 18935
diff changeset
   141
  let
3b76383e3ab3 renamed space to space_of;
wenzelm
parents: 18935
diff changeset
   142
    fun err msg (c, T) =
3b76383e3ab3 renamed space to space_of;
wenzelm
parents: 18935
diff changeset
   143
      raise TYPE (msg ^ " " ^ quote c ^ " :: " ^ Pretty.string_of_typ pp T, [], []);
3b76383e3ab3 renamed space to space_of;
wenzelm
parents: 18935
diff changeset
   144
    fun cert tm =
3b76383e3ab3 renamed space to space_of;
wenzelm
parents: 18935
diff changeset
   145
      let
3b76383e3ab3 renamed space to space_of;
wenzelm
parents: 18935
diff changeset
   146
        val (head, args) = Term.strip_comb tm;
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   147
        fun comb h = Term.list_comb (h, map cert args);
18965
3b76383e3ab3 renamed space to space_of;
wenzelm
parents: 18935
diff changeset
   148
      in
3b76383e3ab3 renamed space to space_of;
wenzelm
parents: 18935
diff changeset
   149
        (case head of
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   150
          Abs (x, T, t) => comb (Abs (x, T, cert t))
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   151
        | Const (c, T) =>
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   152
            let
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   153
              val T' = Type.cert_typ tsig T;
20667
953b68f4a9f3 serial numbers for consts;
wenzelm
parents: 20548
diff changeset
   154
              val (U, kind) = #1 (#1 (the_const consts c));
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   155
            in
19433
c7a2b7a8c4cb certify: ignore sort constraints of declarations (MAJOR CHANGE);
wenzelm
parents: 19364
diff changeset
   156
              if not (Type.raw_instance (T', U)) then
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   157
                err "Illegal type for constant" (c, T)
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   158
              else
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   159
                (case (kind, expand_abbrevs) of
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   160
                  (Abbreviation u, true) =>
19433
c7a2b7a8c4cb certify: ignore sort constraints of declarations (MAJOR CHANGE);
wenzelm
parents: 19364
diff changeset
   161
                    Term.betapplys (Envir.expand_atom T' (U, u) handle TYPE _ =>
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   162
                      err "Illegal type for abbreviation" (c, T), map cert args)
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   163
                | _ => comb head)
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   164
            end
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   165
        | _ => comb head)
18965
3b76383e3ab3 renamed space to space_of;
wenzelm
parents: 18935
diff changeset
   166
      end;
3b76383e3ab3 renamed space to space_of;
wenzelm
parents: 18935
diff changeset
   167
  in cert end;
3b76383e3ab3 renamed space to space_of;
wenzelm
parents: 18935
diff changeset
   168
3b76383e3ab3 renamed space to space_of;
wenzelm
parents: 18935
diff changeset
   169
20667
953b68f4a9f3 serial numbers for consts;
wenzelm
parents: 20548
diff changeset
   170
953b68f4a9f3 serial numbers for consts;
wenzelm
parents: 20548
diff changeset
   171
(** efficient representations **)
953b68f4a9f3 serial numbers for consts;
wenzelm
parents: 20548
diff changeset
   172
953b68f4a9f3 serial numbers for consts;
wenzelm
parents: 20548
diff changeset
   173
(* serial numbers *)
953b68f4a9f3 serial numbers for consts;
wenzelm
parents: 20548
diff changeset
   174
953b68f4a9f3 serial numbers for consts;
wenzelm
parents: 20548
diff changeset
   175
fun serial_of consts c = #2 (the_const consts c);
953b68f4a9f3 serial numbers for consts;
wenzelm
parents: 20548
diff changeset
   176
953b68f4a9f3 serial numbers for consts;
wenzelm
parents: 20548
diff changeset
   177
fun name_of (Consts ({decls = (_, tab), ...}, _)) i =
953b68f4a9f3 serial numbers for consts;
wenzelm
parents: 20548
diff changeset
   178
  (case Symtab.fold (fn (c, (_, j)) => if i = j then K (SOME c) else I) tab NONE of
953b68f4a9f3 serial numbers for consts;
wenzelm
parents: 20548
diff changeset
   179
    SOME c => c
953b68f4a9f3 serial numbers for consts;
wenzelm
parents: 20548
diff changeset
   180
  | NONE => raise TYPE ("Bad serial number of constant", [], []));
953b68f4a9f3 serial numbers for consts;
wenzelm
parents: 20548
diff changeset
   181
953b68f4a9f3 serial numbers for consts;
wenzelm
parents: 20548
diff changeset
   182
18060
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   183
(* typargs -- view actual const type as instance of declaration *)
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   184
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   185
fun subscript (Type (_, Ts)) (i :: is) = subscript (nth Ts i) is
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   186
  | subscript T [] = T
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   187
  | subscript T _ = raise Subscript;
18060
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   188
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   189
fun typargs consts (c, T) = map (subscript T) (type_arguments consts c);
18060
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   190
18163
9b729737bf14 added instance;
wenzelm
parents: 18146
diff changeset
   191
fun instance consts (c, Ts) =
9b729737bf14 added instance;
wenzelm
parents: 18146
diff changeset
   192
  let
9b729737bf14 added instance;
wenzelm
parents: 18146
diff changeset
   193
    val declT = declaration consts c;
9b729737bf14 added instance;
wenzelm
parents: 18146
diff changeset
   194
    val vars = map Term.dest_TVar (typargs consts (c, declT));
20509
073a5ed7dd71 moved term subst functions to TermSubst;
wenzelm
parents: 19677
diff changeset
   195
  in declT |> TermSubst.instantiateT (vars ~~ Ts) end;
18163
9b729737bf14 added instance;
wenzelm
parents: 18146
diff changeset
   196
18060
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   197
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   198
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   199
(** build consts **)
18060
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   200
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   201
fun err_dup_consts cs =
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   202
  error ("Duplicate declaration of constant(s) " ^ commas_quote cs);
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   203
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   204
fun err_inconsistent_constraints cs =
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   205
  error ("Inconsistent type constraints for constant(s) " ^ commas_quote cs);
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   206
18935
f22be3d61ed5 added abbreviations;
wenzelm
parents: 18163
diff changeset
   207
fun extend_decls naming decl tab = NameSpace.extend_table naming (tab, [decl])
f22be3d61ed5 added abbreviations;
wenzelm
parents: 18163
diff changeset
   208
  handle Symtab.DUPS cs => err_dup_consts cs;
18060
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   209
18935
f22be3d61ed5 added abbreviations;
wenzelm
parents: 18163
diff changeset
   210
f22be3d61ed5 added abbreviations;
wenzelm
parents: 18163
diff changeset
   211
(* name space *)
18060
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   212
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   213
fun hide fully c = map_consts (fn (decls, constraints, rev_abbrevs, expand_abbrevs) =>
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   214
  (apfst (NameSpace.hide fully c) decls, constraints, rev_abbrevs, expand_abbrevs));
18935
f22be3d61ed5 added abbreviations;
wenzelm
parents: 18163
diff changeset
   215
f22be3d61ed5 added abbreviations;
wenzelm
parents: 18163
diff changeset
   216
f22be3d61ed5 added abbreviations;
wenzelm
parents: 18163
diff changeset
   217
(* declarations *)
f22be3d61ed5 added abbreviations;
wenzelm
parents: 18163
diff changeset
   218
19677
9d54d6d3bc28 replaced early'' flag by inverted authentic'';
wenzelm
parents: 19673
diff changeset
   219
fun declare naming ((c, declT), authentic) =
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   220
    map_consts (fn (decls, constraints, rev_abbrevs, expand_abbrevs) =>
18060
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   221
  let
18935
f22be3d61ed5 added abbreviations;
wenzelm
parents: 18163
diff changeset
   222
    fun args_of (Type (_, Ts)) pos = args_of_list Ts 0 pos
f22be3d61ed5 added abbreviations;
wenzelm
parents: 18163
diff changeset
   223
      | args_of (TVar v) pos = insert (eq_fst op =) (v, rev pos)
f22be3d61ed5 added abbreviations;
wenzelm
parents: 18163
diff changeset
   224
      | args_of (TFree _) _ = I
f22be3d61ed5 added abbreviations;
wenzelm
parents: 18163
diff changeset
   225
    and args_of_list (T :: Ts) i is = args_of T (i :: is) #> args_of_list Ts (i + 1) is
f22be3d61ed5 added abbreviations;
wenzelm
parents: 18163
diff changeset
   226
      | args_of_list [] _ _ = I;
20667
953b68f4a9f3 serial numbers for consts;
wenzelm
parents: 20548
diff changeset
   227
    val decl =
953b68f4a9f3 serial numbers for consts;
wenzelm
parents: 20548
diff changeset
   228
      (((declT, LogicalConst (map #2 (rev (args_of declT [] [])))), authentic), serial ());
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   229
  in (extend_decls naming (c, decl) decls, constraints, rev_abbrevs, expand_abbrevs) end);
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   230
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   231
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   232
(* constraints *)
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   233
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   234
fun constrain (c, C) consts =
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   235
  consts |> map_consts (fn (decls, constraints, rev_abbrevs, expand_abbrevs) =>
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   236
    (the_const consts c handle TYPE (msg, _, _) => error msg;
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   237
      (decls,
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   238
        constraints |> (case C of SOME T => Symtab.update (c, T) | NONE => Symtab.delete_safe c),
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   239
        rev_abbrevs, expand_abbrevs)));
18060
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   240
18935
f22be3d61ed5 added abbreviations;
wenzelm
parents: 18163
diff changeset
   241
f22be3d61ed5 added abbreviations;
wenzelm
parents: 18163
diff changeset
   242
(* abbreviations *)
f22be3d61ed5 added abbreviations;
wenzelm
parents: 18163
diff changeset
   243
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   244
fun expand_abbrevs b = map_consts (fn (decls, constraints, rev_abbrevs, _) =>
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   245
  (decls, constraints, rev_abbrevs, b));
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   246
19027
adf6fb0db28a added eq_consts;
wenzelm
parents: 18992
diff changeset
   247
local
adf6fb0db28a added eq_consts;
wenzelm
parents: 18992
diff changeset
   248
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   249
fun strip_abss tm = ([], tm) ::
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   250
  (case tm of
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   251
    Abs (a, T, t) =>
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   252
      if Term.loose_bvar1 (t, 0) then
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   253
        strip_abss t |> map (fn (xs, b) => ((a, T) :: xs, b))
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   254
      else []
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   255
  | _ => []);
19027
adf6fb0db28a added eq_consts;
wenzelm
parents: 18992
diff changeset
   256
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   257
fun rev_abbrev const rhs =
18060
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   258
  let
19027
adf6fb0db28a added eq_consts;
wenzelm
parents: 18992
diff changeset
   259
    fun abbrev (xs, body) =
adf6fb0db28a added eq_consts;
wenzelm
parents: 18992
diff changeset
   260
      let val vars = fold (fn (x, T) => cons (Var ((x, 0), T))) (Term.rename_wrt_term body xs) []
adf6fb0db28a added eq_consts;
wenzelm
parents: 18992
diff changeset
   261
      in (Term.subst_bounds (rev vars, body), Term.list_comb (Const const, vars)) end;
19502
wenzelm
parents: 19482
diff changeset
   262
  in map abbrev (strip_abss (Envir.beta_eta_contract rhs)) end;
19027
adf6fb0db28a added eq_consts;
wenzelm
parents: 18992
diff changeset
   263
adf6fb0db28a added eq_consts;
wenzelm
parents: 18992
diff changeset
   264
in
18965
3b76383e3ab3 renamed space to space_of;
wenzelm
parents: 18935
diff changeset
   265
19677
9d54d6d3bc28 replaced early'' flag by inverted authentic'';
wenzelm
parents: 19673
diff changeset
   266
fun abbreviate pp tsig naming mode ((c, raw_rhs), authentic) consts =
18965
3b76383e3ab3 renamed space to space_of;
wenzelm
parents: 18935
diff changeset
   267
  let
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   268
    val rhs = raw_rhs
20548
8ef25fe585a8 renamed Term.map_term_types to Term.map_types (cf. Term.fold_types);
wenzelm
parents: 20509
diff changeset
   269
      |> Term.map_types (Type.cert_typ tsig)
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   270
      |> certify pp tsig (consts |> expand_abbrevs false);
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   271
    val rhs' = rhs
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   272
      |> certify pp tsig (consts |> expand_abbrevs true);
18965
3b76383e3ab3 renamed space to space_of;
wenzelm
parents: 18935
diff changeset
   273
    val T = Term.fastype_of rhs;
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   274
  in
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   275
    consts |> map_consts (fn (decls, constraints, rev_abbrevs, expand_abbrevs) =>
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   276
      let
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   277
        val decls' = decls
20667
953b68f4a9f3 serial numbers for consts;
wenzelm
parents: 20548
diff changeset
   278
          |> extend_decls naming (c, (((T, Abbreviation rhs'), authentic), serial ()));
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   279
        val rev_abbrevs' = rev_abbrevs
19502
wenzelm
parents: 19482
diff changeset
   280
          |> fold (curry Symtab.update_list mode) (rev_abbrev (NameSpace.full naming c, T) rhs);
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   281
      in (decls', constraints, rev_abbrevs', expand_abbrevs) end)
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   282
  end;
18935
f22be3d61ed5 added abbreviations;
wenzelm
parents: 18163
diff changeset
   283
19027
adf6fb0db28a added eq_consts;
wenzelm
parents: 18992
diff changeset
   284
end;
adf6fb0db28a added eq_consts;
wenzelm
parents: 18992
diff changeset
   285
18060
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   286
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   287
(* empty and merge *)
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   288
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   289
val empty = make_consts (NameSpace.empty_table, Symtab.empty, Symtab.empty, true);
18060
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   290
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   291
fun merge
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   292
   (Consts ({decls = decls1, constraints = constraints1,
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   293
      rev_abbrevs = rev_abbrevs1, expand_abbrevs = expand_abbrevs1}, _),
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   294
    Consts ({decls = decls2, constraints = constraints2,
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   295
      rev_abbrevs = rev_abbrevs2, expand_abbrevs = expand_abbrevs2}, _)) =
18060
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   296
  let
18935
f22be3d61ed5 added abbreviations;
wenzelm
parents: 18163
diff changeset
   297
    val decls' = NameSpace.merge_tables (eq_snd (op =)) (decls1, decls2)
18060
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   298
      handle Symtab.DUPS cs => err_dup_consts cs;
18935
f22be3d61ed5 added abbreviations;
wenzelm
parents: 18163
diff changeset
   299
    val constraints' = Symtab.merge (op =) (constraints1, constraints2)
18060
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   300
      handle Symtab.DUPS cs => err_inconsistent_constraints cs;
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   301
    val rev_abbrevs' = (rev_abbrevs1, rev_abbrevs2) |> Symtab.join
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   302
      (K (Library.merge (fn ((t, u), (t', u')) => t aconv t' andalso u aconv u')));
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   303
    val expand_abbrevs' = expand_abbrevs1 orelse expand_abbrevs2;
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   304
  in make_consts (decls', constraints', rev_abbrevs', expand_abbrevs') end;
18060
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   305
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   306
end;