src/Pure/consts.ML
author wenzelm
Fri Jun 13 21:04:42 2008 +0200 (2008-06-13)
changeset 27195 bbf4cbc69243
parent 26050 88bb26089ef5
child 28017 4919bd124a58
permissions -rw-r--r--
map_const: soft version, no failure here;
wenzelm@18060
     1
(*  Title:      Pure/consts.ML
wenzelm@18060
     2
    ID:         $Id$
wenzelm@18060
     3
    Author:     Makarius
wenzelm@18060
     4
wenzelm@18935
     5
Polymorphic constants: declarations, abbreviations, additional type
wenzelm@18935
     6
constraints.
wenzelm@18060
     7
*)
wenzelm@18060
     8
wenzelm@18060
     9
signature CONSTS =
wenzelm@18060
    10
sig
wenzelm@18060
    11
  type T
wenzelm@19027
    12
  val eq_consts: T * T -> bool
wenzelm@19364
    13
  val abbrevs_of: T -> string list -> (term * term) list
wenzelm@18935
    14
  val dest: T ->
wenzelm@25404
    15
   {constants: (typ * term option) NameSpace.table,
wenzelm@18935
    16
    constraints: typ NameSpace.table}
wenzelm@25048
    17
  val the_type: T -> string -> typ                             (*exception TYPE*)
wenzelm@25048
    18
  val the_abbreviation: T -> string -> typ * term              (*exception TYPE*)
wenzelm@25048
    19
  val type_scheme: T -> string -> typ                          (*exception TYPE*)
wenzelm@25048
    20
  val the_tags: T -> string -> Markup.property list            (*exception TYPE*)
wenzelm@25048
    21
  val is_monomorphic: T -> string -> bool                      (*exception TYPE*)
wenzelm@25048
    22
  val the_constraint: T -> string -> typ                       (*exception TYPE*)
wenzelm@18965
    23
  val space_of: T -> NameSpace.T
wenzelm@19364
    24
  val intern: T -> xstring -> string
wenzelm@19364
    25
  val extern: T -> string -> xstring
wenzelm@19364
    26
  val extern_early: T -> string -> xstring
wenzelm@19657
    27
  val syntax: T -> string * mixfix -> string * typ * mixfix
wenzelm@21181
    28
  val syntax_name: T -> string -> string
wenzelm@18965
    29
  val read_const: T -> string -> term
wenzelm@24673
    30
  val certify: Pretty.pp -> 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@24772
    33
  val declare: bool -> NameSpace.naming -> Markup.property list -> (bstring * typ) -> T -> T
wenzelm@19098
    34
  val constrain: string * typ option -> T -> T
wenzelm@24772
    35
  val abbreviate: Pretty.pp -> Type.tsig -> NameSpace.naming -> string -> Markup.property list ->
wenzelm@21794
    36
    bstring * term -> T -> (term * term) * T
wenzelm@25048
    37
  val revert_abbrev: string -> string -> T -> T
wenzelm@18060
    38
  val hide: bool -> string -> T -> T
wenzelm@18060
    39
  val empty: T
wenzelm@18060
    40
  val merge: T * T -> T
wenzelm@19364
    41
end;
wenzelm@18060
    42
wenzelm@18060
    43
structure Consts: CONSTS =
wenzelm@18060
    44
struct
wenzelm@18060
    45
wenzelm@19364
    46
(** consts type **)
wenzelm@19364
    47
wenzelm@19364
    48
(* datatype T *)
wenzelm@18060
    49
wenzelm@24909
    50
type decl = {T: typ, typargs: int list list, tags: Markup.property list, authentic: bool};
wenzelm@24909
    51
type abbrev = {rhs: term, normal_rhs: term, force_expand: bool};
wenzelm@18935
    52
wenzelm@18060
    53
datatype T = Consts of
wenzelm@24909
    54
 {decls: ((decl * abbrev option) * serial) NameSpace.table,
wenzelm@19364
    55
  constraints: typ Symtab.table,
wenzelm@24673
    56
  rev_abbrevs: (term * term) list Symtab.table} * stamp;
wenzelm@19027
    57
wenzelm@19027
    58
fun eq_consts (Consts (_, s1), Consts (_, s2)) = s1 = s2;
wenzelm@18060
    59
wenzelm@24673
    60
fun make_consts (decls, constraints, rev_abbrevs) =
wenzelm@24673
    61
  Consts ({decls = decls, constraints = constraints, rev_abbrevs = rev_abbrevs}, stamp ());
