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