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