caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Defining a family of functors
@ 2009-01-26 15:08 Michaël Grünewald
  2009-01-26 15:15 ` [Caml-list] " Jacques Carette
  0 siblings, 1 reply; 11+ messages in thread
From: Michaël Grünewald @ 2009-01-26 15:08 UTC (permalink / raw)
  To: caml-list

I am facing a situation that could be solved by ``defining a family of 
functors'', I describe the problem and would be very glad to get your 
views about it.

To illustrate the situation, I will suppose A1, ..., An are modules 
implementing the same signature S, let's say S contains the usual 
opertations on groups (mathematical groups, you can replace this by 
vector spaces, or whatever). It is easy to write a Product2 functor

Product2: A1:S -> A2:S -> S2

producing an implementation for the direct product (with signature S) of 
the groups A1 and A2, plus injections j1 : A1.t -> S2.t and j2: A2.t -> 
S2.t and corresponding projections (the type t denotes the module 
thingie, as usual). This extension of S is here written S2.

My problem is ``how do I remove the 2'' ? Would it be possible to define 
a functor scheme [:)]

Productn: A1:S -> .. -> An:S -> Sn

able to instantiate concrete functors for any value of n ?

It is possible to generate automatically appropriate code for small 
values of n, but I am looking for a better approach. It is simple to get 
close of the solution in the object paradigm, since I can simply put 
objects in an array and iterate over this array (IIRC this is the 
aggregate design pattern), but I do not see an easy way to define 
injections and projections.
-- 
Thanks for your suggestions,
Michaël


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

* Re: [Caml-list] Defining a family of functors
  2009-01-26 15:08 Defining a family of functors Michaël Grünewald
@ 2009-01-26 15:15 ` Jacques Carette
  2009-01-27 14:30   ` Andrej Bauer
  0 siblings, 1 reply; 11+ messages in thread
From: Jacques Carette @ 2009-01-26 15:15 UTC (permalink / raw)
  To: Michaël Grünewald; +Cc: caml-list

Without the projections, you can do it via a higher-order Functor which 
does a fold, ie lifting to the Functor level what one usually does to 
get an n-ary product out of a binary product.

With the projections, as this involves name-generation, I don't see how 
to do it without using camlp4.  Even my favourite sledgehammer, 
metocaml, can't help here.

Jacques

Michaël Grünewald wrote:
> I am facing a situation that could be solved by ``defining a family of 
> functors'', I describe the problem and would be very glad to get your 
> views about it.
>
> To illustrate the situation, I will suppose A1, ..., An are modules 
> implementing the same signature S, let's say S contains the usual 
> opertations on groups (mathematical groups, you can replace this by 
> vector spaces, or whatever). It is easy to write a Product2 functor
>
> Product2: A1:S -> A2:S -> S2
>
> producing an implementation for the direct product (with signature S) 
> of the groups A1 and A2, plus injections j1 : A1.t -> S2.t and j2: 
> A2.t -> S2.t and corresponding projections (the type t denotes the 
> module thingie, as usual). This extension of S is here written S2.
>
> My problem is ``how do I remove the 2'' ? Would it be possible to 
> define a functor scheme [:)]
>
> Productn: A1:S -> .. -> An:S -> Sn
>
> able to instantiate concrete functors for any value of n ?
>
> It is possible to generate automatically appropriate code for small 
> values of n, but I am looking for a better approach. It is simple to 
> get close of the solution in the object paradigm, since I can simply 
> put objects in an array and iterate over this array (IIRC this is the 
> aggregate design pattern), but I do not see an easy way to define 
> injections and projections.


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

* Re: [Caml-list] Defining a family of functors
  2009-01-26 15:15 ` [Caml-list] " Jacques Carette
@ 2009-01-27 14:30   ` Andrej Bauer
  2009-01-27 14:47     ` Jacques Carette
  0 siblings, 1 reply; 11+ messages in thread
From: Andrej Bauer @ 2009-01-27 14:30 UTC (permalink / raw)
  To: caml-list

Because of problems like these I sometimes wish I had first-class
modules. I wonder how different that is from having first-class
polymorphic records, actually. Perhaps a bit of camlp4 to wrap up
records in module-like notation would do the trick.

Andrej


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

* Re: [Caml-list] Defining a family of functors
  2009-01-27 14:30   ` Andrej Bauer
@ 2009-01-27 14:47     ` Jacques Carette
  2009-01-27 21:12       ` David Teller
  0 siblings, 1 reply; 11+ messages in thread
From: Jacques Carette @ 2009-01-27 14:47 UTC (permalink / raw)
  To: Andrej Bauer; +Cc: caml-list

Andrej Bauer wrote:
> Because of problems like these I sometimes wish I had first-class
> modules. I wonder how different that is from having first-class
> polymorphic records, actually. Perhaps a bit of camlp4 to wrap up
> records in module-like notation would do the trick.
>   
If your modules do not define new types, then the main difference is 
that of name spaces (records pollute the global namespace, modules 
define local namespaces).  However, I could not get far with modules 
that only define new values (functions).

Bottom line: I too very much wish for first-class, higher-order 
modules.  As O'Caml already has open and closed products (viz rows and 
records), open and closed sums (viz polymorphic and 'normal' variants), 
the resulting system could steal back the 'elegant' monicker that has 
drifted towards Haskell.

Jacques


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

* Re: [Caml-list] Defining a family of functors
  2009-01-27 14:47     ` Jacques Carette
