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