src/Pure/consts.ML
author wenzelm
Fri Oct 07 21:16:48 2016 +0200 (2016-10-07)
changeset 64092 95469c544b82
parent 63632 a59d9b81be24
permissions -rw-r--r--
accept obscure timezone used in 2011;
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@56056
    12
  val change_base: bool -> T -> T
wenzelm@56139
    13
  val change_ignore: T -> T
wenzelm@30566
    14
  val retrieve_abbrevs: T -> string list -> term -> (term * term) list
wenzelm@18935
    15
  val dest: T ->
wenzelm@56025
    16
   {const_space: Name_Space.T,
wenzelm@56062
    17
    constants: (string * (typ * term option)) list,
wenzelm@56062
    18
    constraints: (string * typ) list}
wenzelm@43794
    19
  val the_const: T -> string -> string * typ                   (*exception TYPE*)
wenzelm@25048
    20
  val the_abbreviation: T -> string -> typ * term              (*exception TYPE*)
wenzelm@25048
    21
  val type_scheme: T -> string -> typ                          (*exception TYPE*)
wenzelm@25048
    22
  val is_monomorphic: T -> string -> bool                      (*exception TYPE*)
wenzelm@25048
    23
  val the_constraint: T -> string -> typ                       (*exception TYPE*)
wenzelm@33095
    24
  val space_of: T -> Name_Space.T
wenzelm@35680
    25
  val alias: Name_Space.naming -> binding -> string -> T -> T
wenzelm@33158
    26
  val is_concealed: T -> string -> bool
wenzelm@19364
    27
  val intern: T -> xstring -> string
wenzelm@35255
    28
  val intern_syntax: T -> xstring -> string
wenzelm@55956
    29
  val check_const: Context.generic -> T -> xstring * Position.T list -> term * Position.report list
wenzelm@61262
    30
  val certify: Context.generic -> Type.tsig -> bool -> T -> term -> term  (*exception TYPE*)
wenzelm@18146
    31
  val typargs: T -> string * typ -> typ list
wenzelm@18163
    32
  val instance: T -> string * typ list -> typ
wenzelm@47005
    33
  val declare: Context.generic -> binding * typ -> T -> T
wenzelm@19098
    34
  val constrain: string * typ option -> T -> T
wenzelm@47005
    35
  val abbreviate: Context.generic -> Type.tsig -> string -> 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@35255
    49
type decl = {T: typ, typargs: int list list};
wenzelm@24909
    50
type abbrev = {rhs: term, normal_rhs: term, force_expand: bool};
wenzelm@18935
    51
wenzelm@18060
    52
