我有一个Ring的模块签名。 IntRing(Z)很容易定义,但我想创建IntRingModP(Z_p)。如何在创建模块时将P传递给仿函数来设置它?
File "polynomials.ml", line 25, characters 24-27:
Error: Unbound module type Int
这导致var self = this;
var mongoose = new Mongoose();
mongoose.save(function (err, coll) {
//save to database
self.methodOne(); //If you call `this` from here, This will refer to `mongoose` class
}
答案 0 :(得分:5)
Functors只能将模块作为参数。因此,您需要创建一个包装int
的新模块类型:
module type IntT = sig
val x : int
end;;
module IntRingModP (P : IntT) : Ring = struct
let p = P.x
type t = int
let zero = 0 mod p
let one = 1 mod p
let add a b = (a+b) mod p
let mult a b = (a*b) mod p
let compare a b = compare (a mod p) (b mod p)
let equal a b = ((a mod p) = (b mod p))
let to_string a = Int.to_string (a mod p)
let print a = print_string (to_string a)
end;;
希望有所帮助。
答案 1 :(得分:3)
在将模块作为参数传递给仿函数之前,您需要指定其签名。您指的是模块类型Int
,但默认情况下OCaml中没有定义此类模块类型。你需要自己定义它,如下所示:
module type Modulus = sig
val modulus : int
end
module IntRingModP (P : Modulus) : Ring = struct
let p = P.modulus
type t = int
let zero = 0 mod p
let one = 1 mod p
let add a b = (a+b) mod p
let mult a b = (a*b) mod p
let compare a b = compare (a mod p) (b mod p)
let equal a b = ((a mod p) = (b mod p))
let to_string a = Int.to_string (a mod p)
let print a = print_string (to_string a)
end
要实例化它,您需要提供值:
module Int2 = IntRingModP(struct let modulus = 2 end)