Jump to content

User:Robert Kowalski/sandbox: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
No edit summary
AI Logic section
 
(5 intermediate revisions by the same user not shown)
Line 1: Line 1:
{{User sandbox}}
{{User sandbox}}
<!-- EDIT BELOW THIS LINE -->
<!-- EDIT BELOW THIS LINE -->

==Problems with the Logic section==
# "Logical [[inference]] (or [[Deductive reasoning|deduction]]) is the process of [[logical proof|proving]] a new statement ([[Logical consequence|conclusion]]) from other statements that are already known to be true (the [[premise]]s)."
::: Replace "known to be true" by "assumed to be true".
#

=== Logic ===
Formal [[Logic]] is used for [[knowledge representation]] and [[automatic reasoning|reasoning]].<ref name="Logic">
[[Logic]]:
* {{Harvtxt|Russell|Norvig|2021|loc=chpt. 6–9}}
* {{Harvtxt|Luger|Stubblefield|2004|pp=35–77}}
* {{Harvtxt|Nilsson|1998|loc=chpt. 13–16}}
</ref>
Formal logic comes in two main forms: [[propositional logic]] (which operates on statements that are true or false and uses [[logical connective]]s such as "and", "or", "not" and "implies")<ref name="Propositional logic">
[[Propositional logic]]:
* {{Harvtxt|Russell|Norvig|2021|loc=chpt. 6}}
* {{Harvtxt|Luger|Stubblefield|2004|pp=45–50}}
* {{Harvtxt|Nilsson|1998|loc=chpt. 13}}</ref>
and [[predicate logic]] (which also operates on objects, predicates and relations and uses [[Quantifier (logic)|quantifier]]s such as "''Every'' ''X'' is a ''Y''" and "There are ''some'' ''X''s that are ''Y''s").<ref name="Predicate logic">
[[First-order logic]] and features such as [[Equality (mathematics)|equality]]:
* {{Harvtxt|Russell|Norvig|2021|loc=chpt. 7}}
* {{Harvtxt|Poole|Mackworth|Goebel|1998|pp=268–275}},
* {{Harvtxt|Luger|Stubblefield|2004|pp=50–62}},
* {{Harvtxt|Nilsson|1998|loc=chpt. 15}}
</ref>

[[Deductive reasoning]] in logic is the process of [[logical proof|proving]] a new statement ([[Logical consequence|conclusion]]) from other statements that are given and assumed to be true (the [[premise]]s).<ref name="Inference">
[[Logical inference]]:
* {{Harvtxt|Russell|Norvig|2021|loc = chpt. 10}}
</ref> Proofs can be structured as proof [[tree structure|trees]], in which nodes are labelled by sentences, and children nodes are connected to parent nodes by [[inference rule]]s.

Given a problem and a set of premises, problem-solving reduces to searching for a proof tree whose root node is labelled by a solution of the problem and whose leaf nodes are labelled by premises or [[axiom]]s. In the case of [[Horn clause]]s, problem-solving search can be performed by reasoning [[Forward chaining|forwards]] from the premises or [[backward chaining|backwards]] from the problem.<ref name="Logic as search">logical deduction as search:
* {{Harvtxt|Russell|Norvig|2021|loc= §9.3, §9.4}}
* {{Harvtxt|Poole|Mackworth|Goebel|1998|pp=~46–52}}
* {{Harvtxt|Luger|Stubblefield|2004|pp=62–73}}
* {{Harvtxt|Nilsson|1998|loc=chpt. 4.2, 7.2}}
</ref> In the more general case of the clausal form of first-order logic, [[resolution (logic)|resolution]] is a single, axiom-free rule of inference, in which a problem is solved by proving a contradiction from premises that <ref name="Resolution">
[[Resolution (logic)|Resolution]] and [[unification (computer science)|unification]]:
* {{Harvtxt|Russell|Norvig|2021|loc = §7.5.2, §9.2, §9.5}}
</ref>include the negation of the problem to be solved.

