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.5 required=5.0 tests=AWL 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 3BFA2BBAF for ; Fri, 22 Aug 2008 14:54:17 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AqgAAARUrkjRVdkPlWdsb2JhbACRYz4BAQEBCQMKBw+caocMAQKDJA X-IronPort-AV: E=Sophos;i="4.32,251,1217800800"; d="scan'208";a="16357021" Received: from mail-gx0-f15.google.com ([209.85.217.15]) by mail1-smtp-roc.national.inria.fr with ESMTP; 22 Aug 2008 14:54:16 +0200 Received: by gxk8 with SMTP id 8so1677946gxk.3 for ; Fri, 22 Aug 2008 05:54:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:from:reply-to:to:subject:date :user-agent:content-type:content-transfer-encoding :content-disposition:message-id; bh=79WLdX4XC0Jy8Dj7EF5bzN0+0fUFBkVZ7bBFGTnf7lU=; b=sMDDWLBsRIFuMULID88P/Cr7KqiOTrS/vx+7adx2N9LQXm7yez7f9E42zjzuEBkeVV sPZvalKC87vSLKGiBWFogjanqbDRt1KxFNB0tn1vdXFQODJ0dHWPJxGtrzz+ecia99g2 7VeHwVrC2ct4RoFHcuugfM06lkNOs3xI4Rbkw= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=from:reply-to:to:subject:date:user-agent:content-type :content-transfer-encoding:content-disposition:message-id; b=dkvENyk+8MRq/kB2D0vq+9znr3t0ZJ/lRFSv+D4/WLQlD7YjjjezyWjZud+Ec9xUls H4IHVtyTKKRo6ReG5Gt1mIZVD184Pja0NDeVzMjeKbHlC7+zlc7mhw/DkA/wafWvtjjL M/WJ7l+Skm9Q3zpusoywqWrLAtDtEwNPBzNDo= Received: by 10.150.121.3 with SMTP id t3mr1763762ybc.179.1219409655478; Fri, 22 Aug 2008 05:54:15 -0700 (PDT) Received: from lawn-143-215-204-204.lawn.gatech.edu ( [143.215.204.204]) by mx.google.com with ESMTPS id u25sm1226069ele.1.2008.08.22.05.54.13 (version=TLSv1/SSLv3 cipher=RC4-MD5); Fri, 22 Aug 2008 05:54:14 -0700 (PDT) From: Peng Zang Reply-To: peng.zang@gmail.com To: caml-list@yquem.inria.fr Subject: typeclasses in OCaml Date: Fri, 22 Aug 2008 08:54:09 -0400 User-Agent: KMail/1.9.7 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200808220854.12598.peng.zang@gmail.com> X-Spam: no; 0.00; ocaml:01 hash:01 ocaml:01 haskell:01 forall:01 bool:01 'self:01 'self:01 syntax:01 type-safe:01 peng:98 peng:98 polymorphic:01 objects:02 gnu:03 -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi, I have been using the Object system in OCaml for the equivalent functionality of haskell typeclasses for some time. It allows nice things like: class type ['a] foldable = object method fold : 'z. ('z -> 'a -> 'z) -> 'z -> 'z end let forall (f:'a -> bool) (obj:'a #foldable) = obj#fold (fun acc x -> acc && f x) true Recently however, I tried to make a "mappable" class. Something like: class type ['a] mappable = object('self) method map : 'z. ('a -> 'z) -> 'z 'self end Which of course is invalid syntax. However, I think the point is clear. I want to specify the type of all container objects that can map their contents. How do you specify this in OCaml? I realize there are some inherent issues, eg. polymorphic classes are invarient. So I'm willing to accept some limited use of magic as long as it can be hidden away and offer a type-safe interface. Nevertheless, after half a day of trying, I cannot seem to figure this out. I don't even know how to specify: " a 'z version of 'self " In a syntactically correct way. Is this possible? Thank in advance, Peng -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.7 (GNU/Linux) iD8DBQFIrrb0fIRcEFL/JewRAuDZAJ4zGo7+B6dF0YbnUFwQPGW+2MenZACfbAJ6 OKVIlsPpwkVwOEefT8KiRMU= =+8yF -----END PGP SIGNATURE-----