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