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