src/Pure/sign.ML
author wenzelm
Tue Jul 22 19:33:30 1997 +0200 (1997-07-22)
changeset 3552 f348e8a2db4b
parent 2979 db6941221197
child 3791 c5db2c87a646
permissions -rw-r--r--
tuned error / warning;
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 infer_types: sg -> (indexname -> typ option) ->
paulson@1501
    40
    (indexname -> sort option) -> string list -> bool
paulson@1501
    41
    -> term list * typ -> int * term * (indexname * typ) list
paulson@1501
    42
  val add_classes: (class * class list) list -> sg -> sg
paulson@1501
    43
  val add_classrel: (class * class) list -> sg -> sg
paulson@1501
    44
  val add_defsort: sort -> sg -> sg
paulson@1501
    45
  val add_types: (string * int * mixfix) list -> sg -> sg
paulson@1501
    46
  val add_tyabbrs: (string * string list * string * mixfix) list -> sg -> sg
paulson@1501
    47
  val add_tyabbrs_i: (string * string list * typ * mixfix) list -> sg -> sg
paulson@1501
    48
  val add_arities: (string * sort list * sort) list -> sg -> sg
paulson@1501
    49
  val add_consts: (string * string * mixfix) list -> sg -> sg
paulson@1501
    50
  val add_consts_i: (string * typ * mixfix) list -> sg -> sg
paulson@1501
    51
  val add_syntax: (string * string * mixfix) list -> sg -> sg
paulson@1501
    52
  val add_syntax_i: (string * typ * mixfix) list -> sg -> sg
wenzelm@2359
    53
  val add_modesyntax: (string * bool) * (string * string * mixfix) list -> sg -> sg
wenzelm@2359
    54
  val add_modesyntax_i: (string * bool) * (string * typ * mixfix) list -> sg -> sg
paulson@1501
    55
  val add_trfuns:
paulson@1501
    56
    (string * (ast list -> ast)) list *
paulson@1501
    57
    (string * (term list -> term)) list *
paulson@1501
    58
    (string * (term list -> term)) list *
paulson@1501
    59
    (string * (ast list -> ast)) list -> sg -> sg
wenzelm@2385
    60
  val add_trfunsT:
wenzelm@2385
    61
    (string * (typ -> term list -> term)) list -> sg -> sg
wenzelm@2693
    62
  val add_tokentrfuns:
wenzelm@2693
    63
    (string * string * (string -> string * int)) list -> sg -> sg
paulson@1810
    64
  val add_trrules: (string * string) Syntax.trrule list -> sg -> sg
paulson@1810
    65
  val add_trrules_i: ast Syntax.trrule list -> sg -> sg
paulson@1501
    66
  val add_name: string -> sg -> sg
paulson@1501
    67
  val make_draft: sg -> sg
paulson@1501
    68
  val merge: sg * sg -> sg
paulson@1501
    69
  val proto_pure: sg
paulson@1501
    70
  val pure: sg
paulson@1501
    71
  val cpure: sg
paulson@1501
    72
  val const_of_class: class -> string
paulson@1501
    73
  val class_of_const: string -> class
paulson@1501
    74
  end;
clasohm@0
    75
paulson@1501
    76
structure Sign : SIGN =
wenzelm@143
    77
struct
clasohm@0
    78
wenzelm@2979
    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@2979
    96
(* inclusion and equality *)
paulson@2180
    97
wenzelm@2185
    98
local
wenzelm@2185
    99
  (*avoiding polymorphic equality: factor 10 speedup*)
wenzelm@2185
   100
  fun mem_stamp (_:string ref, []) = false
wenzelm@2185
   101
    | mem_stamp (x, y :: ys) = x = y orelse mem_stamp (x, ys);
wenzelm@2185
   102
wenzelm@2185
   103
  fun subset_stamp ([], ys) = true
wenzelm@2185
   104
    | subset_stamp (x :: xs, ys) =
wenzelm@2185
   105
        mem_stamp (x, ys) andalso subset_stamp (xs, ys);
paulson@2180
   106
wenzelm@2185
   107
  (*fast partial test*)
wenzelm@2185
   108
  fun fast_sub ([]: string ref list, _) = true
wenzelm@2185
   109
    | fast_sub (_, []) = false
wenzelm@2185
   110
    | fast_sub (x :: xs, y :: ys) =
