caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: rossberg@mpi-sws.org
To: "Dawid Toton" <d0@wp.pl>
Cc: "caml-list" <caml-list@yquem.inria.fr>
Subject: Re: [Caml-list] Extending Set - strange behavior of abstract type
Date: Tue, 27 Apr 2010 10:55:52 +0200 (CEST)	[thread overview]
Message-ID: <92c59dfb53fa91a944f33c259638913d.squirrel@mail.mpi-sws.org> (raw)
In-Reply-To: <4BD6A0D9.7070900@wp.pl>

Dawid Toton <d0@wp.pl> 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


  reply	other threads:[~2010-04-27  8:55 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-04-27  8:31 Dawid Toton
2010-04-27  8:55 ` rossberg [this message]
2010-04-27 14:40 ` Sylvain Le Gall

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=92c59dfb53fa91a944f33c259638913d.squirrel@mail.mpi-sws.org \
    --to=rossberg@mpi-sws.org \
    --cc=caml-list@yquem.inria.fr \
    --cc=d0@wp.pl \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).