OCaml: Why does renaming a type fail with “Their kinds differ”

依然范特西╮ 提交于 2020-01-05 01:12:21

问题


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.



来源:https://stackoverflow.com/questions/27784768/ocaml-why-does-renaming-a-type-fail-with-their-kinds-differ

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!