ocaml-modular-implicits icon indicating copy to clipboard operation
ocaml-modular-implicits copied to clipboard

`No instance found` with `open implicit` and type alias

Open gadmm opened this issue 6 years ago • 0 comments

The behaviour of the following example is unexpected:

module type Print = sig
  type t
  val f : t -> unit
end

implicit module Int = struct
  type t = int

  let f _ = ()
end

module type Pair = sig
  type ('a,'b) t = 'a * 'b

  implicit module Make {A:Print} {B:Print} : Print with type t = (A.t, B.t) t
  (* (*works*) implicit module Make {A:Print} {B:Print} : Print with type t = A.t * B.t *)

  val create : 'a -> 'b -> ('a,'b) t
end

module Pair : Pair = struct
  type ('a,'b) t = 'a * 'b

  implicit module Make {A:Print} {B:Print} = struct
    type t = A.t * B.t

    let f _ = ()
  end

  let create x y = (x,y)
end

open implicit Pair
(* (*works*) open Pair *)

let print {A:Print} (x:A.t) = A.f x

let _ = print (Pair.create 3 3)

It fails to compile with Error: No instance found for implicit A.. What I would expect is that it correctly resolves the module Pair.Make {Int} {Int}.

My use-case of interest is when type ('a,'b) t is abstract, but this seems irrelevant for the problem.

It compiles as expected when you either replace the type by its definition in the declaration of Make (uncomment the first comment) or if you replace open implicit with a plain open (uncomment the second comment).

I looked at other bugs, but it did not seem to fit already-reported ones.

gadmm avatar May 12 '19 17:05 gadmm