src/Pure/sign.ML
author wenzelm
Wed Nov 27 16:36:36 1996 +0100 (1996-11-27)
changeset 2252 d54af138f7b2
parent 2228 f381c1a98209
child 2359 97b88cafe1e8
permissions -rw-r--r--
renamed "symbolfont" to "symbols";
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 =
paulson@1501
     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@2197
    15
    stamps: string ref list}
paulson@1501
    16
  val subsig: sg * sg -> bool
paulson@1501
    17
  val eq_sg: sg * sg -> bool
paulson@1501
    18
  val same_sg: sg * sg -> bool
paulson@1501
    19
  val is_draft: sg -> bool
paulson@1501
    20
  val const_type: sg -> string -> typ option
paulson@1501
    21
  val classes: sg -> class list
paulson@1501
    22
  val subsort: sg -> sort * sort -> bool
paulson@1501
    23
  val nodup_Vars: term -> unit
paulson@1501
    24
  val norm_sort: sg -> sort -> sort
paulson@1501
    25
  val nonempty_sort: sg -> sort list -> sort -> bool
paulson@1501
    26
  val print_sg: sg -> unit
paulson@1501
    27
  val pretty_sg: sg -> Pretty.T
paulson@1501
    28
  val pprint_sg: sg -> pprint_args -> unit
paulson@1501
    29
  val pretty_term: sg -> term -> Pretty.T
paulson@1501
    30
  val pretty_typ: sg -> typ -> Pretty.T
paulson@1501
    31
  val pretty_sort: sort -> Pretty.T
paulson@1501
    32
  val string_of_term: sg -> term -> string
paulson@1501
    33
  val string_of_typ: sg -> typ -> string
paulson@1501
    34
  val pprint_term: sg -> term -> pprint_args -> unit
paulson@1501
    35
  val pprint_typ: sg -> typ -> pprint_args -> unit
paulson@1501
    36
  val certify_typ: sg -> typ -> typ
paulson@1501
    37
  val certify_term: sg -> term -> term * typ * int
paulson@1501
    38
  val read_typ: sg * (indexname -> sort option) -> string -> typ
paulson@1501
    39
  val exn_type_msg: sg -> string * typ list * term list -> string
paulson@1501
    40
  val infer_types: sg -> (indexname -> typ option) ->
paulson@1501
    41
    (indexname -> sort option) -> string list -> bool
paulson@1501
    42
    -> term list * typ -> int * term * (indexname * typ) list
paulson@1501
    43
  val add_classes: (class * class list) list -> sg -> sg
paulson@1501
    44
  val add_classrel: (class * class) list -> sg -> sg
paulson@1501
    45
  val add_defsort: sort -> sg -> sg
paulson@1501
    46
  val add_types: (string * int * mixfix) list -> sg -> sg
paulson@1501
    47
  val add_tyabbrs: (string * string list * string * mixfix) list -> sg -> sg
paulson@1501
    48
  val add_tyabbrs_i: (string * string list * typ * mixfix) list -> sg -> sg
paulson@1501
    49
  val add_arities: (string * sort list * sort) list -> sg -> sg
paulson@1501
    50
  val add_consts: (string * string * mixfix) list -> sg -> sg
paulson@1501
    51
  val add_consts_i: (string * typ * mixfix) list -> sg -> sg
paulson@1501
    52
  val add_syntax: (string * string * mixfix) list -> sg -> sg
paulson@1501
    53
  val add_syntax_i: (string * typ * mixfix) list -> sg -> sg
wenzelm@2197
    54
  val add_modesyntax: string * (string * string * mixfix) list -> sg -> sg
wenzelm@2197
    55
  val add_modesyntax_i: string * (string * typ * mixfix) list -> sg -> sg
paulson@1501
    56
  val add_trfuns:
paulson@1501
    57
    (string * (ast list -> ast)) list *
paulson@1501
    58
    (string * (term list -> term)) list *
paulson@1501
    59
    (string * (term list -> term)) list *
paulson@1501
    60
    (string * (ast list -> ast)) list -> sg -> sg
paulson@1810
    61
  val add_trrules: (string * string) Syntax.trrule list -> sg -> sg
paulson@1810
    62
  val add_trrules_i: ast Syntax.trrule list -> sg -> sg
paulson@1501
    63
  val add_name: string -> sg -> sg
paulson@1501
    64
  val make_draft: sg -> sg
paulson@1501
    65
  val merge: sg * sg -> sg
paulson@1501
    66
  val proto_pure: sg
paulson@1501
    67
  val pure: sg
paulson@1501
    68
  val cpure: sg
paulson@1501
    69
  val const_of_class: class -> string
paulson@1501
    70
  val class_of_const: string -> class
paulson@1501
    71
  end;
clasohm@0
    72
paulson@1501
    73
structure Sign : SIGN =
wenzelm@143
    74
struct
clasohm@0
    75
wenzelm@251
    76
(** datatype sg **)
wenzelm@251
    77
wenzelm@251
    78
(*the "ref" in stamps ensures that no two signatures are identical -- it is
wenzelm@251
    79
  impossible to forge a signature*)
wenzelm@143
    80
wenzelm@19
    81
