% mp.pl - A simple meta interpreter

:- op(1150,xfy,'if'),
:- op(1100,xfy,'or').
:- op(1000,xfy,'and').
:- op(900,fy,'~').

:- dynamic if/2.

% true succeeds.
demo(true) :- !.

% false fails.
demo(false) :- !,fail.

% conjunction
demo((P and Q)) :- !,demo(P),demo(Q).

% disjunction
demo((P or _)) :- demo(P).
demo((_ or Q)) :- !,demo(Q).

% negation 
demo(~P) :- demo(P),!,fail.
demo(~_) :- !.

% apply a rule
demo(P) :- (P if Q),demo(Q).

% fall through to prolog
demo(P) :- 
  builtin(P),
  !,
  call(P).

% succeeds if P is a builtin predicate (i.e., there
% are no pclause rules for P).
builtin(P) :- 
  functor(P,Predicate,Arity),
  functor(P2,Predicate,Arity),
  \+((P2 if _)).