wenzelm@2185
   111
        if x = y then fast_sub (xs, ys)
wenzelm@2185
   112
        else fast_sub (x :: xs, ys);
wenzelm@2185
   113
in
wenzelm@2185
   114
  fun subsig (Sg {stamps = s1, ...}, Sg {stamps = s2, ...}) =
wenzelm@2185
   115
    s1 = s2 orelse subset_stamp (s1, s2);
wenzelm@2185
   116
wenzelm@2185
   117
  fun fast_subsig (Sg {stamps = s1, ...}, Sg {stamps = s2, ...}) =
wenzelm@2185
   118
    s1 = s2 orelse fast_sub (s1, s2);
paulson@2180
   119
wenzelm@2185
   120
  fun eq_sg (Sg {stamps = s1, ...}, Sg {stamps = s2, ...}) =
wenzelm@2185
   121
    s1 = s2 orelse (subset_stamp (s1, s2) andalso subset_stamp (s2, s1));
wenzelm@2185
   122
end;
wenzelm@2185
   123
wenzelm@402
   124
wenzelm@2185
   125
(*test if same theory names are contained in signatures' stamps,
wenzelm@2185
   126
  i.e. if signatures belong to same theory but not necessarily to the
wenzelm@2185
   127
  same version of it*)
wenzelm@2185
   128
fun same_sg (Sg {stamps = s1, ...}, Sg {stamps = s2, ...}) =
wenzelm@2185
   129
  eq_set_string (pairself (map (op !)) (s1, s2));
wenzelm@2185
   130
wenzelm@2185
   131
(*test for drafts*)
wenzelm@386
   132
fun is_draft (Sg {stamps = ref "#" :: _, ...}) = true
wenzelm@386
   133
  | is_draft _ = false;
wenzelm@386
   134
clasohm@0
   135
wenzelm@402
   136
(* consts *)
wenzelm@402
   137
wenzelm@2979
   138
fun const_type (Sg {const_tab, ...}) c = 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@2979
   149
(* FIXME move to Sorts? *)
wenzelm@620
   150
fun pretty_sort [c] = Pretty.str c
wenzelm@620
   151
  | pretty_sort cs = Pretty.str_list "{" "}" cs;
wenzelm@620
   152
wenzelm@402
   153
clasohm@0
   154
wenzelm@251
   155
(** print signature **)
wenzelm@251
   156
paulson@2228
   157
fun stamp_names stamps = rev (map ! stamps);
wenzelm@386
   158
wenzelm@251
   159
fun print_sg sg =
wenzelm@251
   160
  let
wenzelm@251
   161
    fun prt_typ syn ty = Pretty.quote (Syntax.pretty_typ syn ty);
wenzelm@251
   162
wenzelm@2963
   163
    fun pretty_classrel (cl, cls) = Pretty.block
wenzelm@1216
   164
      (Pretty.str (cl ^ " <") :: Pretty.brk 1 ::
wenzelm@1216
   165
        Pretty.commas (map Pretty.str cls));
wenzelm@251
   166
wenzelm@251
   167
    fun pretty_default cls = Pretty.block
wenzelm@251
   168
      [Pretty.str "default:", Pretty.brk 1, pretty_sort cls];
wenzelm@251
   169
nipkow@963
   170
    fun pretty_ty (ty, n) = Pretty.str (ty ^ " " ^ string_of_int n);
wenzelm@251
   171
wenzelm@251
   172
    fun pretty_abbr syn (ty, (vs, rhs)) = Pretty.block
wenzelm@620
   173
      [prt_typ syn (Type (ty, map (fn v => TVar ((v, 0), [])) vs)),
wenzelm@251
   174
        Pretty.str " =", Pretty.brk 1, prt_typ syn rhs];
wenzelm@251
   175
wenzelm@251
   176
    fun pretty_arity ty (cl, []) = Pretty.block
wenzelm@251
   177
          [Pretty.str (ty ^ " ::"), Pretty.brk 1, Pretty.str cl]
wenzelm@251
   178
      | pretty_arity ty (cl, srts) = Pretty.block
wenzelm@251
   179
          [Pretty.str (ty ^ " ::"), Pretty.brk 1,
wenzelm@251
   180
            Pretty.list "(" ")" (map pretty_sort srts),
wenzelm@251
   181
            Pretty.brk 1, Pretty.str cl];
