标准ml值限制误差
您好,我需要帮助理解为什么我在这段代码中遇到值限制错误,以及如果可能的话如何解决它。
特别是在 val cnil 中,我试图创建一个空的 CLIST 结构来与签名匹配,但我不断收到此值限制错误。
感谢您的帮助
structure Clist : CLIST =
struct
open CML
datatype 'a request = CONS of 'a | HEAD
datatype 'a clist = CLIST of { reqCh : 'a request chan, replyCh : 'a chan }
(* create a clist *)
val cnil =
let
val reqCh = channel()
val replyCh = channel()
fun loop l = case recv reqCh of
CONS x =>
(loop (x::l))
|
HEAD => (let fun head (h::t) = h | head [] = Empty in send(replyCh, head(l)) end ; loop l)
in
spawn(fn () => loop nil);
CLIST {reqCh = channel(), replyCh = channel() }
end
fun cons x (CLIST {reqCh, replyCh})=
(send (reqCh, CONS x); CLIST {reqCh = reqCh, replyCh = replyCh})
fun hd (CLIST {reqCh, replyCh}) = (send (reqCh, HEAD); recv replyCh)
end
,这是签名文件
signature CLIST =
sig
type 'a clist
val cnil : 'a clist
val cons : 'a -> 'a clist -> 'a clist
val hd : 'a clist -> 'a
end
,这是我收到的错误:
clist.sml:10.7-22.5 Warning: type vars not generalized because of
value restriction are instantiated to dummy types (X1,X2,...)
clist.sml:1.1-29.4 Error: value type in structure doesn't match signature spec
name: cnil
spec: 'a ?.Clist.clist
actual: ?.X1 ?.Clist.clist
hi i need help understanding why I am getting a value restriction error in this code and how I can solve it if possible.
In particular in val cnil, I am trying to create an empty CLIST structure to match with the signature but I keep getting this value restriction error.
thanks for any help
structure Clist : CLIST =
struct
open CML
datatype 'a request = CONS of 'a | HEAD
datatype 'a clist = CLIST of { reqCh : 'a request chan, replyCh : 'a chan }
(* create a clist *)
val cnil =
let
val reqCh = channel()
val replyCh = channel()
fun loop l = case recv reqCh of
CONS x =>
(loop (x::l))
|
HEAD => (let fun head (h::t) = h | head [] = Empty in send(replyCh, head(l)) end ; loop l)
in
spawn(fn () => loop nil);
CLIST {reqCh = channel(), replyCh = channel() }
end
fun cons x (CLIST {reqCh, replyCh})=
(send (reqCh, CONS x); CLIST {reqCh = reqCh, replyCh = replyCh})
fun hd (CLIST {reqCh, replyCh}) = (send (reqCh, HEAD); recv replyCh)
end
here is the signature file
signature CLIST =
sig
type 'a clist
val cnil : 'a clist
val cons : 'a -> 'a clist -> 'a clist
val hd : 'a clist -> 'a
end
and here are the errors I am getting:
clist.sml:10.7-22.5 Warning: type vars not generalized because of
value restriction are instantiated to dummy types (X1,X2,...)
clist.sml:1.1-29.4 Error: value type in structure doesn't match signature spec
name: cnil
spec: 'a ?.Clist.clist
actual: ?.X1 ?.Clist.clist
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的代码有两个问题。一是值限制错误,您可以通过更改
to
和
to
来修复第二个问题是 cnil 似乎没有做任何事情 - 也许是因为 head 函数返回 Empty 而不是引发 Empty 并且您使其变得多余? Basis 库中已经有一个 hd 函数可以执行此操作。 clist 类型也不需要是数据类型。我想你想要的是这样的:
There are two problems with your code. One is the value restriction error, which you can fix by changing
to
and
to
The second problem is that cnil does not seem to be doing anything - perhaps because the head function is returning Empty rather than raising Empty and you made it redundant? There is already a hd function in the Basis Library that does this. The clist type does not need to be a datatype either. I think what you want is this: