:-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) :- macro(X if Y then Z,Out,_).

macro(X,Y,Cf) :- once(macro1(X,Y,Cf)).

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

macro1(X0 or Y0 ,(X,Y)    ,max(Cf1,Cf2)) :- macro(X0, X, Cf1), macro(Y0, Y, Cf2).
macro1(X0 and Y0,(X,Y)    ,min(Cf1,Cf2)) :- macro(X0, X, Cf1), macro(Y0, Y, Cf2).
macro1(not X0   ,X        ,(1 - Cf)) :- macro(X0,X,Cf).
macro1(X at Cf  ,X        ,Cf).
macro1(X        ,X at Cf  ,Cf).

demo1 :-
    R = (rule1 if a and b or not c then d),
    macro(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 .

/*
% demo1
% how the rule is stored iternally:
rule(rule1, d, A) :-
	(   (   a at B,
	b at C
	),
	c at D
	),
	A is max(min(B, C), 1-D).
*/
/*
% demo2
# using the rule

rule1/d at 0.8
*/
