水星:如果在顶层声明,此谓词有效,但不能作为 lambda



(下面的摘录来自这个测试套件,这是这里找到的两个文件程序的一半。

我以两种方式定义了一个辅助谓词:一次在顶层(称为 helper),一次作为 lambda(称为 Helper)。在下面代码的最后三行中,我使用了该辅助谓词。如果我使用helper它可以工作,但如果我使用Helper,我会收到一个错误(如下)。

怀疑问题可能是我能够为类型级谓词指定类型签名和模式签名,但只能为 lambda 指定模式签名。(如果这是问题所在,我不知道该怎么办。

以下是相关代码:

:- pred helper( query, int ).
:- mode helper( in, out ) is nondet.
helper( Q, F ) :-
  inQuery( fiveNumberSpace, Q, F ).
testQAnd = Res :-
    QQF = qqFind( qFind( list.filter( <(3) ) ) )
  , QQC = qqCond( qCond( func( Int )
                         = (if Int > 4 then no else yes) ) )
  , Helper = ( pred( Q :: in, F :: out ) is nondet :-
               inQuery( fiveNumberSpace, Q, F ) )
  % TODO Why can't I use Helper instead of helper for these?
  , solutions( helper( qqAnd( [QQF     ] ) ) , F1 )
  , solutions( helper( qqAnd( [QQF, QQC] ) ) , F2 )
  , solutions( helper( qqAnd( [     QQC] ) ) , F3 )

这是我使用 Helper 时得到的错误:

Making Mercury/cs/test.c
test.m:098: In clause for function `testQAnd'/0:
test.m:098:   in argument 1 of call to predicate `solutions'/2:
test.m:098:   in unification of argument
test.m:098:   and term `Helper(V_34)':
test.m:098:   type error in argument(s) of higher-order term (with arity 1).
test.m:098:   Functor (Helper) has type `pred(query.query, int)',
test.m:098:   expected type was `((func V_13) = V_14)'.
** Error making `Mercury/cs/test.c'.

我对你的问题没有完整的答案,但我相信这与修饰谓词有关。我写了一些测试代码来查询一个小的鱼数据库。

:- pred fish(string::out, int::out) is multi.
fish("Red", 1).
fish("Blue", 2).
fish("Green", 3).

此帮助程序生成与上述相同的错误:

Helper = (pred(S::in, L::out) is nondet :- fish(S, L)),
solutions(Helper("Red"), Sols)

此帮助程序工作正常:

Helper2 = (pred(X::out) is nondet :- fish("Red", X)),
solutions(Helper2, Sols)

我还尝试包装解决方案函数。这工作正常:

:- func solutions_to(string) = list(int).
solutions_to(Color) = Answers :-
  P = (pred(X::out) is nondet :- fish(Color, X)),
  solutions(P, Answers).

我能够编写一个返回 lambda 谓词的谓词,但是我无法编写一个函数来执行相同的操作。这就是我开始感到困惑的地方。根据此页面,仅当谓词包装在可区分的联合类型中时,才可能返回带有模式信息的谓词。以下代码并不是很有用,但它设法两者兼而有之

  1. 实现穷人对λ的咖喱
  2. 从函数返回 lambda 谓词

看:

:- type inner
  ---> inner(pred(string:: out, int::out) is multi).
:- type wrapper
  ---> wrapper(pred(int::out) is nondet).
:- func make_pred(inner, string) = wrapper.
make_pred(Inner, Param) = P :-
  Inner = inner(I),
  P = wrapper(pred(X::out) is nondet :- I(Param, X)).

然后使用它:

Inner = inner((pred(X::out, Y::out) is multi :- fish(X, Y))),
WP = make_pred(Inner, "Red"),
WP = wrapper(P),
solutions(P, Sols),

最新更新