src/Pure/sign.ML
author wenzelm
Mon Aug 01 19:20:42 2005 +0200 (2005-08-01)
changeset 16988 02cd0c8b96d9
parent 16941 0bda949449ee
child 17037 bd15f69bd947
permissions -rw-r--r--
Compress.typ;
wenzelm@19
     1
(*  Title:      Pure/sign.ML
clasohm@0
     2
    ID:         $Id$
wenzelm@251
     3
    Author:     Lawrence C Paulson and Markus Wenzel
clasohm@0
     4
wenzelm@16442
     5
Logical signature content: naming conventions, concrete syntax, type
wenzelm@16442
     6
signature, constant declarations.
clasohm@0
     7
*)
clasohm@0
     8
wenzelm@16442
     9
signature SIGN_THEORY =
wenzelm@3791
    10
sig
wenzelm@16442
    11
  val add_defsort: string -> theory -> theory
wenzelm@16442
    12
  val add_defsort_i: sort -> theory -> theory
wenzelm@16442
    13
  val add_types: (bstring * int * mixfix) list -> theory -> theory
wenzelm@16442
    14
  val add_typedecls: (bstring * string list * mixfix) list -> theory -> theory
wenzelm@16442
    15
  val add_nonterminals: bstring list -> theory -> theory
wenzelm@16442
    16
  val add_tyabbrs: (bstring * string list * string * mixfix) list -> theory -> theory
wenzelm@16442
    17
  val add_tyabbrs_i: (bstring * string list * typ * mixfix) list -> theory -> theory
wenzelm@16442
    18
  val add_arities: (xstring * string list * string) list -> theory -> theory
wenzelm@16442
    19
  val add_arities_i: (string * sort list * sort) list -> theory -> theory
wenzelm@16442
    20
  val add_syntax: (bstring * string * mixfix) list -> theory -> theory
wenzelm@16442
    21
  val add_syntax_i: (bstring * typ * mixfix) list -> theory -> theory
wenzelm@16442
    22
  val add_modesyntax: (string * bool) -> (bstring * string * mixfix) list -> theory -> theory
wenzelm@16442
    23
  val add_modesyntax_i: (string * bool) -> (bstring * typ * mixfix) list -> theory -> theory
wenzelm@16442
    24
  val del_modesyntax: (string * bool) -> (bstring * string * mixfix) list -> theory -> theory
wenzelm@16442
    25
  val del_modesyntax_i: (string * bool) -> (bstring * typ * mixfix) list -> theory -> theory
wenzelm@16442
    26
  val add_consts: (bstring * string * mixfix) list -> theory -> theory
wenzelm@16442
    27
  val add_consts_i: (bstring * typ * mixfix) list -> theory -> theory
wenzelm@16941
    28
  val add_const_constraint: xstring * string -> theory -> theory
wenzelm@16941
    29
  val add_const_constraint_i: string * typ -> theory -> theory
wenzelm@16442
    30
  val add_classes: (bstring * xstring list) list -> theory -> theory
wenzelm@16442
    31
  val add_classes_i: (bstring * class list) list -> theory -> theory
wenzelm@16442
    32
  val add_classrel: (xstring * xstring) list -> theory -> theory
wenzelm@16442
    33
  val add_classrel_i: (class * class) list -> theory -> theory
paulson@1501
    34
  val add_trfuns:
wenzelm@4344
    35
    (string * (ast list -> ast)) list *
wenzelm@4344
    36
    (string * (term list -> term)) list *
wenzelm@4344
    37
    (string * (term list -> term)) list *
wenzelm@16442
    38
    (string * (ast list -> ast)) list -> theory -> theory
wenzelm@2385
    39
  val add_trfunsT:
wenzelm@16442
    40
    (string * (bool -> typ -> term list -> term)) list -> theory -> theory
wenzelm@14645
    41
  val add_advanced_trfuns:
wenzelm@16442
    42
    (string * (theory -> ast list -> ast)) list *
wenzelm@16442
    43
    (string * (theory -> term list -> term)) list *
wenzelm@16442
    44
    (string * (theory -> term list -> term)) list *
wenzelm@16442
    45
    (string * (theory -> ast list -> ast)) list -> theory -> theory
wenzelm@14645
    46
  val add_advanced_trfunsT:
wenzelm@16442
    47
    (string * (theory -> bool -> typ -> term list -> term)) list -> theory -> theory
wenzelm@2693
    48
  val add_tokentrfuns:
wenzelm@16442
    49
    (string * string * (string -> string * real)) list -> theory -> theory
wenzelm@16442
    50
  val add_mode_tokentrfuns: string -> (string * (string -> string * real)) list
wenzelm@16442
    51
    -> theory -> theory
wenzelm@16442
    52
  val add_trrules: (xstring * string) Syntax.trrule list -> theory -> theory
wenzelm@16442
    53
  val add_trrules_i: ast Syntax.trrule list -> theory -> theory
wenzelm@16442
    54
  val add_path: string -> theory -> theory
wenzelm@16442
    55
  val parent_path: theory -> theory
wenzelm@16442
    56
  val root_path: theory -> theory
wenzelm@16442
    57
  val absolute_path: theory -> theory
wenzelm@16442
    58
  val local_path: theory -> theory
wenzelm@16442
    59
  val qualified_names: theory -> theory
wenzelm@16442
    60
  val no_base_names: theory -> theory
wenzelm@16442
    61
  val custom_accesses: (string list -> string list list) -> theory -> theory
wenzelm@16442
    62
  val set_policy: (string -> bstring -> string) * (string list -> string list list) ->
wenzelm@16442
    63
    theory -> theory
wenzelm@16442
    64
  val restore_naming: theory -> theory -> theory
wenzelm@16442
    65
  val hide_classes: bool -> xstring list -> theory -> theory
wenzelm@16442
    66
  val hide_classes_i: bool -> string list -> theory -> theory
wenzelm@16442
    67
  val hide_types: bool -> xstring list -> theory -> theory
wenzelm@16442
    68
  val hide_types_i: bool -> string list -> theory -> theory
wenzelm@16442
    69
  val hide_consts: bool -> xstring list -> theory -> theory
wenzelm@16442
    70
  val hide_consts_i: bool -> string list -> theory -> theory
wenzelm@16442
    71
end
clasohm@0
    72
wenzelm@16442
    73
signature SIGN =
wenzelm@5642
    74
sig
wenzelm@16442
    75
  type sg    (*obsolete*)
wenzelm@16536
    76
  val init_data: theory -> theory
wenzelm@16442
    77
  val rep_sg: theory ->
wenzelm@16442
    78
   {naming: NameSpace.naming,
wenzelm@16597
    79
    syn: Syntax.syntax,
wenzelm@16442
    80
    tsig: Type.tsig,
wenzelm@16941
    81
    consts: (typ * stamp) NameSpace.table * typ Symtab.table}
wenzelm@16442
    82
  val naming_of: theory -> NameSpace.naming
wenzelm@16442
    83
  val base_name: string -> bstring
wenzelm@16442
    84
  val full_name: theory -> bstring -> string
wenzelm@16442
    85
  val full_name_path: theory -> string -> bstring -> string
wenzelm@16442
    86
  val declare_name: theory -> string -> NameSpace.T -> NameSpace.T
