我的逻辑哪里出错了? [英] Where did my logic go wrong?

查看:36
本文介绍了我的逻辑哪里出错了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为 prolog 的初学者,只有 Java 编程方面的知识,我发现很难将逻辑与 prolog 规则联系起来......

as a beginner in prolog with only previous knowledge in Java programming, I find it hard to relate a logic into a prolog rule...

以下是我目前的情况,考虑到我得到的结果,我希望有人指出我哪里出错了.

Below is what I have this far, and I would like anyone to point out where I went wrong, considering the results that I got.

shapes(X):-triangle(X); circle(X); quadrilateral(X); withColour(X).

square(sq).
rectangle(rect).
circle(cir).
triangle(tri).
quadrilateral(X).

quadrilateral(X):-square(X).
quadrilateral(X):-rectangle(X).

red(X):-quadrilateral(X).
yellow(X):-quadrilateral(X).

withColour(X):-red(X).
withColour(X):-yellow(X).

fourEqualside(square(X),length).

quadrilateral(X):- \+triangle(X), \+circle(X), shapes(X).

triangle(X):-hasThreeEdge(X).
triangle(X):- \+circle(X), \+rectangle(X), \+withColour(X), shapes(X), hasThreeEdge(X).

circle(X):-\+quadrilateral(X), \+triangle(X),\+withColour(X), shapes(X).

以下是我的假设:

1) 宇宙只有三种形状:三角形、圆形和四边形.

1) the universe has only 3 types of shapes: triangle, circle and quadrilateral.

2) 四边形可以是正方形也可以是长方形(不能同时是).

2) a quadrilateral can either be a square or a rectangle (NOT BOTH).

3) 任何有颜色的东西都不能是四边形,但四边形必须只有红色或黄色.

3) anything with colour may NOT be a quadrilateral, but a quadrilateral MUST have either RED or YELLOW colour ONLY.

4) 正方形是唯一有 4 条边相等的四边形

4) a square is the ONLY quadrilateral that has 4 equal sides

5) 三角形是唯一有 3 条边的形状.

5) a triangle is the ONLY shape with 3 edge.

6) 三角形、圆形和四边形都是不同的,并且只能是其中之一(即属性不重叠).

6) triangle, circle and quadrilateral are all distinct, and can be ONLY either one (i.e, no overlapping of properties).

以下是我查询 prolog 源时的内容.

The below is what I have when I query the prolog source.

1) 圆(三).-false(红色).

1) circle(tri). -false (in red).

2) 圆(矩形).错误(红色).

2) circle(rect). false (in red).

3) 圆(cir).没有 '.' 为真(黑色),红色假.

3) circle(cir). true without '.' (in black), false in red.

4) 四边形(矩形).真(黑)、真(黑)、出本地栈(红)

4) quadrilateral(rect). true (black), true (black), out of local stack (red)

5) 四边形(cir).真(黑),假(红)

5) quadrilateral(cir). true (black), false (red)

6) 四边形(tri).真(黑),假(红)

6) quadrilateral(tri). true (black), false (red)

7) 三角形(矩形).假(红色)

7) triangle(rect). false (red)

8) 三角形 (cir).假(红色)

8) triangle(cir). false (red)

9) 三角形(tri).真(黑)假(红)

9) triangle(tri). true (black) false (red)

10) hasThreeEdge(rect).错误.顶级未定义过程:swim/1(红色)<-这是什么意思?当我查询圆形和三角形时,答案也是一样的!

10) hasThreeEdge(rect). ERROR. top-level undefined procedure: swim/1 (red) <- what does this mean? answer is same when i query circle and triangle too!

这似乎是我做错了什么......

This seems like there is something that I've done wrong...

推荐答案

我认为您将知识表示到 Prolog 中有点间接.让我们从您的假设开始:

I think your representation of knowledge into Prolog is a little indirect. Let's start with your assumptions:

1) 宇宙只有三种形状:三角形、圆形和四边形.

1) the universe has only 3 types of shapes: triangle, circle and quadrilateral.

这表明我们可能想要定义一个名为 shape 的事实/谓词并断言其中三个:

This indicates we might want to define a fact/predicate called shape and assert three of them:

shape(triangle).
shape(circle).
shape(quadrilateral).

如果你再查询,shape(X)(X 是一个形状),Prolog 会回应:

If you then query, shape(X) (X is a shape), Prolog will respond with:

X = triangle
X = circle
X = quadrilateral

2) 四边形可以是正方形也可以是长方形(不能同时是).

2) a quadrilateral can either be a square or a rectangle (NOT BOTH).

假设 1 表示只有三种形状:三角形、圆形或四边形.但是假设#2 违反了假设#1.假设我们通过添加更多形状来解决这个问题:

Assumption #1 says there are only three shapes: triangle, circle, or quadrilateral. But assumption #2 violates assumption #1. Let's suppose we fix this by adding a couple more shapes:

shape(square).
shape(rectangle).

然后我们可以包括,例如:

Then we can include, for example:

kind(rectangle, quadrilateral).
kind(square, rectangle).

kind_of(X, Y) :- kind(X, Y).
kind_of(X, Y) :- kind(X, Z), kind(Z, Y).

那么kind_of(square, rectangle)为真,kind_of(rectangle, quadrilateral)为真,kind_of(square, quadrilateral)是真的.

Then kind_of(square, rectangle) is true, kind_of(rectangle, quadrilateral) is true, and kind_of(square, quadrilateral) is true.

