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