如何在不可更改的条件下在 Prolog 中模拟 while 循环? [英] How Can I simulate a while loop in Prolog with unchangeable conditions?

查看:57
本文介绍了如何在不可更改的条件下在 Prolog 中模拟 while 循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以基本上我试图在 Prolog 中模拟一些 C 代码.

So basically I am trying to simulate some C code in Prolog.

在 Prolog 中很容易模拟 while 循环,情况如下:

It is easy to simulate while loop in Prolog Here is the case:

C 代码:

int a = 1;

while(N)
{
  N--;
  a++; 
}

序言代码:

prolog_while(0) : !
prolog_while(N, A) :
   N1 is N -1,
   A1 is A + 1,
prolog_while(N1, A1).

一个问题是如何在 Prolog 中模拟一个条件不变的 while 循环?

One problem is how to simulate a while loop in Prolog with unchangeable conditions?

情况如下:

int n = 1;
int a = 1;

while(1)
{
   if (N==0)
     goto while_end;
   else
    {
        N--; A++;
    }       
}

或者

while(1)
{
   if (N==0)
     break;
   else
    {
        N--; A++;
    }       
}

我知道这有点奇怪,但基本上这些C代码是由源代码分析工具自动生成的,所以我必须处理它...

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...

那么基本上我如何在 Prolog 中进行模拟?可行吗?

Then basically how can I simulate in Prolog? Is it doable?

有人可以帮我吗?

====================更新============

===================update============

我尝试用这种方式写了一些Prolog代码,但基本上我仍然不知道如何处理test子句.

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).

推荐答案

在 Prolog 中进行无限循环最明显的方法是使用 repeat/0,它看起来像这样:

The most obvious way to do an infinite loop in Prolog is with repeat/0, and it looks like this:

while(1)
    do_something();

变成

repeat,
do_something.

真正的问题变成了 Prolog 中没有明显的 gotobreak 类似物.所以我倾向于寻找这样的模式:

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();
}

并像这样将其转换为 Prolog:

and convert it to Prolog like this:

loop_entry :-
  test,
  do_something,
  loop_entry.

您当然需要将您的局部变量作为参数添加到 loop_entry/0 并实现 test,但是这样当 test 失败时循环将自然结束.

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.

以 N 和 A 为例会导致这种情况:

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).

在这种情况下,测试"只是 N >0.如果它不是真的,谓词将简单地失败,您可以按照 Prolog 的方式继续生活.

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.

编辑#2.如果您想要结果(N 和 A),请为您想要返回的值添加额外的参数并再添加一个子句:

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.

这篇关于如何在不可更改的条件下在 Prolog 中模拟 while 循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