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: Denis Bueno <dbueno@g...>
Subject: Type error
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