wenzelm@251
   182
nipkow@963
   183
    fun pretty_arities (ty, ars) = map (pretty_arity ty) ars;
wenzelm@251
   184
wenzelm@251
   185
    fun pretty_const syn (c, ty) = Pretty.block
wenzelm@266
   186
      [Pretty.str (quote c ^ " ::"), Pretty.brk 1, prt_typ syn ty];
wenzelm@251
   187
wenzelm@251
   188
wenzelm@251
   189
    val Sg {tsig, const_tab, syn, stamps} = sg;
wenzelm@2963
   190
    val {classes, classrel, default, tycons, abbrs, arities} =
wenzelm@2185
   191
      Type.rep_tsig tsig;
wenzelm@251
   192
  in
wenzelm@386
   193
    Pretty.writeln (Pretty.strs ("stamps:" :: stamp_names stamps));
wenzelm@251
   194
    Pretty.writeln (Pretty.strs ("classes:" :: classes));
wenzelm@2979
   195
    Pretty.writeln (Pretty.big_list "classrel:" (map pretty_classrel classrel));
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:"
wenzelm@2979
   200
      (List.concat (map pretty_arities arities)));
wenzelm@251
   201
    Pretty.writeln (Pretty.big_list "consts:"
wenzelm@2979
   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@3552
   244
    handle TYPE (msg, _, _) => (error_msg 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
wenzelm@2979
   252
(*check for duplicate TVars with distinct sorts*)
wenzelm@2979
   253
fun nodup_TVars (tvars, T) =
wenzelm@2979
   254
  (case T of
wenzelm@2979
   255
    Type (_, Ts) => nodup_TVars_list (tvars, Ts)
wenzelm@2979
   256
  | TFree _ => tvars
wenzelm@2979
   257
  | TVar (v as (a, S)) =>
wenzelm@2979
   258
      (case assoc_string_int (tvars, a) of
wenzelm@2979
   259
        Some S' =>
wenzelm@2979
   260
          if S = S' then tvars
wenzelm@2979
   261
          else raise_type ("Type variable " ^ Syntax.string_of_vname a ^
wenzelm@2979
   262
            " has two distinct sorts") [TVar (a, S'), T] []
wenzelm@2979
   263
      | None => v :: tvars))
wenzelm@2979
   264
(*equivalent to foldl nodup_TVars_list, but 3X faster under Poly/ML*)
wenzelm@2979
   265
and nodup_TVars_list (tvars, []) = tvars
wenzelm@2979
   266
  | nodup_TVars_list (tvars, T :: Ts) =
wenzelm@2979
   267
      nodup_TVars_list (nodup_TVars (tvars, T), Ts);
nipkow@1494
   268
wenzelm@2979
   269
(*check for duplicate Vars with distinct types*)
nipkow@1494
   270
fun nodup_Vars tm =
wenzelm@2979
   271
  let
wenzelm@2979
   272
    fun nodups vars tvars tm =
wenzelm@2979
   273
      (case tm of
wenzelm@2979
   274
        Const (c, T) => (vars, nodup_TVars (tvars, T))
wenzelm@2979
   275
      | Free (a, T) => (vars, nodup_TVars (tvars, T))
wenzelm@2979
   276
      | Var (v as (ixn, T)) =>
wenzelm@2979
   277
          (case assoc_string_int (vars, ixn) of
wenzelm@2979
   278
            Some T' =>
wenzelm@2979
   279
              if T = T' then (vars, nodup_TVars (tvars, T))
wenzelm@2979
   280
              else raise_type ("Variable " ^ Syntax.string_of_vname ixn ^
wenzelm@2979
   281
                " has two distinct types") [T', T] []
wenzelm@2979
   282
          | None => (v :: vars, tvars))
wenzelm@2979
   283
      | Bound _ => (vars, tvars)
wenzelm@2979
   284
      | Abs(_, T, t) => nodups vars (nodup_TVars (tvars, T)) t
wenzelm@2979
   285
      | s $ t =>
wenzelm@2979
   286
          let val (vars',tvars') = nodups vars tvars s in
wenzelm@2979
   287
            nodups vars' tvars' t
wenzelm@2979
   288
          end);
wenzelm@2979
   289
  in nodups [] [] tm; () end;
wenzelm@2979
   290
wenzelm@251
   291
wenzelm@386
   292
fun mapfilt_atoms f (Abs (_, _, t)) = mapfilt_atoms f t
wenzelm@386
   293
  | mapfilt_atoms f (t $ u) = mapfilt_atoms f t @ mapfilt_atoms f u
wenzelm@386
   294
  | mapfilt_atoms f a = (case f a of Some y => [y] | None => []);
clasohm@0
   295
wenzelm@2979
   296
wenzelm@386
   297
fun certify_term (sg as Sg {tsig, ...}) tm =
wenzelm@251
   298
  let
wenzelm@251
   299
    fun valid_const a T =
wenzelm@386
   300
      (case const_type sg a of
wenzelm@2185
   301
        Some U => Type.typ_instance (tsig, T, U)
wenzelm@251
   302
      | _ => false);
wenzelm@169
   303
wenzelm@251
   304
    fun atom_err (Const (a, T)) =
wenzelm@251
   305
          if valid_const a T then None
wenzelm@251
   306
          else Some ("Illegal type for constant " ^ quote a ^ " :: " ^
wenzelm@251
   307
            quote (string_of_typ sg T))
wenzelm@251
   308
      | atom_err (Var ((x, i), _)) =
wenzelm@251
   309
          if i < 0 then Some ("Negative index for Var " ^ quote x) else None
wenzelm@251
   310
      | atom_err _ = None;
wenzelm@251
   311
wenzelm@251
   312
    val norm_tm =
wenzelm@2185
   313
      (case it_term_types (Type.typ_errors tsig) (tm, []) of
wenzelm@2185
   314
        [] => map_term_types (Type.norm_typ tsig) tm
wenzelm@251
   315
      | errs => raise_type (cat_lines errs) [] [tm]);
nipkow@1494
   316
    val _ = nodup_Vars norm_tm;
wenzelm@251
   317
  in
wenzelm@386
   318
    (case mapfilt_atoms atom_err norm_tm of
wenzelm@251
   319
      [] => (norm_tm, type_of norm_tm, maxidx_of_term norm_tm)
wenzelm@251
   320
    | errs => raise_type (cat_lines errs) [] [norm_tm])
wenzelm@251
   321
  end;
wenzelm@251
   322
wenzelm@251
   323
wenzelm@251
   324
wenzelm@583
   325
(** infer_types **)         (*exception ERROR*)
wenzelm@251
   326
wenzelm@2979
   327
(*
wenzelm@2979
   328
  ts: list of alternative parses (hopefully only one is type-correct)
wenzelm@2979
   329
  T: expected type
wenzelm@2979
   330
wenzelm@2979
   331
  def_type: partial map from indexnames to types (constrains Frees, Vars)
wenzelm@2979
   332
  def_sort: partial map from indexnames to sorts (constrains TFrees, TVars)
wenzelm@2979
   333
  used: list of already used type variables
wenzelm@2979
   334
  freeze: if true then generated parameters are turned into TFrees, else TVars
wenzelm@2979
   335
*)
wenzelm@2979
   336
wenzelm@2979
   337
fun infer_types sg def_type def_sort used freeze (ts, T) =
wenzelm@251
   338
  let
wenzelm@562
   339
    val Sg {tsig, ...} = sg;
wenzelm@2979
   340
    val prt = setmp Syntax.show_brackets true (pretty_term sg);
wenzelm@2979
   341
    val prT = pretty_typ sg;
wenzelm@2979
   342
    val infer = Type.infer_types prt prT tsig (const_type sg)
wenzelm@2979
   343
      def_type def_sort used freeze;
wenzelm@169
   344
wenzelm@2979
   345
    val T' = certify_typ sg T handle TYPE (msg, _, _) => error msg;
clasohm@623
   346
wenzelm@2979
   347
    fun warn () =
wenzelm@2979
   348
      if length ts > 1 andalso length ts <= ! Syntax.ambiguity_level
nipkow@952
   349
      then (*no warning shown yet*)
wenzelm@2979
   350
        warning "Currently parsed input \
wenzelm@2979
   351
          \produces more than one parse tree.\n\
wenzelm@2979
   352
          \For more information lower Syntax.ambiguity_level."
nipkow@952
   353
      else ();
clasohm@623
   354
wenzelm@2979
   355
    datatype result =
wenzelm@2979
   356
      One of int * term * (indexname * typ) list |
wenzelm@2979
   357
      Errs of string list |
wenzelm@2979
   358
      Ambigs of term list;
clasohm@623
   359
wenzelm@2979
   360
    fun process_term (res, (t, i)) =
wenzelm@2979
   361
      let val ([u], tye) = infer [T'] [t] in
wenzelm@2979
   362
        (case res of
wenzelm@2979
   363
          One (_, t0, _) => Ambigs ([u, t0])
wenzelm@2979
   364
        | Errs _ => One (i, u, tye)
wenzelm@2979
   365
        | Ambigs us => Ambigs (u :: us))
wenzelm@2979
   366
      end handle TYPE (msg, _, _) =>
wenzelm@2979
   367
        (case res of
wenzelm@2979
   368
          Errs errs => Errs (msg :: errs)
wenzelm@2979
   369
        | _ => res);
wenzelm@2979
   370
  in
wenzelm@2979
   371
    (case foldl process_term (Errs [], ts ~~ (0 upto (length ts - 1))) of
wenzelm@2979
   372
      One res =>
wenzelm@2979
   373
       (if length ts > ! Syntax.ambiguity_level then
wenzelm@3552
   374
          warning "Fortunately, only one parse tree is type correct.\n\
nipkow@952
   375
            \It helps (speed!) if you disambiguate your grammar or your input."
wenzelm@2979
   376
        else (); res)
wenzelm@2979
   377
    | Errs errs => (warn (); error (cat_lines errs))
wenzelm@2979
   378
    | Ambigs us =>
wenzelm@2979
   379
        (warn (); error ("Error: More than one term is type correct:\n" ^
wenzelm@2979
   380
          (cat_lines (map (Pretty.string_of o prt) us)))))
clasohm@623
   381
  end;
wenzelm@251
   382
wenzelm@251
   383
wenzelm@2979
   384
clasohm@623
   385
(** extend signature **)    (*exception ERROR*)
clasohm@623
   386
wenzelm@620
   387
(** signature extension functions **)  (*exception ERROR*)
wenzelm@386
   388
wenzelm@386
   389
fun decls_of name_of mfixs =
wenzelm@386
   390
  map (fn (x, y, mx) => (name_of x mx, y)) mfixs;
wenzelm@386
   391
wenzelm@386
   392
wenzelm@386
   393
(* add default sort *)
wenzelm@386
   394
wenzelm@386
   395
fun ext_defsort (syn, tsig, ctab) defsort =
wenzelm@2185
   396
  (syn, Type.ext_tsig_defsort tsig defsort, ctab);
wenzelm@386
   397
wenzelm@386
   398
wenzelm@386
   399
(* add type constructors *)
wenzelm@386
   400
wenzelm@386
   401
fun ext_types (syn, tsig, ctab) types =
wenzelm@386
   402
  (Syntax.extend_type_gram syn types,
wenzelm@2185
   403
    Type.ext_tsig_types tsig (decls_of Syntax.type_name types),
wenzelm@386
   404
    ctab);
wenzelm@386
   405
wenzelm@386
   406
wenzelm@386
   407
(* add type abbreviations *)
wenzelm@386
   408
wenzelm@386
   409
fun read_abbr syn tsig (t, vs, rhs_src) =
wenzelm@386
   410
  (t, vs, read_raw_typ syn tsig (K None) rhs_src)
wenzelm@386
   411
    handle ERROR => error ("in type abbreviation " ^ t);
wenzelm@386
   412
wenzelm@386
   413
fun ext_abbrs rd_abbr (syn, tsig, ctab) abbrs =
wenzelm@386
   414
  let
wenzelm@386
   415
    fun mfix_of (t, vs, _, mx) = (t, length vs, mx);
wenzelm@386
   416
    val syn1 = Syntax.extend_type_gram syn (map mfix_of abbrs);
wenzelm@386
   417
wenzelm@386
   418
    fun decl_of (t, vs, rhs, mx) =
wenzelm@562
   419
      rd_abbr syn1 tsig (Syntax.type_name t mx, vs, rhs);
wenzelm@386
   420
  in
wenzelm@2185
   421
    (syn1, Type.ext_tsig_abbrs tsig (map decl_of abbrs), ctab)
wenzelm@386
   422
  end;
wenzelm@386
   423
paulson@2228
   424
fun ext_tyabbrs_i arg = ext_abbrs (K (K I)) arg;
paulson@2228
   425
fun ext_tyabbrs   arg = ext_abbrs read_abbr arg;
wenzelm@386
   426
wenzelm@386
   427
wenzelm@386
   428
(* add type arities *)
wenzelm@386
   429
wenzelm@386
   430
fun ext_arities (syn, tsig, ctab) arities =
wenzelm@386
   431
  let
wenzelm@2185
   432
    val tsig1 = Type.ext_tsig_arities tsig arities;
wenzelm@2185
   433
    val log_types = Type.logical_types tsig1;
wenzelm@386
   434
  in
wenzelm@386
   435
    (Syntax.extend_log_types syn log_types, tsig1, ctab)
wenzelm@386
   436
  end;
wenzelm@386
   437
wenzelm@386
   438
wenzelm@386
   439
(* add term constants and syntax *)
wenzelm@386
   440
wenzelm@386
   441
fun err_in_const c =
wenzelm@386
   442
  error ("in declaration of constant " ^ quote c);
wenzelm@386
   443
wenzelm@386
   444
fun err_dup_consts cs =
wenzelm@386
   445
  error ("Duplicate declaration of constant(s) " ^ commas_quote cs);
wenzelm@386
   446
wenzelm@251
   447
wenzelm@386
   448
fun read_const syn tsig (c, ty_src, mx) =
wenzelm@386
   449
  (c, read_raw_typ syn tsig (K None) ty_src, mx)
wenzelm@562
   450
    handle ERROR => err_in_const (Syntax.const_name c mx);
wenzelm@386
   451
wenzelm@2197
   452
fun ext_cnsts rd_const syn_only prmode (syn, tsig, ctab) raw_consts =
wenzelm@386
   453
  let
wenzelm@2979
   454
    fun prep_const (c, ty, mx) =
wenzelm@2185
   455
     (c, compress_type (Type.varifyT (Type.cert_typ tsig (Type.no_tvars ty))), mx)
wenzelm@2185
   456
       handle TYPE (msg, _, _)
wenzelm@3552
   457
         => (error_msg msg; err_in_const (Syntax.const_name c mx));
wenzelm@386
   458
wenzelm@386
   459
    val consts = map (prep_const o rd_const syn tsig) raw_consts;
wenzelm@386
   460
    val decls =
wenzelm@386
   461
      if syn_only then []
wenzelm@562
   462
      else filter_out (equal "" o fst) (decls_of Syntax.const_name consts);
wenzelm@386
   463
  in
wenzelm@2197
   464
    (Syntax.extend_const_gram syn prmode consts, tsig,
wenzelm@386
   465
      Symtab.extend_new (ctab, decls)
wenzelm@386
   466
        handle Symtab.DUPS cs => err_dup_consts cs)
wenzelm@386
   467
  end;
wenzelm@386
   468
wenzelm@2359
   469
val ext_consts_i = ext_cnsts (K (K I)) false ("", true);
wenzelm@2359
   470
val ext_consts = ext_cnsts read_const false ("", true);
wenzelm@2359
   471
val ext_syntax_i = ext_cnsts (K (K I)) true ("", true);
wenzelm@2359
   472
val ext_syntax = ext_cnsts read_const true ("", true);
wenzelm@2197
   473
fun ext_modesyntax_i sg (prmode, consts) = ext_cnsts (K (K I)) true prmode sg consts;
wenzelm@2197
   474
fun ext_modesyntax sg (prmode, consts) = ext_cnsts read_const true prmode sg consts;
wenzelm@386
   475
wenzelm@386
   476
wenzelm@386
   477
(* add type classes *)
wenzelm@386
   478
wenzelm@386
   479
fun const_of_class c = c ^ "_class";
wenzelm@386
   480
wenzelm@386
   481
fun class_of_const c_class =
wenzelm@386
   482
  let
wenzelm@386
   483
    val c = implode (take (size c_class - 6, explode c_class));
wenzelm@386
   484
  in
wenzelm@386
   485
    if const_of_class c = c_class then c
wenzelm@386
   486
    else raise_term ("class_of_const: bad name " ^ quote c_class) []
wenzelm@386
   487
  end;
wenzelm@386
   488
wenzelm@386
   489
wenzelm@386
   490
fun ext_classes (syn, tsig, ctab) classes =
wenzelm@386
   491
  let
wenzelm@562
   492
    val names = map fst classes;
wenzelm@386
   493
    val consts =
wenzelm@386
   494
      map (fn c => (const_of_class c, a_itselfT --> propT, NoSyn)) names;
wenzelm@386
   495
  in
wenzelm@386
   496
    ext_consts_i
wenzelm@2185
   497
      (Syntax.extend_consts syn names, Type.ext_tsig_classes tsig classes, ctab)
wenzelm@386
   498
      consts
wenzelm@386
   499
  end;
wenzelm@386
   500
wenzelm@386
   501
wenzelm@2963
   502
(* add to classrel *)
wenzelm@421
   503
wenzelm@421
   504
fun ext_classrel (syn, tsig, ctab) pairs =
wenzelm@2963
   505
  (syn, Type.ext_tsig_classrel tsig pairs, ctab);
wenzelm@421
   506
wenzelm@421
   507
wenzelm@1159
   508
(* add to syntax *)
wenzelm@386
   509
wenzelm@1159
   510
fun ext_syn extfun (syn, tsig, ctab) args =
wenzelm@1159
   511
  (extfun syn args, tsig, ctab);
wenzelm@386
   512
wenzelm@386
   513
wenzelm@386
   514
(* build signature *)
wenzelm@386
   515
wenzelm@386
   516
fun ext_stamps stamps name =
wenzelm@386
   517
  let
wenzelm@386
   518
    val stmps = (case stamps of ref "#" :: ss => ss | ss => ss);
wenzelm@386
   519
  in
wenzelm@386
   520
    if exists (equal name o !) stmps then
wenzelm@386
   521
      error ("Theory already contains a " ^ quote name ^ " component")
wenzelm@402
   522
    else ref name :: stmps
wenzelm@386
   523
  end;
wenzelm@386
   524
wenzelm@386
   525
fun make_sign (syn, tsig, ctab) stamps name =
wenzelm@386
   526
  Sg {tsig = tsig, const_tab = ctab, syn = syn,
wenzelm@386
   527
    stamps = ext_stamps stamps name};
wenzelm@386
   528
wenzelm@386
   529
fun extend_sign extfun name decls (Sg {tsig, const_tab, syn, stamps}) =
wenzelm@386
   530
  make_sign (extfun (syn, tsig, const_tab) decls) stamps name;
wenzelm@386
   531
wenzelm@386
   532
wenzelm@386
   533
(* the external interfaces *)
wenzelm@386
   534
wenzelm@2197
   535
val add_classes      = extend_sign ext_classes "#";
wenzelm@2197
   536
val add_classrel     = extend_sign ext_classrel "#";
wenzelm@2197
   537
val add_defsort      = extend_sign ext_defsort "#";
wenzelm@2197
   538
val add_types        = extend_sign ext_types "#";
wenzelm@2197
   539
val add_tyabbrs      = extend_sign ext_tyabbrs "#";
wenzelm@2197
   540
val add_tyabbrs_i    = extend_sign ext_tyabbrs_i "#";
wenzelm@2197
   541
val add_arities      = extend_sign ext_arities "#";
wenzelm@2197
   542
val add_consts       = extend_sign ext_consts "#";
wenzelm@2197
   543
val add_consts_i     = extend_sign ext_consts_i "#";
wenzelm@2197
   544
val add_syntax       = extend_sign ext_syntax "#";
wenzelm@2197
   545
val add_syntax_i     = extend_sign ext_syntax_i "#";
wenzelm@2197
   546
val add_modesyntax   = extend_sign ext_modesyntax "#";
wenzelm@2197
   547
val add_modesyntax_i = extend_sign ext_modesyntax_i "#";
wenzelm@2197
   548
val add_trfuns       = extend_sign (ext_syn Syntax.extend_trfuns) "#";
wenzelm@2385
   549
val add_trfunsT      = extend_sign (ext_syn Syntax.extend_trfunsT) "#";
wenzelm@2693
   550
val add_tokentrfuns  = extend_sign (ext_syn Syntax.extend_tokentrfuns) "#";
wenzelm@2197
   551
val add_trrules      = extend_sign (ext_syn Syntax.extend_trrules) "#";
wenzelm@2197
   552
val add_trrules_i    = extend_sign (ext_syn Syntax.extend_trrules_i) "#";
wenzelm@386
   553
wenzelm@386
   554
fun add_name name sg = extend_sign K name () sg;
wenzelm@386
   555
val make_draft = add_name "#";
wenzelm@386
   556
wenzelm@386
   557
wenzelm@386
   558
wenzelm@620
   559
(** merge signatures **)    (*exception TERM*) (*exception ERROR (* FIXME *)*)
wenzelm@143
   560
wenzelm@251
   561
fun merge (sg1, sg2) =
wenzelm@2185
   562
  if fast_subsig (sg2, sg1) then sg1
wenzelm@2185
   563
  else if fast_subsig (sg1, sg2) then sg2
wenzelm@2185
   564
  else if subsig (sg2, sg1) then sg1
wenzelm@251
   565
  else if subsig (sg1, sg2) then sg2
wenzelm@386
   566
  else if is_draft sg1 orelse is_draft sg2 then
wenzelm@386
   567
    raise_term "Illegal merge of draft signatures" []
wenzelm@251
   568
  else
wenzelm@251
   569
    (*neither is union already; must form union*)
wenzelm@251
   570
    let
wenzelm@251
   571
      val Sg {tsig = tsig1, const_tab = const_tab1, syn = syn1,
wenzelm@251
   572
        stamps = stamps1} = sg1;
wenzelm@251
   573
      val Sg {tsig = tsig2, const_tab = const_tab2, syn = syn2,
wenzelm@251
   574
        stamps = stamps2} = sg2;
wenzelm@251
   575
wenzelm@386
   576
wenzelm@402
   577
      val stamps = merge_rev_lists stamps1 stamps2;
wenzelm@402
   578
      val _ =
wenzelm@402
   579
        (case duplicates (stamp_names stamps) of
wenzelm@402
   580
          [] => ()
wenzelm@402
   581
        | dups => raise_term ("Attempt to merge different versions of theories "
wenzelm@402
   582
            ^ commas_quote dups) []);
wenzelm@402
   583
wenzelm@2185
   584
      val tsig = Type.merge_tsigs (tsig1, tsig2);
wenzelm@251
   585
wenzelm@251
   586
      val const_tab = Symtab.merge (op =) (const_tab1, const_tab2)
wenzelm@386
   587
        handle Symtab.DUPS cs =>
wenzelm@386
   588
          raise_term ("Incompatible types for constant(s) " ^ commas_quote cs) [];
wenzelm@386
   589
wenzelm@386
   590
      val syn = Syntax.merge_syntaxes syn1 syn2;
wenzelm@251
   591
    in
wenzelm@251
   592
      Sg {tsig = tsig, const_tab = const_tab, syn = syn, stamps = stamps}
wenzelm@251
   593
    end;
wenzelm@143
   594
clasohm@0
   595
clasohm@0
   596
wenzelm@251
   597
(** the Pure signature **)
clasohm@0
   598
clasohm@922
   599
val proto_pure =
wenzelm@2185
   600
  make_sign (Syntax.pure_syn, Type.tsig0, Symtab.null) [] "#"
wenzelm@410
   601
  |> add_types
wenzelm@386
   602
   (("fun", 2, NoSyn) ::
wenzelm@386
   603
    ("prop", 0, NoSyn) ::
wenzelm@386
   604
    ("itself", 1, NoSyn) ::
wenzelm@562
   605
    Syntax.pure_types)
wenzelm@562
   606
  |> add_classes [(logicC, [])]
wenzelm@410
   607
  |> add_defsort logicS
wenzelm@410
   608
  |> add_arities
wenzelm@386
   609
   [("fun", [logicS, logicS], logicS),
wenzelm@386
   610
    ("prop", [], logicS),
wenzelm@386
   611
    ("itself", [logicS], logicS)]
wenzelm@562
   612
  |> add_syntax Syntax.pure_syntax
wenzelm@2359
   613
  |> add_modesyntax (("symbols", true), Syntax.pure_sym_syntax)
wenzelm@410
   614
  |> add_trfuns Syntax.pure_trfuns
wenzelm@2693
   615
  |> add_trfunsT Syntax.pure_trfunsT
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;