caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Default value in module
@ 2017-09-12 20:00 Paul A. Steckler
  2017-09-12 21:17 ` SP
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Paul A. Steckler @ 2017-09-12 20:00 UTC (permalink / raw)
  To: caml-list

I found myself wanting to define several modules that implement a
module type, where all the implementations but one use the same value
of an item in the type. Essentially, I wanted a default value in the
module type, which I can override if needed. Maybe there's a way to do
this already.

If not, the type could look like:
--
module type Foo =
sig
  val ?x : int = 42
 end
--

and instances could be:

--
module Bar1 : Foo =
struct
  let x = 17
end

module Bar2 : Foo =
struct
end
--

Is this a reasonable idea?

More precisely, I wanted to take an existing module type, add an item
to it, and then edit just the instance with the non-default value. As
it was, I had to edit all the instances. I could have used the class
system to do something similar, but that would have required changing
even more code.

-- Paul

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

* Re: [Caml-list] Default value in module
  2017-09-12 20:00 [Caml-list] Default value in module Paul A. Steckler
@ 2017-09-12 21:17 ` SP
  2017-09-12 21:21 ` Yotam Barnoy
       [not found] ` <21484041-b7ed-33f0-4434-6817a82289fe@lexifi.com>
  2 siblings, 0 replies; 7+ messages in thread
From: SP @ 2017-09-12 21:17 UTC (permalink / raw)
  To: caml-list

Why not use a functor?

-- 
    SP

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

* Re: [Caml-list] Default value in module
  2017-09-12 20:00 [Caml-list] Default value in module Paul A. Steckler
  2017-09-12 21:17 ` SP
@ 2017-09-12 21:21 ` Yotam Barnoy
  2017-09-12 21:29   ` Paul A. Steckler
       [not found] ` <21484041-b7ed-33f0-4434-6817a82289fe@lexifi.com>
  2 siblings, 1 reply; 7+ messages in thread
From: Yotam Barnoy @ 2017-09-12 21:21 UTC (permalink / raw)
  To: Paul A. Steckler; +Cc: Ocaml Mailing List

The easiest thing to do is to create a prototype module with the value
you want, and to include that module (include Foo) in any module you
want to have that basic structure. You can even have as many prototype
modules as you want, so the system is extremely flexible.

On Tue, Sep 12, 2017 at 4:00 PM, Paul A. Steckler <steck@stecksoft.com> wrote:
> I found myself wanting to define several modules that implement a
> module type, where all the implementations but one use the same value
> of an item in the type. Essentially, I wanted a default value in the
> module type, which I can override if needed. Maybe there's a way to do
> this already.
>
> If not, the type could look like:
> --
> module type Foo =
> sig
>   val ?x : int = 42
>  end
> --
>
> and instances could be:
>
> --
> module Bar1 : Foo =
> struct
>   let x = 17
> end
>
> module Bar2 : Foo =
> struct
> end
> --
>
> Is this a reasonable idea?
>
> More precisely, I wanted to take an existing module type, add an item
> to it, and then edit just the instance with the non-default value. As
> it was, I had to edit all the instances. I could have used the class
> system to do something similar, but that would have required changing
> even more code.
>
> -- Paul
>
> --
> 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

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

* Re: [Caml-list] Default value in module
  2017-09-12 21:21 ` Yotam Barnoy
@ 2017-09-12 21:29   ` Paul A. Steckler
  2017-09-13 18:56     ` Martin Jambon
  0 siblings, 1 reply; 7+ messages in thread
From: Paul A. Steckler @ 2017-09-12 21:29 UTC (permalink / raw)
  To: Yotam Barnoy; +Cc: Ocaml Mailing List

On Tue, Sep 12, 2017 at 5:21 PM, Yotam Barnoy <yotambarnoy@gmail.com> wrote:
> The easiest thing to do is to create a prototype module with the value
> you want, and to include that module (include Foo) in any module you
> want to have that basic structure.

Yes, but that means I have to write "include Foo" to get the default
value. My goal is to avoid writing anything extra when I'm using the
default value.

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

* Re: [Caml-list] Default value in module
       [not found] ` <21484041-b7ed-33f0-4434-6817a82289fe@lexifi.com>
@ 2017-09-12 21:49   ` Paul A. Steckler
  2017-09-13 18:57     ` Martin Jambon
  0 siblings, 1 reply; 7+ messages in thread
From: Paul A. Steckler @ 2017-09-12 21:49 UTC (permalink / raw)
  To: Alain Frisch; +Cc: Ocaml Mailing List

