src/Pure/sign.ML
author wenzelm
Mon Oct 20 15:20:20 1997 +0200 (1997-10-20)
changeset 3956 d59fe4579004
parent 3937 988ce6fbf85b
child 3967 edd5ff9371f8
permissions -rw-r--r--
tuned types;
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@251
     5
The abstract type "sg" of signatures.
clasohm@0
     6
*)
clasohm@0
     7
wenzelm@3956
     8
(*base names*)
wenzelm@3956
     9
type bstring = string;
wenzelm@3956
    10
type bclass = class;
wenzelm@3956
    11
(*external forms -- partially qualified names*)
wenzelm@3805
    12
type xstring = string;
wenzelm@3805
    13
type xclass = class;
wenzelm@3805
    14
type xsort = sort;
wenzelm@3805
    15
type xtyp = typ;
wenzelm@3805
    16
type xterm = term;
wenzelm@3805
    17
wenzelm@19
    18
signature SIGN =
wenzelm@3791
    19
sig
paulson@1501
    20
  type sg
wenzelm@2197
    21
  val rep_sg: sg ->
wenzelm@2197
    22
   {tsig: Type.type_sig,
wenzelm@2197
    23
    const_tab: typ Symtab.table,
wenzelm@2197
    24
    syn: Syntax.syntax,
wenzelm@3791
    25
    path: string list,
wenzelm@3791
    26
    spaces: (string * NameSpace.T) list,
wenzelm@3867
    27
    data: Data.T,
wenzelm@2197
    28
    stamps: string ref list}
paulson@1501
    29
  val subsig: sg * sg -> bool
paulson@1501
    30
  val eq_sg: sg * sg -> bool
paulson@1501
    31
  val same_sg: sg * sg -> bool
paulson@1501
    32
  val is_draft: sg -> bool
paulson@1501
    33
  val const_type: sg -> string -> typ option
paulson@1501
    34
  val classes: sg -> class list
paulson@1501
    35
  val subsort: sg -> sort * sort -> bool
paulson@1501
    36
  val nodup_Vars: term -> unit
paulson@1501
    37
  val norm_sort: sg -> sort -> sort
paulson@1501
    38
  val nonempty_sort: sg -> sort list -> sort -> bool
wenzelm@3791
    39
  val long_names: bool ref
wenzelm@3810
    40
  val classK: string
wenzelm@3810
    41
  val typeK: string
wenzelm@3810
    42
  val constK: string
wenzelm@3956
    43
  val full_name: sg -> bstring -> string
wenzelm@3956
    44
  val base_name: string -> bstring
wenzelm@3810
    45
  val intern: sg -> string -> xstring -> string
wenzelm@3810
    46
  val extern: sg -> string -> string -> xstring
wenzelm@3937
    47
  val cond_extern: sg -> string -> string -> xstring
wenzelm@3805
    48
  val intern_class: sg -> xclass -> class
wenzelm@3805
    49
  val intern_tycon: sg -> xstring -> string
wenzelm@3805
    50
  val intern_const: sg -> xstring -> string
wenzelm@3937
    51
  val intern_sort: sg -> xsort -> sort
wenzelm@3937
    52
  val intern_typ: sg -> xtyp -> typ
wenzelm@3937
    53
  val intern_term: sg -> xterm -> term
wenzelm@3937
    54
  val intern_tycons: sg -> xtyp -> typ
paulson@1501
    55
  val print_sg: sg -> unit
paulson@1501
    56
  val pretty_sg: sg -> Pretty.T
paulson@1501
    57
  val pprint_sg: sg -> pprint_args -> unit
paulson@1501
    58
  val pretty_term: sg -> term -> Pretty.T
paulson@1501
    59
  val pretty_typ: sg -> typ -> Pretty.T
wenzelm@3791
    60
  val pretty_sort: sg -> sort -> Pretty.T
paulson@1501
    61
  val string_of_term: sg -> term -> string
paulson@1501
    62
  val string_of_typ: sg -> typ -> string
wenzelm@3791
    63
  val string_of_sort: sg -> sort -> string
wenzelm@3855
    64
  val str_of_sort: sg -> sort -> string
wenzelm@3855
    65
  val str_of_classrel: sg -> class * class -> string
wenzelm@3855
    66
  val str_of_arity: sg -> string * sort list * sort -> string
paulson@1501
    67
  val pprint_term: sg -> term -> pprint_args -> unit
paulson@1501
    68
  val pprint_typ: sg -> typ -> pprint_args -> unit
paulson@1501
    69
  val certify_typ: sg -> typ -> typ
paulson@1501
    70
  val certify_term: sg -> term -> term * typ * int
paulson@1501
    71
  val read_typ: sg * (indexname -> sort option) -> string -> typ
paulson@1501
    72
  val infer_types: sg -> (indexname -> typ option) ->
paulson@1501
    73
    (indexname -> sort option) -> string list -> bool
wenzelm@3805
    74
    -> xterm list * typ -> int * term * (indexname * typ) list
wenzelm@3956
    75
  val add_classes: (bclass * xclass list) list -> sg -> sg
wenzelm@3956
    76
  val add_classes_i: (bclass * class list) list -> sg -> sg
wenzelm@3805
    77
  val add_classrel: (xclass * xclass) list -> sg -> sg
wenzelm@3791
    78
  val add_classrel_i: (class * class) list -> sg -> sg
wenzelm@3805
    79
  val add_defsort: xsort -> sg -> sg
wenzelm@3791
    80
  val add_defsort_i: sort -> sg -> sg
wenzelm@3956
    81
  val add_types: (bstring * int * mixfix) list -> sg -> sg
wenzelm@3956
    82
  val add_tyabbrs: (bstring * string list * string * mixfix) list -> sg -> sg
wenzelm@3956
    83
  val add_tyabbrs_i: (bstring * string list * typ * mixfix) list -> sg -> sg
wenzelm@3805
    84
  val add_arities: (xstring * xsort list * xsort) list -> sg -> sg
wenzelm@3791
    85
  val add_arities_i: (string * sort list * sort) list -> sg -> sg
wenzelm@3956
    86
  val add_consts: (bstring * string * mixfix) list -> sg -> sg
wenzelm@3956
    87
  val add_consts_i: (bstring * typ * mixfix) list -> sg -> sg
wenzelm@3956
    88
  val add_syntax: (bstring * string * mixfix) list -> sg -> sg
wenzelm@3956
    89
  val add_syntax_i: (bstring * typ * mixfix) list -> sg -> sg
wenzelm@3956
    90
  val add_modesyntax: (string * bool) * (bstring * string * mixfix) list -> sg -> sg
wenzelm@3956
    91
  val add_modesyntax_i: (string * bool) * (bstring * typ * mixfix) list -> sg -> sg
paulson@1501
    92
  val add_trfuns:
wenzelm@3956
    93
    (bstring * (ast list -> ast)) list *
wenzelm@3956
    94
    (bstring * (term list -> term)) list *
wenzelm@3956
    95
    (bstring * (term list -> term)) list *
wenzelm@3956
    96
    (bstring * (ast list -> ast)) list -> sg -> sg
wenzelm@2385
    97
  val add_trfunsT:
wenzelm@3956
    98
    (bstring * (typ -> term list -> term)) list -> sg -> sg
wenzelm@2693
    99
  val add_tokentrfuns:
wenzelm@2693
   100
    (string * string * (string -> string * int)) list -> sg -> sg
paulson@1810
   101
  val add_trrules: (string * string) Syntax.trrule list -> sg -> sg
paulson@1810
   102
  val add_trrules_i: ast Syntax.trrule list -> sg -> sg
wenzelm@3791
   103
  val add_path: string -> sg -> sg
wenzelm@3810
   104
  val add_space: string * string list -> sg -> sg
paulson@1501
   105
  val add_name: string -> sg -> sg
wenzelm@3877
   106
  val init_data: string * exn * (exn -> exn) * (exn * exn -> exn) *
