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