On Tue, Sep 12, 2017 at 5:40 PM, Alain Frisch <alain.frisch@lexifi.com> wrote:
> What would be allowed as a default value is not completely clear (you don't
> want to carry runtime values with module types).

Yes, propagating the default value sounds like the main implementation obstacle.

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

* Re: [Caml-list] Default value in module
  2017-09-12 21:29   ` Paul A. Steckler
@ 2017-09-13 18:56     ` Martin Jambon
  0 siblings, 0 replies; 7+ messages in thread
From: Martin Jambon @ 2017-09-13 18:56 UTC (permalink / raw)
  To: caml-list

On 09/12/2017 02:29 PM, Paul A. Steckler wrote:
> On Tue, Sep 12, 2017 at 5:21 PM, Yotam Barnoy <yotambarnoy@gmail.com> wrote:
>> The easiest thing to do is to create a prototype module with the value
>> you want, and to include that module (include Foo) in any module you
>> want to have that basic structure.
> 
> Yes, but that means I have to write "include Foo" to get the default
> value. My goal is to avoid writing anything extra when I'm using the
> default value.

I personally find this solution quite reasonable. You'd write this:

module type A = sig
   val x : int
end

module A_defaults = struct
   let x = 42
end

module A1 : A = struct
   include A_defaults
   let x = 17
end

module A2 : A = struct
   include A_defaults
end

which is really nicely written as:

module A2 : A = A_defaults

Now if you have extra module items to add or to override, the cost of 
inserting the line `include A_defaults` becomes quickly negligible, as 
in the `B1` definition below:

module type B = sig
   val x : int
   val y : int
   val z : int
end

module B_defaults = struct
   let x = 42
   let y = 17
end

module B1 : B = struct
   include B_defaults
   let y = 21
   let z = -1
end

Martin

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

* Re: [Caml-list] Default value in module
  2017-09-12 21:49   ` Paul A. Steckler
@ 2017-09-13 18:57     ` Martin Jambon
  0 siblings, 0 replies; 7+ messages in thread
From: Martin Jambon @ 2017-09-13 18:57 UTC (permalink / raw)
  To: caml-list

On 09/12/2017 02:49 PM, Paul A. Steckler wrote:
> On Tue, Sep 12, 2017 at 5:40 PM, Alain Frisch <alain.frisch@lexifi.com> wrote:
>> What would be allowed as a default value is not completely clear (you don't
>> want to carry runtime values with module types).
> 
> Yes, propagating the default value sounds like the main implementation obstacle.

A possible solution inspired from atdgen is to support only expressions 
that come with no uncertain dependency and no "computation", i.e. 
literal constants of core types. Core types here would include unit, 
bool, int, float, string, and list/option of a core type, and a few more 
(int64 etc.) and would exclude mutable values, records, variants other 
than list/option, and objects.

Then we could write, using atd's conventions for `~` and `?`:

module type A = sig
   val ~x : int = 42 (* just a literal,
                        whose type can be inferred syntactically *)
end

module A1 : A = struct
   let x = 17
end

module A2 : A = struct
   (* implicit `let x = 42` *)
end

Additionally, we could also have canonical defaults:

bool: false
int: 0
float: 0.
string: ""
option: None
list: []

These being common and intuitive defaults, it makes sense to support 
them. So we could write:

module type A = sig
   val ~x : int
end

module A1 : A = struct
   let x = 17
end

module A3 : A = struct
   (* implicit `let x = 0` *)
end

I think this feature would be most useful if it was supported on record 
types.

A record definition equivalent to the module signature above would be:

type a = {
   ~x: int;         (* default value is 0 *)
   ~y: int = 42;    (* default value is 42 *)
   ?z: int option;  (* default value is None *)
}

let a1 : a = {}
   (* equals `{ x = 0; y = 42; z = None }` *)

Martin

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

end of thread, other threads:[~2017-09-13 18:57 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-09-12 20:00 [Caml-list] Default value in module Paul A. Steckler
2017-09-12 21:17 ` SP
2017-09-12 21:21 ` Yotam Barnoy
2017-09-12 21:29   ` Paul A. Steckler
2017-09-13 18:56     ` Martin Jambon
     [not found] ` <21484041-b7ed-33f0-4434-6817a82289fe@lexifi.com>
2017-09-12 21:49   ` Paul A. Steckler
2017-09-13 18:57     ` Martin Jambon

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