Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

CHR(FD) syntax standard

21 views
Skip to first unread message

A. K.

unread,
Feb 8, 2012, 2:01:10 PM2/8/12
to
Dear all

I wonder what would be the standard (or at least most widely used)
syntax for CHR programming?

I installed BProlog, Yap and ECLiPSe on Win7. All claim to have CHR
solvers. From Wikipedia I copied
(http://en.wikipedia.org/wiki/Constraint_programming)

sendmore(Digits) :-
Digits = [S,E,N,D,M,O,R,Y], % Create variables
Digits :: [0..9], % Associate domains to variables
S #\= 0, % Constraint: S must be different from 0
M #\= 0,
alldifferent(Digits), % all the elements must take
different values
1000*S + 100*E + 10*N + D % Other constraints
+ 1000*M + 100*O + 10*R + E
#= 10000*M + 1000*O + 100*N + 10*E + Y,
labeling(Digits). % Start the search

but it runs only with BProlog. Why?

I am not familiar with modern Prolog dialects (having only programmed in
Turbo Prolog in the pre-Windoze era) but at least I managed to smith
together in record time a small Sudoku solver in BProlog. :-) But before
I continue with my experiments, I want to know which system is ideal for
learning, and which one is 'exotic'.

Thanks to all who provide their great works and ideas in logic
programming for free
to simple humans like me! :-)

Joachim Schimpf

unread,
Feb 8, 2012, 8:36:37 PM2/8/12
to
A. K. wrote:
> Dear all
>
> I wonder what would be the standard (or at least most widely used)
> syntax for CHR programming?
>
> I installed BProlog, Yap and ECLiPSe on Win7. All claim to have CHR
> solvers. From Wikipedia I copied
> (http://en.wikipedia.org/wiki/Constraint_programming)
>
> sendmore(Digits) :-
> Digits = [S,E,N,D,M,O,R,Y], % Create variables
> Digits :: [0..9], % Associate domains to variables
> S #\= 0, % Constraint: S must be different from 0
> M #\= 0,
> alldifferent(Digits), % all the elements must take
> different values
> 1000*S + 100*E + 10*N + D % Other constraints
> + 1000*M + 100*O + 10*R + E
> #= 10000*M + 1000*O + 100*N + 10*E + Y,
> labeling(Digits). % Start the search
>
> but it runs only with BProlog. Why?

This is a program with FD constraints. There are no CHR constructs.

For ECLiPSe, prefix the code with a directive to load a suitable solver, e.g.
:- lib(ic).


-- Joachim

Tomasz Budzeń

unread,
Feb 18, 2012, 4:59:59 PM2/18/12
to
I think that the problem is in operators' approach - different Prolog systems defines different operators for CLP(FD) and also imperative way of thinking. But we could define above problem with relational approach, something like that:

sendmore(Digits) :-
clp_fd_problem
(
SendmoreProblem,
variable_set(Digits, [S, E, N, D, M, O, R, Y]),
domain(Digits, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]),
constraints
(
[
different(S, 0),
different(M, 0),
different(Digits),
relation_eq(1000*S + ..., 10000*M + ..)
]
)
),
solver(SendmoreProblem). % Optional, can be handled by clp_fd_problem

Personally I don't like operators in CLP and I think that solver should get as input problem defined as tree of relations (I don't mean translating syntax sugar back to relations, but top-down approach).

Regards,
Tomasz Budzen

Michael Moeller

unread,
Feb 19, 2012, 9:43:54 AM2/19/12
to
Syntax varies depending on the Prolog you use. The example is clp(FD).
GNU-Prolog, BProlog and ECLiPSe used to implement CLP(FD),
Yap CHR and CLP(R) and XSB CHR. SWI has CHR, CLP(FD) and CLP(R, Q).

In gprolog the example will be something like this (taken from the gprolog
distribution):

sendmoremoney(Vars) :-
Vars = [S,E,N,D,M,O,R,Y],
fd_domain(Vars, 0, 9),
S #\= 0,
M #\= 0,
fd_all_different(Vars),
1000*S + 100*E + 10*N + D
+ 1000*M + 100*O + 10*R + E
#= 10000*M + 1000*O + 100*N + 10*E + Y.

send :- Vars=[S,E,N,D,M,O,R,Y], sendmoremoney(Vars), fd_labeling(Vars),
write(Vars), nl.


q :- statistics(runtime,_), send,
statistics(runtime,[_,Y]),
write('time : '), write(Y), nl.

:- initialization(q).



CHR in Yap is like this (just to give you an impression)
% Sieve of Erathostenes (Constraint Handling Rules, Holzbaur/Fruehwirth)

:- use_module(library(chr)).
handler erathostenes.
constraints primes/1, prime/1.

primes(1) <=> true.
primes(N) <=> N > 1 | M is N - 1, prime(N), primes(M).

absorb(J) @ prime(I) \ prime(J) <=> J mod I =:= 0 | true.


Most Prologs come with some examples. In addition lots of
documents are freely available on the internet.


Regards,
Michael

0 new messages