From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by yquem.inria.fr (Postfix) with ESMTP id 58038BBC4 for ; Thu, 5 Mar 2009 12:24:20 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: ArICACBFr0mAsMCNi2dsb2JhbACVAQEBARUKGMMChAgG X-IronPort-AV: E=Sophos;i="4.38,306,1233529200"; d="scan'208";a="25106119" Received: from secmail.uni-muenster.de ([128.176.192.141]) by mail1-smtp-roc.national.inria.fr with ESMTP; 05 Mar 2009 12:24:20 +0100 Received: from [212.144.109.133] (dialin-212-144-109-133.pools.arcor-ip.net [212.144.109.133]) by SECMAIL.UNI-MUENSTER.DE (Postfix) with ESMTP id DDD13BF416; Thu, 5 Mar 2009 12:24:09 +0100 (CET) In-Reply-To: References: <91a2ba3e0903031340wcdc976cp52522eb35f7ccb73@mail.gmail.com> <200903040159.48574.jon@ffconsultancy.com> <200903040920.01990.peng.zang@gmail.com> Mime-Version: 1.0 (Apple Message framework v753.1) Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed Message-Id: <1D555416-544B-4E94-A96C-0E194ED5E27D@uni-muenster.de> Cc: Caml Mailing List Content-Transfer-Encoding: 7bit From: Wolfgang Lux Subject: Re: [Caml-list] stl? Date: Thu, 5 Mar 2009 12:24:08 +0100 To: Brian Hurt X-Mailer: Apple Mail (2.753.1) X-Spam: no; 0.00; wlux:01 uni-muenster:01 stl:01 haskell:01 sexp:01 sexp:01 haskell:01 foo:01 wrote:01 caml-list:01 int:01 int:01 data:02 wolfgang:02 wolfgang:02 Brian Hurt wrote: > [...] > Here are two real problems I've hit with type classes, in only a > few weeks banging around in Haskell. > > For example, you can't have more than one instance of a type class > for any given type. So let's say you want to have a type class for > things that can be converted to and from s-expressions, like: > data Sexp = > Atom String > | List [ Sexp ] > > class Sexpable a where > toSexp :: a -> Sexp > fromSexp :: Sexp -> Maybe a > > So then you start throwing out the standard instances, among > others, you do one for string: > instance Sexpable String where > toSexp s = Atom s > fromSexp (Atom s) = Just s > fromSexp _ = Nothing > > and, a little while later, one for generic lists: > instance Sexpable a => Sexpable [ a ] where > toSexp xs = List $ map toSexp xs > fromSexp (List xs) = mapM fromSexp xs > fromSexp _ = Nothing > > Opps! Now you have conflicting instances for type String- one the > explicit implementation, and one via the generic list. There are > kludges to get around this, but they cause their own problems > (Brian's first law of programming: kludges multiply). A common one > is to add the function toSexpList and fromSexpList to the > fundamental type class, and drop the generic list implementation. Just to fair to Haskell here, you should not drop the list instance altogether, but rather redefine it to use the new methods. instance Sexpable a => Sexpable [ a ] where toSexp = toSexpList fromSexp = fromSexpList Thus, the purported problem with Map Int ([Int], Foo) is void. Wolfgang