Computer Science

Declarative Programming

Declarative programming is a programming paradigm that focuses on describing what the program should accomplish, rather than how it should accomplish it. It involves specifying the desired outcome or result, and the program automatically determines the steps needed to achieve that outcome. This approach is often used in functional programming languages.

Written by Perlego with AI-assistance

9 Key excerpts on "Declarative Programming"

  • Book cover image for: NEW PERSPECTIVES COMPUTER CONCEPTS 2016 ENHANCED
    Copyright 2017 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s). Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it. PROGRAMMING 8 0 3 11 THE DECLARATIVE PARADIGM Although procedural and object-oriented programming languages can deal with decisions and textual data, they begin to stumble when applied to problems with complex sequences of interrelated if...then branches. When dealing with problems that involve a spiderweb of logic, programmers can turn to Declarative Programming languages. This section focuses on the declarative paradigm by offering a little taste of the Prolog language.  What is the declarative paradigm? In earlier sections of this unit, you learned that procedural programming focuses on a step-by-step algo-rithm, which instructs the computer how to arrive at a solution. You also learned that the object-oriented approach emphasizes classes and methods that form objects. In contrast, the declarative paradigm describes aspects of a problem that lead to a solution. As in the expression Well, I do declare!” programmers using declarative languages write code that declares, or states, facts pertaining to a program. The declarative paradigm might sound similar to the procedural paradigm, but it is fundamentally different. Whereas the procedural paradigm focuses on an algorithm that describes a solution, the declarative paradigm focuses on describing the problem. The procedural paradigm focuses on how ; the declarative paradigm focuses on what .
  • Book cover image for: Higher-Order Perl
    eBook - ePub

    Higher-Order Perl

    Transforming Programs with Programs

    CHAPTER 9 Declarative Programming
    Beginning programmers often wish for a way to simply tell the computer what they want, and have the computer figure out how to do it. Declarative Programming is an attempt to do that. The idea is that the programmer will put in the specifications for the value to be computed, and the computer will use the appropriate algorithm.
    Nobody knows how to do this in general, and it may turn out to be impossible. But there are some interesting results we can get in specific problem domains. Regular expressions are a highly successful example of Declarative Programming. You write a pattern that represents the form of the text you are looking for, and then sit back and let the regex engine figure out the best way of locating the matching text.
    Searching in general lends itself to declarative methods: the programmer specifies what they are searching for, and then lets a generic heuristic searching algorithm look for it. Database query languages are highly visible examples of this; consider SQL, or the query language of Chapter 8 . The programming language Prolog is an extension of this idea to allow general computations.
    We’ve seen searching in some detail already, so in this chapter we’ll look at some other techniques and applications of Declarative Programming.

    9.1 CONSTRAINT SYSTEMS

    Suppose you wrote a program to translate Fahrenheit temperatures into Celsius:
    Now you’d like to have a program to perform the opposite conversion, from Celsius to Fahrenheit. Although this calculation is in some sense the same, you’d have to write completely new code, from scratch:
    The idea of constraint systems is to permit the computer to be able to run this sort of calculation in either direction.

    9.2 LOCAL PROPAGATION NETWORKS

    One approach that seems promising is to distribute the logic for the calculation among several objects in a constraint network as shown in Figure 9.1 .
    There is a node in the network for each constant, variable, and operator. Lines between the nodes communicate numeric values between nodes, and they are called wires . A node can set the value on one of its wires; this sends a notification to the node at the other end of the wire that the value has changed. Because values are propagated only from nodes to their adjacent wires to the nodes attached at the other end of the wire, the network is called a local propagation network
  • Book cover image for: Programming with C++
    • Kyla McMullen, Elizabeth Matthews, June Jamrich Parsons, , Kyla McMullen, Kyla McMullen, Elizabeth Matthews, June Jamrich Parsons(Authors)
    • 2021(Publication Date)
    PROGRAMMING WITH C++ 504 Characteristics of the Declarative Paradigm (27.4.2) The main takeaway about the declarative paradigm is that you don’t have to specify the steps for producing output. Instead you need only specify what rather than how: What are the facts, what are the rules, and what are the queries. Let’s recap two other characteristics that surfaced in the logic programming example: • Declarative programs are not based on a step-by step algorithm, so the order of statements is much less important than in imperative programs. • The programming language handles the flow of control, so repetition controls are not needed. Applications for Declarative Paradigms (27.4.3, 27.4.4, 27.4.5, 27.4.6) The declarative approach shines for applications that involve complex relationships, interrelated rules, statistical analysis, and database queries. You might encounter several variations of the declarative paradigm, each with its unique focus and corresponding programming languages, as shown in Figure 27-16. Figure 27-16 Variations of the declarative paradigm Logic programming paradigm. Genealogy involves complex relationships between mothers, fathers, children, siblings, and spouses. The data is text- based, not numerical. Processing involves relationships, rather than calculations. The logic programming paradigm is an effective approach for dealing with complex relationships, decisions, and rule sets that are expressed as text. Programming languages such as Prolog support this declarative approach. Functional programming paradigm. Social networking services have policies against spam, malware, and other inappropriate activities. Identifying abuses involves statistical analysis and processing policy rules. The functional programming paradigm is an effective approach for applications that have interacting text-based or numerical rules.
  • Book cover image for: New Perspectives on Computer Concepts 2018
    However, problems that require intensive computation are not usually best suited for the declar-ative paradigm. As a general rule, Declarative Programming languages are most suitable for problems that pertain to words and concepts rather than to numbers. ª What are the advantages of declarative languages? Declarative languages offer a highly effective programming environment for problems that involve words, concepts, and complex logic. As you learned in this module, declarative languages offer a great deal of flexibility for que-rying a set of facts and rules. These languages also allow programmers to describe problems using words rather than the abstract structures required by procedural and object-oriented languages. Although not in widespread use, Declarative Programming is popular in several niche markets. The U.S. Forest Service uses Prolog programs for resource management. Prolog programs are also used extensively in sci-entific research. The U.S. Department of Homeland Security experimented with a Prolog application developed to comb through social networking sites for terrorist activities. Finally, a growing trend is to incorporate Prolog modules with procedural and object-oriented programs to handle concept and language processing. ª What are the disadvantages of declarative languages? Currently, declarative languages are not commonly used for production applications. To some extent, today’s emphasis on the object-oriented paradigm has pushed declarative languages out of the mainstream, both in education and in the job market. Many aspiring programmers are never introduced to declarative languages, so they are not included among the languages evaluated for a specific project. Declarative languages have a reputation for providing minimal input and output capabilities. Although many of today’s Prolog compilers provide access to Windows and Mac user interface components, programmers are often unaware of this capability.
  • Book cover image for: Computer Hardware Description Languages and their Applications
    eBook - PDF

    Computer Hardware Description Languages and their Applications

    Proceedings of the IFIP WG 10.2 Tenth International Symposium on Computer Hardware Description Languages and their Applications, Marseille, France, 22-24 April 1991

    • D. Borrione, R. Waxman(Authors)
    • 2014(Publication Date)
    • North Holland
      (Publisher)
    Section 4 concludes w i t h some ramifications for the future of declarative system description languages. 1. DECLARATIVE L A N G U A G E S A N D T H E I R S C O P E 1.0 Declarative languages It is necessary to remove some of the confusion around the concept of declarativity that has been caused by the tendency — which unfortunately is rather widespread in the area of computing — to oversell. The definition in the latest Oxford Dictionary of Computing [2] states: 188 With a declarative language, a program explicitly states what properties the desired result is required to exhibit but does not state how the desired result is to be obtained; any means of producing a result that displays the required properties is acceptable. Interestingly enough, the ODC further remarks that the existing languages [in this context, the ODC also m e a n s functional and logic programming languages, R.B.] fall somewhat short of this, either in semantics or in style of use (or both). The definition may be considered quite accurate, except perhaps for the usage of the term program. It is debatable whether this term, which by definition d e s i g n a t e s an effective procedure, is appropriate for a truly declarative description. In fact, the nomenclature related to the functional programming language Miranda [3] u s e s script instead of program for this very reason, even though, as we shall see, Miranda scripts are still programs rather t h a n declarative descriptions in the sense of the ODC definition. For abstract (declarative) or concrete (realization) descriptions of systems and circuits, the term program is clearly unsuitable, but obviously the ODC did not envisage HDLs in its definition. Hence, for the purpose of this paper, we generalize the ODC definition to cover also H D L s by substituting formal description for program.
  • Book cover image for: Automated Physical Database Design and Tuning
    • Nicolas Bruno(Author)
    • 2011(Publication Date)
    • CRC Press
      (Publisher)
    Chapter 1 Declarative Query Processing in Relational Database Systems A common approach to classify programming languages is around paradigms , broadly defined as fundamental styles of computer programming. A tradi-tional classification distinguishes between imperative languages (in which we specify how to obtain results) and declarative languages (in which we specify what we want without detailing how to reach the answer). ∗ In this chapter we contrast these two approaches and motivate the declarative capabilities of Structured Query Language ( SQL ), the most prevalent query language in relational database systems. We then briefly review how SQL queries are eval-uated in modern systems and how, in turn, query processing is influenced by the availability of database indexes. As a consequence, the set of available indexes, or the physical design of a database, is a very important aspect for performance tuning. We next motivate the physical database design problem and note that, unless done automatically, it conflicts with the goal of a declar-ative language. In fact, understanding which are the right indexes for a given query (or set of queries) forces us to think about how queries would be evalu-ated. Any effective step toward a solution to the physical design problem not only would make application development more declarative but also would decrease the total cost of ownership of database installations. 1.1 An Exercise in Imperative Programming Suppose that we have two arrays of integers, R and S , and we need to compute the number of times that some element in R matches some element in S , including duplicates (let us denote this functionality by countMatches ). The ∗ This distinction has been recently blurred by advances in programming languages. We now have declarative dialects in imperative languages (e.g., LInQ dialects in the Microsoft .NET framework) and procedural extensions to declarative languages. 3
  • Book cover image for: Joe Celko's Thinking in Sets: Auxiliary, Temporal, and Virtual Tables in SQL
    SQL Is Declarative, Not Procedural
    I N THE PREFACE I told a short story about FORTRAN programmers who could only solve problems using loops and a LISP programmer who could only solve problems recursively This is not uncommon because we love the tools we know. Let me tell a joke instead of a story: A mathematician, a physicist, and a database programmer were all given a rubber ball and told to find the volume.
    The mathematician carefully measured the diameter and either evaluated the volume of sphere formula or used a triple integral if the ball was not perfectly round. The physicist filled a beaker with water, put the ball in the water, and measured the total displacement. He does not care about the details of the shape of the ball.
    The database programmer looked up the model and serial numbers in his rubber ball manufacturer’s on-line database. He does not care about the actual ball. But he has information about the tolerances to which it was made, the expected shape and size, and a bunch of other things that apply to the entire rubber ball production process.
    The moral of the story is: The mathematician knows how to compute. The physicist knows how to measure. The database guy knows how to look up data. Each person grabs his tools to solve the problem.
    Now change the problem to an inventory of thousands of rubber balls. The mathematician and the physicist are stuck with a lot of manual labor. The database guy does a few downloads and he can produce rubber ball industry standards (assuming that there are such things) and detailed documentation in court with his answers.

    1.1 Different Programming Models

    Perfecting oneself is as much unlearning as it is learning.—Edsgar Dijkstra
    There are many models of programming. Procedural programming languages use a sequence of procedural steps guided by flow of control statements (WHILE-DO, IF-THEN-ELSE, and BEGIN-END ) that change the input data to output data. This was the traditional view of programming, and it is often called the von Neumann Model after John von Neumann, the mathematician who was responsible for it. The same source code runs through the same compiler and generates the same executable module every time. The same program will work exactly the same way every time it is invoked. The keywords in this model are predictable and deterministic. It is also subject to some mathematical analysis because
  • Book cover image for: Handbook of Computer Programming and Hardware Description Languages
    A programming language provides a structured mechanism for defining pieces of data, and the operations or transformations that may be carried out automatically on that data. A programmer uses the abstractions present in the language to represent the concepts involved in a computation. These concepts are represented as a collection of the simplest elements available (called primitives). Programming is the process by which programmers combine these primitives to compose new programs, or adapt existing ones to new uses or a changing environment. Programs for a computer might be executed in a batch process without human interaction, or a user might type commands in an interactive session of an interpreter. In this case the commands are simply programs, whose execution is chained together. When a language is used to give commands to a software application (such as a shell) it is called a scripting language. Measuring language usage It is difficult to determine which programming languages are most widely used, and what usage means varies by context. One language may occupy the greater number of programmer hours, a different one have more lines of code, and a third utilize the most CPU time. Some languages are very popular for particular kinds of applications. For example, COBOL is still strong in the corporate data center, often on large mainframes; FORTRAN in engineering applications; C in embedded applications and operating systems; and other languages are regularly used to write many different kinds of applications.
  • Book cover image for: Computer Programming Languages
    A programming language provides a structured mechanism for defining pieces of data, and the operations or transformations that may be carried out automatically on that data. A programmer uses the abstractions present in the language to represent the concepts involved in a computation. These concepts are represented as a collection of the simplest elements available (called primitives). Programming is the process by which pro-grammers combine these primitives to compose new programs, or adapt existing ones to new uses or a changing environment. Programs for a computer might be executed in a batch process without human interaction, or a user might type commands in an interactive session of an interpreter. In this case the commands are simply programs, whose execution is chained together. When a language is used to give commands to a software application (such as a shell) it is called a scripting language. Measuring language usage It is difficult to determine which programming languages are most widely used, and what usage means varies by context. One language may occupy the greater number of pro-grammer hours, a different one have more lines of code, and a third utilize the most CPU time. Some languages are very popular for particular kinds of applications. For example, COBOL is still strong in the corporate data center, often on large mainframes; FORTRAN in engineering applications; C in embedded applications and operating systems; and other languages are regularly used to write many different kinds of applications.
Index pages curate the most relevant extracts from our library of academic textbooks. They’ve been created using an in-house natural language model (NLM), each adding context and meaning to key research topics.