wenzelm@3877
   107
    (string -> exn -> unit) -> sg -> sg
wenzelm@3867
   108
  val get_data: sg -> string -> exn
wenzelm@3867
   109
  val put_data: string * exn -> sg -> sg
wenzelm@3877
   110
  val print_data: sg -> string -> unit
wenzelm@3877
   111
  val prep_ext: sg -> sg
paulson@1501
   112
  val merge: sg * sg -> sg
wenzelm@3877
   113
  val nontriv_merge: sg * sg -> sg
paulson@1501
   114
  val proto_pure: sg
paulson@1501
   115
  val pure: sg
paulson@1501
   116
  val cpure: sg
paulson@1501
   117
  val const_of_class: class -> string
paulson@1501
   118
  val class_of_const: string -> class
wenzelm@3791
   119
end;
clasohm@0
   120
paulson@1501
   121
structure Sign : SIGN =
wenzelm@143
   122
struct
clasohm@0
   123
wenzelm@251
   124
(** datatype sg **)
wenzelm@251
   125
wenzelm@19
   126
datatype sg =
wenzelm@143
   127
  Sg of {
wenzelm@3791
   128
    tsig: Type.type_sig,                        (*order-sorted signature of types*)
wenzelm@3805
   129
    const_tab: typ Symtab.table,                (*type schemes of constants*)
wenzelm@3791
   130
    syn: Syntax.syntax,                         (*syntax for parsing and printing*)
wenzelm@3805
   131
    path: string list,                     	(*current name space entry prefix*)
wenzelm@3791
   132
    spaces: (string * NameSpace.T) list,   	(*name spaces for consts, types etc.*)
wenzelm@3867
   133
    data: Data.T,				(*additional data*)
wenzelm@3791
   134
    stamps: string ref list};                   (*unique theory indentifier*)
wenzelm@3867
   135
      (*the "ref" in stamps ensures that no two signatures are identical
wenzelm@3867
   136
        -- it is impossible to forge a signature*)
clasohm@0
   137
clasohm@0
   138
fun rep_sg (Sg args) = args;
wenzelm@402
   139
val tsig_of = #tsig o rep_sg;
clasohm@0
   140
nipkow@206
   141
wenzelm@2979
   142
(* inclusion and equality *)
paulson@2180
   143
wenzelm@2185
   144
local
wenzelm@2185
   145
  (*avoiding polymorphic equality: factor 10 speedup*)
wenzelm@2185
   146
  fun mem_stamp (_:string ref, []) = false
wenzelm@2185
   147
    | mem_stamp (x, y :: ys) = x = y orelse mem_stamp (x, ys);
wenzelm@2185
   148
wenzelm@2185
   149
  fun subset_stamp ([], ys) = true
wenzelm@2185
   150
    | subset_stamp (x :: xs, ys) =
wenzelm@2185
   151
        mem_stamp (x, ys) andalso subset_stamp (xs, ys);
paulson@2180
   152
wenzelm@2185
   153
  (*fast partial test*)
wenzelm@2185
   154
  fun fast_sub ([]: string ref list, _) = true
wenzelm@2185
   155
    | fast_sub (_, []) = false
wenzelm@2185
   156
    | fast_sub (x :: xs, y :: ys) =
wenzelm@2185
   157
        if x = y then fast_sub (xs, ys)
wenzelm@2185
   158
        else fast_sub (x :: xs, ys);
wenzelm@2185
   159
in
wenzelm@2185
   160
  fun subsig (Sg {stamps = s1, ...}, Sg {stamps = s2, ...}) =
wenzelm@2185
   161
    s1 = s2 orelse subset_stamp (s1, s2);
wenzelm@2185
   162
wenzelm@2185
   163
  fun fast_subsig (Sg {stamps = s1, ...}, Sg {stamps = s2, ...}) =
wenzelm@2185
   164
    s1 = s2 orelse fast_sub (s1, s2);
paulson@2180
   165
wenzelm@2185
   166
  fun eq_sg (Sg {stamps = s1, ...}, Sg {stamps = s2, ...}) =
wenzelm@2185
   167
    s1 = s2 orelse (subset_stamp (s1, s2) andalso subset_stamp (s2, s1));
wenzelm@2185
   168
end;
wenzelm@2185
   169
wenzelm@402
   170
wenzelm@2185
   171
(*test if same theory names are contained in signatures' stamps,
wenzelm@2185
   172
  i.e. if signatures belong to same theory but not necessarily to the
wenzelm@2185
   173
  same version of it*)
wenzelm@2185
   174
fun same_sg (Sg {stamps = s1, ...}, Sg {stamps = s2, ...}) =
wenzelm@2185
   175
  eq_set_string (pairself (map (op !)) (s1, s2));
wenzelm@2185
   176
wenzelm@2185
   177
(*test for drafts*)
wenzelm@386
   178
fun is_draft (Sg {stamps = ref "#" :: _, ...}) = true
wenzelm@386
   179
  | is_draft _ = false;
wenzelm@386
   180
clasohm@0
   181
wenzelm@402
   182
(* consts *)
wenzelm@402
   183
wenzelm@2979
   184
fun const_type (Sg {const_tab, ...}) c = Symtab.lookup (const_tab, c);
wenzelm@386
   185
wenzelm@386
   186
wenzelm@620
   187
(* classes and sorts *)
wenzelm@402
   188
wenzelm@402
   189
val classes = #classes o Type.rep_tsig o tsig_of;
wenzelm@402
   190
wenzelm@402
   191
val subsort = Type.subsort o tsig_of;
wenzelm@402
   192
val norm_sort = Type.norm_sort o tsig_of;
wenzelm@1216
   193
val nonempty_sort = Type.nonempty_sort o tsig_of;
wenzelm@402
   194
wenzelm@3791
   195
wenzelm@3791
   196
wenzelm@3791
   197
(** name spaces **)
wenzelm@3791
   198
wenzelm@3805
   199
(*prune names on output by default*)
wenzelm@3791
   200
val long_names = ref false;
wenzelm@3791
   201
wenzelm@3791
   202
wenzelm@3791
   203
(* kinds *)
wenzelm@3791
   204
wenzelm@3791
   205
val classK = "class";
wenzelm@3791
   206
val typeK = "type";
wenzelm@3791
   207
val constK = "const";
wenzelm@3791
   208
wenzelm@3791
   209
wenzelm@3791
   210
(* add and retrieve names *)
wenzelm@3791
   211
wenzelm@3791
   212
fun space_of spaces kind =
wenzelm@3791
   213
  if_none (assoc (spaces, kind)) NameSpace.empty;
wenzelm@3791
   214
wenzelm@3791
   215
(*input and output of qualified names*)
wenzelm@3791
   216
fun intrn spaces kind = NameSpace.lookup (space_of spaces kind);
wenzelm@3791
   217
fun extrn spaces kind = NameSpace.prune (space_of spaces kind);
wenzelm@3791
   218
wenzelm@3791
   219
(*add names*)
wenzelm@3791
   220
fun add_names spaces kind names =
wenzelm@3791
   221
  let val space' = NameSpace.extend (names, space_of spaces kind) in
