src/Pure/sign.ML
author wenzelm
Mon Oct 27 10:46:36 1997 +0100 (1997-10-27)
changeset 4007 1d6aed7ff375
parent 3995 88fc1015d241
child 4017 63878e2587a7
permissions -rw-r--r--
made SML/NJ happy;
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
wenzelm@3791
   309
  fun add_typ_classes (Type (_, Ts), cs) = foldr add_typ_classes (Ts, cs)
wenzelm@3791
   310
    | add_typ_classes (TFree (_, S), cs) = S union cs
wenzelm@3791
   311
    | add_typ_classes (TVar (_, S), cs) = S union cs;
wenzelm@3791
   312
wenzelm@3791
   313
  fun add_typ_tycons (Type (c, Ts), cs) = foldr add_typ_tycons (Ts, c ins cs)
wenzelm@3791
   314
    | add_typ_tycons (_, cs) = cs;
wenzelm@3791
   315
wenzelm@3791
   316
  val add_term_classes = it_term_types add_typ_classes;
wenzelm@3791
   317
  val add_term_tycons = it_term_types add_typ_tycons;
wenzelm@3791
   318
wenzelm@3791
   319
  fun add_term_consts (Const (c, _), cs) = c ins cs
wenzelm@3791
   320
    | add_term_consts (t $ u, cs) = add_term_consts (t, add_term_consts (u, cs))
wenzelm@3791
   321
    | add_term_consts (Abs (_, _, t), cs) = add_term_consts (t, cs)
wenzelm@3791
   322
    | add_term_consts (_, cs) = cs;
wenzelm@3791
   323
wenzelm@3791
   324
wenzelm@3791
   325
  (*map classes, tycons*)
wenzelm@3791
   326
  fun map_typ f g (Type (c, Ts)) = Type (g c, map (map_typ f g) Ts)
wenzelm@3791
   327
    | map_typ f _ (TFree (x, S)) = TFree (x, map f S)
wenzelm@3791
   328
    | map_typ f _ (TVar (xi, S)) = TVar (xi, map f S);
wenzelm@3791
   329
wenzelm@3791
   330
  (*map classes, tycons, consts*)
wenzelm@3791
   331
  fun map_term f g h (Const (c, T)) = Const (h c, map_typ f g T)
wenzelm@3791
   332
    | map_term f g _ (Free (x, T)) = Free (x, map_typ f g T)
wenzelm@3791
   333
    | map_term f g _ (Var (xi, T)) = Var (xi, map_typ f g T)
wenzelm@3791
   334
    | map_term _ _ _ (t as Bound _) = t
wenzelm@3791
   335
    | map_term f g h (Abs (x, T, t)) = Abs (x, map_typ f g T, map_term f g h t)
wenzelm@3791
   336
    | map_term f g h (t $ u) = map_term f g h t $ map_term f g h u;
wenzelm@3791
   337
wenzelm@3791
   338
  (*prepare mapping of names*)
wenzelm@3791
   339
  fun mapping f add_xs t =
wenzelm@3791
   340
    let
wenzelm@3791
   341
      fun f' x = let val y = f x in if x = y then None else Some (x, y) end;
wenzelm@3791
   342
      val table = mapfilter f' (add_xs (t, []));
wenzelm@3791
   343
      fun lookup x = if_none (assoc (table, x)) x;
wenzelm@3791
   344
    in lookup end;
wenzelm@3791
   345
wenzelm@3791
   346
  (*intern / extern typ*)
wenzelm@3791
   347
  fun trn_typ trn T =
wenzelm@3791
   348
    T |> map_typ
wenzelm@3791
   349
      (mapping (trn classK) add_typ_classes T)
wenzelm@3791
   350
      (mapping (trn typeK) add_typ_tycons T);
wenzelm@3791
   351
wenzelm@3791
   352
  (*intern / extern term*)
wenzelm@3791
   353
  fun trn_term trn t =
wenzelm@3791
   354
    t |> map_term
wenzelm@3791
   355
      (mapping (trn classK) add_term_classes t)
wenzelm@3791
   356
      (mapping (trn typeK) add_term_tycons t)
wenzelm@3791
   357
      (mapping (trn constK) add_term_consts t);
wenzelm@3791
   358
wenzelm@3791
   359
wenzelm@3975
   360
  val spaces_of = #spaces o rep_sg;
wenzelm@3791
   361
wenzelm@3791
   362
in
wenzelm@3791
   363
wenzelm@3791
   364
  fun intrn_class spaces = intrn spaces classK;
wenzelm@3791
   365
  fun extrn_class spaces = extrn spaces classK;
wenzelm@3937
   366
wenzelm@3791
   367
  val intrn_sort = map o intrn_class;
wenzelm@3791
   368
  val intrn_typ = trn_typ o intrn;
wenzelm@3937
   369
  val intrn_term = trn_term o intrn;
wenzelm@3937
   370
wenzelm@3937
   371
  val extrn_sort = map o extrn_class;
wenzelm@3791
   372
  val extrn_typ = trn_typ o extrn;
wenzelm@3791
   373
  val extrn_term = trn_term o extrn;
wenzelm@3791
   374
wenzelm@3805
   375
  fun intrn_tycons spaces T =
wenzelm@3805
   376
    map_typ I (mapping (intrn spaces typeK) add_typ_tycons T) T;
wenzelm@3805
   377
wenzelm@3810
   378
  val intern = intrn o spaces_of;
wenzelm@3855
   379
  val extern = extrn o spaces_of;
wenzelm@3937
   380
  fun cond_extern sg kind = if ! long_names then I else extern sg kind;
wenzelm@3937
   381
