这个 Prolog 术语是否正确?(事实、规则、程序、谓词……) [英] Is this Prolog terminology correct? (fact, rule, procedure, predicate, ...)

查看:61
本文介绍了这个 Prolog 术语是否正确?(事实、规则、程序、谓词……)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正确使用术语是成功传达概念的一部分,如果在 SO 中使用错误的术语并带有 Prolog 标签,受访者会很好地指出错误.

阅读 William F. Clocksin 于 1997 年撰写的条款和效果 - 工作程序员的序言编程"(WorldCat) 是段落

<块引用>

Prolog 程序由一系列过程组成.每个过程定义了一个特定的谓词,即某种关系参数之间.一个过程由一个或多个断言组成,或条款.考虑两种子句很方便:事实规则.

虽然我理解所有单词,但在交流 Prolog 时,每个粗体单词是否都是当前正确使用的术语?

特别是规则的使用似乎不受欢迎.

解决方案

来自 Prolog ISO 标准

<块引用>

ISO/IEC 13211-1(第一版 1995-06-01)

信息技术 - 编程语言 - Prolog

第 1 部分:通用核心

在第 2-10 页有广泛的词汇表.

<块引用>

3.7 参数:与谓词复合词相关联的.

3.9 arity:复合词参数的数量.在语法上,一个与 functor 相关联的非负整数或谓词.

3.10 assert, to:断言子句就是将它添加到数据库中的用户定义过程谓词定义那个子句.

3.19 主体:一个目标,以其作为规则的一部分的上下文来区分(见 3.154).

3.21 内置谓词:一个过程,其执行处理器实现(见8).

3.32 子句:事实规则.它有两部分:headbody.

3.35 完整数据库 与执行执行相关的一组过程(参见 7.5).

3.37 复合词:NN 正数的函子,以及N 参数.

3.45 控制结构:一个过程,其定义是 Prolog 处理器的一部分(见 7.8).

3.52 数据库:当前在执行期间存在的一组用户定义过程(参见 7.5).

3.57 指令:一个术语 D,影响Prolog文本的含义(见7.4.2)并在 Prolog text 中由 directive-term :-(D) 表示.

3.59 动态(过程):动态过程是可以检查其子句或在执行期间更改,例如通过断言收回子句(见7.5.2).

3.72 事实:一个子句,其正文目标 true.注意 - 在 Prolog text 中,一个事实可以用一个 term 表示,它的 主函子 既不是 (:-)/1(:-)/2.

3.77 函子:一个标识符和一个arity.

3.81 目标:要执行预测(参见正文查询 和 7.7.3).

3.84 head(规则):一个谓词,由其上下文区分.

3.88 标识符:用于表示原子函子名称谓词名称的基本非结构化对象.

3.129 谓词:一个标识符和一个arity.

3.131 谓词指示符:一个复合词 A/N,其中A是一个原子N 是一个非负整数,表示一个特定的过程(见 7.1.6.6).

3.133 谓词:一个带有arity NN谓词> 参数.

3.136 过程:控制结构内置谓词用户定义的过程.过程要么是静态,要么是动态.一种过程要么是私有,要么是公共(见7.5).

3.141 Prolog text:一系列read-terms,表示指令子句(见6.2、7.4).

3.143 查询:作为顶级的交互式输入给出的目标.

3.154 规则:一个子句,其正文不是目标 true.在执行期间,如果body对于某些替换为真,那么head对于那个也为真>替代.规则以Prolog 文本表示,其主函子(:-)/2,其中第一个参数被转换为head,第二个参数转换到正文.

3.164 static(过程):静态过程是一个子句不能改变的过程(见 7.5.2).

3.185 顶级:Prolog 处理器重复输入和执行 * 查询的过程.>

3.195 用户定义的过程:由一系列子句定义的过程,其中head每个子句都有相同的谓词指示符,每个子句Prolog文本或已在执行过程中断言(见 8.9).

注意事项

基本概述

