src/Pure/sign.ML
author wenzelm
Tue Apr 15 18:49:22 2008 +0200 (2008-04-15)
changeset 26667 660b69b3c28a
parent 26637 0bfccafc52eb
child 26701 341c4d51d1c2
permissions -rw-r--r--
removed obsolete SIGN_THEORY -- no name aliases in structure Theory;
simplified hide_XXX interfaces;
moved hide_names to isar_cmd.ML;
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@18062
     6
signature, polymorphic constants.
clasohm@0
     7
*)
clasohm@0
     8
wenzelm@16442
     9
signature SIGN =
wenzelm@5642
    10
sig
wenzelm@16442
    11
  val rep_sg: theory ->
wenzelm@16442
    12
   {naming: NameSpace.naming,
wenzelm@16597
    13
    syn: Syntax.syntax,
wenzelm@16442
    14
    tsig: Type.tsig,
wenzelm@18062
    15
    consts: Consts.T}
wenzelm@16442
    16
  val naming_of: theory -> NameSpace.naming
wenzelm@16442
    17
  val base_name: string -> bstring
wenzelm@16442
    18
  val full_name: theory -> bstring -> string
wenzelm@16442
    19
  val full_name_path: theory -> string -> bstring -> string
wenzelm@16442
    20
  val declare_name: theory -> string -> NameSpace.T -> NameSpace.T
wenzelm@16442
    21
  val syn_of: theory -> Syntax.syntax
wenzelm@16442
    22
  val tsig_of: theory -> Type.tsig
wenzelm@19642
    23
  val classes_of: theory -> Sorts.algebra
wenzelm@21932
    24
  val all_classes: theory -> class list
wenzelm@19407
    25
  val super_classes: theory -> class -> class list
wenzelm@24732
    26
  val minimize_sort: theory -> sort -> sort
wenzelm@24732
    27
  val complete_sort: theory -> sort -> sort
wenzelm@16442
    28
  val defaultS: theory -> sort
wenzelm@16442
    29
  val subsort: theory -> sort * sort -> bool
wenzelm@16442
    30
  val of_sort: theory -> typ * sort -> bool
wenzelm@16442
    31
  val witness_sorts: theory -> sort list -> sort list -> (typ * sort) list
wenzelm@24732
    32
  val is_logtype: theory -> string -> bool
wenzelm@16442
    33
  val typ_instance: theory -> typ * typ -> bool
wenzelm@19427
    34
  val typ_equiv: theory -> typ * typ -> bool
wenzelm@16941
    35
  val typ_match: theory -> typ * typ -> Type.tyenv -> Type.tyenv
wenzelm@16941
    36
  val typ_unify: theory -> typ * typ -> Type.tyenv * int -> Type.tyenv * int
wenzelm@18967
    37
  val consts_of: theory -> Consts.T
haftmann@17037
    38
  val the_const_constraint: theory -> string -> typ
wenzelm@16442
    39
  val const_type: theory -> string -> typ option
wenzelm@16442
    40
  val the_const_type: theory -> string -> typ
wenzelm@16442
    41
  val declared_tyname: theory -> string -> bool
wenzelm@16442
    42
  val declared_const: theory -> string -> bool
wenzelm@18062
    43
  val const_monomorphic: theory -> string -> bool
wenzelm@21183
    44
  val const_syntax_name: theory -> string -> string
wenzelm@18146
    45
  val const_typargs: theory -> string * typ -> typ list
wenzelm@18164
    46
  val const_instance: theory -> string * typ list -> typ
haftmann@26268
    47
  val mk_const: theory -> string * typ list -> term
wenzelm@16442
    48
  val class_space: theory -> NameSpace.T
wenzelm@16442
    49
  val type_space: theory -> NameSpace.T
wenzelm@16442
    50
  val const_space: theory -> NameSpace.T
wenzelm@16442
    51
  val intern_class: theory -> xstring -> string
wenzelm@16442
    52
  val extern_class: theory -> string -> xstring
wenzelm@16442
    53
  val intern_type: theory -> xstring -> string
wenzelm@16442
    54
  val extern_type: theory -> string -> xstring
wenzelm@16442
    55
  val intern_const: theory -> xstring -> string
wenzelm@16442
    56
  val extern_const: theory -> string -> xstring
wenzelm@16442
    57
  val intern_sort: theory -> sort -> sort
wenzelm@16442
    58
  val extern_sort: theory -> sort -> sort
wenzelm@16442
    59
  val intern_typ: theory -> typ -> typ
wenzelm@16442
    60
  val extern_typ: theory -> typ -> typ
wenzelm@16442
    61
  val intern_term: theory -> term -> term
wenzelm@18994
    62
  val extern_term: (string -> xstring) -> theory -> term -> term
wenzelm@16442
    63
  val intern_tycons: theory -> typ -> typ
wenzelm@16442
    64
  val pretty_term: theory -> term -> Pretty.T
wenzelm@16442
    65
  val pretty_typ: theory -> typ -> Pretty.T
wenzelm@16442
    66
  val pretty_sort: theory -> sort -> Pretty.T
wenzelm@16442
    67
  val string_of_term: theory -> term -> string
wenzelm@16442
    68
  val string_of_typ: theory -> typ -> string
wenzelm@16442
    69
  val string_of_sort: theory -> sort -> string
wenzelm@16442
    70
  val pp: theory -> Pretty.pp
wenzelm@19462
    71
  val arity_number: theory -> string -> int
wenzelm@19462
    72
  val arity_sorts: theory -> string -> sort -> sort list
wenzelm@16442
    73
  val certify_class: theory -> class -> class
wenzelm@16442
    74
  val certify_sort: theory -> sort -> sort
wenzelm@16442
    75
  val certify_typ: theory -> typ -> typ
wenzelm@24273
    76
  val certify_typ_mode: Type.mode -> theory -> typ -> typ
wenzelm@24674
    77
  val certify': bool -> Pretty.pp -> bool -> Consts.T -> theory -> term -> term * typ * int
wenzelm@18967
    78
  val certify_term: theory -> term -> term * typ * int
wenzelm@18967
    79
  val certify_prop: theory -> term -> term * typ * int
wenzelm@16494
    80
  val cert_term: theory -> term -> term
wenzelm@16494
    81
  val cert_prop: theory -> term -> term
wenzelm@21741
    82
  val no_frees: Pretty.pp -> term -> term
wenzelm@18941
    83
  val no_vars: Pretty.pp -> term -> term
wenzelm@24981
    84
  val cert_def: Proof.context -> term -> (string * typ) * term
wenzelm@19244
    85
  val read_class: theory -> xstring -> class
wenzelm@19244
    86
  val read_arity: theory -> xstring * string list * string -> arity
wenzelm@19244
    87
  val cert_arity: theory -> arity -> arity
wenzelm@22765
    88
  val get_sort: theory ->
wenzelm@22765
    89
    (indexname -> sort option) -> (indexname * sort) list -> indexname -> sort
wenzelm@22683
    90
  val read_def_typ: theory * (indexname -> sort option) -> string -> typ
