Declarative programming: Difference between revisions

Content deleted Content added
Definition: Added period to bullet for consistency
Removed double spaces throughout article for consistency
Line 49:
 
===Logic programming===
[[Logic programming]] languages, such as [[Prolog]], [[Datalog]] and [[answer set programming]], compute by proving that a goal is a logical consequence of the program, or by showing that the goal is true in a model defined by the program. Prolog computes by reducing goals to subgoals, top-down using [[backward chaining | backward reasoning]], whereas most Datalog systems compute bottom-up using [[forward chaining | forward reasoning]]. Answer set programs typically use [[Boolean SAT solver |SAT solvers]] to generate a model of the program.
 
===Modeling===
{{Main|Mathematical model}}
Models, or mathematical representations, of physical systems may be implemented in computer code that is declarative. The code contains a number of equations, not imperative assignments, that describe ("declare") the behavioral relationships. When a model is expressed in this formalism, a computer is able to perform algebraic manipulations to best formulate the solution algorithm. The mathematical causality is typically imposed at the boundaries of the physical system, while the behavioral description of the system itself is declarative or acausal. Declarative [[modeling language]]s and environments include [[Analytica (software)|Analytica]], [[Modelica]] and [[Simile (computing)|Simile]].<ref>{{cite web|url=http://www.simulistics.com/tour/declarative.htm |title=Declarative modelling |publisher=Simulistics |access-date=15 August 2013}}</ref>
 
==Examples==
Line 59:
===Lisp===
{{copyedit|section|reason=checking relevance and factual accuracy|date=December 2023}}
[[Lisp (programming language) | Lisp]] is a family of programming languages loosely inspired by mathematical notation and [[Alonzo Church]]'s [[lambda calculus]]. While some dialects, such as [[Common Lisp]] are primarily imperative, these lisps support functional programming, and other lisps, such as [[Scheme (programming language) | Scheme]] are designed for functional programming.
 
In scheme, one may define the [[factorial]] function as such:
Line 72:
This can be construed as defining the factorial function using its recursive mathematical definition, as opposed to simply defining a procedure, as one would typically do in an imperative language.
 
In lisps, as in lambda calculus, functions are generally [[first-class citizen | first-class citizens]]. Loosely, this means that functions can return functions, and be used as parameters for other functions.
 
This can greatly simplify the definition of certain functions.
 
For example, if one wants to create a function that returns the first n [[square numbers | squares]] in [[Racket (programming language) | Racket]], one can simply write the following:
 
<syntaxhighlight lang=scheme>
Line 148:
Given this program, the query <syntaxhighlight inline lang=prolog>eat(tom,jerry)</syntaxhighlight> succeeds, while <syntaxhighlight inline lang=prolog>eat(jerry,tom)</syntaxhighlight> fails. Moreover, the query <syntaxhighlight inline lang=prolog>eat(X,jerry)</syntaxhighlight> succeeds with the answer substitution <syntaxhighlight inline lang=prolog>X=tom</syntaxhighlight>.
 
Prolog executes programs top-down, using [[SLD resolution]] to [[backward chaining | reason backwards]], reducing goals to subgoals. In this example, it uses the last rule of the program to reduce the goal of answering the query <syntaxhighlight inline lang=prolog>eat(X,jerry)</syntaxhighlight> to the subgoals of first finding an X such that <syntaxhighlight inline lang=prolog>big(X)</syntaxhighlight> holds and then of showing that <syntaxhighlight inline lang=prolog>small(jerry)</syntaxhighlight> holds. It repeatedly uses rules to further reduce subgoals to other subgoals, until it eventually succeeds in [[Unification (computer science)#Application: unification in logic programming | unifying]] all subgoals with facts in the program. This backward reasoning, goal-reduction strategy treats rules in logic programs as procedures, and makes Prolog both a declarative and [[procedural programming#Logic programming | procedural programming]] language.<ref>Robert Kowalski [http://www.doc.ic.ac.uk/~rak/papers/IFIP%2074.pdf Predicate Logic as a Programming Language] Memo 70, Department of Artificial Intelligence, University of Edinburgh. 1973. Also in Proceedings IFIP Congress, Stockholm, North Holland Publishing Co., 1974, pp. 569-574.</ref>
 
The broad range of Prolog applications is highlighted in the Year of Prolog Book,<ref name="Prolog Book">{{cite book |last1=Warren |first1=D.S. |editor-last1=Warren |editor-first1=D.S. |editor-last2=Dahl |editor-first2=V. |editor-last3=Eiter |editor-first3=T. |editor-last4=Hermenegildo |editor-first4=M.V. |editor-last5=Kowalski |editor-first5=R. |editor-last6=Rossi |editor-first6=F. |chapter=Introduction to Prolog |title=Prolog: The Next 50 Years |series=Lecture Notes in Computer Science() |date=2023 |volume=13900 |publisher=Springer, Cham. |doi=10.1007/978-3-031-35254-6_1 |pages=3–19|isbn=978-3-031-35253-9 }}</ref> celebrating the 50 year anniversary of Prolog.
 
===Datalog===
The [[Datalog#History | origins of Datalog]] date back to the beginning of logic programming, but it was identified as a separate area around 1977. [[Syntax and semantics of logic programming |Syntactically and semantically]], it is a subset of Prolog. But because it does not have [[Prolog#Data types | compound terms]], it is not [[Turing completeness|Turing-complete]].
 
Most Datalog systems execute programs bottom-up, using rules to [[forward chaining | reason forwards]], deriving new facts from existing facts, and terminating when there are no new facts that can be derived, or when the derived facts unify with the query. In the above example, a typical Datalog system would first derive the new facts:
<syntaxhighlight lang="prolog">
animal(tom).