@ 2009-01-27 21:12       ` David Teller
  2009-01-28  0:32         ` Nicolas Pouillard
  0 siblings, 1 reply; 11+ messages in thread
From: David Teller @ 2009-01-27 21:12 UTC (permalink / raw)
  To: Jacques Carette; +Cc: Andrej Bauer, caml-list

I'd like that, too. I may be wrong but I have the impression that most
of this can already be done with the current type system of OCaml.

Unless I'm mistaken, for first-class modules, you essentially need
* extendable records (aka objects, good thing we already have them)
* existential types (which may be encoded with universal types, and
since we have universal types in classes, there may be a way to to this
already)
* namespace (which I'm sure could be encoded somehow).

Now, the syntax would certainly be awful, but if I'm right it wouldn't
take too much to get these modules into the compiler.

Cheers,
 David

On Tue, 2009-01-27 at 09:47 -0500, Jacques Carette wrote:
> Bottom line: I too very much wish for first-class, higher-order 
> modules.  As O'Caml already has open and closed products (viz rows and 
> records), open and closed sums (viz polymorphic and 'normal' variants), 
> the resulting system could steal back the 'elegant' monicker that has 
> drifted towards Haskell.
> 
> Jacques



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

* Re: [Caml-list] Defining a family of functors
  2009-01-27 21:12       ` David Teller
@ 2009-01-28  0:32         ` Nicolas Pouillard
  2009-01-28 11:25           ` David Teller
  0 siblings, 1 reply; 11+ messages in thread
From: Nicolas Pouillard @ 2009-01-28  0:32 UTC (permalink / raw)
  To: David Teller; +Cc: Jacques Carette, caml-list, Andrej Bauer

Excerpts from David Teller's message of Tue Jan 27 22:12:59 +0100 2009:
> I'd like that, too. I may be wrong but I have the impression that most
> of this can already be done with the current type system of OCaml.
> 
> Unless I'm mistaken, for first-class modules, you essentially need
> * extendable records (aka objects, good thing we already have them)

> * existential types (which may be encoded with universal types, and
> since we have universal types in classes, there may be a way to to this
> already)

The encoding of modules using existential types in non modular, this
basically means that you have to heavily transform the source.

What one need to encode modules is "open" existential types, this well
and clearly explained in this POPL'09 paper:

  «Modeling Abstract Types in Modules with Open Existential Types»,
    by Benoît Montagu and Didier Rémy

> * namespace (which I'm sure could be encoded somehow).
> 
> Now, the syntax would certainly be awful, but if I'm right it wouldn't
> take too much to get these modules into the compiler.

Kind regards,

-- 
Nicolas Pouillard


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

* Re: [Caml-list] Defining a family of functors
  2009-01-28  0:32         ` Nicolas Pouillard
@ 2009-01-28 11:25           ` David Teller
  2009-01-29  9:37             ` nicolas.pouillard
  0 siblings, 1 reply; 11+ messages in thread
From: David Teller @ 2009-01-28 11:25 UTC (permalink / raw)
  To: Nicolas Pouillard; +Cc: Jacques Carette, caml-list, Andrej Bauer

On Wed, 2009-01-28 at 01:32 +0100, Nicolas Pouillard wrote:
> The encoding of modules using existential types in non modular, this
> basically means that you have to heavily transform the source.
> 
> What one need to encode modules is "open" existential types, this well
> and clearly explained in this POPL'09 paper:
> 
>   «Modeling Abstract Types in Modules with Open Existential Types»,
>     by Benoît Montagu and Didier Rémy

Yes, I was just reading that paper. However, it is my impression that we
could get away without open existential types, at the cost of reduced
features.

On the other hand, it was pointed to me that Alain already wrote a
compiler patch implementing first-class modules.

Cheers,
 David


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

