src/Pure/type.ML
changeset 14830 faa4865ba1ce
parent 14790 0d984ee030a1
child 14906 2da524f3d785
equal deleted inserted replaced
14829:cfa5fe01a7b7 14830:faa4865ba1ce
    30   val subsort: tsig -> sort * sort -> bool
    30   val subsort: tsig -> sort * sort -> bool
    31   val of_sort: tsig -> typ * sort -> bool
    31   val of_sort: tsig -> typ * sort -> bool
    32   val cert_class: tsig -> class -> class
    32   val cert_class: tsig -> class -> class
    33   val cert_sort: tsig -> sort -> sort
    33   val cert_sort: tsig -> sort -> sort
    34   val witness_sorts: tsig -> sort list -> sort list -> (typ * sort) list
    34   val witness_sorts: tsig -> sort list -> sort list -> (typ * sort) list
    35   val norm_typ: tsig -> typ -> typ
       
    36   val cert_typ: tsig -> typ -> typ
    35   val cert_typ: tsig -> typ -> typ
    37   val cert_typ_syntax: tsig -> typ -> typ
    36   val cert_typ_syntax: tsig -> typ -> typ
    38   val cert_typ_raw: tsig -> typ -> typ
    37   val cert_typ_raw: tsig -> typ -> typ
    39 
    38 
    40   (*special treatment of type vars*)
    39   (*special treatment of type vars*)
    45   val varify: term * string list -> term * (string * indexname) list
    44   val varify: term * string list -> term * (string * indexname) list
    46   val freeze_thaw_type : typ -> typ * (typ -> typ)
    45   val freeze_thaw_type : typ -> typ * (typ -> typ)
    47   val freeze_thaw : term -> term * (term -> term)
    46   val freeze_thaw : term -> term * (term -> term)
    48 
    47 
    49   (*matching and unification*)
    48   (*matching and unification*)
    50   val inst_term_tvars: tsig -> (indexname * typ) list -> term -> term
    49   val inst_typ_tvars: Pretty.pp -> tsig -> (indexname * typ) list -> typ -> typ
    51   val inst_typ_tvars: tsig -> (indexname * typ) list -> typ -> typ
    50   val inst_term_tvars: Pretty.pp -> tsig -> (indexname * typ) list -> term -> term
    52   exception TYPE_MATCH
    51   exception TYPE_MATCH
    53   val typ_match: tsig -> typ Vartab.table * (typ * typ) -> typ Vartab.table
    52   val typ_match: tsig -> typ Vartab.table * (typ * typ) -> typ Vartab.table
    54   val typ_instance: tsig -> typ * typ -> bool
    53   val typ_instance: tsig -> typ * typ -> bool
    55   exception TUNIFY
    54   exception TUNIFY
    56   val unify: tsig -> typ Vartab.table * int -> typ * typ -> typ Vartab.table * int
    55   val unify: tsig -> typ Vartab.table * int -> typ * typ -> typ Vartab.table * int
    57   val raw_unify: typ * typ -> bool
    56   val raw_unify: typ * typ -> bool
    58 
    57 
    59   (*extend and merge type signatures*)
    58   (*extend and merge type signatures*)
    60   val add_classes: (class * class list) list -> tsig -> tsig
    59   val add_classes: Pretty.pp -> (class * class list) list -> tsig -> tsig
    61   val add_classrel: (class * class) list -> tsig -> tsig
    60   val add_classrel: Pretty.pp -> (class * class) list -> tsig -> tsig
    62   val set_defsort: sort -> tsig -> tsig
    61   val set_defsort: sort -> tsig -> tsig
    63   val add_types: (string * int) list -> tsig -> tsig
    62   val add_types: (string * int) list -> tsig -> tsig
    64   val add_abbrs: (string * string list * typ) list -> tsig -> tsig
    63   val add_abbrs: (string * string list * typ) list -> tsig -> tsig
    65   val add_nonterminals: string list -> tsig -> tsig
    64   val add_nonterminals: string list -> tsig -> tsig
    66   val add_arities: (string * sort list * sort) list -> tsig -> tsig
    65   val add_arities: Pretty.pp -> arity list -> tsig -> tsig
    67   val merge_tsigs: tsig * tsig -> tsig
    66   val merge_tsigs: Pretty.pp -> tsig * tsig -> tsig
    68 end;
    67 end;
    69 
    68 
    70 structure Type: TYPE =
    69 structure Type: TYPE =
    71 struct
    70 struct
    72 
    71 
   145 
   144 
   146 
   145 
   147 (* certified types *)
   146 (* certified types *)
   148 
   147 
   149 fun bad_nargs t = "Bad number of arguments for type constructor: " ^ quote t;
   148 fun bad_nargs t = "Bad number of arguments for type constructor: " ^ quote t;
       
   149 fun undecl_type c = "Undeclared type constructor: " ^ quote c;
       
   150 
       
   151 local
   150 
   152 
   151 fun inst_typ tye =
   153 fun inst_typ tye =
   152   let
   154   let
   153     fun inst (var as (v, _)) =
   155     fun inst (var as (v, _)) =
   154       (case assoc_string_int (tye, v) of
   156       (case assoc_string_int (tye, v) of
   155         Some U => inst_typ tye U
   157         Some U => inst_typ tye U
   156       | None => TVar var);
   158       | None => TVar var);
   157   in map_type_tvar inst end;
   159   in map_type_tvar inst end;
   158 
   160 
   159 (*expand type abbreviations and normalize sorts*)
       
   160 fun norm_typ (tsig as TSig {types, ...}) ty =
   161 fun norm_typ (tsig as TSig {types, ...}) ty =
   161   let
   162   let
   162     val idx = Term.maxidx_of_typ ty + 1;
   163     val idx = Term.maxidx_of_typ ty + 1;
   163 
   164 
   164     fun norm (Type (a, Ts)) =
   165     fun norm (Type (a, Ts)) =
   170       | norm (TVar (xi, S)) = TVar (xi, norm_sort tsig S);
   171       | norm (TVar (xi, S)) = TVar (xi, norm_sort tsig S);
   171 
   172 
   172     val ty' = norm ty;
   173     val ty' = norm ty;
   173   in if ty = ty' then ty else ty' end;  (*avoid copying of already normal type*)
   174   in if ty = ty' then ty else ty' end;  (*avoid copying of already normal type*)
   174 
   175 
   175 (*check validity of (not necessarily normal) type*)   (*exception TYPE*)
       
   176 fun certify_typ normalize syntax tsig ty =
   176 fun certify_typ normalize syntax tsig ty =
   177   let
   177   let
   178     val TSig {types, ...} = tsig;
   178     val TSig {types, ...} = tsig;
   179     fun err msg = raise TYPE (msg, [ty], []);
   179     fun err msg = raise TYPE (msg, [ty], []);
   180 
   180 
   184           let fun nargs n = if length Ts <> n then err (bad_nargs c) else () in
   184           let fun nargs n = if length Ts <> n then err (bad_nargs c) else () in
   185             (case Symtab.lookup (types, c) of
   185             (case Symtab.lookup (types, c) of
   186               Some (LogicalType n, _) => nargs n
   186               Some (LogicalType n, _) => nargs n
   187             | Some (Abbreviation (vs, _), _) => nargs (length vs)
   187             | Some (Abbreviation (vs, _), _) => nargs (length vs)
   188             | Some (Nonterminal, _) => nargs 0
   188             | Some (Nonterminal, _) => nargs 0
   189             | None => err ("Undeclared type constructor: " ^ quote c));
   189             | None => err (undecl_type c));
   190             seq check_typ Ts
   190             seq check_typ Ts
   191           end
   191           end
   192     | check_typ (TFree (_, S)) = check_sort S
   192     | check_typ (TFree (_, S)) = check_sort S
   193     | check_typ (TVar ((x, i), S)) =
   193     | check_typ (TVar ((x, i), S)) =
   194         if i < 0 then err ("Malformed type variable: " ^ quote (Term.string_of_vname (x, i)))
   194         if i < 0 then err ("Malformed type variable: " ^ quote (Term.string_of_vname (x, i)))
   204     val _ = check_typ ty;
   204     val _ = check_typ ty;
   205     val ty' = if normalize orelse not syntax then norm_typ tsig ty else ty;
   205     val ty' = if normalize orelse not syntax then norm_typ tsig ty else ty;
   206     val _ = if not syntax then no_syntax ty' else ();
   206     val _ = if not syntax then no_syntax ty' else ();
   207   in ty' end;
   207   in ty' end;
   208 
   208 
       
   209 in
       
   210 
   209 val cert_typ         = certify_typ true false;
   211 val cert_typ         = certify_typ true false;
   210 val cert_typ_syntax  = certify_typ true true;
   212 val cert_typ_syntax  = certify_typ true true;
   211 val cert_typ_raw     = certify_typ false true;
   213 val cert_typ_raw     = certify_typ false true;
   212 
   214 
       
   215 end;
   213 
   216 
   214 
   217 
   215 (** special treatment of type vars **)
   218 (** special treatment of type vars **)
   216 
   219 
   217 (* strip_sorts *)
   220 (* strip_sorts *)
   224 (* no_tvars *)
   227 (* no_tvars *)
   225 
   228 
   226 fun no_tvars T =
   229 fun no_tvars T =
   227   (case typ_tvars T of [] => T
   230   (case typ_tvars T of [] => T
   228   | vs => raise TYPE ("Illegal schematic type variable(s): " ^
   231   | vs => raise TYPE ("Illegal schematic type variable(s): " ^
   229       commas (map (Term.string_of_vname o #1) vs), [T], []));
   232       commas_quote (map (Term.string_of_vname o #1) vs), [T], []));
   230 
   233 
   231 
   234 
   232 (* varify, unvarify *)
   235 (* varify, unvarify *)
   233 
   236 
   234 val varifyT = map_type_tfree (fn (a, S) => TVar ((a, 0), S));
   237 val varifyT = map_type_tfree (fn (a, S) => TVar ((a, 0), S));
   261   TFree (the (assoc (alist, ix)), sort)
   264   TFree (the (assoc (alist, ix)), sort)
   262     handle OPTION =>
   265     handle OPTION =>
   263       raise TYPE ("Failure during freezing of ?" ^ string_of_indexname ix, [], []);
   266       raise TYPE ("Failure during freezing of ?" ^ string_of_indexname ix, [], []);
   264 
   267 
   265 fun thaw_one alist (a,sort) = TVar (the (assoc (alist,a)), sort)
   268 fun thaw_one alist (a,sort) = TVar (the (assoc (alist,a)), sort)
   266       handle OPTION => TFree(a,sort);
   269   handle OPTION => TFree(a, sort);
   267 
   270 
   268 in
   271 in
   269 
   272 
   270 (*this sort of code could replace unvarifyT*)
   273 (*this sort of code could replace unvarifyT*)
   271 fun freeze_thaw_type T =
   274 fun freeze_thaw_type T =
   293 
   296 
   294 (** matching and unification of types **)
   297 (** matching and unification of types **)
   295 
   298 
   296 (* instantiation *)
   299 (* instantiation *)
   297 
   300 
   298 fun type_of_sort tsig (T, S) =
   301 fun type_of_sort pp tsig (T, S) =
   299   if of_sort tsig (T, S) then T
   302   if of_sort tsig (T, S) then T
   300   else raise TYPE ("Type not of sort " ^ Sorts.str_of_sort S, [T], []);
   303   else raise TYPE ("Type not of sort " ^ Pretty.string_of_sort pp S, [T], []);
   301 
   304 
   302 fun inst_typ_tvars tsig tye =
   305 fun inst_typ_tvars pp tsig tye =
   303   let
   306   let
   304     fun inst (var as (v, S)) =
   307     fun inst (var as (v, S)) =
   305       (case assoc_string_int (tye, v) of
   308       (case assoc_string_int (tye, v) of
   306         Some U => type_of_sort tsig (U, S)
   309         Some U => type_of_sort pp tsig (U, S)
   307       | None => TVar var);
   310       | None => TVar var);
   308   in map_type_tvar inst end;
   311   in map_type_tvar inst end;
   309 
   312 
   310 fun inst_term_tvars _ [] t = t
   313 fun inst_term_tvars _ _ [] t = t
   311   | inst_term_tvars tsig tye t = map_term_types (inst_typ_tvars tsig tye) t;
   314   | inst_term_tvars pp tsig tye t = map_term_types (inst_typ_tvars pp tsig tye) t;
   312 
   315 
   313 
   316 
   314 (* matching *)
   317 (* matching *)
   315 
   318 
   316 exception TYPE_MATCH;
   319 exception TYPE_MATCH;
   317 
   320 
   318 fun typ_match tsig =
   321 fun typ_match tsig =
   319   let
   322   let
   320     fun match (subs, (TVar (v, S), T)) =
   323     fun match (subs, (TVar (v, S), T)) =
   321           (case Vartab.lookup (subs, v) of
   324           (case Vartab.lookup (subs, v) of
   322             None => (Vartab.update_new ((v, type_of_sort tsig (T, S)), subs)
   325             None => (Vartab.update_new ((v, type_of_sort Pretty.pp_undef tsig (T, S)), subs)
   323               handle TYPE _ => raise TYPE_MATCH)
   326               handle TYPE _ => raise TYPE_MATCH)
   324           | Some U => if U = T then subs else raise TYPE_MATCH)
   327           | Some U => if U = T then subs else raise TYPE_MATCH)
   325       | match (subs, (Type (a, Ts), Type (b, Us))) =
   328       | match (subs, (Type (a, Ts), Type (b, Us))) =
   326           if a <> b then raise TYPE_MATCH
   329           if a <> b then raise TYPE_MATCH
   327           else foldl match (subs, Ts ~~ Us)
   330           else foldl match (subs, Ts ~~ Us)
   415 (* arities *)
   418 (* arities *)
   416 
   419 
   417 local
   420 local
   418 
   421 
   419 fun err_decl t decl = error ("Illegal " ^ str_of_decl decl ^ ": " ^ quote t);
   422 fun err_decl t decl = error ("Illegal " ^ str_of_decl decl ^ ": " ^ quote t);
   420 fun err_undecl t = error ("Undeclared type constructor: " ^ quote t);
   423 
   421 
   424 fun for_classes _ None = ""
   422 fun err_conflict t (c1, c2) (c, Ss) (c', Ss') =
   425   | for_classes pp (Some (c1, c2)) =
   423   error ("Conflict of type arities for classes " ^ quote c1 ^ " < " ^ quote c2 ^ ":\n  " ^
   426       " for classes " ^ Pretty.string_of_classrel pp [c1, c2];
   424     Sorts.str_of_arity (t, Ss, [c]) ^ " and\n  " ^
   427 
   425     Sorts.str_of_arity (t, Ss', [c']));
   428 fun err_conflict pp t cc (c, Ss) (c', Ss') =
   426 
   429   error ("Conflict of type arities" ^ for_classes pp cc ^ ":\n  " ^
   427 fun coregular C t (c, Ss) ars =
   430     Pretty.string_of_arity pp (t, Ss, [c]) ^ " and\n  " ^
       
   431     Pretty.string_of_arity pp (t, Ss', [c']));
       
   432 
       
   433 fun coregular pp C t (c, Ss) ars =
   428   let
   434   let
   429     fun conflict (c', Ss') =
   435     fun conflict (c', Ss') =
   430       if Sorts.class_le C (c, c') andalso not (Sorts.sorts_le C (Ss, Ss')) then
   436       if Sorts.class_le C (c, c') andalso not (Sorts.sorts_le C (Ss, Ss')) then
   431         Some ((c, c'), (c', Ss'))
   437         Some ((c, c'), (c', Ss'))
   432       else if Sorts.class_le C (c', c) andalso not (Sorts.sorts_le C (Ss', Ss)) then
   438       else if Sorts.class_le C (c', c) andalso not (Sorts.sorts_le C (Ss', Ss)) then
   433         Some ((c', c), (c', Ss'))
   439         Some ((c', c), (c', Ss'))
   434       else None;
   440       else None;
   435   in
   441   in
   436     (case Library.get_first conflict ars of
   442     (case Library.get_first conflict ars of
   437       Some ((c1, c2), (c', Ss')) => err_conflict t (c1, c2) (c, Ss) (c', Ss')
   443       Some ((c1, c2), (c', Ss')) => err_conflict pp t (Some (c1, c2)) (c, Ss) (c', Ss')
   438     | None => (c, Ss) :: ars)
   444     | None => (c, Ss) :: ars)
   439   end;
   445   end;
   440 
   446 
   441 fun insert C t ((c, Ss), ars) =
   447 fun insert pp C t ((c, Ss), ars) =
   442   (case assoc_string (ars, c) of
   448   (case assoc_string (ars, c) of
   443     None => coregular C t (c, Ss) ars
   449     None => coregular pp C t (c, Ss) ars
   444   | Some Ss' =>
   450   | Some Ss' =>
   445       if Sorts.sorts_le C (Ss, Ss') then ars
   451       if Sorts.sorts_le C (Ss, Ss') then ars
   446       else if Sorts.sorts_le C (Ss', Ss)
   452       else if Sorts.sorts_le C (Ss', Ss)
   447       then coregular C t (c, Ss) (ars \ (c, Ss'))
   453       then coregular pp C t (c, Ss) (ars \ (c, Ss'))
   448       else coregular C t (c, Ss) ars);
   454       else err_conflict pp t None (c, Ss) (c, Ss'));
   449 
   455 
   450 fun complete C (c, Ss) = map (rpair Ss) (Graph.all_succs C [c]);
   456 fun complete C (c, Ss) = map (rpair Ss) (Graph.all_succs C [c]);
   451 
   457 
   452 fun insert_arities classes (arities, (t, ars)) =
   458 fun insert_arities pp classes (arities, (t, ars)) =
   453   let val ars' =
   459   let val ars' =
   454     Symtab.lookup_multi (arities, t)
   460     Symtab.lookup_multi (arities, t)
   455     |> curry (foldr (insert classes t)) (flat (map (complete classes) ars))
   461     |> curry (foldr (insert pp classes t)) (flat (map (complete classes) ars))
   456   in Symtab.update ((t, ars'), arities) end;
   462   in Symtab.update ((t, ars'), arities) end;
   457 
   463 
   458 fun insert_table classes = Symtab.foldl (fn (arities, (t, ars)) =>
   464 fun insert_table pp classes = Symtab.foldl (fn (arities, (t, ars)) =>
   459   insert_arities classes (arities, (t, map (apsnd (map (Sorts.norm_sort classes))) ars)));
   465   insert_arities pp classes (arities, (t, map (apsnd (map (Sorts.norm_sort classes))) ars)));
   460 
   466 
   461 in
   467 in
   462 
   468 
   463 fun add_arities decls tsig = tsig |> change_tsig (fn (classes, default, types, arities) =>
   469 fun add_arities pp decls tsig = tsig |> change_tsig (fn (classes, default, types, arities) =>
   464   let
   470   let
   465     fun prep (t, Ss, S) =
   471     fun prep (t, Ss, S) =
   466       (case Symtab.lookup (types, t) of
   472       (case Symtab.lookup (types, t) of
   467         Some (LogicalType n, _) =>
   473         Some (LogicalType n, _) =>
   468           if length Ss = n then
   474           if length Ss = n then
   469             (t, map (cert_sort tsig) Ss, cert_sort tsig S)
   475             (t, map (cert_sort tsig) Ss, cert_sort tsig S)
   470               handle TYPE (msg, _, _) => error msg
   476               handle TYPE (msg, _, _) => error msg
   471           else error (bad_nargs t)
   477           else error (bad_nargs t)
   472       | Some (decl, _) => err_decl t decl
   478       | Some (decl, _) => err_decl t decl
   473       | None => err_undecl t);
   479       | None => error (undecl_type t));
   474 
   480 
   475     val ars = decls |> map ((fn (t, Ss, S) => (t, map (fn c => (c, Ss)) S)) o prep);
   481     val ars = decls |> map ((fn (t, Ss, S) => (t, map (fn c => (c, Ss)) S)) o prep);
   476     val arities' = foldl (insert_arities classes) (arities, ars);
   482     val arities' = foldl (insert_arities pp classes) (arities, ars);
   477   in (classes, default, types, arities') end);
   483   in (classes, default, types, arities') end);
   478 
   484 
   479 fun rebuild_arities classes arities =
   485 fun rebuild_arities pp classes arities =
   480   insert_table classes (Symtab.empty, arities);
   486   insert_table pp classes (Symtab.empty, arities);
   481 
   487 
   482 fun merge_arities classes (arities1, arities2) =
   488 fun merge_arities pp classes (arities1, arities2) =
   483   insert_table classes (insert_table classes (Symtab.empty, arities1), arities2);
   489   insert_table pp classes (insert_table pp classes (Symtab.empty, arities1), arities2);
   484 
   490 
   485 end;
   491 end;
   486 
   492 
   487 
   493 
   488 (* classes *)
   494 (* classes *)
   490 local
   496 local
   491 
   497 
   492 fun err_dup_classes cs =
   498 fun err_dup_classes cs =
   493   error ("Duplicate declaration of class(es): " ^ commas_quote cs);
   499   error ("Duplicate declaration of class(es): " ^ commas_quote cs);
   494 
   500 
   495 fun err_cyclic_classes css =
   501 fun err_cyclic_classes pp css =
   496   error (cat_lines (map (fn cs =>
   502   error (cat_lines (map (fn cs =>
   497     "Cycle in class relation: " ^ space_implode " < " (map quote cs)) css));
   503     "Cycle in class relation: " ^ Pretty.string_of_classrel pp cs) css));
   498 
   504 
   499 fun add_class (c, cs) tsig = tsig |> change_tsig (fn (classes, default, types, arities) =>
   505 fun add_class pp (c, cs) tsig = tsig |> change_tsig (fn (classes, default, types, arities) =>
   500   let
   506   let
   501     val cs' = map (cert_class tsig) cs
   507     val cs' = map (cert_class tsig) cs
   502       handle TYPE (msg, _, _) => error msg;
   508       handle TYPE (msg, _, _) => error msg;
   503     val classes' = classes |> Graph.new_node (c, stamp ())
   509     val classes' = classes |> Graph.new_node (c, stamp ())
   504       handle Graph.DUP d => err_dup_classes [d];
   510       handle Graph.DUP d => err_dup_classes [d];
   505     val classes'' = classes' |> fold Graph.add_edge_trans_acyclic (map (pair c) cs')
   511     val classes'' = classes' |> fold Graph.add_edge_trans_acyclic (map (pair c) cs')
   506       handle Graph.CYCLES css => err_cyclic_classes css;
   512       handle Graph.CYCLES css => err_cyclic_classes pp css;
   507   in (classes'', default, types, arities) end);
   513   in (classes'', default, types, arities) end);
   508 
   514 
   509 in
   515 in
   510 
   516 
   511 val add_classes = fold add_class;
   517 val add_classes = fold o add_class;
   512 
   518 
   513 fun add_classrel ps tsig = tsig |> change_tsig (fn (classes, default, types, arities) =>
   519 fun add_classrel pp ps tsig = tsig |> change_tsig (fn (classes, default, types, arities) =>
   514   let
   520   let
   515     val ps' = map (pairself (cert_class tsig)) ps
   521     val ps' = map (pairself (cert_class tsig)) ps
   516       handle TYPE (msg, _, _) => error msg;
   522       handle TYPE (msg, _, _) => error msg;
   517     val classes' = classes |> fold Graph.add_edge_trans_acyclic ps'
   523     val classes' = classes |> fold Graph.add_edge_trans_acyclic ps'
   518       handle Graph.CYCLES css => err_cyclic_classes css;
   524       handle Graph.CYCLES css => err_cyclic_classes pp css;
   519     val default' = default |> Sorts.norm_sort classes';
   525     val default' = default |> Sorts.norm_sort classes';
   520     val arities' = arities |> rebuild_arities classes';
   526     val arities' = arities |> rebuild_arities pp classes';
   521   in (classes', default', types, arities') end);
   527   in (classes', default', types, arities') end);
   522 
   528 
   523 fun merge_classes CC = Graph.merge_trans_acyclic (op =) CC
   529 fun merge_classes pp CC = Graph.merge_trans_acyclic (op =) CC
   524   handle Graph.DUPS cs => err_dup_classes cs
   530   handle Graph.DUPS cs => err_dup_classes cs
   525     | Graph.CYCLES css => err_cyclic_classes css;
   531     | Graph.CYCLES css => err_cyclic_classes pp css;
   526 
   532 
   527 end;
   533 end;
   528 
   534 
   529 
   535 
   530 (* default sort *)
   536 (* default sort *)
   543 fun err_in_decls c decl decl' =
   549 fun err_in_decls c decl decl' =
   544   let
   550   let
   545     val s = str_of_decl decl;
   551     val s = str_of_decl decl;
   546     val s' = str_of_decl decl';
   552     val s' = str_of_decl decl';
   547   in
   553   in
   548     if s = s' then error ("Duplicate declaration of " ^ s ^ ": " ^ quote c)
   554     if s = s' then
   549     else error ("Conflicting declarations of " ^ s ^ " with " ^ s' ^ ": " ^ quote c)
   555       error ("Duplicate declaration of " ^ s ^ ": " ^ quote c)
       
   556     else error ("Conflict of " ^ s ^ " with " ^ s' ^ ": " ^ quote c)
   550   end;
   557   end;
   551 
   558 
   552 fun new_decl (c, decl) types =
   559 fun new_decl (c, decl) types =
   553   (case Symtab.lookup (types, c) of
   560   (case Symtab.lookup (types, c) of
   554     Some (decl', _) => err_in_decls c decl decl'
   561     Some (decl', _) => err_in_decls c decl decl'
   590 end;
   597 end;
   591 
   598 
   592 
   599 
   593 (* merge type signatures *)
   600 (* merge type signatures *)
   594 
   601 
   595 fun merge_tsigs (tsig1, tsig2) =
   602 fun merge_tsigs pp (tsig1, tsig2) =
   596   let
   603   let
   597     val (TSig {classes = classes1, default = default1, types = types1, arities = arities1,
   604     val (TSig {classes = classes1, default = default1, types = types1, arities = arities1,
   598       log_types = _, witness = _}) = tsig1;
   605       log_types = _, witness = _}) = tsig1;
   599     val (TSig {classes = classes2, default = default2, types = types2, arities = arities2,
   606     val (TSig {classes = classes2, default = default2, types = types2, arities = arities2,
   600       log_types = _, witness = _}) = tsig2;
   607       log_types = _, witness = _}) = tsig2;
   601 
   608 
   602     val classes' = merge_classes (classes1, classes2);
   609     val classes' = merge_classes pp (classes1, classes2);
   603     val default' = Sorts.inter_sort classes' (default1, default2);
   610     val default' = Sorts.inter_sort classes' (default1, default2);
   604     val types' = merge_types (types1, types2);
   611     val types' = merge_types (types1, types2);
   605     val arities' = merge_arities classes' (arities1, arities2);
   612     val arities' = merge_arities pp classes' (arities1, arities2);
   606   in build_tsig (classes', default', types', arities') end;
   613   in build_tsig (classes', default', types', arities') end;
   607 
   614 
   608 end;
   615 end;