h(A,B,C) :- g(A,B),h(B,C),j(A,C).<------------------------------>- (喇叭)条款,这也是一个规则.<------------------->- 规则的主体,这也是一个目标.... 只有一个字面意思:原子目标.<------>- 规则的头部,可以出现作为目标取决于上下文.f(A,B,C).- 带有省略正文`true`的条款.这是事实,但_不是_规则.也称为单位条款.f(A,B,C) :- 真.- 同上,仍然不是规则.f(A,B,C) :- !.- 这是规则吗?我们不知道!:- f(A,B,C).- 指令.:- (foo(bar)).- 另一个指令.

可以在维基百科喇叭条款的条目中找到略有不同的定义.尤其是事实".被称为没有变量的单元子句"- 这不符合 ISO 定义.

非终结符

除了谓词指示符符号A/N(functor/arity),还有符号A//N,不在 ISO 标准中(或尚未在 ISO 标准中,请参阅 本草稿).它告诉读者这个谓词用于定语从句语法 (DCG) 并取 2输入差异列表"的隐藏参数(或列表差异")对以及指示的参数数量.

在上面指出的标准提案中,它被描述为:

<块引用>

3.19 非终结符:复合词A//N,其中A是一个原子,N 是一个非负整数,表示一个特定的非终端

大多数实现将非终结符 nt//n 转换为谓词 nt/n+2.但是,不能依赖精确的翻译方式和通过调用相应谓词直接调用非终结符的结果,即未定义具有相同名称和两个额外参数的谓词.特别是第二个附加参数必须小心处理.直接使用可能会违反稳定性,特别是在使用 .

关于指令

的说明

指令可以是另一种编写查询的方式.来自SICStus Prolog 手册:

<块引用>

查询和指令是引导系统执行的方式一些目标或目标.(...) 指令的主要用途(与查询)是为了允许文件包含调用各种指令谓词,但您不想打印答案出去.在这种情况下,您只想为其调用谓词效果,即您不希望在中间进行终端交互查阅文件.

指令也可以是源文件标记,其位置很重要(即代码元信息).来自 SWI Prolog 手册关于模块 指令:

<块引用>

该指令只能用作源文件的第一个术语.它将文件声明为模块文件,定义一个名为 Module 的模块.

指令中使用的谓词可能非常奇特,为 Prolog 处理器提供指令和谓词元信息.来自 SWI Prolog 手册关于声明谓词属性":

<块引用>

本节描述了操作属性的指令谓词定义.

例如,加载有限域上的约束逻辑编程库":

:- use_module(library(clpfd)).

头部为空的规则:- foo,可以解释为false :- foo,在Prolog中not用来表达约束foo 从来都不是这种情况".它答案集编程的实现中以这种方式使用,例如"ASP序言"(它具有 Prolog-y 语法,但其他方面与 Prolog 完全不同.

注意内置谓词

在第 8 章第 63 页,我们发现:

<块引用>

"内置谓词是一个过程,它由符合标准的处理器"

通俗地说,内置谓词是 Prolog 语言的一部分".其他谓词可能是库谓词,需要通过适当的指令拉入 Prolog 程序.SWI Prolog 示例:库谓词.

注意事实

通俗地说,一个扁平化的事实"是由基本项表示的事实 - 一个没有变量的项.

关于函子

的注意事项

这与函子"无关;范畴论.关于范畴论的函子,维基百科是这样说的:

<块引用>

函子这个词是数学家从哲学家那里借来的Rudolf Carnap,他在语言环境中使用了这个词;看功能词.

关于虚词":

<块引用>

在语言学中,虚词(也称为函子)是词几乎没有词汇意义或意义不明确的表达句子中其他词之间的语法关系,或说明说话者的态度或情绪.

所以函子"的选择对于 Prolog 来说有点不幸.

关于目标

的说明

目标可以是人们所说的简单",例如p(X),在这种情况下,它是一个原子目标,或者由子目标组成的树,例如p(X),q(Y) 因为 , 是主函子 (',')/2 的谓词.

事实上,目标通常被认为是任何可以作为规则体出现的东西.例如,p(X) ->q(Y) ;r(Z),主函子 ;(不是 ->)绝对是一个目标.

