src/Pure/consts.ML
author wenzelm
Sun Mar 01 23:36:12 2009 +0100 (2009-03-01)
changeset 30190 479806475f3c
parent 29606 fedb8be05f24
child 30280 eb98b49ef835
permissions -rw-r--r--
use long names for old-style fold combinators;
wenzelm@18060
     1
(*  Title:      Pure/consts.ML
wenzelm@18060
     2
    Author:     Makarius
wenzelm@18060
     3
wenzelm@18935
     4
Polymorphic constants: declarations, abbreviations, additional type
wenzelm@18935
     5
constraints.
wenzelm@18060
     6
*)
wenzelm@18060
     7
wenzelm@18060
     8
signature CONSTS =
wenzelm@18060
     9
sig
wenzelm@18060
    10
  type T
wenzelm@19027
    11
  val eq_consts: T * T -> bool
wenzelm@19364
    12
  val abbrevs_of: T -> string list -> (term * term) list
wenzelm@18935
    13
  val dest: T ->
wenzelm@25404
    14
   {constants: (typ * term option) NameSpace.table,
wenzelm@18935
    15
    constraints: typ NameSpace.table}
wenzelm@25048
    16
  val the_type: T -> string -> typ                             (*exception TYPE*)
wenzelm@25048
    17
  val the_abbreviation: T -> string -> typ * term              (*exception TYPE*)
wenzelm@25048
    18
  val type_scheme: T -> string -> typ                          (*exception TYPE*)
wenzelm@28017
    19
  val the_tags: T -> string -> Properties.T                    (*exception TYPE*)
wenzelm@25048
    20
  val is_monomorphic: T -> string -> bool                      (*exception TYPE*)
wenzelm@25048
    21
  val the_constraint: T -> string -> typ                       (*exception TYPE*)
wenzelm@18965
    22
  val space_of: T -> NameSpace.T
wenzelm@19364
    23
  val intern: T -> xstring -> string
wenzelm@19364
    24
  val extern: T -> string -> xstring
wenzelm@19364
    25
  val extern_early: T -> string -> xstring
wenzelm@19657
    26
  val syntax: T -> string * mixfix -> string * typ * mixfix
wenzelm@21181
    27
  val syntax_name: T -> string -> string
wenzelm@18965
    28
  val read_const: T -> string -> term
wenzelm@24673
    29
  val certify: Pretty.pp -> Type.tsig -> bool -> T -> term -> term  (*exception TYPE*)
wenzelm@18146
    30
  val typargs: T -> string * typ -> typ list
wenzelm@18163
    31
  val instance: T -> string * typ list -> typ
haftmann@29581
    32
  val declare: bool -> NameSpace.naming -> Properties.T -> (binding * typ) -> T -> T
wenzelm@19098
    33
  val constrain: string * typ option -> T -> T
wenzelm@28017
    34
  val abbreviate: Pretty.pp -> Type.tsig -> NameSpace.naming -> string -> Properties.T ->
haftmann@29581
    35
    binding * term -> T -> (term * term) * T
wenzelm@25048
    36
  val revert_abbrev: string -> string -> T -> T
wenzelm@18060
    37
  val hide: bool -> string -> T -> T
wenzelm@18060
    38
  val empty: T
wenzelm@18060
    39
  val merge: T * T -> T
wenzelm@19364
    40
end;
wenzelm@18060
    41
wenzelm@18060
    42
structure Consts: CONSTS =
wenzelm@18060
    43
struct
wenzelm@18060
    44
wenzelm@19364
    45
(** consts type **)
wenzelm@19364
    46
wenzelm@19364
    47
(* datatype T *)
wenzelm@18060
    48
wenzelm@28017
    49
type decl = {T: typ, typargs: int list list, tags: Properties.T, authentic: bool};
wenzelm@24909
    50
type abbrev = {rhs: term, normal_rhs: term, force_expand: bool};
wenzelm@18935
    51
wenzelm@18060
    52
datatype T = Consts of
wenzelm@24909
    53
 {decls: ((decl * abbrev option) * serial) NameSpace.table,
wenzelm@19364
    54
  constraints: typ Symtab.table,
wenzelm@24673
    55
  rev_abbrevs: (term * term) list Symtab.table} * stamp;