datatype T = Consts of
wenzelm@33095
    53
 {decls: (decl * abbrev option) Name_Space.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@56056
    70
fun change_base begin = map_consts (fn (decls, constraints, rev_abbrevs) =>
wenzelm@56056
    71
  (Name_Space.change_base begin decls, constraints, rev_abbrevs));
wenzelm@56056
    72
wenzelm@56139
    73
val change_ignore = map_consts (fn (decls, constraints, rev_abbrevs) =>
wenzelm@56139
    74
  (Name_Space.change_ignore decls, constraints, rev_abbrevs));
wenzelm@56139
    75
wenzelm@30566
    76
wenzelm@30566
    77
(* reverted abbrevs *)
wenzelm@30566
    78
wenzelm@33173
    79
val empty_abbrevs =
wenzelm@33373
    80
  Item_Net.init (fn ((t, u), (t', u')) => t aconv t' andalso u aconv u') (single o #1);
wenzelm@30566
    81
wenzelm@33373
    82
fun update_abbrevs mode abbrs =
wenzelm@33373
    83
  Symtab.map_default (mode, empty_abbrevs) (Item_Net.update abbrs);
wenzelm@30566
    84
wenzelm@30566
    85
fun retrieve_abbrevs (Consts {rev_abbrevs, ...}) modes =
wenzelm@63632
    86
  let val nets = map_filter (Symtab.lookup rev_abbrevs) modes in
wenzelm@63632
    87
    fn t =>
wenzelm@63632
    88
      let
wenzelm@63632
    89
        val retrieve =
wenzelm@63632
    90
          if Term.could_beta_eta_contract t
wenzelm@63632
    91
          then Item_Net.retrieve
wenzelm@63632
    92
          else Item_Net.retrieve_matching
wenzelm@63632
    93
      in maps (fn net => retrieve net t) nets end
wenzelm@63632
    94
  end;
wenzelm@19364
    95
wenzelm@18965
    96
wenzelm@19364
    97
(* dest consts *)
wenzelm@19364
    98
wenzelm@56025
    99
fun dest (Consts {decls, constraints, ...}) =
wenzelm@56025
   100
 {const_space = Name_Space.space_of_table decls,
wenzelm@56025
   101
  constants =
wenzelm@56025
   102
    Name_Space.fold_table (fn (c, ({T, ...}, abbr)) =>
wenzelm@56062
   103
      cons (c, (T, Option.map #rhs abbr))) decls [],
wenzelm@56062
   104
  constraints = Symtab.dest constraints};
wenzelm@18060
   105
wenzelm@18060
   106
wenzelm@18060
   107
(* lookup consts *)
wenzelm@18060
   108
wenzelm@56025
   109
fun the_entry (Consts {decls, ...}) c =
wenzelm@56025
   110
  (case Name_Space.lookup_key decls c of
wenzelm@43794
   111
    SOME entry => entry
wenzelm@25041
   112
  | NONE => raise TYPE ("Unknown constant: " ^ quote c, [], []));
wenzelm@18935
   113
wenzelm@43794
   114
fun the_const consts c =
wenzelm@43794
   115
  (case the_entry consts c of
wenzelm@43794
   116
    (c', ({T, ...}, NONE)) => (c', T)
wenzelm@21720
   117
  | _ => raise TYPE ("Not a logical constant: " ^ quote c, [], []));
wenzelm@18060
   118
wenzelm@21720
   119
fun the_abbreviation consts c =
wenzelm@43794
   120
  (case the_entry consts c of
wenzelm@43794
   121
    (_, ({T, ...}, SOME {rhs, ...})) => (T, rhs)
wenzelm@21720
   122
  | _ => raise TYPE ("Not an abbreviated constant: " ^ quote c, [], []));
wenzelm@21720
   123
wenzelm@43794
   124
fun the_decl consts = #1 o #2 o the_entry consts;
wenzelm@25041
   125
val type_scheme = #T oo the_decl;
wenzelm@25041
   126
val type_arguments = #typargs oo the_decl;
wenzelm@25041
   127
wenzelm@21720
   128
val is_monomorphic = null oo type_arguments;
wenzelm@18935
   129
wenzelm@30284
   130
fun the_constraint (consts as Consts {constraints, ...}) c =
wenzelm@18060
   131
  (case Symtab.lookup constraints c of
wenzelm@18060
   132
    SOME T => T
wenzelm@25041
   133
  | NONE => type_scheme consts c);
wenzelm@18935
   134
wenzelm@18935
   135
wenzelm@19657
   136
(* name space and syntax *)
wenzelm@19364
   137
wenzelm@56025
   138
fun space_of (Consts {decls, ...}) = Name_Space.space_of_table decls;
wenzelm@19364
   139
wenzelm@56025
   140
fun alias naming binding name = map_consts (fn (decls, constraints, rev_abbrevs) =>
wenzelm@56025
   141
  ((Name_Space.alias_table naming binding name decls), constraints, rev_abbrevs));
wenzelm@35680
   142
wenzelm@33158
   143
val is_concealed = Name_Space.is_concealed o space_of;
wenzelm@33158
   144
wenzelm@33095
   145
val intern = Name_Space.intern o space_of;
wenzelm@19364
   146
wenzelm@35554
   147
fun intern_syntax consts s =
wenzelm@42290
   148
  (case try Lexicon.unmark_const s of
wenzelm@35255
   149
    SOME c => c
wenzelm@35554
   150
  | NONE => intern consts s);
wenzelm@35554
   151
wenzelm@18060
   152
wenzelm@55843
   153
(* check_const *)
wenzelm@19364
   154
wenzelm@55956
   155
fun check_const context consts (xname, ps) =
wenzelm@19364
   156
  let
wenzelm@55843
   157
    val Consts {decls, ...} = consts;
wenzelm@55956
   158
    val ((c, reports), _) = Name_Space.check_reports context decls (xname, ps);
wenzelm@55956
   159
    val T = type_scheme consts c handle TYPE (msg, _, _) => error (msg ^ Position.here_list ps);
wenzelm@55950
   160
  in (Const (c, T), reports) end;
wenzelm@19364
   161
wenzelm@19364
   162
wenzelm@19364
   163
(* certify *)
wenzelm@19364
   164
wenzelm@61262
   165
fun certify context tsig do_expand consts =
wenzelm@18965
   166
  let
wenzelm@18965
   167
    fun err msg (c, T) =
wenzelm@42383
   168
      raise TYPE (msg ^ " " ^ quote c ^ " :: " ^
wenzelm@61262
   169
        Syntax.string_of_typ (Syntax.init_pretty context) T, [], []);
wenzelm@24673
   170
    val certT = Type.cert_typ tsig;
wenzelm@18965
   171
    fun cert tm =
wenzelm@18965
   172
      let
wenzelm@18965
   173
        val (head, args) = Term.strip_comb tm;
wenzelm@21694
   174
        val args' = map cert args;
wenzelm@21694
   175
        fun comb head' = Term.list_comb (head', args');
wenzelm@18965
   176
      in
wenzelm@18965
   177
        (case head of
wenzelm@24673
   178
          Abs (x, T, t) => comb (Abs (x, certT T, cert t))
wenzelm@19364
   179
        | Const (c, T) =>
wenzelm@19364
   180
            let
wenzelm@24673
   181
              val T' = certT T;
wenzelm@43794
   182
              val (_, ({T = U, ...}, abbr)) = the_entry consts c;
wenzelm@25048
   183
              fun expand u =
wenzelm@25048
   184
                Term.betapplys (Envir.expand_atom T' (U, u) handle TYPE _ =>
wenzelm@25048
   185
                  err "Illegal type for abbreviation" (c, T), args');
wenzelm@19364
   186
            in
wenzelm@19433
   187
              if not (Type.raw_instance (T', U)) then
wenzelm@19364
   188
                err "Illegal type for constant" (c, T)
wenzelm@19364
   189
              else
wenzelm@24909
   190
                (case abbr of
wenzelm@25048
   191
                  SOME {rhs, normal_rhs, force_expand} =>
wenzelm@25048
   192
                    if do_expand then expand normal_rhs
wenzelm@25048
   193
                    else if force_expand then expand rhs
wenzelm@21720
   194
                    else comb head
wenzelm@19364
   195
                | _ => comb head)
wenzelm@19364
   196
            end
wenzelm@19364
   197
        | _ => comb head)
wenzelm@18965
   198
      end;
wenzelm@18965
   199
  in cert end;
wenzelm@18965
   200
wenzelm@18965
   201
wenzelm@18060
   202
(* typargs -- view actual const type as instance of declaration *)
wenzelm@18060
   203
wenzelm@24909
   204
local
wenzelm@24909
   205
wenzelm@24909
   206
fun args_of (Type (_, Ts)) pos = args_of_list Ts 0 pos
wenzelm@24909
   207
  | args_of (TVar v) pos = insert (eq_fst op =) (v, rev pos)
wenzelm@24909
   208
  | args_of (TFree _) _ = I
wenzelm@24909
   209
and args_of_list (T :: Ts) i is = args_of T (i :: is) #> args_of_list Ts (i + 1) is
wenzelm@24909
   210
  | args_of_list [] _ _ = I;
wenzelm@24909
   211
wenzelm@19364
   212
fun subscript (Type (_, Ts)) (i :: is) = subscript (nth Ts i) is
wenzelm@19364
   213
  | subscript T [] = T
wenzelm@32784
   214
  | subscript _ _ = raise Subscript;
wenzelm@18060
   215
wenzelm@24909
   216
in
wenzelm@24909
   217
wenzelm@24909
   218
fun typargs_of T = map #2 (rev (args_of T [] []));
wenzelm@24909
   219
wenzelm@19364
   220
fun typargs consts (c, T) = map (subscript T) (type_arguments consts c);
wenzelm@18060
   221
wenzelm@24909
   222
end;
wenzelm@24909
   223
wenzelm@18163
   224
fun instance consts (c, Ts) =
wenzelm@18163
   225
  let
wenzelm@25041
   226
    val declT = type_scheme consts c;
wenzelm@18163
   227
    val vars = map Term.dest_TVar (typargs consts (c, declT));
wenzelm@40722
   228
    val inst = vars ~~ Ts handle ListPair.UnequalLengths =>
wenzelm@24673
   229
      raise TYPE ("Consts.instance", Ts, [Const (c, dummyT)]);
wenzelm@31977
   230
  in declT |> Term_Subst.instantiateT inst end;
wenzelm@18163
   231
wenzelm@18060
   232
wenzelm@18060
   233
wenzelm@19364
   234
(** build consts **)
wenzelm@18060
   235
wenzelm@18935
   236
(* name space *)
wenzelm@18060
   237
wenzelm@24673
   238
fun hide fully c = map_consts (fn (decls, constraints, rev_abbrevs) =>
wenzelm@56025
   239
  (Name_Space.hide_table fully c decls, constraints, rev_abbrevs));
wenzelm@18935
   240
wenzelm@18935
   241
wenzelm@18935
   242
(* declarations *)
wenzelm@18935
   243
wenzelm@47005
   244
fun declare context (b, declT) =
wenzelm@33092
   245
  map_consts (fn (decls, constraints, rev_abbrevs) =>
wenzelm@33092
   246
    let
wenzelm@35255
   247
      val decl = {T = declT, typargs = typargs_of declT};
wenzelm@41254
   248
      val _ = Binding.check b;
wenzelm@47005
   249
      val (_, decls') = decls |> Name_Space.define context true (b, (decl, NONE));
wenzelm@33092
   250
    in (decls', constraints, rev_abbrevs) end);
wenzelm@19364
   251
wenzelm@19364
   252
wenzelm@19364
   253
(* constraints *)
wenzelm@19364
   254
wenzelm@19364
   255
fun constrain (c, C) consts =
wenzelm@24673
   256
  consts |> map_consts (fn (decls, constraints, rev_abbrevs) =>
wenzelm@43794
   257
    (#2 (the_entry consts c) handle TYPE (msg, _, _) => error msg;
wenzelm@19364
   258
      (decls,
wenzelm@19364
   259
        constraints |> (case C of SOME T => Symtab.update (c, T) | NONE => Symtab.delete_safe c),
wenzelm@24673
   260
        rev_abbrevs)));
wenzelm@18060
   261
wenzelm@18935
   262
wenzelm@18935
   263
(* abbreviations *)
wenzelm@18935
   264
wenzelm@19027
   265
local
wenzelm@19027
   266
wenzelm@30568
   267
fun strip_abss (t as Abs (x, T, b)) =
wenzelm@42083
   268
      if Term.is_dependent b then strip_abss b |>> cons (x, T)  (* FIXME decr!? *)
wenzelm@30568
   269
      else ([], t)
wenzelm@30568
   270
  | strip_abss t = ([], t);
wenzelm@19027
   271
wenzelm@21720
   272
fun rev_abbrev lhs rhs =
wenzelm@18060
   273
  let
wenzelm@30568
   274
    val (xs, body) = strip_abss (Envir.beta_eta_contract rhs);
wenzelm@30568
   275
    val vars = fold (fn (x, T) => cons (Var ((x, 0), T))) (Term.rename_wrt_term body xs) [];
wenzelm@30568
   276
  in (Term.subst_bounds (rev vars, body), Term.list_comb (lhs, vars)) end;
wenzelm@19027
   277
wenzelm@19027
   278
in
wenzelm@18965
   279
wenzelm@47005
   280
fun abbreviate context tsig mode (b, raw_rhs) consts =
wenzelm@18965
   281
  let
wenzelm@61262
   282
    val cert_term = certify context tsig false consts;
wenzelm@61262
   283
    val expand_term = certify context tsig true consts;
wenzelm@37146
   284
    val force_expand = mode = Print_Mode.internal;
wenzelm@21720
   285
wenzelm@30286
   286
    val _ = Term.exists_subterm Term.is_Var raw_rhs andalso
wenzelm@42381
   287
      error ("Illegal schematic variables on rhs of abbreviation " ^ Binding.print b);
wenzelm@30286
   288
wenzelm@19364
   289
    val rhs = raw_rhs
wenzelm@20548
   290
      |> Term.map_types (Type.cert_typ tsig)
wenzelm@30286
   291
      |> cert_term
wenzelm@30286
   292
      |> Term.close_schematic_term;
wenzelm@25404
   293
    val normal_rhs = expand_term rhs;
wenzelm@21794
   294
    val T = Term.fastype_of rhs;
wenzelm@47005
   295
    val lhs = Const (Name_Space.full_name (Name_Space.naming_of context) b, T);
wenzelm@19364
   296
  in
wenzelm@24673
   297
    consts |> map_consts (fn (decls, constraints, rev_abbrevs) =>
wenzelm@19364
   298
      let
wenzelm@35255
   299
        val decl = {T = T, typargs = typargs_of T};
wenzelm@25404
   300
        val abbr = {rhs = rhs, normal_rhs = normal_rhs, force_expand = force_expand};
wenzelm@41254
   301
        val _ = Binding.check b;
haftmann@28861
   302
        val (_, decls') = decls
wenzelm@47005
   303
          |> Name_Space.define context true (b, (decl, SOME abbr));
wenzelm@19364
   304
        val rev_abbrevs' = rev_abbrevs
wenzelm@33373
   305
          |> update_abbrevs mode (rev_abbrev lhs rhs);
wenzelm@24673
   306
      in (decls', constraints, rev_abbrevs') end)
wenzelm@21794
   307
    |> pair (lhs, rhs)
wenzelm@19364
   308
  end;
wenzelm@18935
   309
wenzelm@25048
   310
fun revert_abbrev mode c consts = consts |> map_consts (fn (decls, constraints, rev_abbrevs) =>
wenzelm@25048
   311
  let
wenzelm@25048
   312
    val (T, rhs) = the_abbreviation consts c;
wenzelm@25048
   313
    val rev_abbrevs' = rev_abbrevs
wenzelm@33373
   314
      |> update_abbrevs mode (rev_abbrev (Const (c, T)) rhs);
wenzelm@25048
   315
  in (decls, constraints, rev_abbrevs') end);
wenzelm@25048
   316
wenzelm@19027
   317
end;
wenzelm@19027
   318
wenzelm@18060
   319
wenzelm@18060
   320
(* empty and merge *)
wenzelm@18060
   321
wenzelm@45666
   322
val empty =
wenzelm@50201
   323
  make_consts (Name_Space.empty_table Markup.constantN, Symtab.empty, Symtab.empty);
wenzelm@18060
   324
wenzelm@18060
   325
fun merge
wenzelm@30284
   326
   (Consts {decls = decls1, constraints = constraints1, rev_abbrevs = rev_abbrevs1},
wenzelm@30284
   327
    Consts {decls = decls2, constraints = constraints2, rev_abbrevs = rev_abbrevs2}) =
wenzelm@18060
   328
  let
wenzelm@33095
   329
    val decls' = Name_Space.merge_tables (decls1, decls2);
wenzelm@50768
   330
    val constraints' = Symtab.merge (K true) (constraints1, constraints2);
wenzelm@30566
   331
    val rev_abbrevs' = Symtab.join (K Item_Net.merge) (rev_abbrevs1, rev_abbrevs2);
wenzelm@24673
   332
  in make_consts (decls', constraints', rev_abbrevs') end;
wenzelm@18060
   333
wenzelm@18060
   334
end;