4

I'm trying to do one of the extended exercises in http://okmij.org/ftp/tagless-final/nondet-effect.html#no-functor and replace the int_t type with 'a repr. While trying to do this, I'm stuck on the following error:

Values do not match:
  val cons : '_a repr -> '_a list_t -> '_a list_t
is not included in
  val cons : 'a repr -> 'a list_t -> 'a list_t

my implementation of cons looks like

let cons: 'a repr -> 'a list_t -> 'a list_t =
  liftm2 (fun h t -> h::t)

which definitely has the right type. Why are these apparently identical types incompatible?

porglezomp
  • 1,333
  • 12
  • 24

1 Answers1

2

Making a minimal example helped me solve the problem! I was able to reduce the failing case down to this:

module type Test = sig
  type 'a t
  val id: 'a t -> 'a t
end

module TestT: Test = struct
  type 'a t = 'a

  let id_maker () x = x
  let id: 'a t -> 'a t =
    id_maker ()
end

which indicates that I'm falling victim to value restriction. There's a similar problem in this other stack overflow question, but I was misled by the module error message. I fixed the problem by changing from

let cons: 'a repr -> 'a list_t -> 'a list_t =
  liftm2 (fun h t -> h::t)

to

let cons: 'a repr -> 'a list_t -> 'a list_t =
  fun h t -> liftm2 (fun h t -> h::t) h t
Community
  • 1
  • 1
porglezomp
  • 1,333
  • 12
  • 24