src/Pure/sign.ML
changeset 14784 e65d77313a94
parent 14700 2f885b7e5ba7
child 14828 15d12761ba54
equal deleted inserted replaced
14783:e7f7ed4c06f2 14784:e65d77313a94
    21   type sg
    21   type sg
    22   type sg_ref
    22   type sg_ref
    23   type data
    23   type data
    24   val rep_sg: sg ->
    24   val rep_sg: sg ->
    25    {self: sg_ref,
    25    {self: sg_ref,
    26     tsig: Type.type_sig,
    26     tsig: Type.tsig,
    27     const_tab: typ Symtab.table,
    27     consts: (typ * stamp) Symtab.table,
    28     path: string list option,
    28     path: string list option,
    29     spaces: (string * NameSpace.T) list,
    29     spaces: (string * NameSpace.T) list,
    30     data: data}
    30     data: data}
    31   val name_of: sg -> string
    31   val name_of: sg -> string
    32   val stamp_names_of: sg -> string list
    32   val stamp_names_of: sg -> string list
    33   val exists_stamp: string -> sg -> bool
    33   val exists_stamp: string -> sg -> bool
    34   val tsig_of: sg -> Type.type_sig
    34   val tsig_of: sg -> Type.tsig
    35   val deref: sg_ref -> sg
    35   val deref: sg_ref -> sg
    36   val self_ref: sg -> sg_ref
    36   val self_ref: sg -> sg_ref
    37   val subsig: sg * sg -> bool
    37   val subsig: sg * sg -> bool
    38   val joinable: sg * sg -> bool
    38   val joinable: sg * sg -> bool
    39   val eq_sg: sg * sg -> bool
    39   val eq_sg: sg * sg -> bool
    44   val const_type: sg -> string -> typ option
    44   val const_type: sg -> string -> typ option
    45   val classes: sg -> class list
    45   val classes: sg -> class list
    46   val defaultS: sg -> sort
    46   val defaultS: sg -> sort
    47   val subsort: sg -> sort * sort -> bool
    47   val subsort: sg -> sort * sort -> bool
    48   val nodup_vars: term -> term
    48   val nodup_vars: term -> term
    49   val norm_sort: sg -> sort -> sort
       
    50   val of_sort: sg -> typ * sort -> bool
    49   val of_sort: sg -> typ * sort -> bool
    51   val witness_sorts: sg -> sort list -> sort list -> (typ * sort) list
    50   val witness_sorts: sg -> sort list -> sort list -> (typ * sort) list
    52   val univ_witness: sg -> (typ * sort) option
    51   val universal_witness: sg -> (typ * sort) option
    53   val typ_instance: sg -> typ * typ -> bool
    52   val typ_instance: sg -> typ * typ -> bool
    54   val classK: string
    53   val classK: string
    55   val typeK: string
    54   val typeK: string
    56   val constK: string
    55   val constK: string
    57   val full_name: sg -> bstring -> string
    56   val full_name: sg -> bstring -> string
    90   val pprint_term: sg -> term -> pprint_args -> unit
    89   val pprint_term: sg -> term -> pprint_args -> unit
    91   val pprint_typ: sg -> typ -> pprint_args -> unit
    90   val pprint_typ: sg -> typ -> pprint_args -> unit
    92   val certify_class: sg -> class -> class
    91   val certify_class: sg -> class -> class
    93   val certify_sort: sg -> sort -> sort
    92   val certify_sort: sg -> sort -> sort
    94   val certify_typ: sg -> typ -> typ
    93   val certify_typ: sg -> typ -> typ
    95   val certify_typ_no_norm: sg -> typ -> typ
    94   val certify_typ_raw: sg -> typ -> typ
    96   val certify_tycon: sg -> string -> string
       
    97   val certify_tyabbr: sg -> string -> string
       
    98   val certify_tyname: sg -> string -> string
    95   val certify_tyname: sg -> string -> string
    99   val certify_const: sg -> string -> string
    96   val certify_const: sg -> string -> string
   100   val certify_term: sg -> term -> term * typ * int
    97   val certify_term: sg -> term -> term * typ * int
   101   val read_sort: sg -> string -> sort
    98   val read_sort: sg -> string -> sort
   102   val read_raw_typ: sg * (indexname -> sort option) -> string -> typ
    99   val read_raw_typ: sg * (indexname -> sort option) -> string -> typ
   103   val read_typ: sg * (indexname -> sort option) -> string -> typ
   100   val read_typ: sg * (indexname -> sort option) -> string -> typ
   104   val read_typ': Syntax.syntax -> sg * (indexname -> sort option) -> string -> typ
   101   val read_typ': Syntax.syntax -> sg * (indexname -> sort option) -> string -> typ
   105   val read_typ_no_norm': Syntax.syntax -> sg * (indexname -> sort option) -> string -> typ
   102   val read_typ_raw': Syntax.syntax -> sg * (indexname -> sort option) -> string -> typ
   106   val infer_types: sg -> (indexname -> typ option) ->
   103   val infer_types: sg -> (indexname -> typ option) ->
   107     (indexname -> sort option) -> string list -> bool
   104     (indexname -> sort option) -> string list -> bool
   108     -> xterm list * typ -> term * (indexname * typ) list
   105     -> xterm list * typ -> term * (indexname * typ) list
   109   val infer_types_simult: sg -> (indexname -> typ option) ->
   106   val infer_types_simult: sg -> (indexname -> typ option) ->
   110     (indexname -> sort option) -> string list -> bool
   107     (indexname -> sort option) -> string list -> bool
   188   Sg of
   185   Sg of
   189    {id: string ref,                             (*id*)
   186    {id: string ref,                             (*id*)
   190     stamps: string ref list,                    (*unique theory indentifier*)
   187     stamps: string ref list,                    (*unique theory indentifier*)
   191     syn: syn} *                                 (*syntax for parsing and printing*)
   188     syn: syn} *                                 (*syntax for parsing and printing*)
   192    {self: sg_ref,                               (*mutable self reference*)
   189    {self: sg_ref,                               (*mutable self reference*)
   193     tsig: Type.type_sig,                        (*order-sorted signature of types*)
   190     tsig: Type.tsig,                            (*order-sorted signature of types*)
   194     const_tab: typ Symtab.table,                (*type schemes of constants*)
   191     consts: (typ * stamp) Symtab.table,         (*type schemes of constants*)
   195     path: string list option,                   (*current name space entry prefix*)
   192     path: string list option,                   (*current name space entry prefix*)
   196     spaces: (string * NameSpace.T) list,        (*name spaces for consts, types etc.*)
   193     spaces: (string * NameSpace.T) list,        (*name spaces for consts, types etc.*)
   197     data: data}                                 (*anytype data*)
   194     data: data}                                 (*anytype data*)
   198 and data =
   195 and data =
   199   Data of
   196   Data of
   213       ((sg -> ast list -> ast) * stamp) list Symtab.table)
   210       ((sg -> ast list -> ast) * stamp) list Symtab.table)
   214 and sg_ref =
   211 and sg_ref =
   215   SgRef of sg ref option;
   212   SgRef of sg ref option;
   216 
   213 
   217 (*make signature*)
   214 (*make signature*)
   218 fun make_sign (id, self, tsig, const_tab, syn, path, spaces, data, stamps) =
   215 fun make_sign (id, self, tsig, consts, syn, path, spaces, data, stamps) =
   219   Sg ({id = id, stamps = stamps, syn = syn}, {self = self, tsig = tsig,
   216   Sg ({id = id, stamps = stamps, syn = syn}, {self = self, tsig = tsig,
   220     const_tab = const_tab, path = path, spaces = spaces, data = data});
   217     consts = consts, path = path, spaces = spaces, data = data});
   221 
   218 
   222 
   219 
   223 (* basic operations *)
   220 (* basic operations *)
   224 
   221 
   225 fun rep_sg (Sg (_, args)) = args;
   222 fun rep_sg (Sg (_, args)) = args;
   229 fun pretty_sg sg = Pretty.str_list "{" "}" (stamp_names_of sg);
   226 fun pretty_sg sg = Pretty.str_list "{" "}" (stamp_names_of sg);
   230 val str_of_sg = Pretty.str_of o pretty_sg;
   227 val str_of_sg = Pretty.str_of o pretty_sg;
   231 val pprint_sg = Pretty.pprint o pretty_sg;
   228 val pprint_sg = Pretty.pprint o pretty_sg;
   232 
   229 
   233 val tsig_of = #tsig o rep_sg;
   230 val tsig_of = #tsig o rep_sg;
   234 fun const_type (Sg (_, {const_tab, ...})) c = Symtab.lookup (const_tab, c);
   231 fun const_type (Sg (_, {consts, ...})) c = apsome #1 (Symtab.lookup (consts, c));
   235 
   232 
   236 
   233 
   237 (* id and self *)
   234 (* id and self *)
   238 
   235 
   239 fun check_stale (sg as Sg ({id, ...},
   236 fun check_stale (sg as Sg ({id, ...},
   332 (* classes and sorts *)
   329 (* classes and sorts *)
   333 
   330 
   334 val classes = Type.classes o tsig_of;
   331 val classes = Type.classes o tsig_of;
   335 val defaultS = Type.defaultS o tsig_of;
   332 val defaultS = Type.defaultS o tsig_of;
   336 val subsort = Type.subsort o tsig_of;
   333 val subsort = Type.subsort o tsig_of;
   337 val norm_sort = Type.norm_sort o tsig_of;
       
   338 val of_sort = Type.of_sort o tsig_of;
   334 val of_sort = Type.of_sort o tsig_of;
   339 val witness_sorts = Type.witness_sorts o tsig_of;
   335 val witness_sorts = Type.witness_sorts o tsig_of;
   340 val univ_witness = Type.univ_witness o tsig_of;
   336 val universal_witness = Type.universal_witness o tsig_of;
   341 fun typ_instance sg (T, U) = Type.typ_instance (tsig_of sg, T, U);
   337 val typ_instance = Type.typ_instance o tsig_of;
   342 
   338 
   343 
   339 
   344 (** signature data **)
   340 (** signature data **)
   345 
   341 
   346 (* errors *)
   342 (* errors *)
   355 
   351 
   356 fun err_dup_init sg kind =
   352 fun err_dup_init sg kind =
   357   error ("Duplicate initialization of " ^ quote kind ^ " data" ^ of_theory sg);
   353   error ("Duplicate initialization of " ^ quote kind ^ " data" ^ of_theory sg);
   358 
   354 
   359 fun err_uninit sg kind =
   355 fun err_uninit sg kind =
   360   error ("Tried to access uninitialized " ^ quote kind ^ " data" ^
   356   error ("Tried to access uninitialized " ^ quote kind ^ " data" ^ of_theory sg);
   361          of_theory sg);
       
   362 
   357 
   363 (*Trying to access theory data using get / put operations from a different
   358 (*Trying to access theory data using get / put operations from a different
   364   instance of the TheoryDataFun result.  Typical cure: re-load all files*)
   359   instance of the TheoryDataFun result.  Typical cure: re-load all files*)
   365 fun err_access sg kind =
   360 fun err_access sg kind =
   366   error ("Unauthorized access to " ^ quote kind ^ " data" ^ of_theory sg);
   361   error ("Unauthorized access to " ^ quote kind ^ " data" ^ of_theory sg);
   452     | _ => sys_error "Sign.create_sign");
   447     | _ => sys_error "Sign.create_sign");
   453     sign
   448     sign
   454   end;
   449   end;
   455 
   450 
   456 fun extend_sign keep extfun name decls
   451 fun extend_sign keep extfun name decls
   457     (sg as Sg ({id = _, stamps, syn}, {self, tsig, const_tab, path, spaces, data})) =
   452     (sg as Sg ({id = _, stamps, syn}, {self, tsig, consts, path, spaces, data})) =
   458   let
   453   let
   459     val _ = check_stale sg;
   454     val _ = check_stale sg;
   460     val (self', data') =
   455     val (self', data') =
   461       if is_draft sg andalso keep then (self, data)
   456       if is_draft sg andalso keep then (self, data)
   462       else (SgRef (Some (ref sg)), prep_ext_data data);
   457       else (SgRef (Some (ref sg)), prep_ext_data data);
   463   in
   458   in
   464     create_sign self' stamps name
   459     create_sign self' stamps name
   465       (extfun sg (syn, tsig, const_tab, (path, spaces), data') decls)
   460       (extfun sg (syn, tsig, consts, (path, spaces), data') decls)
   466   end;
   461   end;
   467 
   462 
   468 
   463 
   469 
   464 
   470 (** name spaces **)
   465 (** name spaces **)
   492 fun add_names x = change_space NameSpace.extend x;
   487 fun add_names x = change_space NameSpace.extend x;
   493 fun hide_names b x = change_space (NameSpace.hide b) x;
   488 fun hide_names b x = change_space (NameSpace.hide b) x;
   494 
   489 
   495 (*make full names*)
   490 (*make full names*)
   496 fun full _ "" = error "Attempt to declare empty name \"\""
   491 fun full _ "" = error "Attempt to declare empty name \"\""
   497   | full None name = name
   492   | full None bname = bname
   498   | full (Some path) name =
   493   | full (Some path) bname =
   499       if NameSpace.is_qualified name then
   494       if NameSpace.is_qualified bname then
   500         error ("Attempt to declare qualified name " ^ quote name)
   495         error ("Attempt to declare qualified name " ^ quote bname)
   501       else
   496       else
   502        (if not (Syntax.is_identifier name)
   497         let val name = NameSpace.pack (path @ [bname]) in
   503         then warning ("Declared non-identifier name " ^ quote name) else ();
   498           if Syntax.is_identifier bname then ()
   504         NameSpace.pack (path @ [name]));
   499           else warning ("Declared non-identifier " ^ quote name); name
       
   500         end;
   505 
   501 
   506 (*base name*)
   502 (*base name*)
   507 val base_name = NameSpace.base;
   503 val base_name = NameSpace.base;
   508 
   504 
   509 
   505 
   577 (** partial Pure signature **)
   573 (** partial Pure signature **)
   578 
   574 
   579 val pure_syn =
   575 val pure_syn =
   580   Syn (Syntax.pure_syn, (Symtab.empty, Symtab.empty, Symtab.empty, Symtab.empty));
   576   Syn (Syntax.pure_syn, (Symtab.empty, Symtab.empty, Symtab.empty, Symtab.empty));
   581 
   577 
   582 val dummy_sg = make_sign (ref "", SgRef None, Type.tsig0,
   578 val dummy_sg = make_sign (ref "", SgRef None, Type.empty_tsig,
   583   Symtab.empty, pure_syn, Some [], [], empty_data, []);
   579   Symtab.empty, pure_syn, Some [], [], empty_data, []);
   584 
   580 
   585 val pre_pure =
   581 val pre_pure =
   586   create_sign (SgRef (Some (ref dummy_sg))) [] "#"
   582   create_sign (SgRef (Some (ref dummy_sg))) [] "#"
   587     (pure_syn, Type.tsig0, Symtab.empty, (Some [], []), empty_data);
   583     (pure_syn, Type.empty_tsig, Symtab.empty, (Some [], []), empty_data);
   588 
   584 
   589 val PureN = "Pure";
   585 val PureN = "Pure";
   590 val CPureN = "CPure";
   586 val CPureN = "CPure";
   591 
   587 
   592 
   588 
   656 
   652 
   657 fun err_in_type s =
   653 fun err_in_type s =
   658   error ("The error(s) above occurred in type " ^ quote s);
   654   error ("The error(s) above occurred in type " ^ quote s);
   659 
   655 
   660 fun rd_raw_typ syn tsig spaces def_sort str =
   656 fun rd_raw_typ syn tsig spaces def_sort str =
   661   intrn_tycons spaces
   657   intrn_tycons spaces (Syntax.read_typ syn
   662     (Syntax.read_typ syn (Type.get_sort tsig def_sort (intrn_sort spaces)) (intrn_sort spaces) str
   658     (TypeInfer.get_sort tsig def_sort (intrn_sort spaces)) (intrn_sort spaces) str
   663       handle ERROR => err_in_type str);
   659       handle ERROR => err_in_type str);
   664 
   660 
   665 fun read_raw_typ' syn (sg as Sg (_, {tsig, spaces, ...}), def_sort) str =
   661 fun read_raw_typ' syn (sg as Sg (_, {tsig, spaces, ...}), def_sort) str =
   666   (check_stale sg; rd_raw_typ syn tsig spaces def_sort str);
   662   (check_stale sg; rd_raw_typ syn tsig spaces def_sort str);
   667 
   663 
   671 local
   667 local
   672   fun read_typ_aux rd cert (sg, def_sort) str =
   668   fun read_typ_aux rd cert (sg, def_sort) str =
   673     (cert (tsig_of sg) (rd (sg, def_sort) str)
   669     (cert (tsig_of sg) (rd (sg, def_sort) str)
   674       handle TYPE (msg, _, _) => (error_msg msg; err_in_type str));
   670       handle TYPE (msg, _, _) => (error_msg msg; err_in_type str));
   675 in
   671 in
   676   val read_typ              = read_typ_aux read_raw_typ Type.cert_typ;
   672   val read_typ          = read_typ_aux read_raw_typ Type.cert_typ;
   677   val read_typ_no_norm      = read_typ_aux read_raw_typ Type.cert_typ_no_norm;
   673   val read_typ_raw      = read_typ_aux read_raw_typ Type.cert_typ_raw;
   678   fun read_typ' syn         = read_typ_aux (read_raw_typ' syn) Type.cert_typ;
   674   fun read_typ' syn     = read_typ_aux (read_raw_typ' syn) Type.cert_typ;
   679   fun read_typ_no_norm' syn = read_typ_aux (read_raw_typ' syn) Type.cert_typ_no_norm;
   675   fun read_typ_raw' syn = read_typ_aux (read_raw_typ' syn) Type.cert_typ_raw;
   680 end;
   676 end;
   681 
   677 
   682 
   678 
   683 
   679 
   684 (** certify classes, sorts, types and terms **)   (*exception TYPE*)
   680 (** certify classes, sorts, types and terms **)   (*exception TYPE*)
   685 
   681 
   686 val certify_class = Type.cert_class o tsig_of;
   682 val certify_class = Type.cert_class o tsig_of;
   687 val certify_sort = Type.cert_sort o tsig_of;
   683 val certify_sort = Type.cert_sort o tsig_of;
   688 val certify_typ = Type.cert_typ o tsig_of;
   684 val certify_typ = Type.cert_typ o tsig_of;
   689 val certify_typ_no_norm = Type.cert_typ_no_norm o tsig_of;
   685 val certify_typ_raw = Type.cert_typ_raw o tsig_of;
   690 
       
   691 fun certify_tycon sg c =
       
   692   if is_some (Symtab.lookup (#tycons (Type.rep_tsig (tsig_of sg)), c)) then c
       
   693   else raise TYPE ("Undeclared type constructor " ^ quote c, [], []);
       
   694 
       
   695 fun certify_tyabbr sg c =
       
   696   if is_some (Symtab.lookup (#abbrs (Type.rep_tsig (tsig_of sg)), c)) then c
       
   697   else raise TYPE ("Unknown type abbreviation " ^ quote c, [], []);
       
   698 
   686 
   699 fun certify_tyname sg c =
   687 fun certify_tyname sg c =
   700   certify_tycon sg c handle TYPE _ => certify_tyabbr sg c handle TYPE _ =>
   688   if is_some (Symtab.lookup (#types (Type.rep_tsig (tsig_of sg)), c)) then c
   701     raise TYPE ("Unknown type name " ^ quote c, [], []);
   689   else raise TYPE ("Undeclared type constructor: " ^ quote c, [], []);
   702 
   690 
   703 fun certify_const sg c =
   691 fun certify_const sg c =
   704   if is_some (const_type sg c) then c else raise TYPE ("Undeclared constant " ^ quote c, [], []);
   692   if is_some (const_type sg c) then c
       
   693   else raise TYPE ("Undeclared constant: " ^ quote c, [], []);
   705 
   694 
   706 
   695 
   707 (* certify_term *)
   696 (* certify_term *)
   708 
   697 
   709 (*check for duplicate occurrences of TFree/TVar with distinct sorts*)
   698 (*check for duplicate occurrences of TFree/TVar with distinct sorts*)
   779             | _ => err_appl "Operator not of function type." bs t T u U)
   768             | _ => err_appl "Operator not of function type." bs t T u U)
   780           end;
   769           end;
   781 
   770 
   782   in typ_of ([], tm) end;
   771   in typ_of ([], tm) end;
   783 
   772 
   784 
       
   785 fun certify_term sg tm =
   773 fun certify_term sg tm =
   786   let
   774   let
   787     val _ = check_stale sg;
   775     val _ = check_stale sg;
   788     val tsig = tsig_of sg;
   776 
       
   777     val tm' = Term.map_term_types (Type.cert_typ (tsig_of sg)) tm;
       
   778     val tm' = if tm = tm' then tm else tm';  (*avoid copying of already normal term*)
       
   779 
       
   780     fun err msg = raise TYPE (msg, [], [tm']);
   789 
   781 
   790     fun show_const a T = quote a ^ " :: " ^ quote (string_of_typ sg T);
   782     fun show_const a T = quote a ^ " :: " ^ quote (string_of_typ sg T);
   791 
   783 
   792     fun atom_err (errs, Const (a, T)) =
   784     fun check_atoms (t $ u) = (check_atoms t; check_atoms u)
   793         (case const_type sg a of
   785       | check_atoms (Abs (_, _, t)) = check_atoms t
   794           None => ("Undeclared constant " ^ show_const a T) :: errs
   786       | check_atoms (Const (a, T)) =
   795         | Some U =>
   787           (case const_type sg a of
   796             if typ_instance sg (T, U) then errs
   788             None => err ("Undeclared constant " ^ show_const a T)
   797             else ("Illegal type for constant " ^ show_const a T) :: errs)
   789           | Some U =>
   798       | atom_err (errs, Var ((x, i), _)) =
   790               if typ_instance sg (T, U) then ()
   799           if i < 0 then ("Negative index for Var " ^ quote x) :: errs else errs
   791               else err ("Illegal type for constant " ^ show_const a T))
   800       | atom_err (errs, _) = errs;
   792       | check_atoms (Var ((x, i), _)) =
   801 
   793           if i < 0 then err ("Malformed variable: " ^ quote x) else ()
   802     val norm_tm =
   794       | check_atoms _ = ();
   803       (case it_term_types (Type.typ_errors tsig) (tm, []) of
       
   804         [] => Type.norm_term tsig tm
       
   805       | errs => raise TYPE (cat_lines errs, [], [tm]));
       
   806     val _ = nodup_vars norm_tm;
       
   807   in
   795   in
   808     (case foldl_aterms atom_err ([], norm_tm) of
   796     check_atoms tm';
   809       [] => (norm_tm, type_check sg norm_tm, maxidx_of_term norm_tm)
   797     nodup_vars tm';
   810     | errs => raise TYPE (cat_lines errs, [], [norm_tm]))
   798     (tm', type_check sg tm', maxidx_of_term tm')
   811   end;
   799   end;
       
   800 
   812 
   801 
   813 
   802 
   814 (** infer_types **)         (*exception ERROR*)
   803 (** infer_types **)         (*exception ERROR*)
   815 
   804 
   816 (*
   805 (*
   832     val termss = foldr multiply (map fst args, [[]]);
   821     val termss = foldr multiply (map fst args, [[]]);
   833     val typs =
   822     val typs =
   834       map (fn (_, T) => certify_typ sg T handle TYPE (msg, _, _) => error msg) args;
   823       map (fn (_, T) => certify_typ sg T handle TYPE (msg, _, _) => error msg) args;
   835 
   824 
   836     fun infer ts = OK
   825     fun infer ts = OK
   837       (Type.infer_types prt prT tsig (const_type sg) def_type def_sort
   826       (TypeInfer.infer_types prt prT tsig (const_type sg) def_type def_sort
   838         (intern_const sg) (intern_tycons sg) (intern_sort sg) used freeze typs ts)
   827         (intern_const sg) (intern_tycons sg) (intern_sort sg) used freeze typs ts)
   839       handle TYPE (msg, _, _) => Error msg;
   828       handle TYPE (msg, _, _) => Error msg;
   840 
   829 
   841     val err_results = map infer termss;
   830     val err_results = map infer termss;
   842     val errs = mapfilter get_error err_results;
   831     val errs = mapfilter get_error err_results;
   897 
   886 
   898 
   887 
   899 (* add default sort *)
   888 (* add default sort *)
   900 
   889 
   901 fun ext_defS prep_sort sg (syn, tsig, ctab, (path, spaces), data) S =
   890 fun ext_defS prep_sort sg (syn, tsig, ctab, (path, spaces), data) S =
   902   (syn, Type.ext_tsig_defsort tsig (prep_sort sg syn tsig spaces S),
   891   (syn, Type.set_defsort (prep_sort sg syn tsig spaces S) tsig,
   903     ctab, (path, spaces), data);
   892     ctab, (path, spaces), data);
   904 
   893 
   905 fun ext_defsort arg = ext_defS rd_sort arg;
   894 fun ext_defsort arg = ext_defS rd_sort arg;
   906 fun ext_defsort_i arg = ext_defS cert_sort arg;
   895 fun ext_defsort_i arg = ext_defS cert_sort arg;
   907 
   896 
   909 (* add type constructors *)
   898 (* add type constructors *)
   910 
   899 
   911 fun ext_types _ (syn, tsig, ctab, (path, spaces), data) types =
   900 fun ext_types _ (syn, tsig, ctab, (path, spaces), data) types =
   912   let val decls = decls_of path Syntax.type_name types in
   901   let val decls = decls_of path Syntax.type_name types in
   913     (map_syntax (Syntax.extend_type_gram types) syn,
   902     (map_syntax (Syntax.extend_type_gram types) syn,
   914       Type.ext_tsig_types tsig decls, ctab,
   903       Type.add_types decls tsig, ctab,
   915       (path, add_names spaces typeK (map fst decls)), data)
   904       (path, add_names spaces typeK (map fst decls)), data)
   916   end;
   905   end;
   917 
       
   918 fun ext_nonterminals sign sg nonterms =
       
   919   ext_types sign sg (map (fn n => (n, 0, Syntax.NoSyn)) nonterms);
       
   920 
   906 
   921 
   907 
   922 (* add type abbreviations *)
   908 (* add type abbreviations *)
   923 
   909 
   924 fun read_abbr sg syn tsig spaces (t, vs, rhs_src) =
   910 fun read_abbr sg syn tsig spaces (t, vs, rhs_src) =
   934       map (fn (t, vs, rhs, mx) =>
   920       map (fn (t, vs, rhs, mx) =>
   935         (full path (Syntax.type_name t mx), vs, rhs)) abbrs;
   921         (full path (Syntax.type_name t mx), vs, rhs)) abbrs;
   936     val spaces' = add_names spaces typeK (map #1 abbrs');
   922     val spaces' = add_names spaces typeK (map #1 abbrs');
   937     val decls = map (rd_abbr sg syn' tsig spaces') abbrs';
   923     val decls = map (rd_abbr sg syn' tsig spaces') abbrs';
   938   in
   924   in
   939     (syn', Type.ext_tsig_abbrs tsig decls, ctab, (path, spaces'), data)
   925     (syn', Type.add_abbrs decls tsig, ctab, (path, spaces'), data)
   940   end;
   926   end;
   941 
   927 
   942 fun ext_tyabbrs abbrs = ext_abbrs read_abbr abbrs;
   928 fun ext_tyabbrs abbrs = ext_abbrs read_abbr abbrs;
   943 fun ext_tyabbrs_i abbrs = ext_abbrs no_read abbrs;
   929 fun ext_tyabbrs_i abbrs = ext_abbrs no_read abbrs;
       
   930 
       
   931 
       
   932 (* add nonterminals *)
       
   933 
       
   934 fun ext_nonterminals _ (syn, tsig, ctab, (path, spaces), data) bnames =
       
   935   let val names = map (full path) bnames in
       
   936     (map_syntax (Syntax.extend_consts names) syn,
       
   937       Type.add_nonterminals names tsig, ctab,
       
   938       (path, add_names spaces typeK names), data)
       
   939   end;
   944 
   940 
   945 
   941 
   946 (* add type arities *)
   942 (* add type arities *)
   947 
   943 
   948 fun ext_ars int prep_sort sg (syn, tsig, ctab, (path, spaces), data) arities =
   944 fun ext_ars int prep_sort sg (syn, tsig, ctab, (path, spaces), data) arities =
   949   let
   945   let
   950     val prepS = prep_sort sg syn tsig spaces;
   946     val prepS = prep_sort sg syn tsig spaces;
   951     fun prep_arity (c, Ss, S) =
   947     fun prep_arity (c, Ss, S) =
   952       (if int then intrn spaces typeK c else c, map prepS Ss, prepS S);
   948       (if int then intrn spaces typeK c else c, map prepS Ss, prepS S);
   953     val tsig' = Type.ext_tsig_arities tsig (map prep_arity arities);
   949     val tsig' = Type.add_arities (map prep_arity arities) tsig;
   954     val log_types = Type.logical_types tsig';
   950     val log_types = Type.logical_types tsig';
   955   in
   951   in
   956     (map_syntax (Syntax.extend_log_types log_types) syn,
   952     (map_syntax (Syntax.extend_log_types log_types) syn,
   957       tsig', ctab, (path, spaces), data)
   953       tsig', ctab, (path, spaces), data)
   958   end;
   954   end;
   978     handle ERROR => err_in_const (const_name path c mx);
   974     handle ERROR => err_in_const (const_name path c mx);
   979 
   975 
   980 fun ext_cnsts rd_const syn_only prmode sg (syn, tsig, ctab, (path, spaces), data)
   976 fun ext_cnsts rd_const syn_only prmode sg (syn, tsig, ctab, (path, spaces), data)
   981     raw_consts =
   977     raw_consts =
   982   let
   978   let
   983     fun prep_const (c, ty, mx) =
   979     val prep_typ = compress_type o Type.varifyT o Type.no_tvars o
   984       (c, compress_type (Type.varifyT (Type.cert_typ tsig (Type.no_tvars ty))), mx)
   980       (if syn_only then Type.cert_typ_syntax tsig else Term.no_dummyT o Type.cert_typ tsig);
   985         handle TYPE (msg, _, _) =>
   981     fun prep_const (c, ty, mx) = (c, prep_typ ty, mx) handle TYPE (msg, _, _) =>
   986           (error_msg msg; err_in_const (const_name path c mx));
   982       (error_msg msg; err_in_const (const_name path c mx));
   987 
   983 
   988     val consts = map (prep_const o rd_const sg syn tsig (path, spaces)) raw_consts;
   984     val consts = map (prep_const o rd_const sg syn tsig (path, spaces)) raw_consts;
   989     val decls =
   985     val decls =
   990       if syn_only then []
   986       if syn_only then []
   991       else decls_of path Syntax.const_name consts;
   987       else decls_of path Syntax.const_name consts;
   992   in
   988   in
   993     (map_syntax (Syntax.extend_const_gram prmode consts) syn, tsig,
   989     (map_syntax (Syntax.extend_const_gram prmode consts) syn, tsig,
   994       Symtab.extend (ctab, decls)
   990       Symtab.extend (ctab, map (fn (c, T) => (c, (T, stamp ()))) decls)
   995         handle Symtab.DUPS cs => err_dup_consts cs,
   991         handle Symtab.DUPS cs => err_dup_consts cs,
   996       (path, add_names spaces constK (map fst decls)), data)
   992       (path, add_names spaces constK (map fst decls)), data)
   997   end;
   993   end;
   998 
   994 
   999 fun ext_consts_i x = ext_cnsts no_read false ("", true) x;
   995 fun ext_consts_i x = ext_cnsts no_read false ("", true) x;
  1029     val classes' =
  1025     val classes' =
  1030       ListPair.map (fn (c, (_, cs)) => (c, intrn cs)) (full_names, classes);
  1026       ListPair.map (fn (c, (_, cs)) => (c, intrn cs)) (full_names, classes);
  1031   in
  1027   in
  1032     ext_consts_i sg
  1028     ext_consts_i sg
  1033       (map_syntax (Syntax.extend_consts names) syn,
  1029       (map_syntax (Syntax.extend_consts names) syn,
  1034         Type.ext_tsig_classes tsig classes', ctab, (path, spaces'), data)
  1030         Type.add_classes classes' tsig, ctab, (path, spaces'), data)
  1035     consts
  1031     consts
  1036   end;
  1032   end;
  1037 
  1033 
  1038 
  1034 
  1039 (* add to classrel *)
  1035 (* add to classrel *)
  1040 
  1036 
  1041 fun ext_classrel int _ (syn, tsig, ctab, (path, spaces), data) pairs =
  1037 fun ext_classrel int _ (syn, tsig, ctab, (path, spaces), data) pairs =
  1042   let val intrn = if int then map (pairself (intrn_class spaces)) else I in
  1038   let val intrn = if int then map (pairself (intrn_class spaces)) else I in
  1043     (syn, Type.ext_tsig_classrel tsig (intrn pairs), ctab, (path, spaces), data)
  1039     (syn, Type.add_classrel (intrn pairs) tsig, ctab, (path, spaces), data)
  1044   end;
  1040   end;
  1045 
  1041 
  1046 
  1042 
  1047 (* add translation functions *)
  1043 (* add translation functions *)
  1048 
  1044 
  1114 
  1110 
  1115 
  1111 
  1116 fun copy_data (k, (e, mths as (cp, _, _, _))) =
  1112 fun copy_data (k, (e, mths as (cp, _, _, _))) =
  1117   (k, (cp e handle exn => err_method "copy" (Object.name_of_kind k) exn, mths));
  1113   (k, (cp e handle exn => err_method "copy" (Object.name_of_kind k) exn, mths));
  1118 
  1114 
  1119 fun copy (sg as Sg ({id = _, stamps, syn}, {self, tsig, const_tab, path, spaces, data})) =
  1115 fun copy (sg as Sg ({id = _, stamps, syn}, {self, tsig, consts, path, spaces, data})) =
  1120   let
  1116   let
  1121     val _ = check_stale sg;
  1117     val _ = check_stale sg;
  1122     val self' = SgRef (Some (ref sg));
  1118     val self' = SgRef (Some (ref sg));
  1123     val Data tab = data;
  1119     val Data tab = data;
  1124     val data' = Data (Symtab.map copy_data tab);
  1120     val data' = Data (Symtab.map copy_data tab);
  1125   in create_sign self' stamps "#" (syn, tsig, const_tab, (path, spaces), data') end;
  1121   in create_sign self' stamps "#" (syn, tsig, consts, (path, spaces), data') end;
  1126 
  1122 
  1127 
  1123 
  1128 (* the external interfaces *)
  1124 (* the external interfaces *)
  1129 
  1125 
  1130 val add_classes          = extend_sign true (ext_classes true) "#";
  1126 val add_classes          = extend_sign true (ext_classes true) "#";
  1202       exists_stamp CPureN sg1 andalso exists_stamp PureN sg2 then
  1198       exists_stamp CPureN sg1 andalso exists_stamp PureN sg2 then
  1203     raise TERM ("Cannot merge Pure and CPure signatures", [])
  1199     raise TERM ("Cannot merge Pure and CPure signatures", [])
  1204   else
  1200   else
  1205     let
  1201     let
  1206       val Sg ({id = _, stamps = stamps1, syn = Syn (syntax1, trfuns1)},
  1202       val Sg ({id = _, stamps = stamps1, syn = Syn (syntax1, trfuns1)},
  1207           {self = _, tsig = tsig1, const_tab = const_tab1,
  1203           {self = _, tsig = tsig1, consts = consts1,
  1208             path = _, spaces = spaces1, data = data1}) = sg1;
  1204             path = _, spaces = spaces1, data = data1}) = sg1;
  1209       val Sg ({id = _, stamps = stamps2, syn = Syn (syntax2, trfuns2)},
  1205       val Sg ({id = _, stamps = stamps2, syn = Syn (syntax2, trfuns2)},
  1210           {self = _, tsig = tsig2, const_tab = const_tab2,
  1206           {self = _, tsig = tsig2, consts = consts2,
  1211             path = _, spaces = spaces2, data = data2}) = sg2;
  1207             path = _, spaces = spaces2, data = data2}) = sg2;
  1212 
  1208 
  1213       val id = ref "";
  1209       val id = ref "";
  1214       val self_ref = ref dummy_sg;
  1210       val self_ref = ref dummy_sg;
  1215       val self = SgRef (Some self_ref);
  1211       val self = SgRef (Some self_ref);
  1216 
  1212 
  1217       val stamps = merge_stamps stamps1 stamps2;
  1213       val stamps = merge_stamps stamps1 stamps2;
  1218       val syntax = Syntax.merge_syntaxes syntax1 syntax2;
  1214       val syntax = Syntax.merge_syntaxes syntax1 syntax2;
  1219       val trfuns = merge_trfuns trfuns1 trfuns2;
  1215       val trfuns = merge_trfuns trfuns1 trfuns2;
  1220       val tsig = Type.merge_tsigs (tsig1, tsig2);
  1216       val tsig = Type.merge_tsigs (tsig1, tsig2);
  1221       val const_tab = Symtab.merge (op =) (const_tab1, const_tab2)
  1217       val consts = Symtab.merge eq_snd (consts1, consts2)
  1222         handle Symtab.DUPS cs =>
  1218         handle Symtab.DUPS cs => err_dup_consts cs;
  1223           raise TERM ("Incompatible types for constant(s) " ^ commas_quote cs, []);
       
  1224 
  1219 
  1225       val path = Some [];
  1220       val path = Some [];
  1226       val kinds = distinct (map fst (spaces1 @ spaces2));
  1221       val kinds = distinct (map fst (spaces1 @ spaces2));
  1227       val spaces =
  1222       val spaces =
  1228         kinds ~~
  1223         kinds ~~
  1229           ListPair.map NameSpace.merge
  1224           ListPair.map NameSpace.merge
  1230             (map (space_of spaces1) kinds, map (space_of spaces2) kinds);
  1225             (map (space_of spaces1) kinds, map (space_of spaces2) kinds);
  1231 
  1226 
  1232       val data = merge_data (data1, data2);
  1227       val data = merge_data (data1, data2);
  1233 
  1228 
  1234       val sign = make_sign (id, self, tsig, const_tab, Syn (syntax, trfuns),
  1229       val sign = make_sign (id, self, tsig, consts, Syn (syntax, trfuns),
  1235         path, spaces, data, stamps);
  1230         path, spaces, data, stamps);
  1236     in self_ref := sign; syn_of sign; sign end;
  1231     in self_ref := sign; syn_of sign; sign end;
  1237 
  1232 
  1238 end;
  1233 end;