我想实现一个使用标准ML的CML扩展的并发列表,但我遇到了可能与我是一个标准ML中的新手做的错误。我将clist实现为具有输入和输出通道,并将列表状态存储在一个循环中。但是我的代码无法编译,并给出以下标准的ML/CML错误的操作符 - 操作数错误
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签名?这是你得到的类型错误,所以让它们可用是很好的。主要的问题是你的'头部'操作不会解构我想的列表。 – 2011-02-25 13:32:02
哪一行是33行? – tgdavies 2011-02-25 13:32:36
我只是重申了错误部分,因为我使用的是注释掉的文件,而且行是错误的。我还包括签名 – spydadome 2011-02-25 13:44:13