wenzelm@19027
    56
wenzelm@19027
    57
fun eq_consts (Consts (_, s1), Consts (_, s2)) = s1 = s2;
wenzelm@18060
    58
wenzelm@24673
    59
fun make_consts (decls, constraints, rev_abbrevs) =
wenzelm@24673
    60
  Consts ({decls = decls, constraints = constraints, rev_abbrevs = rev_abbrevs}, stamp ());
wenzelm@18060
    61
wenzelm@24673
    62
fun map_consts f (Consts ({decls, constraints, rev_abbrevs}, _)) =
wenzelm@24673
    63
  make_consts (f (decls, constraints, rev_abbrevs));
wenzelm@19364
    64
wenzelm@19364
    65
fun abbrevs_of (Consts ({rev_abbrevs, ...}, _)) modes =
wenzelm@19482
    66
  maps (Symtab.lookup_list rev_abbrevs) modes;
wenzelm@19364
    67
wenzelm@18965
    68
wenzelm@19364
    69
(* dest consts *)
wenzelm@19364
    70
wenzelm@19364
    71
fun dest (Consts ({decls = (space, decls), constraints, ...}, _)) =
wenzelm@19364
    72
 {constants = (space,
wenzelm@24909
    73
    Symtab.fold (fn (c, (({T, ...}, abbr), _)) =>
wenzelm@25404
    74
      Symtab.update (c, (T, Option.map #rhs abbr))) decls Symtab.empty),
wenzelm@18060
    75
  constraints = (space, constraints)};
wenzelm@18060
    76
wenzelm@18060
    77
wenzelm@18060
    78
(* lookup consts *)
wenzelm@18060
    79
wenzelm@19027
    80
fun the_const (Consts ({decls = (_, tab), ...}, _)) c =
wenzelm@19364
    81
  (case Symtab.lookup tab c of
wenzelm@24772
    82
    SOME (decl, _) => decl
wenzelm@25041
    83
  | NONE => raise TYPE ("Unknown constant: " ^ quote c, [], []));
wenzelm@18935
    84
wenzelm@25041
    85
fun the_type consts c =
wenzelm@24772
    86
  (case the_const consts c of
wenzelm@24909
    87
    ({T, ...}, NONE) => T
wenzelm@21720
    88
  | _ => raise TYPE ("Not a logical constant: " ^ quote c, [], []));
wenzelm@18060
    89
wenzelm@21720
    90
fun the_abbreviation consts c =
wenzelm@24772
    91
  (case the_const consts c of
wenzelm@25048
    92
    ({T, ...}, SOME {rhs, ...}) => (T, rhs)
wenzelm@21720
    93
  | _ => raise TYPE ("Not an abbreviated constant: " ^ quote c, [], []));
wenzelm@21720
    94
wenzelm@25041
    95
val the_decl = #1 oo the_const;
wenzelm@25041
    96
val type_scheme = #T oo the_decl;
wenzelm@25041
    97
val type_arguments = #typargs oo the_decl;
wenzelm@25041
    98
val the_tags = #tags oo the_decl;
wenzelm@25041
    99
wenzelm@21720
   100
val is_monomorphic = null oo type_arguments;
wenzelm@18935
   101
wenzelm@21720
   102
fun the_constraint (consts as Consts ({constraints, ...}, _)) c =
wenzelm@18060
   103
  (case Symtab.lookup constraints c of
wenzelm@18060
   104
    SOME T => T
wenzelm@25041
   105
  | NONE => type_scheme 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@24909
   117
    SOME ({authentic = 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@24909
   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@21181
   126
fun syntax_name consts c = #1 (syntax consts (c, NoSyn));
wenzelm@21181
   127
wenzelm@18060
   128
wenzelm@19364
   129
(* read_const *)
wenzelm@19364
   130
wenzelm@19364
   131
fun read_const consts raw_c =
wenzelm@19364
   132
  let
wenzelm@19364
   133
    val c = intern consts raw_c;
wenzelm@25041
   134
    val T = type_scheme consts c handle TYPE (msg, _, _) => error msg;
wenzelm@21205
   135
  in Const (c, T) end;
wenzelm@19364
   136
wenzelm@19364
   137
wenzelm@19364
   138
(* certify *)
wenzelm@19364
   139
wenzelm@24673
   140
fun certify pp tsig do_expand consts =
wenzelm@18965
   141
  let
wenzelm@18965
   142
    fun err msg (c, T) =
wenzelm@18965
   143
      raise TYPE (msg ^ " " ^ quote c ^ " :: " ^ Pretty.string_of_typ pp T, [], []);
wenzelm@24673
   144
    val certT = Type.cert_typ tsig;
wenzelm@18965
   145
    fun cert tm =
wenzelm@18965
   146
      let
wenzelm@18965
   147
        val (head, args) = Term.strip_comb tm;
wenzelm@21694
   148
        val args' = map cert args;
wenzelm@21694
   149
        fun comb head' = Term.list_comb (head', args');
wenzelm@18965
   150
      in
wenzelm@18965
   151
        (case head of
wenzelm@24673
   152
          Abs (x, T, t) => comb (Abs (x, certT T, cert t))
wenzelm@19364
   153
        | Const (c, T) =>
wenzelm@19364
   154
            let
wenzelm@24673
   155
              val T' = certT T;
wenzelm@24909
   156
              val ({T = U, ...}, abbr) = the_const consts c;
wenzelm@25048
   157
              fun expand u =
wenzelm@25048
   158
                Term.betapplys (Envir.expand_atom T' (U, u) handle TYPE _ =>
wenzelm@25048
   159
                  err "Illegal type for abbreviation" (c, T), args');
wenzelm@19364
   160
            in
wenzelm@19433
   161
              if not (Type.raw_instance (T', U)) then
wenzelm@19364
   162
                err "Illegal type for constant" (c, T)
wenzelm@19364
   163
              else
wenzelm@24909
   164
                (case abbr of
wenzelm@25048
   165
                  SOME {rhs, normal_rhs, force_expand} =>
wenzelm@25048
   166
                    if do_expand then expand normal_rhs
wenzelm@25048
   167
                    else if force_expand then expand rhs
wenzelm@21720
   168
                    else comb head
wenzelm@19364
   169
                | _ => comb head)
wenzelm@19364
   170
            end
wenzelm@19364
   171
        | _ => comb head)
wenzelm@18965
   172
      end;
wenzelm@18965
   173
  in cert end;
wenzelm@18965
   174
wenzelm@18965
   175
wenzelm@18060
   176
(* typargs -- view actual const type as instance of declaration *)
wenzelm@18060
   177
wenzelm@24909
   178
local
wenzelm@24909
   179
wenzelm@24909
   180
fun args_of (Type (_, Ts)) pos = args_of_list Ts 0 pos
wenzelm@24909
   181
  | args_of (TVar v) pos = insert (eq_fst op =) (v, rev pos)
wenzelm@24909
   182
  | args_of (TFree _) _ = I
wenzelm@24909
   183
and args_of_list (T :: Ts) i is = args_of T (i :: is) #> args_of_list Ts (i + 1) is
wenzelm@24909
   184
  | args_of_list [] _ _ = I;
wenzelm@24909
   185
wenzelm@19364
   186
fun subscript (Type (_, Ts)) (i :: is) = subscript (nth Ts i) is
wenzelm@19364
   187
  | subscript T [] = T
wenzelm@19364
   188
  | subscript T _ = raise Subscript;
wenzelm@18060
   189
wenzelm@24909
   190
in
wenzelm@24909
   191
wenzelm@24909
   192
fun typargs_of T = map #2 (rev (args_of T [] []));
wenzelm@24909
   193
wenzelm@19364
   194
fun typargs consts (c, T) = map (subscript T) (type_arguments consts c);
wenzelm@18060
   195
wenzelm@24909
   196
end;
wenzelm@24909
   197
wenzelm@18163
   198
fun instance consts (c, Ts) =
wenzelm@18163
   199
  let
wenzelm@25041
   200
    val declT = type_scheme consts c;
wenzelm@18163
   201
    val vars = map Term.dest_TVar (typargs consts (c, declT));
wenzelm@24673
   202
    val inst = vars ~~ Ts handle UnequalLengths =>
wenzelm@24673
   203
      raise TYPE ("Consts.instance", Ts, [Const (c, dummyT)]);
wenzelm@24673
   204
  in declT |> TermSubst.instantiateT inst end;
wenzelm@18163
   205
wenzelm@18060
   206
wenzelm@18060
   207
wenzelm@19364
   208
(** build consts **)
wenzelm@18060
   209
wenzelm@23655
   210
fun err_dup_const c =
wenzelm@23655
   211
  error ("Duplicate declaration of constant " ^ quote c);
wenzelm@18060
   212
haftmann@28965
   213
fun extend_decls naming decl tab = NameSpace.bind naming decl tab
wenzelm@23655
   214
  handle Symtab.DUP c => err_dup_const c;
wenzelm@18060
   215
wenzelm@18935
   216
wenzelm@18935
   217
(* name space *)
wenzelm@18060
   218
wenzelm@24673
   219
fun hide fully c = map_consts (fn (decls, constraints, rev_abbrevs) =>
wenzelm@24673
   220
  (apfst (NameSpace.hide fully c) decls, constraints, rev_abbrevs));
wenzelm@18935
   221
wenzelm@18935
   222
wenzelm@18935
   223
(* declarations *)
wenzelm@18935
   224
haftmann@28861
   225
fun declare authentic naming tags (b, declT) = map_consts (fn (decls, constraints, rev_abbrevs) =>
wenzelm@18060
   226
  let
wenzelm@26050
   227
    val tags' = tags |> Position.default_properties (Position.thread_data ());
wenzelm@26050
   228
    val decl = {T = declT, typargs = typargs_of declT, tags = tags', authentic = authentic};
haftmann@28861
   229
    val (_, decls') = decls |> extend_decls naming (b, ((decl, NONE), serial ()));
wenzelm@24772
   230
  in (decls', constraints, rev_abbrevs) end);
wenzelm@19364
   231
wenzelm@19364
   232
wenzelm@19364
   233
(* constraints *)
wenzelm@19364
   234
wenzelm@19364
   235
fun constrain (c, C) consts =
wenzelm@24673
   236
  consts |> map_consts (fn (decls, constraints, rev_abbrevs) =>
wenzelm@19364
   237
    (the_const consts c handle TYPE (msg, _, _) => error msg;
wenzelm@19364
   238
      (decls,
wenzelm@19364
   239
        constraints |> (case C of SOME T => Symtab.update (c, T) | NONE => Symtab.delete_safe c),
wenzelm@24673
   240
        rev_abbrevs)));
wenzelm@18060
   241
wenzelm@18935
   242
wenzelm@18935
   243
(* abbreviations *)
wenzelm@18935
   244
wenzelm@19027
   245
local
wenzelm@19027
   246
wenzelm@19364
   247
fun strip_abss tm = ([], tm) ::
wenzelm@19364
   248
  (case tm of
wenzelm@19364
   249
    Abs (a, T, t) =>
wenzelm@19364
   250
      if Term.loose_bvar1 (t, 0) then
wenzelm@19364
   251
        strip_abss t |> map (fn (xs, b) => ((a, T) :: xs, b))
wenzelm@19364
   252
      else []
wenzelm@19364
   253
  | _ => []);
wenzelm@19027
   254
wenzelm@21720
   255
fun rev_abbrev lhs rhs =
wenzelm@18060
   256
  let
wenzelm@19027
   257
    fun abbrev (xs, body) =
wenzelm@19027
   258
      let val vars = fold (fn (x, T) => cons (Var ((x, 0), T))) (Term.rename_wrt_term body xs) []
wenzelm@21720
   259
      in (Term.subst_bounds (rev vars, body), Term.list_comb (lhs, vars)) end;
wenzelm@19502
   260
  in map abbrev (strip_abss (Envir.beta_eta_contract rhs)) end;
wenzelm@19027
   261
wenzelm@19027
   262
in
wenzelm@18965
   263
haftmann@28861
   264
fun abbreviate pp tsig naming mode tags (b, raw_rhs) consts =
wenzelm@18965
   265
  let
wenzelm@24673
   266
    val cert_term = certify pp tsig false consts;
wenzelm@24673
   267
    val expand_term = certify pp tsig true consts;
wenzelm@25116
   268
    val force_expand = mode = PrintMode.internal;
wenzelm@21720
   269
wenzelm@19364
   270
    val rhs = raw_rhs
wenzelm@20548
   271
      |> Term.map_types (Type.cert_typ tsig)
wenzelm@21720
   272
      |> cert_term;
wenzelm@25404
   273
    val normal_rhs = expand_term rhs;
wenzelm@21794
   274
    val T = Term.fastype_of rhs;
haftmann@28965
   275
    val lhs = Const (NameSpace.full_name naming b, T);
wenzelm@21720
   276
ballarin@29251
   277
    fun err msg = (warning (* FIXME should be error *) (msg ^ " on rhs of abbreviation:\n" ^
ballarin@29251
   278
      Pretty.string_of_term pp (Logic.mk_equals (lhs, rhs))); true);
wenzelm@25048
   279
    val _ = Term.exists_subterm Term.is_Var rhs andalso err "Illegal schematic variables";
wenzelm@25048
   280
    val _ = null (Term.hidden_polymorphism rhs) orelse err "Extra type variables";
wenzelm@19364
   281
  in
wenzelm@24673
   282
    consts |> map_consts (fn (decls, constraints, rev_abbrevs) =>
wenzelm@19364
   283
      let
wenzelm@26050
   284
        val tags' = tags |> Position.default_properties (Position.thread_data ());
wenzelm@26050
   285
        val decl = {T = T, typargs = typargs_of T, tags = tags', authentic = true};
wenzelm@25404
   286
        val abbr = {rhs = rhs, normal_rhs = normal_rhs, force_expand = force_expand};
haftmann@28861
   287
        val (_, decls') = decls
haftmann@28861
   288
          |> extend_decls naming (b, ((decl, SOME abbr), serial ()));
wenzelm@19364
   289
        val rev_abbrevs' = rev_abbrevs
wenzelm@25389
   290
          |> fold (curry Symtab.cons_list mode) (rev_abbrev lhs rhs);
wenzelm@24673
   291
      in (decls', constraints, rev_abbrevs') end)
wenzelm@21794
   292
    |> pair (lhs, rhs)
wenzelm@19364
   293
  end;
wenzelm@18935
   294
wenzelm@25048
   295
fun revert_abbrev mode c consts = consts |> map_consts (fn (decls, constraints, rev_abbrevs) =>
wenzelm@25048
   296
  let
wenzelm@25048
   297
    val (T, rhs) = the_abbreviation consts c;
wenzelm@25048
   298
    val rev_abbrevs' = rev_abbrevs
wenzelm@25389
   299
      |> fold (curry Symtab.cons_list mode) (rev_abbrev (Const (c, T)) rhs);
wenzelm@25048
   300
  in (decls, constraints, rev_abbrevs') end);
wenzelm@25048
   301
wenzelm@19027
   302
end;
wenzelm@19027
   303
wenzelm@18060
   304
wenzelm@18060
   305
(* empty and merge *)
wenzelm@18060
   306
wenzelm@24673
   307
val empty = make_consts (NameSpace.empty_table, Symtab.empty, Symtab.empty);
wenzelm@18060
   308
wenzelm@18060
   309
fun merge
wenzelm@24673
   310
   (Consts ({decls = decls1, constraints = constraints1, rev_abbrevs = rev_abbrevs1}, _),
wenzelm@24673
   311
    Consts ({decls = decls2, constraints = constraints2, rev_abbrevs = rev_abbrevs2}, _)) =
wenzelm@18060
   312
  let
wenzelm@18935
   313
    val decls' = NameSpace.merge_tables (eq_snd (op =)) (decls1, decls2)
wenzelm@23655
   314
      handle Symtab.DUP c => err_dup_const c;
haftmann@25037
   315
    val constraints' = Symtab.join (K fst) (constraints1, constraints2);
wenzelm@19364
   316
    val rev_abbrevs' = (rev_abbrevs1, rev_abbrevs2) |> Symtab.join
wenzelm@19364
   317
      (K (Library.merge (fn ((t, u), (t', u')) => t aconv t' andalso u aconv u')));
wenzelm@24673
   318
  in make_consts (decls', constraints', rev_abbrevs') end;
wenzelm@18060
   319
wenzelm@18060
   320
end;