目标也可以是一个解析为目标的变量,它可能被传递给一个元谓词,比如 call/1 例如:X=(Z is 1+2), call(X)..

变体是元谓词使用的不完整原子目标.这命名了一个带有一些参数在左边"的可调用谓词;预设.元谓词将与在右边"的参数相邻.这称为闭包,尽管与函数式编程不同,它实际上并不是一个引用在函数创建时有效的上下文的函数.

比如三个调用都输出u v w:

foo(X,Y,Z) :- 格式(~q ~q ~q\n", [X,Y,Z]).地图列表(foo,[u],[v],[w]).% 调用foo"有 3 个参数地图列表(foo(u),[v],[w]).% 用 2 个参数调用闭包 foo(u)地图列表(foo(u,v),[w]).% 用 1 个参数调用闭包 foo(u,v)

关于谓词过程谓词指示符

的注意事项

这个谓词的概念似乎漂浮在语义空间"中.而不是语法空间":

  • 谓词事物"的名称或声明;是谓词指示符;
  • 谓语事物"的谓语定义是过程(大概基于代码、Prolog 或其他).

示例:

对于计算阶乘函数的元数 2 的 谓词 fact:

fact/2谓词指示符,并且

fact(0,1) :- !.事实(X,F):- Xp 是(X-1),事实(Xp,Fp),F 是(Fp*X).

是可能对应的过程.

在实践中,谓词也用于表示任何过程,并且编写谓词指示符来识别它.

承认逻辑解释并允许其任何参数的变量的过程将成为数据库交互这个词的逻辑解释.

在Prolog 中的编程(第 5 版)"中(Clocksin & Mellish 2003),它只是在 p.第188话

<块引用>

给定谓词的子句集合称为过程.

关于序言文本

的注意事项

Prolog 程序"(ISO 标准中未定义的术语)将是由(可能是标准的)处理器运行的一组 Prolog 文本 的口语描述.

Prolog 文本 还包括在顶层输入的文本,它不是 Prolog 程序,例如

?- X 是 5 * 3.X = 15.

Getting the terminology correct is part of the success to communicating a concept and when the wrong terminology is used here at SO with the Prolog tag the respondents nicely point out the mistake.

In reading "Clause and Effect - Prolog Programming for the Working Programmer" by William F. Clocksin in 1997 (WorldCat) is the paragraph

A Prolog program consists of a collection of procedures. Each procedure defines a particular predicate, being a certain relationship between its arguments. A procedure consists of one or more assertions, or clauses. It is convenient to think of two kinds of clauses: facts and rules.

While I understand all of the words, is each bold word the currently correct terminology for use when communicating about Prolog?

In particular the use of rule seems to be frowned upon.

解决方案

From the Prolog ISO Standard

ISO/IEC 13211-1 (First edition 1995-06-01)

Information technology - Programming languages - Prolog

Part 1: General Core

which has an extensive glossary on pages 2-10.

3.7 argument: A term which is associated with a predication or compound term.

3.9 arity: The number of arguments of a compound term. Syntactically, a non-negative integer associated with a functor or predicate.

3.10 assert, to: To assert a clause is to add it to the user-defined procedure in the database defined by the predicate of that clause.

3.19 body: A goal, distinguished by its context as part of a rule (see 3.154).

3.21 built-in predicate: A procedure whose execution is implemented by the processor (see 8).

3.32 clause: A fact or a rule. It has two parts: a head, and a body.

3.35 complete database The set of procedures with respect to which execution is performed (see 7.5).

3.37 compound term: A functor of arity N, N positive, together with a sequence of N arguments.

3.45 control construct: A procedure whose definition is part of the Prolog processor (see 7.8).

3.52 database: The set of user-defined procedures which currently exist during execution (see 7.5).

3.57 directive: A term D which affects the meaning of Prolog text (see 7.4.2) and is denoted in that Prolog text by the directive-term :-(D).

3.59 dynamic (of a procedure): A dynamic procedure is one whose clauses can be inspected or altered during execution, for example by asserting or retracting clauses (see 7.5.2).

3.72 fact: A clause whose body is the goal true. NOTE - A fact can be represented in Prolog text by a term whose principal functor is neither (:-)/1 nor (:-)/2.

3.77 functor: An identifier together with an arity.

3.81 goal: A predication which is to be executed (see body, query, and 7.7.3).

3.84 head (of a rule): A predication, distinguished by its context.

3.88 identifier: A basic unstructured object used to denote an atom, functor name or predicate name.

3.129 predicate: An identifier together with an arity.

3.131 predicate indicator: A compound term A/N, where A is an atom and N is a non-negative integer, denoting one particular procedure (see 7.1.6.6).

3.133 predication: A predicate with arity N and a sequence of N arguments.

3.136 procedure: A control construct, a built-in predicate, or a user-defined procedure. A procedure is either static or dynamic. A procedure is either private or public (see 7.5).

3.141 Prolog text: A sequence of read-terms denoting directives and clauses (see 6.2, 7.4).

3.143 query: A goal given as interactive input to the top level.

3.154 rule: A clause whose body is not the goal true. During execution, if the body is true for some substitution, then the head is also true for that substitution. A rule is represented in Prolog text by a term whose principal functor is (:-)/2 where the first argument is converted to the head, and the second argument is converted to the body.

3.164 static (of a procedure): A static procedure is one whose clauses cannot be altered (see 7.5.2).

3.185 top level: A process whereby a Prolog processor repeatedly inputs and executes * queries.

3.195 user-defined procedure: A procedure which is defined by a sequence of clauses where the head of each clause has the same predicate indicator, and each clause is expressed by Prolog text or has been asserted during execution (see 8.9).

Notes

Basic overview

h(A,B,C) :- g(A,B),h(B,C),j(A,C).
<------------------------------->  - A (HORN) CLAUSE, which is also a RULE. 
            <------------------>   - BODY of the RULE, which also a GOAL.
                                     ... only one literal: ATOMIC GOAL.
<------>                           - HEAD of the RULE, which can appear
                                     as GOAL depending on context.

f(A,B,C).                          - A CLAUSE with the elided body `true`.
                                     This is a FACT, but _not_ a RULE.
                                     Also called a UNIT CLAUSE.
f(A,B,C)  :- true.                 - Same as above, still not a RULE.
f(A,B,C)  :- !.                    - Is it a RULE? We don't know!

          :- f(A,B,C).             - A DIRECTIVE.
          :- (foo(bar)).           - Another DIRECTIVE.

Slightly different definitions can be found at the entry for Horn Clause a Wikipedia. In particular, "fact" is said to be "a unit clause without variables" - this is not in accordance wit the ISO definition.

The non-terminal indicator

In addition to the predicate indicator notation A/N (functor/arity), there is the notation A//N, which is not in the ISO standard (or not yet, see this draft). It tells the reader that this predicate is used in a Definite Clause Grammar (DCG) and takes 2 hidden arguments for the input "difference list" (or "list difference") pair in addition to the number of arguments indicated.

In the standard proposal indicated above, it is described as:

3.19 non-terminal indicator: A compound term A//N where A is an atom and N is a non-negative integer, denoting one particular non-terminal

Most implementations translate a non-terminal nt//n to a predicate nt/n+2. However, one cannot rely on the precise way of translation and the outcome of calling a non-terminal directly by calling the corresponding predicate, that is, with the same name and two extra arguments is not defined. In particular the second additional argument has to be handled with care. A direct use might violate steadfastness, in particular when using .

Note on directive

The directive can be another way of writing a query. From the SICStus Prolog manual:

Queries and directives are ways of directing the system to execute some goal or goals. (...) The principal use for directives (as opposed to queries) is to allow files to contain directives that call various predicates, but for which you do not want to have the answers printed out. In such cases you only want to call the predicates for their effect, i.e. you do not want terminal interaction in the middle of consulting the file.

A directive can also be source file markup, whose position is important (i.e. code meta-information). From the SWI Prolog manual on the module directive:

This directive can only be used as the first term of a source file. It declares the file to be a module file, defining a module named Module.

The predicates used in a directive may be quite peculiar, giving instructions and predicate meta-information to the Prolog processor. From the SWI Prolog manual on "declaring predicate properties":

This section describes directives which manipulate attributes of predicate definitions.

For example, "load the library for constraint logic programming over finite domains":

:- use_module(library(clpfd)).

The rule with the empty head :- foo, which could be interpreted as false :- foo is not used in Prolog to express the constraint "it is never the case that foo". It is used that way in implementations of Answer Set Programming like "ASP Prolog" (which has Prolog-y syntax but otherwise is nothing like Prolog).

Note on built-in predicate

In chapter 8, on page 63, we find:

"A built-in predicate is a procedure which is provided by a standard-conforming processor"

Colloquially, "the built-in predicate is part of the Prolog language". Other predicates may be library predicates which need to be pulled into the Prolog program by appropriate directive. Example from SWI Prolog: library predicates.

Note on fact

Colloquially, a "flat fact" is a fact represented by ground term - a term without variables.

Note on functor

This has nothing to do with the "functor" of Category Theory. On the functor of Category Theory, Wikipedia has this to say:

The word functor was borrowed by mathematicians from the philosopher Rudolf Carnap, who used the term in a linguistic context; see function word.

And on "function word":

In linguistics, function words (also called functors) are words that have little lexical meaning or have ambiguous meaning and express grammatical relationships among other words within a sentence, or specify the attitude or mood of the speaker.

So the choice of "functor" for Prolog is a bit unfortunate.

Note on goal

The goal can be what one would describe as "simple", e.g. p(X), in which case it is an atomic goal, or a tree composed of subgoals e.g. p(X),q(Y) because , is a predication with the principal functor (',')/2.

In fact, goal is generally taken to be anything that can appear as a rule body. For example, p(X) -> q(Y) ; r(Z), with principal functor ; (not ->) is absolutely a goal.

The goal could also be a variable resolving to a goal, which might be passed to a meta-predicate like call/1 for example: X=(Z is 1+2), call(X)..

A variation is the incomplete atomic goal used by a meta-predicate. This names a callable predicate with some arguments "on the left" pre-set. The meta-predicate will adjoin arguments "on the right". This is called a closure although, unlike in functional programming, it isn't actually a function referring to context valid at function creation time.

For example, the three calls all output u v w:

foo(X,Y,Z) :- format("~q ~q ~q\n", [X,Y,Z]).

maplist(foo,    [u],[v],[w]). % call "foo" with 3 arguments
maplist(foo(u),     [v],[w]). % call closure foo(u) with 2 arguments
maplist(foo(u,v)       ,[w]). % call closure foo(u,v) with 1 argument

Note on predicate vs. procedure vs. predicate indicator

This concept of predicate seems to float in the "space of semantics" rather than the "space of syntax":

  • the name or declaration of the "predicate thing" is the predicate indicator;
  • the definition of the predicate of the "predicate thing" is the procedure (based presumably on code, Prolog or otherwise).

Example:

For the predicate fact of arity 2 which computes the factorial function:

fact/2 is the predicate indicator, and

fact(0,1) :- !.
fact(X,F) :- Xp is (X-1), fact(Xp,Fp), F is (Fp*X).

is the possible corresponding procedure.

In practice, predicate is used to denote any procedure as well and one writes the predicate indicator to identify it.

A procedure which admits to a logical interpretation and allows variables for any of its arguments would a relation in the database interpetation or the logic interpretation of that word.

In "Programming in Prolog (5th ed.)" (Clocksin & Mellish 2003), it is simply said on p. 188 that

The collection of clauses for a given predicate is called a procedure.

Note on Prolog text

A "Prolog program" (a term which is not defined in the ISO Standard) would be the colloquial description of a set of Prolog texts to be run by the (possibly standard) processor.

Prolog text also includes text entered at the top level which is not a Prolog program, e.g.

?- X is 5 * 3.
X = 15.

这篇关于这个 Prolog 术语是否正确?(事实、规则、程序、谓词……)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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