蕴涵分支看不到变量
Implication branch doesn't see variables
这里有一个小测试:
:- begin_tests(tuturuu).
test(foo) :- Expected=[a,b],Got=[a,b,c],verdict(foo,Expected,Got).
% helper: transform lists A,B into ordered sets As,Bs
setify(A,B,As,Bs) :- % A,B -> As,Bs
list_to_ord_set(A,As), % A->As
list_to_ord_set(B,Bs). % B->Bs
% did the test pass? set-compare Expected and Got
verdict(Value,Expected,Got) :- % V,E,G -> b?
setify(Expected,Got,Eos,Gos),
format("For ~w, expected ~w and got ~w\n",[Value,Eos,Gos]),
ord_seteq(Eos,Gos)
-> true ; (format("For ~w, expected ~w but got ~w\n",[Value,Eos,Gos]),fail).
:- end_tests(tuturuu).
rt :- run_tests(tuturuu).
当我将其加载到 SWI-Prolog 中时:
?- [foo].
Warning: /home/somone/foo.pl:13:
Warning: Singleton variable in branch: Eos
Warning: Singleton variable in branch: Gos
true.
->
分支无法访问(本地)变量 Eos
和 Gos
?但是为什么?
它可以访问 Value
,因为我想它出现在头部。
确实:
?- rt.
% PL-Unit: tuturuu For foo, expected [a,b] and got [a,b,c]
For foo, expected _29026 but got _29032 <--- YUP NO ACCESS
ERROR: /home/someone/foo.pl:3:
test foo: failed
done
% 1 test failed
% 0 tests passed
false.
不太明白为什么会出现这个限制,估计是编译器没有实现的部分吧?
(顺便说一句,我们需要其中一个 "keep the bar in the green windows" 用于来自 Java 世界的单元测试,就像这样:(从 here 中窃取)
缺少括号?尝试:
verdict(Value,Expected,Got) :- % V,E,G -> b?
setify(Expected,Got,Eos,Gos),
( ord_seteq(Eos,Gos)
-> true
; format("For ~w, expected ~w but got ~w\n",[Value,Eos,Gos]),
fail
).
请注意,标准运算符定义为:
?- current_op(Priority, Type, ',').
Priority = 1000,
Type = xfy.
?- current_op(Priority, Type, '->').
Priority = 1050,
Type = xfy.
W.r.t。 "keep the bar in the green windows",您可以使用 Logtalk lgtunit
工具轻松完成(您也可以使用它来测试 Prolog 代码)。例如。 https://logtalk.org/files/blog/2019_11_06/xunit_report.html
有关介绍,请参阅 https://logtalk.org/2019/11/06/testing-multiple-implementations-of-a-protocol.html and https://logtalk.org/2019/12/02/generating-code-coverage-reports.html。
这里有一个小测试:
:- begin_tests(tuturuu).
test(foo) :- Expected=[a,b],Got=[a,b,c],verdict(foo,Expected,Got).
% helper: transform lists A,B into ordered sets As,Bs
setify(A,B,As,Bs) :- % A,B -> As,Bs
list_to_ord_set(A,As), % A->As
list_to_ord_set(B,Bs). % B->Bs
% did the test pass? set-compare Expected and Got
verdict(Value,Expected,Got) :- % V,E,G -> b?
setify(Expected,Got,Eos,Gos),
format("For ~w, expected ~w and got ~w\n",[Value,Eos,Gos]),
ord_seteq(Eos,Gos)
-> true ; (format("For ~w, expected ~w but got ~w\n",[Value,Eos,Gos]),fail).
:- end_tests(tuturuu).
rt :- run_tests(tuturuu).
当我将其加载到 SWI-Prolog 中时:
?- [foo].
Warning: /home/somone/foo.pl:13:
Warning: Singleton variable in branch: Eos
Warning: Singleton variable in branch: Gos
true.
->
分支无法访问(本地)变量 Eos
和 Gos
?但是为什么?
它可以访问 Value
,因为我想它出现在头部。
确实:
?- rt.
% PL-Unit: tuturuu For foo, expected [a,b] and got [a,b,c]
For foo, expected _29026 but got _29032 <--- YUP NO ACCESS
ERROR: /home/someone/foo.pl:3:
test foo: failed
done
% 1 test failed
% 0 tests passed
false.
不太明白为什么会出现这个限制,估计是编译器没有实现的部分吧?
(顺便说一句,我们需要其中一个 "keep the bar in the green windows" 用于来自 Java 世界的单元测试,就像这样:(从 here 中窃取)
缺少括号?尝试:
verdict(Value,Expected,Got) :- % V,E,G -> b?
setify(Expected,Got,Eos,Gos),
( ord_seteq(Eos,Gos)
-> true
; format("For ~w, expected ~w but got ~w\n",[Value,Eos,Gos]),
fail
).
请注意,标准运算符定义为:
?- current_op(Priority, Type, ',').
Priority = 1000,
Type = xfy.
?- current_op(Priority, Type, '->').
Priority = 1050,
Type = xfy.
W.r.t。 "keep the bar in the green windows",您可以使用 Logtalk lgtunit
工具轻松完成(您也可以使用它来测试 Prolog 代码)。例如。 https://logtalk.org/files/blog/2019_11_06/xunit_report.html
有关介绍,请参阅 https://logtalk.org/2019/11/06/testing-multiple-implementations-of-a-protocol.html and https://logtalk.org/2019/12/02/generating-code-coverage-reports.html。