Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

Syntax and semantics of logic programming

From Wikipedia, the free encyclopedia
This article includes a list ofgeneral references, butit lacks sufficient correspondinginline citations. Please help toimprove this article byintroducing more precise citations.(March 2023) (Learn how and when to remove this message)
Formal semantics of logic programming languages

Logic programming is aprogramming paradigm that includes languages based on formal logic, includingDatalog andProlog. This article describes the syntax and semantics of the purelydeclarative subset of these languages. Confusingly, the name "logic programming" also refers to aspecific programming language that roughly corresponds to the declarative subset of Prolog. Unfortunately, the term must be used in both senses in this article.

Declarative logic programs consist entirely ofrules of the form

H:-B1,...,BN.

Each such rule can be read as animplication:

B1BnH{\displaystyle B_{1}\land \ldots \land B_{n}\rightarrow H}

meaning "If eachBi{\displaystyle B_{i}} is true, thenH{\displaystyle H} is true". Logic programs compute the set of facts that are implied by their rules.

Many implementations of Datalog, Prolog, and related languages add procedural features such as Prolog'scut operator or extra-logical features such as aforeign function interface. The formal semantics of such extensions are beyond the scope of this article.

Datalog

[edit]
Main article:Datalog

Datalog is the simplest widely-studied logic programming language. There are three major definitions of the semantics of Datalog, and they are all equivalent. The syntax and semantics of other logic programming languages are extensions and generalizations of those of Datalog.

Syntax

[edit]

A Datalog program consists of a list ofrules (Horn clauses).[1] Ifconstant andvariable are twocountable sets of constants and variables respectively andrelation is a countable set ofpredicate symbols, then the followingBNF grammar expresses the structure of a Datalog program:

<program>::=<rule><program> | ""<rule>::=<atom> ":-"<atom-list> "."<atom>::=<relation> "("<term-list> ")"<atom-list>::=<atom> |<atom> ","<atom-list> | ""<term>::=<constant> |<variable><term-list>::=<term> |<term> ","<term-list> | ""

Atoms are also referred to asliterals. The atom to the left of the:- symbol is called thehead of the rule; the atoms to the right are thebody. Every Datalog program must satisfy the condition that every variable that appears in the head of a rule also appears in the body (this condition is sometimes called therange restriction).[1][2]

Rules with empty bodies are calledfacts. For example, the following rule is a fact:

r(x):-.

Syntactic sugar

[edit]

Many implementations of logic programming extend the above grammar to allow writing facts without the:-, like so:

r(x).

Many also allow writing 0-ary relations without parentheses, like so:

p:-q.

These are merely abbreviations (syntactic sugar); they have no impact on the semantics of the program.

Example

[edit]

The following program computes the relationpath, which is thetransitive closure of the relationedge.

edge(x,y).edge(y,z).path(A,B):-edge(A,B).path(A,C):-path(A,B),edge(B,C).

Semantics

[edit]

There are three widely-used approaches to the semantics of Datalog programs:model-theoretic,fixed-point, andproof-theoretic. These three approaches can be proven to be equivalent.[3]

An atom is calledground if none of its subterms are variables. Intuitively, each of the semantics define the meaning of a program to be the set of all ground atoms that can be deduced from the rules of the program, starting from the facts.

Model theoretic

[edit]
Hasse diagram of Herbrand interpretations of the Datalog program
e(x,y).e(y,z).p(A,B):-e(A,B).p(A,C):-p(A,B),e(B,C).
The interpretationM{\displaystyle M} is the minimal Herbrand model. All interpretations above it are also models, all interpretations below it are not models.

A rule is called ground if all of its atoms (head and body) are ground. A ground ruleR2 is aground instance of another ruleR1 ifR2 is the result of asubstitution of constants for all the variables inR1.

TheHerbrand base of a Datalog program is the set of all ground atoms that can be made with the constants appearing in the program. Aninterpretation (also known as adatabase instance) is a subset of the Herbrand base. A ground atom is true in an interpretationI if it is an element ofI. A rule istrue in an interpretationI if for each ground instance of that rule, if all the clauses in the body are true inI, then the head of the rule is also true inI.

AHerbrand model of a Datalog programP is an interpretationI ofP which contains all the ground facts ofP, and makes all of the rules ofP true inI.Model-theoretic semantics state that the meaning of a Datalog program is its minimal Herbrand model (equivalently, the intersection of all its Herbrand models).[4]

For example, this program:

edge(x,y).edge(y,z).path(A,B):-edge(A,B).path(A,C):-path(A,B),edge(B,C).

has this Herbrand universe:x,y,z

and this Herbrand base:edge(x, x),edge(x, y), ...,edge(z, z),path(x, x), ...,path(z, z)

