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