1

defining IF like this :

dynamic(if/1).

op(200,  fx, if).
op(150, xfx, then).
op(100, xfy, and).
op(100, xfy, or).

generates the following canonical form :

?- 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))

Is there a way to generate :

if( conds, then(actions) ).

OR even better :

if( conds, (actions) ).

like this :

if(x,y)
if(x, then(y))
if( and(x,or(z,t)),  then(y))
if( and(x,or(z,t)),  (y))

one possible alternative I can see :)

?- op(200,  xfy, ==>).

?- write_canonical(x ==> y).
 ==>(x,y)

?- write_canonical(x and z ==> y).
 ==>(and(x,z),y)
sten
  • 7,028
  • 9
  • 41
  • 63
  • Did you try defining `if` as a binary operator instead of a unary operator as you currently show it? `if(x, y)`, `if(x, then(y))`, etc, treat `if` as a binary operation. – lurker Feb 03 '18 at 22:59
  • just tried.. the same result. of course then :) ?- write_canonical(x if y). if(x,y) – sten Feb 03 '18 at 23:07

1 Answers1

0

I found better solution to generate normal clauses. Instead of "then" I can just use ":-"

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

?- assert(if x and z :- write(axz) ).
?- if x and z.
axz
sten
  • 7,028
  • 9
  • 41
  • 63
  • 2
    That visually might work, but semantically, it's backwards from normal Prolog. When `:-` is used in the context of `head :- clause_body`, the meaning is *`head` is true **if** `clause_body` succeeds*. – lurker Feb 04 '18 at 02:50