caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* type troubles with functors
@ 2005-06-22  4:53 Scott Duckworth
  2005-06-22  6:48 ` [Caml-list] " Pietro Abate
  0 siblings, 1 reply; 3+ messages in thread
From: Scott Duckworth @ 2005-06-22  4:53 UTC (permalink / raw)
  To: caml-list

I'm having trouble figuring out a way to represent functor modules as
types in a module type definition.  For lack of a better way of
explaining it, here's the problem I'm having in code:
*************************************
(* OrderedSet.mli *)
(* Ordered sets over ordered types *)

module type OrderedType = (* same as in .ml file *)
module type S = (* same as in .ml file *)
module Make (Ord : OrderedType) : S with type elt = Ord.t
*************************************
(* OrderedSet.ml *)
(* Ordered sets over ordered types *)

module type OrderedType = sig
	type t
	val compare : t -> t -> int
end

module type S = sig
	type elt
	type t
	val to_list : t -> elt list
	val to_set : t -> EltSet.t     (* PROBLEM *)
	(* ... *)
end

module Make (Ord : OrderedType) = struct
	module EltSet = Set.Make(Ord)      (* THIS IS WHAT I WANT TO RETURN *)
	type elt = Ord.t
	type t = { l : elt list; s : EltSet.t }
	let to_list s = s.l
	let to_set s = s.s    (* THIS IS WHERE I WANT TO RETURN A SET *)
	(* ... *)
end
*************************************
I realize that EltSet.t is not defined at the two points I have noted
a problem, I'm simply stating my intention.  The problem is that
OrderdSet.S does not know the OrderedSet.OrderedType that will be used
in OrderedSet.Make, but it does know that it will be of type Set.S.t. 
(This is one instance where I wish Set was implemented as a
parameterized type, not a functor.)

My question is, how can I correctly type to_set?  I have tried many
different configurations of Set.Make and Set.S, but none of them have
worked.  If anyone knows how this can be done in OCaml's type system,
please let me know.  Thanks!
-- 
Scott Duckworth


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] type troubles with functors
  2005-06-22  4:53 type troubles with functors Scott Duckworth
@ 2005-06-22  6:48 ` Pietro Abate
  2005-06-22  7:50   ` Virgile Prevosto
  0 siblings, 1 reply; 3+ messages in thread
From: Pietro Abate @ 2005-06-22  6:48 UTC (permalink / raw)
  To: caml-list, caml-list

what about 

module type OrderedType = sig
      type t
      val compare : t -> t -> int
end


module type S = sig
      type elt
      type t
      type set
      val to_list : t -> elt list
      val to_set : t -> set
end

module Make (Ord : OrderedType) : S = struct
        module EltSet = Set.Make(Ord)
        type elt = EltSet.elt
        type set = EltSet.t
        type t = { l : elt list; s : set }
        let to_list t = t.l
        let to_set t = t.s

end


I'm far to be an expert, but this (or something similar) should work.

p

-- 
++ Blog: http://blog.rsise.anu.edu.au/?q=pietro
++ 
++ "All great truths begin as blasphemies." -George Bernard Shaw
++ Please avoid sending me Word or PowerPoint attachments.
   See http://www.fsf.org/philosophy/no-word-attachments.html


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] type troubles with functors
  2005-06-22  6:48 ` [Caml-list] " Pietro Abate
@ 2005-06-22  7:50   ` Virgile Prevosto
  0 siblings, 0 replies; 3+ messages in thread
From: Virgile Prevosto @ 2005-06-22  7:50 UTC (permalink / raw)
  To: caml-list

Le 06/22/2005, à 04:48:36 PM, Pietro Abate a écrit:
> module type OrderedType = sig
>       type t
>       val compare : t -> t -> int
> end
> 
> 
> module type S = sig
>       type elt
>       type t
>       type set
>       val to_list : t -> elt list
>       val to_set : t -> set
> end
> 
> module Make (Ord : OrderedType) : S = struct

I guess it might be useful to have as signature
'S with type elt = Ord.t', otherwise the elements of the sets would be
abstract as well.
Another solution for the module type would be to export explicitely the
set module -with another type constraint stating that it manipulate
elements of the same type as S itself:

module type S = sig
    type elt
    module EltSet: Set.S with type elt = elt
    type t
    val to_list : t -> elt list
    val to_set: t -> EltSet.t
end

This way, you'll have access directly to all the functions provided in
Set.S, while with the above signature, you must write them explicitely
in the signature S in order to use them (but this also allows you to
restrict the operations that are allowed on values of EltSet.t, which is
not the case when exposing the whole module).



-- 
E tutto per oggi, a la prossima volta
Virgile


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2005-06-22  7:50 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-06-22  4:53 type troubles with functors Scott Duckworth
2005-06-22  6:48 ` [Caml-list] " Pietro Abate
2005-06-22  7:50   ` Virgile Prevosto

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).