Version française
Home     About     Download     Resources     Contact us    
Browse thread
How do I abstract out polymorphic variants via functors?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Jacques Garrigue <garrigue@m...>
Subject: Re: [Caml-list] How do I abstract out polymorphic variants via functors?
From: Jacques Carette <carette@mcmaster.ca>
Subject: [Caml-list] How do I abstract out polymorphic variants via functors?
Date: Fri, 18 Aug 2006 09:35:34 -0400

> I have 3 module types, each of which start out "the same", viz:
> module type DETERMINANT = sig
>   type 'a lstate
>   type 'a tag_lstate = [`TDet of 'a lstate ]
>   type ('b,'v) lm = ('a,'v,'s,'w) cmonad
>     constraint 's = [> 'a tag_lstate]
>     constraint 'b = 'a * 's * 'w
>   ...
> end
> module type RANK = sig
>   type 'a lstate = ('a, int ref) code
>   type 'a tag_lstate = [`TRan of 'a lstate ]
>   type ('b,'v) lm = ('a,'v,'s,'w) cmonad
>     constraint 's = [> 'a tag_lstate]
>     constraint 'b = 'a * 's * 'w
>   ...
> end
[...]
> The only thing different between those 3 is the name of the polymorphic 
> variant - so clearly an opportunity for abstraction!  But I can't seem 
> to achieve this.  Is there a way?

Not currently.
The problem is that [> 'a tag_lstate] is only defined when tag_lstate
has a concrete definition. So if you abstract tag_lstate, you cannot
keep this constraint. Private row types won't help either, since they
can only be implemented by structurally monomorphic types. So I see no
way out, at least with this encoding. Another approach might be to add
variance annotations, and use subtyping rather than polymorphism (but
I don't know whether it makes sense here.)

Jacques Garrigue