datatype sg =
wenzelm@143
    82
  Sg of {
wenzelm@2185
    83
    tsig: Type.type_sig,            (*order-sorted signature of types*)
wenzelm@143
    84
    const_tab: typ Symtab.table,    (*types of constants*)
wenzelm@143
    85
    syn: Syntax.syntax,             (*syntax for parsing and printing*)
wenzelm@143
    86
    stamps: string ref list};       (*unique theory indentifier*)
clasohm@0
    87
clasohm@0
    88
fun rep_sg (Sg args) = args;
wenzelm@402
    89
val tsig_of = #tsig o rep_sg;
clasohm@0
    90
nipkow@206
    91
wenzelm@2185
    92
(* stamps *)
paulson@2180
    93
wenzelm@2185
    94
(*inclusion, equality*)
wenzelm@2185
    95
local
wenzelm@2185
    96
  (*avoiding polymorphic equality: factor 10 speedup*)
wenzelm@2185
    97
  fun mem_stamp (_:string ref, []) = false
wenzelm@2185
    98
    | mem_stamp (x, y :: ys) = x = y orelse mem_stamp (x, ys);
wenzelm@2185
    99
wenzelm@2185
   100
  fun subset_stamp ([], ys) = true
wenzelm@2185
   101
    | subset_stamp (x :: xs, ys) =
wenzelm@2185
   102
        mem_stamp (x, ys) andalso subset_stamp (xs, ys);
paulson@2180
   103
wenzelm@2185
   104
  (*fast partial test*)
wenzelm@2185
   105
  fun fast_sub ([]: string ref list, _) = true
wenzelm@2185
   106
    | fast_sub (_, []) = false
wenzelm@2185
   107
    | fast_sub (x :: xs, y :: ys) =
wenzelm@2185
   108
        if x = y then fast_sub (xs, ys)
wenzelm@2185
   109
        else fast_sub (x :: xs, ys);
wenzelm@2185
   110
in
wenzelm@2185
   111
  fun subsig (Sg {stamps = s1, ...}, Sg {stamps = s2, ...}) =
wenzelm@2185
   112
    s1 = s2 orelse subset_stamp (s1, s2);
wenzelm@2185
   113
wenzelm@2185
   114
  fun fast_subsig (Sg {stamps = s1, ...}, Sg {stamps = s2, ...}) =
wenzelm@2185
   115
    s1 = s2 orelse fast_sub (s1, s2);
paulson@2180
   116
wenzelm@2185
   117
  fun eq_sg (Sg {stamps = s1, ...}, Sg {stamps = s2, ...}) =
wenzelm@2185
   118
    s1 = s2 orelse (subset_stamp (s1, s2) andalso subset_stamp (s2, s1));
wenzelm@2185
   119
end;
wenzelm@2185
   120
wenzelm@402
   121
wenzelm@2185
   122
(*test if same theory names are contained in signatures' stamps,
wenzelm@2185
   123
  i.e. if signatures belong to same theory but not necessarily to the
wenzelm@2185
   124
  same version of it*)
wenzelm@2185
   125
fun same_sg (Sg {stamps = s1, ...}, Sg {stamps = s2, ...}) =
wenzelm@2185
   126
  eq_set_string (pairself (map (op !)) (s1, s2));
wenzelm@2185
   127
wenzelm@2185
   128
(*test for drafts*)
wenzelm@386
   129
fun is_draft (Sg {stamps = ref "#" :: _, ...}) = true
wenzelm@386
   130
  | is_draft _ = false;
wenzelm@386
   131
clasohm@0
   132
wenzelm@402
   133
(* consts *)
wenzelm@402
   134
wenzelm@386
   135
fun const_type (Sg {const_tab, ...}) c =
wenzelm@386
   136
  Symtab.lookup (const_tab, c);
wenzelm@386
   137
wenzelm@386
   138
wenzelm@620
   139
(* classes and sorts *)
wenzelm@402
   140
wenzelm@402
   141
val classes = #classes o Type.rep_tsig o tsig_of;
wenzelm@402
   142
wenzelm@402
   143
val subsort = Type.subsort o tsig_of;
wenzelm@402
   144
val norm_sort = Type.norm_sort o tsig_of;
wenzelm@1216
   145
val nonempty_sort = Type.nonempty_sort o tsig_of;
wenzelm@402
   146
wenzelm@620
   147
fun pretty_sort [c] = Pretty.str c
wenzelm@620
   148
  | pretty_sort cs = Pretty.str_list "{" "}" cs;
wenzelm@620
   149
wenzelm@402
   150
clasohm@0
   151
wenzelm@251
   152
(** print signature **)
wenzelm@251
   153
paulson@2228
   154
fun stamp_names stamps = rev (map ! stamps);
wenzelm@386
   155
wenzelm@251
   156
fun print_sg sg =
wenzelm@251
   157
  let
wenzelm@251
   158
    fun prt_typ syn ty = Pretty.quote (Syntax.pretty_typ syn ty);
wenzelm@251
   159
wenzelm@251
   160
    fun pretty_subclass (cl, cls) = Pretty.block
wenzelm@1216
   161
      (Pretty.str (cl ^ " <") :: Pretty.brk 1 ::
wenzelm@1216
   162
        Pretty.commas (map Pretty.str cls));
wenzelm@251
   163
wenzelm@251
   164
    fun pretty_default cls = Pretty.block
wenzelm@251
   165
      [Pretty.str "default:", Pretty.brk 1, pretty_sort cls];
wenzelm@251
   166
nipkow@963
   167
    fun pretty_ty (ty, n) = Pretty.str (ty ^ " " ^ string_of_int n);
wenzelm@251
   168
wenzelm@251
   169
    fun pretty_abbr syn (ty, (vs, rhs)) = Pretty.block
wenzelm@620
   170
      [prt_typ syn (Type (ty, map (fn v => TVar ((v, 0), [])) vs)),
wenzelm@251
   171
        Pretty.str " =", Pretty.brk 1, prt_typ syn rhs];
wenzelm@251
   172
wenzelm@251
   173
    fun pretty_arity ty (cl, []) = Pretty.block
wenzelm@251
   174
          [Pretty.str (ty ^ " ::"), Pretty.brk 1, Pretty.str cl]
wenzelm@251
   175
      | pretty_arity ty (cl, srts) = Pretty.block
wenzelm@251
   176
          [Pretty.str (ty ^ " ::"), Pretty.brk 1,
wenzelm@251
   177
            Pretty.list "(" ")" (map pretty_sort srts),
wenzelm@251
   178
            Pretty.brk 1, Pretty.str cl];
wenzelm@251
   179
nipkow@963
   180
    fun pretty_arities (ty, ars) = map (pretty_arity ty) ars;
wenzelm@251
   181
wenzelm@251
   182
    fun pretty_const syn (c, ty) = Pretty.block
wenzelm@266
   183
      [Pretty.str (quote c ^ " ::"), Pretty.brk 1, prt_typ syn ty];
wenzelm@251
   184
wenzelm@251
   185
wenzelm@251
   186
    val Sg {tsig, const_tab, syn, stamps} = sg;
wenzelm@2185
   187
    val {classes, subclass, default, tycons, abbrs, arities} =
wenzelm@2185
   188
      Type.rep_tsig tsig;
wenzelm@251
   189
  in
wenzelm@386
   190
    Pretty.writeln (Pretty.strs ("stamps:" :: stamp_names stamps));
wenzelm@251
   191
    Pretty.writeln (Pretty.strs ("classes:" :: classes));
nipkow@963
   192
    Pretty.writeln (Pretty.big_list "subclass:"
nipkow@963
   193
                      (map pretty_subclass subclass));
wenzelm@251
   194
    Pretty.writeln (pretty_default default);
nipkow@963
   195
    Pretty.writeln (Pretty.big_list "types:" (map pretty_ty tycons));
wenzelm@251
   196
    Pretty.writeln (Pretty.big_list "abbrs:" (map (pretty_abbr syn) abbrs));
nipkow@963
   197
    Pretty.writeln (Pretty.big_list "arities:"
nipkow@963
   198
                      (flat (map pretty_arities arities)));
wenzelm@251
   199
    Pretty.writeln (Pretty.big_list "consts:"
nipkow@963
   200
                      (map (pretty_const syn) (Symtab.dest const_tab)))
wenzelm@251
   201
  end;
wenzelm@251
   202
wenzelm@251
   203
wenzelm@562
   204
fun pretty_sg (Sg {stamps, ...}) =
wenzelm@562
   205
  Pretty.str_list "{" "}" (stamp_names stamps);
wenzelm@562
   206
wenzelm@562
   207
val pprint_sg = Pretty.pprint o pretty_sg;
wenzelm@251
   208
wenzelm@251
   209
wenzelm@251
   210
wenzelm@251
   211
(** pretty printing of terms and types **)
wenzelm@251
   212
clasohm@922
   213
fun pretty_term (Sg {syn, stamps, ...}) =
paulson@2138
   214
  let val curried = "CPure" mem_string (map ! stamps);
clasohm@922
   215
  in Syntax.pretty_term curried syn end;
clasohm@922
   216
wenzelm@251
   217
fun pretty_typ (Sg {syn, ...}) = Syntax.pretty_typ syn;
wenzelm@251
   218
wenzelm@1239
   219
fun string_of_term (Sg {syn, stamps, ...}) =
paulson@2138
   220
  let val curried = "CPure" mem_string (map ! stamps);
clasohm@922
   221
  in Syntax.string_of_term curried syn end;
clasohm@922
   222
wenzelm@251
   223
fun string_of_typ (Sg {syn, ...}) = Syntax.string_of_typ syn;
wenzelm@251
   224
wenzelm@251
   225
fun pprint_term sg = Pretty.pprint o Pretty.quote o (pretty_term sg);
wenzelm@251
   226
fun pprint_typ sg = Pretty.pprint o Pretty.quote o (pretty_typ sg);
clasohm@0
   227
clasohm@0
   228
wenzelm@169
   229
wenzelm@562
   230
(** read types **)  (*exception ERROR*)
wenzelm@562
   231
wenzelm@562
   232
fun err_in_type s =
wenzelm@562
   233
  error ("The error(s) above occurred in type " ^ quote s);
wenzelm@562
   234
wenzelm@562
   235
fun read_raw_typ syn tsig sort_of str =
wenzelm@2185
   236
  Syntax.read_typ syn (fn x => if_none (sort_of x) (Type.defaultS tsig)) str
wenzelm@562
   237
    handle ERROR => err_in_type str;
wenzelm@562
   238