* Re: [Caml-list] Defining a family of functors
  2009-01-28 11:25           ` David Teller
@ 2009-01-29  9:37             ` nicolas.pouillard
  2009-01-29 10:40               ` David Allsopp
  0 siblings, 1 reply; 11+ messages in thread
From: nicolas.pouillard @ 2009-01-29  9:37 UTC (permalink / raw)
  To: David.Teller; +Cc: Jacques Carette, caml-list, Andrej Bauer

Excerpts from David Teller's message of Wed Jan 28 12:25:26 +0100 2009:
> On Wed, 2009-01-28 at 01:32 +0100, Nicolas Pouillard wrote:
> > The encoding of modules using existential types in non modular, this
> > basically means that you have to heavily transform the source.
> > 
> > What one need to encode modules is "open" existential types, this well
> > and clearly explained in this POPL'09 paper:
> > 
> >   «Modeling Abstract Types in Modules with Open Existential Types»,
> >     by Benoît Montagu and Didier Rémy
> 
> Yes, I was just reading that paper. However, it is my impression that we
> could get away without open existential types, at the cost of reduced
> features.
> 
> On the other hand, it was pointed to me that Alain already wrote a
> compiler patch implementing first-class modules.

That's right.

-- 
Nicolas Pouillard


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

* RE: [Caml-list] Defining a family of functors
  2009-01-29  9:37             ` nicolas.pouillard
@ 2009-01-29 10:40               ` David Allsopp
  2009-01-29 21:14                 ` Mauricio Fernandez
  0 siblings, 1 reply; 11+ messages in thread
From: David Allsopp @ 2009-01-29 10:40 UTC (permalink / raw)
  To: 'nicolas.pouillard', 'David.Teller'
  Cc: 'Jacques Carette', 'caml-list', 'Andrej Bauer'

> > On the other hand, it was pointed to me that Alain already wrote a
> > compiler patch implementing first-class modules.
> 
> That's right.

I have two questions - the first of which will probably demonstrate my lack of skill with the OCaml module system.

1. Does first-class modules, as with first-class functions mean that you can pass them around in code? For example, I have a web-app where all of the stuff which has to access databases is put into a single module with a known signature DB. I then have two modules MSSQLDriver and PGSQLDriver which implement the database functions for two different back-ends and have in DB.ml

module DB = MSSQLDriver
include DB

So when I build for Postgres, I just change the module statement and recompile. Would first class modules allow me to read the back-end configuration from a config file and write something like:

module DB = if Config.driver = `MSSQL then MSSQLDriver else PGSQLDriver

Or is it about something totally different? :o)

2. Is this patch intended for a future version of OCaml or just for research purposes?


David


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

* Re: [Caml-list] Defining a family of functors
  2009-01-29 10:40               ` David Allsopp
@ 2009-01-29 21:14                 ` Mauricio Fernandez
  2009-01-30  8:25                   ` Michaël Grünewald
  0 siblings, 1 reply; 11+ messages in thread
From: Mauricio Fernandez @ 2009-01-29 21:14 UTC (permalink / raw)
  To: caml-list

On Thu, Jan 29, 2009 at 10:40:50AM -0000, David Allsopp wrote:
> > > On the other hand, it was pointed to me that Alain already wrote a
> > > compiler patch implementing first-class modules.
> > 
> > That's right.
> 
> I have two questions - the first of which will probably demonstrate my lack
> of skill with the OCaml module system.
> 
> 1. Does first-class modules, as with first-class functions mean that you can
> pass them around in code? For example, I have a web-app where all of the
> stuff which has to access databases is put into a single module with a known
> signature DB. I then have two modules MSSQLDriver and PGSQLDriver which
> implement the database functions for two different back-ends and have in
> DB.ml
> 
> module DB = MSSQLDriver
> include DB
> 
> So when I build for Postgres, I just change the module statement and
> recompile. Would first class modules allow me to read the back-end
> configuration from a config file and write something like:
> 
> module DB = if Config.driver = `MSSQL then MSSQLDriver else PGSQLDriver
> 
> Or is it about something totally different? :o)

See this page:

http://ocaml.janestreet.com/?q=node/29#comment-77

-- 
Mauricio Fernandez  -   http://eigenclass.org


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

* Re: [Caml-list] Defining a family of functors
  2009-01-29 21:14                 ` Mauricio Fernandez
@ 2009-01-30  8:25                   ` Michaël Grünewald
  0 siblings, 0 replies; 11+ messages in thread
From: Michaël Grünewald @ 2009-01-30  8:25 UTC (permalink / raw)
  To: caml-list

Mauricio Fernandez a écrit :
> See this page:
>
> http://ocaml.janestreet.com/?q=node/29#comment-77
Thank you for this reference. I would also like to thank all 
contributors for their input.

It seems that my best bet is to go with camlp4/camlp4.
-- 
Kind regards,
Michaël


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

end of thread, other threads:[~2009-01-30  8:25 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-01-26 15:08 Defining a family of functors Michaël Grünewald
2009-01-26 15:15 ` [Caml-list] " Jacques Carette
2009-01-27 14:30   ` Andrej Bauer
2009-01-27 14:47     ` Jacques Carette
2009-01-27 21:12       ` David Teller
2009-01-28  0:32         ` Nicolas Pouillard
2009-01-28 11:25           ` David Teller
2009-01-29  9:37             ` nicolas.pouillard
2009-01-29 10:40               ` David Allsopp
2009-01-29 21:14                 ` Mauricio Fernandez
2009-01-30  8:25                   ` Michaël Grünewald

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