src/Pure/consts.ML
author wenzelm
Thu Oct 04 20:29:42 2007 +0200 (2007-10-04)
changeset 24850 0cfd722ab579
parent 24772 2b838dfeca1e
child 24909 afae05eb1f1c
permissions -rw-r--r--
Name.uu, Name.aT;
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@21720
    17
  val the_abbreviation: T -> string -> typ * (term * term)          (*exception TYPE*)
wenzelm@21720
    18
  val the_declaration: T -> string -> typ                           (*exception TYPE*)
wenzelm@21720
    19
  val is_monomorphic: T -> string -> bool                           (*exception TYPE*)
wenzelm@21720
    20
  val the_constraint: T -> string -> typ                            (*exception TYPE*)
wenzelm@24772
    21
  val the_tags: T -> string -> Markup.property list                 (*exception TYPE*)
wenzelm@18965
    22
  val space_of: T -> NameSpace.T
wenzelm@19364
    23
  val intern: T -> xstring -> string
wenzelm@19364
    24
  val extern: T -> string -> xstring
wenzelm@19364
    25
  val extern_early: T -> string -> xstring
wenzelm@19657
    26
  val syntax: T -> string * mixfix -> string * typ * mixfix
wenzelm@21181
    27
  val syntax_name: T -> string -> string
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@24772
    32
  val declare: bool -> NameSpace.naming -> Markup.property list -> (bstring * typ) -> T -> T
wenzelm@19098
    33
  val constrain: string * typ option -> T -> T
wenzelm@24772
    34
  val abbreviate: Pretty.pp -> Type.tsig -> NameSpace.naming -> string -> Markup.property list ->
wenzelm@21794
    35
    bstring * term -> T -> (term * term) * 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
wenzelm@19364
    45
(** consts type **)
wenzelm@19364
    46
wenzelm@19364
    47
(* datatype T *)
wenzelm@18060
    48
wenzelm@19364
    49
datatype kind =
wenzelm@21720
    50
  LogicalConst of int list list |      (*typargs positions*)
wenzelm@21720
    51
  Abbreviation of term * term * bool   (*rhs, normal rhs, force_expand*);
wenzelm@19364
    52
wenzelm@24772
    53
type decl = {T: typ, kind: kind, tags: Markup.property list, authentic: bool};
wenzelm@18935
    54
wenzelm@18060
    55
