标准ml值限制误差

发布于 2024-10-19 23:55:42 字数 1439 浏览 6 评论 0原文

您好,我需要帮助理解为什么我在这段代码中遇到值限制错误,以及如果可能的话如何解决它。

特别是在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

草莓味的萝莉 2024-10-26 23:55:42

您的代码有两个问题。一是值限制错误,您可以通过更改

val cnil : 'a clist

to

val cnil : unit -> 'a clist

val cnil =

to

fun cnil () =

来修复第二个问题是 cnil 似乎没有做任何事情 - 也许是因为 head 函数返回 Empty 而不是引发 Empty 并且您使其变得多余? Basis 库中已经有一个 hd 函数可以执行此操作。 clist 类型也不需要是数据类型。我想你想要的是这样的:

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文