Logic programming is a programming paradigm that uses rules and facts to describe the problem to be solved. It's based on formal logic, which is a branch of mathematics that deals with statements that are either true or false. In logic programming, you define a set of rules and facts, and the program uses these rules and facts to derive new information.
It is one type of declarative programming paradigm, which means that you describe what you want to happen, rather than how to make it happen.
Here's an example of logic programming in Prolog, a popular logic programming language:
likes(goku, chichi).
likes(chichi, gohan).
likes(gohan, goku).
likes_each_other(X, Y) :- likes(X, Y), likes(Y, X).
In this example, we have three facts, and a rule. The rule says that if X
likes Y
and Y
likes X
, then they like each other. From these facts and this rule, Prolog will be able to say if gohan likes goku, or if goku likes gohan:
?- likes_each_other(goku, gohan).
true.
See Also
Related terms in the Glossary