3

I am trying to implement a concurrent list using CML extensions of Standard ML but i am running into errors that are probably to do with my being a newbie in Standard ML. I have implemented the clist as having an input and output channel and I store the list state in a loop. However my code does not compile and gives errors below

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

This 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

Errors I am getting:

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}
4

1 回答 1

0

所以你的问题在于你的定义clist

datatype 'a clist = CLIST of { reqCh : 'a request chan, replyCh : 'a chan }

这就是说请求通道接受 的请求'a并以'a. 这与您的实现不一致。CONS x当您在频道上发送请求时,您是在说将x类型添加'a到列表中,但是当您发送HEAD请求时,您是在说将整个列表还给我。因此,一个CONS请求应该接受 a'a并且一个HEAD请求应该返回一个'a list。您可以通过将clist定义更改为来解决您的问题

datatype 'a clist = CLIST of { reqCh : 'a request chan, replyCh : 'a list chan }

我还建议将您的定义更改cnilunit -> 'a clist函数,这样您就可以创建不同的并发列表。

例如:

val l1 = Clist.cnil()  
val l2 = Clist.cnil() (*cons'ing onto l2 won't affect l1*)
于 2015-06-05T19:02:22.093 回答