wenzelm@22683
    91
  val read_typ: theory -> string -> typ
wenzelm@22683
    92
  val read_typ_syntax: theory -> string -> typ
wenzelm@22683
    93
  val read_typ_abbrev: theory -> string -> typ
wenzelm@16442
    94
  val read_def_terms:
wenzelm@16442
    95
    theory * (indexname -> typ option) * (indexname -> sort option) ->
wenzelm@16442
    96
    string list -> bool -> (string * typ) list -> term list * (indexname * typ) list
wenzelm@16442
    97
  val simple_read_term: theory -> typ -> string -> term
wenzelm@16494
    98
  val read_term: theory -> string -> term
wenzelm@16494
    99
  val read_prop: theory -> string -> term
wenzelm@26667
   100
  val add_defsort: string -> theory -> theory
wenzelm@26667
   101
  val add_defsort_i: sort -> theory -> theory
wenzelm@26667
   102
  val add_types: (bstring * int * mixfix) list -> theory -> theory
wenzelm@26667
   103
  val add_nonterminals: bstring list -> theory -> theory
wenzelm@26667
   104
  val add_tyabbrs: (bstring * string list * string * mixfix) list -> theory -> theory
wenzelm@26667
   105
  val add_tyabbrs_i: (bstring * string list * typ * mixfix) list -> theory -> theory
wenzelm@26667
   106
  val add_syntax: (bstring * string * mixfix) list -> theory -> theory
wenzelm@26667
   107
  val add_syntax_i: (bstring * typ * mixfix) list -> theory -> theory
wenzelm@26667
   108
  val add_modesyntax: Syntax.mode -> (bstring * string * mixfix) list -> theory -> theory
wenzelm@26667
   109
  val add_modesyntax_i: Syntax.mode -> (bstring * typ * mixfix) list -> theory -> theory
wenzelm@26667
   110
  val del_modesyntax: Syntax.mode -> (bstring * string * mixfix) list -> theory -> theory
wenzelm@26667
   111
  val del_modesyntax_i: Syntax.mode -> (bstring * typ * mixfix) list -> theory -> theory
wenzelm@26667
   112
  val notation: bool -> Syntax.mode -> (term * mixfix) list -> theory -> theory
wenzelm@26667
   113
  val add_consts: (bstring * string * mixfix) list -> theory -> theory
wenzelm@26667
   114
  val add_consts_i: (bstring * typ * mixfix) list -> theory -> theory
wenzelm@24973
   115
  val declare_const: Markup.property list -> bstring * typ * mixfix -> theory -> term * theory
wenzelm@24776
   116
  val add_abbrev: string -> Markup.property list ->
wenzelm@24776
   117
    bstring * term -> theory -> (term * term) * theory
wenzelm@25049
   118
  val revert_abbrev: string -> string -> theory -> theory
wenzelm@24761
   119
  val add_const_constraint: string * typ option -> theory -> theory
wenzelm@22811
   120
  val primitive_class: string * class list -> theory -> theory
wenzelm@22811
   121
  val primitive_classrel: class * class -> theory -> theory
wenzelm@22811
   122
  val primitive_arity: arity -> theory -> theory
wenzelm@26667
   123
  val add_trfuns:
wenzelm@26667
   124
    (string * (ast list -> ast)) list *
wenzelm@26667
   125
    (string * (term list -> term)) list *
wenzelm@26667
   126
    (string * (term list -> term)) list *
wenzelm@26667
   127
    (string * (ast list -> ast)) list -> theory -> theory
wenzelm@26667
   128
  val add_trfunsT:
wenzelm@26667
   129
    (string * (bool -> typ -> term list -> term)) list -> theory -> theory
wenzelm@26667
   130
  val add_advanced_trfuns:
wenzelm@26667
   131
    (string * (Proof.context -> ast list -> ast)) list *
wenzelm@26667
   132
    (string * (Proof.context -> term list -> term)) list *
wenzelm@26667
   133
    (string * (Proof.context -> term list -> term)) list *
wenzelm@26667
   134
    (string * (Proof.context -> ast list -> ast)) list -> theory -> theory
wenzelm@26667
   135
  val add_advanced_trfunsT:
wenzelm@26667
   136
    (string * (Proof.context -> bool -> typ -> term list -> term)) list -> theory -> theory
wenzelm@26667
   137
  val add_tokentrfuns:
wenzelm@26667
   138
    (string * string * (string -> output * int)) list -> theory -> theory
wenzelm@26667
   139
  val add_mode_tokentrfuns: string -> (string * (string -> output * int)) list
wenzelm@26667
   140
    -> theory -> theory
wenzelm@26667
   141
  val add_trrules: (xstring * string) Syntax.trrule list -> theory -> theory
wenzelm@26667
   142
  val del_trrules: (xstring * string) Syntax.trrule list -> theory -> theory
wenzelm@26667
   143
  val add_trrules_i: ast Syntax.trrule list -> theory -> theory
wenzelm@26667
   144
  val del_trrules_i: ast Syntax.trrule list -> theory -> theory
wenzelm@26667
   145
  val add_path: string -> theory -> theory
wenzelm@26667
   146
  val parent_path: theory -> theory
wenzelm@26667
   147
  val root_path: theory -> theory
wenzelm@26667
   148
  val absolute_path: theory -> theory
wenzelm@26667
   149
  val local_path: theory -> theory
wenzelm@26667
   150
  val no_base_names: theory -> theory
wenzelm@26667
   151
  val qualified_names: theory -> theory
wenzelm@26667
   152
  val sticky_prefix: string -> theory -> theory
wenzelm@26667
   153
  val restore_naming: theory -> theory -> theory
wenzelm@26667
   154
  val hide_class: bool -> string -> theory -> theory
wenzelm@26667
   155
  val hide_type: bool -> string -> theory -> theory
wenzelm@26667
   156
  val hide_const: bool -> string -> theory -> theory
wenzelm@16442
   157
end
wenzelm@5642
   158
wenzelm@16442
   159
structure Sign: SIGN =
wenzelm@143
   160
struct
clasohm@0
   161
wenzelm@16442
   162
(** datatype sign **)
wenzelm@16337
   163
wenzelm@16442
   164
datatype sign = Sign of
wenzelm@18062
   165
 {naming: NameSpace.naming,     (*common naming conventions*)
wenzelm@18062
   166
  syn: Syntax.syntax,           (*concrete syntax for terms, types, sorts*)
wenzelm@18062
   167
  tsig: Type.tsig,              (*order-sorted signature of types*)
wenzelm@18062
   168
  consts: Consts.T};            (*polymorphic constants*)
wenzelm@16442
   169
wenzelm@16442
   170
fun make_sign (naming, syn, tsig, consts) =
wenzelm@16442
   171
  Sign {naming = naming, syn = syn, tsig = tsig, consts = consts};
wenzelm@16442
   172
wenzelm@16442
   173
structure SignData = TheoryDataFun
wenzelm@22846
   174
