带有模块

时间:2015-06-08 21:39:05

标签: types constructor module ocaml

我正在研究这个问题,我非常肯定,在我写这篇文章时,错误就不会显示出来。现在它确实如此,我不知道为什么以及如何解决它,我搜索了很长时间,但没有。 它在最后一行给出了Unbound类型构造函数env 感谢大家的反馈

module type ENV =
sig
type 't env
val emptyenv : 't -> 't env
val bind : 't env * string * 't -> 't env
val bindlist : 't env * (string list) * ('t list)-> 't env
val applyenv : 't env * string -> 't
exception WrongBindlist
end

module Funenv:ENV =
struct
type 't env = string -> 't
exception WrongBindlist
let emptyenv(x) = function (y: string) -> x
(* x: valore default *)
let applyenv(x,y) = x y
let bind(r, l, e) =
function lu -> if lu = l then e else applyenv(r,lu)
let rec bindlist(r, il, el) = match (il,el) with
| ([],[]) -> r
| i::il1, e::el1 -> bindlist (bind(r, i, e), il1, el1)
| _ -> raise WrongBindlist
end

module Listenv:ENV =
struct
type 't env = (string * 't) list
exception WrongBindlist
let emptyenv(x) = [("", x)]
let rec applyenv(x,y) = match x with
| [(_, e)] -> e
| (i1, e1) :: x1 -> if y = i1 then e1
else applyenv(x1, y)
| [] -> failwith("wrong env")
let bind(r, l, e) = (l, e) :: r
let rec bindlist(r, il, el) = match (il, el) with
| ([],[]) -> r
| i::il1, e::el1 -> bindlist (bind(r, i, e), il1, el1)
| _ -> raise WrongBindlist
end

type ide = string


type exp = Eint of int
| Ebool of bool

type eval = Int of int
| Bool of bool
| Unbound
| Funval of efun
and efun = exp * eval env

1 个答案:

答案 0 :(得分:1)

您希望env最后一行是谁? env不是范围内的某种类型构造函数,而是属于ENV模块的类型构造函数。

您可以在其中一个模块中定义expefun,或者使用Listenv.env

通过以下内容编译:

type eval = Int of int
| Bool of bool
| Unbound
| Funval of efun
and efun = exp * eval Listenv.env

另一种解决方案,也就是您正在做的事情,就是将此定义直接放在签名中:

module type ENV =
  sig
    type 't env
    val emptyenv : 't -> 't env
    val bind : 't env * string * 't -> 't env
    val bindlist : 't env * string list * 't list -> 't env
    val applyenv : 't env * string -> 't

    type ide  = string
    type exp  = Eint of int | Ebool of bool
    type eval = Int of int | Bool of bool | Unbound | Funval of efun
    and  efun = exp * eval env

    exception WrongBindlist
  end

但是,您必须在ListenvFunenv中再次提供这些内容。

相关问题