caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list]  Labels at the module level?
@ 2015-06-23  8:50 Oleg
  0 siblings, 0 replies; 6+ messages in thread
From: Oleg @ 2015-06-23  8:50 UTC (permalink / raw)
  To: caml-list; +Cc: asai


> I want to define a functor that accepts a module of sig:
> module type A_t = sig
>   val x : int
>   val y : int
> end
> but if the user did not specify some of the values in the signature,
> some default values will be used.

It is even easier at the module level: include comes in handy. Suppose
we have a consumer

module Universe(Params: A_t) = struct
  let r () = Printf.printf "x = %d y = %d\n" Params.x Params.y
end;;

First, we define defaults:

module Def : A_t = struct let x = 1 let y = 2 end;;

We can invoke it as

let module M = Universe(Def) in M.r ();;

and also as

let module M = Universe(struct include Def let x = 100 end) in M.r ();;

BTW, groups of related parameters could be further grouped into
submodules.


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

* Re: [Caml-list] Labels at the module level?
  2015-06-24  0:25 ` Kenichi Asai
@ 2015-06-26  8:12   ` Ben Millwood
  0 siblings, 0 replies; 6+ messages in thread
From: Ben Millwood @ 2015-06-26  8:12 UTC (permalink / raw)
  To: Kenichi Asai; +Cc: caml users

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

Instead of having Client and Server depend on Message, consider having
Check_message_types depend on both Client and Server, and in
check_message_types.ml you have:

let check_message_types_are_equal (x : Client.t) : Server.t = x

On 24 June 2015 at 01:25, Kenichi Asai <asai@is.ocha.ac.jp> wrote:

> Thanks for all the answers!  Let me ask one more (somewhat related)
> question.  I want to share a type of messages between a client and a
> server (in communicating games students write to avoid segmentation
> fault caused by marshaling or a runtime error caused by bin-prot).
> One natural way to do it is to have a file message.ml:
>
> message.ml:
> -----
> type t = int * int (* example type of messages *)
> -----
>
> and use it in both the client and the server:
>
> client.ml:
> -----
> let send (message : Message.t) =
>   ... marshal the message and send to the server ...
> -----
>
> server.ml:
> -----
> let receive () : Message.t =
>   ... unmarshal the message received from the client ...
> -----
>
> In this case, however, students always have to write message.ml and
> declare Message.t even if it can be automatically inferred from the
> code of client.ml and server.ml.  Ideally, I want to provide
> message.ml something like:
>
> message.ml:
> -----
> type t = '_a
> -----
>
> which signifies that the type Message.t can be anything as long as it
> is instantiated to exactly one type throughout the program (i.e., both
> in the client and the server).  Students could then write only
> client.ml and server.ml.  If they instantiate Message.t consistently,
> the program compiles fine; otherwise, compilation leads to a type error.
>
> Because the above definition of message.ml is not allowed, I defined:
>
> message.ml:
> -----
> type t = int (* dummy type *)
> -----
>
> and tried to override it:
>
> client.ml:
> -----
> include Message
> type t = int * int
> -----
>
> but it doesn't work.  Would there be any way to impose a constraint
> that two types in two different modules to be the same without
> specifying the type itself?
>
> Sincerely,
>
> --
> Kenichi Asai
>
> --
> Caml-list mailing list.  Subscription management and archives:
> https://sympa.inria.fr/sympa/arc/caml-list
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>

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

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

* Re: [Caml-list] Labels at the module level?
  2015-06-23  8:26 Kenichi Asai
  2015-06-23  8:43 ` Jeremy Yallop
@ 2015-06-24  0:25 ` Kenichi Asai
  2015-06-26  8:12   ` Ben Millwood
  1 sibling, 1 reply; 6+ messages in thread
From: Kenichi Asai @ 2015-06-24  0:25 UTC (permalink / raw)
  To: caml-list

Thanks for all the answers!  Let me ask one more (somewhat related)
question.  I want to share a type of messages between a client and a
server (in communicating games students write to avoid segmentation
fault caused by marshaling or a runtime error caused by bin-prot).
One natural way to do it is to have a file message.ml:

message.ml:
-----
type t = int * int (* example type of messages *)
-----

and use it in both the client and the server:

client.ml:
-----
let send (message : Message.t) =
  ... marshal the message and send to the server ...
-----

server.ml:
-----
let receive () : Message.t =
  ... unmarshal the message received from the client ...
-----

In this case, however, students always have to write message.ml and
declare Message.t even if it can be automatically inferred from the
code of client.ml and server.ml.  Ideally, I want to provide
message.ml something like:

message.ml:
-----
type t = '_a
-----

which signifies that the type Message.t can be anything as long as it
is instantiated to exactly one type throughout the program (i.e., both
in the client and the server).  Students could then write only
client.ml and server.ml.  If they instantiate Message.t consistently,
the program compiles fine; otherwise, compilation leads to a type error.

Because the above definition of message.ml is not allowed, I defined:

message.ml:
-----
type t = int (* dummy type *)
-----

and tried to override it:

client.ml:
-----
include Message
type t = int * int
-----

but it doesn't work.  Would there be any way to impose a constraint
that two types in two different modules to be the same without
specifying the type itself?

Sincerely,

-- 
Kenichi Asai

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

* Re: [Caml-list] Labels at the module level?
  2015-06-23  8:43 ` Jeremy Yallop