(
wenzelm@16442
   175
  type T = sign;
wenzelm@16442
   176
  val copy = I;
wenzelm@17405
   177
  fun extend (Sign {syn, tsig, consts, ...}) =
wenzelm@17405
   178
    make_sign (NameSpace.default_naming, syn, tsig, consts);
wenzelm@16442
   179
wenzelm@18062
   180
  val empty =
wenzelm@18714
   181
    make_sign (NameSpace.default_naming, Syntax.basic_syn, Type.empty_tsig, Consts.empty);
wenzelm@16442
   182
wenzelm@16442
   183
  fun merge pp (sign1, sign2) =
wenzelm@16442
   184
    let
wenzelm@18062
   185
      val Sign {naming = _, syn = syn1, tsig = tsig1, consts = consts1} = sign1;
wenzelm@18062
   186
      val Sign {naming = _, syn = syn2, tsig = tsig2, consts = consts2} = sign2;
wenzelm@16442
   187
wenzelm@16442
   188
      val naming = NameSpace.default_naming;
wenzelm@16597
   189
      val syn = Syntax.merge_syntaxes syn1 syn2;
wenzelm@16442
   190
      val tsig = Type.merge_tsigs pp (tsig1, tsig2);
wenzelm@18062
   191
      val consts = Consts.merge (consts1, consts2);
wenzelm@18062
   192
    in make_sign (naming, syn, tsig, consts) end;
wenzelm@22846
   193
);
wenzelm@16442
   194
wenzelm@16442
   195
fun rep_sg thy = SignData.get thy |> (fn Sign args => args);
wenzelm@16442
   196
wenzelm@16442
   197
fun map_sign f = SignData.map (fn Sign {naming, syn, tsig, consts} =>
wenzelm@16442
   198
  make_sign (f (naming, syn, tsig, consts)));
wenzelm@16442
   199
wenzelm@16442
   200
fun map_naming f = map_sign (fn (naming, syn, tsig, consts) => (f naming, syn, tsig, consts));
wenzelm@16442
   201
fun map_syn f = map_sign (fn (naming, syn, tsig, consts) => (naming, f syn, tsig, consts));
wenzelm@16442
   202
fun map_tsig f = map_sign (fn (naming, syn, tsig, consts) => (naming, syn, f tsig, consts));
wenzelm@16442
   203
fun map_consts f = map_sign (fn (naming, syn, tsig, consts) => (naming, syn, tsig, f consts));
wenzelm@16442
   204
wenzelm@16337
   205
wenzelm@16337
   206
(* naming *)
wenzelm@16337
   207
wenzelm@16442
   208
val naming_of = #naming o rep_sg;
wenzelm@16337
   209
val base_name = NameSpace.base;
wenzelm@16337
   210
val full_name = NameSpace.full o naming_of;
wenzelm@16442
   211
fun full_name_path thy elems = NameSpace.full (NameSpace.add_path elems (naming_of thy));
wenzelm@16337
   212
val declare_name = NameSpace.declare o naming_of;
wenzelm@14645
   213
wenzelm@14645
   214
wenzelm@14645
   215
(* syntax *)
wenzelm@14645
   216
wenzelm@16597
   217
val syn_of = #syn o rep_sg;
wenzelm@14645
   218
clasohm@0
   219
wenzelm@16337
   220
(* type signature *)
wenzelm@16337
   221
wenzelm@16337
   222
val tsig_of = #tsig o rep_sg;
wenzelm@24732
   223
wenzelm@19642
   224
val classes_of = #2 o #classes o Type.rep_tsig o tsig_of;
wenzelm@21932
   225
val all_classes = Sorts.all_classes o classes_of;
wenzelm@19642
   226
val super_classes = Sorts.super_classes o classes_of;
wenzelm@24732
   227
val minimize_sort = Sorts.minimize_sort o classes_of;
wenzelm@24732
   228
val complete_sort = Sorts.complete_sort o classes_of;
wenzelm@24732
   229
wenzelm@4844
   230
val defaultS = Type.defaultS o tsig_of;
wenzelm@4568
   231
val subsort = Type.subsort o tsig_of;
wenzelm@7640
   232
val of_sort = Type.of_sort o tsig_of;
wenzelm@7640
   233
val witness_sorts = Type.witness_sorts o tsig_of;
wenzelm@24732
   234
val is_logtype = member (op =) o Type.logical_types o tsig_of;
wenzelm@24732
   235
wenzelm@14784
   236
val typ_instance = Type.typ_instance o tsig_of;
wenzelm@19427
   237
fun typ_equiv thy (T, U) = typ_instance thy (T, U) andalso typ_instance thy (U, T);
wenzelm@16941
   238
val typ_match = Type.typ_match o tsig_of;
wenzelm@16941
   239
val typ_unify = Type.unify o tsig_of;
wenzelm@4256
   240
wenzelm@4256
   241
wenzelm@18062
   242
(* polymorphic constants *)
wenzelm@16941
   243
wenzelm@18062
   244
val consts_of = #consts o rep_sg;
wenzelm@21722
   245
val the_const_constraint = Consts.the_constraint o consts_of;
wenzelm@25042
   246
val the_const_type = Consts.the_type o consts_of;
wenzelm@18062
   247
val const_type = try o the_const_type;
wenzelm@21722
   248
val const_monomorphic = Consts.is_monomorphic o consts_of;
wenzelm@21183
   249
val const_syntax_name = Consts.syntax_name o consts_of;
wenzelm@18062
   250
val const_typargs = Consts.typargs o consts_of;
wenzelm@18164
   251
val const_instance = Consts.instance o consts_of;
wenzelm@4256
   252
haftmann@26268
   253
fun mk_const thy (c, Ts) = Const (c, const_instance thy (c, Ts));
haftmann@26268
   254
wenzelm@16894
   255
val declared_tyname = Symtab.defined o #2 o #types o Type.rep_tsig o tsig_of;
wenzelm@24761
   256
val declared_const = can o the_const_constraint;
wenzelm@620
   257
wenzelm@402
   258
clasohm@0
   259
wenzelm@16337
   260
(** intern / extern names **)
wenzelm@16337
   261
wenzelm@16368
   262
val class_space = #1 o #classes o Type.rep_tsig o tsig_of;
wenzelm@16368
   263
val type_space = #1 o #types o Type.rep_tsig o tsig_of;
wenzelm@18967
   264
val const_space = Consts.space_of o consts_of;
wenzelm@14645
   265
wenzelm@16368
   266
val intern_class = NameSpace.intern o class_space;
wenzelm@16368
   267
val extern_class = NameSpace.extern o class_space;
wenzelm@16368
   268
val intern_type = NameSpace.intern o type_space;
wenzelm@16368
   269
val extern_type = NameSpace.extern o type_space;
wenzelm@16368
   270
val intern_const = NameSpace.intern o const_space;
wenzelm@16368
   271
val extern_const = NameSpace.extern o const_space;
wenzelm@16337
   272
wenzelm@16337
   273
