src/Pure/consts.ML
author wenzelm
Sat, 08 Apr 2006 22:51:17 +0200
changeset 19364 38799cfa34e5
parent 19098 fc736dbbe333
child 19433 c7a2b7a8c4cb
permissions -rw-r--r--
added intern/extern/extern_early; added expand_abbrevs flag; strip_abss: demand ocurrences of bounds in body; const decl: added flag for early externing (disabled for authentic syntax); abbrevs: support print mode; major cleanup;
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
18965
3b76383e3ab3 renamed space to space_of;
wenzelm
parents: 18935
diff changeset
    24
  val read_const: T -> string -> term
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
    25
  val certify: Pretty.pp -> Type.tsig -> T -> term -> term          (*exception TYPE*)
18146
47463b1825c6 uncurried Consts.typargs;
wenzelm
parents: 18117
diff changeset
    26
  val typargs: T -> string * typ -> typ list
18163
9b729737bf14 added instance;
wenzelm
parents: 18146
diff changeset
    27
  val instance: T -> string * typ list -> typ
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
    28
  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
    29
  val constrain: string * typ option -> T -> T
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
    30
  val expand_abbrevs: bool -> T -> T
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
    31
  val abbreviate: Pretty.pp -> Type.tsig -> NameSpace.naming -> string ->
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
    32
    (bstring * term) * bool -> T -> T
18060
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    33
  val hide: bool -> string -> T -> T
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    34
  val empty: T
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    35
  val merge: T * T -> T
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
    36
end;
18060
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    37
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    38
structure Consts: CONSTS =
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    39
struct
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    40
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
    41
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
    42
(** consts type **)
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
    43
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
    44
(* datatype T *)
18060
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    45
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
    46
datatype kind =
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
    47
  LogicalConst of int list list |
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
    48
  Abbreviation of term;
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
    49
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
    50
type decl =
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
    51
  (typ * kind) *
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
    52
  bool; (*early externing*)
18935
f22be3d61ed5 added abbreviations;
wenzelm
parents: 18163
diff changeset
    53
18060
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    54
datatype T = Consts of
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
    55
 {decls: (decl * stamp) NameSpace.table,
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
    56
  constraints: typ Symtab.table,
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
    57
  rev_abbrevs: (term * term) list Symtab.table,
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
    58
  expand_abbrevs: bool} * stamp;
19027
adf6fb0db28a added eq_consts;
wenzelm
parents: 18992
diff changeset
    59
adf6fb0db28a added eq_consts;
wenzelm
parents: 18992
diff changeset
    60
fun eq_consts (Consts (_, s1), Consts (_, s2)) = s1 = s2;
18060
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    61
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
    62
fun make_consts (decls, constraints, rev_abbrevs, expand_abbrevs) =
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
    63
  Consts ({decls = decls, constraints = constraints,
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
    64
    expand_abbrevs = expand_abbrevs, rev_abbrevs = rev_abbrevs}, stamp ());
18060
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    65
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
    66
fun map_consts f (Consts ({decls, constraints, rev_abbrevs, expand_abbrevs}, _)) =
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
    67
  make_consts (f (decls, constraints, rev_abbrevs, expand_abbrevs));
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 =
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
    70
  List.concat (map (Symtab.lookup_list rev_abbrevs) modes);
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
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
    76
  | dest_kind (Abbreviation t) = SOME t;
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
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
    89
    SOME (decl, _) => decl
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 =
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
    93
  (case the_const consts c of
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
    94
    ((T, LogicalConst ps), _) => (T, ps)
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
    95
  | _ => raise TYPE ("Illegal abbreviation: " ^ quote c, [], []));
18060
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
    96
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
    97
val declaration = #1 oo logical_const;
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
    98
val type_arguments = #2 oo logical_const;
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
    99
val monomorphic = null oo type_arguments;
18935
f22be3d61ed5 added abbreviations;
wenzelm
parents: 18163
diff changeset
   100
19027
adf6fb0db28a added eq_consts;
wenzelm
parents: 18992
diff changeset
   101
