【发布时间】:2011-11-14 21:06:40
【问题描述】:
当我编译下面的 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。”
我的问题:
- 声明类型确定性的语法是什么?我尝试过的猜测都导致了语法错误。
- 有人能解释
TestCase的实例化的/* unique */部分是什么意思吗?这会导致给定实例化与预期实例化不匹配吗? - 在
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).
【问题讨论】:
标签: lambda non-deterministic mercury