| ?- kind_of(X, Y).

X = rectangle
Y = quadrilateral ? ;

X = square
Y = rectangle ? ;

X = square
Y = quadrilateral

yes

最初的假设也有缺陷,因为正方形确实是一种矩形.所以一个四边形既可以是正方形也可以是长方形,如果它是正方形的话.

The original assumption is also a flawed in that a square is, indeed, a type of rectangle. So a quadrilateral can be both a square and a rectangle if it is square.

3) 任何有颜色的东西都不能是四边形,但四边形必须只有红色或黄色.

3) anything with colour may NOT be a quadrilateral, but a quadrilateral MUST have either RED or YELLOW colour ONLY.

这个有点不清楚.可能,您的意思是可能或不?或者你的意思是必须?如果必须,那么这显然是矛盾的.所以我认为这意味着可能会或可能不会?

This is a little unclear. By may, do you mean may or may not? Or do you mean must? If must, then this is clearly a contradiction. So I assume this means may or may not?

您需要引入有效的颜色.一种方法是:

You need to introduce valid colors. One way would be:

color(red).
color(yellow).
color(black).
...

您可以指出形状具有哪些有效颜色:

You can indicate what valid colors a shape has:

shape_color(quadrilateral, red).
shape_color(quadrilateral, yellow).

shape_color(triangle, X) :- color(X).   % a triangle can be any valid color

4) 正方形是唯一有 4 条边相等的四边形

4) a square is the ONLY quadrilateral that has 4 equal sides

请参考我在 #2 中的评论.需要一些关于尺寸或其他详细形状属性的信息.甚至可以将属性作为每个形状的一部分(例如, square(S) 用于边长 S 的正方形,或 triangle(S1,S2,S3) 表示具有给定边长的三角形).不过,我假设您只想泛泛地引用这些形状.所以我们需要关于它们几何形状的事实.您如何定义这些以及它们的详细程度完全取决于您的需求.但为了让事情简单化:

Refer back to my comment in #2. There needs to be some information about dimensions or other detailed shape attributes. One could go so far as to have the attributes be part of each shape (e.g., square(S) for a square of side length S, or triangle(S1,S2,S3) for a triangle with the given side lengths). I'll assume, though, you only want to refer to the shapes generically. So we need facts about their geometry. How you define these and how detailed they are depends totally upon your needs. But to keep things simplistic:

equal_sides(square, 4).    % all 4 sides equal
equal_sides(rectangle, 2). % opposing 2 sides equal

equal_sides(square, 4). 的查询为真,

5) 三角形是唯一有 3 条边的形状.

5) a triangle is the ONLY shape with 3 edge.

这可以使用一组关于形状有多少条边的事实来完成:

This could be done using a set of facts about how many sides a shape has:

sides(triangle, 3).
sides(square, 4).
sides(rectangle, 4).
sides(quadrilateral, 4).

6) 三角形、圆形和四边形都是不同的,并且只能是其中之一(即属性不重叠).

6) triangle, circle and quadrilateral are all distinct, and can be ONLY either one (i.e, no overlapping of properties).

这自然发生于上述所有情况.

This occurs naturally from all of the above.

上述方法可以支持许多查询和其他谓词.如果你有一个变量,Shape,你可以用shape(Shape). 来询问它是否是一个有效的形状.您还可以通过 kind_of(Shape, Kind) 来查看它是否是某种"其他东西. 更具体地说,您可以通过 kind_of(形状,四边形. 查询 kind_of(circle, rectangle) 会返回 false(圆形不是矩形,甚至不是一种矩形).查询 sides(rectangle, 3). 为假.

The above approach could support a number of queries and other predicates. If you have a variable, Shape, you can ask if it's a valid shape with, shape(Shape).. You can also see if it's a "kind of" something else via, kind_of(Shape, Kind). More specifically, you can ask if a shape is a kind of quadrilateral by, kind_of(Shape, quadrilateral). The query, kind_of(circle, rectangle) would come back false (a circle is not a rectangle, or even a kind of rectangle). The query, sides(rectangle, 3). is false.

关于维度信息,如果您对表示特定对象感兴趣,例如给定半径的圆或给定边长的矩形,您可以增加形状的表示以包含这些属性.例如,您可以使用 circle(R) 而不是 circle,其中 R 是一些任意单位的半径.矩形可以是 rectangle(L, W) 等.您的 shape 事实可能看起来像,shape(circle(_)).形状(四边形(_,_,_,_)).

Regarding dimensional information, if you were interested in representing specific objects, like a circle of a given radius, or a rectangle of given side lengths, you could augment the representation of a shape to include these attributes. For example, instead of just circle you could have circle(R) where R is the radius in some arbitrary units. A rectangle could be rectangle(L, W), etc. Your shape facts could look like, shape(circle(_)). shape(quadrilateral(_,_,_,_)). etc.

您如何实现这一点在很大程度上取决于您想如何使用它.但最重要的是决定以合理的方式表示知识,然后表示规则.此外,一般来说,我会尽量避免根据否定的结合(\+)定义规则,并尝试以积极的方式定义事物,并让规则或事实的缺失导致失败.

How you implement this depends heavily upon how you want to use it. But the most important thing is deciding on reasonable ways of representing the knowledge, and then representing the rules. Also, in general, I would try to avoid defining a rule in terms of a conjunction of negations (\+) and try to define things in a positive way, and let the absence of a rule or fact result in failure.

这篇关于我的逻辑哪里出错了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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