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