开发者

standard ml value restriction errors

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 = CLIS开发者_如何转开发T 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


There are two problems with your code. One is the value restriction error, which you can fix by changing

val cnil : 'a clist

to

val cnil : unit -> 'a clist

and

val cnil =

to

fun cnil () =

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:

structure Clist : CLIST =
struct
  open CML

  datatype 'a request = CONS of 'a | HEAD

  type 'a clist = { reqCh : 'a request chan, replyCh : 'a chan }

  (* create a clist *)
  fun cnil () =
    let
      val clist as {reqCh, replyCh} = {reqCh = channel(), replyCh = channel()}       
      fun loop l = case recv reqCh of
          CONS x =>     
            (loop (x::l))
        |                                     
          HEAD => (send(replyCh, hd l); loop l)
    in                        
      spawn(fn () => loop nil);
      clist
    end


  fun cons x (clist as {reqCh, replyCh})=
    (send (reqCh, CONS x); clist)

  fun hd (CLIST {reqCh, replyCh}) = (send (reqCh, HEAD); recv replyCh)
end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