src/Pure/consts.ML
author wenzelm
Sat Oct 24 19:47:37 2009 +0200 (2009-10-24 ago)
changeset 33095 bbd52d2f8696
parent 33092 c859019d3ac5
child 33096 db3c18fd9708
permissions -rw-r--r--
renamed NameSpace to Name_Space -- also to emphasize its subtle change in semantics;
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@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@33095
    22
  val space_of: T -> Name_Space.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
wenzelm@33095
    32
  val declare: bool -> Name_Space.naming -> Properties.T -> (binding * typ) -> T -> T
wenzelm@19098
    33
  val constrain: string * typ option -> T -> T
wenzelm@33095
    34
  val abbreviate: Pretty.pp -> Type.tsig -> Name_Space.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@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@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@33092
    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@33092
    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@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@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@18935
   224
(* name space *)
wenzelm@18060
   225
wenzelm@24673
   226
fun hide fully c = map_consts (fn (decls, constraints, rev_abbrevs) =>
wenzelm@33095
   227
  (apfst (Name_Space.hide fully c) decls, constraints, rev_abbrevs));
wenzelm@18935
   228
wenzelm@18935
   229
wenzelm@18935
   230
(* declarations *)
wenzelm@18935
   231
wenzelm@33092
   232
fun declare authentic naming tags (b, declT) =
wenzelm@33092
   233
  map_consts (fn (decls, constraints, rev_abbrevs) =>
wenzelm@33092
   234
    let
wenzelm@33092
   235
      val tags' = tags |> Position.default_properties (Position.thread_data ());
wenzelm@33092
   236
      val decl = {T = declT, typargs = typargs_of declT, tags = tags', authentic = authentic};
wenzelm@33095
   237
      val (_, decls') = decls |> Name_Space.define true naming (b, (decl, NONE));
wenzelm@33092
   238
    in (decls', constraints, rev_abbrevs) end);
wenzelm@19364
   239
wenzelm@19364
   240
wenzelm@19364
   241
(* constraints *)
wenzelm@19364
   242
wenzelm@19364
   243
fun constrain (c, C) consts =
wenzelm@24673
   244
  consts |> map_consts (fn (decls, constraints, rev_abbrevs) =>
wenzelm@19364
   245
    (the_const consts c handle TYPE (msg, _, _) => error msg;
wenzelm@19364
   246
      (decls,
wenzelm@19364
   247
        constraints |> (case C of SOME T => Symtab.update (c, T) | NONE => Symtab.delete_safe c),
wenzelm@24673
   248
        rev_abbrevs)));
wenzelm@18060
   249
wenzelm@18935
   250
wenzelm@18935
   251
(* abbreviations *)
wenzelm@18935
   252
wenzelm@19027
   253
local
wenzelm@19027
   254
wenzelm@30568
   255
fun strip_abss (t as Abs (x, T, b)) =
wenzelm@30568
   256
      if Term.loose_bvar1 (b, 0) then strip_abss b |>> cons (x, T)
wenzelm@30568
   257
      else ([], t)
wenzelm@30568
   258
  | strip_abss t = ([], t);
wenzelm@19027
   259
wenzelm@21720
   260
fun rev_abbrev lhs rhs =
wenzelm@18060
   261
  let
wenzelm@30568
   262
    val (xs, body) = strip_abss (Envir.beta_eta_contract rhs);
wenzelm@30568
   263
    val vars = fold (fn (x, T) => cons (Var ((x, 0), T))) (Term.rename_wrt_term body xs) [];
wenzelm@30568
   264
  in (Term.subst_bounds (rev vars, body), Term.list_comb (lhs, vars)) end;
wenzelm@19027
   265
wenzelm@19027
   266
in
wenzelm@18965
   267
haftmann@28861
   268
fun abbreviate pp tsig naming mode tags (b, raw_rhs) consts =
wenzelm@18965
   269
  let
wenzelm@24673
   270
    val cert_term = certify pp tsig false consts;
wenzelm@24673
   271
    val expand_term = certify pp tsig true consts;
wenzelm@25116
   272
    val force_expand = mode = PrintMode.internal;
wenzelm@21720
   273
wenzelm@30286
   274
    val _ = Term.exists_subterm Term.is_Var raw_rhs andalso
wenzelm@33092
   275
      error ("Illegal schematic variables on rhs of abbreviation " ^ quote (Binding.str_of b));
wenzelm@30286
   276
wenzelm@19364
   277
    val rhs = raw_rhs
wenzelm@20548
   278
      |> Term.map_types (Type.cert_typ tsig)
wenzelm@30286
   279
      |> cert_term
wenzelm@30286
   280
      |> Term.close_schematic_term;
wenzelm@25404
   281
    val normal_rhs = expand_term rhs;
wenzelm@21794
   282
    val T = Term.fastype_of rhs;
wenzelm@33095
   283
    val lhs = Const (Name_Space.full_name naming b, T);
wenzelm@19364
   284
  in
wenzelm@24673
   285
    consts |> map_consts (fn (decls, constraints, rev_abbrevs) =>
wenzelm@19364
   286
      let
wenzelm@26050
   287
        val tags' = tags |> Position.default_properties (Position.thread_data ());
wenzelm@26050
   288
        val decl = {T = T, typargs = typargs_of T, tags = tags', authentic = true};
wenzelm@25404
   289
        val abbr = {rhs = rhs, normal_rhs = normal_rhs, force_expand = force_expand};
haftmann@28861
   290
        val (_, decls') = decls
wenzelm@33095
   291
          |> Name_Space.define true naming (b, (decl, SOME abbr));
wenzelm@19364
   292
        val rev_abbrevs' = rev_abbrevs
wenzelm@30566
   293
          |> insert_abbrevs mode (rev_abbrev lhs rhs);
wenzelm@24673
   294
      in (decls', constraints, rev_abbrevs') end)
wenzelm@21794
   295
    |> pair (lhs, rhs)
wenzelm@19364
   296
  end;
wenzelm@18935
   297
wenzelm@25048
   298
fun revert_abbrev mode c consts = consts |> map_consts (fn (decls, constraints, rev_abbrevs) =>
wenzelm@25048
   299
  let
wenzelm@25048
   300
    val (T, rhs) = the_abbreviation consts c;
wenzelm@25048
   301
    val rev_abbrevs' = rev_abbrevs
wenzelm@30566
   302
      |> insert_abbrevs mode (rev_abbrev (Const (c, T)) rhs);
wenzelm@25048
   303
  in (decls, constraints, rev_abbrevs') end);
wenzelm@25048
   304
wenzelm@19027
   305
end;
wenzelm@19027
   306
wenzelm@18060
   307
wenzelm@18060
   308
(* empty and merge *)
wenzelm@18060
   309
wenzelm@33095
   310
val empty = make_consts (Name_Space.empty_table, Symtab.empty, Symtab.empty);
wenzelm@18060
   311
wenzelm@18060
   312
fun merge
wenzelm@30284
   313
   (Consts {decls = decls1, constraints = constraints1, rev_abbrevs = rev_abbrevs1},
wenzelm@30284
   314
    Consts {decls = decls2, constraints = constraints2, rev_abbrevs = rev_abbrevs2}) =
wenzelm@18060
   315
  let
wenzelm@33095
   316
    val decls' = Name_Space.merge_tables (decls1, decls2);
haftmann@25037
   317
    val constraints' = Symtab.join (K fst) (constraints1, constraints2);
wenzelm@30566
   318
    val rev_abbrevs' = Symtab.join (K Item_Net.merge) (rev_abbrevs1, rev_abbrevs2);
wenzelm@24673
   319
  in make_consts (decls', constraints', rev_abbrevs') end;
wenzelm@18060
   320
wenzelm@18060
   321
end;