wenzelm@16442
    87
  val syn_of: theory -> Syntax.syntax
wenzelm@16442
    88
  val tsig_of: theory -> Type.tsig
wenzelm@16442
    89
  val classes: theory -> class list
wenzelm@16442
    90
  val defaultS: theory -> sort
wenzelm@16442
    91
  val subsort: theory -> sort * sort -> bool
wenzelm@16442
    92
  val of_sort: theory -> typ * sort -> bool
wenzelm@16442
    93
  val witness_sorts: theory -> sort list -> sort list -> (typ * sort) list
wenzelm@16442
    94
  val universal_witness: theory -> (typ * sort) option
wenzelm@16655
    95
  val all_sorts_nonempty: theory -> bool
wenzelm@16442
    96
  val typ_instance: theory -> typ * typ -> bool
wenzelm@16941
    97
  val typ_match: theory -> typ * typ -> Type.tyenv -> Type.tyenv
wenzelm@16941
    98
  val typ_unify: theory -> typ * typ -> Type.tyenv * int -> Type.tyenv * int
wenzelm@16442
    99
  val is_logtype: theory -> string -> bool
wenzelm@16941
   100
  val const_constraint: theory -> string -> typ option
wenzelm@16442
   101
  val const_type: theory -> string -> typ option
wenzelm@16442
   102
  val the_const_type: theory -> string -> typ
wenzelm@16442
   103
  val declared_tyname: theory -> string -> bool
wenzelm@16442
   104
  val declared_const: theory -> string -> bool
wenzelm@16442
   105
  val class_space: theory -> NameSpace.T
wenzelm@16442
   106
  val type_space: theory -> NameSpace.T
wenzelm@16442
   107
  val const_space: theory -> NameSpace.T
wenzelm@16442
   108
  val intern_class: theory -> xstring -> string
wenzelm@16442
   109
  val extern_class: theory -> string -> xstring
wenzelm@16442
   110
  val intern_type: theory -> xstring -> string
wenzelm@16442
   111
  val extern_type: theory -> string -> xstring
wenzelm@16442
   112
  val intern_const: theory -> xstring -> string
wenzelm@16442
   113
  val extern_const: theory -> string -> xstring
wenzelm@16442
   114
  val intern_sort: theory -> sort -> sort
wenzelm@16442
   115
  val extern_sort: theory -> sort -> sort
wenzelm@16442
   116
  val intern_typ: theory -> typ -> typ
wenzelm@16442
   117
  val extern_typ: theory -> typ -> typ
wenzelm@16442
   118
  val intern_term: theory -> term -> term
wenzelm@16442
   119
  val extern_term: theory -> term -> term
wenzelm@16442
   120
  val intern_tycons: theory -> typ -> typ
wenzelm@16442
   121
  val pretty_term': Syntax.syntax -> theory -> term -> Pretty.T
wenzelm@16442
   122
  val pretty_term: theory -> term -> Pretty.T
wenzelm@16442
   123
  val pretty_typ: theory -> typ -> Pretty.T
wenzelm@16442
   124
  val pretty_sort: theory -> sort -> Pretty.T
wenzelm@16442
   125
  val pretty_classrel: theory -> class list -> Pretty.T
wenzelm@16442
   126
  val pretty_arity: theory -> arity -> Pretty.T
wenzelm@16442
   127
  val string_of_term: theory -> term -> string
wenzelm@16442
   128
  val string_of_typ: theory -> typ -> string
wenzelm@16442
   129
  val string_of_sort: theory -> sort -> string
wenzelm@16442
   130
  val string_of_classrel: theory -> class list -> string
wenzelm@16442
   131
  val string_of_arity: theory -> arity -> string
wenzelm@16442
   132
  val pprint_term: theory -> term -> pprint_args -> unit
wenzelm@16442
   133
  val pprint_typ: theory -> typ -> pprint_args -> unit
wenzelm@16442
   134
  val pp: theory -> Pretty.pp
wenzelm@16442
   135
  val certify_class: theory -> class -> class
wenzelm@16442
   136
  val certify_sort: theory -> sort -> sort
wenzelm@16442
   137
  val certify_typ: theory -> typ -> typ
wenzelm@16442
   138
  val certify_typ_syntax: theory -> typ -> typ
wenzelm@16442
   139
  val certify_typ_abbrev: theory -> typ -> typ
wenzelm@16442
   140
  val certify_term: Pretty.pp -> theory -> term -> term * typ * int
wenzelm@16494
   141
  val certify_prop: Pretty.pp -> theory -> term -> term * typ * int
wenzelm@16494
   142
  val cert_term: theory -> term -> term
wenzelm@16494
   143
  val cert_prop: theory -> term -> term
wenzelm@16442
   144
  val read_sort': Syntax.syntax -> theory -> string -> sort
wenzelm@16442
   145
  val read_sort: theory -> string -> sort
wenzelm@16442
   146
  val read_typ': Syntax.syntax -> theory * (indexname -> sort option) -> string -> typ
wenzelm@16442
   147
  val read_typ_syntax': Syntax.syntax -> theory * (indexname -> sort option) -> string -> typ
wenzelm@16442
   148
  val read_typ_abbrev': Syntax.syntax -> theory * (indexname -> sort option) -> string -> typ
wenzelm@16442
   149
  val read_typ: theory * (indexname -> sort option) -> string -> typ
wenzelm@16442
   150
  val read_typ_syntax: theory * (indexname -> sort option) -> string -> typ
wenzelm@16442
   151
  val read_typ_abbrev: theory * (indexname -> sort option) -> string -> typ
wenzelm@16442
   152
  val read_tyname: theory -> string -> typ
wenzelm@16442
   153
  val read_const: theory -> string -> term
wenzelm@16442
   154
  val infer_types_simult: Pretty.pp -> theory -> (indexname -> typ option) ->
wenzelm@16442
   155
    (indexname -> sort option) -> string list -> bool
wenzelm@16442
   156
    -> (term list * typ) list -> term list * (indexname * typ) list
wenzelm@16442
   157
  val infer_types: Pretty.pp -> theory -> (indexname -> typ option) ->
wenzelm@16442
   158
    (indexname -> sort option) -> string list -> bool
wenzelm@16442
   159
    -> term list * typ -> term * (indexname * typ) list
wenzelm@16442
   160
  val read_def_terms': Pretty.pp -> (string -> bool) -> Syntax.syntax ->
wenzelm@16442
   161
    theory * (indexname -> typ option) * (indexname -> sort option) ->
wenzelm@16442
   162
    string list -> bool -> (string * typ) list -> term list * (indexname * typ) list
wenzelm@16442
   163
  val read_def_terms:
wenzelm@16442
   164
    theory * (indexname -> typ option) * (indexname -> sort option) ->
wenzelm@16442
   165
    string list -> bool -> (string * typ) list -> term list * (indexname * typ) list
wenzelm@16442
   166
  val simple_read_term: theory -> typ -> string -> term
wenzelm@16494
   167
  val read_term: theory -> string -> term
wenzelm@16494
   168
  val read_prop: theory -> string -> term
wenzelm@16442
   169
  val const_of_class: class -> string
wenzelm@16442
   170
  val class_of_const: string -> class
