:-op( 999, xfx, if   ).
:-op( 998, xfx, then ).
:-op( 997, xfy, or   ).
:-op( 996 ,xfy, and  ).
:-op( 995 , fy, not  ).
:-op(   1 ,xfx, at   ).

term_expansion(X if Y then Z,Out) :-
    xpand(X if Y then Z,Out,_).



xpand(X,Y,Cf) :- once(xpand1(X,Y,Cf)).

xpand1(X if Lhs0 then Rhs,
      (rule(X, Rhs,Cf) :- Lhs,Cf is Cf0),Cf) :-
    xpand(Lhs0, Lhs,Cf0).

xpand1(X0 or Y0,(X,Y), max(Cf1,Cf2)) :-
    xpand(X0, X, Cf1),
    xpand(Y0, Y, Cf2).

xpand1(X0 and Y0,(X,Y), min(Cf1,Cf2)) :-
    xpand(X0, X, Cf1),
    xpand(Y0, Y, Cf2).

xpand1(not X0, X, (1 - Cf)) :-
    xpand(X0,X,Cf).

xpand1(X at Cf,X,Cf).

xpand1(X,X at Cf,Cf).

demo1 :-
    R = (rule1 if a and b or not c then d),
    xpand(R,Z,_),
    portray_clause(Z).

demo2 :-
    rule(Label, Conclusion, Certainty),
    print(Label/Conclusion  at Certainty).

rule1 if a and b or not c then d.

a at 0.7.
b at 1.
c at 0.2 .

