caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Yotam Barnoy <yotambarnoy@gmail.com>
To: Jeremy Yallop <yallop@gmail.com>
Cc: Ocaml Mailing List <caml-list@inria.fr>
Subject: Re: [Caml-list] Implicit module question
Date: Tue, 9 Sep 2014 11:53:27 -0400	[thread overview]
Message-ID: <CAN6ygOmhLekvcg8GXu+aSWYSpwkDtTxRDHOs1CwVFGN1hmwp9A@mail.gmail.com> (raw)
In-Reply-To: <CAAxsn=FCKAe=vXM_tqJhB3fKBWx3ZE=dnC75Oi7LZeDD5wLpNw@mail.gmail.com>

[-- 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 --]

      reply	other threads:[~2014-09-09 15:53 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-09-09 15:32 Yotam Barnoy
2014-09-09 15:47 ` Jeremy Yallop
2014-09-09 15:53   ` Yotam Barnoy [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAN6ygOmhLekvcg8GXu+aSWYSpwkDtTxRDHOs1CwVFGN1hmwp9A@mail.gmail.com \
    --to=yotambarnoy@gmail.com \
    --cc=caml-list@inria.fr \
    --cc=yallop@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).