SML/NJ 中的模式匹配空 Fifo.fifo

Pattern-match empty Fifo.fifo in SML/NJ

当我尝试写类似

的东西时
fun test Fifo.empty = true
  | test _ = false`

我收到一条错误消息 Error: variable found where constructor is required: Fifo.empty。我真的是 SML/NJ 的新手。原来它与 Fifo.empty 是一个 longId 有关,但我还没有想出如何解决这个问题,除了通过将 Fifo.isEmpty 作为参数传递来修补它,但这几乎不是解决方案...

检查Fifo模块时,

- open Fifo;
[autoloading]
[library $SMLNJ-LIB/Util/smlnj-lib.cm is stable]
[autoloading done]
opening Fifo
  datatype 'a fifo = ...
  exception Dequeue
  val empty : 'a fifo
  (* and so on *)

你可以看到 Fifo.empty 是一个 'a fifo 值。看看它是如何制作的,

- Fifo.empty;
val it = Q {front=[],rear=[]} : 'a fifo

不幸的是,'a fifoQ 数据类型构造函数被 opaque 模块隐藏(因此 datatype 定义显示为“...”。当你定义一个数据类型时,它的构造函数(例如 Q)变成了 值构造函数模式构造函数,但是当你使用这样的值构造函数声明像 Fifo.empty 这样的值时,它们不会也成为模式构造函数。

I haven't figured out how to fix this, except by patching it by passing the Fifo.isEmpty as an argument, but that's hardly a solution...

我不确定你为什么需要传入 Fifo.isEmpty 作为参数;不能只在函数体中引用 Fifo.isEmpty 吗?也许您没有解释这个问题的某些方面。

下面的怎么样:

fun test queue = Fifo.isEmpty queue

或者简单地说:

val test = Fifo.isEmpty

一般来说,如果您想传递一堆库函数作为参数,您可以考虑构建一个高阶模块(仿函数),它接受另一个模块作为参数。