wenzelm@16442
   171
  include SIGN_THEORY
wenzelm@16442
   172
end
wenzelm@5642
   173
wenzelm@16442
   174
structure Sign: SIGN =
wenzelm@143
   175
struct
clasohm@0
   176
wenzelm@16442
   177
type sg = theory;
wenzelm@2185
   178
wenzelm@402
   179
wenzelm@16442
   180
(** datatype sign **)
wenzelm@16337
   181
wenzelm@16442
   182
datatype sign = Sign of
wenzelm@16442
   183
 {naming: NameSpace.naming,                 (*common naming conventions*)
wenzelm@16597
   184
  syn: Syntax.syntax,                       (*concrete syntax for terms, types, sorts*)
wenzelm@16442
   185
  tsig: Type.tsig,                          (*order-sorted signature of types*)
wenzelm@16941
   186
  consts:
wenzelm@16941
   187
    (typ * stamp) NameSpace.table *         (*type schemes of declared term constants*)
wenzelm@16941
   188
    typ Symtab.table};                      (*type constraints for constants*)
wenzelm@16941
   189
wenzelm@16442
   190
wenzelm@16442
   191
fun make_sign (naming, syn, tsig, consts) =
wenzelm@16442
   192
  Sign {naming = naming, syn = syn, tsig = tsig, consts = consts};
wenzelm@16442
   193
wenzelm@16442
   194
fun err_dup_consts cs =
wenzelm@16442
   195
  error ("Duplicate declaration of constant(s) " ^ commas_quote cs);
wenzelm@16442
   196
wenzelm@16941
   197
fun err_inconsistent_constraints cs =
wenzelm@16941
   198
  error ("Inconsistent type constraints for constant(s) " ^ commas_quote cs);
wenzelm@16941
   199
wenzelm@16442
   200
structure SignData = TheoryDataFun
wenzelm@16442
   201
(struct
wenzelm@16442
   202
  val name = "Pure/sign";
wenzelm@16442
   203
  type T = sign;
wenzelm@16442
   204
  val copy = I;
wenzelm@16442
   205
  val extend = I;
wenzelm@16442
   206
wenzelm@16941
   207
  val empty = make_sign (NameSpace.default_naming, Syntax.pure_syn, Type.empty_tsig,
wenzelm@16941
   208
    (NameSpace.empty_table, Symtab.empty));
wenzelm@16442
   209
wenzelm@16442
   210
  fun merge pp (sign1, sign2) =
wenzelm@16442
   211
    let
wenzelm@16941
   212
      val Sign {naming = _, syn = syn1, tsig = tsig1, consts = (consts1, constraints1)} = sign1;
wenzelm@16941
   213
      val Sign {naming = _, syn = syn2, tsig = tsig2, consts = (consts2, constraints2)} = sign2;
wenzelm@16442
   214
wenzelm@16442
   215
      val naming = NameSpace.default_naming;
wenzelm@16597
   216
      val syn = Syntax.merge_syntaxes syn1 syn2;
wenzelm@16442
   217
      val tsig = Type.merge_tsigs pp (tsig1, tsig2);
wenzelm@16442
   218
      val consts = NameSpace.merge_tables eq_snd (consts1, consts2)
wenzelm@16442
   219
        handle Symtab.DUPS cs => err_dup_consts cs;
wenzelm@16941
   220
      val constraints = Symtab.merge (op =) (constraints1, constraints2)
wenzelm@16941
   221
        handle Symtab.DUPS cs => err_inconsistent_constraints cs;
wenzelm@16941
   222
    in make_sign (naming, syn, tsig, (consts, constraints)) end;
wenzelm@16442
   223
wenzelm@16442
   224
  fun print _ _ = ();
wenzelm@16442
   225
end);
wenzelm@16442
   226
wenzelm@16536
   227
val init_data = SignData.init;
wenzelm@16442
   228
wenzelm@16442
   229
fun rep_sg thy = SignData.get thy |> (fn Sign args => args);
wenzelm@16442
   230
wenzelm@16442
   231
fun map_sign f = SignData.map (fn Sign {naming, syn, tsig, consts} =>
wenzelm@16442
   232
  make_sign (f (naming, syn, tsig, consts)));
wenzelm@16442
   233
wenzelm@16442
   234
fun map_naming f = map_sign (fn (naming, syn, tsig, consts) => (f naming, syn, tsig, consts));
wenzelm@16442
   235
fun map_syn f = map_sign (fn (naming, syn, tsig, consts) => (naming, f syn, tsig, consts));
wenzelm@16442
   236
fun map_tsig f = map_sign (fn (naming, syn, tsig, consts) => (naming, syn, f tsig, consts));
wenzelm@16442
   237
fun map_consts f = map_sign (fn (naming, syn, tsig, consts) => (naming, syn, tsig, f consts));
wenzelm@16442
   238
wenzelm@16337
   239
wenzelm@16337
   240
(* naming *)
wenzelm@16337
   241
wenzelm@16442
   242
val naming_of = #naming o rep_sg;
wenzelm@16337
   243
val base_name = NameSpace.base;
wenzelm@16337
   244
val full_name = NameSpace.full o naming_of;
wenzelm@16442
   245
fun full_name_path thy elems = NameSpace.full (NameSpace.add_path elems (naming_of thy));
wenzelm@16337
   246
val declare_name = NameSpace.declare o naming_of;
wenzelm@14645
   247
wenzelm@14645
   248
wenzelm@14645
   249
(* syntax *)
wenzelm@14645
   250
wenzelm@16597
   251
val syn_of = #syn o rep_sg;
wenzelm@14645
   252
clasohm@0
   253
wenzelm@16337
   254
(* type signature *)
wenzelm@16337
   255
wenzelm@16337
   256
val tsig_of = #tsig o rep_sg;
wenzelm@7640
   257
val classes = Type.classes o tsig_of;
wenzelm@4844
   258
val defaultS = Type.defaultS o tsig_of;
wenzelm@4568
   259
val subsort = Type.subsort o tsig_of;
wenzelm@7640
   260
val of_sort = Type.of_sort o tsig_of;
wenzelm@7640
   261
val witness_sorts = Type.witness_sorts o tsig_of;
wenzelm@14784
   262
val universal_witness = Type.universal_witness o tsig_of;
wenzelm@16655
   263
val all_sorts_nonempty = is_some o universal_witness;
wenzelm@14784
   264
val typ_instance = Type.typ_instance o tsig_of;
wenzelm@16941
   265
val typ_match = Type.typ_match o tsig_of;
wenzelm@16941
   266
val typ_unify = Type.unify o tsig_of;
wenzelm@16442
   267
fun is_logtype thy c = c mem_string Type.logical_types (tsig_of thy);
wenzelm@4256
   268
wenzelm@4256
   269
wenzelm@16337
   270
(* consts signature *)
wenzelm@4256
   271
wenzelm@16941
   272
fun const_constraint thy c =
wenzelm@16941
   273
  let val ((_, consts), constraints) = #consts (rep_sg thy) in
wenzelm@16941
   274
    (case Symtab.lookup (constraints, c) of
wenzelm@16941
   275
      NONE => Option.map #1 (Symtab.lookup (consts, c))
wenzelm@16941
   276
    | some => some)
