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