Monday, 5 May 2008

Prolog CHR - Prime Seive One Liner


%% http://www.cs.kuleuven.be/~dtai/projects/CHR/

:- use_module(library(chr)).
:- chr_constraint prime/1.

prime(X) \ prime(Y) <=> 0 is Y mod X | true.

%% This simpagation rules states:
%% given there are clauses prime(X) and prime(Y)
%% such that 0 is Y mod X, remove the clause prime(Y).


%% To try it out:
primes(1). primes(N) :- prime(N), succ(M,N), primes(M).
%% ?- primes(50) % generate all candidates <= 50
%% % the CHR rule seives out those which are not prime

No comments: