src/Pure/consts.ML
author wenzelm
Wed Dec 12 23:36:07 2012 +0100 (2012-12-12 ago)
changeset 50499 f496b2b7bafb
parent 50201 c26369c9eda6
child 50768 2172f82de515
permissions -rw-r--r--
rendering of selected dialog_result as active_result_color, depending on dynamic command status in output panel, but not static popups etc.;
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@43794
    16
  val the_const: T -> string -> 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@42358
    25
  val extern: Proof.context -> T -> string -> xstring
wenzelm@35255
    26
  val intern_syntax: T -> xstring -> string
wenzelm@42358
    27
  val extern_syntax: Proof.context -> T -> string -> xstring
wenzelm@42469
    28
  val read_const: T -> string * Position.T -> term
wenzelm@42383
    29
  val certify: Context.pretty -> 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@47005
    32
  val declare: Context.generic -> binding * typ -> T -> T
wenzelm@19098
    33
  val constrain: string * typ option -> T -> T
wenzelm@47005
    34
  val abbreviate: Context.generic -> Type.tsig -> string -> binding * term -> T -> (term * term) * T
wenzelm@25048
    35
  val revert_abbrev: string -> string -> T -> T
wenzelm@18060
    36
  val hide: bool -> string -> T -> T
wenzelm@18060
    37
  val empty: T
wenzelm@18060
    38
  val merge: T * T -> T
wenzelm@19364
    39
end;
wenzelm@18060
    40
wenzelm@18060
    41
structure Consts: CONSTS =
wenzelm@18060
    42
struct
wenzelm@18060
    43
wenzelm@19364
    44
(** consts type **)
wenzelm@19364
    45
wenzelm@19364
    46
(* datatype T *)
wenzelm@18060
    47
wenzelm@35255
    48
type decl = {T: typ, typargs: int list list};
wenzelm@24909
    49
type abbrev = {rhs: term, normal_rhs: term, force_expand: bool};
wenzelm@18935
    50
wenzelm@18060
    51
datatype T = Consts of
wenzelm@33095
    52
 {decls: (decl * abbrev option) Name_Space.table,
wenzelm@19364
    53
  constraints: typ Symtab.table,
wenzelm@30566
    54
  rev_abbrevs: (term * term) Item_Net.T Symtab.table};
wenzelm@18060
    55
wenzelm@30424
    56
fun eq_consts
wenzelm@30424
    57
   (Consts {decls = decls1, constraints = constraints1, rev_abbrevs = rev_abbrevs1},
wenzelm@30424
    58
    Consts {decls = decls2, constraints = constraints2, rev_abbrevs = rev_abbrevs2}) =
wenzelm@30424
    59
  pointer_eq (decls1, decls2) andalso
wenzelm@30424
    60
  pointer_eq (constraints1, constraints2) andalso
wenzelm@30424
    61
  pointer_eq (rev_abbrevs1, rev_abbrevs2);
wenzelm@30424
    62
wenzelm@24673
    63
fun make_consts (decls, constraints, rev_abbrevs) =
wenzelm@30284
    64
  Consts {decls = decls, constraints = constraints, rev_abbrevs = rev_abbrevs};
wenzelm@18060
    65
wenzelm@30284
    66
fun map_consts f (Consts {decls, constraints, rev_abbrevs}) =
wenzelm@24673
    67
  make_consts (f (decls, constraints, rev_abbrevs));
wenzelm@19364
    68
wenzelm@30566
    69
wenzelm@30566
    70
(* reverted abbrevs *)
wenzelm@30566
    71
wenzelm@33173
    72