val intern_sort = map o intern_class;
wenzelm@16337
   274
val extern_sort = map o extern_class;
wenzelm@16337
   275
wenzelm@16337
   276
local
wenzelm@14645
   277
wenzelm@18892
   278
fun map_typ f g (Type (c, Ts)) = Type (g c, map (map_typ f g) Ts)
wenzelm@18892
   279
  | map_typ f _ (TFree (x, S)) = TFree (x, map f S)
wenzelm@18892
   280
  | map_typ f _ (TVar (xi, S)) = TVar (xi, map f S);
wenzelm@18892
   281
wenzelm@18892
   282
fun map_term f g h (Const (c, T)) = Const (h c, map_typ f g T)
wenzelm@18892
   283
  | map_term f g _ (Free (x, T)) = Free (x, map_typ f g T)
wenzelm@18892
   284
  | map_term f g _ (Var (xi, T)) = Var (xi, map_typ f g T)
wenzelm@18892
   285
  | map_term _ _ _ (t as Bound _) = t
wenzelm@18892
   286
  | map_term f g h (Abs (x, T, t)) = Abs (x, map_typ f g T, map_term f g h t)
wenzelm@18892
   287
  | map_term f g h (t $ u) = map_term f g h t $ map_term f g h u;
wenzelm@18892
   288
wenzelm@18994
   289
val add_classesT = Term.fold_atyps
wenzelm@18994
   290
  (fn TFree (_, S) => fold (insert (op =)) S
wenzelm@18994
   291
    | TVar (_, S) => fold (insert (op =)) S
wenzelm@18994
   292
    | _ => I);
wenzelm@18994
   293
wenzelm@18994
   294
fun add_tyconsT (Type (c, Ts)) = insert (op =) c #> fold add_tyconsT Ts
wenzelm@18994
   295
  | add_tyconsT _ = I;
wenzelm@18994
   296
wenzelm@18994
   297
val add_consts = Term.fold_aterms (fn Const (c, _) => insert (op =) c | _ => I);
wenzelm@18994
   298
wenzelm@16337
   299
fun mapping add_names f t =
wenzelm@16337
   300
  let
wenzelm@20664
   301
    fun f' (x: string) = let val y = f x in if x = y then NONE else SOME (x, y) end;
wenzelm@19482
   302
    val tab = map_filter f' (add_names t []);
wenzelm@18941
   303
    fun get x = the_default x (AList.lookup (op =) tab x);
wenzelm@16337
   304
  in get end;
wenzelm@16337
   305
wenzelm@16442
   306
fun typ_mapping f g thy T =
wenzelm@18892
   307
  T |> map_typ
wenzelm@18994
   308
    (mapping add_classesT (f thy) T)
wenzelm@18994
   309
    (mapping add_tyconsT (g thy) T);
wenzelm@14645
   310
wenzelm@16442
   311
fun term_mapping f g h thy t =
wenzelm@18892
   312
  t |> map_term
wenzelm@18994
   313
    (mapping (Term.fold_types add_classesT) (f thy) t)
wenzelm@18994
   314
    (mapping (Term.fold_types add_tyconsT) (g thy) t)
wenzelm@18994
   315
    (mapping add_consts (h thy) t);
wenzelm@16337
   316
wenzelm@16337
   317
in
wenzelm@14645
   318
wenzelm@16368
   319
val intern_typ = typ_mapping intern_class intern_type;
wenzelm@16368
   320
val extern_typ = typ_mapping extern_class extern_type;
wenzelm@16368
   321
val intern_term = term_mapping intern_class intern_type intern_const;
wenzelm@18994
   322
fun extern_term h = term_mapping extern_class extern_type (K h);
wenzelm@16368
   323
val intern_tycons = typ_mapping (K I) intern_type;
wenzelm@16337
   324
wenzelm@16337
   325
end;
wenzelm@14645
   326
wenzelm@14645
   327
wenzelm@14645
   328
wenzelm@4249
   329
(** pretty printing of terms, types etc. **)
wenzelm@3937
   330
wenzelm@24921
   331
val pretty_term = Syntax.pretty_term o ProofContext.init;
wenzelm@24921
   332
val pretty_typ = Syntax.pretty_typ o ProofContext.init;
wenzelm@24921
   333
val pretty_sort = Syntax.pretty_sort o ProofContext.init;
wenzelm@18857
   334
wenzelm@24921
   335
val string_of_term = Syntax.string_of_term o ProofContext.init;
wenzelm@24921
   336
val string_of_typ = Syntax.string_of_typ o ProofContext.init;
wenzelm@24921
   337
val string_of_sort = Syntax.string_of_sort o ProofContext.init;
wenzelm@3937
   338
wenzelm@24921
   339
(*pp operations -- deferred evaluation*)
wenzelm@24921
   340
fun pp thy = Pretty.pp
wenzelm@24921
   341
 (fn x => pretty_term thy x,
wenzelm@24921
   342
  fn x => pretty_typ thy x,
wenzelm@24921
   343
  fn x => pretty_sort thy x,
wenzelm@24921
   344
  fn x => Syntax.pretty_classrel (ProofContext.init thy) x,
wenzelm@24921
   345
  fn x => Syntax.pretty_arity (ProofContext.init thy) x);
wenzelm@14828
   346
wenzelm@3937
   347
wenzelm@3937
   348
wenzelm@16337
   349
(** certify entities **)    (*exception TYPE*)
wenzelm@8898
   350
wenzelm@16337
   351
(* certify wrt. type signature *)
wenzelm@8898
   352
wenzelm@19462
   353
val arity_number = Type.arity_number o tsig_of;
wenzelm@19462
   354
fun arity_sorts thy = Type.arity_sorts (pp thy) (tsig_of thy);
wenzelm@19462
   355
wenzelm@24273
   356
val certify_class         = Type.cert_class o tsig_of;
wenzelm@24273
   357
val certify_sort          = Type.cert_sort o tsig_of;
wenzelm@24273
   358
val certify_typ           = Type.cert_typ o tsig_of;
wenzelm@24273
   359
fun certify_typ_mode mode = Type.cert_typ_mode mode o tsig_of;
wenzelm@10443
   360
wenzelm@4961
   361
wenzelm@18967
   362
(* certify term/prop *)
wenzelm@4961
   363
wenzelm@14987
   364
local
nipkow@1494
   365
wenzelm@16337
   366
fun type_check pp tm =
wenzelm@4961
   367
  let
wenzelm@4961
   368
    fun err_appl why bs t T u U =
wenzelm@4961
   369
      let
wenzelm@10404
   370
        val xs = map Free bs;           (*we do not rename here*)
wenzelm@4961
   371
        val t' = subst_bounds (xs, t);
wenzelm@4961
   372
        val u' = subst_bounds (xs, u);
wenzelm@16337
   373
        val msg = cat_lines
wenzelm@14828
   374
          (TypeInfer.appl_error (Syntax.pp_show_brackets pp) why t' T u' U);
wenzelm@16337
   375
      in raise TYPE (msg, [T, U], [t', u']) end;
