/*
在创建需要运算符的构造时,我喜欢从
将所有操作员优先级设置为 10'1 。
如果可以使构造与优先级为 10'1 的所有运算符一起使用,那是理想的。
一旦确定了优先级,就可以更改优先级以适应进一步的要求。
例如if _if_ then _then_ else _else_的进一步要求
是对 _if_ 和 _then_ 和 _else_ 部分中应该包含的内容的考虑。
例如,如果 ; 需要这样,例如 if foo ; bar then baz ; qux
那么优先级必须大于10'1100。
例如,如果 , 需要这样,例如 if foo , bar then baz , qux
那么优先级必须大于10'1000。
例如,如果 \+ 需要这样,例如 if \+ foo then \+ bar
那么优先级必须大于10'950。
例如,如果 = 需要这样,例如 if _foo_ = bar then _baz_ = qux
那么优先级必须大于 10'700 。
这 4 个运营商是最重要的考虑因素。
*/
:- op(10'1,'fx','if') .
:- op(10'1,'yfx','then') .
:- op(10'1,'yfx','else') .
if _if_ then _then_ else _else_
:-
_if_ -> _then_ ; _else_
.
if _if_ then _then_
:-
if _if_ then _then_ else false
.
/* -- testing -- */
:- op(10'1,'fy','consider') .
:- op(10'1,'xfy','perform') .
:- op(10'1,'xfy','form') .
:- op(10'1,'xfy','expect') .
:- op(10'1,'xfy','actual') .
:- op(10'1,'xfy','success') .
:- op(10'1,'xfy','fail') .
consider _consider_ perform _perform_ form _form_ expect _expect_
:-
findall(_form_,(_consider_,_perform_),_actual_) ,
(
_actual_ = _expect_ ->
writeq((success perform _perform_)) , nl ;
writeq((failure perform _perform_ expect _expect_ actual _actual_)) , nl
)
.
test
:-
consider
(
(_a_ = true) ;
(_a_ = false)
)
perform
(
if _a_ then (_z_ = b) else (_z_ = c)
)
form
(
_z_
)
expect
[
b ,
c
]
.
test
:-
consider
(
(_a_ = true) ;
(_a_ = false)
)
perform
(
if _a_ then (_z_ = b)
)
form
(
_z_
)
expect
[
b
]
.
test
:-
consider
(
(_a_ = true , _b_ = true) ;
(_a_ = true , _b_ = false) ;
(_a_ = false , _b_ = true) ;
(_a_ = false , _b_ = false)
)
perform
(
if _a_ then (if _b_ then (_z_ = c) else (_z_ = d)) else (_z_ = e)
)
form
(
_z_
)
expect
[
c ,
d ,
e ,
e
]
.
/*
YAP 6.2.2 (x86_64-linux): Sat Sep 17 13:59:03 UTC 2016
I ?- \+ (test , \+ true) .
(success)perform if _131254 then(_131271=b)else(_131271=c)
(success)perform if _131254 then(_131268=b)
(success)perform if _131257 then(if _131260 then(_131315=c)else(_131315=d))else(_131315=e)
yes
*/