gpt4 book ai didi

data-structures - 如何识别 Prolog 术语的浪费表示

转载 作者:行者123 更新时间:2023-12-04 07:08:28 46 4
gpt4 key购买 nike

什么是有助于显示 Prolog 术语的浪费表示的 Prolog 谓词?

补充

除了早期的 Prolog SO 答案之外,IIRC 来自 mat ,它使用 Prolog 谓词来分析 Prolog 术语并显示它是如何过于复杂的。

特别是对于像这样的术语

[op(add),[[number(0)],[op(add),[[number(1)],[number(1)]]]]]

它透露这有很多 [] .

我搜索了我的 Prolog 问题并查看了两次答案,但仍然找不到。我还记得它不在 SWI-Prolog 中,而是在另一个 Prolog 中,所以我可以使用带有 online version 的谓词而不是安装另一个 Prolog。序言。

如果您阅读评论中的内容,您会看到垫子标识了 post我正在寻找。

我所寻求的

关于 的选择,我有最后一点说明代表 .请尝试以下操作,例如使用 GNU Prolog 或任何其他符合标准的 Prolog 系统:
| ?- write_canonical([op(add),[Left,Right]]). '.'(op(add),'.'('.'(_18,'.'(_19,[])),[]))

This shows that this is a rather wasteful representation, and at the same time prevents uniform treatment of all expressions you generate, combining several disadvantages.

You can make this more compact for example using Left+Right, or make all terms uniformly available using for example op_arguments(add, [Left,Right]), op_arguments(number, [1]) etc.


Evolution of a Prolog data structure

If you don't know it already the question is related to writing a term rewriting system in Prolog that does symbolic math and I am mostly concentrating on simplification rewrites at present.

Most people only see math expressions in a natural representation

x + 0 + sin(y)

并且计算机程序员意识到大多数编程语言在使用之前必须解析数学表达式并将其转换为 AST
add(add(X,0),sin(Y))

但是大多数编程语言不能使用上面写的 AST 并且必须创建数据结构见: Compiler/lexical analyzer , Compiler/syntax analyzer , Compiler/AST interpreter

现在,如果您在学习 Prolog 时做过的不仅仅是将脚趾浸入水中,您将会遇到 Program 3.30 Derivative rules ,包含在 this 中,但此人没有给出归属。

如果您尝试使用自己的代码使用 Prolog 进行符号数学运算,您可以尝试使用 is/2但是很快发现不行,然后发现Prolog可以读取以下内容为 compound terms
 add(add(X,0),sin(Y))

这将开始工作,直到您需要访问 functor 的名称为止。并找到 functor/3但随后我们又不得不解析输入,但是正如 mat 和 "The Art of Prolog" 所指出的那样如果要 使结构的名称可访问
 op(add,(op(add,X,0),op(sin,Y)))

现在,人们不仅可以访问表达式的术语,还可以以 Prolog 友好的方式访问运算符。

如果不是因为旁边的垫子,代码仍将使用嵌套列表数据结构,现在正在转换为使用公开结构名称的复合术语。我想知道是否有一个常用的短语来描述,如果没有,应该有一个。

无论如何,新的更简单的数据结构在第一组测试中起作用,现在看看它是否能随着项目的进一步发展而成立。

自己上网试试

使用 GNU Prologtutorialspoint.com进入
:- initialization(main).
main :- write_canonical([op(add),[Left,Right]]).

然后单击执行并查看输出
sh-4.3$ gprolog --consult  file main.pg  
GNU Prolog 1.4.4 (64 bits)
Compiled Aug 16 2014, 23:07:54 with gcc
By Daniel Diaz
Copyright (C) 1999-2013 Daniel Diaz
compiling /home/cg/root/main.pg for byte code...
/home/cg/root/main.pg:2: warning: singleton variables [Left,Right] for main/0
/home/cg/root/main.pg compiled, 2 lines read - 524 bytes written, 9 ms
'.'(op(add),'.'('.'(_39,'.'(_41,[])),[]))| ?-  

enter image description here

Clean vs. defaulty representations

From The Power of Prolog作者:马库斯·特里斯卡

当用 Prolog 术语表示数据时,问自己以下问题:

Can I distinguish the kind of each component from its outermost functor and arity?


如果这成立,则您的表示被称为干净。如果您无法通过最外层的仿函数和元数来区分元素,则您的表示被称为 defaulty,一种结合“default”和“faulty”的文字游戏。这是因为对您的数据进行推理需要一个“默认情况”,如果其他一切都失败了,就会应用该情况。此外,这样的表示阻止了参数索引,并且由于这个缺点被认为是错误的。始终旨在避免违约陈述!而是以更清晰的表示为目标。

最佳答案

请看最后一部分:

https://stackoverflow.com/a/42722823/1613573

它使用 write_canonical/1 显示 规范 术语的表示。

这个谓词在学习 Prolog 时非常有用,有助于清除初学者常见的几个误解。例如,参见最近关于 hyphens 的问题,它也会有所帮助。

请注意,在 SWI 中,输出通常偏离规范的 Prolog 语法,因此在解释 Prolog 语法时我没有使用 SWI。

关于data-structures - 如何识别 Prolog 术语的浪费表示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45102330/

46 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com