val empty_abbrevs =
wenzelm@33373
    73
  Item_Net.init (fn ((t, u), (t', u')) => t aconv t' andalso u aconv u') (single o #1);
wenzelm@30566
    74
wenzelm@33373
    75
fun update_abbrevs mode abbrs =
wenzelm@33373
    76
  Symtab.map_default (mode, empty_abbrevs) (Item_Net.update 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@43794
    94
fun the_entry (Consts {decls = (_, tab), ...}) c =
wenzelm@43794
    95
  (case Symtab.lookup_key tab 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@30284
   123
fun space_of (Consts {decls = (space, _), ...}) = space;
wenzelm@19364
   124
wenzelm@35680
   125
fun alias naming binding name = map_consts (fn ((space, decls), constraints, rev_abbrevs) =>
wenzelm@35680
   126
  ((Name_Space.alias naming binding name space, 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@42358
   131
fun extern ctxt = Name_Space.extern ctxt o space_of;
wenzelm@19364
   132
wenzelm@35554
   133
fun intern_syntax consts s =
wenzelm@42290
   134
  (case try Lexicon.unmark_const s of
wenzelm@35255
   135
    SOME c => c
wenzelm@35554
   136
  | NONE => intern consts s);
wenzelm@35554
   137
wenzelm@42358
   138
fun extern_syntax ctxt consts s =
wenzelm@42290
   139
  (case try Lexicon.unmark_const s of
wenzelm@42358
   140
    SOME c => extern ctxt consts c
wenzelm@35554
   141
  | NONE => s);
wenzelm@21181
   142
wenzelm@18060
   143
wenzelm@19364
   144
(* read_const *)
wenzelm@19364
   145
wenzelm@42469
   146
fun read_const consts (raw_c, pos) =
wenzelm@19364
   147
  let
wenzelm@19364
   148
    val c = intern consts raw_c;
wenzelm@48992
   149
    val T = type_scheme consts c handle TYPE (msg, _, _) => error (msg ^ Position.here pos);
wenzelm@21205
   150
  in Const (c, T) end;
wenzelm@19364
   151
wenzelm@19364
   152
wenzelm@19364
   153
(* certify *)
wenzelm@19364
   154
wenzelm@24673
   155
fun certify pp tsig do_expand consts =
wenzelm@18965
   156
  let
wenzelm@18965
   157
    fun err msg (c, T) =
wenzelm@42383
   158
      raise TYPE (msg ^ " " ^ quote c ^ " :: " ^
wenzelm@42383
   159
        Syntax.string_of_typ (Syntax.init_pretty 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@43794
   172
              val (_, ({T = U, ...}, abbr)) = the_entry 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@40722
   218
    val inst = vars ~~ Ts handle ListPair.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@47005
   234
fun declare context (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@41254
   238
      val _ = Binding.check b;
wenzelm@47005
   239
      val (_, decls') = decls |> Name_Space.define context true (b, (decl, NONE));
wenzelm@33092
   240
    in (decls', constraints, rev_abbrevs) end);
wenzelm@19364
   241
wenzelm@19364
   242
wenzelm@19364
   243
(* constraints *)
wenzelm@19364
   244
wenzelm@19364
   245
fun constrain (c, C) consts =
wenzelm@24673
   246
  consts |> map_consts (fn (decls, constraints, rev_abbrevs) =>
wenzelm@43794
   247
    (#2 (the_entry consts c) handle TYPE (msg, _, _) => error msg;
wenzelm@19364
   248
      (decls,
wenzelm@19364
   249
        constraints |> (case C of SOME T => Symtab.update (c, T) | NONE => Symtab.delete_safe c),
wenzelm@24673
   250
        rev_abbrevs)));
wenzelm@18060
   251
wenzelm@18935
   252
wenzelm@18935
   253
(* abbreviations *)
wenzelm@18935
   254
wenzelm@19027
   255
local
wenzelm@19027
   256
wenzelm@30568
   257
fun strip_abss (t as Abs (x, T, b)) =
wenzelm@42083
   258
      if Term.is_dependent b then strip_abss b |>> cons (x, T)  (* FIXME decr!? *)
wenzelm@30568
   259
      else ([], t)
wenzelm@30568
   260
  | strip_abss t = ([], t);
wenzelm@19027
   261
wenzelm@21720
   262
fun rev_abbrev lhs rhs =
wenzelm@18060
   263
  let
wenzelm@30568
   264
    val (xs, body) = strip_abss (Envir.beta_eta_contract rhs);
wenzelm@30568
   265
    val vars = fold (fn (x, T) => cons (Var ((x, 0), T))) (Term.rename_wrt_term body xs) [];
wenzelm@30568
   266
  in (Term.subst_bounds (rev vars, body), Term.list_comb (lhs, vars)) end;
wenzelm@19027
   267
wenzelm@19027
   268
in
wenzelm@18965
   269
wenzelm@47005
   270
fun abbreviate context tsig mode (b, raw_rhs) consts =
wenzelm@18965
   271
  let
wenzelm@47005
   272
    val pp = Context.pretty_generic context;
wenzelm@24673
   273
    val cert_term = certify pp tsig false consts;
wenzelm@24673
   274
    val expand_term = certify pp tsig true consts;
wenzelm@37146
   275
    val force_expand = mode = Print_Mode.internal;
wenzelm@21720
   276
wenzelm@30286
   277
    val _ = Term.exists_subterm Term.is_Var raw_rhs andalso
wenzelm@42381
   278
      error ("Illegal schematic variables on rhs of abbreviation " ^ Binding.print b);
wenzelm@30286
   279
wenzelm@19364
   280
    val rhs = raw_rhs
wenzelm@20548
   281
      |> Term.map_types (Type.cert_typ tsig)
wenzelm@30286
   282
      |> cert_term
wenzelm@30286
   283
      |> Term.close_schematic_term;
wenzelm@25404
   284
    val normal_rhs = expand_term rhs;
wenzelm@21794
   285
    val T = Term.fastype_of rhs;
wenzelm@47005
   286
    val lhs = Const (Name_Space.full_name (Name_Space.naming_of context) b, T);
wenzelm@19364
   287
  in
wenzelm@24673
   288
    consts |> map_consts (fn (decls, constraints, rev_abbrevs) =>
wenzelm@19364
   289
      let
wenzelm@35255
   290
        val decl = {T = T, typargs = typargs_of T};
wenzelm@25404
   291
        val abbr = {rhs = rhs, normal_rhs = normal_rhs, force_expand = force_expand};
wenzelm@41254
   292
        val _ = Binding.check b;
haftmann@28861
   293
        val (_, decls') = decls
wenzelm@47005
   294
          |> Name_Space.define context true (b, (decl, SOME abbr));
wenzelm@19364
   295
        val rev_abbrevs' = rev_abbrevs
wenzelm@33373
   296
          |> update_abbrevs mode (rev_abbrev lhs rhs);
wenzelm@24673
   297
      in (decls', constraints, rev_abbrevs') end)
wenzelm@21794
   298
    |> pair (lhs, rhs)
wenzelm@19364
   299
  end;
wenzelm@18935
   300
wenzelm@25048
   301
fun revert_abbrev mode c consts = consts |> map_consts (fn (decls, constraints, rev_abbrevs) =>
wenzelm@25048
   302
  let
wenzelm@25048
   303
    val (T, rhs) = the_abbreviation consts c;
wenzelm@25048
   304
    val rev_abbrevs' = rev_abbrevs
wenzelm@33373
   305
      |> update_abbrevs mode (rev_abbrev (Const (c, T)) rhs);
wenzelm@25048
   306
  in (decls, constraints, rev_abbrevs') end);
wenzelm@25048
   307
wenzelm@19027
   308
end;
wenzelm@19027
   309
wenzelm@18060
   310
wenzelm@18060
   311
(* empty and merge *)
wenzelm@18060
   312
wenzelm@45666
   313
val empty =
wenzelm@50201
   314
  make_consts (Name_Space.empty_table Markup.constantN, Symtab.empty, Symtab.empty);
wenzelm@18060
   315
wenzelm@18060
   316
fun merge
wenzelm@30284
   317
   (Consts {decls = decls1, constraints = constraints1, rev_abbrevs = rev_abbrevs1},
wenzelm@30284
   318
    Consts {decls = decls2, constraints = constraints2, rev_abbrevs = rev_abbrevs2}) =
wenzelm@18060
   319
  let
wenzelm@33095
   320
    val decls' = Name_Space.merge_tables (decls1, decls2);
haftmann@25037
   321
    val constraints' = Symtab.join (K fst) (constraints1, constraints2);
wenzelm@30566
   322
    val rev_abbrevs' = Symtab.join (K Item_Net.merge) (rev_abbrevs1, rev_abbrevs2);
wenzelm@24673
   323
  in make_consts (decls', constraints', rev_abbrevs') end;
wenzelm@18060
   324
wenzelm@18060
   325
end;