Nepedia
Back to Discover
Advanced Reader
💻

Declarative programming

technology Maturity 7-9 Next article →

Declarative programming is a major paradigm in computer science. It focuses on expressing the logic of a computation. It does not describe the control flow of the program. In this style, a programmer describes what a computation should accomplish. They do not list the specific steps to get there. This is the opposite of imperative programming. Imperative programming uses explicit steps to implement algorithms. Declarative programming often views programs as theories of formal logic. In this view, computations are seen as deductions within a logical space. This approach can greatly simplify the process of writing parallel programs.

To understand the mechanism, compare it to how we solve problems. In an imperative system, you provide a sequence of commands. In a declarative system, you describe the desired result. Many definitions of this style overlap. Some define it as any programming style that is not imperative. Others focus on the lack of side effects. A side effect is a change that happens outside the immediate task. A language with referential transparency is often considered declarative. This means a program has a clear correspondence to mathematical logic. The computer handles the "how" through its own implementation.

Declarative programming is an umbrella term for several sub-paradigms. One major type is logic programming. In logic programming, programs consist of sentences in logical form. Computation involves using these sentences to solve problems. Another type is functional programming. Languages like Haskell use functional programming to evaluate expressions. They use function application to transform data. Functional programming places very little emphasis on explicit sequencing. Instead, it uses recursive higher-order function application. This can be seen as a set of mappings between different domains.

Constraint programming is another important sub-paradigm. It states relations between variables using constraints. These constraints specify the properties of a target solution. The system solves the set by assigning values to each variable. The goal is to stay consistent with the maximum number of constraints. This method often complements functional or logic programming. There are also domain-specific languages, or DSLs. These are languages built for specific tasks. Examples include HTML for web pages or SQL for databases. HTML describes what should appear on a page. It does not describe the control flow for rendering it.

History shows the development of these powerful tools. Prolog, which stands for "PROgramming in LOGic," was developed in 1972. It was created to help computers answer questions using natural language. It uses SL resolution to deduce answers and parse sentences. Datalog is a subset of Prolog that became a separate area around 1977. It lacks compound terms and is not Turing-complete. Most Datalog systems use forward reasoning to derive new facts. Answer set programming, or ASP, evolved in the late 1990s. It is based on the stable model semantics of logic programming. ASP helps solve difficult search problems and knowledge representation.

Specific examples show how these languages function in the real world. The language ML, standing for Meta Language, was introduced in 1973. It is statically typed, meaning types are checked strictly. It is tailored to process lists, much like the Lisp language. In Prolog, the building blocks are facts and rules. For example, a rule might state that every cat is an animal. If you query the system about a specific cat, it uses backward reasoning. It reduces goals to subgoals until it finds a match in the facts. This makes Prolog both a declarative and a procedural language.

Declarative methods also extend to mathematical modeling. Engineers use declarative code to create models of physical systems. These models contain equations rather than imperative assignments. The code declares the behavioral relationships of the system. This allows a computer to perform algebraic manipulations to find solutions. This is used in environments like Modelica or Analytica. By using these systems, programmers can focus on the physics of a problem. They leave the complex math and execution steps to the computer.

653 words

Where will your curiosity go next?

Keep exploring at your chosen reading level.

More to explore

What is Nepedia?

A free, ad-free encyclopedia for children. Every article is written at five reading levels, so the same page works for a five-year-old and a fifteen-year-old — use the level switcher above to see this one change. No account needed to read.