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