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

7 Key excerpts on "Declarative Programming"

  • 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
  • Object-Oriented Programming Languages and Event-Driven Programming
    namespace is very similar, except that all the code for a namespace does not have to be included in a single source file, nor even in a contiguous area of program text.
    1.2.6  Declarative Programming Languages
    Opposed to the idea of imperative programming is that of declarative programming. A declarative program defines relationships and provides the programmer the opportunity to search for entities satisfying those relationships, rather than explicitly specifying a sequence of program steps. Like any useful language, a declarative language has a library of functions which are predefined. Those which return Boolean results are called predicates, and the programmer is allowed to define his own predicates. In fact, the process of writing a declarative program is precisely that of defining predicates, either by making simple assertions or by specifying rules which the run-time system (sometimes called an inference engine) can use for its reasoning processes.
    The most prevalent form of Declarative Programming is based on the Horn clause, which is an if…then statement consisting of a conjunction of conditions and a conclusion. In the Prolog language, the conclusion is stated first and the separate clauses in the conjunction are separated by commas. For example, given that the predicate parent(a,b) states that a is a parent of b , a statement that indicates two people are first cousins if one has a parent who is a sibling of the other's parent could be made as follows:
        firstcousins(X,Y) :-         parent(P1,X), parent(P2,Y), siblings(P1,P2), X \= Y.
    Here X , Y , P1 , and P2 are typeless variables which can be bound by a pattern-matching process called unification
  • Designing and Developing Robust Instructional Apps
    • Kenneth J. Luterbach(Author)
    • 2018(Publication Date)
    • Routledge
      (Publisher)
    Computer Programming
    Computer programming languages are tools for problem solving and creative expression. Indeed, people engaged in creative problem solving with computer programming languages developed technologies that resulted in the epochal transformation of the past half century. This information era is marked by computer programs (software) that implement the communication protocols enabling the Internet and the web; software enabling mobile communications; software enabling email and other messaging; software for shopping; software for creating 2D images and 3D models; animation software; office productivity software; voice recognition software, and software for learning, for instance. Learning a computer programming language is extremely beneficial as an outlet for creative expression and problem solving.
    One selects a particular programming language to fit a variety of circumstances. When it is necessary to receive and respond to data at particular microsecond intervals, one writes code in a low-level programming language using the CPU’s instruction set, which addresses memory and input ports directly. Commonly, though, application software is written in a high-level computer programming language, which eliminates the need to consider the computer’s architecture. After distinguishing between low-level and high-level computer programming languages, it is helpful to recognize four main programming paradigms: imperative (procedural/ structural); object-oriented; functional; and logical. Each paradigm has something to offer problem solvers (Kedar, 2011). In this book, we will not be using Haskell (Thompson, 2011) or any functional programming language (so no f(g(x)) for us in this work). Also, we do not consider a logic programming language, such as Prolog. We will consider object-oriented concepts, as necessary. Object-oriented languages, such as C++, C# (C sharp), Objective-C, and Java include imperative language components. Indeed, C++, C#, and Objective-C are implementations of C with objects. C, like JavaScript, Perl, Lua, Swift, Java, and Python, all have imperative (procedural) language components, which enable input/output, and solve problems using the following core elements:
  • Information Technology
    eBook - ePub

    Information Technology

    An Introduction for Today's Digital World

    A declarative program does not contain control constructs like loops and subroutines. Instead, the programming language environment is tasked with a repeated pattern of matching rules against known facts to determine which rules are applicable. The environment then selects one rule to “fire” (execute). The result of the rule is usually new facts to add to what is already known. The environment then has a built-in loop that repeats this process until either all knowledge is exhausted or an ending point has been reached. Every rule is a form of selection statement. So, with this style of programming there is little need to utilize other control statements.
    The original Declarative Programming language was called Prolog and was developed for AI research. Figure 8.10 provides an example of a small set of knowledge and rules that we might encode using a Declarative Programming language, presented in both English and Prolog.
    The first seven items are rules. A rule has the consequence (the “then clause”) on the left side and the condition(s) on the right side. A comma between conditions indicates “AND” and a semicolon indicates “OR.”
    After the seven rules are two pieces of knowledge, or true statements. These declare that Cocoa is a poodle and Kitty is a cat. The last item is a question that we want the Prolog environment to solve for us, “what can be an indoor pet?”
    To answer this question, Prolog looks for any rule(s) with indoorpet on the left side. It finds one: an indoorpet, X, is a pet and is small. Now Prolog must find an X that satisfies both pet and small. To find a pet, X, Prolog finds one rule that states that the pet is an animal and tame. So, to solve indoorpet(X), Prolog must solve pet(X) and small(X). To solve pet(X), it must solve animal(X) and tame(X). Tame is true if something is a dog. Animal is true if something is a dog or cat. A dog can be either a poodle or a collie. Small is true if something is a poodle. We know of a poodle named Cocoa. Therefore, because Cocoa is a poodle, Cocoa is a dog and so is tame and is also small.
  • 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
  • Software Development in Practice
    Programming languages can be categorised in several different ways. A popular approach is to consider their paradigms – their style or way of doing something. Common paradigms include:
    • Procedural: solves problems in an algorithmic way, with the code typically broken down into procedures (e.g. functions).
    • Object-oriented: models the real world, creating encapsulated classes which interact with others to create solutions.
    • Declarative: defines what is needed but not how this is to be achieved.
    • Functional: solves problems using stateless functional components.
    • Event-driven: captures system or user events and writes suitable handlers to deal with them.
    This list is not exhaustive: many different variations and combinations exist. Broadly speaking, programming languages tend to get (rightly or wrongly) pigeonholed into a single paradigm, for example:
    • C is procedural.
    • C++ and Java are object-oriented.
    • Prolog is declarative.
    • Visual Basic .NET is event-driven.
    However, the distinction is not always clear-cut. For instance, Java also supports a user’s interaction with an object, such as clicking a button; this is a classic trait of an event-driven feature. Python, a very popular language at the time of writing, supports procedural, object-oriented and functional programming paradigms.
    Fortunately, many development tools (editors, compilers, debuggers, etc.) are freely downloadable, offering the fledgling programmer many opportunities to learn new skills and experience different approaches to problem-solving prior to gaining employment and working on commercial projects. The key question is: which programming languages should you focus on?
    The simple (if obvious) answer is: the ones that are in commercial demand. After all, you want to start a successful and financially rewarding career as a programmer, don’t you?
  • A Companion to the Philosophy of Language
    • Bob Hale, Crispin Wright, Alexander Miller, Bob Hale, Crispin Wright, Alexander Miller(Authors)
    • 2017(Publication Date)
    • Wiley-Blackwell
      (Publisher)
    One obstacle to this is that many readers will have little fluency with the kind of programming we need. But we can work around that. Those readers who are acquainted with programming may be thinking of it on the model of a sequence of instructions. That is the “imperatival” model of computation. But there are other models, too, such as the “declarative” or “functional” model. The existence of this model of computation may be less familiar, but what it understands computation as is closer to ideas that philosophers and linguists are well‐acquainted with, like formulas of predicate logic. On the declarative model, arithmetic expressions like 2 + 7 ≰ 9, 2 + 7, and 2 all count as programs, which evaluate to true, 9, and 2, respectively. The set of inequalities {2 + x ≰ 9, x > 5} might be a program that evaluates to a set of assignments binding x to 6 or 7. The regular expression /ima[a–z]*ng/ might be a program that evaluates to the set of English words { imaging, imagining }. And so on. There’s nothing in either of these models of computation that prevents it from expressing or achieving all that the other can; their styles of doing so will just tend to be different. We’ll work with the declarative model. If readers have ever encountered a formal semantics for a programming language (from either of the models of computing just described, or another), it’s likely to be of the form computer scientists call “operational semantics.” This will have surface resemblance to a proof theory, or a system of rules for rewriting the program text. (Those analogies have defects, but that’s not important for our purposes here.) But there are other forms of semantics for programming languages
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.