fun constraint (consts as Consts ({constraints, ...}, _)) c =
18060
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   102
  (case Symtab.lookup constraints c of
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   103
    SOME T => T
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   104
  | NONE => #1 (#1 (the_const consts c)));
18935
f22be3d61ed5 added abbreviations;
wenzelm
parents: 18163
diff changeset
   105
f22be3d61ed5 added abbreviations;
wenzelm
parents: 18163
diff changeset
   106
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   107
(* name space *)
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   108
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   109
fun space_of (Consts ({decls = (space, _), ...}, _)) = space;
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   110
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   111
val intern = NameSpace.intern o space_of;
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   112
val extern = NameSpace.extern o space_of;
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 extern_early consts c =
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   115
  (case try (the_const consts) c of
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   116
    SOME (_, true) => extern consts c
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   117
  | _ => Syntax.constN ^ c);
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   118
18060
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   119
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   120
(* read_const *)
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   121
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   122
fun read_const consts raw_c =
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   123
  let
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   124
    val c = intern consts raw_c;
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   125
    val _ = the_const consts c handle TYPE (msg, _, _) => error msg;
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   126
  in Const (c, dummyT) end;
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   127
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   128
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   129
(* certify *)
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 certify pp tsig (consts as Consts ({expand_abbrevs, ...}, _)) =
18965
3b76383e3ab3 renamed space to space_of;
wenzelm
parents: 18935
diff changeset
   132
  let
3b76383e3ab3 renamed space to space_of;
wenzelm
parents: 18935
diff changeset
   133
    fun err msg (c, T) =
3b76383e3ab3 renamed space to space_of;
wenzelm
parents: 18935
diff changeset
   134
      raise TYPE (msg ^ " " ^ quote c ^ " :: " ^ Pretty.string_of_typ pp T, [], []);
3b76383e3ab3 renamed space to space_of;
wenzelm
parents: 18935
diff changeset
   135
    fun cert tm =
3b76383e3ab3 renamed space to space_of;
wenzelm
parents: 18935
diff changeset
   136
      let
3b76383e3ab3 renamed space to space_of;
wenzelm
parents: 18935
diff changeset
   137
        val (head, args) = Term.strip_comb tm;
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   138
        fun comb h = Term.list_comb (h, map cert args);
18965
3b76383e3ab3 renamed space to space_of;
wenzelm
parents: 18935
diff changeset
   139
      in
3b76383e3ab3 renamed space to space_of;
wenzelm
parents: 18935
diff changeset
   140
        (case head of
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   141
          Abs (x, T, t) => comb (Abs (x, T, cert t))
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   142
        | Const (c, T) =>
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   143
            let
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   144
              val T' = Type.cert_typ tsig T;
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   145
              val ((U, kind), _) = the_const consts c;
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   146
            in
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   147
              if not (Type.typ_instance tsig (T', U)) then
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   148
                err "Illegal type for constant" (c, T)
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   149
              else
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   150
                (case (kind, expand_abbrevs) of
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   151
                  (Abbreviation u, true) =>
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   152
                    Term.betapplys (Envir.expand_atom tsig T' (U, u) handle TYPE _ =>
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   153
                      err "Illegal type for abbreviation" (c, T), map cert args)
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   154
                | _ => comb head)
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   155
            end
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   156
        | _ => comb head)
18965
3b76383e3ab3 renamed space to space_of;
wenzelm
parents: 18935
diff changeset
   157
      end;
3b76383e3ab3 renamed space to space_of;
wenzelm
parents: 18935
diff changeset
   158
  in cert end;
3b76383e3ab3 renamed space to space_of;
wenzelm
parents: 18935
diff changeset
   159
3b76383e3ab3 renamed space to space_of;
wenzelm
parents: 18935
diff changeset
   160
18060
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   161
(* typargs -- view actual const type as instance of declaration *)
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   162
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   163
fun subscript (Type (_, Ts)) (i :: is) = subscript (nth Ts i) is
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   164
  | subscript T [] = T
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   165
  | subscript T _ = raise Subscript;
18060
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   166
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   167
fun typargs consts (c, T) = map (subscript T) (type_arguments consts c);
18060
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   168
18163
9b729737bf14 added instance;
wenzelm
parents: 18146
diff changeset
   169
fun instance consts (c, Ts) =
9b729737bf14 added instance;
wenzelm
parents: 18146
diff changeset
   170
  let
9b729737bf14 added instance;
wenzelm
parents: 18146
diff changeset
   171
    val declT = declaration consts c;
9b729737bf14 added instance;
wenzelm
parents: 18146
diff changeset
   172
    val vars = map Term.dest_TVar (typargs consts (c, declT));
9b729737bf14 added instance;
wenzelm
parents: 18146
diff changeset
   173
  in declT |> Term.instantiateT (vars ~~ Ts) end;
9b729737bf14 added instance;
wenzelm
parents: 18146
diff changeset
   174
18060
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   175
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   176
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   177
(** build consts **)
18060
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   178
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   179
fun err_dup_consts cs =
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   180
  error ("Duplicate declaration of constant(s) " ^ commas_quote cs);
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   181
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   182
fun err_inconsistent_constraints cs =
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   183
  error ("Inconsistent type constraints for constant(s) " ^ commas_quote cs);
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   184
18935
f22be3d61ed5 added abbreviations;
wenzelm
parents: 18163
diff changeset
   185
fun extend_decls naming decl tab = NameSpace.extend_table naming (tab, [decl])
f22be3d61ed5 added abbreviations;
wenzelm
parents: 18163
diff changeset
   186
  handle Symtab.DUPS cs => err_dup_consts cs;
18060
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   187
18935
f22be3d61ed5 added abbreviations;
wenzelm
parents: 18163
diff changeset
   188
f22be3d61ed5 added abbreviations;
wenzelm
parents: 18163
diff changeset
   189
(* name space *)
18060
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   190
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   191
fun hide fully c = map_consts (fn (decls, constraints, rev_abbrevs, expand_abbrevs) =>
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   192
  (apfst (NameSpace.hide fully c) decls, constraints, rev_abbrevs, expand_abbrevs));
18935
f22be3d61ed5 added abbreviations;
wenzelm
parents: 18163
diff changeset
   193
f22be3d61ed5 added abbreviations;
wenzelm
parents: 18163
diff changeset
   194
f22be3d61ed5 added abbreviations;
wenzelm
parents: 18163
diff changeset
   195
(* declarations *)
f22be3d61ed5 added abbreviations;
wenzelm
parents: 18163
diff changeset
   196
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   197
fun declare naming ((c, declT), early) =
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   198
    map_consts (fn (decls, constraints, rev_abbrevs, expand_abbrevs) =>
18060
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   199
  let
18935
f22be3d61ed5 added abbreviations;
wenzelm
parents: 18163
diff changeset
   200
    fun args_of (Type (_, Ts)) pos = args_of_list Ts 0 pos
f22be3d61ed5 added abbreviations;
wenzelm
parents: 18163
diff changeset
   201
      | args_of (TVar v) pos = insert (eq_fst op =) (v, rev pos)
f22be3d61ed5 added abbreviations;
wenzelm
parents: 18163
diff changeset
   202
      | args_of (TFree _) _ = I
f22be3d61ed5 added abbreviations;
wenzelm
parents: 18163
diff changeset
   203
    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
   204
      | args_of_list [] _ _ = I;
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   205
    val decl = (((declT, LogicalConst (map #2 (rev (args_of declT [] [])))), early), stamp ());
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   206
  in (extend_decls naming (c, decl) decls, constraints, rev_abbrevs, expand_abbrevs) end);
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   207
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   208
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   209
(* constraints *)
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   210
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   211
fun constrain (c, C) consts =
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   212
  consts |> map_consts (fn (decls, constraints, rev_abbrevs, expand_abbrevs) =>
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   213
    (the_const consts c handle TYPE (msg, _, _) => error msg;
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   214
      (decls,
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   215
        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
   216
        rev_abbrevs, expand_abbrevs)));
18060
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   217
18935
f22be3d61ed5 added abbreviations;
wenzelm
parents: 18163
diff changeset
   218
f22be3d61ed5 added abbreviations;
wenzelm
parents: 18163
diff changeset
   219
(* abbreviations *)
f22be3d61ed5 added abbreviations;
wenzelm
parents: 18163
diff changeset
   220
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   221
fun expand_abbrevs b = map_consts (fn (decls, constraints, rev_abbrevs, _) =>
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   222
  (decls, constraints, rev_abbrevs, b));
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   223
19027
adf6fb0db28a added eq_consts;
wenzelm
parents: 18992
diff changeset
   224
local
adf6fb0db28a added eq_consts;
wenzelm
parents: 18992
diff changeset
   225
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   226
fun strip_abss tm = ([], tm) ::
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   227
  (case tm of
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   228
    Abs (a, T, t) =>
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   229
      if Term.loose_bvar1 (t, 0) then
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   230
        strip_abss t |> map (fn (xs, b) => ((a, T) :: xs, b))
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   231
      else []
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   232
  | _ => []);
19027
adf6fb0db28a added eq_consts;
wenzelm
parents: 18992
diff changeset
   233
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   234
fun rev_abbrev const rhs =
18060
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   235
  let
19027
adf6fb0db28a added eq_consts;
wenzelm
parents: 18992
diff changeset
   236
    fun abbrev (xs, body) =
adf6fb0db28a added eq_consts;
wenzelm
parents: 18992
diff changeset
   237
      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
   238
      in (Term.subst_bounds (rev vars, body), Term.list_comb (Const const, vars)) end;
adf6fb0db28a added eq_consts;
wenzelm
parents: 18992
diff changeset
   239
  in map abbrev (rev (strip_abss (Envir.beta_eta_contract rhs))) end;
adf6fb0db28a added eq_consts;
wenzelm
parents: 18992
diff changeset
   240
adf6fb0db28a added eq_consts;
wenzelm
parents: 18992
diff changeset
   241
in
18965
3b76383e3ab3 renamed space to space_of;
wenzelm
parents: 18935
diff changeset
   242
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   243
fun abbreviate pp tsig naming mode ((c, raw_rhs), early) consts =
18965
3b76383e3ab3 renamed space to space_of;
wenzelm
parents: 18935
diff changeset
   244
  let
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   245
    val rhs = raw_rhs
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   246
      |> Term.map_term_types (Type.cert_typ tsig)
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   247
      |> certify pp tsig (consts |> expand_abbrevs false);
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   248
    val rhs' = rhs
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   249
      |> certify pp tsig (consts |> expand_abbrevs true);
18965
3b76383e3ab3 renamed space to space_of;
wenzelm
parents: 18935
diff changeset
   250
    val T = Term.fastype_of rhs;
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   251
  in
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   252
    consts |> map_consts (fn (decls, constraints, rev_abbrevs, expand_abbrevs) =>
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   253
      let
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   254
        val decls' = decls
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   255
          |> extend_decls naming (c, (((T, Abbreviation rhs'), early), stamp ()));
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   256
        val rev_abbrevs' = rev_abbrevs
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   257
          |> Symtab.default (mode, [])
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   258
          |> Symtab.map_entry mode (append (rev_abbrev (NameSpace.full naming c, T) rhs));
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   259
      in (decls', constraints, rev_abbrevs', expand_abbrevs) end)
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   260
  end;
18935
f22be3d61ed5 added abbreviations;
wenzelm
parents: 18163
diff changeset
   261
19027
adf6fb0db28a added eq_consts;
wenzelm
parents: 18992
diff changeset
   262
end;
adf6fb0db28a added eq_consts;
wenzelm
parents: 18992
diff changeset
   263
18060
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   264
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   265
(* empty and merge *)
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   266
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   267
val empty = make_consts (NameSpace.empty_table, Symtab.empty, Symtab.empty, true);
18060
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   268
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   269
fun merge
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   270
   (Consts ({decls = decls1, constraints = constraints1,
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   271
      rev_abbrevs = rev_abbrevs1, expand_abbrevs = expand_abbrevs1}, _),
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   272
    Consts ({decls = decls2, constraints = constraints2,
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   273
      rev_abbrevs = rev_abbrevs2, expand_abbrevs = expand_abbrevs2}, _)) =
18060
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   274
  let
18935
f22be3d61ed5 added abbreviations;
wenzelm
parents: 18163
diff changeset
   275
    val decls' = NameSpace.merge_tables (eq_snd (op =)) (decls1, decls2)
18060
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   276
      handle Symtab.DUPS cs => err_dup_consts cs;
18935
f22be3d61ed5 added abbreviations;
wenzelm
parents: 18163
diff changeset
   277
    val constraints' = Symtab.merge (op =) (constraints1, constraints2)
18060
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   278
      handle Symtab.DUPS cs => err_inconsistent_constraints cs;
19364
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   279
    val rev_abbrevs' = (rev_abbrevs1, rev_abbrevs2) |> Symtab.join
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   280
      (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
   281
    val expand_abbrevs' = expand_abbrevs1 orelse expand_abbrevs2;
38799cfa34e5 added intern/extern/extern_early;
wenzelm
parents: 19098
diff changeset
   282
  in make_consts (decls', constraints', rev_abbrevs', expand_abbrevs') end;
18060
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   283
afcc28d16629 Polymorphic constants.
wenzelm
parents:
diff changeset
   284
end;