From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail4-relais-sop.national.inria.fr (mail4-relais-sop.national.inria.fr [192.134.164.105]) by yquem.inria.fr (Postfix) with ESMTP id 8376FBC57 for ; Tue, 27 Apr 2010 10:55:59 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: Ao0AAAZD1kuLEwExkWdsb2JhbACcXQEBAQEJCwoHEQUdrm8Bjw0FhQw X-IronPort-AV: E=Sophos;i="4.52,279,1270418400"; d="scan'208";a="61476124" Received: from infao0809.mpi-sb.mpg.de (HELO hera.mpi-sb.mpg.de) ([139.19.1.49]) by mail4-smtp-sop.national.inria.fr with ESMTP/TLS/AES256-SHA; 27 Apr 2010 10:55:59 +0200 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mpi-sb.mpg.de; s=mail200803; h=Message-ID:In-Reply-To: References:Date:Subject:From:To:Cc:MIME-Version:Content-Type: Content-Transfer-Encoding; bh=NF7mtoY3ImGuU9yUZnWfissNEMamW44PSc DDZhRJjiQ=; b=aZyf5h9c0N7+8Zmu4PMXfVp233F9QugAo3vkTJhuwBdeioWmAu HFTMSZAF8KaQrX9waYjVnU5jBwk7BbSHbdGWzlLYXkXbSpu5zvUB1T2NlTXpK0LU dmY/6WnhbAdV4KiuP1gkFLeiywfm9ZJxqm9gk9VPeAKbhNStTmxf4WCro= Received: from maniac.mpi-sb.mpg.de ([139.19.1.26]:58283) by hera.mpi-sb.mpg.de (envelope-from ) with esmtp (Exim 4.69) id 1O6ga9-0003yb-1Y; Tue, 27 Apr 2010 10:55:58 +0200 Received: from www-data by maniac.mpi-sb.mpg.de with local (Exim 4.69) (envelope-from ) id 1O6ga8-0000le-Na; Tue, 27 Apr 2010 10:55:52 +0200 Received: from 80.146.185.83 (SquirrelMail authenticated user rossberg) by mail.mpi-sws.org with HTTP; Tue, 27 Apr 2010 10:55:52 +0200 (CEST) Message-ID: <92c59dfb53fa91a944f33c259638913d.squirrel@mail.mpi-sws.org> In-Reply-To: <4BD6A0D9.7070900@wp.pl> References: <4BD6A0D9.7070900@wp.pl> Date: Tue, 27 Apr 2010 10:55:52 +0200 (CEST) Subject: Re: [Caml-list] Extending Set - strange behavior of abstract type From: rossberg@mpi-sws.org To: "Dawid Toton" Cc: "caml-list" User-Agent: SquirrelMail/1.4.15 MIME-Version: 1.0 Content-Type: text/plain;charset=iso-8859-15 Content-Transfer-Encoding: 8bit X-Priority: 3 (Normal) Importance: Normal X-Spam: no; 0.00; rossberg:01 struct:01 ord:01 orderedtype:01 struct:01 ord:01 ocaml's:01 subset:01 functor:01 abbreviation:01 ocaml:01 functor:01 char:01 char:01 wrote:01 Dawid Toton wrote: > I tried to extend the standard Set module with new operations. I got > error messages about type incompatibilities (the Set.S.t as exposed by > my implementation and Set.S.t used by functions from the original Set). > I have reduced my code to the following small example: > > module Set = struct > module Make (Ord : Set.OrderedType) = struct > module Set = Set.Make(Ord) > include Set > end > end > > module OrdChar = struct type t = char let compare = compare end > module Raw1 = Set.Make (OrdChar) > module Raw2 = Set.Make (struct type t = char let compare = compare end) > > let aaa (aa : Raw1.t) (bb : Raw1.Set.t) = (aa = bb) > let aaa (aa : Raw2.t) (bb : Raw2.Set.t) = (aa = bb) > > Only the last line results in an error: > Error: This expression has type Raw2.Set.t but is here used with type > Raw2.t That is a known limitation of Ocaml's module system: type equivalence is only propagated through a syntactic subset of module expressions, so called "paths" (which consist of only module names, dot access, and functor applications). Roughly, in your example, Raw1.t is just an abbreviation for Set.Make(OrdChar).t, which in turn abbreviates Set.Make(OrdChar).Set.t, which expands to Set.Set.Make(OrdChar).t. In all these type names the bits before the ".t" are in path form, which makes the type expressions legal. Raw2.t, on the other hand, would expand to Set.Make(struct ... end).t - however, literal structures are not allowed in paths, so this type expression is illegal, and Ocaml simply "forgets" the equivalence. The workaround is never to apply a functor to an anonymous structure if you care about maximum type propagation. Just name it, which is easy enough in most cases. /Andreas