wenzelm@4961
   376
wenzelm@4961
   377
    fun typ_of (_, Const (_, T)) = T
wenzelm@4961
   378
      | typ_of (_, Free  (_, T)) = T
wenzelm@4961
   379
      | typ_of (_, Var (_, T)) = T
skalberg@15570
   380
      | typ_of (bs, Bound i) = snd (List.nth (bs, i) handle Subscript =>
wenzelm@4961
   381
          raise TYPE ("Loose bound variable: B." ^ string_of_int i, [], [Bound i]))
wenzelm@4961
   382
      | typ_of (bs, Abs (x, T, body)) = T --> typ_of ((x, T) :: bs, body)
wenzelm@4961
   383
      | typ_of (bs, t $ u) =
wenzelm@4961
   384
          let val T = typ_of (bs, t) and U = typ_of (bs, u) in
wenzelm@4961
   385
            (case T of
wenzelm@4961
   386
              Type ("fun", [T1, T2]) =>
wenzelm@14828
   387
                if T1 = U then T2 else err_appl "Incompatible operand type" bs t T u U
wenzelm@14828
   388
            | _ => err_appl "Operator not of function type" bs t T u U)
wenzelm@4961
   389
          end;
wenzelm@18967
   390
  in typ_of ([], tm) end;
wenzelm@4961
   391
wenzelm@18967
   392
fun err msg = raise TYPE (msg, [], []);
wenzelm@18967
   393
wenzelm@18967
   394
fun check_vars (t $ u) = (check_vars t; check_vars u)
wenzelm@18967
   395
  | check_vars (Abs (_, _, t)) = check_vars t
wenzelm@18967
   396
  | check_vars (Var (xi as (_, i), _)) =
wenzelm@18967
   397
      if i < 0 then err ("Malformed variable: " ^ quote (Term.string_of_vname xi)) else ()
wenzelm@18967
   398
  | check_vars _ = ();
clasohm@0
   399
wenzelm@14987
   400
in
wenzelm@14987
   401
wenzelm@24674
   402
fun certify' prop pp do_expand consts thy tm =
wenzelm@251
   403
  let
wenzelm@18967
   404
    val _ = check_vars tm;
wenzelm@20548
   405
    val tm' = Term.map_types (certify_typ thy) tm;
wenzelm@18967
   406
    val T = type_check pp tm';
wenzelm@18967
   407
    val _ = if prop andalso T <> propT then err "Term not of type prop" else ();
wenzelm@24674
   408
    val tm'' = Consts.certify pp (tsig_of thy) do_expand consts tm';
wenzelm@18967
   409
  in (if tm = tm'' then tm else tm'', T, Term.maxidx_of_term tm'') end;
wenzelm@169
   410
wenzelm@24674
   411
fun certify_term thy = certify' false (pp thy) true (consts_of thy) thy;
wenzelm@24674
   412
fun certify_prop thy = certify' true (pp thy) true (consts_of thy) thy;
wenzelm@18967
   413
wenzelm@24674
   414
fun cert_term_abbrev thy = #1 o certify' false (pp thy) false (consts_of thy) thy;
wenzelm@18967
   415
val cert_term = #1 oo certify_term;
wenzelm@18967
   416
val cert_prop = #1 oo certify_prop;
wenzelm@251
   417
wenzelm@14987
   418
end;
wenzelm@14987
   419
wenzelm@251
   420
wenzelm@18941
   421
(* specifications *)
wenzelm@18941
   422
wenzelm@21741
   423
fun no_variables kind add addT mk mkT pp tm =
wenzelm@21741
   424
  (case (add tm [], addT tm []) of
wenzelm@18941
   425
    ([], []) => tm
wenzelm@25117
   426
  | (frees, tfrees) => error (Pretty.string_of (Pretty.block
wenzelm@25117
   427
      (Pretty.str ("Illegal " ^ kind ^ " variable(s) in term:") :: Pretty.brk 1 ::
wenzelm@25117
   428
       Pretty.commas (map (Pretty.term pp o mk) frees @ map (Pretty.typ pp o mkT) tfrees)))));
wenzelm@21741
   429
wenzelm@21741
   430
val no_frees = no_variables "free" Term.add_frees Term.add_tfrees Free TFree;
wenzelm@21741
   431
val no_vars = no_variables "schematic" Term.add_vars Term.add_tvars Var TVar;
wenzelm@18941
   432
wenzelm@24981
   433
fun cert_def ctxt tm =
wenzelm@18941
   434
  let val ((lhs, rhs), _) = tm
wenzelm@24981
   435
    |> no_vars (Syntax.pp ctxt)
wenzelm@18941
   436
    |> Logic.strip_imp_concl
wenzelm@24981
   437
    |> PrimitiveDefs.dest_def ctxt Term.is_Const (K false) (K false)
wenzelm@18941
   438
  in (Term.dest_Const (Term.head_of lhs), rhs) end
wenzelm@18941
   439
  handle TERM (msg, _) => error msg;
wenzelm@18941
   440
wenzelm@18941
   441
wenzelm@16337
   442
wenzelm@16442
   443
(** read and certify entities **)    (*exception ERROR*)
wenzelm@16337
   444
wenzelm@24273
   445
(* classes *)
wenzelm@19244
   446
wenzelm@19427
   447
fun read_class thy c = certify_class thy (intern_class thy c)
wenzelm@19427
   448
  handle TYPE (msg, _, _) => error msg;
wenzelm@16337
   449
wenzelm@16337
   450
wenzelm@19244
   451
(* type arities *)
wenzelm@19244
   452
wenzelm@19244
   453
fun prep_arity prep_tycon prep_sort thy (t, Ss, S) =
wenzelm@19244
   454
  let val arity = (prep_tycon thy t, map (prep_sort thy) Ss, prep_sort thy S)
wenzelm@19513
   455
  in Type.add_arity (pp thy) arity (tsig_of thy); arity end;
wenzelm@19244
   456
wenzelm@24707
   457
val read_arity = prep_arity intern_type Syntax.read_sort_global;
wenzelm@19244
   458
val cert_arity = prep_arity (K I) certify_sort;
wenzelm@19244
   459
wenzelm@19244
   460
wenzelm@16337
   461
(* types *)
wenzelm@16337
   462
wenzelm@22765
   463
fun get_sort thy def_sort raw_env =
wenzelm@22696
   464
  let
wenzelm@22765
   465
    val tsig = tsig_of thy;
wenzelm@22765
   466
wenzelm@22696
   467
    fun eq ((xi, S), (xi', S')) =
wenzelm@22696
   468
      Term.eq_ix (xi, xi') andalso Type.eq_sort tsig (S, S');
wenzelm@22696
   469
    val env = distinct eq raw_env;