wenzelm@16941
   277
  end;
wenzelm@16941
   278
wenzelm@16941
   279
fun const_type thy c = Option.map #1 (Symtab.lookup (#2 (#1 (#consts (rep_sg thy))), c));
wenzelm@4256
   280
wenzelm@16442
   281
fun the_const_type thy c =
wenzelm@16442
   282
  (case const_type thy c of SOME T => T
wenzelm@16337
   283
  | NONE => raise TYPE ("Undeclared constant " ^ quote c, [], []));
wenzelm@4256
   284
wenzelm@16894
   285
val declared_tyname = Symtab.defined o #2 o #types o Type.rep_tsig o tsig_of;
wenzelm@16442
   286
val declared_const = is_some oo const_type;
wenzelm@620
   287
wenzelm@402
   288
clasohm@0
   289
wenzelm@16337
   290
(** intern / extern names **)
wenzelm@16337
   291
wenzelm@16368
   292
val class_space = #1 o #classes o Type.rep_tsig o tsig_of;
wenzelm@16368
   293
val type_space = #1 o #types o Type.rep_tsig o tsig_of;
wenzelm@16941
   294
val const_space = #1 o #1 o #consts o rep_sg
wenzelm@14645
   295
wenzelm@16368
   296
val intern_class = NameSpace.intern o class_space;
wenzelm@16368
   297
val extern_class = NameSpace.extern o class_space;
wenzelm@16368
   298
val intern_type = NameSpace.intern o type_space;
wenzelm@16368
   299
val extern_type = NameSpace.extern o type_space;
wenzelm@16368
   300
val intern_const = NameSpace.intern o const_space;
wenzelm@16368
   301
val extern_const = NameSpace.extern o const_space;
wenzelm@16337
   302
wenzelm@16337
   303
val intern_sort = map o intern_class;
wenzelm@16337
   304
val extern_sort = map o extern_class;
wenzelm@16337
   305
wenzelm@16337
   306
local
wenzelm@14645
   307
wenzelm@16337
   308
fun mapping add_names f t =
wenzelm@16337
   309
  let
wenzelm@16337
   310
    fun f' x = let val y = f x in if x = y then NONE else SOME (x, y) end;
wenzelm@16337
   311
    val tab = List.mapPartial f' (add_names (t, []));
wenzelm@16337
   312
    fun get x = if_none (assoc_string (tab, x)) x;
wenzelm@16337
   313
  in get end;
wenzelm@16337
   314
wenzelm@16442
   315
fun typ_mapping f g thy T =
wenzelm@16337
   316
  T |> Term.map_typ
wenzelm@16442
   317
    (mapping add_typ_classes (f thy) T)
wenzelm@16442
   318
    (mapping add_typ_tycons (g thy) T);
wenzelm@14645
   319
wenzelm@16442
   320
fun term_mapping f g h thy t =
wenzelm@16337
   321
  t |> Term.map_term
wenzelm@16442
   322
    (mapping add_term_classes (f thy) t)
wenzelm@16442
   323
    (mapping add_term_tycons (g thy) t)
wenzelm@16442
   324
    (mapping add_term_consts (h thy) t);
wenzelm@16337
   325
wenzelm@16337
   326
in
wenzelm@14645
   327
wenzelm@16368
   328
val intern_typ = typ_mapping intern_class intern_type;
wenzelm@16368
   329
val extern_typ = typ_mapping extern_class extern_type;
wenzelm@16368
   330
val intern_term = term_mapping intern_class intern_type intern_const;
wenzelm@16368
   331
val extern_term = term_mapping extern_class extern_type extern_const;
wenzelm@16368
   332
val intern_tycons = typ_mapping (K I) intern_type;
wenzelm@16337
   333
wenzelm@16337
   334
end;
wenzelm@14645
   335
wenzelm@14645
   336
wenzelm@14645
   337
wenzelm@4249
   338
(** pretty printing of terms, types etc. **)
wenzelm@3937
   339
wenzelm@16442
   340
fun pretty_term' syn thy t =
wenzelm@16597
   341
  Syntax.pretty_term thy syn (Context.exists_name Context.CPureN thy) (extern_term thy t);
wenzelm@16442
   342
fun pretty_term thy t = pretty_term' (syn_of thy) thy t;
wenzelm@16597
   343
fun pretty_typ thy T = Syntax.pretty_typ thy (syn_of thy) (extern_typ thy T);
wenzelm@16597
   344
fun pretty_sort thy S = Syntax.pretty_sort thy (syn_of thy) (extern_sort thy S);
wenzelm@3937
   345
wenzelm@16442
   346
fun pretty_classrel thy cs = Pretty.block (List.concat
wenzelm@16442
   347
  (separate [Pretty.str " <", Pretty.brk 1] (map (single o pretty_sort thy o single) cs)));
wenzelm@3937
   348
wenzelm@16442
   349
fun pretty_arity thy (a, Ss, S) =
wenzelm@3937
   350
  let
wenzelm@16442
   351
    val a' = extern_type thy a;
wenzelm@3937
   352
    val dom =
wenzelm@3937
   353
      if null Ss then []
wenzelm@16442
   354
      else [Pretty.list "(" ")" (map (pretty_sort thy) Ss), Pretty.brk 1];
wenzelm@16442
   355
  in Pretty.block ([Pretty.str (a' ^ " ::"), Pretty.brk 1] @ dom @ [pretty_sort thy S]) end;
wenzelm@3937
   356
wenzelm@14828
   357
val string_of_term = Pretty.string_of oo pretty_term;
wenzelm@14828
   358
val string_of_typ = Pretty.string_of oo pretty_typ;
wenzelm@14828
   359
val string_of_sort = Pretty.string_of oo pretty_sort;
wenzelm@14828
   360
val string_of_classrel = Pretty.string_of oo pretty_classrel;
wenzelm@14828
   361
val string_of_arity = Pretty.string_of oo pretty_arity;
wenzelm@3937
   362
wenzelm@16337
   363
val pprint_term = (Pretty.pprint o Pretty.quote) oo pretty_term;
wenzelm@16337
   364
val pprint_typ = (Pretty.pprint o Pretty.quote) oo pretty_typ;
wenzelm@3937
   365
wenzelm@16442
   366
fun pp thy = Pretty.pp (pretty_term thy, pretty_typ thy, pretty_sort thy,
wenzelm@16442
   367
  pretty_classrel thy, pretty_arity thy);
wenzelm@14828
   368
wenzelm@3937
   369
wenzelm@3937
   370
wenzelm@16337
   371
(** certify entities **)    (*exception TYPE*)
wenzelm@8898
   372
wenzelm@16337
   373
(* certify wrt. type signature *)
wenzelm@8898
   374
wenzelm@16723
   375
fun certify cert = cert o tsig_of o Context.check_thy;
wenzelm@562
   376
wenzelm@16337
   377
val certify_class      = certify Type.cert_class;
wenzelm@16337
   378
val certify_sort       = certify Type.cert_sort;
wenzelm@16337
   379
val certify_typ        = certify Type.cert_typ;
wenzelm@16337
   380
val certify_typ_syntax = certify Type.cert_typ_syntax;
wenzelm@16337
   381
val certify_typ_abbrev = certify Type.cert_typ_abbrev;
wenzelm@10443
   382
wenzelm@4961
   383
wenzelm@4961
   384
(* certify_term *)
wenzelm@4961
   385
wenzelm@14987
   386
local
nipkow@1494
   387
wenzelm@16337
   388
(*determine and check the type of a term*)
wenzelm@16337
   389
fun type_check pp tm =
wenzelm@4961
   390
  let
wenzelm@4961
   391
    fun err_appl why bs t T u U =
wenzelm@4961
   392
      let
wenzelm@10404
   393
        val xs = map Free bs;           (*we do not rename here*)
wenzelm@4961
   394
        val t' = subst_bounds (xs, t);
wenzelm@4961
   395
        val u' = subst_bounds (xs, u);
wenzelm@16337
   396
        val msg = cat_lines
wenzelm@14828
   397
          (TypeInfer.appl_error (Syntax.pp_show_brackets pp) why t' T u' U);
wenzelm@16337
   398
      in raise TYPE (msg, [T, U], [t', u']) end;
wenzelm@4961
   399
wenzelm@4961
   400
    fun typ_of (_, Const (_, T)) = T
wenzelm@4961
   401
      | typ_of (_, Free  (_, T)) = T
wenzelm@4961
   402
      | typ_of (_, Var (_, T)) = T
skalberg@15570
   403
      | typ_of (bs, Bound i) = snd (List.nth (bs, i) handle Subscript =>
wenzelm@4961
   404
          raise TYPE ("Loose bound variable: B." ^ string_of_int i, [], [Bound i]))
wenzelm@4961
   405
      | typ_of (bs, Abs (x, T, body)) = T --> typ_of ((x, T) :: bs, body)
wenzelm@4961
   406
      | typ_of (bs, t $ u) =
wenzelm@4961
   407
          let val T = typ_of (bs, t) and U = typ_of (bs, u) in
wenzelm@4961
   408
            (case T of
wenzelm@4961
   409
              Type ("fun", [T1, T2]) =>
wenzelm@14828
   410
                if T1 = U then T2 else err_appl "Incompatible operand type" bs t T u U
wenzelm@14828
   411
            | _ => err_appl "Operator not of function type" bs t T u U)
wenzelm@4961
   412
          end;
wenzelm@4961
   413
wenzelm@4961
   414
  in typ_of ([], tm) end;
clasohm@0
   415
wenzelm@14987
   416
in
wenzelm@14987
   417
wenzelm@16442
   418
fun certify_term pp thy tm =
wenzelm@251
   419
  let
wenzelm@16723
   420
    val _ = Context.check_thy thy;
wenzelm@14784
   421
wenzelm@16442
   422
    val tm' = map_term_types (certify_typ thy) tm;
wenzelm@14784
   423
    val tm' = if tm = tm' then tm else tm';  (*avoid copying of already normal term*)
wenzelm@14784
   424
wenzelm@14784
   425
    fun err msg = raise TYPE (msg, [], [tm']);
wenzelm@3969
   426
wenzelm@14828
   427
    fun show_const a T = quote a ^ " :: " ^ Pretty.string_of_typ pp T;
wenzelm@169
   428
wenzelm@14784
   429
    fun check_atoms (t $ u) = (check_atoms t; check_atoms u)
wenzelm@14784
   430
      | check_atoms (Abs (_, _, t)) = check_atoms t
wenzelm@14784
   431
      | check_atoms (Const (a, T)) =
wenzelm@16442
   432
          (case const_type thy a of
skalberg@15531
   433
            NONE => err ("Undeclared constant " ^ show_const a T)
skalberg@15531
   434
          | SOME U =>
wenzelm@16442
   435
              if typ_instance thy (T, U) then ()
wenzelm@14784
   436
              else err ("Illegal type for constant " ^ show_const a T))
wenzelm@14784
   437
      | check_atoms (Var ((x, i), _)) =
wenzelm@14784
   438
          if i < 0 then err ("Malformed variable: " ^ quote x) else ()
wenzelm@14784
   439
      | check_atoms _ = ();
wenzelm@14784
   440
  in
wenzelm@14784
   441
    check_atoms tm';
wenzelm@16337
   442
    (tm', type_check pp tm', maxidx_of_term tm')
wenzelm@14784
   443
  end;
wenzelm@251
   444
wenzelm@14987
   445
end;
wenzelm@14987
   446
wenzelm@16494
   447
fun certify_prop pp thy tm =
wenzelm@16494
   448
  let val res as (tm', T, _) = certify_term pp thy tm
wenzelm@16494
   449
  in if T <> propT then raise TYPE ("Term not of type prop", [T], [tm']) else res end;
wenzelm@16494
   450
wenzelm@16494
   451
fun cert_term thy tm = #1 (certify_term (pp thy) thy tm);
wenzelm@16494
   452
fun cert_prop thy tm = #1 (certify_prop (pp thy) thy tm);
wenzelm@16494
   453
wenzelm@251
   454
wenzelm@16337
   455
wenzelm@16442
   456
(** read and certify entities **)    (*exception ERROR*)
wenzelm@16337
   457
wenzelm@16337
   458
(* sorts *)
wenzelm@16337
   459
wenzelm@16442
   460
fun read_sort' syn thy str =
wenzelm@16337
   461
  let
wenzelm@16723
   462
    val _ = Context.check_thy thy;
wenzelm@16597
   463
    val S = intern_sort thy (Syntax.read_sort thy syn str);
wenzelm@16442
   464
  in certify_sort thy S handle TYPE (msg, _, _) => error msg end;
wenzelm@16337
   465
wenzelm@16442
   466
fun read_sort thy str = read_sort' (syn_of thy) thy str;
wenzelm@16337
   467
wenzelm@16337
   468
wenzelm@16337
   469
(* types *)
wenzelm@16337
   470
wenzelm@16337
   471
local
wenzelm@16337
   472
wenzelm@16442
   473
fun gen_read_typ' cert syn (thy, def_sort) str =
wenzelm@16337
   474
  let
wenzelm@16723
   475
    val _ = Context.check_thy thy;
wenzelm@16442
   476
    val get_sort = TypeInfer.get_sort (tsig_of thy) def_sort (intern_sort thy);
wenzelm@16597
   477
    val T = intern_tycons thy (Syntax.read_typ thy syn get_sort (intern_sort thy) str);
wenzelm@16442
   478
  in cert thy T handle TYPE (msg, _, _) => error msg end
wenzelm@16337
   479
  handle ERROR => error ("The error(s) above occurred in type " ^ quote str);
wenzelm@16337
   480
wenzelm@16442
   481
fun gen_read_typ cert (thy, def_sort) str =
wenzelm@16442
   482
  gen_read_typ' cert (syn_of thy) (thy, def_sort) str;
wenzelm@16337
   483
wenzelm@16337
   484
in
wenzelm@16337
   485
wenzelm@16442
   486
fun no_def_sort thy = (thy: theory, K NONE);
wenzelm@16337
   487
wenzelm@16442
   488
val read_typ'        = gen_read_typ' certify_typ;
wenzelm@16442
   489
val read_typ_syntax' = gen_read_typ' certify_typ_syntax;
wenzelm@16442
   490
val read_typ_abbrev' = gen_read_typ' certify_typ_abbrev;
wenzelm@16442
   491
val read_typ         = gen_read_typ certify_typ;
wenzelm@16442
   492
val read_typ_syntax  = gen_read_typ certify_typ_syntax;
wenzelm@16442
   493
val read_typ_abbrev  = gen_read_typ certify_typ_abbrev;
wenzelm@16337
   494
wenzelm@16337
   495
end;
wenzelm@16337
   496
wenzelm@16337
   497
wenzelm@16368
   498
(* type and constant names *)
wenzelm@15703
   499
wenzelm@16442
   500
fun read_tyname thy raw_c =
wenzelm@16442
   501
  let val c = intern_type thy raw_c in
wenzelm@16442
   502
    (case Symtab.lookup (#2 (#types (Type.rep_tsig (tsig_of thy))), c) of
wenzelm@15703
   503
      SOME (Type.LogicalType n, _) => Type (c, replicate n dummyT)
wenzelm@16368
   504
    | _ => error ("Undeclared type constructor: " ^ quote c))
wenzelm@15703
   505
  end;
wenzelm@15703
   506
wenzelm@16442
   507
fun read_const thy raw_c =
wenzelm@16368
   508
  let
wenzelm@16442
   509
    val c = intern_const thy raw_c;
wenzelm@16442
   510
    val _ = the_const_type thy c handle TYPE (msg, _, _) => error msg;
wenzelm@16368
   511
  in Const (c, dummyT) end;
wenzelm@15703
   512
wenzelm@15703
   513
wenzelm@251
   514
wenzelm@583
   515
(** infer_types **)         (*exception ERROR*)
wenzelm@251
   516
wenzelm@2979
   517
(*
wenzelm@16337
   518
  def_type: partial map from indexnames to types (constrains Frees and Vars)
wenzelm@16337
   519
  def_sort: partial map from indexnames to sorts (constrains TFrees and TVars)
wenzelm@2979
   520
  used: list of already used type variables
wenzelm@2979
   521
  freeze: if true then generated parameters are turned into TFrees, else TVars
wenzelm@4249
   522
wenzelm@4249
   523
  termss: lists of alternative parses (only one combination should be type-correct)
wenzelm@4249
   524
  typs: expected types
wenzelm@2979
   525
*)
wenzelm@2979
   526
wenzelm@16442
   527
fun infer_types_simult pp thy def_type def_sort used freeze args =
wenzelm@251
   528
  let
skalberg@15574
   529
    val termss = foldr multiply [[]] (map fst args);
wenzelm@4249
   530
    val typs =
wenzelm@16442
   531
      map (fn (_, T) => certify_typ thy T handle TYPE (msg, _, _) => error msg) args;
wenzelm@169
   532
wenzelm@16442
   533
    fun infer ts = OK (TypeInfer.infer_types (Syntax.pp_show_brackets pp) (tsig_of thy)
wenzelm@16941
   534
        (const_constraint thy) def_type def_sort (intern_const thy) (intern_tycons thy)
wenzelm@16442
   535
        (intern_sort thy) used freeze typs ts)
wenzelm@4249
   536
      handle TYPE (msg, _, _) => Error msg;
clasohm@623
   537
wenzelm@4249
   538
    val err_results = map infer termss;
skalberg@15570
   539
    val errs = List.mapPartial get_error err_results;
skalberg@15570
   540
    val results = List.mapPartial get_ok err_results;
wenzelm@4249
   541
wenzelm@14645
   542
    val ambiguity = length termss;
wenzelm@14645
   543
wenzelm@4249
   544
    fun ambig_msg () =
wenzelm@4249
   545
      if ambiguity > 1 andalso ambiguity <= ! Syntax.ambiguity_level
wenzelm@4249
   546
      then
wenzelm@4249
   547
        error_msg "Got more than one parse tree.\n\
wenzelm@3805
   548
          \Retry with smaller Syntax.ambiguity_level for more information."
nipkow@952
   549
      else ();
wenzelm@4249
   550
  in
wenzelm@4249
   551
    if null results then (ambig_msg (); error (cat_lines errs))
wenzelm@4249
   552
    else if length results = 1 then
wenzelm@4249
   553
      (if ambiguity > ! Syntax.ambiguity_level then
wenzelm@4249
   554
        warning "Fortunately, only one parse tree is type correct.\n\
wenzelm@4249
   555
          \You may still want to disambiguate your grammar or your input."
wenzelm@4249
   556
      else (); hd results)
wenzelm@4249
   557
    else (ambig_msg (); error ("More than one term is type correct:\n" ^
skalberg@15570
   558
      cat_lines (map (Pretty.string_of_term pp) (List.concat (map fst results)))))
wenzelm@4249
   559
  end;
clasohm@623
   560
wenzelm@16442
   561
fun infer_types pp thy def_type def_sort used freeze tsT =
wenzelm@16442
   562
  apfst hd (infer_types_simult pp thy def_type def_sort used freeze [tsT]);
wenzelm@251
   563
wenzelm@251
   564
wenzelm@16494
   565
(* read_def_terms -- read terms and infer types *)    (*exception ERROR*)
wenzelm@16337
   566
wenzelm@16442
   567
fun read_def_terms' pp is_logtype syn (thy, types, sorts) used freeze sTs =
wenzelm@8607
   568
  let
wenzelm@8607
   569
    fun read (s, T) =
wenzelm@16442
   570
      let val T' = certify_typ thy T handle TYPE (msg, _, _) => error msg
wenzelm@16597
   571
      in (Syntax.read thy is_logtype syn T' s, T') end;
wenzelm@16442
   572
  in infer_types_simult pp thy types sorts used freeze (map read sTs) end;
wenzelm@8607
   573
wenzelm@16442
   574
fun read_def_terms (thy, types, sorts) =
wenzelm@16442
   575
  read_def_terms' (pp thy) (is_logtype thy) (syn_of thy) (thy, types, sorts);
wenzelm@12068
   576
wenzelm@16442
   577
fun simple_read_term thy T s =
wenzelm@16442
   578
  let val ([t], _) = read_def_terms (thy, K NONE, K NONE) [] true [(s, T)]
wenzelm@16337
   579
  in t end
wenzelm@16337
   580
  handle ERROR => error ("The error(s) above occurred for term " ^ s);
wenzelm@8802
   581
wenzelm@16494
   582
fun read_term thy = simple_read_term thy TypeInfer.logicT;
wenzelm@16494
   583
fun read_prop thy = simple_read_term thy propT;
wenzelm@16494
   584
wenzelm@8607
   585
wenzelm@2979
   586
wenzelm@16337
   587
(** signature extension functions **)  (*exception ERROR/TYPE*)
wenzelm@386
   588
wenzelm@386
   589
(* add default sort *)
wenzelm@386
   590
wenzelm@16442
   591
fun gen_add_defsort prep_sort s thy =
wenzelm@16442
   592
  thy |> map_tsig (Type.set_defsort (prep_sort thy s));
wenzelm@8898
   593
wenzelm@16337
   594
val add_defsort = gen_add_defsort read_sort;
wenzelm@16337
   595
val add_defsort_i = gen_add_defsort certify_sort;
wenzelm@386
   596
wenzelm@386
   597
wenzelm@386
   598
(* add type constructors *)
wenzelm@386
   599
wenzelm@16442
   600
fun add_types types thy = thy |> map_sign (fn (naming, syn, tsig, consts) =>
wenzelm@14856
   601
  let
wenzelm@16597
   602
    val syn' = Syntax.extend_type_gram types syn;
wenzelm@16368
   603
    val decls = map (fn (a, n, mx) => (Syntax.type_name a mx, n)) types;
wenzelm@16442
   604
    val tsig' = Type.add_types naming decls tsig;
wenzelm@16442
   605
  in (naming, syn', tsig', consts) end);
wenzelm@16442
   606
wenzelm@16442
   607
fun add_typedecls decls thy =
wenzelm@16442
   608
  let
wenzelm@16442
   609
    fun type_of (a, vs, mx) =
wenzelm@16442
   610
      if null (duplicates vs) then (a, length vs, mx)
wenzelm@16442
   611
      else error ("Duplicate parameters in type declaration: " ^ quote a);
wenzelm@16442
   612
  in add_types (map type_of decls) thy end;
wenzelm@16337
   613
wenzelm@16337
   614
wenzelm@16337
   615
(* add nonterminals *)
wenzelm@16337
   616
wenzelm@16442
   617
fun add_nonterminals ns thy = thy |> map_sign (fn (naming, syn, tsig, consts) =>
wenzelm@16337
   618
  let
wenzelm@16597
   619
    val syn' = Syntax.extend_consts ns syn;
wenzelm@16442
   620
    val tsig' = Type.add_nonterminals naming ns tsig;
wenzelm@16442
   621
  in (naming, syn', tsig', consts) end);
wenzelm@386
   622
wenzelm@386
   623
wenzelm@386
   624
(* add type abbreviations *)
wenzelm@386
   625
wenzelm@16442
   626
fun gen_add_tyabbr prep_typ (a, vs, rhs, mx) thy =
wenzelm@16442
   627
  thy |> map_sign (fn (naming, syn, tsig, consts) =>
wenzelm@16337
   628
    let
wenzelm@16597
   629
      val syn' = Syntax.extend_type_gram [(a, length vs, mx)] syn;
wenzelm@16368
   630
      val a' = Syntax.type_name a mx;
wenzelm@16442
   631
      val abbr = (a', vs, prep_typ thy rhs)
wenzelm@16442
   632
        handle ERROR => error ("in type abbreviation " ^ quote a');
wenzelm@16442
   633
      val tsig' = Type.add_abbrevs naming [abbr] tsig;
wenzelm@16442
   634
    in (naming, syn', tsig', consts) end);
wenzelm@386
   635
wenzelm@16337
   636
val add_tyabbrs = fold (gen_add_tyabbr (read_typ_syntax o no_def_sort));
wenzelm@16337
   637
val add_tyabbrs_i = fold (gen_add_tyabbr certify_typ_syntax);
wenzelm@14784
   638
wenzelm@14784
   639
wenzelm@386
   640
(* add type arities *)
wenzelm@386
   641
wenzelm@16442
   642
fun gen_add_arities int_type prep_sort arities thy = thy |> map_tsig (fn tsig =>
wenzelm@16368
   643
  let
wenzelm@16442
   644
    fun prep_arity (a, Ss, S) = (int_type thy a, map (prep_sort thy) Ss, prep_sort thy S)
wenzelm@16368
   645
      handle ERROR => error ("in arity for type " ^ quote a);
wenzelm@16442
   646
    val tsig' = Type.add_arities (pp thy) (map prep_arity arities) tsig;
wenzelm@16368
   647
  in tsig' end);
wenzelm@386
   648
wenzelm@16368
   649
val add_arities = gen_add_arities intern_type read_sort;
wenzelm@16337
   650
val add_arities_i = gen_add_arities (K I) certify_sort;
wenzelm@8898
   651
wenzelm@386
   652
wenzelm@16337
   653
(* modify syntax *)
wenzelm@16337
   654
wenzelm@16442
   655
fun gen_syntax change_gram prep_typ prmode args thy =
wenzelm@16337
   656
  let
wenzelm@16442
   657
    fun prep (c, T, mx) = (c, prep_typ thy T, mx)
wenzelm@16442
   658
      handle ERROR => error ("in syntax declaration " ^ quote (Syntax.const_name c mx));
wenzelm@16597
   659
  in thy |> map_syn (change_gram (is_logtype thy) prmode (map prep args)) end;
wenzelm@16337
   660
wenzelm@16337
   661
fun gen_add_syntax x = gen_syntax Syntax.extend_const_gram x;
wenzelm@386
   662
wenzelm@16337
   663
val add_modesyntax = gen_add_syntax (read_typ_syntax o no_def_sort);
wenzelm@16337
   664
val add_modesyntax_i = gen_add_syntax certify_typ_syntax;
wenzelm@16442
   665
val add_syntax = add_modesyntax Syntax.default_mode;
wenzelm@16442
   666
val add_syntax_i = add_modesyntax_i Syntax.default_mode;
wenzelm@16337
   667
val del_modesyntax = gen_syntax Syntax.remove_const_gram (read_typ_syntax o no_def_sort);
wenzelm@16337
   668
val del_modesyntax_i = gen_syntax Syntax.remove_const_gram certify_typ_syntax;
wenzelm@3805
   669
wenzelm@16337
   670
wenzelm@16337
   671
(* add constants *)
wenzelm@386
   672
wenzelm@16442
   673
fun gen_add_consts prep_typ raw_args thy =
wenzelm@386
   674
  let
wenzelm@16988
   675
    val prepT = Compress.typ thy o Type.varifyT o Type.no_tvars o Term.no_dummyT o prep_typ thy;
wenzelm@16337
   676
    fun prep (c, T, mx) = ((c, prepT T, mx) handle TYPE (msg, _, _) => error msg)
wenzelm@16337
   677
      handle ERROR => error ("in declaration of constant " ^ quote (Syntax.const_name c mx));
wenzelm@16337
   678
    val args = map prep raw_args;
wenzelm@16368
   679
    val decls = args |> map (fn (c, T, mx) => (Syntax.const_name c mx, (T, stamp ())));
wenzelm@16442
   680
wenzelm@16442
   681
    fun extend_consts consts = NameSpace.extend_table (naming_of thy) (consts, decls)
wenzelm@16368
   682
      handle Symtab.DUPS cs => err_dup_consts cs;
wenzelm@16442
   683
  in
wenzelm@16442
   684
    thy
wenzelm@16941
   685
    |> map_consts (apfst extend_consts)
wenzelm@16442
   686
    |> add_syntax_i args
wenzelm@16442
   687
  end;
wenzelm@386
   688
wenzelm@16337
   689
val add_consts = gen_add_consts (read_typ o no_def_sort);
wenzelm@16337
   690
val add_consts_i = gen_add_consts certify_typ;
wenzelm@386
   691
wenzelm@386
   692
wenzelm@16941
   693
(* add constraints *)
wenzelm@16941
   694
wenzelm@16941
   695
fun gen_add_constraint int_const prep_typ (raw_c, raw_T) thy =
wenzelm@16941
   696
  let
wenzelm@16941
   697
    val c = int_const thy raw_c;
wenzelm@16941
   698
    val T = Term.zero_var_indexesT (Term.no_dummyT (prep_typ thy raw_T))
wenzelm@16941
   699
      handle TYPE (msg, _, _) => error msg;
wenzelm@16941
   700
    val _ = cert_term thy (Const (c, T))
wenzelm@16941
   701
      handle TYPE (msg, _, _) => error msg;
wenzelm@16941
   702
  in thy |> map_consts (apsnd (curry Symtab.update (c, T))) end;
wenzelm@16941
   703
wenzelm@16941
   704
val add_const_constraint = gen_add_constraint intern_const (read_typ o no_def_sort);
wenzelm@16941
   705
val add_const_constraint_i = gen_add_constraint (K I) certify_typ;
wenzelm@16941
   706
wenzelm@16941
   707
wenzelm@386
   708
(* add type classes *)
wenzelm@386
   709
wenzelm@16337
   710
val classN = "_class";
wenzelm@386
   711
wenzelm@16337
   712
val const_of_class = suffix classN;
wenzelm@16337
   713
fun class_of_const c = unsuffix classN c
wenzelm@16337
   714
  handle Fail _ => raise TERM ("class_of_const: bad name " ^ quote c, []);
wenzelm@386
   715
wenzelm@16442
   716
fun gen_add_class int_class (bclass, raw_classes) thy =
wenzelm@16442
   717
  thy |> map_sign (fn (naming, syn, tsig, consts) =>
wenzelm@16337
   718
    let
wenzelm@16442
   719
      val classes = map (int_class thy) raw_classes;
wenzelm@16597
   720
      val syn' = Syntax.extend_consts [bclass] syn;
wenzelm@16442
   721
      val tsig' = Type.add_classes (pp thy) naming [(bclass, classes)] tsig;
wenzelm@16442
   722
    in (naming, syn', tsig', consts) end)
wenzelm@16337
   723
  |> add_consts_i [(const_of_class bclass, a_itselfT --> propT, Syntax.NoSyn)];
wenzelm@3791
   724
wenzelm@16337
   725
val add_classes = fold (gen_add_class intern_class);
wenzelm@16337
   726
val add_classes_i = fold (gen_add_class (K I));
wenzelm@386
   727
wenzelm@386
   728
wenzelm@2963
   729
(* add to classrel *)
wenzelm@421
   730
wenzelm@16442
   731
fun gen_add_classrel int_class raw_pairs thy = thy |> map_tsig (fn tsig =>
wenzelm@16368
   732
  let
wenzelm@16442
   733
    val pairs = map (pairself (int_class thy)) raw_pairs;
wenzelm@16442
   734
    val tsig' = Type.add_classrel (pp thy) pairs tsig;
wenzelm@16368
   735
  in tsig' end);
wenzelm@16337
   736
wenzelm@16337
   737
val add_classrel = gen_add_classrel intern_class;
wenzelm@16337
   738
val add_classrel_i = gen_add_classrel (K I);
wenzelm@421
   739
wenzelm@421
   740
wenzelm@14645
   741
(* add translation functions *)
wenzelm@14645
   742
wenzelm@15746
   743
local
wenzelm@15746
   744
wenzelm@15746
   745
fun mk trs = map Syntax.mk_trfun trs;
wenzelm@15746
   746
wenzelm@16597
   747
fun gen_add_trfuns ext non_typed (atrs, trs, tr's, atr's) =
wenzelm@16597
   748
  map_syn (ext (mk atrs, mk trs, mk (map (apsnd non_typed) tr's), mk atr's));
wenzelm@14645
   749
wenzelm@16597
   750
fun gen_add_trfunsT ext tr's = map_syn (ext ([], [], mk tr's, []));
wenzelm@14645
   751
wenzelm@15746
   752
in
wenzelm@15746
   753
wenzelm@16597
   754
val add_trfuns = gen_add_trfuns Syntax.extend_trfuns Syntax.non_typed_tr';
wenzelm@16597
   755
val add_trfunsT = gen_add_trfunsT Syntax.extend_trfuns;
wenzelm@16597
   756
val add_advanced_trfuns = gen_add_trfuns Syntax.extend_advanced_trfuns Syntax.non_typed_tr'';
wenzelm@16597
   757
val add_advanced_trfunsT = gen_add_trfunsT Syntax.extend_advanced_trfuns;
wenzelm@14645
   758
wenzelm@15746
   759
end;
wenzelm@15746
   760
wenzelm@16597
   761
val add_tokentrfuns = map_syn o Syntax.extend_tokentrfuns;
wenzelm@16442
   762
fun add_mode_tokentrfuns m = add_tokentrfuns o map (fn (s, f) => (m, s, f));
wenzelm@14645
   763
wenzelm@14645
   764
wenzelm@4619
   765
(* add translation rules *)
wenzelm@4619
   766
wenzelm@16442
   767
fun add_trrules args thy = thy |> map_syn (fn syn =>
wenzelm@16442
   768
  let val rules = map (Syntax.map_trrule (apfst (intern_type thy))) args
wenzelm@16597
   769
  in Syntax.extend_trrules thy (is_logtype thy) syn rules syn end);
wenzelm@8725
   770
wenzelm@16597
   771
val add_trrules_i = map_syn o Syntax.extend_trrules_i;
wenzelm@386
   772
wenzelm@386
   773
wenzelm@16337
   774
(* modify naming *)
wenzelm@6546
   775
wenzelm@16337
   776
val add_path        = map_naming o NameSpace.add_path;
wenzelm@16337
   777
val qualified_names = map_naming NameSpace.qualified_names;
wenzelm@16337
   778
val no_base_names   = map_naming NameSpace.no_base_names;
wenzelm@16337
   779
val custom_accesses = map_naming o NameSpace.custom_accesses;
wenzelm@16337
   780
val set_policy      = map_naming o NameSpace.set_policy;
wenzelm@16337
   781
val restore_naming  = map_naming o K o naming_of;
wenzelm@6546
   782
wenzelm@16442
   783
val parent_path     = add_path "..";
wenzelm@16442
   784
val root_path       = add_path "/";
wenzelm@16442
   785
val absolute_path   = add_path "//";
wenzelm@16442
   786
wenzelm@16442
   787
fun local_path thy = thy |> root_path |> add_path (Context.theory_name thy);
wenzelm@16442
   788
wenzelm@6546
   789
wenzelm@16337
   790
(* hide names *)
wenzelm@386
   791
wenzelm@16442
   792
fun hide_classes b xs thy = thy |> map_tsig (Type.hide_classes b (map (intern_class thy) xs));
wenzelm@16368
   793
val hide_classes_i = map_tsig oo Type.hide_classes;
wenzelm@16442
   794
fun hide_types b xs thy = thy |> map_tsig (Type.hide_types b (map (intern_type thy) xs));
wenzelm@16368
   795
val hide_types_i = map_tsig oo Type.hide_types;
wenzelm@16442
   796
fun hide_consts b xs thy =
wenzelm@16941
   797
  thy |> map_consts (apfst (apfst (fold (NameSpace.hide b o intern_const thy) xs)));
wenzelm@16941
   798
val hide_consts_i = (map_consts o apfst o apfst) oo (fold o NameSpace.hide);
wenzelm@386
   799
clasohm@0
   800
end;