Home > Archive > Prolog > May 2006 > Control structures in Prolog
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Control structures in Prolog
|
|
|
| Hi all,
I'm a real newbye with Prolog and, coming from other programming fields
(namely, C/C++) I need a little help in understanding how to implement
control structure such as if and for in Prolog.
I understood I should use recursion in order to implement a for cycle,
is it right?
For example, I would apply a constraint to a set of variable. Using
pseudo-code I would write:
for i in SetOfVariables
apply_constraint_to(i)
next i
How should I translate it in Prolog?
Moreover, is it possible to apply a constraint depending on the value of
the variable, that is, implementing an if construct?
if value_of(i) == n then apply_constraint_to(i)
else do_something_else(i)
I would appreciate your help, hints or references on docs.
TIA
Libra
| |
|
|
|
| Hi, I always write as below.
I think parentheses are very useful in Prolog programming.
> for i in SetOfVariables
> apply_constraint_to(i)
> next i
>
> How should I translate it in Prolog?
forall(member(X,SetOfVariables),(
apply_constraint_to(X)
))
> if value_of(i) == n then apply_constraint_to(i)
> else do_something_else(i)
( value_of(X,V), V == n
-> apply_constraint_to(X)
; do_something_else(X)
)
--Shinichi
| |
| Cesar Rabak 2006-05-10, 7:03 pm |
| knd escreveu:
> Hi, I always write as below.
> I think parentheses are very useful in Prolog programming.
>
>
>
>
> forall(member(X,SetOfVariables),(
> apply_constraint_to(X)
> ))
>
>
>
>
> ( value_of(X,V), V == n
> -> apply_constraint_to(X)
> ; do_something_else(X)
> )
>
A question to c.l.p: would you think this could be added to the FAQ?
--
Cesar Rabak
| |
| Joachim Schimpf 2006-05-10, 7:03 pm |
| knd wrote:
> Hi, I always write as below.
> I think parentheses are very useful in Prolog programming.
>
>
> forall(member(X,SetOfVariables),(
> apply_constraint_to(X)
> ))
Wrong, this does nothing. (Assuming the common definition
of forall/2 and assuming that apply_constraint_to(X) really
only applies a constraint to X).
>
If you already know that the value of i is n, what's the
point of applying a constraint to it? You already know the
result.
[color=darkred]
>
> ( value_of(X,V), V == n
> -> apply_constraint_to(X)
> ; do_something_else(X)
> )
Probably doesn't do what you want . (Assuming you are really
using constraints and are not only using confusing terminology).
-- Joachim
| |
|
| knd ha scritto:
> Hi, I always write as below.
> forall(member(X,SetOfVariables),(
> apply_constraint_to(X)
> ))
Maybe I did not understand your answer, but I'm using GnuProlog and
forall and value_of do not seem to be standard predicates. I only found
the predicates repeat/0 (ISO) and for/3 (GNUProlog)
To which Prolog flavour are you referring :-) ?
Any other idea?
Libra
| |
|
| Joachim Schimpf ha scritto:
> knd wrote:
> Wrong, this does nothing. (Assuming the common definition
> of forall/2 and assuming that apply_constraint_to(X) really
> only applies a constraint to X).
Is forall/2 a standard predicate? In GnuProlog I can't find it.
>
> If you already know that the value of i is n, what's the
> point of applying a constraint to it? You already know the
> result.
Of course, you're right. This seems to be not the best example, anyway I
would be able to perform an "action" (sorry for my poor terminology, as
I said I'm a real beginner) depending on the value of a variable.
As an example, assuming that I would solve a constraint optimization
problem, I would solve several constraint satisfaction problems, adding
each time a new constraint forcing the further solutions to have a
better value. I know there is a GNUProlog predicate (fd_maximize/2 and
fd_minimize/2) devoted to this, but I would try to do it by myself to
better understand how it works.
Thanks
Libra
| |
| Markus Triska 2006-05-10, 10:00 pm |
| Hi,
> I would try to do it by myself to better understand how it works.
You can do it like this in SWI Prolog:
:- use_module(library(bounds)).
maximize(M0, Vs0, Max, Vs) :-
copy_term(M0-Vs0, M1-Vs1),
once(label(Vs1)),
maximize_(M0-Vs0, M1-Vs1, Max, Vs).
maximize_(Ms, M0-Vs0, Max, Vs) :-
copy_term(Ms, M1-Vs1),
( M1 #> M0, label(Vs1) ->
maximize_(Ms, M1-Vs1, Max, Vs)
;
(M0,Vs0) = (Max,Vs)
).
?- length(Vs, 3), Vs in 0..1, sum(Vs, #=, Sum), maximize(Sum, Vs, Max, Ms).
Max = 3
Ms = [1, 1, 1] ;
All the best,
Markus.
| |
|
| Libra wrote:
> Maybe I did not understand your answer, but I'm using GnuProlog and
> forall and value_of do not seem to be standard predicates. I only found
> the predicates repeat/0 (ISO) and for/3 (GNUProlog)
> To which Prolog flavour are you referring :-) ?
Sorry, forall/2 predicate is not a standard predicate
(defined in SWI-Prolog), and the exapmple I wrote is wrong.
I think it would be applied:
foreach(Cond,Action) :-
Cond,Action,fail; true.
foreach(member(X,SetOfVariables),(
apply_constraint_to(X)
))
--Shinichi
| |
|
| knd ha scritto:
> Libra wrote:
> foreach(Cond,Action) :-
> Cond,Action,fail; true.
> foreach(member(X,SetOfVariables),(
> apply_constraint_to(X)
Great, it works pretty well.
Thanks a lot.
Libra
| |
| Joachim Schimpf 2006-05-11, 7:02 pm |
| Libra wrote:
> knd ha scritto:
>
> Great, it works pretty well.
No it doesn't:
?- foreach(member(X, [1, 2, 3]), X < 0).
X = X
Yes (0.00s cpu)
You would expect that to fail, wouldn't you?
Libra wrote:
> I understood I should use recursion in order to implement a for cycle,
> is it right?
Yes.
-- Joachim
| |
|
| Joachim Schimpf ha scritto:
> Libra wrote:
>
> No it doesn't:
>
> ?- foreach(member(X, [1, 2, 3]), X < 0).
> X = X
> Yes (0.00s cpu)
> You would expect that to fail, wouldn't you?
Yes, you're right. At least, it works pretty well for me :-) .
| ?- foreach(member(X,[a,b,c]), (write(X),write('\n'))).
a
b
c
Anyway, I tried just the above example that do not implies math
comparison. Can you also explain why it fails in your example? Is it
related to the mathematical comparison?
Thanks
Libra
|
|
|
|
|