From: Jeremy Yallop <yallop@gmail.com>
To: Yotam Barnoy <yotambarnoy@gmail.com>
Cc: Ocaml Mailing List <caml-list@inria.fr>
Subject: Re: [Caml-list] Implicit module question
Date: Tue, 9 Sep 2014 16:47:02 +0100 [thread overview]
Message-ID: <CAAxsn=FCKAe=vXM_tqJhB3fKBWx3ZE=dnC75Oi7LZeDD5wLpNw@mail.gmail.com> (raw)
In-Reply-To: <CAN6ygOkMTDYLbfZrL2rSBFK15psESCgEnoeDPMBXWsLt8HCEhQ@mail.gmail.com>
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
next prev parent reply other threads:[~2014-09-09 15:47 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 [this message]
2014-09-09 15:53 ` Yotam Barnoy
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='CAAxsn=FCKAe=vXM_tqJhB3fKBWx3ZE=dnC75Oi7LZeDD5wLpNw@mail.gmail.com' \
--to=yallop@gmail.com \
--cc=caml-list@inria.fr \
--cc=yotambarnoy@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