Inference in both Horn clause logic and [[first-order logic]] is [[Undecidable problem|undecidable]], and therefore [[Intractable problem|intractable]]. However, backward reasoning with Horn clauses, which underpins computation in the [[logic programming]] language [[Prolog]], is [[Turing completeness|Turing complete]]. Moreover, its efficiency is competitive with computation in other [[symbolic programming]] languages.<ref>{{cite journal |last1=Warren |first1=D.H. |last2=Pereira |first2=L.M. |last3=Pereira |first3=F. |date=1977 |title=Prolog-the language and its implementation compared with Lisp |journal=[[ACM SIGPLAN Notices]] |volume=12 |issue=8 |pages=109–115|doi=10.1145/872734.806939 }}</ref>




[[Fuzzy logic]] assigns a "degree of truth" between 0 and 1. It can therefore handle propositions that are vague and partially true.<ref name="Fuzzy logic">
Fuzzy logic:
* {{Harvtxt|Russell|Norvig|2021|pp=214, 255, 459}}
* {{Harvtxt|Scientific American|1999}}
</ref>
[[Non-monotonic logic]]s, including Horn clause logic augmented with [[negation as failure]], are designed to handle [[default reasoning]].<ref name="Default reasoning and non-monotonic logic"/>
Other specialized versions of logic have been developed to describe many complex domains (see [[#Knowledge representation|knowledge representation]] above).


==Recursion in Logic Programming==

In the procedural interpretation of [[logic programming|logic programs]], clauses (or rules) of the form <syntaxhighlight inline lang="prolog">A :- B</syntaxhighlight> are treated as procedures, which reduce goals of the form <code>A</code> to subgoals of the form <code>B</code>.
For example, the [[Prolog]] clauses:

<syntaxhighlight lang="prolog">
path(X,Y) :- arc(X,Y).
path(X,Y) :- arc(X,Z), path(Z,Y).
</syntaxhighlight>

define a procedure, which can be used to search for a path from X to Y, either by finding a direct arc from X to Y, or by finding an arc from X to Z, and then searching recursively for a path from Z to Y. Prolog executes the procedure by [[backward chaining|reasoning top-down (or backwards)]] and searching the space of possible paths depth-first, one branch at a time. If it tries the second clause, and finitely fails to find a path from Z to Y, it backtracks and tries to find an arc from X to another node, and then searches for a path from that other node to Y.

However, in the logical reading of logic programs, clauses are understood as universally quantified conditionals. For example, the recursive clause of the path-finding procedure is understood [[declarative program|declaratively]] as [[Knowledge representation and reasoning|representing the knowledge]] that, for any X, Y and Z, if there is an arc from X to Z and a path from Z to Y then there is a path from X to Y. In symbolic form:

:<math>\forall X, Y, Z(arc(X,Z)\land path(Z,Y) \rightarrow path(X,Y)).</math>

The logical reading is independent of the way the knowledge is used to solve problems. The knowledge can be used top-down, as in Prolog, to reduce problems to subproblems. Or it can be used [[forward chaining|bottom-up (or forwards)]], as in [[Datalog]], to derive conclusions from conditions. This [[separation of concerns]] is a form of [[Abstraction (computer science)|abstraction]], which [[Algorithm#Algorithm = Logic + Control|separates declarative knowledge from problems solving methods]].<ref>{{Cite journal|last=Kowalski|first=Robert|author-link=Robert Kowalski|title=Algorithm=Logic+Control|journal=[[Communications of the ACM]]|volume=22|issue=7|pages=424–436|year=1979|doi=10.1145/359131.359136|s2cid=2509896|doi-access=free}}</ref><ref>{{Cite book
| first1 = Stuart J. | last1 = Russell | author1-link = Stuart J. Russell
| first2 = Peter. | last2 = Norvig | author2-link = Peter Norvig
| title=[[Artificial Intelligence: A Modern Approach]] §9.3, §9.4
| year = 2021
| edition = 4th
| isbn = 978-0134610993
| lccn = 20190474
| publisher = Pearson | location = Hoboken
}}</ref>

_________________________________________
_________________________________________




[[User:Robert Kowalski#test|definition]]
[[User:Robert Kowalski#test|definition]]


{{See also|Definition#Logic programs}}
{{See also|Definition#Logic programs}}

<syntaxhighlight inline lang="prolog">tick</syntaxhighlight>

Notes for [[Recursion (computer science)]]:
#Recursion solves such recursive problems by using functions that call themselves from within their own code. '''Replace function'''.
# In a properly designed recursive function, with each recursive call, the input problem must be simplified in such a way that eventually the base case must be reached. '''This assumes a top-down or backward reasoning strategy'''.
#Language designers often express grammars in a syntax such as Backus–Naur form; here is such a grammar, for a simple language of arithmetic expressions with multiplication and addition: '''Note that grammars can be parsed top-down or bottom-up'''. See [[Parsing#Types of parsers]].
#This is very similar to an inductive definition of lists of strings; the difference is that this definition specifies how to access the contents of the data structure—namely, via the accessor functions head and tail—and what those contents may be, whereas the inductive definition specifies how to create the structure and what it may be created from. '''In LP there is no difference between accessing and creating'''.
#Some authors classify recursion as either "structural" or "generative". The distinction is related to where a recursive procedure gets the data that it works on, and how it processes that data. '''This distinction may not arise with logic programs'''.
__________________________________________________________________________
__________________________________________________________________________



Latest revision as of 22:37, 26 March 2024

Problems with the Logic section

[edit]
  1. "Logical inference (or deduction) is the process of proving a new statement (conclusion) from other statements that are already known to be true (the premises)."
Replace "known to be true" by "assumed to be true".

Logic

[edit]

Formal Logic is used for knowledge representation and reasoning.[1] Formal logic comes in two main forms: propositional logic (which operates on statements that are true or false and uses logical connectives such as "and", "or", "not" and "implies")[2] and predicate logic (which also operates on objects, predicates and relations and uses quantifiers such as "Every X is a Y" and "There are some Xs that are Ys").[3]

Deductive reasoning in logic is the process of proving a new statement (conclusion) from other statements that are given and assumed to be true (the premises).[4] Proofs can be structured as proof trees, in which nodes are labelled by sentences, and children nodes are connected to parent nodes by inference rules.

Given a problem and a set of premises, problem-solving reduces to searching for a proof tree whose root node is labelled by a solution of the problem and whose leaf nodes are labelled by premises or axioms. In the case of Horn clauses, problem-solving search can be performed by reasoning forwards from the premises or backwards from the problem.[5] In the more general case of the clausal form of first-order logic, resolution is a single, axiom-free rule of inference, in which a problem is solved by proving a contradiction from premises that [6]include the negation of the problem to be solved.

Inference in both Horn clause logic and first-order logic is undecidable, and therefore intractable. However, backward reasoning with Horn clauses, which underpins computation in the logic programming language Prolog, is Turing complete. Moreover, its efficiency is competitive with computation in other symbolic programming languages.[7]



Fuzzy logic assigns a "degree of truth" between 0 and 1. It can therefore handle propositions that are vague and partially true.[8] Non-monotonic logics, including Horn clause logic augmented with negation as failure, are designed to handle default reasoning.[9] Other specialized versions of logic have been developed to describe many complex domains (see knowledge representation above).


Recursion in Logic Programming

[edit]

In the procedural interpretation of logic programs, clauses (or rules) of the form A :- B are treated as procedures, which reduce goals of the form A to subgoals of the form B. For example, the Prolog clauses:

path(X,Y) :- arc(X,Y).
path(X,Y) :- arc(X,Z), path(Z,Y).

define a procedure, which can be used to search for a path from X to Y, either by finding a direct arc from X to Y, or by finding an arc from X to Z, and then searching recursively for a path from Z to Y. Prolog executes the procedure by reasoning top-down (or backwards) and searching the space of possible paths depth-first, one branch at a time. If it tries the second clause, and finitely fails to find a path from Z to Y, it backtracks and tries to find an arc from X to another node, and then searches for a path from that other node to Y.

However, in the logical reading of logic programs, clauses are understood as universally quantified conditionals. For example, the recursive clause of the path-finding procedure is understood declaratively as representing the knowledge that, for any X, Y and Z, if there is an arc from X to Z and a path from Z to Y then there is a path from X to Y. In symbolic form:

The logical reading is independent of the way the knowledge is used to solve problems. The knowledge can be used top-down, as in Prolog, to reduce problems to subproblems. Or it can be used bottom-up (or forwards), as in Datalog, to derive conclusions from conditions. This separation of concerns is a form of abstraction, which separates declarative knowledge from problems solving methods.[10][11]

_________________________________________ _________________________________________


definition

tick

Notes for Recursion (computer science):

  1. Recursion solves such recursive problems by using functions that call themselves from within their own code. Replace function.
  2. In a properly designed recursive function, with each recursive call, the input problem must be simplified in such a way that eventually the base case must be reached. This assumes a top-down or backward reasoning strategy.
  3. Language designers often express grammars in a syntax such as Backus–Naur form; here is such a grammar, for a simple language of arithmetic expressions with multiplication and addition: Note that grammars can be parsed top-down or bottom-up. See Parsing#Types of parsers.
  4. This is very similar to an inductive definition of lists of strings; the difference is that this definition specifies how to access the contents of the data structure—namely, via the accessor functions head and tail—and what those contents may be, whereas the inductive definition specifies how to create the structure and what it may be created from. In LP there is no difference between accessing and creating.
  5. Some authors classify recursion as either "structural" or "generative". The distinction is related to where a recursive procedure gets the data that it works on, and how it processes that data. This distinction may not arise with logic programs.

__________________________________________________________________________

The text below was originally intended for the Computer program entry. I edited the entry with a much shorter text.

The remainder can be used elsewhere. But where?

____________________________________________________________________

In the simplest case of Horn clauses (or "definite" clauses), all of the H, B1, ..., Bn are atomic formulae of the form p (t1 ,…, tn), where p is a predicate symbol naming a relation, and the tn are terms naming objects (or individuals). Terms can be constants, variables or composite terms of the form f (t1 ,…, tn), where f is a functor (or function symbol) and the tn are terms. Variables are written as a string beginning with an upper case letter or an underscore.

For example[12]:

  • All dragons smoke, or equivalently, a thing smokes if the thing is a dragon:
smokes(X) :-
    is_a_dragon(X).
  • A creature smokes if one of its parents smokes:
smokes(X) :-
    is_a_creature(X),
    is_a_parent_of(Y,X),
    smokes(Y).
  • A thing X is a parent of a thing Y if X is the mother of Y or X is the father of Y:
 
is_a_parent_of(X, Y):- is_the_mother_of(X, Y).
is_a_parent_of(X, Y):- is_the_father_of(X, Y).
  • A thing is a creature if the thing is a dragon:
is_a_creature(X) :-
    is_a_dragon(X).
  • Alice is a dragon, and Bob is a creature. Alice is the mother of Bob.
is_a_dragon(alice). 
is_a_creature(bob).
is_the_mother_of(alice, bob).

The example illustrates several features of Prolog and of logic programs more generally: All of the variables in a clause are universally quantified, meaning that the clause holds for all instances in which the variables are replaced by terms not containing variables. Also, all variables are local to the clause in which they occur. So, there is no relationship, for example, between the variables X and Y in the second clause and the variables X and Y in the third clause. (In fact, their roles in the parent relation in the two clauses are actually reversed.)

Notice that the second clause is a recursive (or inductive) definition. It can be understood purely declaratively, without any need to understand how it is executed. The third clause shows how functions, as in functional programming, are represented by relations. Here the mother and father relations are functions in the sense that every individual has only one mother and only one father.

Prolog is an untyped language. However, types (or classes) can be represented by predicates. The fourth clause defines dragon as a subtype (or subclass) of creature.

Computation in Prolog can be regarded as question-answering or theorem-proving. It is initiated by a query or theorem to be proved, and solutions are generated by backward reasoning. For example:

  • Answer the query: which thing smokes? Or, equivalently, prove that some thing smokes!
 ?- smokes(X).

All variables in queries are existentially quantified, and execution of the program generates instances of the query that make the query true.

In the case where all clauses are Horn clauses, backward reasoning is performed by using SLD resolution, which is a variant of SL resolution for Horn clauses (or definite clauses). In this example, backward reasoning uses the first clause to reduce the goal to the subgoal:

 ?- is_a_dragon(X).

and it uses the second clause to reduce the goal to the subgoals:

 ?-  is_a_creature(X),
    is_a_parent_of(Y,X),
    smokes(Y).

It solves the subgoal is_a_dragon(X) generated by the first clause, by unifying it with the fact is_a_dragon(alice), instantiating the variable X to the constant alice, giving the solution, X = alice. It solves the three subgoals generated by the second clause, by unifying them with the facts is_a_creature(bob), is_a_parent_of(alice, bob) and smokes(alice), instantiating the variable X to the constant bob, giving a second solution, X = bob.

Goal-reduction, performed in this way, generates a generalised form of and-or tree of goals and subgoals. Prolog explores this tree depth-first, trying different clauses unifying with the same subgoal in the order in which the clauses are written, and trying to solve different subgoals coming from the same clause in the order in which the subgoals are written in the clause. In this example, the computation will generate the solution, X = alice, followed by X = bob.

Prolog programs can also include negative conditions in the body of clauses, as in:

  • A creature is healthy if the creature does not smoke:
is_healthy(X) :-
    is_a_creature(X),
    not smokes(X).

Prolog uses negation as failure (NAF) [13] to solve negative subgoals of the form not pby showing that the corresponding unnegated subgoal p fails to hold. For example, the query:

 ?- not healthy(bob).

returns the answer true, because the query

 ?- healthy(bob).

fails, because the subgoal:

 ?- not smokes(bob).

fails, because:

 ?- smokes(bob).

succeeds.

Prolog is a homoiconic language, in which atomic formulae and clauses are represented by composite terms. This is a powerful feature which has many applications, including its use for implementing metainterpreters and for representing and reasoning about propositional attitudes. For example:

  • It is prohibited for a creature that the creature teaches deportment if the creature smokes. A creature is ostracised if it is prohibited for the creature that a thing and the thing holds:
prohibited(X, teaches(X, deportment)) :- creature(X), smokes(X).
ostracised(X) :- creature(X), prohibited(X, P), holds(P).

The simplest case in which a proposition P holds is defined by:

holds(A) :- clause(A, true).

where

clause(Head, Body)

is a built-in metapredicate that holds when Head can be unified with the head of a clause and Body can be unified with the corresponding clause body, where Body = true if the clause is a fact.

Given the fact (assumption or premise):

teaches(bob, deportment).

and the query:

ostracised(X).

Prolog derives the answer X = bob

These examples illustrate some of the unique features of Prolog, which distinguish it from more conventional programming languages, and which highlight its use as a logic-based language for knowledge representation and problem solving in Artificial Intelligence.


Prolog has many applications. But many large-scale applications require the use of Prolog's "impure features", which do not have an obvious logical interpretation, or they are implemented in combination with other programming languages. The broad range of Prolog applications, both in isolation and in combination with other languages is highlighted in the Year of Prolog Book [14], celebrating the 50 year anniversary of Prolog.

  1. ^ Logic:
  2. ^ Propositional logic:
  3. ^ First-order logic and features such as equality:
  4. ^ Logical inference:
  5. ^ logical deduction as search:
  6. ^ Resolution and unification:
  7. ^ Warren, D.H.; Pereira, L.M.; Pereira, F. (1977). "Prolog-the language and its implementation compared with Lisp". ACM SIGPLAN Notices. 12 (8): 109–115. doi:10.1145/872734.806939.
  8. ^ Fuzzy logic:
  9. ^ Cite error: The named reference Default reasoning and non-monotonic logic was invoked but never defined (see the help page).
  10. ^ Kowalski, Robert (1979). "Algorithm=Logic+Control". Communications of the ACM. 22 (7): 424–436. doi:10.1145/359131.359136. S2CID 2509896.
  11. ^ Russell, Stuart J.; Norvig, Peter. (2021). Artificial Intelligence: A Modern Approach §9.3, §9.4 (4th ed.). Hoboken: Pearson. ISBN 978-0134610993. LCCN 20190474.
  12. ^ Kowalski, R., Dávila, J., Sartor, G. and Calejo, M., 2023. Logical English for law and education. In Prolog: The Next 50 Years (pp. 287-299). Cham: Springer Nature Switzerland.
  13. ^ Krzysztof Apt and Maarten van Emden, Contributions to the Theory of Logic Programming, Journal of the Association for Computing Machinery. Vol, 1982 - portal.acm.org
  14. ^ Cite error: The named reference Prolog Book was invoked but never defined (see the help page).