wenzelm@18060
    62
wenzelm@24673
    63
fun map_consts f (Consts ({decls, constraints, rev_abbrevs}, _)) =
wenzelm@24673
    64
  make_consts (f (decls, constraints, rev_abbrevs));
wenzelm@19364
    65
wenzelm@19364
    66
fun abbrevs_of (Consts ({rev_abbrevs, ...}, _)) modes =
wenzelm@19482
    67
  maps (Symtab.lookup_list rev_abbrevs) modes;
wenzelm@19364
    68
wenzelm@18965
    69
wenzelm@19364
    70
(* dest consts *)
wenzelm@19364
    71
wenzelm@19364
    72
fun dest (Consts ({decls = (space, decls), constraints, ...}, _)) =
wenzelm@19364
    73
 {constants = (space,
wenzelm@24909
    74
    Symtab.fold (fn (c, (({T, ...}, abbr), _)) =>
wenzelm@25404
    75
      Symtab.update (c, (T, Option.map #rhs abbr))) decls Symtab.empty),
wenzelm@18060
    76
  constraints = (space, constraints)};
wenzelm@18060
    77
wenzelm@18060
    78
wenzelm@18060
    79
(* lookup consts *)
wenzelm@18060
    80
wenzelm@19027
    81
fun the_const (Consts ({decls = (_, tab), ...}, _)) c =
wenzelm@19364
    82
  (case Symtab.lookup tab c of
wenzelm@24772
    83
    SOME (decl, _) => decl
wenzelm@25041
    84
  | NONE => raise TYPE ("Unknown constant: " ^ quote c, [], []));
wenzelm@18935
    85
wenzelm@25041
    86
fun the_type consts c =
wenzelm@24772
    87
  (case the_const consts c of
wenzelm@24909
    88
    ({T, ...}, NONE) => T
wenzelm@21720
    89
  | _ => raise TYPE ("Not a logical constant: " ^ quote c, [], []));
wenzelm@18060
    90
wenzelm@21720
    91
fun the_abbreviation consts c =
wenzelm@24772
    92
  (case the_const consts c of
wenzelm@25048
    93
    ({T, ...}, SOME {rhs, ...}) => (T, rhs)
wenzelm@21720
    94
  | _ => raise TYPE ("Not an abbreviated constant: " ^ quote c, [], []));
wenzelm@21720
    95
wenzelm@25041
    96
val the_decl = #1 oo the_const;
wenzelm@25041
    97
val type_scheme = #T oo the_decl;
wenzelm@25041
    98
val type_arguments = #typargs oo the_decl;
wenzelm@25041
    99
val the_tags = #tags oo the_decl;
wenzelm@25041
   100
wenzelm@21720
   101
val is_monomorphic = null oo type_arguments;
wenzelm@18935
   102
wenzelm@21720
   103
fun the_constraint (consts as Consts ({constraints, ...}, _)) c =
wenzelm@18060
   104
  (case Symtab.lookup constraints c of
wenzelm@18060
   105
    SOME T => T
wenzelm@25041
   106
  | NONE => type_scheme consts c);
wenzelm@18935
   107
wenzelm@18935
   108
wenzelm@19657
   109
(* name space and syntax *)
wenzelm@19364
   110
wenzelm@19364
   111
fun space_of (Consts ({decls = (space, _), ...}, _)) = space;
wenzelm@19364
   112
wenzelm@19364
   113
val intern = NameSpace.intern o space_of;
wenzelm@19364
   114
val extern = NameSpace.extern o space_of;
wenzelm@19364
   115
wenzelm@19364
   116
fun extern_early consts c =
wenzelm@19364
   117
  (case try (the_const consts) c of
wenzelm@24909
   118
    SOME ({authentic = true, ...}, _) => Syntax.constN ^ c
wenzelm@19576
   119
  | _ => extern consts c);
wenzelm@19364
   120
wenzelm@19657
   121
fun syntax consts (c, mx) =
wenzelm@19657
   122
  let
wenzelm@24909
   123
    val ({T, authentic, ...}, _) = the_const consts c handle TYPE (msg, _, _) => error msg;
wenzelm@19677
   124
    val c' = if authentic then Syntax.constN ^ c else NameSpace.base c;
wenzelm@19657
   125
  in (c', T, mx) end;
wenzelm@19657
   126
wenzelm@21181
   127
fun syntax_name consts c = #1 (syntax consts (c, NoSyn));
wenzelm@21181
   128
wenzelm@18060
   129
wenzelm@19364
   130
(* read_const *)
wenzelm@19364
   131
wenzelm@19364
   132
fun read_const consts raw_c =
wenzelm@19364
   133
  let
wenzelm@19364
   134
    val c = intern consts raw_c;
wenzelm@25041
   135
    val T = type_scheme consts c handle TYPE (msg, _, _) => error msg;
wenzelm@21205
   136
  in Const (c, T) end;
wenzelm@19364
   137
wenzelm@19364
   138
wenzelm@19364
   139
(* certify *)
wenzelm@19364
   140
wenzelm@24673
   141
fun certify pp tsig do_expand consts =
wenzelm@18965
   142
  let
wenzelm@18965
   143
    fun err msg (c, T) =
wenzelm@18965
   144
      raise TYPE (msg ^ " " ^ quote c ^ " :: " ^ Pretty.string_of_typ pp T, [], []);
wenzelm@24673
   145
    val certT = Type.cert_typ tsig;
wenzelm@18965
   146
    fun cert tm =
wenzelm@18965
   147
      let
wenzelm@18965
   148
        val (head, args) = Term.strip_comb tm;
wenzelm@21694
   149
        val args' = map cert args;
wenzelm@21694
   150
        fun comb head' = Term.list_comb (head', args');
wenzelm@18965
   151
      in
wenzelm@18965
   152
        (case head of
wenzelm@24673
   153
          Abs (x, T, t) => comb (Abs (x, certT T, cert t))
wenzelm@19364
   154
        | Const (c, T) =>
wenzelm@19364
   155
            let
wenzelm@24673
   156
              val T' = certT T;
wenzelm@24909
   157
              val ({T = U, ...}, abbr) = the_const consts c;
wenzelm@25048
   158
              fun expand u =
wenzelm@25048
   159
                Term.betapplys (Envir.expand_atom T' (U, u) handle TYPE _ =>
wenzelm@25048
   160
                  err "Illegal type for abbreviation" (c, T), args');
wenzelm@19364
   161
            in
wenzelm@19433
   162
              if not (Type.raw_instance (T', U)) then
wenzelm@19364
   163
                err "Illegal type for constant" (c, T)
wenzelm@19364
   164
              else
wenzelm@24909
   165
                (case abbr of
wenzelm@25048
   166
                  SOME {rhs, normal_rhs, force_expand} =>
wenzelm@25048
   167
                    if do_expand then expand normal_rhs
wenzelm@25048
   168
                    else if force_expand then expand rhs
wenzelm@21720
   169
                    else comb head
wenzelm@19364
   170
                | _ => comb head)
wenzelm@19364
   171
            end
wenzelm@19364
   172
        | _ => comb head)
wenzelm@18965
   173
      end;
wenzelm@18965
   174
  in cert end;
wenzelm@18965
   175
wenzelm@18965
   176
wenzelm@18060
   177
(* typargs -- view actual const type as instance of declaration *)
wenzelm@18060
   178
wenzelm@24909
   179
local
wenzelm@24909
   180
wenzelm@24909
   181
fun args_of (Type (_, Ts)) pos = args_of_list Ts 0 pos
wenzelm@24909
   182
  | args_of (TVar v) pos = insert (eq_fst op =) (v, rev pos)
wenzelm@24909
   183
  | args_of (TFree _) _ = I
wenzelm@24909
   184
and args_of_list (T :: Ts) i is = args_of T (i :: is) #> args_of_list Ts (i + 1) is
wenzelm@24909
   185
  | args_of_list [] _ _ = I;
wenzelm@24909
   186
wenzelm@19364
   187
fun subscript (Type (_, Ts)) (i :: is) = subscript (nth Ts i) is
wenzelm@19364
   188
  | subscript T [] = T
wenzelm@19364
   189
  | subscript T _ = raise Subscript;
wenzelm@18060
   190
wenzelm@24909
   191
in
wenzelm@24909
   192
wenzelm@24909
   193
fun typargs_of T = map #2 (rev (args_of T [] []));
wenzelm@24909
   194
wenzelm@19364
   195
fun typargs consts (c, T) = map (subscript T) (type_arguments consts c);
wenzelm@18060
   196
wenzelm@24909
   197
end;
wenzelm@24909
   198
wenzelm@18163
   199
fun instance consts (c, Ts) =
wenzelm@18163
   200
  let
wenzelm@25041
   201
    val declT = type_scheme consts c;
wenzelm@18163
   202
    val vars = map Term.dest_TVar (typargs consts (c, declT));
wenzelm@24673
   203
    val inst = vars ~~ Ts handle UnequalLengths =>
wenzelm@24673
   204
      raise TYPE ("Consts.instance", Ts, [Const (c, dummyT)]);
wenzelm@24673
   205
  in declT |> TermSubst.instantiateT inst end;
wenzelm@18163
   206
wenzelm@18060
   207
wenzelm@18060
   208
wenzelm@19364
   209
(** build consts **)
wenzelm@18060
   210
wenzelm@23655
   211
fun err_dup_const c =
wenzelm@23655
   212
  error ("Duplicate declaration of constant " ^ quote c);
wenzelm@18060
   213
haftmann@23086
   214
fun extend_decls naming decl tab = NameSpace.extend_table naming [decl] tab
wenzelm@23655
   215
  handle Symtab.DUP c => err_dup_const c;
wenzelm@18060
   216
wenzelm@18935
   217
wenzelm@18935
   218
(* name space *)
wenzelm@18060
   219
wenzelm@24673
   220
fun hide fully c = map_consts (fn (decls, constraints, rev_abbrevs) =>
wenzelm@24673
   221
  (apfst (NameSpace.hide fully c) decls, constraints, rev_abbrevs));
wenzelm@18935
   222
wenzelm@18935
   223
wenzelm@18935
   224
(* declarations *)
wenzelm@18935
   225
wenzelm@24772
   226
fun declare authentic naming tags (c, declT) = map_consts (fn (decls, constraints, rev_abbrevs) =>
wenzelm@18060
   227
  let
wenzelm@26050
   228
    val tags' = tags |> Position.default_properties (Position.thread_data ());
wenzelm@26050
   229
    val decl = {T = declT, typargs = typargs_of declT, tags = tags', authentic = authentic};
wenzelm@24909
   230
    val decls' = decls |> extend_decls naming (c, ((decl, NONE), serial ()));
wenzelm@24772
   231
  in (decls', constraints, rev_abbrevs) end);
wenzelm@19364
   232
wenzelm@19364
   233
wenzelm@19364
   234
(* constraints *)
wenzelm@19364
   235
wenzelm@19364
   236
fun constrain (c, C) consts =
wenzelm@24673
   237
  consts |> map_consts (fn (decls, constraints, rev_abbrevs) =>
wenzelm@19364
   238
    (the_const consts c handle TYPE (msg, _, _) => error msg;
wenzelm@19364
   239
      (decls,
wenzelm@19364
   240
        constraints |> (case C of SOME T => Symtab.update (c, T) | NONE => Symtab.delete_safe c),
wenzelm@24673
   241
        rev_abbrevs)));
wenzelm@18060
   242
wenzelm@18935
   243
wenzelm@18935
   244
(* abbreviations *)
wenzelm@18935
   245
wenzelm@19027
   246
local
wenzelm@19027
   247
wenzelm@19364
   248
fun strip_abss tm = ([], tm) ::
wenzelm@19364
   249
  (case tm of
wenzelm@19364
   250
    Abs (a, T, t) =>
wenzelm@19364
   251
      if Term.loose_bvar1 (t, 0) then
wenzelm@19364
   252
        strip_abss t |> map (fn (xs, b) => ((a, T) :: xs, b))
wenzelm@19364
   253
      else []
wenzelm@19364
   254
  | _ => []);
wenzelm@19027
   255
wenzelm@21720
   256
fun rev_abbrev lhs rhs =
wenzelm@18060
   257
  let
wenzelm@19027
   258
    fun abbrev (xs, body) =
wenzelm@19027
   259
      let val vars = fold (fn (x, T) => cons (Var ((x, 0), T))) (Term.rename_wrt_term body xs) []
wenzelm@21720
   260
      in (Term.subst_bounds (rev vars, body), Term.list_comb (lhs, vars)) end;
wenzelm@19502
   261
  in map abbrev (strip_abss (Envir.beta_eta_contract rhs)) end;
wenzelm@19027
   262
wenzelm@19027
   263
in
wenzelm@18965
   264
wenzelm@24772
   265
fun abbreviate pp tsig naming mode tags (c, raw_rhs) consts =
wenzelm@18965
   266
  let
wenzelm@24673
   267
    val cert_term = certify pp tsig false consts;
wenzelm@24673
   268
    val expand_term = certify pp tsig true consts;
wenzelm@25116
   269
    val force_expand = mode = PrintMode.internal;
wenzelm@21720
   270
wenzelm@19364
   271
    val rhs = raw_rhs
wenzelm@20548
   272
      |> Term.map_types (Type.cert_typ tsig)
wenzelm@21720
   273
      |> cert_term;
wenzelm@25404
   274
    val normal_rhs = expand_term rhs;
wenzelm@21794
   275
    val T = Term.fastype_of rhs;
wenzelm@21794
   276
    val lhs = Const (NameSpace.full naming c, T);
wenzelm@21720
   277
wenzelm@21680
   278
    fun err msg = error (msg ^ " on rhs of abbreviation:\n" ^
wenzelm@21720
   279
      Pretty.string_of_term pp (Logic.mk_equals (lhs, rhs)));
wenzelm@25048
   280
    val _ = Term.exists_subterm Term.is_Var rhs andalso err "Illegal schematic variables";
wenzelm@25048
   281
    val _ = null (Term.hidden_polymorphism rhs) orelse err "Extra type variables";
wenzelm@19364
   282
  in
wenzelm@24673
   283
    consts |> map_consts (fn (decls, constraints, rev_abbrevs) =>
wenzelm@19364
   284
      let
wenzelm@26050
   285
        val tags' = tags |> Position.default_properties (Position.thread_data ());
wenzelm@26050
   286
        val decl = {T = T, typargs = typargs_of T, tags = tags', authentic = true};
wenzelm@25404
   287
        val abbr = {rhs = rhs, normal_rhs = normal_rhs, force_expand = force_expand};
wenzelm@24772
   288
        val decls' = decls
wenzelm@24909
   289
          |> extend_decls naming (c, ((decl, SOME abbr), serial ()));
wenzelm@19364
   290
        val rev_abbrevs' = rev_abbrevs
wenzelm@25389
   291
          |> fold (curry Symtab.cons_list mode) (rev_abbrev lhs rhs);
wenzelm@24673
   292
      in (decls', constraints, rev_abbrevs') end)
wenzelm@21794
   293
    |> pair (lhs, rhs)
wenzelm@19364
   294
  end;
wenzelm@18935
   295
wenzelm@25048
   296
fun revert_abbrev mode c consts = consts |> map_consts (fn (decls, constraints, rev_abbrevs) =>
wenzelm@25048
   297
  let
wenzelm@25048
   298
    val (T, rhs) = the_abbreviation consts c;
wenzelm@25048
   299
    val rev_abbrevs' = rev_abbrevs
wenzelm@25389
   300
      |> fold (curry Symtab.cons_list mode) (rev_abbrev (Const (c, T)) rhs);
wenzelm@25048
   301
  in (decls, constraints, rev_abbrevs') end);
wenzelm@25048
   302
wenzelm@19027
   303
end;
wenzelm@19027
   304
wenzelm@18060
   305
wenzelm@18060
   306
(* empty and merge *)
wenzelm@18060
   307
wenzelm@24673
   308
val empty = make_consts (NameSpace.empty_table, Symtab.empty, Symtab.empty);
wenzelm@18060
   309
wenzelm@18060
   310
fun merge
wenzelm@24673
   311
   (Consts ({decls = decls1, constraints = constraints1, rev_abbrevs = rev_abbrevs1}, _),
wenzelm@24673
   312
    Consts ({decls = decls2, constraints = constraints2, rev_abbrevs = rev_abbrevs2}, _)) =
wenzelm@18060
   313
  let
wenzelm@18935
   314
    val decls' = NameSpace.merge_tables (eq_snd (op =)) (decls1, decls2)
wenzelm@23655
   315
      handle Symtab.DUP c => err_dup_const c;
haftmann@25037
   316
    val constraints' = Symtab.join (K fst) (constraints1, constraints2);
wenzelm@19364
   317
    val rev_abbrevs' = (rev_abbrevs1, rev_abbrevs2) |> Symtab.join
wenzelm@19364
   318
      (K (Library.merge (fn ((t, u), (t', u')) => t aconv t' andalso u aconv u')));
wenzelm@24673
   319
  in make_consts (decls', constraints', rev_abbrevs') end;
wenzelm@18060
   320
wenzelm@18060
   321
end;