【问题标题】:IF-THEN in canonical form?IF-THEN 的规范形式?
【发布时间】: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


【解决方案1】:

我找到了生成普通子句的更好解决方案。而不是“那么”,我可以只使用“:-”

?- write_canonical(if x and z :- y ).
:-(if(and(x,z)),y)

?- assert(if x and z :- write(axz) ).
?- if x and z.
axz

【讨论】:

  • 这在视觉上可能有效,但在语义上,它与普通的 Prolog 相比是倒退的。当:-head :- clause_body的上下文中使用时,意思是head为真如果clause_body成功
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-06-24
  • 2018-09-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-11
相关资源
最近更新 更多