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