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