wenzelm@3791
   222
    overwrite (spaces, (kind, space'))
wenzelm@3791
   223
  end;
wenzelm@3791
   224
wenzelm@3810
   225
(*make full names*)
wenzelm@3937
   226
fun full path name =
wenzelm@3937
   227
  if NameSpace.qualified name then
wenzelm@3937
   228
    error ("Attempt to declare qualified name " ^ quote name)
wenzelm@3937
   229
  else NameSpace.pack (path @ [name]);
wenzelm@3937
   230
wenzelm@3937
   231
(*base name*)
wenzelm@3937
   232
val base_name = NameSpace.base;
wenzelm@3791
   233
wenzelm@3791
   234
wenzelm@3791
   235
(* intern / extern names *)
wenzelm@3791
   236
wenzelm@3791
   237
local
wenzelm@3791
   238
wenzelm@3791
   239
  fun add_typ_classes (Type (_, Ts), cs) = foldr add_typ_classes (Ts, cs)
wenzelm@3791
   240
    | add_typ_classes (TFree (_, S), cs) = S union cs
wenzelm@3791
   241
    | add_typ_classes (TVar (_, S), cs) = S union cs;
wenzelm@3791
   242
wenzelm@3791
   243
  fun add_typ_tycons (Type (c, Ts), cs) = foldr add_typ_tycons (Ts, c ins cs)
wenzelm@3791
   244
    | add_typ_tycons (_, cs) = cs;
wenzelm@3791
   245
wenzelm@3791
   246
  val add_term_classes = it_term_types add_typ_classes;
wenzelm@3791
   247
  val add_term_tycons = it_term_types add_typ_tycons;
wenzelm@3791
   248
wenzelm@3791
   249
  fun add_term_consts (Const (c, _), cs) = c ins cs
wenzelm@3791
   250
    | add_term_consts (t $ u, cs) = add_term_consts (t, add_term_consts (u, cs))
wenzelm@3791
   251
    | add_term_consts (Abs (_, _, t), cs) = add_term_consts (t, cs)
wenzelm@3791
   252
    | add_term_consts (_, cs) = cs;
wenzelm@3791
   253
wenzelm@3791
   254
wenzelm@3791
   255
  (*map classes, tycons*)
wenzelm@3791
   256
  fun map_typ f g (Type (c, Ts)) = Type (g c, map (map_typ f g) Ts)
wenzelm@3791
   257
    | map_typ f _ (TFree (x, S)) = TFree (x, map f S)
wenzelm@3791
   258
    | map_typ f _ (TVar (xi, S)) = TVar (xi, map f S);
wenzelm@3791
   259
wenzelm@3791
   260
  (*map classes, tycons, consts*)
wenzelm@3791
   261
  fun map_term f g h (Const (c, T)) = Const (h c, map_typ f g T)
wenzelm@3791
   262
    | map_term f g _ (Free (x, T)) = Free (x, map_typ f g T)
wenzelm@3791
   263
    | map_term f g _ (Var (xi, T)) = Var (xi, map_typ f g T)
wenzelm@3791
   264
    | map_term _ _ _ (t as Bound _) = t
wenzelm@3791
   265
    | map_term f g h (Abs (x, T, t)) = Abs (x, map_typ f g T, map_term f g h t)
wenzelm@3791
   266
    | map_term f g h (t $ u) = map_term f g h t $ map_term f g h u;
wenzelm@3791
   267
wenzelm@3791
   268
  (*prepare mapping of names*)
wenzelm@3791
   269
  fun mapping f add_xs t =
wenzelm@3791
   270
    let
wenzelm@3791
   271
      fun f' x = let val y = f x in if x = y then None else Some (x, y) end;
wenzelm@3791
   272
      val table = mapfilter f' (add_xs (t, []));
wenzelm@3791
   273
      fun lookup x = if_none (assoc (table, x)) x;
wenzelm@3791
   274
    in lookup end;
wenzelm@3791
   275
wenzelm@3791
   276
  (*intern / extern typ*)
wenzelm@3791
   277
  fun trn_typ trn T =
wenzelm@3791
   278
    T |> map_typ
wenzelm@3791
   279
      (mapping (trn classK) add_typ_classes T)
wenzelm@3791
   280
      (mapping (trn typeK) add_typ_tycons T);
wenzelm@3791
   281
wenzelm@3791
   282
  (*intern / extern term*)
wenzelm@3791
   283
  fun trn_term trn t =
wenzelm@3791
   284
    t |> map_term
wenzelm@3791
   285
      (mapping (trn classK) add_term_classes t)
wenzelm@3791
   286
      (mapping (trn typeK) add_term_tycons t)
wenzelm@3791
   287
      (mapping (trn constK) add_term_consts t);
wenzelm@3791
   288
wenzelm@3791
   289
wenzelm@3791
   290
  fun spaces_of (Sg {spaces, ...}) = spaces;
wenzelm@3791
   291
wenzelm@3791
   292
in
wenzelm@3791
   293
wenzelm@3791
   294
  fun intrn_class spaces = intrn spaces classK;
wenzelm@3791
   295
  fun extrn_class spaces = extrn spaces classK;
wenzelm@3937
   296
wenzelm@3791
   297
  val intrn_sort = map o intrn_class;
wenzelm@3791
   298
  val intrn_typ = trn_typ o intrn;
wenzelm@3937
   299
  val intrn_term = trn_term o intrn;
wenzelm@3937
   300
wenzelm@3937
   301
  val extrn_sort = map o extrn_class;
wenzelm@3791
   302
  val extrn_typ = trn_typ o extrn;
wenzelm@3791
   303
  val extrn_term = trn_term o extrn;
wenzelm@3791
   304
wenzelm@3805
   305
  fun intrn_tycons spaces T =
wenzelm@3805
   306
    map_typ I (mapping (intrn spaces typeK) add_typ_tycons T) T;
wenzelm@3805
   307
wenzelm@3810
   308
  val intern = intrn o spaces_of;
wenzelm@3855
   309
  val extern = extrn o spaces_of;
wenzelm@3937
   310
  fun cond_extern sg kind = if ! long_names then I else extern sg kind;
wenzelm@3937
   311
wenzelm@3791
   312
  val intern_class = intrn_class o spaces_of;
wenzelm@3791
   313
  val intern_sort = intrn_sort o spaces_of;
wenzelm@3791
   314
  val intern_typ = intrn_typ o spaces_of;
wenzelm@3791
   315
  val intern_term = intrn_term o spaces_of;
wenzelm@3791
   316
wenzelm@3791
   317
  fun intern_tycon sg = intrn (spaces_of sg) typeK;
wenzelm@3791
   318
  fun intern_const sg = intrn (spaces_of sg) constK;
wenzelm@3937
   319
wenzelm@3791
   320
  val intern_tycons = intrn_tycons o spaces_of;
wenzelm@3791
   321
wenzelm@3810
   322
  fun full_name (Sg {path, ...}) = full path;
wenzelm@3791
   323
end;
wenzelm@620
   324
wenzelm@402
   325
clasohm@0
   326
wenzelm@3937
   327
(** pretty printing of terms and types **)
wenzelm@3937
   328
wenzelm@3937
   329
fun pretty_term (Sg {syn, spaces, stamps, ...}) t =
wenzelm@3937
   330
  Syntax.pretty_term syn
wenzelm@3937
   331
    ("CPure" mem_string (map ! stamps))
wenzelm@3937
   332
    (if ! long_names then t else extrn_term spaces t);
wenzelm@3937
   333
wenzelm@3937
   334
fun pretty_typ (Sg {syn, spaces, ...}) T =
wenzelm@3937
   335
  Syntax.pretty_typ syn
wenzelm@3937
   336
    (if ! long_names then T else extrn_typ spaces T);
wenzelm@3937
   337
wenzelm@3937
   338
fun pretty_sort (Sg {syn, spaces, ...}) S =
wenzelm@3937
   339
  Syntax.pretty_sort syn
wenzelm@3937
   340
    (if ! long_names then S else extrn_sort spaces S);
wenzelm@3937
   341
wenzelm@3937
   342
fun pretty_classrel sg (c1, c2) = Pretty.block
wenzelm@3937
   343
  [pretty_sort sg [c1], Pretty.str " <", Pretty.brk 1, pretty_sort sg [c2]];
wenzelm@3937
   344
wenzelm@3937
   345
fun pretty_arity sg (t, Ss, S) =
wenzelm@3937
   346
  let
wenzelm@3937
   347
    val t' = cond_extern sg typeK t;
wenzelm@3937
   348
    val dom =
wenzelm@3937
   349
      if null Ss then []
wenzelm@3937
   350
      else [Pretty.list "(" ")" (map (pretty_sort sg) Ss), Pretty.brk 1];
wenzelm@3937
   351
  in
wenzelm@3937
   352
    Pretty.block
wenzelm@3937
   353
      ([Pretty.str (t' ^ " ::"), Pretty.brk 1] @ dom @ [pretty_sort sg S])
wenzelm@3937
   354
  end;
wenzelm@3937
   355
wenzelm@3937
   356
fun string_of_term sg t = Pretty.string_of (pretty_term sg t);
wenzelm@3937
   357
fun string_of_typ sg T = Pretty.string_of (pretty_typ sg T);
wenzelm@3937
   358
fun string_of_sort sg S = Pretty.string_of (pretty_sort sg S);
wenzelm@3937
   359
wenzelm@3937
   360
fun str_of_sort sg S = Pretty.str_of (pretty_sort sg S);
wenzelm@3937
   361
fun str_of_classrel sg c1_c2 = Pretty.str_of (pretty_classrel sg c1_c2);
wenzelm@3937
   362
fun str_of_arity sg ar = Pretty.str_of (pretty_arity sg ar);
wenzelm@3937
   363
wenzelm@3937
   364
fun pprint_term sg = Pretty.pprint o Pretty.quote o (pretty_term sg);
wenzelm@3937
   365
fun pprint_typ sg = Pretty.pprint o Pretty.quote o (pretty_typ sg);
wenzelm@3937
   366
wenzelm@3937
   367
wenzelm@3937
   368
wenzelm@251
   369
(** print signature **)
wenzelm@251
   370
paulson@2228
   371
fun stamp_names stamps = rev (map ! stamps);
wenzelm@386
   372
wenzelm@251
   373
fun print_sg sg =
wenzelm@251
   374
  let
wenzelm@3937
   375
    fun prt_cls c = pretty_sort sg [c];
wenzelm@3937
   376
    fun prt_sort S = pretty_sort sg S;
wenzelm@3937
   377
    fun prt_tycon t = Pretty.str (cond_extern sg typeK t);
wenzelm@3937
   378
    fun prt_arity t (c, Ss) = pretty_arity sg (t, Ss, [c]);
wenzelm@3937
   379
    fun prt_typ ty = Pretty.quote (pretty_typ sg ty);
wenzelm@3937
   380
    fun prt_const c = Pretty.quote (Pretty.str (cond_extern sg constK c));
wenzelm@3791
   381
wenzelm@3791
   382
wenzelm@3791
   383
    fun pretty_space (kind, space) = Pretty.block (Pretty.breaks
wenzelm@3791
   384
      (map Pretty.str (kind ^ ":" :: map quote (NameSpace.dest space))));
wenzelm@251
   385
wenzelm@3791
   386
    fun pretty_classes cs = Pretty.block
wenzelm@3791
   387
      (Pretty.breaks (Pretty.str "classes:" :: map prt_cls cs));
wenzelm@251
   388
wenzelm@3791
   389
    fun pretty_classrel (c, cs) = Pretty.block
wenzelm@3791
   390
      (prt_cls c :: Pretty.str " <" :: Pretty.brk 1 ::
wenzelm@3791
   391
        Pretty.commas (map prt_cls cs));
wenzelm@3791
   392
wenzelm@3791
   393
    fun pretty_default S = Pretty.block
wenzelm@3937
   394
      [Pretty.str "default:", Pretty.brk 1, pretty_sort sg S];
wenzelm@251
   395
wenzelm@3937
   396
    fun pretty_ty (t, n) = Pretty.block
wenzelm@3937
   397
      [prt_tycon t, Pretty.str (" " ^ string_of_int n)];
wenzelm@251
   398
wenzelm@3937
   399
    fun pretty_abbr (t, (vs, rhs)) = Pretty.block
wenzelm@3937
   400
      [prt_typ (Type (t, map (fn v => TVar ((v, 0), [])) vs)),
wenzelm@3791
   401
        Pretty.str " =", Pretty.brk 1, prt_typ rhs];
wenzelm@251
   402
wenzelm@3937
   403
    fun pretty_arities (t, ars) = map (prt_arity t) ars;
wenzelm@251
   404
wenzelm@3791
   405
    fun pretty_const (c, ty) = Pretty.block
wenzelm@3937
   406
      [prt_const c, Pretty.str " ::", Pretty.brk 1, prt_typ ty];
wenzelm@251
   407
wenzelm@3877
   408
    val Sg {tsig, const_tab, syn = _, path, spaces, data, stamps} = sg;
wenzelm@3810
   409
    val spaces' = sort (fn ((k1, _), (k2, _)) => k1 < k2) spaces;
wenzelm@2963
   410
    val {classes, classrel, default, tycons, abbrs, arities} =
wenzelm@2185
   411
      Type.rep_tsig tsig;
wenzelm@251
   412
  in
wenzelm@386
   413
    Pretty.writeln (Pretty.strs ("stamps:" :: stamp_names stamps));
wenzelm@3877
   414
    Pretty.writeln (Pretty.strs ("data:" :: Data.kinds data));
wenzelm@3791
   415
    Pretty.writeln (Pretty.strs ["name entry path:", NameSpace.pack path]);
wenzelm@3810
   416
    Pretty.writeln (Pretty.big_list "name spaces:" (map pretty_space spaces'));
wenzelm@3791
   417
    Pretty.writeln (pretty_classes classes);
wenzelm@3791
   418
    Pretty.writeln (Pretty.big_list "class relation:" (map pretty_classrel classrel));
wenzelm@251
   419
    Pretty.writeln (pretty_default default);
wenzelm@3791
   420
    Pretty.writeln (Pretty.big_list "type constructors:" (map pretty_ty tycons));
wenzelm@3791
   421
    Pretty.writeln (Pretty.big_list "type abbreviations:" (map pretty_abbr abbrs));
wenzelm@3791
   422
    Pretty.writeln (Pretty.big_list "type arities:" (flat (map pretty_arities arities)));
wenzelm@3791
   423
    Pretty.writeln (Pretty.big_list "consts:" (map pretty_const (Symtab.dest const_tab)))
wenzelm@251
   424
  end;
wenzelm@251
   425
wenzelm@251
   426
wenzelm@562
   427
fun pretty_sg (Sg {stamps, ...}) =
wenzelm@562
   428
  Pretty.str_list "{" "}" (stamp_names stamps);
wenzelm@562
   429
wenzelm@562
   430
val pprint_sg = Pretty.pprint o pretty_sg;
wenzelm@251
   431
wenzelm@251
   432
wenzelm@251
   433
wenzelm@562
   434
(** read types **)  (*exception ERROR*)
wenzelm@562
   435
wenzelm@562
   436
fun err_in_type s =
wenzelm@562
   437
  error ("The error(s) above occurred in type " ^ quote s);
wenzelm@562
   438
wenzelm@3791
   439
fun read_raw_typ syn tsig spaces def_sort str =
wenzelm@3791
   440
  intrn_tycons spaces
wenzelm@3791
   441
    (Syntax.read_typ syn (Type.get_sort tsig def_sort (intrn_sort spaces)) str
wenzelm@3791
   442
      handle ERROR => err_in_type str);
wenzelm@3791
   443
  
wenzelm@562
   444
(*read and certify typ wrt a signature*)
wenzelm@3791
   445
fun read_typ (Sg {tsig, syn, spaces, ...}, def_sort) str =
wenzelm@3791
   446
  Type.cert_typ tsig (read_raw_typ syn tsig spaces def_sort str)
wenzelm@3552
   447
    handle TYPE (msg, _, _) => (error_msg msg; err_in_type str);
wenzelm@562
   448
wenzelm@562
   449
wenzelm@562
   450
wenzelm@386
   451
(** certify types and terms **)   (*exception TYPE*)
wenzelm@251
   452
wenzelm@2185
   453
fun certify_typ (Sg {tsig, ...}) ty = Type.cert_typ tsig ty;
wenzelm@251
   454
wenzelm@2979
   455
(*check for duplicate TVars with distinct sorts*)
wenzelm@2979
   456
fun nodup_TVars (tvars, T) =
wenzelm@2979
   457
  (case T of
wenzelm@2979
   458
    Type (_, Ts) => nodup_TVars_list (tvars, Ts)
wenzelm@2979
   459
  | TFree _ => tvars
wenzelm@2979
   460
  | TVar (v as (a, S)) =>
wenzelm@2979
   461
      (case assoc_string_int (tvars, a) of
wenzelm@2979
   462
        Some S' =>
wenzelm@2979
   463
          if S = S' then tvars
wenzelm@3791
   464
          else raise TYPE ("Type variable " ^ Syntax.string_of_vname a ^
wenzelm@3791
   465
            " has two distinct sorts", [TVar (a, S'), T], [])
wenzelm@2979
   466
      | None => v :: tvars))
wenzelm@2979
   467
(*equivalent to foldl nodup_TVars_list, but 3X faster under Poly/ML*)
wenzelm@2979
   468
and nodup_TVars_list (tvars, []) = tvars
wenzelm@2979
   469
  | nodup_TVars_list (tvars, T :: Ts) =
wenzelm@2979
   470
      nodup_TVars_list (nodup_TVars (tvars, T), Ts);
nipkow@1494
   471
wenzelm@2979
   472
(*check for duplicate Vars with distinct types*)
nipkow@1494
   473
fun nodup_Vars tm =
wenzelm@2979
   474
  let
wenzelm@2979
   475
    fun nodups vars tvars tm =
wenzelm@2979
   476
      (case tm of
wenzelm@2979
   477
        Const (c, T) => (vars, nodup_TVars (tvars, T))
wenzelm@2979
   478
      | Free (a, T) => (vars, nodup_TVars (tvars, T))
wenzelm@2979
   479
      | Var (v as (ixn, T)) =>
wenzelm@2979
   480
          (case assoc_string_int (vars, ixn) of
wenzelm@2979
   481
            Some T' =>
wenzelm@2979
   482
              if T = T' then (vars, nodup_TVars (tvars, T))
wenzelm@3791
   483
              else raise TYPE ("Variable " ^ Syntax.string_of_vname ixn ^
wenzelm@3791
   484
                " has two distinct types", [T', T], [])
wenzelm@2979
   485
          | None => (v :: vars, tvars))
wenzelm@2979
   486
      | Bound _ => (vars, tvars)
wenzelm@3791
   487
      | Abs (_, T, t) => nodups vars (nodup_TVars (tvars, T)) t
wenzelm@2979
   488
      | s $ t =>
wenzelm@2979
   489
          let val (vars',tvars') = nodups vars tvars s in
wenzelm@2979
   490
            nodups vars' tvars' t
wenzelm@2979
   491
          end);
wenzelm@2979
   492
  in nodups [] [] tm; () end;
wenzelm@2979
   493
wenzelm@251
   494
wenzelm@386
   495
fun mapfilt_atoms f (Abs (_, _, t)) = mapfilt_atoms f t
wenzelm@386
   496
  | mapfilt_atoms f (t $ u) = mapfilt_atoms f t @ mapfilt_atoms f u
wenzelm@386
   497
  | mapfilt_atoms f a = (case f a of Some y => [y] | None => []);
clasohm@0
   498
wenzelm@2979
   499
wenzelm@386
   500
fun certify_term (sg as Sg {tsig, ...}) tm =
wenzelm@251
   501
  let
wenzelm@251
   502
    fun valid_const a T =
wenzelm@386
   503
      (case const_type sg a of
wenzelm@2185
   504
        Some U => Type.typ_instance (tsig, T, U)
wenzelm@251
   505
      | _ => false);
wenzelm@169
   506
wenzelm@251
   507
    fun atom_err (Const (a, T)) =
wenzelm@251
   508
          if valid_const a T then None
wenzelm@251
   509
          else Some ("Illegal type for constant " ^ quote a ^ " :: " ^
wenzelm@251
   510
            quote (string_of_typ sg T))
wenzelm@251
   511
      | atom_err (Var ((x, i), _)) =
wenzelm@251
   512
          if i < 0 then Some ("Negative index for Var " ^ quote x) else None
wenzelm@251
   513
      | atom_err _ = None;
wenzelm@251
   514
wenzelm@251
   515
    val norm_tm =
wenzelm@2185
   516
      (case it_term_types (Type.typ_errors tsig) (tm, []) of
wenzelm@2185
   517
        [] => map_term_types (Type.norm_typ tsig) tm
wenzelm@3791
   518
      | errs => raise TYPE (cat_lines errs, [], [tm]));
nipkow@1494
   519
    val _ = nodup_Vars norm_tm;
wenzelm@251
   520
  in
wenzelm@386
   521
    (case mapfilt_atoms atom_err norm_tm of
wenzelm@251
   522
      [] => (norm_tm, type_of norm_tm, maxidx_of_term norm_tm)
wenzelm@3791
   523
    | errs => raise TYPE (cat_lines errs, [], [norm_tm]))
wenzelm@251
   524
  end;
wenzelm@251
   525
wenzelm@251
   526
wenzelm@251
   527
wenzelm@583
   528
(** infer_types **)         (*exception ERROR*)
wenzelm@251
   529
wenzelm@2979
   530
(*
wenzelm@2979
   531
  ts: list of alternative parses (hopefully only one is type-correct)
wenzelm@2979
   532
  T: expected type
wenzelm@2979
   533
wenzelm@2979
   534
  def_type: partial map from indexnames to types (constrains Frees, Vars)
wenzelm@2979
   535
  def_sort: partial map from indexnames to sorts (constrains TFrees, TVars)
wenzelm@2979
   536
  used: list of already used type variables
wenzelm@2979
   537
  freeze: if true then generated parameters are turned into TFrees, else TVars
wenzelm@2979
   538
*)
wenzelm@2979
   539
wenzelm@2979
   540
fun infer_types sg def_type def_sort used freeze (ts, T) =
wenzelm@251
   541
  let
wenzelm@562
   542
    val Sg {tsig, ...} = sg;
wenzelm@3810
   543
    val prt =
wenzelm@3810
   544
      setmp Syntax.show_brackets true
wenzelm@3810
   545
        (setmp long_names true (pretty_term sg));
wenzelm@3810
   546
    val prT = setmp long_names true (pretty_typ sg);
wenzelm@3791
   547
    val infer = Type.infer_types prt prT tsig (const_type sg) def_type def_sort
wenzelm@3791
   548
      (intern_const sg) (intern_tycons sg) (intern_sort sg) used freeze;
wenzelm@169
   549
wenzelm@2979
   550
    val T' = certify_typ sg T handle TYPE (msg, _, _) => error msg;
clasohm@623
   551
wenzelm@2979
   552
    fun warn () =
wenzelm@2979
   553
      if length ts > 1 andalso length ts <= ! Syntax.ambiguity_level
nipkow@952
   554
      then (*no warning shown yet*)
wenzelm@3805
   555
        warning "Got more than one parse tree.\n\
wenzelm@3805
   556
          \Retry with smaller Syntax.ambiguity_level for more information."
nipkow@952
   557
      else ();
clasohm@623
   558
wenzelm@2979
   559
    datatype result =
wenzelm@2979
   560
      One of int * term * (indexname * typ) list |
wenzelm@2979
   561
      Errs of string list |
wenzelm@2979
   562
      Ambigs of term list;
clasohm@623
   563
wenzelm@2979
   564
    fun process_term (res, (t, i)) =
wenzelm@2979
   565
      let val ([u], tye) = infer [T'] [t] in
wenzelm@2979
   566
        (case res of
wenzelm@2979
   567
          One (_, t0, _) => Ambigs ([u, t0])
wenzelm@2979
   568
        | Errs _ => One (i, u, tye)
wenzelm@2979
   569
        | Ambigs us => Ambigs (u :: us))
wenzelm@2979
   570
      end handle TYPE (msg, _, _) =>
wenzelm@2979
   571
        (case res of
wenzelm@2979
   572
          Errs errs => Errs (msg :: errs)
wenzelm@2979
   573
        | _ => res);
wenzelm@2979
   574
  in
wenzelm@2979
   575
    (case foldl process_term (Errs [], ts ~~ (0 upto (length ts - 1))) of
wenzelm@2979
   576
      One res =>
wenzelm@2979
   577
       (if length ts > ! Syntax.ambiguity_level then
wenzelm@3552
   578
          warning "Fortunately, only one parse tree is type correct.\n\
wenzelm@3805
   579
            \You may still want to disambiguate your grammar or your input."
wenzelm@2979
   580
        else (); res)
wenzelm@2979
   581
    | Errs errs => (warn (); error (cat_lines errs))
wenzelm@2979
   582
    | Ambigs us =>
wenzelm@3791
   583
        (warn (); error ("More than one term is type correct:\n" ^
wenzelm@2979
   584
          (cat_lines (map (Pretty.string_of o prt) us)))))
clasohm@623
   585
  end;
wenzelm@251
   586
wenzelm@251
   587
wenzelm@2979
   588
clasohm@623
   589
(** extend signature **)    (*exception ERROR*)
clasohm@623
   590
wenzelm@620
   591
(** signature extension functions **)  (*exception ERROR*)
wenzelm@386
   592
wenzelm@3791
   593
fun decls_of path name_of mfixs =
wenzelm@3810
   594
  map (fn (x, y, mx) => (full path (name_of x mx), y)) mfixs;
wenzelm@3791
   595
wenzelm@3791
   596
fun no_read _ _ _ decl = decl;
wenzelm@386
   597
wenzelm@386
   598
wenzelm@386
   599
(* add default sort *)
wenzelm@386
   600
wenzelm@3791
   601
fun ext_defsort int (syn, tsig, ctab, (path, spaces)) S =
wenzelm@3791
   602
  (syn, Type.ext_tsig_defsort tsig (if int then intrn_sort spaces S else S),
wenzelm@3791
   603
    ctab, (path, spaces));
wenzelm@386
   604
wenzelm@386
   605
wenzelm@386
   606
(* add type constructors *)
wenzelm@386
   607
wenzelm@3791
   608
fun ext_types (syn, tsig, ctab, (path, spaces)) types =
wenzelm@3791
   609
  let val decls = decls_of path Syntax.type_name types in
wenzelm@3791
   610
    (Syntax.extend_type_gram syn types,
wenzelm@3791
   611
      Type.ext_tsig_types tsig decls, ctab,
wenzelm@3791
   612
      (path, add_names spaces typeK (map fst decls)))
wenzelm@3791
   613
  end;
wenzelm@386
   614
wenzelm@386
   615
wenzelm@386
   616
(* add type abbreviations *)
wenzelm@386
   617
wenzelm@3791
   618
fun read_abbr syn tsig spaces (t, vs, rhs_src) =
wenzelm@3791
   619
  (t, vs, read_raw_typ syn tsig spaces (K None) rhs_src)
wenzelm@386
   620
    handle ERROR => error ("in type abbreviation " ^ t);
wenzelm@386
   621
wenzelm@3791
   622
fun ext_abbrs rd_abbr (syn, tsig, ctab, (path, spaces)) abbrs =
wenzelm@386
   623
  let
wenzelm@386
   624
    fun mfix_of (t, vs, _, mx) = (t, length vs, mx);
wenzelm@3791
   625
    val syn' = Syntax.extend_type_gram syn (map mfix_of abbrs);
wenzelm@386
   626
wenzelm@3791
   627
    val abbrs' =
wenzelm@3791
   628
      map (fn (t, vs, rhs, mx) =>
wenzelm@3810
   629
        (full path (Syntax.type_name t mx), vs, rhs)) abbrs;
wenzelm@3805
   630
    val spaces' = add_names spaces typeK (map #1 abbrs');
wenzelm@3805
   631
    val decls = map (rd_abbr syn' tsig spaces') abbrs';
wenzelm@386
   632
  in
wenzelm@3805
   633
    (syn', Type.ext_tsig_abbrs tsig decls, ctab, (path, spaces'))
wenzelm@386
   634
  end;
wenzelm@386
   635
wenzelm@3791
   636
fun ext_tyabbrs abbrs = ext_abbrs read_abbr abbrs;
wenzelm@3791
   637
fun ext_tyabbrs_i abbrs = ext_abbrs no_read abbrs;
wenzelm@386
   638
wenzelm@386
   639
wenzelm@386
   640
(* add type arities *)
wenzelm@386
   641
wenzelm@3791
   642
fun ext_arities int (syn, tsig, ctab, (path, spaces)) arities =
wenzelm@386
   643
  let
wenzelm@3791
   644
    fun intrn_arity (c, Ss, S) =
wenzelm@3791
   645
      (intrn spaces typeK c, map (intrn_sort spaces) Ss, intrn_sort spaces S);
wenzelm@3791
   646
    val intrn = if int then map intrn_arity else I;
wenzelm@3791
   647
    val tsig' = Type.ext_tsig_arities tsig (intrn arities);
wenzelm@3791
   648
    val log_types = Type.logical_types tsig';
wenzelm@386
   649
  in
wenzelm@3791
   650
    (Syntax.extend_log_types syn log_types, tsig', ctab, (path, spaces))
wenzelm@386
   651
  end;
wenzelm@386
   652
wenzelm@386
   653
wenzelm@386
   654
(* add term constants and syntax *)
wenzelm@386
   655
wenzelm@3805
   656
fun const_name path c mx =
wenzelm@3810
   657
  full path (Syntax.const_name c mx);
wenzelm@3805
   658
wenzelm@386
   659
fun err_in_const c =
wenzelm@386
   660
  error ("in declaration of constant " ^ quote c);
wenzelm@386
   661
wenzelm@386
   662
fun err_dup_consts cs =
wenzelm@386
   663
  error ("Duplicate declaration of constant(s) " ^ commas_quote cs);
wenzelm@386
   664
wenzelm@251
   665
wenzelm@3805
   666
fun read_const syn tsig (path, spaces) (c, ty_src, mx) =
wenzelm@3791
   667
  (c, read_raw_typ syn tsig spaces (K None) ty_src, mx)
wenzelm@3805
   668
    handle ERROR => err_in_const (const_name path c mx);
wenzelm@386
   669
wenzelm@3791
   670
fun ext_cnsts rd_const syn_only prmode (syn, tsig, ctab, (path, spaces)) raw_consts =
wenzelm@386
   671
  let
wenzelm@2979
   672
    fun prep_const (c, ty, mx) =
wenzelm@3791
   673
      (c, compress_type (Type.varifyT (Type.cert_typ tsig (Type.no_tvars ty))), mx)
wenzelm@3791
   674
        handle TYPE (msg, _, _) =>
wenzelm@3805
   675
          (error_msg msg; err_in_const (const_name path c mx));
wenzelm@386
   676
wenzelm@3805
   677
    val consts = map (prep_const o rd_const syn tsig (path, spaces)) raw_consts;
wenzelm@386
   678
    val decls =
wenzelm@386
   679
      if syn_only then []
wenzelm@3791
   680
      else decls_of path Syntax.const_name consts;
wenzelm@386
   681
  in
wenzelm@2197
   682
    (Syntax.extend_const_gram syn prmode consts, tsig,
wenzelm@386
   683
      Symtab.extend_new (ctab, decls)
wenzelm@3791
   684
        handle Symtab.DUPS cs => err_dup_consts cs,
wenzelm@3791
   685
      (path, add_names spaces constK (map fst decls)))
wenzelm@386
   686
  end;
wenzelm@386
   687
wenzelm@3791
   688
val ext_consts_i = ext_cnsts no_read false ("", true);
wenzelm@2359
   689
val ext_consts = ext_cnsts read_const false ("", true);
wenzelm@3791
   690
val ext_syntax_i = ext_cnsts no_read true ("", true);
wenzelm@2359
   691
val ext_syntax = ext_cnsts read_const true ("", true);
wenzelm@3791
   692
fun ext_modesyntax_i sg (prmode, consts) = ext_cnsts no_read true prmode sg consts;
wenzelm@2197
   693
fun ext_modesyntax sg (prmode, consts) = ext_cnsts read_const true prmode sg consts;
wenzelm@386
   694
wenzelm@386
   695
wenzelm@386
   696
(* add type classes *)
wenzelm@386
   697
wenzelm@386
   698
fun const_of_class c = c ^ "_class";
wenzelm@386
   699
wenzelm@386
   700
fun class_of_const c_class =
wenzelm@386
   701
  let
wenzelm@3791
   702
    val c = implode (take (size c_class - size "_class", explode c_class));
wenzelm@386
   703
  in
wenzelm@386
   704
    if const_of_class c = c_class then c
wenzelm@3791
   705
    else raise TERM ("class_of_const: bad name " ^ quote c_class, [])
wenzelm@386
   706
  end;
wenzelm@386
   707
wenzelm@386
   708
wenzelm@3791
   709
fun ext_classes int (syn, tsig, ctab, (path, spaces)) classes =
wenzelm@386
   710
  let
wenzelm@562
   711
    val names = map fst classes;
wenzelm@386
   712
    val consts =
wenzelm@386
   713
      map (fn c => (const_of_class c, a_itselfT --> propT, NoSyn)) names;
wenzelm@3791
   714
wenzelm@3810
   715
    val full_names = map (full path) names;
wenzelm@3791
   716
    val spaces' = add_names spaces classK full_names;
wenzelm@3791
   717
    val intrn = if int then map (intrn_class spaces') else I;
wenzelm@3791
   718
    val classes' =
wenzelm@3791
   719
      ListPair.map (fn (c, (_, cs)) => (c, intrn cs)) (full_names, classes);
wenzelm@386
   720
  in
wenzelm@386
   721
    ext_consts_i
wenzelm@3791
   722
      (Syntax.extend_consts syn names,
wenzelm@3791
   723
        Type.ext_tsig_classes tsig classes', ctab, (path, spaces'))
wenzelm@3791
   724
    consts
wenzelm@386
   725
  end;
wenzelm@386
   726
wenzelm@386
   727
wenzelm@2963
   728
(* add to classrel *)
wenzelm@421
   729
wenzelm@3791
   730
fun ext_classrel int (syn, tsig, ctab, (path, spaces)) pairs =
wenzelm@3791
   731
  let val intrn = if int then map (pairself (intrn_class spaces)) else I in
wenzelm@3791
   732
    (syn, Type.ext_tsig_classrel tsig (intrn pairs), ctab, (path, spaces))
wenzelm@3791
   733
  end;
wenzelm@421
   734
wenzelm@421
   735
wenzelm@1159
   736
(* add to syntax *)
wenzelm@386
   737
wenzelm@3791
   738
fun ext_syn extfun (syn, tsig, ctab, names) args =
wenzelm@3791
   739
  (extfun syn args, tsig, ctab, names);
wenzelm@3791
   740
wenzelm@3791
   741
wenzelm@3791
   742
(* add to path *)
wenzelm@3791
   743
wenzelm@3791
   744
fun ext_path (syn, tsig, ctab, (path, spaces)) elem =
wenzelm@3791
   745
  let
wenzelm@3791
   746
    val path' =
wenzelm@3791
   747
      if elem = ".." andalso not (null path) then fst (split_last path)
wenzelm@3791
   748
      else if elem = "/" then []
wenzelm@3791
   749
      else path @ NameSpace.unpack elem;
wenzelm@3791
   750
  in
wenzelm@3791
   751
    (syn, tsig, ctab, (path', spaces))
wenzelm@3791
   752
  end;      
wenzelm@3791
   753
wenzelm@3791
   754
wenzelm@3791
   755
(* add to name space *)
wenzelm@3791
   756
wenzelm@3791
   757
fun ext_space (syn, tsig, ctab, (path, spaces)) (kind, names) =
wenzelm@3791
   758
  (syn, tsig, ctab, (path, add_names spaces kind names));
wenzelm@386
   759
wenzelm@386
   760
wenzelm@386
   761
(* build signature *)
wenzelm@386
   762
wenzelm@386
   763
fun ext_stamps stamps name =
wenzelm@386
   764
  let
wenzelm@386
   765
    val stmps = (case stamps of ref "#" :: ss => ss | ss => ss);
wenzelm@386
   766
  in
wenzelm@386
   767
    if exists (equal name o !) stmps then
wenzelm@386
   768
      error ("Theory already contains a " ^ quote name ^ " component")
wenzelm@402
   769
    else ref name :: stmps
wenzelm@386
   770
  end;
wenzelm@386
   771
wenzelm@3867
   772
fun make_sign (syn, tsig, ctab, (path, spaces)) data stamps name =
wenzelm@3791
   773
  Sg {tsig = tsig, const_tab = ctab, syn = syn, path = path, spaces = spaces,
wenzelm@3867
   774
    data = data, stamps = ext_stamps stamps name};
wenzelm@386
   775
wenzelm@3867
   776
fun extend_sign extfun name decls
wenzelm@3867
   777
    (Sg {tsig, const_tab, syn, path, spaces, data, stamps}) =
wenzelm@3867
   778
  make_sign (extfun (syn, tsig, const_tab, (path, spaces)) decls) data stamps name;
wenzelm@386
   779
wenzelm@386
   780
wenzelm@386
   781
(* the external interfaces *)
wenzelm@386
   782
wenzelm@3791
   783
val add_classes      = extend_sign (ext_classes true) "#";
wenzelm@3791
   784
val add_classes_i    = extend_sign (ext_classes false) "#";
wenzelm@3791
   785
val add_classrel     = extend_sign (ext_classrel true) "#";
wenzelm@3791
   786
val add_classrel_i   = extend_sign (ext_classrel false) "#";
wenzelm@3791
   787
val add_defsort      = extend_sign (ext_defsort true) "#";
wenzelm@3791
   788
val add_defsort_i    = extend_sign (ext_defsort false) "#";
wenzelm@2197
   789
val add_types        = extend_sign ext_types "#";
wenzelm@2197
   790
val add_tyabbrs      = extend_sign ext_tyabbrs "#";
wenzelm@2197
   791
val add_tyabbrs_i    = extend_sign ext_tyabbrs_i "#";
wenzelm@3791
   792
val add_arities      = extend_sign (ext_arities true) "#";
wenzelm@3791
   793
val add_arities_i    = extend_sign (ext_arities false) "#";
wenzelm@2197
   794
val add_consts       = extend_sign ext_consts "#";
wenzelm@2197
   795
val add_consts_i     = extend_sign ext_consts_i "#";
wenzelm@2197
   796
val add_syntax       = extend_sign ext_syntax "#";
wenzelm@2197
   797
val add_syntax_i     = extend_sign ext_syntax_i "#";
wenzelm@2197
   798
val add_modesyntax   = extend_sign ext_modesyntax "#";
wenzelm@2197
   799
val add_modesyntax_i = extend_sign ext_modesyntax_i "#";
wenzelm@2197
   800
val add_trfuns       = extend_sign (ext_syn Syntax.extend_trfuns) "#";
wenzelm@2385
   801
val add_trfunsT      = extend_sign (ext_syn Syntax.extend_trfunsT) "#";
wenzelm@2693
   802
val add_tokentrfuns  = extend_sign (ext_syn Syntax.extend_tokentrfuns) "#";
wenzelm@2197
   803
val add_trrules      = extend_sign (ext_syn Syntax.extend_trrules) "#";
wenzelm@2197
   804
val add_trrules_i    = extend_sign (ext_syn Syntax.extend_trrules_i) "#";
wenzelm@3791
   805
val add_path         = extend_sign ext_path "#";
wenzelm@3791
   806
val add_space        = extend_sign ext_space "#";
wenzelm@3791
   807
fun add_name name sg = extend_sign K name () sg;
wenzelm@386
   808
wenzelm@386
   809
val make_draft = add_name "#";
wenzelm@386
   810
wenzelm@386
   811
wenzelm@3867
   812
(* additional signature data *)
wenzelm@3867
   813
wenzelm@3867
   814
fun map_data f (Sg {tsig, const_tab, syn, path, spaces, data, stamps}) =
wenzelm@3867
   815
  make_sign (syn, tsig, const_tab, (path, spaces)) (f data) stamps "#";
wenzelm@3867
   816
wenzelm@3877
   817
fun init_data (kind, e, ext, mrg, prt) =
wenzelm@3877
   818
  map_data (fn d => Data.init d kind e ext mrg prt);
wenzelm@3877
   819
wenzelm@3877
   820
fun get_data (Sg {data, ...}) = Data.get data;
wenzelm@3867
   821
fun put_data (kind, e) = map_data (fn d => Data.put d kind e);
wenzelm@3877
   822
fun print_data (Sg {data, ...}) kind = Data.print data kind;
wenzelm@3877
   823
wenzelm@3877
   824
(*prepare extension*)
wenzelm@3899
   825
fun prep_ext sg =
wenzelm@3899
   826
  map_data Data.prep_ext sg |> add_path "/";
wenzelm@3867
   827
wenzelm@3867
   828
wenzelm@386
   829
wenzelm@3805
   830
(** merge signatures **)    (*exception TERM*)
wenzelm@143
   831
wenzelm@3877
   832
fun merge_aux triv_only (sg1, sg2) =
wenzelm@2185
   833
  if fast_subsig (sg2, sg1) then sg1
wenzelm@2185
   834
  else if fast_subsig (sg1, sg2) then sg2
wenzelm@2185
   835
  else if subsig (sg2, sg1) then sg1
wenzelm@251
   836
  else if subsig (sg1, sg2) then sg2
wenzelm@386
   837
  else if is_draft sg1 orelse is_draft sg2 then
wenzelm@3791
   838
    raise TERM ("Illegal merge of draft signatures", [])
wenzelm@3877
   839
  else if triv_only then
wenzelm@3877
   840
    raise TERM ("Illegal non-trivial merge of signatures", [])
wenzelm@251
   841
  else
wenzelm@251
   842
    (*neither is union already; must form union*)
wenzelm@251
   843
    let
wenzelm@251
   844
      val Sg {tsig = tsig1, const_tab = const_tab1, syn = syn1,
wenzelm@3867
   845
        path = _, spaces = spaces1, data = data1, stamps = stamps1} = sg1;
wenzelm@251
   846
      val Sg {tsig = tsig2, const_tab = const_tab2, syn = syn2,
wenzelm@3867
   847
        path = _, spaces = spaces2, data = data2, stamps = stamps2} = sg2;
wenzelm@251
   848
wenzelm@386
   849
wenzelm@402
   850
      val stamps = merge_rev_lists stamps1 stamps2;
wenzelm@402
   851
      val _ =
wenzelm@402
   852
        (case duplicates (stamp_names stamps) of
wenzelm@402
   853
          [] => ()
wenzelm@3791
   854
        | dups => raise TERM ("Attempt to merge different versions of theories "
wenzelm@3791
   855
            ^ commas_quote dups, []));
wenzelm@402
   856
wenzelm@2185
   857
      val tsig = Type.merge_tsigs (tsig1, tsig2);
wenzelm@251
   858
wenzelm@251
   859
      val const_tab = Symtab.merge (op =) (const_tab1, const_tab2)
wenzelm@386
   860
        handle Symtab.DUPS cs =>
wenzelm@3791
   861
          raise TERM ("Incompatible types for constant(s) " ^ commas_quote cs, []);
wenzelm@386
   862
wenzelm@386
   863
      val syn = Syntax.merge_syntaxes syn1 syn2;
wenzelm@3791
   864
wenzelm@3791
   865
      val kinds = distinct (map fst (spaces1 @ spaces2));
wenzelm@3791
   866
      val spaces =
wenzelm@3791
   867
        kinds ~~
wenzelm@3791
   868
          ListPair.map NameSpace.merge
wenzelm@3791
   869
            (map (space_of spaces1) kinds, map (space_of spaces2) kinds);
wenzelm@3867
   870
wenzelm@3867
   871
      val data = Data.merge (data1, data2);
wenzelm@251
   872
    in
wenzelm@3791
   873
      Sg {tsig = tsig, const_tab = const_tab, syn = syn,
wenzelm@3867
   874
        path = [], spaces = spaces, data = data, stamps = stamps}
wenzelm@251
   875
    end;
wenzelm@143
   876
wenzelm@3877
   877
fun gen_merge triv sgs =
wenzelm@3877
   878
  (case handle_error (merge_aux triv) sgs of
wenzelm@3805
   879
    OK sg => sg
wenzelm@3805
   880
  | Error msg => raise TERM (msg, []));
wenzelm@3805
   881
wenzelm@3877
   882
val merge = gen_merge true;
wenzelm@3877
   883
val nontriv_merge = gen_merge false;
wenzelm@3877
   884
clasohm@0
   885
clasohm@0
   886
wenzelm@251
   887
(** the Pure signature **)
clasohm@0
   888
clasohm@922
   889
val proto_pure =
wenzelm@3867
   890
  make_sign (Syntax.pure_syn, Type.tsig0, Symtab.null, ([], [])) Data.empty [] "#"
wenzelm@410
   891
  |> add_types
wenzelm@386
   892
   (("fun", 2, NoSyn) ::
wenzelm@386
   893
    ("prop", 0, NoSyn) ::
wenzelm@386
   894
    ("itself", 1, NoSyn) ::
wenzelm@562
   895
    Syntax.pure_types)
wenzelm@3937
   896
  |> add_classes_i [(logicC, [])]
wenzelm@3791
   897
  |> add_defsort_i logicS
wenzelm@3791
   898
  |> add_arities_i
wenzelm@386
   899
   [("fun", [logicS, logicS], logicS),
wenzelm@386
   900
    ("prop", [], logicS),
wenzelm@386
   901
    ("itself", [logicS], logicS)]
wenzelm@562
   902
  |> add_syntax Syntax.pure_syntax
wenzelm@2359
   903
  |> add_modesyntax (("symbols", true), Syntax.pure_sym_syntax)
wenzelm@410
   904
  |> add_trfuns Syntax.pure_trfuns
wenzelm@2693
   905
  |> add_trfunsT Syntax.pure_trfunsT
wenzelm@3810
   906
  |> add_syntax
wenzelm@3810
   907
   [("==>", "[prop, prop] => prop", Delimfix "op ==>")]
wenzelm@410
   908
  |> add_consts
wenzelm@2197
   909
   [("==", "['a::{}, 'a] => prop", InfixrName ("==", 2)),
wenzelm@2197
   910
    ("=?=", "['a::{}, 'a] => prop", InfixrName ("=?=", 2)),
wenzelm@2211
   911
    ("==>", "[prop, prop] => prop", Mixfix ("(_/ ==> _)", [2, 1], 1)),
clasohm@865
   912
    ("all", "('a => prop) => prop", Binder ("!!", 0, 0)),
wenzelm@573
   913
    ("TYPE", "'a itself", NoSyn)]
clasohm@922
   914
  |> add_name "ProtoPure";
clasohm@922
   915
clasohm@922
   916
val pure = proto_pure
clasohm@1180
   917
  |> add_syntax Syntax.pure_appl_syntax
wenzelm@410
   918
  |> add_name "Pure";
clasohm@0
   919
clasohm@922
   920
val cpure = proto_pure
clasohm@1180
   921
  |> add_syntax Syntax.pure_applC_syntax
clasohm@922
   922
  |> add_name "CPure";
clasohm@0
   923
wenzelm@3791
   924
clasohm@0
   925
end;
wenzelm@3791
   926
wenzelm@3791
   927
wenzelm@3791
   928
val long_names = Sign.long_names;