src/Pure/sign.ML
changeset 3967 edd5ff9371f8
parent 3956 d59fe4579004
child 3969 9c742951a923
equal deleted inserted replaced
3966:b06face07498 3967:edd5ff9371f8
    16 type xterm = term;
    16 type xterm = term;
    17 
    17 
    18 signature SIGN =
    18 signature SIGN =
    19 sig
    19 sig
    20   type sg
    20   type sg
       
    21   type sg_ref
    21   val rep_sg: sg ->
    22   val rep_sg: sg ->
    22    {tsig: Type.type_sig,
    23    {id: string ref,			(* FIXME hide!? *)
       
    24     self: sg_ref,
       
    25     tsig: Type.type_sig,
    23     const_tab: typ Symtab.table,
    26     const_tab: typ Symtab.table,
    24     syn: Syntax.syntax,
    27     syn: Syntax.syntax,
    25     path: string list,
    28     path: string list,
    26     spaces: (string * NameSpace.T) list,
    29     spaces: (string * NameSpace.T) list,
    27     data: Data.T,
    30     data: Data.T,
    28     stamps: string ref list}
    31     stamps: string ref list}		(* FIXME hide!? *)
       
    32   val tsig_of: sg -> Type.type_sig
       
    33   val deref: sg_ref -> sg
       
    34   val self_ref: sg -> sg_ref
    29   val subsig: sg * sg -> bool
    35   val subsig: sg * sg -> bool
    30   val eq_sg: sg * sg -> bool
    36   val eq_sg: sg * sg -> bool
    31   val same_sg: sg * sg -> bool
    37   val same_sg: sg * sg -> bool
    32   val is_draft: sg -> bool
    38   val is_draft: sg -> bool
    33   val const_type: sg -> string -> typ option
    39   val const_type: sg -> string -> typ option
   106   val init_data: string * exn * (exn -> exn) * (exn * exn -> exn) *
   112   val init_data: string * exn * (exn -> exn) * (exn * exn -> exn) *
   107     (string -> exn -> unit) -> sg -> sg
   113     (string -> exn -> unit) -> sg -> sg
   108   val get_data: sg -> string -> exn
   114   val get_data: sg -> string -> exn
   109   val put_data: string * exn -> sg -> sg
   115   val put_data: string * exn -> sg -> sg
   110   val print_data: sg -> string -> unit
   116   val print_data: sg -> string -> unit
   111   val prep_ext: sg -> sg
   117   val merge_refs: sg_ref * sg_ref -> sg_ref
       
   118   val make_draft: sg -> sg
   112   val merge: sg * sg -> sg
   119   val merge: sg * sg -> sg
   113   val nontriv_merge: sg * sg -> sg
       
   114   val proto_pure: sg
   120   val proto_pure: sg
   115   val pure: sg
   121   val pure: sg
   116   val cpure: sg
   122   val cpure: sg
   117   val const_of_class: class -> string
   123   val const_of_class: class -> string
   118   val class_of_const: string -> class
   124   val class_of_const: string -> class
   119 end;
   125 end;
   120 
   126 
   121 structure Sign : SIGN =
   127 structure Sign : SIGN =
   122 struct
   128 struct
   123 
   129 
       
   130 
   124 (** datatype sg **)
   131 (** datatype sg **)
   125 
   132 
   126 datatype sg =
   133 datatype sg =
   127   Sg of {
   134   Sg of {
       
   135     id: string ref,				(*id*)
       
   136     self: sg_ref,				(*mutable self reference*)
   128     tsig: Type.type_sig,                        (*order-sorted signature of types*)
   137     tsig: Type.type_sig,                        (*order-sorted signature of types*)
   129     const_tab: typ Symtab.table,                (*type schemes of constants*)
   138     const_tab: typ Symtab.table,                (*type schemes of constants*)
   130     syn: Syntax.syntax,                         (*syntax for parsing and printing*)
   139     syn: Syntax.syntax,                         (*syntax for parsing and printing*)
   131     path: string list,                     	(*current name space entry prefix*)
   140     path: string list,                     	(*current name space entry prefix*)
   132     spaces: (string * NameSpace.T) list,   	(*name spaces for consts, types etc.*)
   141     spaces: (string * NameSpace.T) list,   	(*name spaces for consts, types etc.*)
   133     data: Data.T,				(*additional data*)
   142     data: Data.T,				(*additional data*)
   134     stamps: string ref list};                   (*unique theory indentifier*)
   143     stamps: string ref list}                    (*unique theory indentifier*)
   135       (*the "ref" in stamps ensures that no two signatures are identical
   144       (*the "ref" in stamps ensures that no two signatures are identical
   136         -- it is impossible to forge a signature*)
   145         -- it is impossible to forge a signature*)
   137 
   146 and sg_ref =
       
   147   SgRef of sg ref option;
       
   148 
       
   149 (*make signature*)
       
   150 fun make_sign (id, self, tsig, const_tab, syn, path, spaces, data, stamps) =
       
   151   Sg {id = id, self = self, tsig = tsig, const_tab = const_tab, syn = syn,
       
   152     path = path, spaces = spaces, data = data, stamps = stamps};
       
   153 
       
   154 (*dest signature*)
   138 fun rep_sg (Sg args) = args;
   155 fun rep_sg (Sg args) = args;
   139 val tsig_of = #tsig o rep_sg;
   156 val tsig_of = #tsig o rep_sg;
       
   157 val self_ref = #self o rep_sg;
       
   158 
       
   159 fun get_data (Sg {data, ...}) = Data.get data;
       
   160 fun print_data (Sg {data, ...}) = Data.print data;
       
   161 
       
   162 
       
   163 (*show stamps*)
       
   164 fun stamp_names stamps = rev (map ! stamps);
       
   165 
       
   166 fun pretty_sg (Sg {stamps, ...}) = Pretty.str_list "{" "}" (stamp_names stamps);
       
   167 val pprint_sg = Pretty.pprint o pretty_sg;
       
   168 
       
   169 
       
   170 (* signature id *)
       
   171 
       
   172 fun deref (SgRef (Some (ref sg))) = sg
       
   173   | deref (SgRef None) = sys_error "Sign.deref";
       
   174 
       
   175 fun check_stale (sg as Sg {id, self = SgRef (Some (ref (Sg {id = id', ...}))), ...}) =
       
   176       if id = id' then sg
       
   177       else raise TERM ("Stale signature: " ^ Pretty.str_of (pretty_sg sg), [])
       
   178   | check_stale _ = sys_error "Sign.check_stale";
   140 
   179 
   141 
   180 
   142 (* inclusion and equality *)
   181 (* inclusion and equality *)
   143 
   182 
   144 local
   183 local
   155     | fast_sub (_, []) = false
   194     | fast_sub (_, []) = false
   156     | fast_sub (x :: xs, y :: ys) =
   195     | fast_sub (x :: xs, y :: ys) =
   157         if x = y then fast_sub (xs, ys)
   196         if x = y then fast_sub (xs, ys)
   158         else fast_sub (x :: xs, ys);
   197         else fast_sub (x :: xs, ys);
   159 in
   198 in
   160   fun subsig (Sg {stamps = s1, ...}, Sg {stamps = s2, ...}) =
   199   fun eq_sg (sg1 as Sg {id = id1, ...}, sg2 as Sg {id = id2, ...}) =
   161     s1 = s2 orelse subset_stamp (s1, s2);
   200     (check_stale sg1; check_stale sg2; id1 = id2);
   162 
   201 
   163   fun fast_subsig (Sg {stamps = s1, ...}, Sg {stamps = s2, ...}) =
   202   fun subsig (sg1 as Sg {stamps = s1, ...}, sg2 as Sg {stamps = s2, ...}) =
   164     s1 = s2 orelse fast_sub (s1, s2);
   203     eq_sg (sg1, sg2) orelse subset_stamp (s1, s2);
   165 
   204 
   166   fun eq_sg (Sg {stamps = s1, ...}, Sg {stamps = s2, ...}) =
   205   fun fast_subsig (sg1 as Sg {stamps = s1, ...}, sg2 as Sg {stamps = s2, ...}) =
   167     s1 = s2 orelse (subset_stamp (s1, s2) andalso subset_stamp (s2, s1));
   206     eq_sg (sg1, sg2) orelse fast_sub (s1, s2);
   168 end;
   207 end;
   169 
   208 
   170 
   209 
   171 (*test if same theory names are contained in signatures' stamps,
   210 (*test if same theory names are contained in signatures' stamps,
   172   i.e. if signatures belong to same theory but not necessarily to the
   211   i.e. if signatures belong to same theory but not necessarily to the
   173   same version of it*)
   212   same version of it*)
   174 fun same_sg (Sg {stamps = s1, ...}, Sg {stamps = s2, ...}) =
   213 fun same_sg (sg1 as Sg {stamps = s1, ...}, sg2 as Sg {stamps = s2, ...}) =
   175   eq_set_string (pairself (map (op !)) (s1, s2));
   214   eq_sg (sg1, sg2) orelse eq_set_string (pairself (map (op !)) (s1, s2));
   176 
   215 
   177 (*test for drafts*)
   216 (*test for drafts*)
   178 fun is_draft (Sg {stamps = ref "#" :: _, ...}) = true
   217 fun is_draft (Sg {stamps = ref "#" :: _, ...}) = true
   179   | is_draft _ = false;
   218   | is_draft _ = false;
       
   219 
       
   220 
       
   221 (* build signature *)
       
   222 
       
   223 fun ext_stamps stamps (id as ref name) =
       
   224   let val stmps = (case stamps of ref "#" :: ss => ss | ss => ss) in
       
   225     if exists (equal name o !) stmps then
       
   226       error ("Theory already contains a " ^ quote name ^ " component")
       
   227     else id :: stmps
       
   228   end;
       
   229 
       
   230 fun create_sign self stamps name (syn, tsig, ctab, (path, spaces), data) =
       
   231   let
       
   232     val id = ref name;
       
   233     val sign =
       
   234       make_sign (id, self, tsig, ctab, syn, path, spaces, data, ext_stamps stamps id);
       
   235   in
       
   236     (case self of
       
   237       SgRef (Some r) => r := sign
       
   238     | _ => sys_error "Sign.create_sign");
       
   239     sign
       
   240   end;
       
   241 
       
   242 fun extend_sign extfun name decls
       
   243     (sg as Sg {id = _, self, tsig, const_tab, syn, path, spaces, data, stamps}) =
       
   244   let
       
   245     val _ = check_stale sg;
       
   246     val (self', data') =
       
   247       if is_draft sg then (self, data)
       
   248       else (SgRef (Some (ref sg)), Data.prep_ext data);
       
   249   in
       
   250     create_sign self' stamps name
       
   251       (extfun (syn, tsig, const_tab, (path, spaces), data') decls)
       
   252   end;
   180 
   253 
   181 
   254 
   182 (* consts *)
   255 (* consts *)
   183 
   256 
   184 fun const_type (Sg {const_tab, ...}) c = Symtab.lookup (const_tab, c);
   257 fun const_type (Sg {const_tab, ...}) c = Symtab.lookup (const_tab, c);
   318   fun intern_const sg = intrn (spaces_of sg) constK;
   391   fun intern_const sg = intrn (spaces_of sg) constK;
   319 
   392 
   320   val intern_tycons = intrn_tycons o spaces_of;
   393   val intern_tycons = intrn_tycons o spaces_of;
   321 
   394 
   322   fun full_name (Sg {path, ...}) = full path;
   395   fun full_name (Sg {path, ...}) = full path;
       
   396 
   323 end;
   397 end;
   324 
   398 
   325 
   399 
   326 
   400 
   327 (** pretty printing of terms and types **)
   401 (** pretty printing of terms and types **)
   365 fun pprint_typ sg = Pretty.pprint o Pretty.quote o (pretty_typ sg);
   439 fun pprint_typ sg = Pretty.pprint o Pretty.quote o (pretty_typ sg);
   366 
   440 
   367 
   441 
   368 
   442 
   369 (** print signature **)
   443 (** print signature **)
   370 
       
   371 fun stamp_names stamps = rev (map ! stamps);
       
   372 
   444 
   373 fun print_sg sg =
   445 fun print_sg sg =
   374   let
   446   let
   375     fun prt_cls c = pretty_sort sg [c];
   447     fun prt_cls c = pretty_sort sg [c];
   376     fun prt_sort S = pretty_sort sg S;
   448     fun prt_sort S = pretty_sort sg S;
   403     fun pretty_arities (t, ars) = map (prt_arity t) ars;
   475     fun pretty_arities (t, ars) = map (prt_arity t) ars;
   404 
   476 
   405     fun pretty_const (c, ty) = Pretty.block
   477     fun pretty_const (c, ty) = Pretty.block
   406       [prt_const c, Pretty.str " ::", Pretty.brk 1, prt_typ ty];
   478       [prt_const c, Pretty.str " ::", Pretty.brk 1, prt_typ ty];
   407 
   479 
   408     val Sg {tsig, const_tab, syn = _, path, spaces, data, stamps} = sg;
   480     val Sg {id = _, self = _, tsig, const_tab, syn = _, path, spaces, data, stamps} = sg;
   409     val spaces' = sort (fn ((k1, _), (k2, _)) => k1 < k2) spaces;
   481     val spaces' = sort (fn ((k1, _), (k2, _)) => k1 < k2) spaces;
   410     val {classes, classrel, default, tycons, abbrs, arities} =
   482     val {classes, classrel, default, tycons, abbrs, arities} =
   411       Type.rep_tsig tsig;
   483       Type.rep_tsig tsig;
   412   in
   484   in
   413     Pretty.writeln (Pretty.strs ("stamps:" :: stamp_names stamps));
   485     Pretty.writeln (Pretty.strs ("stamps:" :: stamp_names stamps));
   420     Pretty.writeln (Pretty.big_list "type constructors:" (map pretty_ty tycons));
   492     Pretty.writeln (Pretty.big_list "type constructors:" (map pretty_ty tycons));
   421     Pretty.writeln (Pretty.big_list "type abbreviations:" (map pretty_abbr abbrs));
   493     Pretty.writeln (Pretty.big_list "type abbreviations:" (map pretty_abbr abbrs));
   422     Pretty.writeln (Pretty.big_list "type arities:" (flat (map pretty_arities arities)));
   494     Pretty.writeln (Pretty.big_list "type arities:" (flat (map pretty_arities arities)));
   423     Pretty.writeln (Pretty.big_list "consts:" (map pretty_const (Symtab.dest const_tab)))
   495     Pretty.writeln (Pretty.big_list "consts:" (map pretty_const (Symtab.dest const_tab)))
   424   end;
   496   end;
   425 
       
   426 
       
   427 fun pretty_sg (Sg {stamps, ...}) =
       
   428   Pretty.str_list "{" "}" (stamp_names stamps);
       
   429 
       
   430 val pprint_sg = Pretty.pprint o pretty_sg;
       
   431 
   497 
   432 
   498 
   433 
   499 
   434 (** read types **)  (*exception ERROR*)
   500 (** read types **)  (*exception ERROR*)
   435 
   501 
   596 fun no_read _ _ _ decl = decl;
   662 fun no_read _ _ _ decl = decl;
   597 
   663 
   598 
   664 
   599 (* add default sort *)
   665 (* add default sort *)
   600 
   666 
   601 fun ext_defsort int (syn, tsig, ctab, (path, spaces)) S =
   667 fun ext_defsort int (syn, tsig, ctab, (path, spaces), data) S =
   602   (syn, Type.ext_tsig_defsort tsig (if int then intrn_sort spaces S else S),
   668   (syn, Type.ext_tsig_defsort tsig (if int then intrn_sort spaces S else S),
   603     ctab, (path, spaces));
   669     ctab, (path, spaces), data);
   604 
   670 
   605 
   671 
   606 (* add type constructors *)
   672 (* add type constructors *)
   607 
   673 
   608 fun ext_types (syn, tsig, ctab, (path, spaces)) types =
   674 fun ext_types (syn, tsig, ctab, (path, spaces), data) types =
   609   let val decls = decls_of path Syntax.type_name types in
   675   let val decls = decls_of path Syntax.type_name types in
   610     (Syntax.extend_type_gram syn types,
   676     (Syntax.extend_type_gram syn types,
   611       Type.ext_tsig_types tsig decls, ctab,
   677       Type.ext_tsig_types tsig decls, ctab,
   612       (path, add_names spaces typeK (map fst decls)))
   678       (path, add_names spaces typeK (map fst decls)), data)
   613   end;
   679   end;
   614 
   680 
   615 
   681 
   616 (* add type abbreviations *)
   682 (* add type abbreviations *)
   617 
   683 
   618 fun read_abbr syn tsig spaces (t, vs, rhs_src) =
   684 fun read_abbr syn tsig spaces (t, vs, rhs_src) =
   619   (t, vs, read_raw_typ syn tsig spaces (K None) rhs_src)
   685   (t, vs, read_raw_typ syn tsig spaces (K None) rhs_src)
   620     handle ERROR => error ("in type abbreviation " ^ t);
   686     handle ERROR => error ("in type abbreviation " ^ t);
   621 
   687 
   622 fun ext_abbrs rd_abbr (syn, tsig, ctab, (path, spaces)) abbrs =
   688 fun ext_abbrs rd_abbr (syn, tsig, ctab, (path, spaces), data) abbrs =
   623   let
   689   let
   624     fun mfix_of (t, vs, _, mx) = (t, length vs, mx);
   690     fun mfix_of (t, vs, _, mx) = (t, length vs, mx);
   625     val syn' = Syntax.extend_type_gram syn (map mfix_of abbrs);
   691     val syn' = Syntax.extend_type_gram syn (map mfix_of abbrs);
   626 
   692 
   627     val abbrs' =
   693     val abbrs' =
   628       map (fn (t, vs, rhs, mx) =>
   694       map (fn (t, vs, rhs, mx) =>
   629         (full path (Syntax.type_name t mx), vs, rhs)) abbrs;
   695         (full path (Syntax.type_name t mx), vs, rhs)) abbrs;
   630     val spaces' = add_names spaces typeK (map #1 abbrs');
   696     val spaces' = add_names spaces typeK (map #1 abbrs');
   631     val decls = map (rd_abbr syn' tsig spaces') abbrs';
   697     val decls = map (rd_abbr syn' tsig spaces') abbrs';
   632   in
   698   in
   633     (syn', Type.ext_tsig_abbrs tsig decls, ctab, (path, spaces'))
   699     (syn', Type.ext_tsig_abbrs tsig decls, ctab, (path, spaces'), data)
   634   end;
   700   end;
   635 
   701 
   636 fun ext_tyabbrs abbrs = ext_abbrs read_abbr abbrs;
   702 fun ext_tyabbrs abbrs = ext_abbrs read_abbr abbrs;
   637 fun ext_tyabbrs_i abbrs = ext_abbrs no_read abbrs;
   703 fun ext_tyabbrs_i abbrs = ext_abbrs no_read abbrs;
   638 
   704 
   639 
   705 
   640 (* add type arities *)
   706 (* add type arities *)
   641 
   707 
   642 fun ext_arities int (syn, tsig, ctab, (path, spaces)) arities =
   708 fun ext_arities int (syn, tsig, ctab, (path, spaces), data) arities =
   643   let
   709   let
   644     fun intrn_arity (c, Ss, S) =
   710     fun intrn_arity (c, Ss, S) =
   645       (intrn spaces typeK c, map (intrn_sort spaces) Ss, intrn_sort spaces S);
   711       (intrn spaces typeK c, map (intrn_sort spaces) Ss, intrn_sort spaces S);
   646     val intrn = if int then map intrn_arity else I;
   712     val intrn = if int then map intrn_arity else I;
   647     val tsig' = Type.ext_tsig_arities tsig (intrn arities);
   713     val tsig' = Type.ext_tsig_arities tsig (intrn arities);
   648     val log_types = Type.logical_types tsig';
   714     val log_types = Type.logical_types tsig';
   649   in
   715   in
   650     (Syntax.extend_log_types syn log_types, tsig', ctab, (path, spaces))
   716     (Syntax.extend_log_types syn log_types, tsig', ctab, (path, spaces), data)
   651   end;
   717   end;
   652 
   718 
   653 
   719 
   654 (* add term constants and syntax *)
   720 (* add term constants and syntax *)
   655 
   721 
   665 
   731 
   666 fun read_const syn tsig (path, spaces) (c, ty_src, mx) =
   732 fun read_const syn tsig (path, spaces) (c, ty_src, mx) =
   667   (c, read_raw_typ syn tsig spaces (K None) ty_src, mx)
   733   (c, read_raw_typ syn tsig spaces (K None) ty_src, mx)
   668     handle ERROR => err_in_const (const_name path c mx);
   734     handle ERROR => err_in_const (const_name path c mx);
   669 
   735 
   670 fun ext_cnsts rd_const syn_only prmode (syn, tsig, ctab, (path, spaces)) raw_consts =
   736 fun ext_cnsts rd_const syn_only prmode (syn, tsig, ctab, (path, spaces), data) raw_consts =
   671   let
   737   let
   672     fun prep_const (c, ty, mx) =
   738     fun prep_const (c, ty, mx) =
   673       (c, compress_type (Type.varifyT (Type.cert_typ tsig (Type.no_tvars ty))), mx)
   739       (c, compress_type (Type.varifyT (Type.cert_typ tsig (Type.no_tvars ty))), mx)
   674         handle TYPE (msg, _, _) =>
   740         handle TYPE (msg, _, _) =>
   675           (error_msg msg; err_in_const (const_name path c mx));
   741           (error_msg msg; err_in_const (const_name path c mx));
   680       else decls_of path Syntax.const_name consts;
   746       else decls_of path Syntax.const_name consts;
   681   in
   747   in
   682     (Syntax.extend_const_gram syn prmode consts, tsig,
   748     (Syntax.extend_const_gram syn prmode consts, tsig,
   683       Symtab.extend_new (ctab, decls)
   749       Symtab.extend_new (ctab, decls)
   684         handle Symtab.DUPS cs => err_dup_consts cs,
   750         handle Symtab.DUPS cs => err_dup_consts cs,
   685       (path, add_names spaces constK (map fst decls)))
   751       (path, add_names spaces constK (map fst decls)), data)
   686   end;
   752   end;
   687 
   753 
   688 val ext_consts_i = ext_cnsts no_read false ("", true);
   754 val ext_consts_i = ext_cnsts no_read false ("", true);
   689 val ext_consts = ext_cnsts read_const false ("", true);
   755 val ext_consts = ext_cnsts read_const false ("", true);
   690 val ext_syntax_i = ext_cnsts no_read true ("", true);
   756 val ext_syntax_i = ext_cnsts no_read true ("", true);
   704     if const_of_class c = c_class then c
   770     if const_of_class c = c_class then c
   705     else raise TERM ("class_of_const: bad name " ^ quote c_class, [])
   771     else raise TERM ("class_of_const: bad name " ^ quote c_class, [])
   706   end;
   772   end;
   707 
   773 
   708 
   774 
   709 fun ext_classes int (syn, tsig, ctab, (path, spaces)) classes =
   775 fun ext_classes int (syn, tsig, ctab, (path, spaces), data) classes =
   710   let
   776   let
   711     val names = map fst classes;
   777     val names = map fst classes;
   712     val consts =
   778     val consts =
   713       map (fn c => (const_of_class c, a_itselfT --> propT, NoSyn)) names;
   779       map (fn c => (const_of_class c, a_itselfT --> propT, NoSyn)) names;
   714 
   780 
   718     val classes' =
   784     val classes' =
   719       ListPair.map (fn (c, (_, cs)) => (c, intrn cs)) (full_names, classes);
   785       ListPair.map (fn (c, (_, cs)) => (c, intrn cs)) (full_names, classes);
   720   in
   786   in
   721     ext_consts_i
   787     ext_consts_i
   722       (Syntax.extend_consts syn names,
   788       (Syntax.extend_consts syn names,
   723         Type.ext_tsig_classes tsig classes', ctab, (path, spaces'))
   789         Type.ext_tsig_classes tsig classes', ctab, (path, spaces'), data)
   724     consts
   790     consts
   725   end;
   791   end;
   726 
   792 
   727 
   793 
   728 (* add to classrel *)
   794 (* add to classrel *)
   729 
   795 
   730 fun ext_classrel int (syn, tsig, ctab, (path, spaces)) pairs =
   796 fun ext_classrel int (syn, tsig, ctab, (path, spaces), data) pairs =
   731   let val intrn = if int then map (pairself (intrn_class spaces)) else I in
   797   let val intrn = if int then map (pairself (intrn_class spaces)) else I in
   732     (syn, Type.ext_tsig_classrel tsig (intrn pairs), ctab, (path, spaces))
   798     (syn, Type.ext_tsig_classrel tsig (intrn pairs), ctab, (path, spaces), data)
   733   end;
   799   end;
   734 
   800 
   735 
   801 
   736 (* add to syntax *)
   802 (* add to syntax *)
   737 
   803 
   738 fun ext_syn extfun (syn, tsig, ctab, names) args =
   804 fun ext_syn extfun (syn, tsig, ctab, names, data) args =
   739   (extfun syn args, tsig, ctab, names);
   805   (extfun syn args, tsig, ctab, names, data);
   740 
   806 
   741 
   807 
   742 (* add to path *)
   808 (* add to path *)
   743 
   809 
   744 fun ext_path (syn, tsig, ctab, (path, spaces)) elem =
   810 fun ext_path (syn, tsig, ctab, (path, spaces), data) elem =
   745   let
   811   let
   746     val path' =
   812     val path' =
   747       if elem = ".." andalso not (null path) then fst (split_last path)
   813       if elem = ".." andalso not (null path) then fst (split_last path)
   748       else if elem = "/" then []
   814       else if elem = "/" then []
   749       else path @ NameSpace.unpack elem;
   815       else path @ NameSpace.unpack elem;
   750   in
   816   in
   751     (syn, tsig, ctab, (path', spaces))
   817     (syn, tsig, ctab, (path', spaces), data)
   752   end;      
   818   end;      
   753 
   819 
   754 
   820 
   755 (* add to name space *)
   821 (* add to name space *)
   756 
   822 
   757 fun ext_space (syn, tsig, ctab, (path, spaces)) (kind, names) =
   823 fun ext_space (syn, tsig, ctab, (path, spaces), data) (kind, names) =
   758   (syn, tsig, ctab, (path, add_names spaces kind names));
   824   (syn, tsig, ctab, (path, add_names spaces kind names), data);
   759 
   825 
   760 
   826 
   761 (* build signature *)
   827 (* signature data *)
   762 
   828 
   763 fun ext_stamps stamps name =
   829 fun ext_init_data (syn, tsig, ctab, names, data) (kind, e, ext, mrg, prt) =
   764   let
   830   (syn, tsig, ctab, names, Data.init data kind e ext mrg prt);
   765     val stmps = (case stamps of ref "#" :: ss => ss | ss => ss);
   831 
   766   in
   832 fun ext_put_data (syn, tsig, ctab, names, data) (kind, e) =
   767     if exists (equal name o !) stmps then
   833   (syn, tsig, ctab, names, Data.put data kind e);
   768       error ("Theory already contains a " ^ quote name ^ " component")
       
   769     else ref name :: stmps
       
   770   end;
       
   771 
       
   772 fun make_sign (syn, tsig, ctab, (path, spaces)) data stamps name =
       
   773   Sg {tsig = tsig, const_tab = ctab, syn = syn, path = path, spaces = spaces,
       
   774     data = data, stamps = ext_stamps stamps name};
       
   775 
       
   776 fun extend_sign extfun name decls
       
   777     (Sg {tsig, const_tab, syn, path, spaces, data, stamps}) =
       
   778   make_sign (extfun (syn, tsig, const_tab, (path, spaces)) decls) data stamps name;
       
   779 
   834 
   780 
   835 
   781 (* the external interfaces *)
   836 (* the external interfaces *)
   782 
   837 
   783 val add_classes      = extend_sign (ext_classes true) "#";
   838 val add_classes      = extend_sign (ext_classes true) "#";
   802 val add_tokentrfuns  = extend_sign (ext_syn Syntax.extend_tokentrfuns) "#";
   857 val add_tokentrfuns  = extend_sign (ext_syn Syntax.extend_tokentrfuns) "#";
   803 val add_trrules      = extend_sign (ext_syn Syntax.extend_trrules) "#";
   858 val add_trrules      = extend_sign (ext_syn Syntax.extend_trrules) "#";
   804 val add_trrules_i    = extend_sign (ext_syn Syntax.extend_trrules_i) "#";
   859 val add_trrules_i    = extend_sign (ext_syn Syntax.extend_trrules_i) "#";
   805 val add_path         = extend_sign ext_path "#";
   860 val add_path         = extend_sign ext_path "#";
   806 val add_space        = extend_sign ext_space "#";
   861 val add_space        = extend_sign ext_space "#";
       
   862 val init_data        = extend_sign ext_init_data "#";
       
   863 val put_data         = extend_sign ext_put_data "#";
   807 fun add_name name sg = extend_sign K name () sg;
   864 fun add_name name sg = extend_sign K name () sg;
   808 
   865 
   809 val make_draft = add_name "#";
   866 val make_draft = add_name "#";
   810 
   867 
   811 
   868 
   812 (* additional signature data *)
       
   813 
       
   814 fun map_data f (Sg {tsig, const_tab, syn, path, spaces, data, stamps}) =
       
   815   make_sign (syn, tsig, const_tab, (path, spaces)) (f data) stamps "#";
       
   816 
       
   817 fun init_data (kind, e, ext, mrg, prt) =
       
   818   map_data (fn d => Data.init d kind e ext mrg prt);
       
   819 
       
   820 fun get_data (Sg {data, ...}) = Data.get data;
       
   821 fun put_data (kind, e) = map_data (fn d => Data.put d kind e);
       
   822 fun print_data (Sg {data, ...}) kind = Data.print data kind;
       
   823 
       
   824 (*prepare extension*)
       
   825 fun prep_ext sg =
       
   826   map_data Data.prep_ext sg |> add_path "/";
       
   827 
       
   828 
       
   829 
   869 
   830 (** merge signatures **)    (*exception TERM*)
   870 (** merge signatures **)    (*exception TERM*)
   831 
   871 
   832 fun merge_aux triv_only (sg1, sg2) =
   872 (* merge of sg_refs -- trivial only *)
   833   if fast_subsig (sg2, sg1) then sg1
   873 
   834   else if fast_subsig (sg1, sg2) then sg2
   874 fun merge_refs (sgr1 as SgRef (Some (ref sg1)),
   835   else if subsig (sg2, sg1) then sg1
   875         sgr2 as SgRef (Some (ref sg2))) =
       
   876       if fast_subsig (sg2, sg1) then sgr1
       
   877       else if fast_subsig (sg1, sg2) then sgr2
       
   878       else if subsig (sg2, sg1) then sgr1
       
   879       else if subsig (sg1, sg2) then sgr2
       
   880       else raise TERM ("Attempt to do non-trivial merge of signatures", [])
       
   881   | merge_refs _ = sys_error "Sign.merge_refs";
       
   882 
       
   883 
       
   884 
       
   885 (* proper merge *)
       
   886 
       
   887 fun merge_aux (sg1, sg2) =
       
   888   if subsig (sg2, sg1) then sg1
   836   else if subsig (sg1, sg2) then sg2
   889   else if subsig (sg1, sg2) then sg2
   837   else if is_draft sg1 orelse is_draft sg2 then
   890   else if is_draft sg1 orelse is_draft sg2 then
   838     raise TERM ("Illegal merge of draft signatures", [])
   891     raise TERM ("Attempt to merge draft signatures", [])
   839   else if triv_only then
       
   840     raise TERM ("Illegal non-trivial merge of signatures", [])
       
   841   else
   892   else
   842     (*neither is union already; must form union*)
   893     (*neither is union already; must form union*)
   843     let
   894     let
   844       val Sg {tsig = tsig1, const_tab = const_tab1, syn = syn1,
   895       val Sg {id = _, self = _, tsig = tsig1, const_tab = const_tab1, syn = syn1,
   845         path = _, spaces = spaces1, data = data1, stamps = stamps1} = sg1;
   896         path = _, spaces = spaces1, data = data1, stamps = stamps1} = sg1;
   846       val Sg {tsig = tsig2, const_tab = const_tab2, syn = syn2,
   897       val Sg {id = _, self = _, tsig = tsig2, const_tab = const_tab2, syn = syn2,
   847         path = _, spaces = spaces2, data = data2, stamps = stamps2} = sg2;
   898         path = _, spaces = spaces2, data = data2, stamps = stamps2} = sg2;
   848 
   899 
   849 
   900 
       
   901       val id = ref "";
       
   902       val self_ref = ref sg1;			(*dummy value*)
       
   903       val self = SgRef (Some self_ref);
   850       val stamps = merge_rev_lists stamps1 stamps2;
   904       val stamps = merge_rev_lists stamps1 stamps2;
   851       val _ =
   905       val _ =
   852         (case duplicates (stamp_names stamps) of
   906         (case duplicates (stamp_names stamps) of
   853           [] => ()
   907           [] => ()
   854         | dups => raise TERM ("Attempt to merge different versions of theories "
   908         | dups => raise TERM ("Attempt to merge different versions of theories "
   855             ^ commas_quote dups, []));
   909             ^ commas_quote dups, []));
   856 
   910 
   857       val tsig = Type.merge_tsigs (tsig1, tsig2);
   911       val tsig = Type.merge_tsigs (tsig1, tsig2);
   858 
       
   859       val const_tab = Symtab.merge (op =) (const_tab1, const_tab2)
   912       val const_tab = Symtab.merge (op =) (const_tab1, const_tab2)
   860         handle Symtab.DUPS cs =>
   913         handle Symtab.DUPS cs =>
   861           raise TERM ("Incompatible types for constant(s) " ^ commas_quote cs, []);
   914           raise TERM ("Incompatible types for constant(s) " ^ commas_quote cs, []);
   862 
       
   863       val syn = Syntax.merge_syntaxes syn1 syn2;
   915       val syn = Syntax.merge_syntaxes syn1 syn2;
   864 
   916 
       
   917       val path = [];
   865       val kinds = distinct (map fst (spaces1 @ spaces2));
   918       val kinds = distinct (map fst (spaces1 @ spaces2));
   866       val spaces =
   919       val spaces =
   867         kinds ~~
   920         kinds ~~
   868           ListPair.map NameSpace.merge
   921           ListPair.map NameSpace.merge
   869             (map (space_of spaces1) kinds, map (space_of spaces2) kinds);
   922             (map (space_of spaces1) kinds, map (space_of spaces2) kinds);
   870 
   923 
   871       val data = Data.merge (data1, data2);
   924       val data = Data.merge (data1, data2);
       
   925 
       
   926       val sign = make_sign (id, self, tsig, const_tab, syn, path, spaces, data, stamps);
   872     in
   927     in
   873       Sg {tsig = tsig, const_tab = const_tab, syn = syn,
   928       self_ref := sign; sign
   874         path = [], spaces = spaces, data = data, stamps = stamps}
       
   875     end;
   929     end;
   876 
   930 
   877 fun gen_merge triv sgs =
   931 fun merge sg1_sg2 =
   878   (case handle_error (merge_aux triv) sgs of
   932   (case handle_error merge_aux sg1_sg2 of
   879     OK sg => sg
   933     OK sg => sg
   880   | Error msg => raise TERM (msg, []));
   934   | Error msg => raise TERM (msg, []));
   881 
   935 
   882 val merge = gen_merge true;
       
   883 val nontriv_merge = gen_merge false;
       
   884 
       
   885 
   936 
   886 
   937 
   887 (** the Pure signature **)
   938 (** the Pure signature **)
   888 
   939 
       
   940 val dummy_sg = make_sign (ref "", SgRef None, Type.tsig0,
       
   941   Symtab.null, Syntax.pure_syn, [], [], Data.empty, []);
       
   942 
   889 val proto_pure =
   943 val proto_pure =
   890   make_sign (Syntax.pure_syn, Type.tsig0, Symtab.null, ([], [])) Data.empty [] "#"
   944   create_sign (SgRef (Some (ref dummy_sg))) [] "#"
       
   945     (Syntax.pure_syn, Type.tsig0, Symtab.null, ([], []), Data.empty)
   891   |> add_types
   946   |> add_types
   892    (("fun", 2, NoSyn) ::
   947    (("fun", 2, NoSyn) ::
   893     ("prop", 0, NoSyn) ::
   948     ("prop", 0, NoSyn) ::
   894     ("itself", 1, NoSyn) ::
   949     ("itself", 1, NoSyn) ::
   895     Syntax.pure_types)
   950     Syntax.pure_types)