2

First the code:

module Boolean = struct
  exception SizeMismatch
  type boolean = T | F | Vec of boolean array 

  let to_bool v = match v with 
    T -> true
  | F -> false 
  | _ -> raise SizeMismatch
end

module Logic = struct
  type 'a var_t = { name: string; mutable value: 'a }
  type 'a bexp  = Const of 'a
  |             Var of 'a var_t

  let eval exp = match exp with
    Const x -> x
  | Var x   -> x.value

  let make_var s v = { name = s; value = v }
  let set v n = v.value <- n
  let get_var_name v = v.name
  let get_var_val  v = v.value
end

module type EXP =
  sig
    type  'a var_t
    type  'a bexp
    val eval_exp     : 'a bexp -> bool
    val get_var_name : 'a var_t -> string
    val get_var_val  : 'a var_t -> 'a
  end

module LogicExp = 
  struct
    include Logic
    let eval_exp exp = Boolean.to_bool (Logic.eval exp)
  end

module FSM ( Exp : EXP ) = 
  struct
    let print_var v = Printf.printf "%s = %d\n" (Exp.get_var_name v)
    (Exp.get_var_val v)

  end

module MyFSM = FSM(LogicExp) 

let myvar = Logic.make_var "foo" 1;;

MyFSM.print_var myvar ;;

When I compile it I get the following error:

File "test.ml", line 57, characters 19-27:
Error: Signature mismatch:
   Modules do not match:
     sig
       type 'a var_t =
         'a Logic.var_t = {
         name : string;
         mutable value : 'a;
       }
       type 'a bexp = 'a Logic.bexp = Const of 'a | Var of 'a var_t
       val eval : 'a bexp -> 'a
       val make_var : string -> 'a -> 'a var_t
       val set : 'a var_t -> 'a -> unit
       val get_var_name : 'a var_t -> string
       val get_var_val : 'a var_t -> 'a
       val eval_exp : Boolean.boolean Logic.bexp -> bool
     end
   is not included in
     EXP
   Values do not match:
     val eval_exp : Boolean.boolean Logic.bexp -> bool
   is not included in
     val eval_exp : 'a bexp -> bool

What I don't understand is how the more specific type isn't included in the more general type?

4

1 回答 1

5

错误信息实际上是相当准确的:

Values do not match:
  val eval_exp : Boolean.boolean Logic.bexp -> bool
is not included in
  val eval_exp : 'a bexp -> bool

MyFSM函子需要一个模块参数,除其他外,它应该包含一个eval_exp类型的函数'a bexp -> bool。这意味着给定一个类型的值'a bexp对于函数的任何选择都'a应该产生一个类型的值bool。但是,您提供的模块包含一个函数,该函数仅针对 的一个特定选择执行此操作'a,即模块'a中的类型。booleanBoolean

这个最快的解决方法是将您的签名定义EXP

module type EXP =
  sig
    type  b  (* added *)
    type  'a var_t
    type  'a bexp
    val eval_exp     : b bexp -> bool  (* changed *)
    val get_var_name : 'a var_t -> string
    val get_var_val  : 'a var_t -> 'a
  end

所以eval_exp现在对固定类型的布尔表达式进行操作b,然后定义LogicExp

module LogicExp =
  struct
    type b = Boolean.boolean  (* added *)
    include Logic
    let eval_exp exp = Boolean.to_bool (Logic.eval exp)
  end

使其固定bBoolean.boolean.

实施这些更改将使您的代码编译。

现在,让我们看看您关于“更具体的类型如何不包含在更一般的类型中?”的问题。这假设'a bexp -> bool确实比 更一般boolean bexp -> bool,但实际上并非如此。如果一个函数类型比函数类型更通用并且比 更通用,A -> B则认为函数类型比函数类型更通用:C -> DCABD

A <: C        D <: B
--------------------
  C -> D <: A -> B

注意前提中C和的“翻转” 。A我们说函数空间构造函数的参数位置... -> ...逆变的(相对于结果位置的协变)。

直观地说,如果一个类型包含更多值,则它比另一个类型更通用。要了解为什么函数空间构造函数的参数位置是逆变的,请考虑某些类型f的类型函数和. 现在,考虑一个比 严格更一般的类型,即 in 中的所有值也在 in 中,但包含一些不在 in 中的值。因此,我们至少可以分配一个值 type ,但不能分配 type 。它的类型告诉我们知道如何对 type 的值进行操作。但是,如果我们要(错误地!)从中得出结论,那么我们可以使用它,就好像它有类型A -> CACBAABBAbBAfAA <: BA -> C <: B -> CfB -> C因此,我们可以将值b作为参数传递给f. 但b不是类型Af只知道如何操作类型的值A

显然,... -> ...参数位置的协方差不起作用。要查看逆变确实起作用,请考虑相同的类型A, BC现在还要考虑g类型的函数B -> C。也就是说,g知道如何对 type 的所有值进行操作B。函数空间构造函数在其参数位置的逆变使我们可以得出结论,g也可以安全地分配 type A -> C。我们知道 in 中的所有值A也都在B并且g知道如何处理所有B这些都没有问题,我们可以安全地将值传递Ag.

于 2012-04-20T04:45:07.670 回答