From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@sympa.inria.fr Delivered-To: caml-list@sympa.inria.fr Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by sympa.inria.fr (Postfix) with ESMTPS id 367297F75C for ; Tue, 9 Sep 2014 17:47:03 +0200 (CEST) Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of yallop@gmail.com) identity=pra; client-ip=209.85.212.181; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="yallop@gmail.com"; x-sender="yallop@gmail.com"; x-conformance=sidf_compatible Received-SPF: Pass (mail2-smtp-roc.national.inria.fr: domain of yallop@gmail.com designates 209.85.212.181 as permitted sender) identity=mailfrom; client-ip=209.85.212.181; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="yallop@gmail.com"; x-sender="yallop@gmail.com"; x-conformance=sidf_compatible; x-record-type="v=spf1" Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of postmaster@mail-wi0-f181.google.com) identity=helo; client-ip=209.85.212.181; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="yallop@gmail.com"; x-sender="postmaster@mail-wi0-f181.google.com"; x-conformance=sidf_compatible X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AugBALofD1TRVdS1m2dsb2JhbABZg2BNCgSCeMcvh0wBgQUIFhABAQEBAQYLCwkUKoQEAQEEEhEEGQEbHQEDDAYFCw0CAiYCAiEBAREBBQEcBhMiiAsBAxENmnFrizCBcoMQiTUKGScNZoV3AREBBQ6BHot0gi0HgnmBUwWVcYRyghCBX40UhEkYKYUTPC8Bgk4BAQE X-IPAS-Result: AugBALofD1TRVdS1m2dsb2JhbABZg2BNCgSCeMcvh0wBgQUIFhABAQEBAQYLCwkUKoQEAQEEEhEEGQEbHQEDDAYFCw0CAiYCAiEBAREBBQEcBhMiiAsBAxENmnFrizCBcoMQiTUKGScNZoV3AREBBQ6BHot0gi0HgnmBUwWVcYRyghCBX40UhEkYKYUTPC8Bgk4BAQE X-IronPort-AV: E=Sophos;i="5.04,492,1406584800"; d="scan'208";a="93798180" Received: from mail-wi0-f181.google.com ([209.85.212.181]) by mail2-smtp-roc.national.inria.fr with ESMTP/TLS/RC4-SHA; 09 Sep 2014 17:47:02 +0200 Received: by mail-wi0-f181.google.com with SMTP id e4so4715921wiv.14 for ; Tue, 09 Sep 2014 08:47:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=20hyurOTukV88HzgVjTFV0UWZGDaOk7PYi1aZ5wEGsY=; b=kY+N6e2JZi7G2GHWyGIZ9SBeFnBbvl1t5OQRhUh692UWYzaXRZk4vCAXekQH4WbY8q 6LBjNnQjbnhvqBeWQG7vvUogdS99X/9MJeui7dpd8TBIqtph5bTKGTONLMtxmkpA3YjF 6T7DbFeR47yiqMFuFTTfPy9arVnSPKMJDOd53QoD8aLCm9AOrca4gVwjnko9cw9DTi80 +/PmUEqpMCzb0+aMzOTNIPveyrpt43V1EYdAAcHZnwuNyrBDBrNZDdtJ3ZIPLQs9aXw1 LsDRLIG6xU69CjfS19z6TXn5fy6rYzVO0mPs8eTiq7rzkOMHD5Y42CxlO/GEj3BhN5xP LtMQ== MIME-Version: 1.0 X-Received: by 10.194.3.106 with SMTP id b10mr42953922wjb.3.1410277622721; Tue, 09 Sep 2014 08:47:02 -0700 (PDT) Received: by 10.217.129.194 with HTTP; Tue, 9 Sep 2014 08:47:02 -0700 (PDT) In-Reply-To: References: Date: Tue, 9 Sep 2014 16:47:02 +0100 Message-ID: From: Jeremy Yallop To: Yotam Barnoy Cc: Ocaml Mailing List Content-Type: text/plain; charset=UTF-8 Subject: Re: [Caml-list] Implicit module question On 9 September 2014 16:32, Yotam Barnoy 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