So basically I am trying to simulate some C code in Prolog.
It is easy to simulate while loop in Prolog Here is the case:
C code:
int a = 1;
while(N)
{
N--;
a++;
}
Prolog code:
prolog_while(0) : !
prolog_while(N, A) :
N1 is N -1,
A1 is A + 1,
prolog_while(N1, A1).
One problem is how to simulate a while loop in Prolog with unchangeable conditions?
Here is the case:
int n = 1;
int a = 1;
while(1)
{
if (N==0)
goto while_end;
else
{
N--; A++;
}
}
Or
while(1)
{
if (N==0)
break;
else
{
N--; A++;
}
}
I know it is kinda of weird but basically these kind of C code is automatically generated by a source code analysis tool, so I have to deal with it...
Then basically how can I simulate in Prolog? Is it doable?
Could anyone give me some help?
===================update============
I tried to write some Prolog code in this way, but basically I still don't know how to handle test clause.
main :- loop_entry(2, 1), write(N), nl, write(A), nl.
do(A, N, A1, N1) :- A1 is (A + 1), N1 is (N - 1).
test(N) :- ... <----- # How to write this part?
loop_entry(N, A) :-
test(N),
do(A, N, A1, N1),
loop_entry(N1,A1).
SystemVerilog while and do-while loop Both while and do while are looping constructs that execute the given set of statements as long as the given condition is true. A while loop first checks if the condition is true and then executes the statements if it is true. If the condition turns out to be false, the loop ends right there.
Loops in Prolog. The looping facility is contained in most of the programming languages. Looping is used to enable a set of instructions to be repeatedly executed either a fixed number of times or until a given condition met. Prolog has no looping facility, but we can obtain a similar effect. Using this effect, we can evaluate a sequence ...
In Prolog, there is no such facility available directly, but using recursion, we can obtain a similar effect, which is shown in the following programs: This program outputs the integer value from a specified value down to 1. loop (0). loop (N) :- N>0, write ('value of N is: '), write (N), nl. S is N-1, loop (S).
A while loop first checks if the condition is true and then executes the statements if it is true. If the condition turns out to be false, the loop ends right there.
The most obvious way to do an infinite loop in Prolog is with repeat/0
, and it looks like this:
while(1)
do_something();
becomes
repeat,
do_something.
The real problem then becomes that there's no obvious analog to goto
or break
in Prolog. So I would be inclined to look for a pattern like this:
while(1) {
if (test)
break;
do_something();
}
and convert it to Prolog like this:
loop_entry :-
test,
do_something,
loop_entry.
You will of course need to add your local variables as parameters to loop_entry/0
and implement test
, but this way when test
fails the loop will end naturally.
Following your example with N and A leads to this kind of thing:
loop_entry(N, A) :-
N > 0,
succ(N0, N),
succ(A, A1),
loop_entry(N0, A1).
The "test" in this case is simply N > 0
. If it isn't true, the predicate will simply fail and you can go on with life the Prolog way.
Edit #2. If you want the results (N and A) then add additional parameters for the values you want to return and add one more clause:
loop_entry(N, A, ResultN, ResultA) :-
N > 0, !,
succ(N0, N),
succ(A, A1),
loop_entry(N0, A1, ResultN, ResultA).
loop_entry(N, A, N, A).
You can either add a cut after the condition or put the inverse condition in this new clause.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With