wenzelm@22696
   470
    val _ = (case duplicates (eq_fst (op =)) env of [] => ()
wenzelm@22696
   471
      | dups => error ("Inconsistent sort constraints for type variable(s) "
wenzelm@22696
   472
          ^ commas_quote (map (Term.string_of_vname' o fst) dups)));
wenzelm@22696
   473
wenzelm@22696
   474
    fun get xi =
wenzelm@22696
   475
      (case (AList.lookup (op =) env xi, def_sort xi) of
wenzelm@22696
   476
        (NONE, NONE) => Type.defaultS tsig
wenzelm@22696
   477
      | (NONE, SOME S) => S
wenzelm@22696
   478
      | (SOME S, NONE) => S
wenzelm@22696
   479
      | (SOME S, SOME S') =>
wenzelm@22696
   480
          if Type.eq_sort tsig (S, S') then S'
wenzelm@22696
   481
          else error ("Sort constraint inconsistent with default for type variable " ^
wenzelm@22696
   482
            quote (Term.string_of_vname' xi)));
wenzelm@22696
   483
  in get end;
wenzelm@22696
   484
wenzelm@16337
   485
local
wenzelm@16337
   486
wenzelm@24273
   487
fun gen_read_typ mode (thy, def_sort) str =
wenzelm@16337
   488
  let
wenzelm@24273
   489
    val ctxt = ProofContext.init thy;
wenzelm@24273
   490
    val syn = syn_of thy;
wenzelm@22696
   491
    val T = intern_tycons thy
wenzelm@24273
   492
      (Syntax.standard_parse_typ ctxt syn (get_sort thy def_sort) (intern_sort thy) str);
wenzelm@24273
   493
  in certify_typ_mode mode thy T handle TYPE (msg, _, _) => error msg end
wenzelm@18678
   494
  handle ERROR msg => cat_error msg ("The error(s) above occurred in type " ^ quote str);
wenzelm@16337
   495
wenzelm@16337
   496
in
wenzelm@16337
   497
wenzelm@16442
   498
fun no_def_sort thy = (thy: theory, K NONE);
wenzelm@16337
   499
wenzelm@24273
   500
val read_def_typ     = gen_read_typ Type.mode_default;
wenzelm@24273
   501
val read_typ         = gen_read_typ Type.mode_default o no_def_sort;
wenzelm@24273
   502
val read_typ_syntax  = gen_read_typ Type.mode_syntax o no_def_sort;
wenzelm@24273
   503
val read_typ_abbrev  = gen_read_typ Type.mode_abbrev o no_def_sort;
wenzelm@16337
   504
wenzelm@16337
   505
end;
wenzelm@16337
   506
wenzelm@16337
   507
wenzelm@22696
   508
(* read_def_terms -- read terms and infer types *)    (*exception ERROR*)
wenzelm@251
   509
wenzelm@2979
   510
(*
wenzelm@16337
   511
  def_type: partial map from indexnames to types (constrains Frees and Vars)
wenzelm@16337
   512
  def_sort: partial map from indexnames to sorts (constrains TFrees and TVars)
wenzelm@20155
   513
  used: context of already used type variables
wenzelm@2979
   514
  freeze: if true then generated parameters are turned into TFrees, else TVars
wenzelm@2979
   515
*)
wenzelm@2979
   516
wenzelm@24233
   517
fun read_def_terms'
wenzelm@24233
   518
    pp is_logtype syn consts map_free ctxt (def_type, def_sort) used freeze raw_args =
wenzelm@251
   519
  let
wenzelm@22696
   520
    val thy = ProofContext.theory_of ctxt;
wenzelm@24517
   521
    fun check_typs Ts = map (certify_typ thy) Ts
wenzelm@24517
   522
      handle TYPE (msg, _, _) => error msg;
wenzelm@22696
   523
wenzelm@24485
   524
    fun infer args = TypeInfer.infer_types pp (tsig_of thy) check_typs
wenzelm@24761
   525
        (try (Consts.the_constraint consts)) def_type used ~1 (SOME freeze) args |>> map fst
wenzelm@24233
   526
      handle TYPE (msg, _, _) => error msg;
wenzelm@22696
   527
wenzelm@25476
   528
    fun check T t = (singleton (fst o infer) (t, T); NONE) handle ERROR msg => SOME msg;
wenzelm@24233
   529
    val map_const = try (#1 o Term.dest_Const o Consts.read_const consts);
wenzelm@24370
   530
    fun read T = Syntax.standard_parse_term pp (check T) (get_sort thy def_sort) map_const map_free
wenzelm@24233
   531
        (intern_tycons thy) (intern_sort thy) ctxt is_logtype syn T;
wenzelm@4249
   532
  in
wenzelm@24233
   533
    raw_args
wenzelm@24233
   534
    |> map (fn (s, raw_T) =>
wenzelm@24233
   535
      let val T = certify_typ thy raw_T handle TYPE (msg, _, _) => error msg
wenzelm@24233
   536
      in (read (#1 (TypeInfer.paramify_dummies T 0)) s, T) end)
wenzelm@24233
   537
    |> infer
wenzelm@4249
   538
  end;
clasohm@623
   539
wenzelm@20230
   540
fun read_def_terms (thy, types, sorts) used freeze sTs =
wenzelm@20230
   541
  let
wenzelm@20230
   542
    val pp = pp thy;
wenzelm@20230
   543
    val consts = consts_of thy;
wenzelm@24674
   544
    val cert_consts = Consts.certify pp (tsig_of thy) true consts;
wenzelm@22765
   545
    fun map_free x = if is_some (types (x, ~1)) then SOME x else NONE;
wenzelm@20230
   546
    val (ts, inst) =
wenzelm@22765
   547
      read_def_terms' pp (is_logtype thy) (syn_of thy) consts map_free
wenzelm@21772
   548
        (ProofContext.init thy) (types, sorts) (Name.make_context used) freeze sTs;
wenzelm@20230
   549
  in (map cert_consts ts, inst) end;
wenzelm@12068
   550
wenzelm@16442
   551
fun simple_read_term thy T s =
wenzelm@16442
   552
  let val ([t], _) = read_def_terms (thy, K NONE, K NONE) [] true [(s, T)]
wenzelm@20230
   553
  in t end handle ERROR msg => cat_error msg ("The error(s) above occurred for term " ^ s);
wenzelm@8802
   554
wenzelm@22709
   555
fun read_term thy = simple_read_term thy dummyT;
wenzelm@16494
   556
fun read_prop thy = simple_read_term thy propT;
wenzelm@16494
   557
wenzelm@8607
   558
wenzelm@2979
   559
wenzelm@16337
   560
(** signature extension functions **)  (*exception ERROR/TYPE*)
wenzelm@386
   561
wenzelm@386
   562
(* add default sort *)
wenzelm@386
   563
wenzelm@16442
   564
fun gen_add_defsort prep_sort s thy =
wenzelm@16442
   565
  thy |> map_tsig (Type.set_defsort (prep_sort thy s));
wenzelm@8898
   566
wenzelm@24707
   567
val add_defsort = gen_add_defsort Syntax.read_sort_global;
wenzelm@16337
   568
val add_defsort_i = gen_add_defsort certify_sort;
wenzelm@386
   569
wenzelm@386
   570
wenzelm@386
   571
(* add type constructors *)
wenzelm@386
   572
wenzelm@16442
   573
fun add_types types thy = thy |> map_sign (fn (naming, syn, tsig, consts) =>
wenzelm@14856
   574
  let
wenzelm@25390
   575
    val syn' = Syntax.update_type_gram types syn;
wenzelm@16368
   576
    val decls = map (fn (a, n, mx) => (Syntax.type_name a mx, n)) types;
wenzelm@16442
   577
    val tsig' = Type.add_types naming decls tsig;
wenzelm@16442
   578
  in (naming, syn', tsig', consts) end);
wenzelm@16442
   579
wenzelm@16337
   580
wenzelm@16337
   581
(* add nonterminals *)
wenzelm@16337
   582
wenzelm@16442
   583
fun add_nonterminals ns thy = thy |> map_sign (fn (naming, syn, tsig, consts) =>
wenzelm@16337
   584
  let
wenzelm@25390
   585
    val syn' = Syntax.update_consts ns syn;
wenzelm@16442
   586
    val tsig' = Type.add_nonterminals naming ns tsig;
wenzelm@16442
   587
  in (naming, syn', tsig', consts) end);
wenzelm@386
   588
wenzelm@386
   589
wenzelm@386
   590
(* add type abbreviations *)
wenzelm@386
   591
wenzelm@25366
   592
fun gen_add_tyabbr parse_typ (a, vs, rhs, mx) thy =
wenzelm@16442
   593
  thy |> map_sign (fn (naming, syn, tsig, consts) =>
wenzelm@16337
   594
    let
wenzelm@25366
   595
      val ctxt = ProofContext.init thy;
wenzelm@25390
   596
      val syn' = Syntax.update_type_gram [(a, length vs, mx)] syn;
wenzelm@16368
   597
      val a' = Syntax.type_name a mx;
wenzelm@25366
   598
      val abbr = (a', vs, certify_typ_mode Type.mode_syntax thy (parse_typ ctxt rhs))
wenzelm@18678
   599
        handle ERROR msg => cat_error msg ("in type abbreviation " ^ quote a');
wenzelm@16442
   600
      val tsig' = Type.add_abbrevs naming [abbr] tsig;
wenzelm@16442
   601
    in (naming, syn', tsig', consts) end);
wenzelm@386
   602
wenzelm@24707
   603
val add_tyabbrs = fold (gen_add_tyabbr Syntax.parse_typ);
wenzelm@24273
   604
val add_tyabbrs_i = fold (gen_add_tyabbr (K I));
wenzelm@14784
   605
wenzelm@14784
   606
wenzelm@16337
   607
(* modify syntax *)
wenzelm@16337
   608
wenzelm@25366
   609
fun gen_syntax change_gram parse_typ mode args thy =
wenzelm@16337
   610
  let
wenzelm@25366
   611
    val ctxt = ProofContext.init thy;
wenzelm@25366
   612
    fun prep (c, T, mx) = (c, certify_typ_mode Type.mode_syntax thy (parse_typ ctxt T), mx)
wenzelm@24273
   613
      handle ERROR msg =>
wenzelm@24273
   614
        cat_error msg ("in syntax declaration " ^ quote (Syntax.const_name c mx));
wenzelm@20784
   615
  in thy |> map_syn (change_gram (is_logtype thy) mode (map prep args)) end;
wenzelm@16337
   616
wenzelm@25390
   617
fun gen_add_syntax x = gen_syntax Syntax.update_const_gram x;
wenzelm@386
   618
wenzelm@24707
   619
val add_modesyntax = gen_add_syntax Syntax.parse_typ;
wenzelm@24273
   620
val add_modesyntax_i = gen_add_syntax (K I);
wenzelm@24959
   621
val add_syntax = add_modesyntax Syntax.mode_default;
wenzelm@24959
   622
val add_syntax_i = add_modesyntax_i Syntax.mode_default;
wenzelm@24707
   623
val del_modesyntax = gen_syntax Syntax.remove_const_gram Syntax.parse_typ;
wenzelm@24273
   624
val del_modesyntax_i = gen_syntax Syntax.remove_const_gram (K I);
wenzelm@3805
   625
wenzelm@25383
   626
fun notation add mode args thy =
wenzelm@25383
   627
  let
wenzelm@25383
   628
    val change_gram = if add then Syntax.update_const_gram else Syntax.remove_const_gram;
wenzelm@25383
   629
    fun const_syntax (Const (c, _), mx) = try (Consts.syntax (consts_of thy)) (c, mx)
wenzelm@25383
   630
      | const_syntax _ = NONE;
wenzelm@25383
   631
  in gen_syntax change_gram (K I) mode (map_filter const_syntax args) thy end;
wenzelm@19658
   632
wenzelm@16337
   633
wenzelm@16337
   634
(* add constants *)
wenzelm@386
   635
wenzelm@17995
   636
local
wenzelm@17995
   637
wenzelm@25366
   638
fun gen_add_consts parse_typ authentic tags raw_args thy =
wenzelm@386
   639
  let
wenzelm@25366
   640
    val ctxt = ProofContext.init thy;
wenzelm@25366
   641
    val prepT = Type.no_tvars o Term.no_dummyT o certify_typ thy o parse_typ ctxt;
wenzelm@19658
   642
    fun prep (raw_c, raw_T, raw_mx) =
wenzelm@19658
   643
      let
wenzelm@19658
   644
        val (c, mx) = Syntax.const_mixfix raw_c raw_mx;
wenzelm@24959
   645
        val full_c = full_name thy c;
wenzelm@24959
   646
        val c' = if authentic then Syntax.constN ^ full_c else c;
wenzelm@19658
   647
        val T = (prepT raw_T handle TYPE (msg, _, _) => error msg) handle ERROR msg =>
wenzelm@19658
   648
          cat_error msg ("in declaration of constant " ^ quote c);
wenzelm@26631
   649
        val T' = Logic.varifyT T;
wenzelm@24959
   650
      in ((c, T'), (c', T', mx), Const (full_c, T)) end;
wenzelm@16337
   651
    val args = map prep raw_args;
wenzelm@16442
   652
  in
wenzelm@16442
   653
    thy
wenzelm@24776
   654
    |> map_consts (fold (Consts.declare authentic (naming_of thy) tags o #1) args)
wenzelm@19658
   655
    |> add_syntax_i (map #2 args)
wenzelm@24959
   656
    |> pair (map #3 args)
wenzelm@16442
   657
  end;
wenzelm@386
   658
wenzelm@17995
   659
in
wenzelm@17995
   660
wenzelm@25366
   661
val add_consts = snd oo gen_add_consts Syntax.parse_typ false [];
wenzelm@25366
   662
val add_consts_i = snd oo gen_add_consts (K I) false [];
wenzelm@24959
   663
wenzelm@25366
   664
fun declare_const tags arg = gen_add_consts (K I) true tags [arg] #>> the_single;
wenzelm@386
   665
wenzelm@17995
   666
end;
wenzelm@17995
   667
wenzelm@386
   668
wenzelm@25049
   669
(* abbreviations *)
wenzelm@18941
   670
wenzelm@24776
   671
fun add_abbrev mode tags (c, raw_t) thy =
wenzelm@18941
   672
  let
wenzelm@21741
   673
    val pp = pp thy;
wenzelm@26631
   674
    val prep_tm = no_frees pp o Term.no_dummy_patterns o cert_term_abbrev thy;
wenzelm@19806
   675
    val t = (prep_tm raw_t handle TYPE (msg, _, _) => error msg | TERM (msg, _) => error msg)
wenzelm@19366
   676
      handle ERROR msg => cat_error msg ("in constant abbreviation " ^ quote c);
wenzelm@21696
   677
    val (res, consts') = consts_of thy
wenzelm@24776
   678
      |> Consts.abbreviate pp (tsig_of thy) (naming_of thy) mode tags (c, t);
wenzelm@25049
   679
  in (res, thy |> map_consts (K consts')) end;
wenzelm@25049
   680
wenzelm@25049
   681
fun revert_abbrev mode c = map_consts (Consts.revert_abbrev mode c);
wenzelm@18941
   682
wenzelm@18941
   683
wenzelm@16941
   684
(* add constraints *)
wenzelm@16941
   685
wenzelm@24761
   686
fun add_const_constraint (c, opt_T) thy =
wenzelm@16941
   687
  let
wenzelm@19099
   688
    fun prepT raw_T =
wenzelm@24761
   689
      let val T = Logic.varifyT (Type.no_tvars (Term.no_dummyT (certify_typ thy raw_T)))
wenzelm@19099
   690
      in cert_term thy (Const (c, T)); T end
wenzelm@16941
   691
      handle TYPE (msg, _, _) => error msg;
wenzelm@19099
   692
  in thy |> map_consts (Consts.constrain (c, Option.map prepT opt_T)) end;
wenzelm@16941
   693
wenzelm@16941
   694
wenzelm@19513
   695
(* primitive classes and arities *)
wenzelm@386
   696
wenzelm@19513
   697
fun primitive_class (bclass, classes) thy =
wenzelm@16442
   698
  thy |> map_sign (fn (naming, syn, tsig, consts) =>
wenzelm@16337
   699
    let
wenzelm@25390
   700
      val syn' = Syntax.update_consts [bclass] syn;
wenzelm@19513
   701
      val tsig' = Type.add_class (pp thy) naming (bclass, classes) tsig;
wenzelm@16442
   702
    in (naming, syn', tsig', consts) end)
wenzelm@19391
   703
  |> add_consts_i [(Logic.const_of_class bclass, Term.a_itselfT --> propT, NoSyn)];
wenzelm@3791
   704
wenzelm@19513
   705
fun primitive_classrel arg thy = thy |> map_tsig (Type.add_classrel (pp thy) arg);
wenzelm@19513
   706
fun primitive_arity arg thy = thy |> map_tsig (Type.add_arity (pp thy) arg);
wenzelm@421
   707
wenzelm@421
   708
wenzelm@14645
   709
(* add translation functions *)
wenzelm@14645
   710
wenzelm@15746
   711
local
wenzelm@15746
   712
wenzelm@15746
   713
fun mk trs = map Syntax.mk_trfun trs;
wenzelm@15746
   714
wenzelm@16597
   715
fun gen_add_trfuns ext non_typed (atrs, trs, tr's, atr's) =
wenzelm@16597
   716
  map_syn (ext (mk atrs, mk trs, mk (map (apsnd non_typed) tr's), mk atr's));
wenzelm@14645
   717
wenzelm@16597
   718
fun gen_add_trfunsT ext tr's = map_syn (ext ([], [], mk tr's, []));
wenzelm@14645
   719
wenzelm@15746
   720
in
wenzelm@15746
   721
wenzelm@25390
   722
val add_trfuns = gen_add_trfuns Syntax.update_trfuns Syntax.non_typed_tr';
wenzelm@25390
   723
val add_trfunsT = gen_add_trfunsT Syntax.update_trfuns;
wenzelm@25390
   724
val add_advanced_trfuns = gen_add_trfuns Syntax.update_advanced_trfuns Syntax.non_typed_tr'';
wenzelm@25390
   725
val add_advanced_trfunsT = gen_add_trfunsT Syntax.update_advanced_trfuns;
wenzelm@14645
   726
wenzelm@15746
   727
end;
wenzelm@15746
   728
wenzelm@16597
   729
val add_tokentrfuns = map_syn o Syntax.extend_tokentrfuns;
wenzelm@16442
   730
fun add_mode_tokentrfuns m = add_tokentrfuns o map (fn (s, f) => (m, s, f));
wenzelm@14645
   731
wenzelm@14645
   732
wenzelm@19258
   733
(* translation rules *)
wenzelm@4619
   734
wenzelm@19258
   735
fun gen_trrules f args thy = thy |> map_syn (fn syn =>
wenzelm@16442
   736
  let val rules = map (Syntax.map_trrule (apfst (intern_type thy))) args
wenzelm@21772
   737
  in f (ProofContext.init thy) (is_logtype thy) syn rules syn end);
wenzelm@8725
   738
wenzelm@25390
   739
val add_trrules = gen_trrules Syntax.update_trrules;
wenzelm@19258
   740
val del_trrules = gen_trrules Syntax.remove_trrules;
wenzelm@25390
   741
val add_trrules_i = map_syn o Syntax.update_trrules_i;
wenzelm@19258
   742
val del_trrules_i = map_syn o Syntax.remove_trrules_i;
wenzelm@386
   743
wenzelm@386
   744
wenzelm@26667
   745
(* naming *)
wenzelm@6546
   746
wenzelm@19054
   747
val add_path        = map_naming o NameSpace.add_path;
wenzelm@19054
   748
val no_base_names   = map_naming NameSpace.no_base_names;
wenzelm@19054
   749
val qualified_names = map_naming NameSpace.qualified_names;
wenzelm@19054
   750
val sticky_prefix   = map_naming o NameSpace.sticky_prefix;
wenzelm@19054
   751
val restore_naming  = map_naming o K o naming_of;
wenzelm@6546
   752
wenzelm@19013
   753
val parent_path   = add_path "..";
wenzelm@19013
   754
val root_path     = add_path "/";
wenzelm@19013
   755
val absolute_path = add_path "//";
wenzelm@16442
   756
wenzelm@16442
   757
fun local_path thy = thy |> root_path |> add_path (Context.theory_name thy);
wenzelm@16442
   758
wenzelm@6546
   759
wenzelm@16337
   760
(* hide names *)
wenzelm@386
   761
wenzelm@26667
   762
val hide_class = map_tsig oo Type.hide_class;
wenzelm@26667
   763
val hide_type = map_tsig oo Type.hide_type;
wenzelm@26667
   764
val hide_const = map_consts oo Consts.hide;
wenzelm@17343
   765
clasohm@0
   766
end;