caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Expanding functors
@ 2015-08-27 13:29 Yotam Barnoy
  2015-08-27 13:33 ` Drup
  2015-08-27 13:35 ` Gabriel Scherer
  0 siblings, 2 replies; 4+ messages in thread
From: Yotam Barnoy @ 2015-08-27 13:29 UTC (permalink / raw)
  To: Ocaml Mailing List

[-- Attachment #1: Type: text/plain, Size: 955 bytes --]

One problem I've commonly encountered in OCaml is the inability to expand
the interface of functors after they've been created (I'm not talking about
post-application). For example, Map.Make in the stdlib takes an OrderedType
module which contains only the compare function. What happens if I want to
add something to this interface, such as a show function? I have to copy
the whole implementation of Map.Make into my own file to modify it. Compare
this to the ability to 'include' a regular module and just add the new
functionality, and to take the type of a module and expand that type as
needed. Functors are severely lacking in this regard.

What do people think of this idea -- of allowing functors to be expanded?
Ideally, expanding a functor would allow for both replacing its argument
type (as in the example I gave) and for adding a second/third functor
argument type (so Map.Make(OrderedType) would become
Map.Make(OrderedType)(Show).

-Yotam

[-- Attachment #2: Type: text/html, Size: 1058 bytes --]

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

* Re: [Caml-list] Expanding functors
  2015-08-27 13:29 [Caml-list] Expanding functors Yotam Barnoy
@ 2015-08-27 13:33 ` Drup
  2015-08-27 13:35 ` Gabriel Scherer
  1 sibling, 0 replies; 4+ messages in thread
From: Drup @ 2015-08-27 13:33 UTC (permalink / raw)
  To: Yotam Barnoy, Ocaml Mailing List

Le 27/08/2015 15:29, Yotam Barnoy a écrit :
> One problem I've commonly encountered in OCaml is the inability to 
> expand the interface of functors after they've been created (I'm not 
> talking about post-application). For example, Map.Make in the stdlib 
> takes an OrderedType module which contains only the compare function. 
> What happens if I want to add something to this interface, such as a 
> show function? I have to copy the whole implementation of Map.Make 
> into my own file to modify it. Compare this to the ability to 
> 'include' a regular module and just add the new functionality, and to 
> take the type of a module and expand that type as needed. Functors are 
> severely lacking in this regard.
>
> What do people think of this idea -- of allowing functors to be 
> expanded? Ideally, expanding a functor would allow for both replacing 
> its argument type (as in the example I gave) and for adding a 
> second/third functor argument type (so Map.Make(OrderedType) would 
> become Map.Make(OrderedType)(Show).
>
> -Yotam

module Make(O : Map.OrderedType) = struct
   include Map.Make(O)
   let foo = ....
end

You successfully extended Map without breaking anyone's code !


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

* Re: [Caml-list] Expanding functors
  2015-08-27 13:29 [Caml-list] Expanding functors Yotam Barnoy
  2015-08-27 13:33 ` Drup
@ 2015-08-27 13:35 ` Gabriel Scherer
  2015-08-27 14:17   ` Yotam Barnoy
  1 sibling, 1 reply; 4+ messages in thread
From: Gabriel Scherer @ 2015-08-27 13:35 UTC (permalink / raw)
  To: Yotam Barnoy; +Cc: Ocaml Mailing List

I don't follow. Doesn't

  module MakeBetter (K : sig include Map.OrderedType val show : t ->
string end) =
  struct
    include Map.Make(K)
    let show show_val m = List.show (Pair.show K.show show_val) (bindings m)
  end

suit your need?

The extensibility problem I see is that you cannot use the map's
internal representation because it is an abstract type -- which
prevents from efficiently implementing certain operations -- but this
is unrelated to being a functor.

On Thu, Aug 27, 2015 at 3:29 PM, Yotam Barnoy <yotambarnoy@gmail.com> wrote:
> One problem I've commonly encountered in OCaml is the inability to expand
> the interface of functors after they've been created (I'm not talking about
> post-application). For example, Map.Make in the stdlib takes an OrderedType
> module which contains only the compare function. What happens if I want to
> add something to this interface, such as a show function? I have to copy the
> whole implementation of Map.Make into my own file to modify it. Compare this
> to the ability to 'include' a regular module and just add the new
> functionality, and to take the type of a module and expand that type as
> needed. Functors are severely lacking in this regard.
>
> What do people think of this idea -- of allowing functors to be expanded?
> Ideally, expanding a functor would allow for both replacing its argument
> type (as in the example I gave) and for adding a second/third functor
> argument type (so Map.Make(OrderedType) would become
> Map.Make(OrderedType)(Show).
>
> -Yotam

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

* Re: [Caml-list] Expanding functors
  2015-08-27 13:35 ` Gabriel Scherer
@ 2015-08-27 14:17   ` Yotam Barnoy
  0 siblings, 0 replies; 4+ messages in thread
From: Yotam Barnoy @ 2015-08-27 14:17 UTC (permalink / raw)
  To: Gabriel Scherer; +Cc: Ocaml Mailing List

[-- Attachment #1: Type: text/plain, Size: 1821 bytes --]

OK the other option I was hoping for was that I was missing a simple
solution, and one was provided, so thank you!


On Thu, Aug 27, 2015 at 9:35 AM, Gabriel Scherer <gabriel.scherer@gmail.com>
wrote:

> I don't follow. Doesn't
>
>   module MakeBetter (K : sig include Map.OrderedType val show : t ->
> string end) =
>   struct
>     include Map.Make(K)
>     let show show_val m = List.show (Pair.show K.show show_val) (bindings
> m)
>   end
>
> suit your need?
>
> The extensibility problem I see is that you cannot use the map's
> internal representation because it is an abstract type -- which
> prevents from efficiently implementing certain operations -- but this
> is unrelated to being a functor.
>
> On Thu, Aug 27, 2015 at 3:29 PM, Yotam Barnoy <yotambarnoy@gmail.com>
> wrote:
> > One problem I've commonly encountered in OCaml is the inability to expand
> > the interface of functors after they've been created (I'm not talking
> about
> > post-application). For example, Map.Make in the stdlib takes an
> OrderedType
> > module which contains only the compare function. What happens if I want
> to
> > add something to this interface, such as a show function? I have to copy
> the
> > whole implementation of Map.Make into my own file to modify it. Compare
> this
> > to the ability to 'include' a regular module and just add the new
> > functionality, and to take the type of a module and expand that type as
> > needed. Functors are severely lacking in this regard.
> >
> > What do people think of this idea -- of allowing functors to be expanded?
> > Ideally, expanding a functor would allow for both replacing its argument
> > type (as in the example I gave) and for adding a second/third functor
> > argument type (so Map.Make(OrderedType) would become
> > Map.Make(OrderedType)(Show).
> >
> > -Yotam
>

[-- Attachment #2: Type: text/html, Size: 2415 bytes --]

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

end of thread, other threads:[~2015-08-27 14:18 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-08-27 13:29 [Caml-list] Expanding functors Yotam Barnoy
2015-08-27 13:33 ` Drup
2015-08-27 13:35 ` Gabriel Scherer
2015-08-27 14:17   ` Yotam Barnoy

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