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