私は以下マーキュリーコードをコンパイルするとき、私は、コンパイラからこのエラーを取得する: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
ラムダを渡しました。 "
私の質問:
- タイプの決定論を宣言するための構文は何ですか?私が試した推測では、すべて構文エラーが発生しています。
/* unique */
TestCase
のインスタンス化の意味は何かを説明できますか?それは与えられた&とインスタンス化の間の不一致を引き起こすでしょうか?main
にラムダを宣言する方法はあまりありませんか?私はラムダ内でコードを行うのと同じくらい多くのラムダ宣言をしています。
コード:
% (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).
ありがとう、ベン。 – Evan
@エヴァン:私の答えに追加するのを忘れましたが、高次の用語の複雑なモードのためのモードエイリアスを作成することもできます(これは私の問題を解決しました)。私は同じタイプの高次の引数を取る数十の述語を持つモジュールに対してこれを一度行なった。私はタイプとモードをモジュールの上に宣言し、その中でそれらを使用しました。 – Ben
@Evan:ねえ、あなたは http://stackoverflow.com/questions/13896296/creating-a-deterministic-finite-automata-dfa-mercury を見てみたいものです - これを行うには何もポストは、エバンに注目を集めるためだけに – Dieter