@ 2015-06-23  8:46   ` Benjamin Greenman
  0 siblings, 0 replies; 6+ messages in thread
From: Benjamin Greenman @ 2015-06-23  8:46 UTC (permalink / raw)
  Cc: Kenichi Asai, Caml List

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

Instead of making a functor, you could try a first-class module:

module type Person = sig
  val name : string
  val age : int
end

let make_person ?(name="anon") (age : int) : (module Person) =
  (module struct
    let name = name
    let age = age
  end)

On Tue, Jun 23, 2015 at 4:43 AM, Jeremy Yallop <yallop@gmail.com> wrote:

> On 23 June 2015 at 09:26, Kenichi Asai <asai@is.ocha.ac.jp> wrote:
> > Using labeled arguments, one can supply default values for unspecified
> > arguments:
> >
> > let f ?(x = 3) y = x * y
> > in f 5
> >
> > will return 15 without specifying the value of x at the second line.
> > Is there a way to do a similar thing at the module level?  Namely, I
> > want to define a functor that accepts a module of sig:
> >
> > module type A_t = sig
> >   val x : int
> >   val y : int
> > end
> >
> > but if the user did not specify some of the values in the signature,
> > some default values will be used.
> >
> > Background: in the universe library for OCaml:
> >
> > http://pllab.is.ocha.ac.jp/~asai/Universe/
> >
> > one specifies various handlers for events (such as tick, mouse, and
> > key events).  Currently, these handlers are registered to the big_bang
> > function that accepts them using labeled arguments (with default
> > values).  I wonder if I can specify the interface as a module
> > signature and let the user write a module of that signature but only
> > those handlers that the user is interested in.
>
> Alain Frisch once had a patch to add exactly what you're asking for as
> a language feature:
>
>      http://alain.frisch.fr/soft.html#patches   (Scroll to 'optional
> fields in modules')
>      http://alain.frisch.fr/info/patch-option-announce
>
> In the absence of such a feature, one approach is to use 'include' to
> provide the defaults:
>
>     module M
>       include M_defaults
>       let y = 3
>    end
>
> Jeremy.
>
> --
> Caml-list mailing list.  Subscription management and archives:
> https://sympa.inria.fr/sympa/arc/caml-list
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>

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

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

* Re: [Caml-list] Labels at the module level?
  2015-06-23  8:26 Kenichi Asai
@ 2015-06-23  8:43 ` Jeremy Yallop
  2015-06-23  8:46   ` Benjamin Greenman
  2015-06-24  0:25 ` Kenichi Asai
  1 sibling, 1 reply; 6+ messages in thread
From: Jeremy Yallop @ 2015-06-23  8:43 UTC (permalink / raw)
  To: Kenichi Asai; +Cc: Caml List

On 23 June 2015 at 09:26, Kenichi Asai <asai@is.ocha.ac.jp> wrote:
> Using labeled arguments, one can supply default values for unspecified
> arguments:
>
> let f ?(x = 3) y = x * y
> in f 5
>
> will return 15 without specifying the value of x at the second line.
> Is there a way to do a similar thing at the module level?  Namely, I
> want to define a functor that accepts a module of sig:
>
> module type A_t = sig
>   val x : int
>   val y : int
> end
>
> but if the user did not specify some of the values in the signature,
> some default values will be used.
>
> Background: in the universe library for OCaml:
>
> http://pllab.is.ocha.ac.jp/~asai/Universe/
>
> one specifies various handlers for events (such as tick, mouse, and
> key events).  Currently, these handlers are registered to the big_bang
> function that accepts them using labeled arguments (with default
> values).  I wonder if I can specify the interface as a module
> signature and let the user write a module of that signature but only
> those handlers that the user is interested in.

Alain Frisch once had a patch to add exactly what you're asking for as
a language feature:

     http://alain.frisch.fr/soft.html#patches   (Scroll to 'optional
fields in modules')
     http://alain.frisch.fr/info/patch-option-announce

In the absence of such a feature, one approach is to use 'include' to
provide the defaults:

    module M
      include M_defaults
      let y = 3
   end

Jeremy.

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

* [Caml-list] Labels at the module level?
@ 2015-06-23  8:26 Kenichi Asai
  2015-06-23  8:43 ` Jeremy Yallop
  2015-06-24  0:25 ` Kenichi Asai
  0 siblings, 2 replies; 6+ messages in thread
From: Kenichi Asai @ 2015-06-23  8:26 UTC (permalink / raw)
  To: caml-list

Using labeled arguments, one can supply default values for unspecified
arguments:

let f ?(x = 3) y = x * y
in f 5

will return 15 without specifying the value of x at the second line.
Is there a way to do a similar thing at the module level?  Namely, I
want to define a functor that accepts a module of sig:

module type A_t = sig
  val x : int
  val y : int
end

but if the user did not specify some of the values in the signature,
some default values will be used.

Background: in the universe library for OCaml:

http://pllab.is.ocha.ac.jp/~asai/Universe/

one specifies various handlers for events (such as tick, mouse, and
key events).  Currently, these handlers are registered to the big_bang
function that accepts them using labeled arguments (with default
values).  I wonder if I can specify the interface as a module
signature and let the user write a module of that signature but only
those handlers that the user is interested in.

-- 
Kenichi Asai

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

end of thread, other threads:[~2015-06-26  8:12 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-06-23  8:50 [Caml-list] Labels at the module level? Oleg
  -- strict thread matches above, loose matches on Subject: below --
2015-06-23  8:26 Kenichi Asai
2015-06-23  8:43 ` Jeremy Yallop
2015-06-23  8:46   ` Benjamin Greenman
2015-06-24  0:25 ` Kenichi Asai
2015-06-26  8:12   ` Ben Millwood

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