src/Pure/consts.ML
author wenzelm
Fri May 28 20:41:23 2010 +0200 (2010-05-28)
changeset 37174 6feaab4fc27d
parent 37146 f652333bbf8e
child 40722 441260986b63
permissions -rw-r--r--
assume given SCALA_HOME, e.g. from component settings or external setup;
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@35680
    22
  val alias: Name_Space.naming -> binding -> string -> T -> T
wenzelm@33158
    23
  val is_concealed: T -> string -> bool
wenzelm@19364
    24
  val intern: T -> xstring -> string
wenzelm@35554
    25
  val extern: T -> string -> xstring
wenzelm@35255
    26
  val intern_syntax: T -> xstring -> string
wenzelm@35554
    27
  val extern_syntax: T -> string -> xstring
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@35255
    32
  val declare: Name_Space.naming -> binding * typ -> T -> T
wenzelm@19098
    33
  val constrain: string * typ option -> T -> T
wenzelm@33173
    34
  val abbreviate: Pretty.pp -> Type.tsig -> Name_Space.naming -> string ->
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@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@30566
    70
wenzelm@30566
    71
(* reverted abbrevs *)
wenzelm@30566
    72
wenzelm@33173
    73
val empty_abbrevs =
wenzelm@33373
    74
  Item_Net.init (fn ((t, u), (t', u')) => t aconv t' andalso u aconv u') (single o #1);
wenzelm@30566
    75
wenzelm@33373
    76
fun update_abbrevs mode abbrs =
wenzelm@33373
    77
  Symtab.map_default (mode, empty_abbrevs) (Item_Net.update abbrs);
wenzelm@30566
    78
wenzelm@30566
    79
fun retrieve_abbrevs (Consts {rev_abbrevs, ...}) modes =
wenzelm@30566
    80
  let val nets = map_filter (Symtab.lookup rev_abbrevs) modes
wenzelm@30566
    81
  in fn t => maps (fn net => Item_Net.retrieve net t) nets end;
wenzelm@19364
    82
wenzelm@18965
    83
wenzelm@19364
    84
(* dest consts *)
wenzelm@19364
    85
wenzelm@30284
    86
fun dest (Consts {decls = (space, decls), constraints, ...}) =
wenzelm@19364
    87
 {constants = (space,
wenzelm@33092
    88
    Symtab.fold (fn (c, ({T, ...}, abbr)) =>
wenzelm@25404
    89
      Symtab.update (c, (T, Option.map #rhs abbr))) decls Symtab.empty),
wenzelm@18060
    90
  constraints = (space, constraints)};
wenzelm@18060
    91
wenzelm@18060
    92
wenzelm@18060
    93
(* lookup consts *)
wenzelm@18060
    94
wenzelm@30284
    95
fun the_const (Consts {decls = (_, tab), ...}) c =
wenzelm@19364
    96
  (case Symtab.lookup tab c of
wenzelm@33092
    97
    SOME decl => decl
wenzelm@25041
    98
  | NONE => raise TYPE ("Unknown constant: " ^ quote c, [], []));
wenzelm@18935
    99
wenzelm@25041
   100
fun the_type consts c =
wenzelm@24772
   101
  (case the_const consts c of
wenzelm@24909
   102
    ({T, ...}, NONE) => T
wenzelm@21720
   103
  | _ => raise TYPE ("Not a logical constant: " ^ quote c, [], []));
wenzelm@18060
   104
wenzelm@21720
   105
fun the_abbreviation consts c =
wenzelm@24772
   106
  (case the_const consts c of
wenzelm@25048
   107
    ({T, ...}, SOME {rhs, ...}) => (T, rhs)
wenzelm@21720
   108
  | _ => raise TYPE ("Not an abbreviated constant: " ^ quote c, [], []));
wenzelm@21720
   109
wenzelm@25041
   110
val the_decl = #1 oo the_const;
wenzelm@25041
   111
val type_scheme = #T oo the_decl;
wenzelm@25041
   112
val type_arguments = #typargs 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@35680
   126
fun alias naming binding name = map_consts (fn ((space, decls), constraints, rev_abbrevs) =>
wenzelm@35680
   127
  ((Name_Space.alias naming binding name space, decls), constraints, rev_abbrevs));
wenzelm@35680
   128
wenzelm@33158
   129
val is_concealed = Name_Space.is_concealed o space_of;
wenzelm@33158
   130
wenzelm@33095
   131
val intern = Name_Space.intern o space_of;
wenzelm@33095
   132
val extern = Name_Space.extern o space_of;
wenzelm@19364
   133
wenzelm@35554
   134
fun intern_syntax consts s =
wenzelm@35554
   135
  (case try Syntax.unmark_const s of
wenzelm@35255
   136
    SOME c => c
wenzelm@35554
   137
  | NONE => intern consts s);
wenzelm@35554
   138
wenzelm@35554
   139
fun extern_syntax consts s =
wenzelm@35554
   140
  (case try Syntax.unmark_const s of
wenzelm@35554
   141
    SOME c => extern consts c
wenzelm@35554
   142
  | NONE => s);
wenzelm@21181
   143
wenzelm@18060
   144
wenzelm@19364
   145
(* read_const *)
wenzelm@19364
   146
wenzelm@19364
   147
fun read_const consts raw_c =
wenzelm@19364
   148
  let
wenzelm@19364
   149
    val c = intern consts raw_c;
wenzelm@25041
   150
    val T = type_scheme consts c handle TYPE (msg, _, _) => error msg;
wenzelm@21205
   151
  in Const (c, T) end;
wenzelm@19364
   152
wenzelm@19364
   153
wenzelm@19364
   154
(* certify *)
wenzelm@19364
   155
wenzelm@24673
   156
fun certify pp tsig do_expand consts =
wenzelm@18965
   157
  let
wenzelm@18965
   158
    fun err msg (c, T) =
wenzelm@18965
   159
      raise TYPE (msg ^ " " ^ quote c ^ " :: " ^ Pretty.string_of_typ pp T, [], []);
wenzelm@24673
   160
    val certT = Type.cert_typ tsig;
wenzelm@18965
   161
    fun cert tm =
wenzelm@18965
   162
      let
wenzelm@18965
   163
        val (head, args) = Term.strip_comb tm;
wenzelm@21694
   164
        val args' = map cert args;
wenzelm@21694
   165
        fun comb head' = Term.list_comb (head', args');
wenzelm@18965
   166
      in
wenzelm@18965
   167
        (case head of
wenzelm@24673
   168
          Abs (x, T, t) => comb (Abs (x, certT T, cert t))
wenzelm@19364
   169
        | Const (c, T) =>
wenzelm@19364
   170
            let
wenzelm@24673
   171
              val T' = certT T;
wenzelm@24909
   172
              val ({T = U, ...}, abbr) = the_const consts c;
wenzelm@25048
   173
              fun expand u =
wenzelm@25048
   174
                Term.betapplys (Envir.expand_atom T' (U, u) handle TYPE _ =>
wenzelm@25048
   175
                  err "Illegal type for abbreviation" (c, T), args');
wenzelm@19364
   176
            in
wenzelm@19433
   177
              if not (Type.raw_instance (T', U)) then
wenzelm@19364
   178
                err "Illegal type for constant" (c, T)
wenzelm@19364
   179
              else
wenzelm@24909
   180
                (case abbr of
wenzelm@25048
   181
                  SOME {rhs, normal_rhs, force_expand} =>
wenzelm@25048
   182
                    if do_expand then expand normal_rhs
wenzelm@25048
   183
                    else if force_expand then expand rhs
wenzelm@21720
   184
                    else comb head
wenzelm@19364
   185
                | _ => comb head)
wenzelm@19364
   186
            end
wenzelm@19364
   187
        | _ => comb head)
wenzelm@18965
   188
      end;
wenzelm@18965
   189
  in cert end;
wenzelm@18965
   190
wenzelm@18965
   191
wenzelm@18060
   192
(* typargs -- view actual const type as instance of declaration *)
wenzelm@18060
   193
wenzelm@24909
   194
local
wenzelm@24909
   195
wenzelm@24909
   196
fun args_of (Type (_, Ts)) pos = args_of_list Ts 0 pos
wenzelm@24909
   197
  | args_of (TVar v) pos = insert (eq_fst op =) (v, rev pos)
wenzelm@24909
   198
  | args_of (TFree _) _ = I
wenzelm@24909
   199
and args_of_list (T :: Ts) i is = args_of T (i :: is) #> args_of_list Ts (i + 1) is
wenzelm@24909
   200
  | args_of_list [] _ _ = I;
wenzelm@24909
   201
wenzelm@19364
   202
fun subscript (Type (_, Ts)) (i :: is) = subscript (nth Ts i) is
wenzelm@19364
   203
  | subscript T [] = T
wenzelm@32784
   204
  | subscript _ _ = raise Subscript;
wenzelm@18060
   205
wenzelm@24909
   206
in
wenzelm@24909
   207
wenzelm@24909
   208
fun typargs_of T = map #2 (rev (args_of T [] []));
wenzelm@24909
   209
wenzelm@19364
   210
fun typargs consts (c, T) = map (subscript T) (type_arguments consts c);
wenzelm@18060
   211
wenzelm@24909
   212
end;
wenzelm@24909
   213
wenzelm@18163
   214
fun instance consts (c, Ts) =
wenzelm@18163
   215
  let
wenzelm@25041
   216
    val declT = type_scheme consts c;
wenzelm@18163
   217
    val vars = map Term.dest_TVar (typargs consts (c, declT));
wenzelm@24673
   218
    val inst = vars ~~ Ts handle UnequalLengths =>
wenzelm@24673
   219
      raise TYPE ("Consts.instance", Ts, [Const (c, dummyT)]);
wenzelm@31977
   220
  in declT |> Term_Subst.instantiateT inst end;
wenzelm@18163
   221
wenzelm@18060
   222
wenzelm@18060
   223
wenzelm@19364
   224
(** build consts **)
wenzelm@18060
   225
wenzelm@18935
   226
(* name space *)
wenzelm@18060
   227
wenzelm@24673
   228
fun hide fully c = map_consts (fn (decls, constraints, rev_abbrevs) =>
wenzelm@33095
   229
  (apfst (Name_Space.hide fully c) decls, constraints, rev_abbrevs));
wenzelm@18935
   230
wenzelm@18935
   231
wenzelm@18935
   232
(* declarations *)
wenzelm@18935
   233
wenzelm@35255
   234
fun declare naming (b, declT) =
wenzelm@33092
   235
  map_consts (fn (decls, constraints, rev_abbrevs) =>
wenzelm@33092
   236
    let
wenzelm@35255
   237
      val decl = {T = declT, typargs = typargs_of declT};
wenzelm@33095
   238
      val (_, decls') = decls |> Name_Space.define true naming (b, (decl, NONE));
wenzelm@33092
   239
    in (decls', constraints, rev_abbrevs) end);
wenzelm@19364
   240
wenzelm@19364
   241
wenzelm@19364
   242
(* constraints *)
wenzelm@19364
   243
wenzelm@19364
   244
fun constrain (c, C) consts =
wenzelm@24673
   245
  consts |> map_consts (fn (decls, constraints, rev_abbrevs) =>
wenzelm@19364
   246
    (the_const consts c handle TYPE (msg, _, _) => error msg;
wenzelm@19364
   247
      (decls,
wenzelm@19364
   248
        constraints |> (case C of SOME T => Symtab.update (c, T) | NONE => Symtab.delete_safe c),
wenzelm@24673
   249
        rev_abbrevs)));
wenzelm@18060
   250
wenzelm@18935
   251
wenzelm@18935
   252
(* abbreviations *)
wenzelm@18935
   253
wenzelm@19027
   254
local
wenzelm@19027
   255
wenzelm@30568
   256
fun strip_abss (t as Abs (x, T, b)) =
wenzelm@30568
   257
      if Term.loose_bvar1 (b, 0) then strip_abss b |>> cons (x, T)
wenzelm@30568
   258
      else ([], t)
wenzelm@30568
   259
  | strip_abss t = ([], t);
wenzelm@19027
   260
wenzelm@21720
   261
fun rev_abbrev lhs rhs =
wenzelm@18060
   262
  let
wenzelm@30568
   263
    val (xs, body) = strip_abss (Envir.beta_eta_contract rhs);
wenzelm@30568
   264
    val vars = fold (fn (x, T) => cons (Var ((x, 0), T))) (Term.rename_wrt_term body xs) [];
wenzelm@30568
   265
  in (Term.subst_bounds (rev vars, body), Term.list_comb (lhs, vars)) end;
wenzelm@19027
   266
wenzelm@19027
   267
in
wenzelm@18965
   268
wenzelm@33173
   269
fun abbreviate pp tsig naming mode (b, raw_rhs) consts =
wenzelm@18965
   270
  let
wenzelm@24673
   271
    val cert_term = certify pp tsig false consts;
wenzelm@24673
   272
    val expand_term = certify pp tsig true consts;
wenzelm@37146
   273
    val force_expand = mode = Print_Mode.internal;
wenzelm@21720
   274
wenzelm@30286
   275
    val _ = Term.exists_subterm Term.is_Var raw_rhs andalso
wenzelm@33092
   276
      error ("Illegal schematic variables on rhs of abbreviation " ^ quote (Binding.str_of b));
wenzelm@30286
   277
wenzelm@19364
   278
    val rhs = raw_rhs
wenzelm@20548
   279
      |> Term.map_types (Type.cert_typ tsig)
wenzelm@30286
   280
      |> cert_term
wenzelm@30286
   281
      |> Term.close_schematic_term;
wenzelm@25404
   282
    val normal_rhs = expand_term rhs;
wenzelm@21794
   283
    val T = Term.fastype_of rhs;
wenzelm@33095
   284
    val lhs = Const (Name_Space.full_name naming b, T);
wenzelm@19364
   285
  in
wenzelm@24673
   286
    consts |> map_consts (fn (decls, constraints, rev_abbrevs) =>
wenzelm@19364
   287
      let
wenzelm@35255
   288
        val decl = {T = T, typargs = typargs_of T};
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@33373
   293
          |> update_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@33373
   302
      |> update_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@33096
   310
val empty = make_consts (Name_Space.empty_table "constant", 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;