简体   繁体   English

如何在C中编写一阶逻辑公式?

[英]How to code first order logic formula in C?

I am newbie in C and new to stackoveflow too. 我是C的新手,也是stackoveflow的新手。 I have some problems in coding first oder formula like 我在编码第一个oder公式时遇到一些问题

forall([X],implies(X,f(X)))

Here x is a variable, implies is predicate and f is function. 这里x是一个变量,隐含谓词,f是函数。 It sounds like for all x, x implies function of x i'e f(x). 听起来对于所有x,x都包含x的函数,即f(x)。

using C. Any kind of suggestions and help will be appreciated. 使用C。任何形式的建议和帮助将不胜感激。

First order formulas have boolean propositional parts (in your example, "implies(x,f(x))") and quantifiers ("Forall x"). 一阶公式具有布尔命题部分(在您的示例中为“ implies(x,f(x))”)和量词(“ Forall x”)。

You should already know that coding a function call "f(x)" is coded exactly that way in C. 您应该已经知道,对函数调用“ f(x)”进行编码的方式完全是在C语言中进行的。

You code the propositional part as boolean C code using logic connectives. 您可以使用逻辑连接词将命题部分编码为布尔C代码。 For your example, "implication" isn't a native C operator so you have to substitute slightly different code for it. 对于您的示例,“蕴涵”不是本机C运算符,因此您必须用稍有不同的代码替代它。 In c, the "?" 在c中,“?” operator does the trick. 运算符可以解决问题。 "a?b:c" produces "b" if "a" is true, and "c" otherwise. 如果“ a”为真,则“ a?b:c”产生“ b”,否则为“ c”。 For your example: 例如:

   x?f(x):false

Quantifiers mean you have to enumerate the set of possible values of the quantified variable, which always has some abstract type. 量词意味着您必须枚举量化变量的可能值集,该变量始终具有某种抽象类型。 In logic, this set might be infinite, but it computers it is not. 从逻辑上讲,此集合可能是无限的,但它不是计算机。 In your case, you need to enumerate the set of values which could be "x"s. 在您的情况下,您需要枚举可能是“ x”的一组值。 To do that, you need a way to represent a set; 为此,您需要一种表示集合的方法。 a cheesy way to do that in C is to use an array to hold the set members X, and to iterate through the array: 在C语言中这样做的一种俗气的方法是使用一个数组来保存集合成员X,并遍历该数组:

type_of_x set_of_x[1000];
  ... fill x somehow ...
for(i=1;i<number_of_set_elements;i++)
{   x= set_of_x[i];
      ... evaluate formula ...
}

Since a "forall" is false if any propositional instance is false, you need to exit the enumeration when you find a false example: 由于如果任何命题实例为false,则“ forall”为false,因此当您找到错误的示例时,需要退出枚举:

boolean set_of_x[1000]; // in your example, x must be a boolean variable
// forall x
... fill x somehow ...
final_value=true;
for (i=1;i<number_set_elements; i++)
{  x= set_of_x[i];
   if (x?f(x):false)
      { final_value=false;
         break;
      }
}
... final_value set correctly here...

"exists" is true if any propositional instance is true, so you need to exit the enumeration when you find a true result: 如果任何命题实例为true,则“ exists”为true,因此当您找到真实结果时,您需要退出枚举:

// exists x
... fill x somehow ...
final_value=false;
for (i=1;i<number_set_elements; i++)
{  x= set_of_x[i];
   if (x?f(x):false)
      { final_value=true;
         break;
      }
}
... final_value set correctly here...

If you have multiple quantifiers, you'll end up with nested loops, one loop for each quantifier. 如果您有多个量词,则最终会出现嵌套循环,每个量词一个循环。 If your formula is complex, you'll likely need several intermediate boolean variables to compute the values of the various parts. 如果您的公式很复杂,则可能需要几个中间的布尔变量来计算各个部分的值。

You'll also end up with a variety of "sets" (some arrays, some linked lists, some hash tables) so you'll need to learn how to use these data structures. 您还将最终得到各种各样的“集合”(一些数组,一些链表,一些哈希表),因此您将需要学习如何使用这些数据结构。 Also, your quantified values might not be booleans, but that's OK; 另外,您的量化值可能不是布尔值,但是没关系; you can still pass them to functions that compute boolean values. 您仍然可以将它们传递给计算布尔值的函数。 To compute the FOL for: 为以下目的计算FOL:

 forall p:Person old(p) and forall f:Food ~likes(p,f)

the following code skeleton would be used (details left to the reader): 将使用以下代码框架(详细信息留给读者):

 person array_of_persons[...];
 foods array_of_foods[...]

 for (i=...
 { p=array_of_persons[i];
   is_old = old(p);
   for(j=...
    { f=array_of_foods[j];
      ...
         if (is_old && !likes(p,f)) ...
    }
 }

C is an imperative programming language. C是命令式编程语言。 "Imperative" here means that execution happens via the programmer specifically telling the computer what to do. “即时”在这里意味着执行是通过程序员专门告诉计算机做什么来进行的。

For what you want, Prolog is more appropriate. 对于您想要的, Prolog更合适。 It is based on first-order predicate logic, and execution happens by trying to "find a resolution refutation of the negated query" that's specified by the user's goal. 它基于一阶谓词逻辑,并且通过尝试“找到用户目标所指定的否定查询的解析引用”来执行。 This approach is very unlike C since execution is much more implicit and the expression of intent looks much different. 这种方法与C非常不同,因为执行更加隐式,并且意图表达似乎大不相同。

If you have lots of time, you can write your own constraint solver or Prolog interpreter in C, but by default, C has no first-class support for what you're looking for. 如果您有很多时间,则可以用C编写自己的约束求解器或Prolog解释器,但是默认情况下,C不为您要的内容提供一流的支持。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM