在实现之间绑定记录类型

时间:2016-01-12 12:23:35

标签: ocaml

我坚持如何正确实现这一点。我有一个文件Mytypes.ml,它包含本地和远程类型的签名,其中包含密码类型的密钥 - 包含签名和验证功能的记录。我还有另外两个文件Cipher1.ml和Cipher2.ml

module Mytypes = struct
            type local_t
            type remote_t
            type cipher_t = {
                local_create : unit -> local_t;
                local_sign : local_t -> Cstruct.t -> Cstruct.t;
                remote_create : Cstruct.t -> remote_t;
                remote_validate : remote_t -> Cstruct.t -> bool;
            }
            type key_sets = {
                mutable locals : local_t list;
                mutable remotes : remote_t list;
                mutable ciphers : cipher_t list;
            }
        end

        module Cipher1 = struct
            open Mytypes
            type local_t = {
                secretkey : Cstruct.t;
            }
            type remote_t = {
                publickey : Cstruct.t;
            }
            let c1_local_create () = {secretkey = Cstruct.create 321}
            let c1_local_sign local data () = Cstruct.create 21
            let c1_remote_create public_key_data = {publickey = Cstruct.create 123}
            let c1_remote_validate remote data = true
            let create () = {
                local_create = c1_local_create;
                local_sign   = c1_local_sign;
                remote_create = c1_remote_create;
                remote_validate = c1_remote_validate;
            }
        end

        module Cipher2 = struct
            open Mytypes

            type local_t = {
                ekey1 : Cstruct.t;
            }
            type remote_t = {
                ekey2 : Cstruct.t;
            }
            let c2_local_create () = {ekey1 = Cstruct.create 321}
            let c2_local_sign local data () = Cstruct.create 12
            let c2_remote_create public_key_data = {ekey1 = Cstruct.create 123}
            let c2_remote_validate remote data = true
            let create () = {
                local_create = c2_local_create;
                local_sign   = c2_local_sign;
                remote_create = c2_remote_create;
                remote_validate = c2_remote_validate;
            }
        end

我总是得到的错误是:

File "cipher1.ml", line 14, characters 19-34:
        Error: This expression has type unit -> local_t
               but an expression was expected of type unit -> Mytypes.local_t
               Type local_t is not compatible with type Mytypes.local_t 
        ocamlmklib returned with exit code 2
  • 第14行是:local_create = c1_local_create;

有人可以提出可能的前进方向吗?

1 个答案:

答案 0 :(得分:1)

目前还不完全清楚,但我假设您的示例代码代表了三个名为mytypes.mlcipher1.mlcipher2.ml的文件。

首先要注意的是,当您说open Mtypes时,您可以使文件mtypes.ml的顶级名称可用。但是该文件中只有一个顶级名称Mytypes

OCaml在文件的最外层为您提供免费模块,因此拥有一个只包含一个模块的文件是相当罕见的。对于cipher1.mlcipher2.ml也是如此。

接下来要注意的是,您的Mytypes模块将local_tremote_t声明为抽象类型。由于没有函数返回这些类型的值,因此其他模块无法创建这些类型的值。

当然 其他模块不能声明他们想要使用的local_tremote_t类型的自己版本。

从您编写代码的方式来看,您可能希望Mytypes中的定义是模块类型而不是模块。您可以声明这样的模块类型:

module type Mytypes = sig ... end

然后你可以说其他模块都有这种类型:

module cipher1 : Mytypes = struct ... end