(* Oukseh Lee Copyright(c) 2000-2004 KAIST/SNU Research On Program Analysis System (National Creative Research Initiative Center 1998-2003) http://ropas.snu.ac.kr/n All rights reserved. This file is distributed under the terms of an Open Source License. *) structure TyError = struct open Info.StringInfo open Format structure A = String_ast.Ast type class = Var | Typ | Tv | Str | Sig | Fct type id = string type lid = id list type gid = class * lid (* type id = Varid of A.varlongid | Tyid of A.tylongid | Tvid of tyvarinfo | Strid of A.strlongid | Sigid of sigidinfo | Fctid of fctidinfo *) type match = Domain of gid | Value of id * Ty.scheme * Ty.scheme | Type of id * tymatch and tymatch = Arity of int * int | Unknown | Name of Ty.Tn.t * Ty.Tn.t | String of Ty.TS.t * Ty.TS.t | Constype of id * (Ty.scheme option) * (Ty.scheme option) | Consexist of id | Consreq of id type cases = SS | FI | FS type t = Unbound of gid | Duplicate of gid | UnifyFail of Ty.ty * Ty.ty | TsApply of lid * int * int | Match of cases * lid option * lid option * (id list * match) | RecursiveType of id list | ConstraintSig of lid | RConstraintSig of lid | FunName of id * id | FunLength of int * int type w = Overload exception TM of tymatch exception M of id list * match exception T of t * Location.t exception W of w * Location.t fun to_lid (a,(b,_),_) = (List.map (fn (x,_) => x) a)@[b] fun to_gid class x = (class, to_lid x) fun to_id (b,_) = b fun add_str str (x,y) = (x,str@y) fun unbound_var (id as (_,_,l)) = raise T(Unbound(to_gid Var id),l) fun unbound_ty (id as (_,_,l)) = raise T(Unbound(to_gid Typ id),l) fun unbound_str (id as (_,_,l)) = raise T(Unbound(to_gid Str id),l) fun unbound_tyvar (id,l) = raise T(Unbound(Tv,[id]),l) fun unbound_sig (id,l) = raise T(Unbound(Sig,[id]),l) fun unbound_fct (id,l) = raise T(Unbound(Fct,[id]),l) fun duplicate_var (id,l) = raise T(Duplicate(Var, [id]), l) fun ts_apply x y z l = raise T(TsApply(to_lid x,y,z),l) fun constraint_sig (id as (_,_,l)) = raise T(ConstraintSig(to_lid id),l) fun redundant_constraint_sig (id as (_,_,l)) = raise T(RConstraintSig(to_lid id),l) fun pp_error() = printf "@[<6>¿À·ù: " fun pp_scheme ((n,t):Ty.scheme) = (printf "@["; TyPrint.typ t; printf "@]") fun pp_list f g [] = () | pp_list f g [h] = f h | pp_list f g (h::t) = (f h; g(); pp_list f g t) fun pp_lid lid = pp_list (printf "%s") (fn _ => printf ".") lid fun pp' (t,l) = let fun fv (class, x) = let val hd = fn (Var | Typ) => "" | Str => "¸ðµâ " | Sig => "¸ðµâŸÀÔ " | Fct => "¸ðµâÇÔ¼ö " | Tv => "ŸÀÔ º¯¼ö " val tl = fn Typ => " ŸÀÔ" | _ => " " in printf "%s" (hd class); pp_lid x; printf "%s" (tl class) end fun fvtail (x,_) = case x of Typ => printf "À»" | _ => printf "À»(¸¦)" fun fvtaili (x,_) = case x of Typ => printf "ÀÌ" | _ => printf "ÀÌ(°¡)" fun fnum 0 s1 s2 = printf "%s" s2 | fnum i s1 s2 = s1 i fun fopt None _ g = g() | fopt (Some t) f _ = f t fun fstropt x = fopt x (fn s => fv(Str, s)) (fn _ => printf "¸ðµâ") fun fsigopt x = fopt x (fn s => fv(Sig, s)) (fn _ => printf "¸ðµâŸÀÔ") fun ffctopt x = fopt x (fn s => fv(Fct, s)) (fn _ => printf "¸ðµâÇÔ¼ö") (* fun pt _ = pp_ty *) fun ps _ = pp_scheme fun pv _ = fv and pvtail _ = fvtail and pvtaili _ = fvtaili fun pstropt _ = fstropt and psigopt _ = fsigopt and pfctopt _ = ffctopt fun pstrtaili _ None = printf "ÀÌ" | pstrtaili _ _ = printf "ÀÌ(°¡)" fun f (Unbound s) = printf "%a%a ¸ð¸¨´Ï´Ù." pv s pvtail s | f (Duplicate s) = printf "%a%a Áߺ¹ µÇ¾ú½À´Ï´Ù." pv s pvtaili s | f (UnifyFail(t1,t2)) = ( TyPrint.start(); printf "@[@["; TyPrint.typ' t1; printf "@]@ ŸÀÔÀ̾î¾ß@ Çϴµ¥@]@ @[@["; TyPrint.typ' t2; printf "@]@ ŸÀÔÀÔ´Ï´Ù.@]"; TyPrint.ending()) | f (TsApply(id,n,m)) = ( printf "%aÀÇ@ ÀÎÀÚ°¡@ " pv (Typ,id); printf "%t¾ß Çϴµ¥@ " (fn _ => fnum n (printf "%d °³") "¾ø¾î"); printf "%t´Ï´Ù." (fn _ => fnum m (printf "%d °³ÀÔ") "¾ø½À")) | f (RecursiveType [a,b]) = ( fv(Typ, [a]); printf "°ú@ "; fv(Typ, [b]); printf "ÀÇ@ Á¤Àǰ¡@ ¼­·Î ¸Â¹°·Á ÀÖ½À´Ï´Ù.") | f (RecursiveType ids) = ( printf "´ÙÀ½ ŸÀÔµéÀÇ Á¤Àǰ¡@ ¼­·Î ¸Â¹°·Á ÀÖ½À´Ï´Ù:@ "; pp_list (fn s => printf "%s" s) (fn _ => printf ",@ ") ids; printf ".") | f (ConstraintSig id) = ( fv(Typ, id); printf "Àº@ ÀÌ¹Ì ¸ðµâŸÀÔ ³»¿¡ Á¤ÀǵǾî@ Á¦ÇÑÇÒ ¼ö ¾ø½À´Ï´Ù.") | f (RConstraintSig id) = ( fv(Typ, id); printf "Àº@ ÀÌ¹Ì Á¦ÇѵǾî@ ¹Ýº¹Çؼ­ Á¦ÇÑÇÒ ¼ö ¾ø½À´Ï´Ù.") | f (FunName(id1,id2)) = printf "%s ¸¦(À») Á¤ÀÇÇÏ´Â µµÁß@ %s ¸¦(À») Á¤ÀÇÇß½À´Ï´Ù." id1 id2 | f (FunLength(n1,n2)) = printf "%d °³ÀÇ ÆÐÅÏÀ̾î¾ß Çϴµ¥@ %d °³°¡ ¿Ô½À´Ï´Ù." n1 n2 | f (Match(c,st,sg,(l,m))) = let val s1 = case c of FS => "Á¤ÀÇ" | _ => "¸ðµâ" val s2 = case c of FI => "Á¢¼Ó¹æ¾È" | _ => "¸ðµâŸÀÔ" in (case c of SS => printf "%a%a@ %a¿¡@ ¸ÂÁö ¾Ê½À´Ï´Ù.@;" pstropt st pstrtaili st psigopt sg | FI => printf "%a%a@ %aÀÇ ÀÔ·ÂÀ¸·Î@ ºÎÀû´çÇÕ´Ï´Ù.@;" pstropt st pstrtaili st pfctopt sg | FS => printf "%aÀÇ Á¤Àǰ¡@ %a¿¡@ ¸ÂÁö ¾Ê½À´Ï´Ù.@;" pfctopt st psigopt sg); (case m of Value(x,t,s) => printf "%aÀÇ Å¸ÀÔÀÌ@ ÀûÀÀ ¾ÈµÇ±â ¶§¹®ÀÔ´Ï´Ù.@\n" pv (Var,l@[x]); printf "@[<4>- %s¿¡¼­´Â@ %a À̰í@]@\n@[<4>- %s¿¡¼­´Â@ %a ÀÔ´Ï´Ù.@]" s1 ps t s2 ps s | Domain x => let val x' = add_str l x in printf "%a%a %s¿¡ ¾ø±â ¶§¹®ÀÔ´Ï´Ù." pv x' pvtaili x' s1 end | Type(t,r) => let val t' = (Typ, l@[t]) in case r of Arity(i,j) => let fun f1 _ = fnum i (printf "%d °³Àε¥") "¾ø´Âµ¥" fun f2 _ = fnum j (printf "%d °³ÀÌ") "¾ø" in printf "%aÀÇ ÀÎÀÚ°¡ %s¿¡´Â@ %t@ %s¿¡´Â@ %t±â ¶§¹®ÀÔ´Ï´Ù." pv t' s1 f1 s2 f2 end | Constype(c,sc1,sc2) => printf "%aÀÇ µ¥ÀÌŸ ±¸¼ºÀÚ %s ÀÇ ÀÎÀÚ Å¸ÀÔÀÌ ´Ù¸£±â ¶§¹®ÀÔ´Ï´Ù.@\n" pv t' c; printf "@[<4>- %s¿¡¼­´Â@ " s1; fopt sc1 (printf "%a À̰í" ps) (fn _ => printf "ÀÎÀÚ°¡ ¾ø°í"); printf "@]@\n@[<4>- %s¿¡¼­´Â@ " s2; fopt sc2 (printf "%a ÀÔ´Ï´Ù." ps) (fn _ => printf "ÀÎÀÚ°¡ ¾ø½À´Ï´Ù") | Consexist c => printf "%aÀÇ@ µ¥ÀÌŸ ±¸¼ºÀÚ %s °¡@ %s¿¡´Â@ ¾ø±â ¶§¹®ÀÔ´Ï´Ù." pv t' c s2 | Consreq c => printf "%aÀÇ@ µ¥ÀÌŸ ±¸¼ºÀÚ %s °¡@ %s¿¡@ ¾ø±â ¶§¹®ÀÔ´Ï´Ù." pv t' c s1 | _ => printf "%aÀÌ@ ´Ù¸£±â ¶§¹®ÀÔ´Ï´Ù." pv t' end) end in Location.print Format.std_formatter l; printf "@[<6>¿À·ù:@ "; f t; printf "@]" end fun pp x = pp' x fun warning (x,l) = let fun message Overload = "Á¤¼öÇüÀ¸·Î °£ÁÖÇÕ´Ï´Ù." val ppf = Format.std_formatter val msg = "ÁÖÀÇ: "^message x val newlines = ref 0 (* val _ = for i=0; i