caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "Jonathan Roewen" <jonathan.roewen@gmail.com>
To: "Denis Bueno" <dbueno@gmail.com>
Cc: "OCaml Mailing List" <caml-list@inria.fr>
Subject: Re: [Caml-list] Type error
Date: Sun, 15 Oct 2006 16:40:52 +1300	[thread overview]
Message-ID: <ad8cfe7e0610142040i634ea4f6gb4d7e128d5e886ab@mail.gmail.com> (raw)
In-Reply-To: <6dbd4d000610142029m92d7005v65e95f031e7eae9b@mail.gmail.com>

How about not defining a default, and matching on getter as an option?

let of_array ?getter xs = match getter with
| None -> Array.fold_right (fun x set -> add x set) xs empty
| Some f -> Array.fold_right (fun x set -> add (f x) set) xs empty

Jonathan


On 10/15/06, Denis Bueno <dbueno@gmail.com> wrote:
> I'm writing a simple (stupid) de-functorised Set library. Its skeleton is:
>
> ,----
> | type 'a t
> |   (** The type of sets. *)
> |
> | val empty: 'a t
> |   (** The empty set. *)
> |
> | val mem: 'a -> 'a t -> bool
> |   (** [mem x s] tests whether [x] belongs to the set [s]. *)
> |
> | val add: 'a -> 'a t -> 'a t
> |   (** [add x s] returns a set containing all elements of [s],
> |       plus [x]. If [x] was already in [s], [s] is returned unchanged. *)
> `----
>
> In my implementation, sets are lists.
>
> One of the things I'd like to do is write an of_array to create an
> array from a set. However, it is often the case that I have an array
> of objects from which I'd like to pull out one field & make a set of
> those fields. So, my of_array looks like this:
>
> Interface:
> ,----
> | val of_array: ?getter : ('b -> 'a) -> 'b array -> 'a t
> |   (** [of_array xs] returns a set containing the elements in [xs]. *)
> `----
>
> Implementation:
> ,----
> | let of_array ?(getter = fun x -> x) xs =
> |   Array.fold_right (fun x set -> add (getter x) set) xs empty;;
> `----
>
> I get a type error when trying to compile this. I don't know how to
> fix it, or even if it's possible without wrecking my nice of_array
> signature. The type error:
>
> ,----
> | The implementation pSet.ml does not match the interface pSet.cmi:
> | Values do not match:
> |   val of_array : ?getter:('a -> 'a) -> 'a array -> 'a list
> | is not included in
> |   val of_array : ?getter:('a -> 'b) -> 'a array -> 'b t
> `----
>
> I understand why this shouldn't be compilable, but, is it possible to
> do something similarly elegant (without creating a separate function)?
>
> -Denis
>
> _______________________________________________
> Caml-list mailing list. Subscription management:
> http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
> Archives: http://caml.inria.fr
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>


  reply	other threads:[~2006-10-15  3:40 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-10-15  3:29 Denis Bueno
2006-10-15  3:40 ` Jonathan Roewen [this message]
2006-10-15  3:48 ` [Caml-list] " skaller
2006-10-15  3:54 ` Jonathan Roewen
2006-10-15 11:26 ` Etienne Miret
2006-10-15 23:37 ` Jacques Garrigue
  -- strict thread matches above, loose matches on Subject: below --
2008-04-01  2:27 type error Jacques Le Normand
2008-04-01  2:42 ` [Caml-list] " Jacques Garrigue
2008-03-23 22:01 Jacques Le Normand
2008-03-23 22:19 ` [Caml-list] " Martin Jambon
2005-04-02  1:29 Type error Jon Harrop
2005-04-02  7:50 ` [Caml-list] " Jacques Garrigue

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=ad8cfe7e0610142040i634ea4f6gb4d7e128d5e886ab@mail.gmail.com \
    --to=jonathan.roewen@gmail.com \
    --cc=caml-list@inria.fr \
    --cc=dbueno@gmail.com \
    /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).