src/Pure/consts.ML
author wenzelm
Tue Oct 16 17:06:09 2007 +0200 (2007-10-16 ago)
changeset 25048 5a94a87af697
parent 25041 c1efae25ee76
child 25116 31551aae280f
permissions -rw-r--r--
the_abbreviation: return plain rhs only;
force_expand: expand to plain rhs;
added revert_abbrev;
tuned;
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@21720
    15
   {constants: (typ * (term * 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@19364
    51
wenzelm@24909
    52
type abbrev = {rhs: term, normal_rhs: term, force_expand: bool};
wenzelm@24909
    53
fun dest_abbrev ({rhs, normal_rhs, ...}: abbrev) = (rhs, normal_rhs);
wenzelm@18935
    54
wenzelm@18060
    55
datatype T = Consts of
wenzelm@24909
    56
 {decls: ((decl * abbrev option) * serial) NameSpace.table,
wenzelm@19364
    57
  constraints: typ Symtab.table,
wenzelm@24673
    58
  rev_abbrevs: (term * term) list Symtab.table} * stamp;
wenzelm@19027
    59
wenzelm@19027
    60
fun eq_consts (Consts (_, s1), Consts (_, s2)) = s1 = s2;
wenzelm@18060
    61
wenzelm@24673
    62
fun make_consts (decls, constraints, rev_abbrevs) =
wenzelm@24673
    63
  Consts ({decls = decls, constraints = constraints, rev_abbrevs = rev_abbrevs}, stamp ());
wenzelm@18060
    64
wenzelm@24673
    65
fun map_consts f (Consts ({decls, constraints, rev_abbrevs}, _)) =
wenzelm@24673
    66
  make_consts (f (decls, constraints, rev_abbrevs));
wenzelm@19364
    67
wenzelm@19364
    68
fun abbrevs_of (Consts ({rev_abbrevs, ...}, _)) modes =
wenzelm@19482
    69
  maps (Symtab.lookup_list rev_abbrevs) modes;
wenzelm@19364
    70
wenzelm@18965
    71
wenzelm@19364
    72
(* dest consts *)
wenzelm@19364
    73
wenzelm@19364
    74
fun dest (Consts ({decls = (space, decls), constraints, ...}, _)) =
wenzelm@19364
    75
 {constants = (space,
wenzelm@24909
    76
    Symtab.fold (fn (c, (({T, ...}, abbr), _)) =>
wenzelm@24909
    77
      Symtab.update (c, (T, Option.map dest_abbrev abbr))) decls Symtab.empty),
wenzelm@18060
    78
  constraints = (space, constraints)};
wenzelm@18060
    79
wenzelm@18060
    80
wenzelm@18060
    81
(* lookup consts *)
wenzelm@18060
    82
wenzelm@19027
    83
fun the_const (Consts ({decls = (_, tab), ...}, _)) c =
wenzelm@19364
    84
  (case Symtab.lookup tab c of
wenzelm@24772
    85
    SOME (decl, _) => decl
wenzelm@25041
    86
  | NONE => raise TYPE ("Unknown constant: " ^ quote c, [], []));
wenzelm@18935
    87
wenzelm@25041
    88
fun the_type consts c =
wenzelm@24772
    89
  (case the_const consts c of
wenzelm@24909
    90
    ({T, ...}, NONE) => T
wenzelm@21720
    91
  | _ => raise TYPE ("Not a logical constant: " ^ quote c, [], []));
wenzelm@18060
    92
wenzelm@21720
    93
fun the_abbreviation consts c =
wenzelm@24772
    94
  (case the_const consts c of
wenzelm@25048
    95
    ({T, ...}, SOME {rhs, ...}) => (T, rhs)
wenzelm@21720
    96
  | _ => raise TYPE ("Not an abbreviated constant: " ^ quote c, [], []));
wenzelm@21720
    97
wenzelm@25041
    98
val the_decl = #1 oo the_const;
wenzelm@25041
    99
val type_scheme = #T oo the_decl;
wenzelm@25041
   100
val type_arguments = #typargs oo the_decl;
wenzelm@25041
   101
val the_tags = #tags oo the_decl;
wenzelm@25041
   102
wenzelm@21720
   103
val is_monomorphic = null oo type_arguments;
wenzelm@18935
   104
wenzelm@21720
   105
fun the_constraint (consts as Consts ({constraints, ...}, _)) c =
wenzelm@18060
   106
  (case Symtab.lookup constraints c of
wenzelm@18060
   107
    SOME T => T
wenzelm@25041
   108
  | NONE => type_scheme consts c);
wenzelm@18935
   109
wenzelm@18935
   110
wenzelm@19657
   111
(* name space and syntax *)
wenzelm@19364
   112
wenzelm@19364
   113
fun space_of (Consts ({decls = (space, _), ...}, _)) = space;
wenzelm@19364
   114
wenzelm@19364
   115
val intern = NameSpace.intern o space_of;
wenzelm@19364
   116
val extern = NameSpace.extern o space_of;
wenzelm@19364
   117
wenzelm@19364
   118
fun extern_early consts c =
wenzelm@19364
   119
  (case try (the_const consts) c of
wenzelm@24909
   120
    SOME ({authentic = true, ...}, _) => Syntax.constN ^ c
wenzelm@19576
   121
  | _ => extern consts c);
wenzelm@19364
   122
wenzelm@19657
   123
fun syntax consts (c, mx) =
wenzelm@19657
   124
  let
wenzelm@24909
   125
    val ({T, authentic, ...}, _) = the_const consts c handle TYPE (msg, _, _) => error msg;
wenzelm@19677
   126
    val c' = if authentic then Syntax.constN ^ c else NameSpace.base c;
wenzelm@19657
   127
  in (c', T, mx) end;
wenzelm@19657
   128
wenzelm@21181
   129
fun syntax_name consts c = #1 (syntax consts (c, NoSyn));
wenzelm@21181
   130
wenzelm@18060
   131
wenzelm@19364
   132
(* read_const *)
wenzelm@19364
   133
wenzelm@19364
   134
fun read_const consts raw_c =
wenzelm@19364
   135
  let
wenzelm@19364
   136
    val c = intern consts raw_c;
wenzelm@25041
   137
    val T = type_scheme consts c handle TYPE (msg, _, _) => error msg;
wenzelm@21205
   138
  in Const (c, T) end;
wenzelm@19364
   139
wenzelm@19364
   140
wenzelm@19364
   141
(* certify *)
wenzelm@19364
   142
wenzelm@24673
   143
fun certify pp tsig do_expand consts =
wenzelm@18965
   144
  let
wenzelm@18965
   145
    fun err msg (c, T) =
wenzelm@18965
   146
      raise TYPE (msg ^ " " ^ quote c ^ " :: " ^ Pretty.string_of_typ pp T, [], []);
wenzelm@24673
   147
    val certT = Type.cert_typ tsig;
wenzelm@18965
   148
    fun cert tm =
wenzelm@18965
   149
      let
wenzelm@18965
   150
        val (head, args) = Term.strip_comb tm;
wenzelm@21694
   151
        val args' = map cert args;
wenzelm@21694
   152
        fun comb head' = Term.list_comb (head', args');
wenzelm@18965
   153
      in
wenzelm@18965
   154
        (case head of
wenzelm@24673
   155
          Abs (x, T, t) => comb (Abs (x, certT T, cert t))
wenzelm@19364
   156
        | Const (c, T) =>
wenzelm@19364
   157
            let
wenzelm@24673
   158
              val T' = certT T;
wenzelm@24909
   159
              val ({T = U, ...}, abbr) = the_const consts c;
wenzelm@25048
   160
              fun expand u =
wenzelm@25048
   161
                Term.betapplys (Envir.expand_atom T' (U, u) handle TYPE _ =>
wenzelm@25048
   162
                  err "Illegal type for abbreviation" (c, T), args');
wenzelm@19364
   163
            in
wenzelm@19433
   164
              if not (Type.raw_instance (T', U)) then
wenzelm@19364
   165
                err "Illegal type for constant" (c, T)
wenzelm@19364
   166
              else
wenzelm@24909
   167
                (case abbr of
wenzelm@25048
   168
                  SOME {rhs, normal_rhs, force_expand} =>
wenzelm@25048
   169
                    if do_expand then expand normal_rhs
wenzelm@25048
   170
                    else if force_expand then expand rhs
wenzelm@21720
   171
                    else comb head
wenzelm@19364
   172
                | _ => comb head)
wenzelm@19364
   173
            end
wenzelm@19364
   174
        | _ => comb head)
wenzelm@18965
   175
      end;
wenzelm@18965
   176
  in cert end;
wenzelm@18965
   177
wenzelm@18965
   178
wenzelm@18060
   179
(* typargs -- view actual const type as instance of declaration *)
wenzelm@18060
   180
wenzelm@24909
   181
local
wenzelm@24909
   182
wenzelm@24909
   183
fun args_of (Type (_, Ts)) pos = args_of_list Ts 0 pos
wenzelm@24909
   184
  | args_of (TVar v) pos = insert (eq_fst op =) (v, rev pos)
wenzelm@24909
   185
  | args_of (TFree _) _ = I
wenzelm@24909
   186
and args_of_list (T :: Ts) i is = args_of T (i :: is) #> args_of_list Ts (i + 1) is
wenzelm@24909
   187
  | args_of_list [] _ _ = I;
wenzelm@24909
   188
wenzelm@19364
   189
fun subscript (Type (_, Ts)) (i :: is) = subscript (nth Ts i) is
wenzelm@19364
   190
  | subscript T [] = T
wenzelm@19364
   191
  | subscript T _ = raise Subscript;
wenzelm@18060
   192
wenzelm@24909
   193
in
wenzelm@24909
   194
wenzelm@24909
   195
fun typargs_of T = map #2 (rev (args_of T [] []));
wenzelm@24909
   196
wenzelm@19364
   197
fun typargs consts (c, T) = map (subscript T) (type_arguments consts c);
wenzelm@18060
   198
wenzelm@24909
   199
end;
wenzelm@24909
   200
wenzelm@18163
   201
fun instance consts (c, Ts) =
wenzelm@18163
   202
  let
wenzelm@25041
   203
    val declT = type_scheme consts c;
wenzelm@18163
   204
    val vars = map Term.dest_TVar (typargs consts (c, declT));
wenzelm@24673
   205
    val inst = vars ~~ Ts handle UnequalLengths =>
wenzelm@24673
   206
      raise TYPE ("Consts.instance", Ts, [Const (c, dummyT)]);
wenzelm@24673
   207
  in declT |> TermSubst.instantiateT inst end;
wenzelm@18163
   208
wenzelm@18060
   209
wenzelm@18060
   210
wenzelm@19364
   211
(** build consts **)
wenzelm@18060
   212
wenzelm@23655
   213
fun err_dup_const c =
wenzelm@23655
   214
  error ("Duplicate declaration of constant " ^ quote c);
wenzelm@18060
   215
haftmann@23086
   216
fun extend_decls naming decl tab = NameSpace.extend_table naming [decl] tab
wenzelm@23655
   217
  handle Symtab.DUP c => err_dup_const c;
wenzelm@18060
   218
wenzelm@18935
   219
wenzelm@18935
   220
(* name space *)
wenzelm@18060
   221
wenzelm@24673
   222
fun hide fully c = map_consts (fn (decls, constraints, rev_abbrevs) =>
wenzelm@24673
   223
  (apfst (NameSpace.hide fully c) decls, constraints, rev_abbrevs));
wenzelm@18935
   224
wenzelm@18935
   225
wenzelm@18935
   226
(* declarations *)
wenzelm@18935
   227
wenzelm@24772
   228
fun declare authentic naming tags (c, declT) = map_consts (fn (decls, constraints, rev_abbrevs) =>
wenzelm@18060
   229
  let
wenzelm@24909
   230
    val decl = {T = declT, typargs = typargs_of declT, tags = tags, authentic = authentic};
wenzelm@24909
   231
    val decls' = decls |> extend_decls naming (c, ((decl, NONE), serial ()));
wenzelm@24772
   232
  in (decls', constraints, rev_abbrevs) end);
wenzelm@19364
   233
wenzelm@19364
   234
wenzelm@19364
   235
(* constraints *)
wenzelm@19364
   236
wenzelm@19364
   237
fun constrain (c, C) consts =
wenzelm@24673
   238
  consts |> map_consts (fn (decls, constraints, rev_abbrevs) =>
wenzelm@19364
   239
    (the_const consts c handle TYPE (msg, _, _) => error msg;
wenzelm@19364
   240
      (decls,
wenzelm@19364
   241
        constraints |> (case C of SOME T => Symtab.update (c, T) | NONE => Symtab.delete_safe c),
wenzelm@24673
   242
        rev_abbrevs)));
wenzelm@18060
   243
wenzelm@18935
   244
wenzelm@18935
   245
(* abbreviations *)
wenzelm@18935
   246
wenzelm@19027
   247
local
wenzelm@19027
   248
wenzelm@19364
   249
fun strip_abss tm = ([], tm) ::
wenzelm@19364
   250
  (case tm of
wenzelm@19364
   251
    Abs (a, T, t) =>
wenzelm@19364
   252
      if Term.loose_bvar1 (t, 0) then
wenzelm@19364
   253
        strip_abss t |> map (fn (xs, b) => ((a, T) :: xs, b))
wenzelm@19364
   254
      else []
wenzelm@19364
   255
  | _ => []);
wenzelm@19027
   256
wenzelm@21720
   257
fun rev_abbrev lhs rhs =
wenzelm@18060
   258
  let
wenzelm@19027
   259
    fun abbrev (xs, body) =
wenzelm@19027
   260
      let val vars = fold (fn (x, T) => cons (Var ((x, 0), T))) (Term.rename_wrt_term body xs) []
wenzelm@21720
   261
      in (Term.subst_bounds (rev vars, body), Term.list_comb (lhs, vars)) end;
wenzelm@19502
   262
  in map abbrev (strip_abss (Envir.beta_eta_contract rhs)) end;
wenzelm@19027
   263
wenzelm@19027
   264
in
wenzelm@18965
   265
wenzelm@24772
   266
fun abbreviate pp tsig naming mode tags (c, raw_rhs) consts =
wenzelm@18965
   267
  let
wenzelm@24673
   268
    val cert_term = certify pp tsig false consts;
wenzelm@24673
   269
    val expand_term = certify pp tsig true consts;
wenzelm@21822
   270
    val force_expand = mode = Syntax.internalM;
wenzelm@21720
   271
wenzelm@19364
   272
    val rhs = raw_rhs
wenzelm@20548
   273
      |> Term.map_types (Type.cert_typ tsig)
wenzelm@21720
   274
      |> cert_term;
wenzelm@21794
   275
    val T = Term.fastype_of rhs;
wenzelm@21794
   276
    val lhs = Const (NameSpace.full naming c, T);
wenzelm@21720
   277
    val rhs' = expand_term rhs;
wenzelm@21720
   278
wenzelm@21680
   279
    fun err msg = error (msg ^ " on rhs of abbreviation:\n" ^
wenzelm@21720
   280
      Pretty.string_of_term pp (Logic.mk_equals (lhs, rhs)));
wenzelm@25048
   281
    val _ = Term.exists_subterm Term.is_Var rhs andalso err "Illegal schematic variables";
wenzelm@25048
   282
    val _ = null (Term.hidden_polymorphism rhs) orelse err "Extra type variables";
wenzelm@19364
   283
  in
wenzelm@24673
   284
    consts |> map_consts (fn (decls, constraints, rev_abbrevs) =>
wenzelm@19364
   285
      let
wenzelm@24909
   286
        val decl = {T = T, typargs = typargs_of T, tags = tags, authentic = true};
wenzelm@24909
   287
        val abbr = {rhs = rhs, normal_rhs = 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@21720
   291
          |> fold (curry Symtab.update_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@25048
   300
      |> fold (curry Symtab.update_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;