wenzelm@562
   239
(*read and certify typ wrt a signature*)
wenzelm@562
   240
fun read_typ (Sg {tsig, syn, ...}, sort_of) str =
wenzelm@2185
   241
  Type.cert_typ tsig (read_raw_typ syn tsig sort_of str)
wenzelm@562
   242
    handle TYPE (msg, _, _) => (writeln msg; err_in_type str);
wenzelm@562
   243
wenzelm@562
   244
wenzelm@562
   245
wenzelm@386
   246
(** certify types and terms **)   (*exception TYPE*)
wenzelm@251
   247
wenzelm@2185
   248
fun certify_typ (Sg {tsig, ...}) ty = Type.cert_typ tsig ty;
wenzelm@251
   249
nipkow@1494
   250
(* check for duplicate TVars with distinct sorts *)
nipkow@1494
   251
fun nodup_TVars(tvars,T) = (case T of
paulson@2144
   252
      Type(_,Ts) => nodup_TVars_list (tvars,Ts)
nipkow@1494
   253
    | TFree _ => tvars
nipkow@1494
   254
    | TVar(v as (a,S)) =>
berghofe@1576
   255
        (case assoc_string_int(tvars,a) of
nipkow@1494
   256
           Some(S') => if S=S' then tvars
nipkow@1494
   257
                       else raise_type
nipkow@1494
   258
                            ("Type variable "^Syntax.string_of_vname a^
paulson@1890
   259
                             " has two distinct sorts") [TVar(a,S'),T] []
paulson@2144
   260
         | None => v::tvars))
paulson@2144
   261
and (*equivalent to foldl nodup_TVars_list, but 3X faster under Poly/ML*)
paulson@2144
   262
    nodup_TVars_list (tvars,[]) = tvars
paulson@2144
   263
  | nodup_TVars_list (tvars,T::Ts) = nodup_TVars_list(nodup_TVars(tvars,T), 
paulson@2144
   264
						      Ts);
nipkow@1494
   265
nipkow@1494
   266
(* check for duplicate Vars with distinct types *)
nipkow@1494
   267
fun nodup_Vars tm =
nipkow@1494
   268
let fun nodups vars tvars tm = (case tm of
nipkow@1494
   269
          Const(c,T) => (vars, nodup_TVars(tvars,T))
nipkow@1494
   270
        | Free(a,T) => (vars, nodup_TVars(tvars,T))
nipkow@1494
   271
        | Var(v as (ixn,T)) =>
berghofe@1576
   272
            (case assoc_string_int(vars,ixn) of
nipkow@1494
   273
               Some(T') => if T=T' then (vars,nodup_TVars(tvars,T))
nipkow@1494
   274
                           else raise_type
nipkow@1494
   275
                             ("Variable "^Syntax.string_of_vname ixn^
paulson@1890
   276
                              " has two distinct types") [T',T] []
nipkow@1494
   277
             | None => (v::vars,tvars))
nipkow@1494
   278
        | Bound _ => (vars,tvars)
nipkow@1494
   279
        | Abs(_,T,t) => nodups vars (nodup_TVars(tvars,T)) t
nipkow@1494
   280
        | s$t => let val (vars',tvars') = nodups vars tvars s
nipkow@1494
   281
                 in nodups vars' tvars' t end);
nipkow@1494
   282
in nodups [] [] tm; () end;
wenzelm@251
   283
wenzelm@386
   284
fun mapfilt_atoms f (Abs (_, _, t)) = mapfilt_atoms f t
wenzelm@386
   285
  | mapfilt_atoms f (t $ u) = mapfilt_atoms f t @ mapfilt_atoms f u
wenzelm@386
   286
  | mapfilt_atoms f a = (case f a of Some y => [y] | None => []);
clasohm@0
   287
wenzelm@386
   288
fun certify_term (sg as Sg {tsig, ...}) tm =
wenzelm@251
   289
  let
wenzelm@251
   290
    fun valid_const a T =
wenzelm@386
   291
      (case const_type sg a of
wenzelm@2185
   292
        Some U => Type.typ_instance (tsig, T, U)
wenzelm@251
   293
      | _ => false);
wenzelm@169
   294
wenzelm@251
   295
    fun atom_err (Const (a, T)) =
wenzelm@251
   296
          if valid_const a T then None
wenzelm@251
   297
          else Some ("Illegal type for constant " ^ quote a ^ " :: " ^
wenzelm@251
   298
            quote (string_of_typ sg T))
wenzelm@251
   299
      | atom_err (Var ((x, i), _)) =
wenzelm@251
   300
          if i < 0 then Some ("Negative index for Var " ^ quote x) else None
wenzelm@251
   301
      | atom_err _ = None;
wenzelm@251
   302
wenzelm@251
   303
    val norm_tm =
wenzelm@2185
   304
      (case it_term_types (Type.typ_errors tsig) (tm, []) of
wenzelm@2185
   305
        [] => map_term_types (Type.norm_typ tsig) tm
wenzelm@251
   306
      | errs => raise_type (cat_lines errs) [] [tm]);
nipkow@1494
   307
    val _ = nodup_Vars norm_tm;
wenzelm@251
   308
  in
wenzelm@386
   309
    (case mapfilt_atoms atom_err norm_tm of
wenzelm@251
   310
      [] => (norm_tm, type_of norm_tm, maxidx_of_term norm_tm)
wenzelm@251
   311
    | errs => raise_type (cat_lines errs) [] [norm_tm])
wenzelm@251
   312
  end;
wenzelm@251
   313
wenzelm@251
   314
paulson@1393
   315
(*Package error messages from type checking*)
paulson@1393
   316
fun exn_type_msg sg (msg, Ts, ts) =
paulson@1393
   317
    let val show_typ = string_of_typ sg
clasohm@1460
   318
	val show_term = string_of_term sg
clasohm@1460
   319
	fun term_err [] = ""
clasohm@1460
   320
	  | term_err [t] = "\nInvolving this term:\n" ^ show_term t
clasohm@1460
   321
	  | term_err ts =
clasohm@1460
   322
	    "\nInvolving these terms:\n" ^ cat_lines (map show_term ts);
paulson@1393
   323
    in  "\nType checking error: " ^ msg ^ "\n" ^
clasohm@1460
   324
	cat_lines (map show_typ Ts) ^ term_err ts ^ "\n"
paulson@1393
   325
    end; 
paulson@1393
   326
paulson@1393
   327
wenzelm@251
   328
wenzelm@583
   329
(** infer_types **)         (*exception ERROR*)
wenzelm@251
   330
paulson@1393
   331
(*ts is the list of alternative parses; only one is hoped to be type-correct.
paulson@1393
   332
  T is the expected type for the correct term.
paulson@1393
   333
  Other standard arguments:
paulson@1393
   334
    types is a partial map from indexnames to types (constrains Free, Var).
paulson@1393
   335
    sorts is a partial map from indexnames to sorts (constrains TFree, TVar).
paulson@1393
   336
    used is the list of already used type variables.
paulson@1393
   337
    If freeze then internal TVars are turned into TFrees, else TVars.*)
clasohm@959
   338
fun infer_types sg types sorts used freeze (ts, T) =
wenzelm@251
   339
  let
wenzelm@562
   340
    val Sg {tsig, ...} = sg;
wenzelm@169
   341
paulson@1393
   342
    val T' = certify_typ sg T handle TYPE arg => error (exn_type_msg sg arg);
clasohm@623
   343
clasohm@623
   344
    val ct = const_type sg;
clasohm@623
   345
nipkow@952
   346
    fun warn() =
nipkow@952
   347
      if length ts > 1 andalso length ts <= !Syntax.ambiguity_level
nipkow@952
   348
      then (*no warning shown yet*)
berghofe@1580
   349
           warning "Currently parsed input \
nipkow@952
   350
                   \produces more than one parse tree.\n\
nipkow@952
   351
                   \For more information lower Syntax.ambiguity_level."
nipkow@952
   352
      else ();
clasohm@623
   353
nipkow@952
   354
    datatype result = One of int * term * (indexname * typ) list
nipkow@952
   355
                    | Errs of (string * typ list * term list)list
nipkow@952
   356
                    | Ambigs of term list;
clasohm@623
   357
nipkow@952
   358
    fun process_term(res,(t,i)) =
paulson@1393
   359
       let val ([u],tye) = 
clasohm@1460
   360
	       Type.infer_types(tsig,ct,types,sorts,used,freeze,[T'],[t])
nipkow@952
   361
       in case res of
nipkow@952
   362
            One(_,t0,_) => Ambigs([u,t0])
nipkow@952
   363
          | Errs _ => One(i,u,tye)
nipkow@952
   364
          | Ambigs(us) => Ambigs(u::us)
nipkow@952
   365
       end
nipkow@952
   366
       handle TYPE arg => (case res of Errs(errs) => Errs(arg::errs)
nipkow@952
   367
                                     | _ => res);
clasohm@623
   368
nipkow@952
   369
  in case foldl process_term (Errs[], ts ~~ (0 upto (length ts - 1))) of
nipkow@952
   370
       One(res) =>
nipkow@952
   371
         (if length ts > !Syntax.ambiguity_level
nipkow@952
   372
          then writeln "Fortunately, only one parse tree is type correct.\n\
nipkow@952
   373
            \It helps (speed!) if you disambiguate your grammar or your input."
nipkow@952
   374
          else ();
nipkow@952
   375
          res)
paulson@1393
   376
     | Errs(errs) => (warn(); error(cat_lines(map (exn_type_msg sg) errs)))
nipkow@952
   377
     | Ambigs(us) =>
nipkow@952
   378
         (warn();
nipkow@952
   379
          let val old_show_brackets = !show_brackets
clasohm@1178
   380
              val dummy = show_brackets := true;
paulson@1393
   381
              val errs = cat_lines(map (string_of_term sg) us)
nipkow@952
   382
          in show_brackets := old_show_brackets;
nipkow@952
   383
             error("Error: More than one term is type correct:\n" ^ errs)
nipkow@952
   384
          end)
clasohm@623
   385
  end;
wenzelm@251
   386
wenzelm@251
   387
clasohm@623
   388
(** extend signature **)    (*exception ERROR*)
clasohm@623
   389
wenzelm@620
   390
(** signature extension functions **)  (*exception ERROR*)
wenzelm@386
   391
wenzelm@386
   392
fun decls_of name_of mfixs =
wenzelm@386
   393
  map (fn (x, y, mx) => (name_of x mx, y)) mfixs;
wenzelm@386
   394
wenzelm@386
   395
wenzelm@386
   396
(* add default sort *)
wenzelm@386
   397
wenzelm@386
   398
fun ext_defsort (syn, tsig, ctab) defsort =
wenzelm@2185
   399
  (syn, Type.ext_tsig_defsort tsig defsort, ctab);
wenzelm@386
   400
wenzelm@386
   401
wenzelm@386
   402
(* add type constructors *)
wenzelm@386
   403
wenzelm@386
   404
fun ext_types (syn, tsig, ctab) types =
wenzelm@386
   405
  (Syntax.extend_type_gram syn types,
wenzelm@2185
   406
    Type.ext_tsig_types tsig (decls_of Syntax.type_name types),
wenzelm@386
   407
    ctab);
wenzelm@386
   408
wenzelm@386
   409
wenzelm@386
   410
(* add type abbreviations *)
wenzelm@386
   411
wenzelm@386
   412
fun read_abbr syn tsig (t, vs, rhs_src) =
wenzelm@386
   413
  (t, vs, read_raw_typ syn tsig (K None) rhs_src)
wenzelm@386
   414
    handle ERROR => error ("in type abbreviation " ^ t);
wenzelm@386
   415
wenzelm@386
   416
fun ext_abbrs rd_abbr (syn, tsig, ctab) abbrs =
wenzelm@386
   417
  let
wenzelm@386
   418
    fun mfix_of (t, vs, _, mx) = (t, length vs, mx);
wenzelm@386
   419
    val syn1 = Syntax.extend_type_gram syn (map mfix_of abbrs);
wenzelm@386
   420
wenzelm@386
   421
    fun decl_of (t, vs, rhs, mx) =
wenzelm@562
   422
      rd_abbr syn1 tsig (Syntax.type_name t mx, vs, rhs);
wenzelm@386
   423
  in
wenzelm@2185
   424
    (syn1, Type.ext_tsig_abbrs tsig (map decl_of abbrs), ctab)
wenzelm@386
   425
  end;
wenzelm@386
   426
paulson@2228
   427
fun ext_tyabbrs_i arg = ext_abbrs (K (K I)) arg;
paulson@2228
   428
fun ext_tyabbrs   arg = ext_abbrs read_abbr arg;
wenzelm@386
   429
wenzelm@386
   430
wenzelm@386
   431
(* add type arities *)
wenzelm@386
   432
wenzelm@386
   433
fun ext_arities (syn, tsig, ctab) arities =
wenzelm@386
   434
  let
wenzelm@2185
   435
    val tsig1 = Type.ext_tsig_arities tsig arities;
wenzelm@2185
   436
    val log_types = Type.logical_types tsig1;
wenzelm@386
   437
  in
wenzelm@386
   438
    (Syntax.extend_log_types syn log_types, tsig1, ctab)
wenzelm@386
   439
  end;
wenzelm@386
   440
wenzelm@386
   441
wenzelm@386
   442
(* add term constants and syntax *)
wenzelm@386
   443
wenzelm@386
   444
fun err_in_const c =
wenzelm@386
   445
  error ("in declaration of constant " ^ quote c);
wenzelm@386
   446
wenzelm@386
   447
fun err_dup_consts cs =
wenzelm@386
   448
  error ("Duplicate declaration of constant(s) " ^ commas_quote cs);
wenzelm@386
   449
wenzelm@251
   450
wenzelm@386
   451
fun read_const syn tsig (c, ty_src, mx) =
wenzelm@386
   452
  (c, read_raw_typ syn tsig (K None) ty_src, mx)
wenzelm@562
   453
    handle ERROR => err_in_const (Syntax.const_name c mx);
wenzelm@386
   454
wenzelm@2197
   455
fun ext_cnsts rd_const syn_only prmode (syn, tsig, ctab) raw_consts =
wenzelm@386
   456
  let
paulson@1414
   457
    fun prep_const (c, ty, mx) = 
wenzelm@2185
   458
     (c, compress_type (Type.varifyT (Type.cert_typ tsig (Type.no_tvars ty))), mx)
wenzelm@2185
   459
       handle TYPE (msg, _, _)
wenzelm@2185
   460
         => (writeln msg; err_in_const (Syntax.const_name c mx));
wenzelm@386
   461
wenzelm@386
   462
    val consts = map (prep_const o rd_const syn tsig) raw_consts;
wenzelm@386
   463
    val decls =
wenzelm@386
   464
      if syn_only then []
wenzelm@562
   465
      else filter_out (equal "" o fst) (decls_of Syntax.const_name consts);
wenzelm@386
   466
  in
wenzelm@2197
   467
    (Syntax.extend_const_gram syn prmode consts, tsig,
wenzelm@386
   468
      Symtab.extend_new (ctab, decls)
wenzelm@386
   469
        handle Symtab.DUPS cs => err_dup_consts cs)
wenzelm@386
   470
  end;
wenzelm@386
   471
wenzelm@2197
   472
val ext_consts_i = ext_cnsts (K (K I)) false "";
wenzelm@2197
   473
val ext_consts = ext_cnsts read_const false "";
wenzelm@2197
   474
val ext_syntax_i = ext_cnsts (K (K I)) true "";
wenzelm@2197
   475
val ext_syntax = ext_cnsts read_const true "";
wenzelm@2197
   476
fun ext_modesyntax_i sg (prmode, consts) = ext_cnsts (K (K I)) true prmode sg consts;
wenzelm@2197
   477
fun ext_modesyntax sg (prmode, consts) = ext_cnsts read_const true prmode sg consts;
wenzelm@386
   478
wenzelm@386
   479
wenzelm@386
   480
(* add type classes *)
wenzelm@386
   481
wenzelm@386
   482
fun const_of_class c = c ^ "_class";
wenzelm@386
   483
wenzelm@386
   484
fun class_of_const c_class =
wenzelm@386
   485
  let
wenzelm@386
   486
    val c = implode (take (size c_class - 6, explode c_class));
wenzelm@386
   487
  in
wenzelm@386
   488
    if const_of_class c = c_class then c
wenzelm@386
   489
    else raise_term ("class_of_const: bad name " ^ quote c_class) []
wenzelm@386
   490
  end;
wenzelm@386
   491
wenzelm@386
   492
wenzelm@386
   493
fun ext_classes (syn, tsig, ctab) classes =
wenzelm@386
   494
  let
wenzelm@562
   495
    val names = map fst classes;
wenzelm@386
   496
    val consts =
wenzelm@386
   497
      map (fn c => (const_of_class c, a_itselfT --> propT, NoSyn)) names;
wenzelm@386
   498
  in
wenzelm@386
   499
    ext_consts_i
wenzelm@2185
   500
      (Syntax.extend_consts syn names, Type.ext_tsig_classes tsig classes, ctab)
wenzelm@386
   501
      consts
wenzelm@386
   502
  end;
wenzelm@386
   503
wenzelm@386
   504
wenzelm@421
   505
(* add to subclass relation *)
wenzelm@421
   506
wenzelm@421
   507
fun ext_classrel (syn, tsig, ctab) pairs =
wenzelm@2185
   508
  (syn, Type.ext_tsig_subclass tsig pairs, ctab);
wenzelm@421
   509
wenzelm@421
   510
wenzelm@1159
   511
(* add to syntax *)
wenzelm@386
   512
wenzelm@1159
   513
fun ext_syn extfun (syn, tsig, ctab) args =
wenzelm@1159
   514
  (extfun syn args, tsig, ctab);
wenzelm@386
   515
wenzelm@386
   516
wenzelm@386
   517
(* build signature *)
wenzelm@386
   518
wenzelm@386
   519
fun ext_stamps stamps name =
wenzelm@386
   520
  let
wenzelm@386
   521
    val stmps = (case stamps of ref "#" :: ss => ss | ss => ss);
wenzelm@386
   522
  in
wenzelm@386
   523
    if exists (equal name o !) stmps then
wenzelm@386
   524
      error ("Theory already contains a " ^ quote name ^ " component")
wenzelm@402
   525
    else ref name :: stmps
wenzelm@386
   526
  end;
wenzelm@386
   527
wenzelm@386
   528
fun make_sign (syn, tsig, ctab) stamps name =
wenzelm@386
   529
  Sg {tsig = tsig, const_tab = ctab, syn = syn,
wenzelm@386
   530
    stamps = ext_stamps stamps name};
wenzelm@386
   531
wenzelm@386
   532
fun extend_sign extfun name decls (Sg {tsig, const_tab, syn, stamps}) =
wenzelm@386
   533
  make_sign (extfun (syn, tsig, const_tab) decls) stamps name;
wenzelm@386
   534
wenzelm@386
   535
wenzelm@386
   536
(* the external interfaces *)
wenzelm@386
   537
wenzelm@2197
   538
val add_classes      = extend_sign ext_classes "#";
wenzelm@2197
   539
val add_classrel     = extend_sign ext_classrel "#";
wenzelm@2197
   540
val add_defsort      = extend_sign ext_defsort "#";
wenzelm@2197
   541
val add_types        = extend_sign ext_types "#";
wenzelm@2197
   542
val add_tyabbrs      = extend_sign ext_tyabbrs "#";
wenzelm@2197
   543
val add_tyabbrs_i    = extend_sign ext_tyabbrs_i "#";
wenzelm@2197
   544
val add_arities      = extend_sign ext_arities "#";
wenzelm@2197
   545
val add_consts       = extend_sign ext_consts "#";
wenzelm@2197
   546
val add_consts_i     = extend_sign ext_consts_i "#";
wenzelm@2197
   547
val add_syntax       = extend_sign ext_syntax "#";
wenzelm@2197
   548
val add_syntax_i     = extend_sign ext_syntax_i "#";
wenzelm@2197
   549
val add_modesyntax   = extend_sign ext_modesyntax "#";
wenzelm@2197
   550
val add_modesyntax_i = extend_sign ext_modesyntax_i "#";
wenzelm@2197
   551
val add_trfuns       = extend_sign (ext_syn Syntax.extend_trfuns) "#";
wenzelm@2197
   552
val add_trrules      = extend_sign (ext_syn Syntax.extend_trrules) "#";
wenzelm@2197
   553
val add_trrules_i    = extend_sign (ext_syn Syntax.extend_trrules_i) "#";
wenzelm@386
   554
wenzelm@386
   555
fun add_name name sg = extend_sign K name () sg;
wenzelm@386
   556
val make_draft = add_name "#";
wenzelm@386
   557
wenzelm@386
   558
wenzelm@386
   559
wenzelm@620
   560
(** merge signatures **)    (*exception TERM*) (*exception ERROR (* FIXME *)*)
wenzelm@143
   561
wenzelm@251
   562
fun merge (sg1, sg2) =
wenzelm@2185
   563
  if fast_subsig (sg2, sg1) then sg1
wenzelm@2185
   564
  else if fast_subsig (sg1, sg2) then sg2
wenzelm@2185
   565
  else if subsig (sg2, sg1) then sg1
wenzelm@251
   566
  else if subsig (sg1, sg2) then sg2
wenzelm@386
   567
  else if is_draft sg1 orelse is_draft sg2 then
wenzelm@386
   568
    raise_term "Illegal merge of draft signatures" []
wenzelm@251
   569
  else
wenzelm@251
   570
    (*neither is union already; must form union*)
wenzelm@251
   571
    let
wenzelm@251
   572
      val Sg {tsig = tsig1, const_tab = const_tab1, syn = syn1,
wenzelm@251
   573
        stamps = stamps1} = sg1;
wenzelm@251
   574
      val Sg {tsig = tsig2, const_tab = const_tab2, syn = syn2,
wenzelm@251
   575
        stamps = stamps2} = sg2;
wenzelm@251
   576
wenzelm@386
   577
wenzelm@402
   578
      val stamps = merge_rev_lists stamps1 stamps2;
wenzelm@402
   579
      val _ =
wenzelm@402
   580
        (case duplicates (stamp_names stamps) of
wenzelm@402
   581
          [] => ()
wenzelm@402
   582
        | dups => raise_term ("Attempt to merge different versions of theories "
wenzelm@402
   583
            ^ commas_quote dups) []);
wenzelm@402
   584
wenzelm@2185
   585
      val tsig = Type.merge_tsigs (tsig1, tsig2);
wenzelm@251
   586
wenzelm@251
   587
      val const_tab = Symtab.merge (op =) (const_tab1, const_tab2)
wenzelm@386
   588
        handle Symtab.DUPS cs =>
wenzelm@386
   589
          raise_term ("Incompatible types for constant(s) " ^ commas_quote cs) [];
wenzelm@386
   590
wenzelm@386
   591
      val syn = Syntax.merge_syntaxes syn1 syn2;
wenzelm@251
   592
    in
wenzelm@251
   593
      Sg {tsig = tsig, const_tab = const_tab, syn = syn, stamps = stamps}
wenzelm@251
   594
    end;
wenzelm@143
   595
clasohm@0
   596
clasohm@0
   597
wenzelm@251
   598
(** the Pure signature **)
clasohm@0
   599
clasohm@922
   600
val proto_pure =
wenzelm@2185
   601
  make_sign (Syntax.pure_syn, Type.tsig0, Symtab.null) [] "#"
wenzelm@410
   602
  |> add_types
wenzelm@386
   603
   (("fun", 2, NoSyn) ::
wenzelm@386
   604
    ("prop", 0, NoSyn) ::
wenzelm@386
   605
    ("itself", 1, NoSyn) ::
wenzelm@562
   606
    Syntax.pure_types)
wenzelm@562
   607
  |> add_classes [(logicC, [])]
wenzelm@410
   608
  |> add_defsort logicS
wenzelm@410
   609
  |> add_arities
wenzelm@386
   610
   [("fun", [logicS, logicS], logicS),
wenzelm@386
   611
    ("prop", [], logicS),
wenzelm@386
   612
    ("itself", [logicS], logicS)]
wenzelm@562
   613
  |> add_syntax Syntax.pure_syntax
wenzelm@2252
   614
  |> add_modesyntax ("symbols", Syntax.pure_sym_syntax)
wenzelm@410
   615
  |> add_trfuns Syntax.pure_trfuns
wenzelm@410
   616
  |> add_consts
wenzelm@2197
   617
   [("==", "['a::{}, 'a] => prop", InfixrName ("==", 2)),
wenzelm@2197
   618
    ("=?=", "['a::{}, 'a] => prop", InfixrName ("=?=", 2)),
wenzelm@2211
   619
    ("==>", "[prop, prop] => prop", Mixfix ("(_/ ==> _)", [2, 1], 1)),
clasohm@865
   620
    ("all", "('a => prop) => prop", Binder ("!!", 0, 0)),
wenzelm@573
   621
    ("TYPE", "'a itself", NoSyn)]
wenzelm@2211
   622
  |> add_syntax
wenzelm@2211
   623
   [("==>", "[prop, prop] => prop", Delimfix "op ==>")]
clasohm@922
   624
  |> add_name "ProtoPure";
clasohm@922
   625
clasohm@922
   626
val pure = proto_pure
clasohm@1180
   627
  |> add_syntax Syntax.pure_appl_syntax
wenzelm@410
   628
  |> add_name "Pure";
clasohm@0
   629
clasohm@922
   630
val cpure = proto_pure
clasohm@1180
   631
  |> add_syntax Syntax.pure_applC_syntax
clasohm@922
   632
  |> add_name "CPure";
clasohm@0
   633
clasohm@0
   634
end;