src/Pure/sign.ML
author wenzelm
Tue Aug 23 19:31:05 1994 +0200 (1994-08-23)
changeset 573 2fa5ef27bd0a
parent 562 e9572d03b724
child 583 550292083e66
permissions -rw-r--r--
removed constant _constrain from Pure sig;
     1 (*  Title:      Pure/sign.ML
     2     ID:         $Id$
     3     Author:     Lawrence C Paulson and Markus Wenzel
     4 
     5 The abstract type "sg" of signatures.
     6 
     7 TODO:
     8   clean
     9 *)
    10 
    11 signature SIGN =
    12 sig
    13   structure Symtab: SYMTAB
    14   structure Syntax: SYNTAX
    15   structure Type: TYPE
    16   sharing Symtab = Type.Symtab
    17   local open Type Syntax in
    18     type sg
    19     val rep_sg: sg ->
    20       {tsig: type_sig,
    21        const_tab: typ Symtab.table,
    22        syn: syntax,
    23        stamps: string ref list}
    24     val subsig: sg * sg -> bool
    25     val eq_sg: sg * sg -> bool
    26     val is_draft: sg -> bool
    27     val const_type: sg -> string -> typ option
    28     val classes: sg -> class list
    29     val subsort: sg -> sort * sort -> bool      (* FIXME move? *)
    30     val norm_sort: sg -> sort -> sort           (* FIXME move? *)
    31     val print_sg: sg -> unit
    32     val pretty_sg: sg -> Pretty.T
    33     val pprint_sg: sg -> pprint_args -> unit
    34     val pretty_term: sg -> term -> Pretty.T
    35     val pretty_typ: sg -> typ -> Pretty.T
    36     val string_of_term: sg -> term -> string
    37     val string_of_typ: sg -> typ -> string
    38     val pprint_term: sg -> term -> pprint_args -> unit
    39     val pprint_typ: sg -> typ -> pprint_args -> unit
    40     val certify_typ: sg -> typ -> typ
    41     val certify_term: sg -> term -> term * typ * int
    42     val read_typ: sg * (indexname -> sort option) -> string -> typ
    43     val infer_types: sg -> (indexname -> typ option) ->
    44       (indexname -> sort option) -> term * typ -> term * (indexname * typ) list
    45     val add_classes: (class * class list) list -> sg -> sg
    46     val add_classrel: (class * class) list -> sg -> sg
    47     val add_defsort: sort -> sg -> sg
    48     val add_types: (string * int * mixfix) list -> sg -> sg
    49     val add_tyabbrs: (string * string list * string * mixfix) list -> sg -> sg
    50     val add_tyabbrs_i: (string * string list * typ * mixfix) list -> sg -> sg
    51     val add_arities: (string * sort list * sort) list -> sg -> sg
    52     val add_consts: (string * string * mixfix) list -> sg -> sg
    53     val add_consts_i: (string * typ * mixfix) list -> sg -> sg
    54     val add_syntax: (string * string * mixfix) list -> sg -> sg
    55     val add_syntax_i: (string * typ * mixfix) list -> sg -> sg
    56     val add_trfuns:
    57       (string * (ast list -> ast)) list *
    58       (string * (term list -> term)) list *
    59       (string * (term list -> term)) list *
    60       (string * (ast list -> ast)) list -> sg -> sg
    61     val add_trrules: xrule list -> sg -> sg
    62     val add_name: string -> sg -> sg
    63     val make_draft: sg -> sg
    64     val merge: sg * sg -> sg
    65     val pure: sg
    66     val const_of_class: class -> string
    67     val class_of_const: string -> class
    68   end
    69 end;
    70 
    71 functor SignFun(structure Syntax: SYNTAX and Type: TYPE): SIGN =
    72 struct
    73 
    74 structure Symtab = Type.Symtab;
    75 structure Syntax = Syntax;
    76 structure BasicSyntax: BASIC_SYNTAX = Syntax;
    77 structure Pretty = Syntax.Pretty;
    78 structure Type = Type;
    79 open BasicSyntax Type;
    80 
    81 
    82 (** datatype sg **)
    83 
    84 (*the "ref" in stamps ensures that no two signatures are identical -- it is
    85   impossible to forge a signature*)
    86 
    87 datatype sg =
    88   Sg of {
    89     tsig: type_sig,                 (*order-sorted signature of types*)
    90     const_tab: typ Symtab.table,    (*types of constants*)
    91     syn: Syntax.syntax,             (*syntax for parsing and printing*)
    92     stamps: string ref list};       (*unique theory indentifier*)
    93 
    94 fun rep_sg (Sg args) = args;
    95 val tsig_of = #tsig o rep_sg;
    96 
    97 
    98 (* stamps *)
    99 
   100 fun is_draft (Sg {stamps = ref "#" :: _, ...}) = true
   101   | is_draft _ = false;
   102 
   103 fun subsig (Sg {stamps = s1, ...}, Sg {stamps = s2, ...}) = s1 subset s2;
   104 
   105 fun eq_sg (Sg {stamps = s1, ...}, Sg {stamps = s2, ...}) = eq_set (s1, s2);
   106 
   107 
   108 (* consts *)
   109 
   110 fun const_type (Sg {const_tab, ...}) c =
   111   Symtab.lookup (const_tab, c);
   112 
   113 
   114 (* classes *)
   115 
   116 val classes = #classes o Type.rep_tsig o tsig_of;
   117 
   118 val subsort = Type.subsort o tsig_of;
   119 val norm_sort = Type.norm_sort o tsig_of;
   120 
   121 
   122 
   123 (** print signature **)
   124 
   125 val stamp_names = rev o map !;
   126 
   127 fun print_sg sg =
   128   let
   129     fun prt_typ syn ty = Pretty.quote (Syntax.pretty_typ syn ty);
   130 
   131     fun pretty_sort [cl] = Pretty.str cl
   132       | pretty_sort cls = Pretty.str_list "{" "}" cls;
   133 
   134     fun pretty_subclass (cl, cls) = Pretty.block
   135       [Pretty.str (cl ^ " <"), Pretty.brk 1, pretty_sort cls];
   136 
   137     fun pretty_default cls = Pretty.block
   138       [Pretty.str "default:", Pretty.brk 1, pretty_sort cls];
   139 
   140     fun pretty_arg (ty, n) = Pretty.str (ty ^ " " ^ string_of_int n);
   141 
   142     fun pretty_abbr syn (ty, (vs, rhs)) = Pretty.block
   143       [prt_typ syn (Type (ty, map (fn v => TVar (v, [])) vs)),
   144         Pretty.str " =", Pretty.brk 1, prt_typ syn rhs];
   145 
   146     fun pretty_arity ty (cl, []) = Pretty.block
   147           [Pretty.str (ty ^ " ::"), Pretty.brk 1, Pretty.str cl]
   148       | pretty_arity ty (cl, srts) = Pretty.block
   149           [Pretty.str (ty ^ " ::"), Pretty.brk 1,
   150             Pretty.list "(" ")" (map pretty_sort srts),
   151             Pretty.brk 1, Pretty.str cl];
   152 
   153     fun pretty_coreg (ty, ars) = map (pretty_arity ty) ars;
   154 
   155     fun pretty_const syn (c, ty) = Pretty.block
   156       [Pretty.str (quote c ^ " ::"), Pretty.brk 1, prt_typ syn ty];
   157 
   158 
   159     val Sg {tsig, const_tab, syn, stamps} = sg;
   160     val {classes, subclass, default, args, abbrs, coreg} = rep_tsig tsig;
   161   in
   162     Pretty.writeln (Pretty.strs ("stamps:" :: stamp_names stamps));
   163     Pretty.writeln (Pretty.strs ("classes:" :: classes));
   164     Pretty.writeln (Pretty.big_list "subclass:" (map pretty_subclass subclass));
   165     Pretty.writeln (pretty_default default);
   166     Pretty.writeln (Pretty.big_list "types:" (map pretty_arg args));
   167     Pretty.writeln (Pretty.big_list "abbrs:" (map (pretty_abbr syn) abbrs));
   168     Pretty.writeln (Pretty.big_list "coreg:" (flat (map pretty_coreg coreg)));
   169     Pretty.writeln (Pretty.big_list "consts:"
   170       (map (pretty_const syn) (Symtab.dest const_tab)))
   171   end;
   172 
   173 
   174 fun pretty_sg (Sg {stamps, ...}) =
   175   Pretty.str_list "{" "}" (stamp_names stamps);
   176 
   177 val pprint_sg = Pretty.pprint o pretty_sg;
   178 
   179 
   180 
   181 (** pretty printing of terms and types **)
   182 
   183 fun pretty_term (Sg {syn, ...}) = Syntax.pretty_term syn;
   184 fun pretty_typ (Sg {syn, ...}) = Syntax.pretty_typ syn;
   185 
   186 fun string_of_term (Sg {syn, ...}) = Syntax.string_of_term syn;
   187 fun string_of_typ (Sg {syn, ...}) = Syntax.string_of_typ syn;
   188 
   189 fun pprint_term sg = Pretty.pprint o Pretty.quote o (pretty_term sg);
   190 fun pprint_typ sg = Pretty.pprint o Pretty.quote o (pretty_typ sg);
   191 
   192 
   193 
   194 (** read types **)  (*exception ERROR*)
   195 
   196 fun err_in_type s =
   197   error ("The error(s) above occurred in type " ^ quote s);
   198 
   199 fun read_raw_typ syn tsig sort_of str =
   200   Syntax.read_typ syn (fn x => if_none (sort_of x) (defaultS tsig)) str
   201     handle ERROR => err_in_type str;
   202 
   203 (*read and certify typ wrt a signature*)
   204 fun read_typ (Sg {tsig, syn, ...}, sort_of) str =
   205   cert_typ tsig (read_raw_typ syn tsig sort_of str)
   206     handle TYPE (msg, _, _) => (writeln msg; err_in_type str);
   207 
   208 
   209 
   210 (** certify types and terms **)   (*exception TYPE*)
   211 
   212 fun certify_typ (Sg {tsig, ...}) ty = cert_typ tsig ty;
   213 
   214 
   215 fun mapfilt_atoms f (Abs (_, _, t)) = mapfilt_atoms f t
   216   | mapfilt_atoms f (t $ u) = mapfilt_atoms f t @ mapfilt_atoms f u
   217   | mapfilt_atoms f a = (case f a of Some y => [y] | None => []);
   218 
   219 fun certify_term (sg as Sg {tsig, ...}) tm =
   220   let
   221     fun valid_const a T =
   222       (case const_type sg a of
   223         Some U => typ_instance (tsig, T, U)
   224       | _ => false);
   225 
   226     fun atom_err (Const (a, T)) =
   227           if valid_const a T then None
   228           else Some ("Illegal type for constant " ^ quote a ^ " :: " ^
   229             quote (string_of_typ sg T))
   230       | atom_err (Var ((x, i), _)) =
   231           if i < 0 then Some ("Negative index for Var " ^ quote x) else None
   232       | atom_err _ = None;
   233 
   234     val norm_tm =
   235       (case it_term_types (typ_errors tsig) (tm, []) of
   236         [] => map_term_types (norm_typ tsig) tm
   237       | errs => raise_type (cat_lines errs) [] [tm]);
   238   in
   239     (case mapfilt_atoms atom_err norm_tm of
   240       [] => (norm_tm, type_of norm_tm, maxidx_of_term norm_tm)
   241     | errs => raise_type (cat_lines errs) [] [norm_tm])
   242   end;
   243 
   244 
   245 
   246 (** infer_types **)         (*exception ERROR*)   (* FIXME check *)
   247 
   248 fun infer_types sg types sorts (t, T) =
   249   let
   250     val Sg {tsig, ...} = sg;
   251     val show_typ = string_of_typ sg;
   252     val show_term = string_of_term sg;
   253 
   254     fun term_err [] = ""
   255       | term_err [t] = "\nInvolving this term:\n" ^ show_term t
   256       | term_err ts = "\nInvolving these terms:\n" ^ cat_lines (map show_term ts);
   257 
   258     val T' = certify_typ sg T
   259       handle TYPE (msg, _, _) => error msg;
   260     val (t', tye) = Type.infer_types (tsig, const_type sg, types, sorts, T', t)
   261       handle TYPE (msg, Ts, ts) => error ("Type checking error: " ^ msg ^ "\n"
   262         ^ cat_lines (map show_typ Ts) ^ term_err ts);
   263   in (t', tye) end;
   264 
   265 
   266 
   267 (** extend signature **)    (*exception ERROR*)
   268 
   269 (* FIXME -> type.ML *)
   270 
   271 (* FIXME replace? *)
   272 fun varify_typ (Type (t, tys)) = Type (t, map varify_typ tys)
   273   | varify_typ (TFree (a, s)) = TVar ((a, 0), s)
   274   | varify_typ (ty as TVar _) =
   275       raise_type "Illegal schematic type variable" [ty] [];
   276 
   277 
   278 (* fake newstyle interfaces *) (* FIXME -> type.ML *)
   279 
   280 fun ext_tsig_classes tsig classes =
   281   extend_tsig tsig (classes, [], [], []);
   282 
   283 fun ext_tsig_types tsig types =
   284   extend_tsig tsig ([], [], map (fn (t, n) => ([t], n)) types, []);
   285 
   286 (* FIXME varify_typ, rem_sorts; norm_typ (?) *)
   287 fun ext_tsig_abbrs tsig abbrs = Type.ext_tsig_abbrs tsig
   288   (map (fn (t, vs, rhs) => (t, (map (rpair 0) vs, varifyT rhs))) abbrs);
   289 
   290 fun ext_tsig_arities tsig arities = extend_tsig tsig
   291   ([], [], [], flat (map (fn (t, ss, cs) => map (fn c => ([t], (ss, c))) cs) arities));
   292 
   293 
   294 
   295 (** extension functions **)  (*exception ERROR*)
   296 
   297 fun decls_of name_of mfixs =
   298   map (fn (x, y, mx) => (name_of x mx, y)) mfixs;
   299 
   300 
   301 (* add default sort *)
   302 
   303 fun ext_defsort (syn, tsig, ctab) defsort =
   304   (syn, ext_tsig_defsort tsig defsort, ctab);
   305 
   306 
   307 (* add type constructors *)
   308 
   309 fun ext_types (syn, tsig, ctab) types =
   310   (Syntax.extend_type_gram syn types,
   311     ext_tsig_types tsig (decls_of Syntax.type_name types),
   312     ctab);
   313 
   314 
   315 (* add type abbreviations *)
   316 
   317 fun read_abbr syn tsig (t, vs, rhs_src) =
   318   (t, vs, read_raw_typ syn tsig (K None) rhs_src)
   319     handle ERROR => error ("in type abbreviation " ^ t);
   320 
   321 fun ext_abbrs rd_abbr (syn, tsig, ctab) abbrs =
   322   let
   323     fun mfix_of (t, vs, _, mx) = (t, length vs, mx);
   324     val syn1 = Syntax.extend_type_gram syn (map mfix_of abbrs);
   325 
   326     fun decl_of (t, vs, rhs, mx) =
   327       rd_abbr syn1 tsig (Syntax.type_name t mx, vs, rhs);
   328   in
   329     (syn1, ext_tsig_abbrs tsig (map decl_of abbrs), ctab)
   330   end;
   331 
   332 val ext_tyabbrs_i = ext_abbrs (K (K I));
   333 val ext_tyabbrs = ext_abbrs read_abbr;
   334 
   335 
   336 (* add type arities *)
   337 
   338 fun ext_arities (syn, tsig, ctab) arities =
   339   let
   340     val tsig1 = ext_tsig_arities tsig arities;
   341     val log_types = logical_types tsig1;
   342   in
   343     (Syntax.extend_log_types syn log_types, tsig1, ctab)
   344   end;
   345 
   346 
   347 (* add term constants and syntax *)
   348 
   349 fun err_in_const c =
   350   error ("in declaration of constant " ^ quote c);
   351 
   352 fun err_dup_consts cs =
   353   error ("Duplicate declaration of constant(s) " ^ commas_quote cs);
   354 
   355 
   356 fun read_const syn tsig (c, ty_src, mx) =
   357   (c, read_raw_typ syn tsig (K None) ty_src, mx)
   358     handle ERROR => err_in_const (Syntax.const_name c mx);
   359 
   360 (* FIXME move *)
   361 fun no_tvars ty =
   362   if null (typ_tvars ty) then ty
   363   else raise_type "Illegal schematic type variable(s)" [ty] [];
   364 
   365 fun ext_cnsts rd_const syn_only (syn, tsig, ctab) raw_consts =
   366   let
   367     (* FIXME clean *)
   368     fun prep_const (c, ty, mx) = (c, varify_typ (cert_typ tsig (no_tvars ty)), mx)
   369       handle TYPE (msg, _, _) => (writeln msg; err_in_const (Syntax.const_name c mx));
   370 
   371     val consts = map (prep_const o rd_const syn tsig) raw_consts;
   372     val decls =
   373       if syn_only then []
   374       else filter_out (equal "" o fst) (decls_of Syntax.const_name consts);
   375   in
   376     (Syntax.extend_const_gram syn consts, tsig,
   377       Symtab.extend_new (ctab, decls)
   378         handle Symtab.DUPS cs => err_dup_consts cs)
   379   end;
   380 
   381 val ext_consts_i = ext_cnsts (K (K I)) false;
   382 val ext_consts = ext_cnsts read_const false;
   383 val ext_syntax_i = ext_cnsts (K (K I)) true;
   384 val ext_syntax = ext_cnsts read_const true;
   385 
   386 
   387 (* add type classes *)
   388 
   389 fun const_of_class c = c ^ "_class";
   390 
   391 fun class_of_const c_class =
   392   let
   393     val c = implode (take (size c_class - 6, explode c_class));
   394   in
   395     if const_of_class c = c_class then c
   396     else raise_term ("class_of_const: bad name " ^ quote c_class) []
   397   end;
   398 
   399 
   400 fun ext_classes (syn, tsig, ctab) classes =
   401   let
   402     val names = map fst classes;
   403     val consts =
   404       map (fn c => (const_of_class c, a_itselfT --> propT, NoSyn)) names;
   405   in
   406     ext_consts_i
   407       (Syntax.extend_consts syn names, ext_tsig_classes tsig classes, ctab)
   408       consts
   409   end;
   410 
   411 
   412 (* add to subclass relation *)
   413 
   414 fun ext_classrel (syn, tsig, ctab) pairs =
   415   (syn, ext_tsig_subclass tsig pairs, ctab);
   416 
   417 
   418 (* add syntactic translations *)
   419 
   420 fun ext_trfuns (syn, tsig, ctab) trfuns =
   421   (Syntax.extend_trfuns syn trfuns, tsig, ctab);
   422 
   423 fun ext_trrules (syn, tsig, ctab) xrules =
   424   (Syntax.extend_trrules syn xrules, tsig, ctab);
   425 
   426 
   427 (* build signature *)
   428 
   429 fun ext_stamps stamps name =
   430   let
   431     val stmps = (case stamps of ref "#" :: ss => ss | ss => ss);
   432   in
   433     if exists (equal name o !) stmps then
   434       error ("Theory already contains a " ^ quote name ^ " component")
   435     else ref name :: stmps
   436   end;
   437 
   438 fun make_sign (syn, tsig, ctab) stamps name =
   439   Sg {tsig = tsig, const_tab = ctab, syn = syn,
   440     stamps = ext_stamps stamps name};
   441 
   442 fun extend_sign extfun name decls (Sg {tsig, const_tab, syn, stamps}) =
   443   make_sign (extfun (syn, tsig, const_tab) decls) stamps name;
   444 
   445 
   446 (* the external interfaces *)
   447 
   448 val add_classes   = extend_sign ext_classes "#";
   449 val add_classrel  = extend_sign ext_classrel "#";
   450 val add_defsort   = extend_sign ext_defsort "#";
   451 val add_types     = extend_sign ext_types "#";
   452 val add_tyabbrs   = extend_sign ext_tyabbrs "#";
   453 val add_tyabbrs_i = extend_sign ext_tyabbrs_i "#";
   454 val add_arities   = extend_sign ext_arities "#";
   455 val add_consts    = extend_sign ext_consts "#";
   456 val add_consts_i  = extend_sign ext_consts_i "#";
   457 val add_syntax    = extend_sign ext_syntax "#";
   458 val add_syntax_i  = extend_sign ext_syntax_i "#";
   459 val add_trfuns    = extend_sign ext_trfuns "#";
   460 val add_trrules   = extend_sign ext_trrules "#";
   461 
   462 fun add_name name sg = extend_sign K name () sg;
   463 val make_draft = add_name "#";
   464 
   465 
   466 
   467 (** merge signatures **)    (*exception TERM*)
   468 
   469 fun merge (sg1, sg2) =
   470   if subsig (sg2, sg1) then sg1
   471   else if subsig (sg1, sg2) then sg2
   472   else if is_draft sg1 orelse is_draft sg2 then
   473     raise_term "Illegal merge of draft signatures" []
   474   else
   475     (*neither is union already; must form union*)
   476     let
   477       val Sg {tsig = tsig1, const_tab = const_tab1, syn = syn1,
   478         stamps = stamps1} = sg1;
   479       val Sg {tsig = tsig2, const_tab = const_tab2, syn = syn2,
   480         stamps = stamps2} = sg2;
   481 
   482 
   483       val stamps = merge_rev_lists stamps1 stamps2;
   484       val _ =
   485         (case duplicates (stamp_names stamps) of
   486           [] => ()
   487         | dups => raise_term ("Attempt to merge different versions of theories "
   488             ^ commas_quote dups) []);
   489 
   490       val tsig = merge_tsigs (tsig1, tsig2);
   491 
   492       val const_tab = Symtab.merge (op =) (const_tab1, const_tab2)
   493         handle Symtab.DUPS cs =>
   494           raise_term ("Incompatible types for constant(s) " ^ commas_quote cs) [];
   495 
   496       val syn = Syntax.merge_syntaxes syn1 syn2;
   497     in
   498       Sg {tsig = tsig, const_tab = const_tab, syn = syn, stamps = stamps}
   499     end;
   500 
   501 
   502 
   503 (** the Pure signature **)
   504 
   505 val pure =
   506   make_sign (Syntax.type_syn, tsig0, Symtab.null) [] "#"
   507   |> add_types
   508    (("fun", 2, NoSyn) ::
   509     ("prop", 0, NoSyn) ::
   510     ("itself", 1, NoSyn) ::
   511     Syntax.pure_types)
   512   |> add_classes [(logicC, [])]
   513   |> add_defsort logicS
   514   |> add_arities
   515    [("fun", [logicS, logicS], logicS),
   516     ("prop", [], logicS),
   517     ("itself", [logicS], logicS)]
   518   |> add_syntax Syntax.pure_syntax
   519   |> add_trfuns Syntax.pure_trfuns
   520   |> add_consts
   521    [("==", "['a::{}, 'a] => prop", Mixfix ("(_ ==/ _)", [3, 2], 2)),
   522     ("=?=", "['a::{}, 'a] => prop", Mixfix ("(_ =?=/ _)", [3, 2], 2)),
   523     ("==>", "[prop, prop] => prop", Mixfix ("(_ ==>/ _)", [2, 1], 1)),
   524     ("all", "('a => prop) => prop", Binder ("!!", 0)),
   525     ("TYPE", "'a itself", NoSyn)]
   526   |> add_name "Pure";
   527 
   528 
   529 end;
   530