and this minimal Herbrand model:edge(x, y),edge(y, z),path(x, y),path(y, z),path(x, z)

Fixed-point

[edit]

LetI be the set of interpretations of a Datalog programP, that is,I =P(H), whereH is the Herbrand base ofP andP is thepowerset operator. Theimmediate consequence operator forP is the following mapT fromI toI: For each ground instance of each rule inP, if every clause in the body is in the input interpretation, then add the head of the ground instance to the output interpretation. This mapT ismonotonic with respect to thepartial order given by subset inclusion onT. By theKnaster–Tarski theorem, this map has a least fixed point; by theKleene fixed-point theorem the fixed point is thesupremum of the chainT(),T(T()),,Tn(),{\displaystyle T(\emptyset ),T(T(\emptyset )),\ldots ,T^{n}(\emptyset ),\ldots }. The least fixed point ofM coincides with the minimal Herbrand model of the program.[5]

Thefixpoint semantics suggest an algorithm for computing the minimal Herbrand model: Start with the set of ground facts in the program, then repeatedly add consequences of the rules until a fixpoint is reached. This algorithm is callednaïve evaluation.

Proof-theoretic

[edit]
Proof tree showing the derivation of the ground atompath(x, z) from the program
edge(x,y).edge(y,z).path(A,B):-edge(A,B).path(A,C):-path(A,B),edge(B,C).

Given a programP, aproof tree of a ground atomA is atree with a root labeled byA, leaves labeled by ground atoms from the heads of facts inP, and branches with childrenA1,,An{\displaystyle A_{1},\ldots ,A_{n}} labeled by ground atomsG such that there exists a ground instance

G :- A1, ..., An.

of a rule inP. The proof-theoretic semantics defines the meaning of a Datalog program to be the set of ground atoms that can be derived from such trees. This set coincides with the minimal Herbrand model.[6]

One might be interested in knowing whether or not a particular ground atom appears in the minimal Herbrand model of a Datalog program, perhaps without caring much about the rest of the model. A top-down reading of the proof trees described above suggests an algorithm for computing the results of suchqueries, such a reading informs theSLD resolution algorithm, which forms the basis for the evaluation ofProlog.

Other approaches

[edit]

The semantics of Datalog have also been studied in the context of fixpoints over more generalsemirings.[7]

Logic programming

[edit]

While the name "logic programming" is used to refer to the entire paradigm of programming languages including Datalog and Prolog, when discussing formal semantics, it generally refers to an extension of Datalog withfunction symbols. Logic programs are also calledHorn clause programs. Logic programming as discussed in this article is closely related to the "pure" ordeclarative subset ofProlog.

Syntax

[edit]

The syntax of logic programming extends the syntax of Datalog with function symbols. Logic programming drops the range restriction, allowing variables to appear in the heads of rules that do not appear in their bodies.[8]

Semantics

[edit]

Due to the presence of function symbols, the Herbrand models of logic programs can be infinite. However, the semantics of a logic program is still defined to be its minimal Herbrand model. Relatedly, the fixpoint of the immediate consequence operator may not converge in a finite number of steps (or to a finite set). However, any ground atom in the minimal Herbrand model will have a finite proof tree. This is why Prolog is evaluated top-down.[8] Just as in Datalog, the three semantics can be proven equivalent.

Negation

[edit]

Logic programming has the desirable property that all three major definitions of the semantics of logic programs agree. In contrast, there are many conflicting proposals for the semantics of logic programs with negation. The source of the disagreement is that logic programs have a unique minimal Herbrand model, but in general, logic programming (or even Datalog) programs with negation do not.

Syntax

[edit]

Negation is writtennot, and can appear in front of any atom in the body of a rule.

<atom-list>::=<atom> | "not"<atom> |<atom> ","<atom-list> | ""

Semantics

[edit]

Stratified negation

[edit]
[icon]
This sectionneeds expansion with: a formal definition of stratification. You can help byadding to it.(March 2023)

A logic program with negation isstratified when it is possible to assign each relation to somestratum, such that if a relationR appears negated in the body of a relationS, thenR is in a lower stratum thanS.[9] The model-theoretic and fixed-point semantics of Datalog can be extended to handle stratified negation, and such extensions can be proved equivalent.

Many implementations of Datalog use a bottom-up evaluation model inspired by the fixed point semantics. Since this semantics can handle stratified negation, several implementations of Datalog implement stratified negation.

While stratified negation is a common extension to Datalog, there are reasonable programs that cannot be stratified. The following program describes a two-player game where a player wins if their opponent has no moves:[10]

move(a,b).win(X):-move(X,Y),notwin(Y).

This program is not stratified, but it seems reasonable to think thata should win the game.

Completion semantics

[edit]
Main article:Negation as failure § Completion semantics
[icon]
This section is empty. You can help byadding to it.(March 2023)

