OCaml: Why does renaming a type fail with “Their k

2019-07-19 16:50发布

问题:

I'm building an universal container for for pairs of a type witness and a value of the witnessed type. This I want to use for several different types, which gives me errors because the types are all named the same.

So I'm trying to rename types in the result of a functor like this:

module type Witness = sig type 'a key type 'a value end

module type Witnessed = sig
  type 'a key
  type 'a value
  type t
  type ('a, 'b) conv = {
    key : 'c . 'c key -> 'a;
    value : 'c . 'c value -> 'b;
  }
  val box : 'a key -> 'a value -> t
  val conv : ('a, 'b) conv -> t -> ('a * 'b)
end

module MAKE(W : Witness) : Witnessed with type 'a key = 'a W.key
                     and type 'a value = 'a W.value = struct
  include W

  type t = Box : 'a key * 'a value -> t
  let box k v = Box (k, v)
  type ('a, 'b) conv = {
    key : 'c . 'c key -> 'a;
    value : 'c . 'c value -> 'b;
  }
  let conv conv (Box (k, v)) = (conv.key k, conv.value v)
end

type _ token
type _ attrib

module W = struct
  type 'a key = 'a token
  type 'a value = 'a attrib
end

module Boxed = struct
    module T = MAKE(W)
    type lexeme = T.t
    type ('a, 'b) lexeme_conv = ('a, 'b) T.conv
    include (T : module type of T with type 'a key := 'a token
                                  and type 'a value := 'a attrib
                                  and type t := lexeme
                                  and type ('a, 'b) conv := ('a, 'b) lexeme_conv)
end

and ocaml says:

File "foo.ml", line 49, characters 38-80:
Error: This variant or record definition does not match that of type
         ('a, 'b) lexeme_conv
       Their kinds differ.

How can the types of conv and lexeme_conv differ?

回答1:

You can solve the issue by replacing the definition of lexeme_conv by:

type ('a, 'b) lexeme_conv = ('a, 'b) T.conv = {
  key : 'c . 'c T.key -> 'a;
  value : 'c . 'c T.value -> 'b;
}

This is due to the fact that type aliases and type definitions do not behave in the same way in regard to destructive substitution. Here for example, with lexeme_conv an alias, the signature will change (since the record definition would stop being exposed), which is forbidden.