【发布时间】:2023-03-30 18:01:01
【问题描述】:
我正在尝试使用标准 ML 的 CML 扩展来实现并发列表,但我遇到了可能与我是标准 ML 新手有关的错误。我已将 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 with initial contents l *)
val cnil =
let
val req = channel()
val reply = channel()
fun loop l = case recv req of
CONS x =>
(loop (x::l))
|
HEAD => (send(reply, l); loop l)
in
spawn(fn () => loop nil);
CLIST {reqCh=req,replyCh=reply}
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:21.4-21.35 Error: operator and operand don't agree [circularity]
operator domain: {replyCh:'Z list chan, reqCh:'Z list request chan}
operand: {replyCh:'Z list chan, reqCh:'Z request chan}
in expression:
CLIST {reqCh=req,replyCh=reply}
【问题讨论】:
-
CLIST 签名在哪里?这是您遇到的类型错误,因此最好让它们可用。主要问题是您的“头部”操作不会解构我猜的列表。
-
我刚刚重新粘贴了错误部分,因为我使用的是注释掉的文件并且行错误。我还包括了签名
标签: concurrency sml smlnj ml