wenzelm@3791
   382
  val intern_class = intrn_class o spaces_of;
wenzelm@3791
   383
  val intern_sort = intrn_sort o spaces_of;
wenzelm@3791
   384
  val intern_typ = intrn_typ o spaces_of;
wenzelm@3791
   385
  val intern_term = intrn_term o spaces_of;
wenzelm@3791
   386
wenzelm@3791
   387
  fun intern_tycon sg = intrn (spaces_of sg) typeK;
wenzelm@3791
   388
  fun intern_const sg = intrn (spaces_of sg) constK;
wenzelm@3937
   389
wenzelm@3791
   390
  val intern_tycons = intrn_tycons o spaces_of;
wenzelm@3791
   391
wenzelm@3975
   392
  val full_name = full o #path o rep_sg;
wenzelm@3967
   393
wenzelm@3791
   394
end;
wenzelm@620
   395
wenzelm@402
   396
clasohm@0
   397
wenzelm@3937
   398
(** pretty printing of terms and types **)
wenzelm@3937
   399
wenzelm@3975
   400
fun pretty_term (sg as Sg (_, {syn, spaces, ...})) t =
wenzelm@3937
   401
  Syntax.pretty_term syn
wenzelm@3975
   402
    ("CPure" mem_string (stamp_names_of sg))
wenzelm@3937
   403
    (if ! long_names then t else extrn_term spaces t);
wenzelm@3937
   404
wenzelm@3975
   405
fun pretty_typ (Sg (_, {syn, spaces, ...})) T =
wenzelm@3937
   406
  Syntax.pretty_typ syn
wenzelm@3937
   407
    (if ! long_names then T else extrn_typ spaces T);
wenzelm@3937
   408
wenzelm@3975
   409
fun pretty_sort (Sg (_, {syn, spaces, ...})) S =
wenzelm@3937
   410
  Syntax.pretty_sort syn
wenzelm@3937
   411
    (if ! long_names then S else extrn_sort spaces S);
wenzelm@3937
   412
wenzelm@3937
   413
fun pretty_classrel sg (c1, c2) = Pretty.block
wenzelm@3937
   414
  [pretty_sort sg [c1], Pretty.str " <", Pretty.brk 1, pretty_sort sg [c2]];
wenzelm@3937
   415
wenzelm@3937
   416
fun pretty_arity sg (t, Ss, S) =
wenzelm@3937
   417
  let
wenzelm@3937
   418
    val t' = cond_extern sg typeK t;
wenzelm@3937
   419
    val dom =
wenzelm@3937
   420
      if null Ss then []
wenzelm@3937
   421
      else [Pretty.list "(" ")" (map (pretty_sort sg) Ss), Pretty.brk 1];
wenzelm@3937
   422
  in
wenzelm@3937
   423
    Pretty.block
wenzelm@3937
   424
      ([Pretty.str (t' ^ " ::"), Pretty.brk 1] @ dom @ [pretty_sort sg S])
wenzelm@3937
   425
  end;
wenzelm@3937
   426
wenzelm@3937
   427
fun string_of_term sg t = Pretty.string_of (pretty_term sg t);
wenzelm@3937
   428
fun string_of_typ sg T = Pretty.string_of (pretty_typ sg T);
wenzelm@3937
   429
fun string_of_sort sg S = Pretty.string_of (pretty_sort sg S);
wenzelm@3937
   430
wenzelm@3937
   431
fun str_of_sort sg S = Pretty.str_of (pretty_sort sg S);
wenzelm@3937
   432
fun str_of_classrel sg c1_c2 = Pretty.str_of (pretty_classrel sg c1_c2);
wenzelm@3937
   433
fun str_of_arity sg ar = Pretty.str_of (pretty_arity sg ar);
wenzelm@3937
   434
wenzelm@3937
   435
fun pprint_term sg = Pretty.pprint o Pretty.quote o (pretty_term sg);
wenzelm@3937
   436
fun pprint_typ sg = Pretty.pprint o Pretty.quote o (pretty_typ sg);
wenzelm@3937
   437
wenzelm@3937
   438
wenzelm@3937
   439
wenzelm@251
   440
(** print signature **)
wenzelm@251
   441
wenzelm@251
   442
fun print_sg sg =
wenzelm@251
   443
  let
wenzelm@3937
   444
    fun prt_cls c = pretty_sort sg [c];
wenzelm@3937
   445
    fun prt_sort S = pretty_sort sg S;
wenzelm@3937
   446
    fun prt_arity t (c, Ss) = pretty_arity sg (t, Ss, [c]);
wenzelm@3937
   447
    fun prt_typ ty = Pretty.quote (pretty_typ sg ty);
wenzelm@3975
   448
wenzelm@3975
   449
    val ext_class = cond_extern sg classK;
wenzelm@3975
   450
    val ext_tycon = cond_extern sg typeK;
wenzelm@3975
   451
    val ext_const = cond_extern sg constK;
wenzelm@3791
   452
wenzelm@3791
   453
wenzelm@3791
   454
    fun pretty_space (kind, space) = Pretty.block (Pretty.breaks
wenzelm@3791
   455
      (map Pretty.str (kind ^ ":" :: map quote (NameSpace.dest space))));
wenzelm@251
   456
wenzelm@3791
   457
    fun pretty_classes cs = Pretty.block
wenzelm@3791
   458
      (Pretty.breaks (Pretty.str "classes:" :: map prt_cls cs));
wenzelm@251
   459
wenzelm@3791
   460
    fun pretty_classrel (c, cs) = Pretty.block
wenzelm@3791
   461
      (prt_cls c :: Pretty.str " <" :: Pretty.brk 1 ::
wenzelm@3791
   462
        Pretty.commas (map prt_cls cs));
wenzelm@3791
   463
wenzelm@3791
   464
    fun pretty_default S = Pretty.block
wenzelm@3937
   465
      [Pretty.str "default:", Pretty.brk 1, pretty_sort sg S];
wenzelm@251
   466
wenzelm@3937
   467
    fun pretty_ty (t, n) = Pretty.block
wenzelm@3975
   468
      [Pretty.str (ext_tycon t), Pretty.str (" " ^ string_of_int n)];
wenzelm@251
   469
wenzelm@3937
   470
    fun pretty_abbr (t, (vs, rhs)) = Pretty.block
wenzelm@3937
   471
      [prt_typ (Type (t, map (fn v => TVar ((v, 0), [])) vs)),
wenzelm@3791
   472
        Pretty.str " =", Pretty.brk 1, prt_typ rhs];
wenzelm@251
   473
wenzelm@3937
   474
    fun pretty_arities (t, ars) = map (prt_arity t) ars;
wenzelm@251
   475
wenzelm@3791
   476
    fun pretty_const (c, ty) = Pretty.block
wenzelm@3975
   477
      [Pretty.str c, Pretty.str " ::", Pretty.brk 1, prt_typ ty];
wenzelm@251
   478
wenzelm@3975
   479
    val Sg (_, {self = _, tsig, const_tab, syn = _, path, spaces, data}) = sg;
wenzelm@3810
   480
    val spaces' = sort (fn ((k1, _), (k2, _)) => k1 < k2) spaces;
wenzelm@2963
   481
    val {classes, classrel, default, tycons, abbrs, arities} =
wenzelm@2185
   482
      Type.rep_tsig tsig;
wenzelm@3975
   483
    val consts = sort_wrt fst (map (apfst ext_const) (Symtab.dest const_tab));
wenzelm@251
   484
  in
wenzelm@3975
   485
    Pretty.writeln (Pretty.strs ("stamps:" :: stamp_names_of sg));
wenzelm@3877
   486
    Pretty.writeln (Pretty.strs ("data:" :: Data.kinds data));
wenzelm@3791
   487
    Pretty.writeln (Pretty.strs ["name entry path:", NameSpace.pack path]);
wenzelm@3810
   488
    Pretty.writeln (Pretty.big_list "name spaces:" (map pretty_space spaces'));
wenzelm@3791
   489
    Pretty.writeln (pretty_classes classes);
wenzelm@3791
   490
    Pretty.writeln (Pretty.big_list "class relation:" (map pretty_classrel classrel));
wenzelm@251
   491
    Pretty.writeln (pretty_default default);
wenzelm@3791
   492
    Pretty.writeln (Pretty.big_list "type constructors:" (map pretty_ty tycons));
wenzelm@3791
   493
    Pretty.writeln (Pretty.big_list "type abbreviations:" (map pretty_abbr abbrs));
wenzelm@3791
   494
    Pretty.writeln (Pretty.big_list "type arities:" (flat (map pretty_arities arities)));
wenzelm@3975
   495
    Pretty.writeln (Pretty.big_list "consts:" (map pretty_const consts))
wenzelm@251
   496
  end;
wenzelm@251
   497
wenzelm@251
   498
wenzelm@251
   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@3791
   505
fun read_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@3791
   509
  
wenzelm@562
   510
(*read and certify typ wrt a signature*)
wenzelm@3975
   511
fun read_typ (sg as Sg (_, {tsig, syn, spaces, ...}), def_sort) str =
wenzelm@3969
   512
  (check_stale sg;
wenzelm@3969
   513
    Type.cert_typ tsig (read_raw_typ syn tsig spaces def_sort str)
wenzelm@3969
   514
      handle TYPE (msg, _, _) => (error_msg msg; err_in_type str));
wenzelm@562
   515
wenzelm@562
   516
wenzelm@562
   517
wenzelm@386
   518
(** certify types and terms **)   (*exception TYPE*)
wenzelm@251
   519
wenzelm@3975
   520
val certify_typ = Type.cert_typ o tsig_of;
wenzelm@251
   521
wenzelm@2979
   522
(*check for duplicate TVars with distinct sorts*)
wenzelm@2979
   523
fun nodup_TVars (tvars, T) =
wenzelm@2979
   524
  (case T of
wenzelm@2979
   525
    Type (_, Ts) => nodup_TVars_list (tvars, Ts)
wenzelm@2979
   526
  | TFree _ => tvars
wenzelm@2979
   527
  | TVar (v as (a, S)) =>
wenzelm@2979
   528
      (case assoc_string_int (tvars, a) of
wenzelm@2979
   529
        Some S' =>
wenzelm@2979
   530
          if S = S' then tvars
wenzelm@3791
   531
          else raise TYPE ("Type variable " ^ Syntax.string_of_vname a ^
wenzelm@3791
   532
            " has two distinct sorts", [TVar (a, S'), T], [])
wenzelm@2979
   533
      | None => v :: tvars))
wenzelm@2979
   534
(*equivalent to foldl nodup_TVars_list, but 3X faster under Poly/ML*)
wenzelm@2979
   535
and nodup_TVars_list (tvars, []) = tvars
wenzelm@2979
   536
  | nodup_TVars_list (tvars, T :: Ts) =
wenzelm@2979
   537
      nodup_TVars_list (nodup_TVars (tvars, T), Ts);
nipkow@1494
   538
wenzelm@2979
   539
(*check for duplicate Vars with distinct types*)
nipkow@1494
   540
fun nodup_Vars tm =
wenzelm@2979
   541
  let
wenzelm@2979
   542
    fun nodups vars tvars tm =
wenzelm@2979
   543
      (case tm of
wenzelm@2979
   544
        Const (c, T) => (vars, nodup_TVars (tvars, T))
wenzelm@2979
   545
      | Free (a, T) => (vars, nodup_TVars (tvars, T))
wenzelm@2979
   546
      | Var (v as (ixn, T)) =>
wenzelm@2979
   547
          (case assoc_string_int (vars, ixn) of
wenzelm@2979
   548
            Some T' =>
wenzelm@2979
   549
              if T = T' then (vars, nodup_TVars (tvars, T))
wenzelm@3791
   550
              else raise TYPE ("Variable " ^ Syntax.string_of_vname ixn ^
wenzelm@3791
   551
                " has two distinct types", [T', T], [])
wenzelm@2979
   552
          | None => (v :: vars, tvars))
wenzelm@2979
   553
      | Bound _ => (vars, tvars)
wenzelm@3791
   554
      | Abs (_, T, t) => nodups vars (nodup_TVars (tvars, T)) t
wenzelm@2979
   555
      | s $ t =>
wenzelm@2979
   556
          let val (vars',tvars') = nodups vars tvars s in
wenzelm@2979
   557
            nodups vars' tvars' t
wenzelm@2979
   558
          end);
wenzelm@2979
   559
  in nodups [] [] tm; () end;
wenzelm@2979
   560
wenzelm@251
   561
wenzelm@386
   562
fun mapfilt_atoms f (Abs (_, _, t)) = mapfilt_atoms f t
wenzelm@386
   563
  | mapfilt_atoms f (t $ u) = mapfilt_atoms f t @ mapfilt_atoms f u
wenzelm@386
   564
  | mapfilt_atoms f a = (case f a of Some y => [y] | None => []);
clasohm@0
   565
wenzelm@2979
   566
wenzelm@3975
   567
fun certify_term sg tm =
wenzelm@251
   568
  let
wenzelm@3969
   569
    val _ = check_stale sg;
wenzelm@3975
   570
    val tsig = tsig_of sg;
wenzelm@3969
   571
wenzelm@3975
   572
    fun show_const a T = quote a ^ " :: " ^ quote (string_of_typ sg T);
wenzelm@169
   573
wenzelm@251
   574
    fun atom_err (Const (a, T)) =
wenzelm@3975
   575
        (case const_type sg a of
wenzelm@3975
   576
          None => Some ("Undeclared constant " ^ show_const a T)
wenzelm@3975
   577
        | Some U =>
wenzelm@3975
   578
            if Type.typ_instance (tsig, T, U) then None
wenzelm@3975
   579
            else Some ("Illegal type for constant " ^ show_const a T))
wenzelm@251
   580
      | atom_err (Var ((x, i), _)) =
wenzelm@251
   581
          if i < 0 then Some ("Negative index for Var " ^ quote x) else None
wenzelm@251
   582
      | atom_err _ = None;
wenzelm@251
   583
wenzelm@251
   584
    val norm_tm =
wenzelm@2185
   585
      (case it_term_types (Type.typ_errors tsig) (tm, []) of
wenzelm@2185
   586
        [] => map_term_types (Type.norm_typ tsig) tm
wenzelm@3791
   587
      | errs => raise TYPE (cat_lines errs, [], [tm]));
nipkow@1494
   588
    val _ = nodup_Vars norm_tm;
wenzelm@251
   589
  in
wenzelm@386
   590
    (case mapfilt_atoms atom_err norm_tm of
wenzelm@251
   591
      [] => (norm_tm, type_of norm_tm, maxidx_of_term norm_tm)
wenzelm@3791
   592
    | errs => raise TYPE (cat_lines errs, [], [norm_tm]))
wenzelm@251
   593
  end;
wenzelm@251
   594
wenzelm@251
   595
wenzelm@251
   596
wenzelm@583
   597
(** infer_types **)         (*exception ERROR*)
wenzelm@251
   598
wenzelm@2979
   599
(*
wenzelm@2979
   600
  ts: list of alternative parses (hopefully only one is type-correct)
wenzelm@2979
   601
  T: expected type
wenzelm@2979
   602
wenzelm@2979
   603
  def_type: partial map from indexnames to types (constrains Frees, Vars)
wenzelm@2979
   604
  def_sort: partial map from indexnames to sorts (constrains TFrees, TVars)
wenzelm@2979
   605
  used: list of already used type variables
wenzelm@2979
   606
  freeze: if true then generated parameters are turned into TFrees, else TVars
wenzelm@2979
   607
*)
wenzelm@2979
   608
wenzelm@2979
   609
fun infer_types sg def_type def_sort used freeze (ts, T) =
wenzelm@251
   610
  let
wenzelm@3975
   611
    val tsig = tsig_of sg;
wenzelm@3810
   612
    val prt =
wenzelm@3810
   613
      setmp Syntax.show_brackets true
wenzelm@3810
   614
        (setmp long_names true (pretty_term sg));
wenzelm@3810
   615
    val prT = setmp long_names true (pretty_typ sg);
wenzelm@3791
   616
    val infer = Type.infer_types prt prT tsig (const_type sg) def_type def_sort
wenzelm@3791
   617
      (intern_const sg) (intern_tycons sg) (intern_sort sg) used freeze;
wenzelm@169
   618
wenzelm@2979
   619
    val T' = certify_typ sg T handle TYPE (msg, _, _) => error msg;
clasohm@623
   620
wenzelm@2979
   621
    fun warn () =
wenzelm@2979
   622
      if length ts > 1 andalso length ts <= ! Syntax.ambiguity_level
nipkow@952
   623
      then (*no warning shown yet*)
wenzelm@3805
   624
        warning "Got more than one parse tree.\n\
wenzelm@3805
   625
          \Retry with smaller Syntax.ambiguity_level for more information."
nipkow@952
   626
      else ();
clasohm@623
   627
wenzelm@2979
   628
    datatype result =
wenzelm@2979
   629
      One of int * term * (indexname * typ) list |
wenzelm@2979
   630
      Errs of string list |
wenzelm@2979
   631
      Ambigs of term list;
clasohm@623
   632
wenzelm@2979
   633
    fun process_term (res, (t, i)) =
wenzelm@2979
   634
      let val ([u], tye) = infer [T'] [t] in
wenzelm@2979
   635
        (case res of
wenzelm@2979
   636
          One (_, t0, _) => Ambigs ([u, t0])
wenzelm@2979
   637
        | Errs _ => One (i, u, tye)
wenzelm@2979
   638
        | Ambigs us => Ambigs (u :: us))
wenzelm@2979
   639
      end handle TYPE (msg, _, _) =>
wenzelm@2979
   640
        (case res of
wenzelm@2979
   641
          Errs errs => Errs (msg :: errs)
wenzelm@2979
   642
        | _ => res);
wenzelm@2979
   643
  in
wenzelm@2979
   644
    (case foldl process_term (Errs [], ts ~~ (0 upto (length ts - 1))) of
wenzelm@2979
   645
      One res =>
wenzelm@2979
   646
       (if length ts > ! Syntax.ambiguity_level then
wenzelm@3552
   647
          warning "Fortunately, only one parse tree is type correct.\n\
wenzelm@3805
   648
            \You may still want to disambiguate your grammar or your input."
wenzelm@2979
   649
        else (); res)
wenzelm@2979
   650
    | Errs errs => (warn (); error (cat_lines errs))
wenzelm@2979
   651
    | Ambigs us =>
wenzelm@3791
   652
        (warn (); error ("More than one term is type correct:\n" ^
wenzelm@2979
   653
          (cat_lines (map (Pretty.string_of o prt) us)))))
clasohm@623
   654
  end;
wenzelm@251
   655
wenzelm@251
   656
wenzelm@2979
   657
clasohm@623
   658
(** extend signature **)    (*exception ERROR*)
clasohm@623
   659
wenzelm@620
   660
(** signature extension functions **)  (*exception ERROR*)
wenzelm@386
   661
wenzelm@3791
   662
fun decls_of path name_of mfixs =
wenzelm@3810
   663
  map (fn (x, y, mx) => (full path (name_of x mx), y)) mfixs;
wenzelm@3791
   664
wenzelm@3791
   665
fun no_read _ _ _ decl = decl;
wenzelm@386
   666
wenzelm@386
   667
wenzelm@386
   668
(* add default sort *)
wenzelm@386
   669
wenzelm@3967
   670
fun ext_defsort int (syn, tsig, ctab, (path, spaces), data) S =
wenzelm@3791
   671
  (syn, Type.ext_tsig_defsort tsig (if int then intrn_sort spaces S else S),
wenzelm@3967
   672
    ctab, (path, spaces), data);
wenzelm@386
   673
wenzelm@386
   674
wenzelm@386
   675
(* add type constructors *)
wenzelm@386
   676
wenzelm@3967
   677
fun ext_types (syn, tsig, ctab, (path, spaces), data) types =
wenzelm@3791
   678
  let val decls = decls_of path Syntax.type_name types in
wenzelm@3791
   679
    (Syntax.extend_type_gram syn types,
wenzelm@3791
   680
      Type.ext_tsig_types tsig decls, ctab,
wenzelm@3967
   681
      (path, add_names spaces typeK (map fst decls)), data)
wenzelm@3791
   682
  end;
wenzelm@386
   683
wenzelm@386
   684
wenzelm@386
   685
(* add type abbreviations *)
wenzelm@386
   686
wenzelm@3791
   687
fun read_abbr syn tsig spaces (t, vs, rhs_src) =
wenzelm@3791
   688
  (t, vs, read_raw_typ syn tsig spaces (K None) rhs_src)
wenzelm@386
   689
    handle ERROR => error ("in type abbreviation " ^ t);
wenzelm@386
   690
wenzelm@3967
   691
fun ext_abbrs rd_abbr (syn, tsig, ctab, (path, spaces), data) abbrs =
wenzelm@386
   692
  let
wenzelm@386
   693
    fun mfix_of (t, vs, _, mx) = (t, length vs, mx);
wenzelm@3791
   694
    val syn' = Syntax.extend_type_gram syn (map mfix_of abbrs);
wenzelm@386
   695
wenzelm@3791
   696
    val abbrs' =
wenzelm@3791
   697
      map (fn (t, vs, rhs, mx) =>
wenzelm@3810
   698
        (full path (Syntax.type_name t mx), vs, rhs)) abbrs;
wenzelm@3805
   699
    val spaces' = add_names spaces typeK (map #1 abbrs');
wenzelm@3805
   700
    val decls = map (rd_abbr syn' tsig spaces') abbrs';
wenzelm@386
   701
  in
wenzelm@3967
   702
    (syn', Type.ext_tsig_abbrs tsig decls, ctab, (path, spaces'), data)
wenzelm@386
   703
  end;
wenzelm@386
   704
wenzelm@3791
   705
fun ext_tyabbrs abbrs = ext_abbrs read_abbr abbrs;
wenzelm@3791
   706
fun ext_tyabbrs_i abbrs = ext_abbrs no_read abbrs;
wenzelm@386
   707
wenzelm@386
   708
wenzelm@386
   709
(* add type arities *)
wenzelm@386
   710
wenzelm@3967
   711
fun ext_arities int (syn, tsig, ctab, (path, spaces), data) arities =
wenzelm@386
   712
  let
wenzelm@3791
   713
    fun intrn_arity (c, Ss, S) =
wenzelm@3791
   714
      (intrn spaces typeK c, map (intrn_sort spaces) Ss, intrn_sort spaces S);
wenzelm@3791
   715
    val intrn = if int then map intrn_arity else I;
wenzelm@3791
   716
    val tsig' = Type.ext_tsig_arities tsig (intrn arities);
wenzelm@3791
   717
    val log_types = Type.logical_types tsig';
wenzelm@386
   718
  in
wenzelm@3967
   719
    (Syntax.extend_log_types syn log_types, tsig', ctab, (path, spaces), data)
wenzelm@386
   720
  end;
wenzelm@386
   721
wenzelm@386
   722
wenzelm@386
   723
(* add term constants and syntax *)
wenzelm@386
   724
wenzelm@3805
   725
fun const_name path c mx =
wenzelm@3810
   726
  full path (Syntax.const_name c mx);
wenzelm@3805
   727
wenzelm@386
   728
fun err_in_const c =
wenzelm@386
   729
  error ("in declaration of constant " ^ quote c);
wenzelm@386
   730
wenzelm@386
   731
fun err_dup_consts cs =
wenzelm@386
   732
  error ("Duplicate declaration of constant(s) " ^ commas_quote cs);
wenzelm@386
   733
wenzelm@251
   734
wenzelm@3805
   735
fun read_const syn tsig (path, spaces) (c, ty_src, mx) =
wenzelm@3791
   736
  (c, read_raw_typ syn tsig spaces (K None) ty_src, mx)
wenzelm@3805
   737
    handle ERROR => err_in_const (const_name path c mx);
wenzelm@386
   738
wenzelm@3967
   739
fun ext_cnsts rd_const syn_only prmode (syn, tsig, ctab, (path, spaces), data) raw_consts =
wenzelm@386
   740
  let
wenzelm@2979
   741
    fun prep_const (c, ty, mx) =
wenzelm@3791
   742
      (c, compress_type (Type.varifyT (Type.cert_typ tsig (Type.no_tvars ty))), mx)
wenzelm@3791
   743
        handle TYPE (msg, _, _) =>
wenzelm@3805
   744
          (error_msg msg; err_in_const (const_name path c mx));
wenzelm@386
   745
wenzelm@3805
   746
    val consts = map (prep_const o rd_const syn tsig (path, spaces)) raw_consts;
wenzelm@386
   747
    val decls =
wenzelm@386
   748
      if syn_only then []
wenzelm@3791
   749
      else decls_of path Syntax.const_name consts;
wenzelm@386
   750
  in
wenzelm@2197
   751
    (Syntax.extend_const_gram syn prmode consts, tsig,
wenzelm@386
   752
      Symtab.extend_new (ctab, decls)
wenzelm@3791
   753
        handle Symtab.DUPS cs => err_dup_consts cs,
wenzelm@3967
   754
      (path, add_names spaces constK (map fst decls)), data)
wenzelm@386
   755
  end;
wenzelm@386
   756
wenzelm@4007
   757
fun ext_consts_i sg = ext_cnsts no_read false ("", true) sg;
wenzelm@4007
   758
fun ext_consts sg = ext_cnsts read_const false ("", true) sg;
wenzelm@4007
   759
fun ext_syntax_i sg = ext_cnsts no_read true ("", true) sg;
wenzelm@4007
   760
fun ext_syntax sg = ext_cnsts read_const true ("", true) sg;
wenzelm@3791
   761
fun ext_modesyntax_i sg (prmode, consts) = ext_cnsts no_read true prmode sg consts;
wenzelm@2197
   762
fun ext_modesyntax sg (prmode, consts) = ext_cnsts read_const true prmode sg consts;
wenzelm@386
   763
wenzelm@386
   764
wenzelm@386
   765
(* add type classes *)
wenzelm@386
   766
wenzelm@386
   767
fun const_of_class c = c ^ "_class";
wenzelm@386
   768
wenzelm@386
   769
fun class_of_const c_class =
wenzelm@386
   770
  let
wenzelm@3791
   771
    val c = implode (take (size c_class - size "_class", explode c_class));
wenzelm@386
   772
  in
wenzelm@386
   773
    if const_of_class c = c_class then c
wenzelm@3791
   774
    else raise TERM ("class_of_const: bad name " ^ quote c_class, [])
wenzelm@386
   775
  end;
wenzelm@386
   776
wenzelm@386
   777
wenzelm@3967
   778
fun ext_classes int (syn, tsig, ctab, (path, spaces), data) classes =
wenzelm@386
   779
  let
wenzelm@562
   780
    val names = map fst classes;
wenzelm@386
   781
    val consts =
wenzelm@386
   782
      map (fn c => (const_of_class c, a_itselfT --> propT, NoSyn)) names;
wenzelm@3791
   783
wenzelm@3810
   784
    val full_names = map (full path) names;
wenzelm@3791
   785
    val spaces' = add_names spaces classK full_names;
wenzelm@3791
   786
    val intrn = if int then map (intrn_class spaces') else I;
wenzelm@3791
   787
    val classes' =
wenzelm@3791
   788
      ListPair.map (fn (c, (_, cs)) => (c, intrn cs)) (full_names, classes);
wenzelm@386
   789
  in
wenzelm@386
   790
    ext_consts_i
wenzelm@3791
   791
      (Syntax.extend_consts syn names,
wenzelm@3967
   792
        Type.ext_tsig_classes tsig classes', ctab, (path, spaces'), data)
wenzelm@3791
   793
    consts
wenzelm@386
   794
  end;
wenzelm@386
   795
wenzelm@386
   796
wenzelm@2963
   797
(* add to classrel *)
wenzelm@421
   798
wenzelm@3967
   799
fun ext_classrel int (syn, tsig, ctab, (path, spaces), data) pairs =
wenzelm@3791
   800
  let val intrn = if int then map (pairself (intrn_class spaces)) else I in
wenzelm@3967
   801
    (syn, Type.ext_tsig_classrel tsig (intrn pairs), ctab, (path, spaces), data)
wenzelm@3791
   802
  end;
wenzelm@421
   803
wenzelm@421
   804
wenzelm@1159
   805
(* add to syntax *)
wenzelm@386
   806
wenzelm@3967
   807
fun ext_syn extfun (syn, tsig, ctab, names, data) args =
wenzelm@3967
   808
  (extfun syn args, tsig, ctab, names, data);
wenzelm@3791
   809
wenzelm@3791
   810
wenzelm@3791
   811
(* add to path *)
wenzelm@3791
   812
wenzelm@3967
   813
fun ext_path (syn, tsig, ctab, (path, spaces), data) elem =
wenzelm@3791
   814
  let
wenzelm@3791
   815
    val path' =
wenzelm@3791
   816
      if elem = ".." andalso not (null path) then fst (split_last path)
wenzelm@3791
   817
      else if elem = "/" then []
wenzelm@3791
   818
      else path @ NameSpace.unpack elem;
wenzelm@3791
   819
  in
wenzelm@3967
   820
    (syn, tsig, ctab, (path', spaces), data)
wenzelm@3791
   821
  end;      
wenzelm@3791
   822
wenzelm@3791
   823
wenzelm@3791
   824
(* add to name space *)
wenzelm@3791
   825
wenzelm@3967
   826
fun ext_space (syn, tsig, ctab, (path, spaces), data) (kind, names) =
wenzelm@3967
   827
  (syn, tsig, ctab, (path, add_names spaces kind names), data);
wenzelm@386
   828
wenzelm@386
   829
wenzelm@3967
   830
(* signature data *)
wenzelm@386
   831
wenzelm@3995
   832
fun ext_init_data (syn, tsig, ctab, names, data) (kind, (e, ext, mrg, prt)) =
wenzelm@3967
   833
  (syn, tsig, ctab, names, Data.init data kind e ext mrg prt);
wenzelm@386
   834
wenzelm@3967
   835
fun ext_put_data (syn, tsig, ctab, names, data) (kind, e) =
wenzelm@3967
   836
  (syn, tsig, ctab, names, Data.put data kind e);
wenzelm@386
   837
wenzelm@386
   838
wenzelm@386
   839
(* the external interfaces *)
wenzelm@386
   840
wenzelm@3975
   841
val add_classes      = extend_sign true (ext_classes true) "#";
wenzelm@3975
   842
val add_classes_i    = extend_sign true (ext_classes false) "#";
wenzelm@3975
   843
val add_classrel     = extend_sign true (ext_classrel true) "#";
wenzelm@3975
   844
val add_classrel_i   = extend_sign true (ext_classrel false) "#";
wenzelm@3975
   845
val add_defsort      = extend_sign true (ext_defsort true) "#";
wenzelm@3975
   846
val add_defsort_i    = extend_sign true (ext_defsort false) "#";
wenzelm@3975
   847
val add_types        = extend_sign true ext_types "#";
wenzelm@3975
   848
val add_tyabbrs      = extend_sign true ext_tyabbrs "#";
wenzelm@3975
   849
val add_tyabbrs_i    = extend_sign true ext_tyabbrs_i "#";
wenzelm@3975
   850
val add_arities      = extend_sign true (ext_arities true) "#";
wenzelm@3975
   851
val add_arities_i    = extend_sign true (ext_arities false) "#";
wenzelm@3975
   852
val add_consts       = extend_sign true ext_consts "#";
wenzelm@3975
   853
val add_consts_i     = extend_sign true ext_consts_i "#";
wenzelm@3975
   854
val add_syntax       = extend_sign true ext_syntax "#";
wenzelm@3975
   855
val add_syntax_i     = extend_sign true ext_syntax_i "#";
wenzelm@3975
   856
val add_modesyntax   = extend_sign true ext_modesyntax "#";
wenzelm@3975
   857
val add_modesyntax_i = extend_sign true ext_modesyntax_i "#";
wenzelm@3975
   858
val add_trfuns       = extend_sign true (ext_syn Syntax.extend_trfuns) "#";
wenzelm@3975
   859
val add_trfunsT      = extend_sign true (ext_syn Syntax.extend_trfunsT) "#";
wenzelm@3975
   860
val add_tokentrfuns  = extend_sign true (ext_syn Syntax.extend_tokentrfuns) "#";
wenzelm@3975
   861
val add_trrules      = extend_sign true (ext_syn Syntax.extend_trrules) "#";
wenzelm@3975
   862
val add_trrules_i    = extend_sign true (ext_syn Syntax.extend_trrules_i) "#";
wenzelm@3975
   863
val add_path         = extend_sign true ext_path "#";
wenzelm@3975
   864
val add_space        = extend_sign true ext_space "#";
wenzelm@3975
   865
val init_data        = extend_sign true ext_init_data "#";
wenzelm@3975
   866
val put_data         = extend_sign true ext_put_data "#";
wenzelm@3975
   867
fun add_name name sg = extend_sign true K name () sg;
wenzelm@3975
   868
fun prep_ext sg      = extend_sign false K "#" () sg;
wenzelm@386
   869
wenzelm@386
   870
wenzelm@3867
   871
wenzelm@3967
   872
(** merge signatures **)    (*exception TERM*)
wenzelm@3867
   873
wenzelm@3967
   874
(* merge of sg_refs -- trivial only *)
wenzelm@3877
   875
wenzelm@3967
   876
fun merge_refs (sgr1 as SgRef (Some (ref sg1)),
wenzelm@3967
   877
        sgr2 as SgRef (Some (ref sg2))) =
wenzelm@3967
   878
      if fast_subsig (sg2, sg1) then sgr1
wenzelm@3967
   879
      else if fast_subsig (sg1, sg2) then sgr2
wenzelm@3967
   880
      else if subsig (sg2, sg1) then sgr1
wenzelm@3967
   881
      else if subsig (sg1, sg2) then sgr2
wenzelm@3967
   882
      else raise TERM ("Attempt to do non-trivial merge of signatures", [])
wenzelm@3967
   883
  | merge_refs _ = sys_error "Sign.merge_refs";
wenzelm@3867
   884
wenzelm@3867
   885
wenzelm@386
   886
wenzelm@3967
   887
(* proper merge *)
wenzelm@143
   888
wenzelm@3967
   889
fun merge_aux (sg1, sg2) =
wenzelm@3967
   890
  if subsig (sg2, sg1) then sg1
wenzelm@251
   891
  else if subsig (sg1, sg2) then sg2
wenzelm@386
   892
  else if is_draft sg1 orelse is_draft sg2 then
wenzelm@3967
   893
    raise TERM ("Attempt to merge draft signatures", [])
wenzelm@251
   894
  else
wenzelm@251
   895
    (*neither is union already; must form union*)
wenzelm@251
   896
    let
wenzelm@3975
   897
      val Sg ({id = _, stamps = stamps1}, {self = _, tsig = tsig1, const_tab = const_tab1,
wenzelm@3975
   898
        syn = syn1, path = _, spaces = spaces1, data = data1}) = sg1;
wenzelm@3975
   899
      val Sg ({id = _, stamps = stamps2}, {self = _, tsig = tsig2, const_tab = const_tab2,
wenzelm@3975
   900
        syn = syn2, path = _, spaces = spaces2, data = data2}) = sg2;
wenzelm@386
   901
wenzelm@3967
   902
      val id = ref "";
wenzelm@3975
   903
      val self_ref = ref sg1;                   (*dummy value*)
wenzelm@3967
   904
      val self = SgRef (Some self_ref);
wenzelm@402
   905
      val stamps = merge_rev_lists stamps1 stamps2;
wenzelm@402
   906
      val _ =
wenzelm@3975
   907
        (case duplicates (map ! stamps) of
wenzelm@402
   908
          [] => ()
wenzelm@3791
   909
        | dups => raise TERM ("Attempt to merge different versions of theories "
wenzelm@3791
   910
            ^ commas_quote dups, []));
wenzelm@402
   911
wenzelm@2185
   912
      val tsig = Type.merge_tsigs (tsig1, tsig2);
wenzelm@251
   913
      val const_tab = Symtab.merge (op =) (const_tab1, const_tab2)
wenzelm@386
   914
        handle Symtab.DUPS cs =>
wenzelm@3791
   915
          raise TERM ("Incompatible types for constant(s) " ^ commas_quote cs, []);
wenzelm@386
   916
      val syn = Syntax.merge_syntaxes syn1 syn2;
wenzelm@3791
   917
wenzelm@3967
   918
      val path = [];
wenzelm@3791
   919
      val kinds = distinct (map fst (spaces1 @ spaces2));
wenzelm@3791
   920
      val spaces =
wenzelm@3791
   921
        kinds ~~
wenzelm@3791
   922
          ListPair.map NameSpace.merge
wenzelm@3791
   923
            (map (space_of spaces1) kinds, map (space_of spaces2) kinds);
wenzelm@3867
   924
wenzelm@3867
   925
      val data = Data.merge (data1, data2);
wenzelm@3967
   926
wenzelm@3967
   927
      val sign = make_sign (id, self, tsig, const_tab, syn, path, spaces, data, stamps);
wenzelm@251
   928
    in
wenzelm@3967
   929
      self_ref := sign; sign
wenzelm@251
   930
    end;
wenzelm@143
   931
wenzelm@3967
   932
fun merge sg1_sg2 =
wenzelm@3967
   933
  (case handle_error merge_aux sg1_sg2 of
wenzelm@3805
   934
    OK sg => sg
wenzelm@3805
   935
  | Error msg => raise TERM (msg, []));
wenzelm@3805
   936
clasohm@0
   937
clasohm@0
   938
wenzelm@3995
   939
(** partial Pure signature **)
clasohm@0
   940
wenzelm@3967
   941
val dummy_sg = make_sign (ref "", SgRef None, Type.tsig0,
wenzelm@3967
   942
  Symtab.null, Syntax.pure_syn, [], [], Data.empty, []);
wenzelm@3967
   943
wenzelm@3995
   944
val pre_pure =
wenzelm@3967
   945
  create_sign (SgRef (Some (ref dummy_sg))) [] "#"
wenzelm@3995
   946
    (Syntax.pure_syn, Type.tsig0, Symtab.null, ([], []), Data.empty);
clasohm@0
   947
wenzelm@3791
   948
clasohm@0
   949
end;
wenzelm@3791
   950
wenzelm@3791
   951
wenzelm@3791
   952
val long_names = Sign.long_names;