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