Re: some quick help with lists

From: Tom Schrijvers (Tom.Schrijvers_at_cs.kuleuvenspam.ac.be)
Date: 11/23/04


Date: Tue, 23 Nov 2004 11:27:54 +0100

dirty_bit wrote:

> What I thought would be a simple first program to write in Prolog has
> some how become a problem. I am attempting to write a rule that takes
> a list and an integer. The integer is the sum of two elements in the
> list. (e.g. sum(list, num) ).
>
> I was trying to do something along the lines of:
>
> sum([H|T], A) :- select([H|T],X1,T2), select(T2,X2,_), A is +(X1,X2).
>
> This works just fine, but I would like for it to also be able to go
> the other way too. For example if I gave it sum(X, 3). X would equal:
> [1,2], [2,1], [1,2,3], etc. And it would need to stop at a certain
> list length that I could specify so it doesn't loop forever.
>
> Any suggestions would be greatly appreciated. Perhaps my code needs to
> be completely rewritten?

Arithmetic only works one way in plain Prolog: it is a one way constraint.
Only the A is allowed to be an unknown value. X1 and X2 have to be known.
Constraint Logic Programming extends Prolog with full arithmetic
constraints.

Here is some code that imports the simple integer arithmetic constraint
solver included in SWI-Prolog.

:- use_module(library('clp/bounds')).

% generate a list of variables bounded by some maximal length
variable_list([],_).
variable_list([X|Xs],N) :-
        N > 0,
        M is N - 1,
        variable_list(Xs,M).

% compute sum of elements in list
sum([],S) :-
        S #= 0.
sum([X|Xs],S) :-
        S #= X + S0,
        sum(Xs,S0).

Now you can run your in different modi:

% straightforward:
?- sum([1,2],S).
S = 3 ;

No

% backwards:
?- variable_list(L,3), L in 1..10, sum(L,S), S #= 3, label([S|L]).

L = [3] ;

L = [1, 2] ;

L = [2, 1] ;

L = [1, 1, 1] ;

No

L in 1..10 constraints the variables in the list to be between 1 and 10,
otherwise there's an infinite number of possibilities. Labelling of the
variabels is necessary to instantiate the variables in the list to an
actual solution.

You can add additional constraints to the variabels in the list or drop the
S #= 3 constraint to run in a different modus.

Check the documentation of SWI-Prolog for more possibilities or look at the
constraints solvers included in your favorite Prolog system.

Tom



Relevant Pages

  • Re: ...Contemplating unspecified constraint SOLVERS.
    ... >>using a CLPsolving system that came with the Prolog compiler. ... > explain how to write your own constraint solver. ... > PROLOG source code of Word Puzzle Solver ...
    (comp.lang.prolog)
  • Re: ...Contemplating unspecified constraint SOLVERS.
    ... using a CLPsolving system that came with the Prolog compiler. ... And Ivan Bratko's book nowhere explains how to write your own constraint ... write a constraint solver requires that one understands the CHR, ... I've actually been programming for 30 years or so, ...
    (comp.lang.prolog)
  • Re: N-QUEENS problem with some differences
    ... And a basic constraint, for example, to haven't two men placed on the ... How can we express the other constraints like "all the squares must be ... some snippets of Prolog. ... asking about a solution by Prolog programming. ...
    (comp.lang.prolog)
  • Re: ...Contemplating unspecified constraint SOLVERS.
    ... >order to begin to write one's own constraint *solvers*. ... >using a CLPsolving system that came with the Prolog compiler. ... explain how to write your own constraint solver. ... PROLOG source code of Word Puzzle Solver ...
    (comp.lang.prolog)
  • Re: some quick help with lists
    ... >> What I thought would be a simple first program to write in Prolog has ... > Arithmetic only works one way in plain Prolog: it is a one way constraint. ... > solver included in SWI-Prolog. ...
    (comp.lang.prolog)