src/Pure/consts.ML
author wenzelm
Sun Feb 21 22:35:02 2010 +0100 (2010-02-21 ago)
changeset 35262 9ea4445d2ccf
parent 35255 2cb27605301f
child 35554 1e05ea0a5cd7
permissions -rw-r--r--
slightly more abstract syntax mark/unmark operations;
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@33095
    14
   {constants: (typ * term option) Name_Space.table,
wenzelm@33095
    15
    constraints: typ Name_Space.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@25048
    19
  val is_monomorphic: T -> string -> bool                      (*exception TYPE*)
wenzelm@25048
    20
  val the_constraint: T -> string -> typ                       (*exception TYPE*)
wenzelm@33095
    21
  val space_of: T -> Name_Space.T
wenzelm@33158
    22
  val is_concealed: T -> string -> bool
wenzelm@19364
    23
  val intern: T -> xstring -> string
wenzelm@35255
    24
  val intern_syntax: T -> xstring -> string
wenzelm@19364
    25
  val extern: T -> string -> xstring
wenzelm@18965
    26
  val read_const: T -> string -> term
wenzelm@24673
    27
  val certify: Pretty.pp -> Type.tsig -> bool -> T -> term -> term  (*exception TYPE*)
wenzelm@18146
    28
  val typargs: T -> string * typ -> typ list
wenzelm@18163
    29
  val instance: T -> string * typ list -> typ
wenzelm@35255
    30
  val declare: Name_Space.naming -> binding * typ -> T -> T
wenzelm@19098
    31
  val constrain: string * typ option -> T -> T
wenzelm@33173
    32
  val abbreviate: Pretty.pp -> Type.tsig -> Name_Space.naming -> string ->
haftmann@29581
    33
    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@30284
    84
fun dest (Consts {decls = (space, decls), constraints, ...}) =
wenzelm@19364
    85
 {constants = (space,
wenzelm@33092
    86
    Symtab.fold (fn (c, ({T, ...}, abbr)) =>
wenzelm@25404
    87
      Symtab.update (c, (T, Option.map #rhs abbr))) decls Symtab.empty),
wenzelm@18060
    88
  constraints = (space, constraints)};
wenzelm@18060
    89
wenzelm@18060
    90
wenzelm@18060
    91
(* lookup consts *)
wenzelm@18060
    92
wenzelm@30284
    93
fun the_const (Consts {decls = (_, tab), ...}) c =
wenzelm@19364
    94
  (case Symtab.lookup tab c of
wenzelm@33092
    95
    SOME decl => decl
wenzelm@25041
    96
  | NONE => raise TYPE ("Unknown constant: " ^ quote c, [], []));
wenzelm@18935
    97
wenzelm@25041
    98
fun the_type consts c =
wenzelm@24772
    99
  (case the_const consts c of
wenzelm@24909
   100
    ({T, ...}, NONE) => T
wenzelm@21720
   101
  | _ => raise TYPE ("Not a logical constant: " ^ quote c, [], []));
wenzelm@18060
   102
wenzelm@21720
   103
fun the_abbreviation consts c =
wenzelm@24772
   104
  (case the_const consts c of
wenzelm@25048
   105
    ({T, ...}, SOME {rhs, ...}) => (T, rhs)
wenzelm@21720
   106
  | _ => raise TYPE ("Not an abbreviated constant: " ^ quote c, [], []));
wenzelm@21720
   107
wenzelm@25041
   108
val the_decl = #1 oo the_const;
wenzelm@25041
   109
val type_scheme = #T oo the_decl;
wenzelm@25041
   110
val type_arguments = #typargs oo the_decl;
wenzelm@25041
   111
wenzelm@21720
   112
val is_monomorphic = null oo type_arguments;
wenzelm@18935
   113
wenzelm@30284
   114
fun the_constraint (consts as Consts {constraints, ...}) c =
wenzelm@18060
   115
  (case Symtab.lookup constraints c of
wenzelm@18060
   116
    SOME T => T
wenzelm@25041
   117
  | NONE => type_scheme consts c);
wenzelm@18935
   118
wenzelm@18935
   119
wenzelm@19657
   120
(* name space and syntax *)
wenzelm@19364
   121
wenzelm@30284
   122
fun space_of (Consts {decls = (space, _), ...}) = space;
wenzelm@19364
   123
wenzelm@33158
   124
val is_concealed = Name_Space.is_concealed o space_of;
wenzelm@33158
   125
wenzelm@33095
   126
val intern = Name_Space.intern o space_of;
wenzelm@33095
   127
val extern = Name_Space.extern o space_of;
wenzelm@19364
   128
wenzelm@35255
   129
fun intern_syntax consts name =
wenzelm@35262
   130
  (case try Syntax.unmark_const name of
wenzelm@35255
   131
    SOME c => c
wenzelm@35255
   132
  | NONE => intern consts name);
wenzelm@21181
   133
wenzelm@18060
   134
wenzelm@19364
   135
(* read_const *)
wenzelm@19364
   136
wenzelm@19364
   137
fun read_const consts raw_c =
wenzelm@19364
   138
  let
wenzelm@19364
   139
    val c = intern consts raw_c;
wenzelm@25041
   140
    val T = type_scheme consts c handle TYPE (msg, _, _) => error msg;
wenzelm@21205
   141
  in Const (c, T) end;
wenzelm@19364
   142
wenzelm@19364
   143
wenzelm@19364
   144
(* certify *)
wenzelm@19364
   145
wenzelm@24673
   146
fun certify pp tsig do_expand consts =
wenzelm@18965
   147
  let
wenzelm@18965
   148
    fun err msg (c, T) =
wenzelm@18965
   149
      raise TYPE (msg ^ " " ^ quote c ^ " :: " ^ Pretty.string_of_typ pp T, [], []);
wenzelm@24673
   150
    val certT = Type.cert_typ tsig;
wenzelm@18965
   151
    fun cert tm =
wenzelm@18965
   152
      let
wenzelm@18965
   153
        val (head, args) = Term.strip_comb tm;
wenzelm@21694
   154
        val args' = map cert args;
wenzelm@21694
   155
        fun comb head' = Term.list_comb (head', args');
wenzelm@18965
   156
      in
wenzelm@18965
   157
        (case head of
wenzelm@24673
   158
          Abs (x, T, t) => comb (Abs (x, certT T, cert t))
wenzelm@19364
   159
        | Const (c, T) =>
wenzelm@19364
   160
            let
wenzelm@24673
   161
              val T' = certT T;
wenzelm@24909
   162
              val ({T = U, ...}, abbr) = the_const consts c;
wenzelm@25048
   163
              fun expand u =
wenzelm@25048
   164
                Term.betapplys (Envir.expand_atom T' (U, u) handle TYPE _ =>
wenzelm@25048
   165
                  err "Illegal type for abbreviation" (c, T), args');
wenzelm@19364
   166
            in
wenzelm@19433
   167
              if not (Type.raw_instance (T', U)) then
wenzelm@19364
   168
                err "Illegal type for constant" (c, T)
wenzelm@19364
   169
              else
wenzelm@24909
   170
                (case abbr of
wenzelm@25048
   171
                  SOME {rhs, normal_rhs, force_expand} =>
wenzelm@25048
   172
                    if do_expand then expand normal_rhs
wenzelm@25048
   173
                    else if force_expand then expand rhs
wenzelm@21720
   174
                    else comb head
wenzelm@19364
   175
                | _ => comb head)
wenzelm@19364
   176
            end
wenzelm@19364
   177
        | _ => comb head)
wenzelm@18965
   178
      end;
wenzelm@18965
   179
  in cert end;
wenzelm@18965
   180
wenzelm@18965
   181
wenzelm@18060
   182
(* typargs -- view actual const type as instance of declaration *)
wenzelm@18060
   183
wenzelm@24909
   184
local
wenzelm@24909
   185
wenzelm@24909
   186
fun args_of (Type (_, Ts)) pos = args_of_list Ts 0 pos
wenzelm@24909
   187
  | args_of (TVar v) pos = insert (eq_fst op =) (v, rev pos)
wenzelm@24909
   188
  | args_of (TFree _) _ = I
wenzelm@24909
   189
and args_of_list (T :: Ts) i is = args_of T (i :: is) #> args_of_list Ts (i + 1) is
wenzelm@24909
   190
  | args_of_list [] _ _ = I;
wenzelm@24909
   191
wenzelm@19364
   192
fun subscript (Type (_, Ts)) (i :: is) = subscript (nth Ts i) is
wenzelm@19364
   193
  | subscript T [] = T
wenzelm@32784
   194
  | subscript _ _ = raise Subscript;
wenzelm@18060
   195
wenzelm@24909
   196
in
wenzelm@24909
   197
wenzelm@24909
   198
fun typargs_of T = map #2 (rev (args_of T [] []));
wenzelm@24909
   199
wenzelm@19364
   200
fun typargs consts (c, T) = map (subscript T) (type_arguments consts c);
wenzelm@18060
   201
wenzelm@24909
   202
end;
wenzelm@24909
   203
wenzelm@18163
   204
fun instance consts (c, Ts) =
wenzelm@18163
   205
  let
wenzelm@25041
   206
    val declT = type_scheme consts c;
wenzelm@18163
   207
    val vars = map Term.dest_TVar (typargs consts (c, declT));
wenzelm@24673
   208
    val inst = vars ~~ Ts handle UnequalLengths =>
wenzelm@24673
   209
      raise TYPE ("Consts.instance", Ts, [Const (c, dummyT)]);
wenzelm@31977
   210
  in declT |> Term_Subst.instantiateT inst end;
wenzelm@18163
   211
wenzelm@18060
   212
wenzelm@18060
   213
wenzelm@19364
   214
(** build consts **)
wenzelm@18060
   215
wenzelm@18935
   216
(* name space *)
wenzelm@18060
   217
wenzelm@24673
   218
fun hide fully c = map_consts (fn (decls, constraints, rev_abbrevs) =>
wenzelm@33095
   219
  (apfst (Name_Space.hide fully c) decls, constraints, rev_abbrevs));
wenzelm@18935
   220
wenzelm@18935
   221
wenzelm@18935
   222
(* declarations *)
wenzelm@18935
   223
wenzelm@35255
   224
fun declare naming (b, declT) =
wenzelm@33092
   225
  map_consts (fn (decls, constraints, rev_abbrevs) =>
wenzelm@33092
   226
    let
wenzelm@35255
   227
      val decl = {T = declT, typargs = typargs_of declT};
wenzelm@33095
   228
      val (_, decls') = decls |> Name_Space.define true naming (b, (decl, NONE));
wenzelm@33092
   229
    in (decls', constraints, rev_abbrevs) end);
wenzelm@19364
   230
wenzelm@19364
   231
wenzelm@19364
   232
(* constraints *)
wenzelm@19364
   233
wenzelm@19364
   234
fun constrain (c, C) consts =
wenzelm@24673
   235
  consts |> map_consts (fn (decls, constraints, rev_abbrevs) =>
wenzelm@19364
   236
    (the_const consts c handle TYPE (msg, _, _) => error msg;
wenzelm@19364
   237
      (decls,
wenzelm@19364
   238
        constraints |> (case C of SOME T => Symtab.update (c, T) | NONE => Symtab.delete_safe c),
wenzelm@24673
   239
        rev_abbrevs)));
wenzelm@18060
   240
wenzelm@18935
   241
wenzelm@18935
   242
(* abbreviations *)
wenzelm@18935
   243
wenzelm@19027
   244
local
wenzelm@19027
   245
wenzelm@30568
   246
fun strip_abss (t as Abs (x, T, b)) =
wenzelm@30568
   247
      if Term.loose_bvar1 (b, 0) then strip_abss b |>> cons (x, T)
wenzelm@30568
   248
      else ([], t)
wenzelm@30568
   249
  | strip_abss t = ([], t);
wenzelm@19027
   250
wenzelm@21720
   251
fun rev_abbrev lhs rhs =
wenzelm@18060
   252
  let
wenzelm@30568
   253
    val (xs, body) = strip_abss (Envir.beta_eta_contract rhs);
wenzelm@30568
   254
    val vars = fold (fn (x, T) => cons (Var ((x, 0), T))) (Term.rename_wrt_term body xs) [];
wenzelm@30568
   255
  in (Term.subst_bounds (rev vars, body), Term.list_comb (lhs, vars)) end;
wenzelm@19027
   256
wenzelm@19027
   257
in
wenzelm@18965
   258
wenzelm@33173
   259
fun abbreviate pp tsig naming mode (b, raw_rhs) consts =
wenzelm@18965
   260
  let
wenzelm@24673
   261
    val cert_term = certify pp tsig false consts;
wenzelm@24673
   262
    val expand_term = certify pp tsig true consts;
wenzelm@25116
   263
    val force_expand = mode = PrintMode.internal;
wenzelm@21720
   264
wenzelm@30286
   265
    val _ = Term.exists_subterm Term.is_Var raw_rhs andalso
wenzelm@33092
   266
      error ("Illegal schematic variables on rhs of abbreviation " ^ quote (Binding.str_of b));
wenzelm@30286
   267
wenzelm@19364
   268
    val rhs = raw_rhs
wenzelm@20548
   269
      |> Term.map_types (Type.cert_typ tsig)
wenzelm@30286
   270
      |> cert_term
wenzelm@30286
   271
      |> Term.close_schematic_term;
wenzelm@25404
   272
    val normal_rhs = expand_term rhs;
wenzelm@21794
   273
    val T = Term.fastype_of rhs;
wenzelm@33095
   274
    val lhs = Const (Name_Space.full_name naming b, T);
wenzelm@19364
   275
  in
wenzelm@24673
   276
    consts |> map_consts (fn (decls, constraints, rev_abbrevs) =>
wenzelm@19364
   277
      let
wenzelm@35255
   278
        val decl = {T = T, typargs = typargs_of T};
wenzelm@25404
   279
        val abbr = {rhs = rhs, normal_rhs = normal_rhs, force_expand = force_expand};
haftmann@28861
   280
        val (_, decls') = decls
wenzelm@33095
   281
          |> Name_Space.define true naming (b, (decl, SOME abbr));
wenzelm@19364
   282
        val rev_abbrevs' = rev_abbrevs
wenzelm@33373
   283
          |> update_abbrevs mode (rev_abbrev lhs rhs);
wenzelm@24673
   284
      in (decls', constraints, rev_abbrevs') end)
wenzelm@21794
   285
    |> pair (lhs, rhs)
wenzelm@19364
   286
  end;
wenzelm@18935
   287
wenzelm@25048
   288
fun revert_abbrev mode c consts = consts |> map_consts (fn (decls, constraints, rev_abbrevs) =>
wenzelm@25048
   289
  let
wenzelm@25048
   290
    val (T, rhs) = the_abbreviation consts c;
wenzelm@25048
   291
    val rev_abbrevs' = rev_abbrevs
wenzelm@33373
   292
      |> update_abbrevs mode (rev_abbrev (Const (c, T)) rhs);
wenzelm@25048
   293
  in (decls, constraints, rev_abbrevs') end);
wenzelm@25048
   294
wenzelm@19027
   295
end;
wenzelm@19027
   296
wenzelm@18060
   297
wenzelm@18060
   298
(* empty and merge *)
wenzelm@18060
   299
wenzelm@33096
   300
val empty = make_consts (Name_Space.empty_table "constant", Symtab.empty, Symtab.empty);
wenzelm@18060
   301
wenzelm@18060
   302
fun merge
wenzelm@30284
   303
   (Consts {decls = decls1, constraints = constraints1, rev_abbrevs = rev_abbrevs1},
wenzelm@30284
   304
    Consts {decls = decls2, constraints = constraints2, rev_abbrevs = rev_abbrevs2}) =
wenzelm@18060
   305
  let
wenzelm@33095
   306
    val decls' = Name_Space.merge_tables (decls1, decls2);
haftmann@25037
   307
    val constraints' = Symtab.join (K fst) (constraints1, constraints2);
wenzelm@30566
   308
    val rev_abbrevs' = Symtab.join (K Item_Net.merge) (rev_abbrevs1, rev_abbrevs2);
wenzelm@24673
   309
  in make_consts (decls', constraints', rev_abbrevs') end;
wenzelm@18060
   310
wenzelm@18060
   311
end;