Forward-chaining rule interpreter

Description

A forward chaining rule interpreter.

Source: PROLOG programming for artificial intelligence, 3rd Edition, Harlow, 2001, ISBN 0-201-40375-7.

Download

Listing

% Figure 15.7  A forward chaining rule interpreter.
 
:- op( 900, fy, not).
 
% not Goal): negation as failure; 
%   Note: This is often available as a built-in predicate,
%   often written as prefix operator "\+", e.g. \+ likes(mary,snakes)
 
not Goal  :-
  Goal, !, fail
  ; 
  true.
 
:-  op( 800, fx, if).
:-  op( 700, xfx, then).
:-  op( 300, xfy, or).
:-  op( 200, xfy, and).
 
% Simple forward chaining in Prolog
 
forward  :-
   new_derived_fact( P),             % A new fact   
   !,
   write( 'Derived: '), write( P), nl,
   assert( fact( P)),
   forward                           % Continue   
   ;
   write( 'No more facts').          % All facts derived
 
new_derived_fact( Concl)  :-
   if Cond then Concl,               % A rule   
   not fact( Concl),                 % Rule's conclusion not yet a fact
   composed_fact( Cond).             % Condition true? 
 
composed_fact( Cond)  :-
   fact( Cond).                      % Simple fact 
 
composed_fact( Cond1 and Cond2)  :-
   composed_fact( Cond1),
   composed_fact( Cond2).            % Both conjuncts true 
 
composed_fact( Cond1 or Cond2)  :-
   composed_fact( Cond1)
   ;
   composed_fact( Cond2).

Comments

pl/prolog/pllib/forward-chaining_rule_interpreter.txt · ostatnio zmienione: 2017/07/17 08:08 (edycja zewnętrzna)
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0