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