Perfect model semantics

[edit]
[icon]
This section is empty. You can help byadding to it.(March 2023)

Stable model semantics

[edit]
Main article:Stable model semantics

The stable model semantics define a condition for calling certain Herbrand models of a programstable. Intuitively, stable models are the "possible sets of beliefs that a rational agent might hold, given [the program]" as premises.[11]

A program with negation may have many stable models or no stable models. For instance, the program

p:-notq.q:-notp.

has two stable models{p}{\displaystyle \{p\}},{q}{\displaystyle \{q\}}. The one-rule program

p:-notp.

has no stable models.

Every stable model is a minimal Herbrand model. A Datalog program without negation has a single stable model, which is exactly its minimal Herbrand model. The stable model semantics defines the meaning of a logic program with negation to be its stable model, if there is exactly one. However, it can be useful to investigateall (or at least, several) of the stable models of a program; this is the goal ofanswer set programming.

Well-founded semantics

[edit]
Main article:Well-founded semantics
[icon]
This section is empty. You can help byadding to it.(March 2023)

Further extensions

[edit]

Several other extensions of Datalog have been proposed and studied, including variants with support forinteger constants and functions (includingDatalogZ),[12][13]inequality constraints in the bodies of rules, andaggregate functions.

Constraint logic programming allows for constraints over domains such as thereals orintegers to appear in the bodies of rules.

See also

[edit]

References

[edit]

Notes

[edit]
  1. ^abCeri, Gottlob & Tanca 1989, p. 146.
  2. ^Eisner, Jason; Filardo, Nathaniel W. (2011). de Moor, Oege; Gottlob, Georg; Furche, Tim; Sellers, Andrew (eds.).Dyna: Extending Datalog for Modern AI. Datalog Reloaded, First International Workshop, Datalog 2010, Oxford, UK, March 16-19, 2010. Lecture Notes in Computer Science. Vol. 6702. Berlin, Heidelberg: Springer. pp. 181–220.doi:10.1007/978-3-642-24206-9_11.ISBN 978-3-642-24206-9.
  3. ^van Emden, M. H.; Kowalski, R. A. (1976-10-01)."The Semantics of Predicate Logic as a Programming Language".Journal of the ACM.23 (4):733–742.doi:10.1145/321978.321991.ISSN 0004-5411.S2CID 11048276.
  4. ^Ceri, Gottlob & Tanca 1989, p. 149.
  5. ^Ceri, Gottlob & Tanca 1989, p. 150.
  6. ^Abiteboul, Serge (1996).Foundations of databases. Addison-Wesley.ISBN 0-201-53771-0.OCLC 247979782.
  7. ^Khamis, Mahmoud Abo; Ngo, Hung Q.; Pichler, Reinhard; Suciu, Dan; Wang, Yisu Remy (2023-02-01). "Convergence of Datalog over (Pre-) Semirings".arXiv:2105.14435 [cs.DB].
  8. ^abAbiteboul 1996, p. 299.
  9. ^Halevy, Alon Y.; Mumick, Inderpal Singh; Sagiv, Yehoshua; Shmueli, Oded (2001-09-01)."Static analysis in datalog extensions".Journal of the ACM.48 (5):971–1012.doi:10.1145/502102.502104.ISSN 0004-5411.S2CID 18868009.
  10. ^Leone, N; Rullo, P (1992-01-01)."Safe computation of the well-founded semantics of datalog queries".Information Systems.17 (1):17–31.doi:10.1016/0306-4379(92)90003-6.ISSN 0306-4379.
  11. ^Gelfond, Michael; Lifschitz, Vladimir (1988). "The Stable Model Semantics for Logic Programming". In Kowalski, Robert; Bowen, Kenneth (eds.).Proceedings of International Logic Programming Conference and Symposium. MIT Press. pp. 1070–1080.
  12. ^Kaminski, Mark; Grau, Bernardo Cuenca; Kostylev, Egor V.; Motik, Boris; Horrocks, Ian (2017-11-12). "Foundations of Declarative Data Analysis Using Limit Datalog Programs".arXiv:1705.06927 [cs.AI].
  13. ^Grau, Bernardo Cuenca; Horrocks, Ian; Kaminski, Mark; Kostylev, Egor V.; Motik, Boris (2020-02-25)."Limit Datalog: A Declarative Query Language for Data Analysis".ACM SIGMOD Record.48 (4):6–17.doi:10.1145/3385658.3385660.ISSN 0163-5808.S2CID 211520719.

Sources

[edit]
Retrieved from "https://en.wikipedia.org/w/index.php?title=Syntax_and_semantics_of_logic_programming&oldid=1323745138"
Categories:
Hidden categories:

[8]ページ先頭

©2009-2025 Movatter.jp