【发布时间】:2018-07-14 02:53:41
【问题描述】:
这样定义 IF :
dynamic(if/1).
op(200, fx, if).
op(150, xfx, then).
op(100, xfy, and).
op(100, xfy, or).
生成以下规范形式:
?- write_canonical(if x then y).
if(then(x,y))
?- write_canonical(if x and z then y).
if(then(and(x,z),y))
?- write_canonical(if x and z or t then y).
if(then(and(x,or(z,t)),y))
有没有办法生成:
if( conds, then(actions) ).
或者更好:
if( conds, (actions) ).
像这样:
if(x,y)
if(x, then(y))
if( and(x,or(z,t)), then(y))
if( and(x,or(z,t)), (y))
我可以看到一个可能的替代方案:)
?- op(200, xfy, ==>).
?- write_canonical(x ==> y).
==>(x,y)
?- write_canonical(x and z ==> y).
==>(and(x,z),y)
【问题讨论】:
-
您是否尝试将
if定义为二元运算符,而不是您当前显示的一元运算符?if(x, y)、if(x, then(y))等,将if视为二元运算。 -
刚试过..同样的结果。当然然后:) ?- write_canonical(x if y)。如果(x,y)
标签: prolog operator-precedence canonical-form