当我编译下面的 Mercury 代码时,我从编译器收到此错误:
In clause for `main(di, uo)':
in argument 1 of call to predicate
`test_with_anonymous_functions.assert_equals'/5:
mode error: variable `V_15' has
instantiatedness `/* unique */((func) =
(free >> ground) is semidet)',
expected instantiatedness was `((func) =
(free >> ground) is det)'.
我认为编译器所说的是“当您声明类型
test_case
时,您没有指定确定性,所以我假设您的意思是 det
。但随后您传入了 semidet
lambda。”我的问题:
/* unique */
TestCase
的一部分的实例化是什么意思?这会导致给定和预期实例化之间的不匹配吗? main
中是否有更简洁的声明 lambda 的方法? ?我对 lambda 的声明和我在 lambda 中的代码一样多。 编码:
% (Boilerplate statements at the top are omitted.)
% Return the nth item of a list
:- func nth(list(T), int) = T.
:- mode nth(in, in) = out is semidet.
nth([Hd | Tl], N) = (if N = 0 then Hd else nth(Tl, N - 1)).
% Unit testing: Execute TestCase to get the
% actual value. Print a message if (a) the lambda fails
% or (b) the actual value isn't the expected value.
:- type test_case(T) == ((func) = T).
:- pred assert_equals(test_case(T), T, string, io.state, io.state).
:- mode assert_equals(in, in, in, di, uo) is det.
assert_equals(TestCase, Expected, Message, !IO) :-
if Actual = apply(TestCase), Actual = Expected
then true % test passed. do nothing.
else io.format("Fail:\t%s\n", [s(Message)], !IO).
main(!IO) :-
List = [1, 2, 3, 4],
assert_equals( ((func) = (nth(List, 0)::out) is semidet),
1, "Nth", !IO).
最佳答案
本是对的,
我想补充一点,Mercury 假设函数在默认情况下是确定性的(函数应该是确定性的,这是明智的)。对于必须声明确定性的谓词,情况并非如此。这使得使用确定性函数进行高阶编程比任何其他函数或谓词更容易,仅仅是因为样板较少。
因此,您可以使 lambda 表达式更简洁一些。您还可以通过将头部中的变量 S 替换为主体,从而将 lambda 表达式的主体移入头部。
apply_transformer((func(S0) = "Hello " ++ S0),
"World", Msg),
关于lambda - Mercury:如何声明高阶数据类型的确定性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7380469/