caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Implicit module question
@ 2014-09-09 15:32 Yotam Barnoy
  2014-09-09 15:47 ` Jeremy Yallop
  0 siblings, 1 reply; 3+ messages in thread
From: Yotam Barnoy @ 2014-09-09 15:32 UTC (permalink / raw)
  To: Ocaml Mailing List

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

I loved the Implicit Module presentation, and I'm very happy for ocaml to
be moving from a fully dynamic first-class-module existential type model
(excluding functors of course) that can't be optimized to one where the
compiler is in control and can do significant static analysis. I have a
question about the implicit module implementation: Is there any way to
combine modules automatically? For example, suppose I have a Show implicit
module and an Ord implicit module, and a function receives both, and wants
to infer both functionalities for an incoming type so we can run both show
and compare on the same type. Does the current model cover such a use-case?
If not, is there any idea how to move in this direction? Automatic module
fusion perhaps?

Thanks,

Yotam

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

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

* Re: [Caml-list] Implicit module question
  2014-09-09 15:32 [Caml-list] Implicit module question Yotam Barnoy
@ 2014-09-09 15:47 ` Jeremy Yallop
  2014-09-09 15:53   ` Yotam Barnoy
  0 siblings, 1 reply; 3+ messages in thread
From: Jeremy Yallop @ 2014-09-09 15:47 UTC (permalink / raw)
  To: Yotam Barnoy; +Cc: Ocaml Mailing List

On 9 September 2014 16:32, Yotam Barnoy <yotambarnoy@gmail.com> wrote:
> I have a question about the implicit module implementation: Is there any way to
> combine modules automatically? For example, suppose I have a Show implicit
> module and an Ord implicit module, and a function receives both, and wants
> to infer both functionalities for an incoming type so we can run both show
> and compare on the same type. Does the current model cover such a use-case?

Yes, it does.  The implicits language is essentially the same as
OCaml's module language, so you can use constraints/equations on the
module types in the regular way.  For example, suppose you have module
types for SHOW and ORD together with corresponding top-level
functions:

  module type SHOW =
  sig
    type t
    val show : t -> string
  end

  let show (implicit Show : SHOW) (x : Show.t) = Show.show x

  module type ORD =
  sig
    type t
    val compare : t -> t -> int
  end

  let compare (implicit Ord : ORD) (x : Ord.t) (y : Ord.t) = Ord.compare x y

You can write a function which operates on a value with implicit
instances for both SHOW and ORD by adding some kind of type
constraint.  For example, you might write:

   let f (implicit Show: SHOW) (implicit Ord: ORD with type t =
Show.t) (x : Show.t) y =
     if compare x y < 0 then show x else show y

or perhaps

   let f (type a) (implicit Show: SHOW with type t = a) (implicit Ord:
ORD with type t = a) (x : a) y =
     if compare x y < 0 then show x else show y

The inferred type is just as you'd expect:

  val f :
    (implicit Show : SHOW with type t = 'a) ->
    (implicit Ord : ORD with type t = 'a) ->
    'a -> 'a -> string

You might like to try out the implementation for yourself, either via
the opam switch or via the IOCaml top-level that Andrew Ray's set up:

   http://andrewray.github.io/iocamljs/modimp_show.html

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

* Re: [Caml-list] Implicit module question
  2014-09-09 15:47 ` Jeremy Yallop
@ 2014-09-09 15:53   ` Yotam Barnoy
  0 siblings, 0 replies; 3+ messages in thread
From: Yotam Barnoy @ 2014-09-09 15:53 UTC (permalink / raw)
  To: Jeremy Yallop; +Cc: Ocaml Mailing List

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

Amazing! This is SO cool. I'll definitely give it a try.

So here's my followup question: what will it take to have implicits
everywhere so we can abolish (ie. deprecate) all polymorphic functions? Is
this feasible? Is it as simple as Pervasives declaring implicits for Ord,
Eq, Show etc for the basic types?

On Tue, Sep 9, 2014 at 11:47 AM, Jeremy Yallop <yallop@gmail.com> wrote:

> On 9 September 2014 16:32, Yotam Barnoy <yotambarnoy@gmail.com> wrote:
> > I have a question about the implicit module implementation: Is there any
> way to
> > combine modules automatically? For example, suppose I have a Show
> implicit
> > module and an Ord implicit module, and a function receives both, and
> wants
> > to infer both functionalities for an incoming type so we can run both
> show
> > and compare on the same type. Does the current model cover such a
> use-case?
>
> Yes, it does.  The implicits language is essentially the same as
> OCaml's module language, so you can use constraints/equations on the
> module types in the regular way.  For example, suppose you have module
> types for SHOW and ORD together with corresponding top-level
> functions:
>
>   module type SHOW =
>   sig
>     type t
>     val show : t -> string
>   end
>
>   let show (implicit Show : SHOW) (x : Show.t) = Show.show x
>
>   module type ORD =
>   sig
>     type t
>     val compare : t -> t -> int
>   end
>
>   let compare (implicit Ord : ORD) (x : Ord.t) (y : Ord.t) = Ord.compare x
> y
>
> You can write a function which operates on a value with implicit
> instances for both SHOW and ORD by adding some kind of type
> constraint.  For example, you might write:
>
>    let f (implicit Show: SHOW) (implicit Ord: ORD with type t =
> Show.t) (x : Show.t) y =
>      if compare x y < 0 then show x else show y
>
> or perhaps
>
>    let f (type a) (implicit Show: SHOW with type t = a) (implicit Ord:
> ORD with type t = a) (x : a) y =
>      if compare x y < 0 then show x else show y
>
> The inferred type is just as you'd expect:
>
>   val f :
>     (implicit Show : SHOW with type t = 'a) ->
>     (implicit Ord : ORD with type t = 'a) ->
>     'a -> 'a -> string
>
> You might like to try out the implementation for yourself, either via
> the opam switch or via the IOCaml top-level that Andrew Ray's set up:
>
>    http://andrewray.github.io/iocamljs/modimp_show.html
>

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

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

end of thread, other threads:[~2014-09-09 15:53 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-09-09 15:32 [Caml-list] Implicit module question Yotam Barnoy
2014-09-09 15:47 ` Jeremy Yallop
2014-09-09 15:53   ` 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).