datatype T = Consts of
wenzelm@20667
    56
 {decls: (decl * 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_kind (LogicalConst _) = NONE
wenzelm@21720
    75
  | dest_kind (Abbreviation (t, t', _)) = SOME (t, t');
wenzelm@19364
    76
wenzelm@19364
    77
fun dest (Consts ({decls = (space, decls), constraints, ...}, _)) =
wenzelm@19364
    78
 {constants = (space,
wenzelm@24772
    79
    Symtab.fold (fn (c, ({T, kind, ...}, _)) =>
wenzelm@19364
    80
      Symtab.update (c, (T, dest_kind kind))) decls Symtab.empty),
wenzelm@18060
    81
  constraints = (space, constraints)};
wenzelm@18060
    82
wenzelm@18060
    83
wenzelm@18060
    84
(* lookup consts *)
wenzelm@18060
    85
wenzelm@19027
    86
fun the_const (Consts ({decls = (_, tab), ...}, _)) c =
wenzelm@19364
    87
  (case Symtab.lookup tab c of
wenzelm@24772
    88
    SOME (decl, _) => decl
wenzelm@19364
    89
  | NONE => raise TYPE ("Undeclared constant: " ^ quote c, [], []));
wenzelm@18935
    90
wenzelm@18935
    91
fun logical_const consts c =
wenzelm@24772
    92
  (case the_const consts c of
wenzelm@24772
    93
    {T, kind = LogicalConst ps, ...} => (T, ps)
wenzelm@21720
    94
  | _ => raise TYPE ("Not a logical constant: " ^ quote c, [], []));
wenzelm@18060
    95
wenzelm@21720
    96
fun the_abbreviation consts c =
wenzelm@24772
    97
  (case the_const consts c of
wenzelm@24772
    98
    {T, kind = Abbreviation (t, t', _), ...} => (T, (t, t'))
wenzelm@21720
    99
  | _ => raise TYPE ("Not an abbreviated constant: " ^ quote c, [], []));
wenzelm@21720
   100
wenzelm@21720
   101
val the_declaration = #1 oo logical_const;
wenzelm@19364
   102
val type_arguments = #2 oo logical_const;
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@24772
   108
  | NONE => #T (the_const consts c));
wenzelm@24772
   109
wenzelm@24772
   110
val the_tags = #tags oo the_const;
wenzelm@18935
   111
wenzelm@18935
   112
wenzelm@19657
   113
(* name space and syntax *)
wenzelm@19364
   114
wenzelm@19364
   115
fun space_of (Consts ({decls = (space, _), ...}, _)) = space;
wenzelm@19364
   116
wenzelm@19364
   117
val intern = NameSpace.intern o space_of;
wenzelm@19364
   118
val extern = NameSpace.extern o space_of;
wenzelm@19364
   119
wenzelm@19364
   120
fun extern_early consts c =
wenzelm@19364
   121
  (case try (the_const consts) c of
wenzelm@24772
   122
    SOME {authentic = true, ...} => Syntax.constN ^ c
wenzelm@19576
   123
  | _ => extern consts c);
wenzelm@19364
   124
wenzelm@19657
   125
fun syntax consts (c, mx) =
wenzelm@19657
   126
  let
wenzelm@24772
   127
    val {T, authentic, ...} = the_const consts c handle TYPE (msg, _, _) => error msg;
wenzelm@19677
   128
    val c' = if authentic then Syntax.constN ^ c else NameSpace.base c;
wenzelm@19657
   129
  in (c', T, mx) end;
wenzelm@19657
   130
wenzelm@21181
   131
fun syntax_name consts c = #1 (syntax consts (c, NoSyn));
wenzelm@21181
   132
wenzelm@18060
   133
wenzelm@19364
   134
(* read_const *)
wenzelm@19364
   135
wenzelm@19364
   136
fun read_const consts raw_c =
wenzelm@19364
   137
  let
wenzelm@19364
   138
    val c = intern consts raw_c;
wenzelm@24772
   139
    val {T, ...} = the_const consts c handle TYPE (msg, _, _) => error msg;
wenzelm@21205
   140
  in Const (c, T) end;
wenzelm@19364
   141
wenzelm@19364
   142
wenzelm@19364
   143
(* certify *)
wenzelm@19364
   144
wenzelm@24673
   145
fun certify pp tsig do_expand consts =
wenzelm@18965
   146
  let
wenzelm@18965
   147
    fun err msg (c, T) =
wenzelm@18965
   148
      raise TYPE (msg ^ " " ^ quote c ^ " :: " ^ Pretty.string_of_typ pp T, [], []);
wenzelm@24673
   149
    val certT = Type.cert_typ tsig;
wenzelm@18965
   150
    fun cert tm =
wenzelm@18965
   151
      let
wenzelm@18965
   152
        val (head, args) = Term.strip_comb tm;
wenzelm@21694
   153
        val args' = map cert args;
wenzelm@21694
   154
        fun comb head' = Term.list_comb (head', args');
wenzelm@18965
   155
      in
wenzelm@18965
   156
        (case head of
wenzelm@24673
   157
          Abs (x, T, t) => comb (Abs (x, certT T, cert t))
wenzelm@19364
   158
        | Const (c, T) =>
wenzelm@19364
   159
            let
wenzelm@24673
   160
              val T' = certT T;
wenzelm@24772
   161
              val {T = U, kind, ...} = the_const consts c;
wenzelm@19364
   162
            in
wenzelm@19433
   163
              if not (Type.raw_instance (T', U)) then
wenzelm@19364
   164
                err "Illegal type for constant" (c, T)
wenzelm@19364
   165
              else
wenzelm@21720
   166
                (case kind of
wenzelm@21720
   167
                  Abbreviation (_, u, force_expand) =>
wenzelm@21720
   168
                    if do_expand orelse force_expand then
wenzelm@21720
   169
                      Term.betapplys (Envir.expand_atom T' (U, u) handle TYPE _ =>
wenzelm@21720
   170
                        err "Illegal type for abbreviation" (c, T), args')
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@19364
   181
fun subscript (Type (_, Ts)) (i :: is) = subscript (nth Ts i) is
wenzelm@19364
   182
  | subscript T [] = T
wenzelm@19364
   183
  | subscript T _ = raise Subscript;
wenzelm@18060
   184
wenzelm@19364
   185
fun typargs consts (c, T) = map (subscript T) (type_arguments consts c);
wenzelm@18060
   186
wenzelm@18163
   187
fun instance consts (c, Ts) =
wenzelm@18163
   188
  let
wenzelm@21720
   189
    val declT = the_declaration consts c;
wenzelm@18163
   190
    val vars = map Term.dest_TVar (typargs consts (c, declT));
wenzelm@24673
   191
    val inst = vars ~~ Ts handle UnequalLengths =>
wenzelm@24673
   192
      raise TYPE ("Consts.instance", Ts, [Const (c, dummyT)]);
wenzelm@24673
   193
  in declT |> TermSubst.instantiateT inst end;
wenzelm@18163
   194
wenzelm@18060
   195
wenzelm@18060
   196
wenzelm@19364
   197
(** build consts **)
wenzelm@18060
   198
wenzelm@23655
   199
fun err_dup_const c =
wenzelm@23655
   200
  error ("Duplicate declaration of constant " ^ quote c);
wenzelm@18060
   201
wenzelm@23655
   202
fun err_inconsistent_constraints c =
wenzelm@23655
   203
  error ("Inconsistent type constraints for constant " ^ quote c);
wenzelm@18060
   204
haftmann@23086
   205
fun extend_decls naming decl tab = NameSpace.extend_table naming [decl] tab
wenzelm@23655
   206
  handle Symtab.DUP c => err_dup_const c;
wenzelm@18060
   207
wenzelm@18935
   208
wenzelm@18935
   209
(* name space *)
wenzelm@18060
   210
wenzelm@24673
   211
fun hide fully c = map_consts (fn (decls, constraints, rev_abbrevs) =>
wenzelm@24673
   212
  (apfst (NameSpace.hide fully c) decls, constraints, rev_abbrevs));
wenzelm@18935
   213
wenzelm@18935
   214
wenzelm@18935
   215
(* declarations *)
wenzelm@18935
   216
wenzelm@24772
   217
fun declare authentic naming tags (c, declT) = map_consts (fn (decls, constraints, rev_abbrevs) =>
wenzelm@18060
   218
  let
wenzelm@18935
   219
    fun args_of (Type (_, Ts)) pos = args_of_list Ts 0 pos
wenzelm@18935
   220
      | args_of (TVar v) pos = insert (eq_fst op =) (v, rev pos)
wenzelm@18935
   221
      | args_of (TFree _) _ = I
wenzelm@18935
   222
    and args_of_list (T :: Ts) i is = args_of T (i :: is) #> args_of_list Ts (i + 1) is
wenzelm@18935
   223
      | args_of_list [] _ _ = I;
wenzelm@20667
   224
    val decl =
wenzelm@24772
   225
      {T = declT, kind = LogicalConst (map #2 (rev (args_of declT [] []))),
wenzelm@24772
   226
        tags = tags, authentic = authentic};
wenzelm@24772
   227
    val decls' = decls |> extend_decls naming (c, (decl, serial ()));
wenzelm@24772
   228
  in (decls', constraints, rev_abbrevs) end);
wenzelm@19364
   229
wenzelm@19364
   230
wenzelm@19364
   231
(* constraints *)
wenzelm@19364
   232
wenzelm@19364
   233
fun constrain (c, C) consts =
wenzelm@24673
   234
  consts |> map_consts (fn (decls, constraints, rev_abbrevs) =>
wenzelm@19364
   235
    (the_const consts c handle TYPE (msg, _, _) => error msg;
wenzelm@19364
   236
      (decls,
wenzelm@19364
   237
        constraints |> (case C of SOME T => Symtab.update (c, T) | NONE => Symtab.delete_safe c),
wenzelm@24673
   238
        rev_abbrevs)));
wenzelm@18060
   239
wenzelm@18935
   240
wenzelm@18935
   241
(* abbreviations *)
wenzelm@18935
   242
wenzelm@19027
   243
local
wenzelm@19027
   244
wenzelm@19364
   245
fun strip_abss tm = ([], tm) ::
wenzelm@19364
   246
  (case tm of
wenzelm@19364
   247
    Abs (a, T, t) =>
wenzelm@19364
   248
      if Term.loose_bvar1 (t, 0) then
wenzelm@19364
   249
        strip_abss t |> map (fn (xs, b) => ((a, T) :: xs, b))
wenzelm@19364
   250
      else []
wenzelm@19364
   251
  | _ => []);
wenzelm@19027
   252
wenzelm@21720
   253
fun rev_abbrev lhs rhs =
wenzelm@18060
   254
  let
wenzelm@19027
   255
    fun abbrev (xs, body) =
wenzelm@19027
   256
      let val vars = fold (fn (x, T) => cons (Var ((x, 0), T))) (Term.rename_wrt_term body xs) []
wenzelm@21720
   257
      in (Term.subst_bounds (rev vars, body), Term.list_comb (lhs, vars)) end;
wenzelm@19502
   258
  in map abbrev (strip_abss (Envir.beta_eta_contract rhs)) end;
wenzelm@19027
   259
wenzelm@19027
   260
in
wenzelm@18965
   261
wenzelm@24772
   262
fun abbreviate pp tsig naming mode tags (c, raw_rhs) consts =
wenzelm@18965
   263
  let
wenzelm@24673
   264
    val cert_term = certify pp tsig false consts;
wenzelm@24673
   265
    val expand_term = certify pp tsig true consts;
wenzelm@21822
   266
    val force_expand = mode = Syntax.internalM;
wenzelm@21720
   267
wenzelm@19364
   268
    val rhs = raw_rhs
wenzelm@20548
   269
      |> Term.map_types (Type.cert_typ tsig)
wenzelm@21720
   270
      |> cert_term;
wenzelm@21794
   271
    val T = Term.fastype_of rhs;
wenzelm@21794
   272
    val lhs = Const (NameSpace.full naming c, T);
wenzelm@21720
   273
    val rhs' = expand_term rhs;
wenzelm@21720
   274
wenzelm@21680
   275
    fun err msg = error (msg ^ " on rhs of abbreviation:\n" ^
wenzelm@21720
   276
      Pretty.string_of_term pp (Logic.mk_equals (lhs, rhs)));
wenzelm@21680
   277
    val _ = Term.exists_subterm Term.is_Var rhs andalso err "Illegal schematic variables"
wenzelm@21680
   278
    val _ = null (Term.hidden_polymorphism rhs T) orelse err "Extra type variables";
wenzelm@19364
   279
  in
wenzelm@24673
   280
    consts |> map_consts (fn (decls, constraints, rev_abbrevs) =>
wenzelm@19364
   281
      let
wenzelm@24772
   282
        val decl =
wenzelm@24772
   283
          {T = T, kind = Abbreviation (rhs, rhs', force_expand), tags = tags, authentic = true};
wenzelm@24772
   284
        val decls' = decls
wenzelm@24772
   285
          |> extend_decls naming (c, (decl, serial ()));
wenzelm@19364
   286
        val rev_abbrevs' = rev_abbrevs
wenzelm@21720
   287
          |> fold (curry Symtab.update_list mode) (rev_abbrev lhs rhs);
wenzelm@24673
   288
      in (decls', constraints, rev_abbrevs') end)
wenzelm@21794
   289
    |> pair (lhs, rhs)
wenzelm@19364
   290
  end;
wenzelm@18935
   291
wenzelm@19027
   292
end;
wenzelm@19027
   293
wenzelm@18060
   294
wenzelm@18060
   295
(* empty and merge *)
wenzelm@18060
   296
wenzelm@24673
   297
val empty = make_consts (NameSpace.empty_table, Symtab.empty, Symtab.empty);
wenzelm@18060
   298
wenzelm@18060
   299
fun merge
wenzelm@24673
   300
   (Consts ({decls = decls1, constraints = constraints1, rev_abbrevs = rev_abbrevs1}, _),
wenzelm@24673
   301
    Consts ({decls = decls2, constraints = constraints2, rev_abbrevs = rev_abbrevs2}, _)) =
wenzelm@18060
   302
  let
wenzelm@18935
   303
    val decls' = NameSpace.merge_tables (eq_snd (op =)) (decls1, decls2)
wenzelm@23655
   304
      handle Symtab.DUP c => err_dup_const c;
wenzelm@18935
   305
    val constraints' = Symtab.merge (op =) (constraints1, constraints2)
wenzelm@23655
   306
      handle Symtab.DUP c => err_inconsistent_constraints c;
wenzelm@19364
   307
    val rev_abbrevs' = (rev_abbrevs1, rev_abbrevs2) |> Symtab.join
wenzelm@19364
   308
      (K (Library.merge (fn ((t, u), (t', u')) => t aconv t' andalso u aconv u')));
wenzelm@24673
   309
  in make_consts (decls', constraints', rev_abbrevs') end;
wenzelm@18060
   310
wenzelm@18060
   311
end;