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