Version française
Home     About     Download     Resources     Contact us    
Browse thread
Type error
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Jonathan Roewen <jonathan.roewen@g...>
Subject: Re: [Caml-list] Type error
Oops, I should've tested first -- still same problem with the explicit
matching :-) I think you either have to always 'map', or never 'map'.

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
>