带有模块的 Ocaml 未绑定类型构造函数
Ocaml unbound type constructor with module
我正在研究这个,我很确定在我写它的时候没有出现错误。现在它确实如此,我不知道为什么以及如何解决它,我已经搜索了很长时间但一无所获。
它在最后一行给出了 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
你认为 env
谁会出现在最后一行?
env
不是您在范围内拥有的某种类型构造函数,而是属于 ENV
模块的类型构造函数。
您可以在其中一个模块中定义 exp
和 efun
或使用 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
但是,您必须在 Listenv
和 Funenv
中再次提供它们。
我正在研究这个,我很确定在我写它的时候没有出现错误。现在它确实如此,我不知道为什么以及如何解决它,我已经搜索了很长时间但一无所获。 它在最后一行给出了 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
你认为 env
谁会出现在最后一行?
env
不是您在范围内拥有的某种类型构造函数,而是属于 ENV
模块的类型构造函数。
您可以在其中一个模块中定义 exp
和 efun
或使用 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
但是,您必须在 Listenv
和 Funenv
中再次提供它们。