PL Category: Logic Pls Introduction to Prolog
Total Page:16
File Type:pdf, Size:1020Kb
PL Category: Logic PLs Introduction to Prolog CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Friday, April 19, 2019 Glenn G. Chappell Department of Computer Science University of Alaska Fairbanks [email protected] © 2017–2019 Glenn G. Chappell Review PL Feature: Execution II [1/6] Recall. When a program is executed, the computations it specifies actually occur. In all cases, something drives the execution. § There is some task the computer is attempting to perform. § There is some strategy for carrying out the execution. Both of these can vary significantly from one programming language to another. 19 Apr 2019 CS F331 / CSCE A331 Spring 2019 2 Review PL Feature: Execution II [2/6] What task drives the execution of a Lua program? Answer. The code at global scope in a source file is executed. Strategy. The computer is given a sequence of commands. It carries out each, in order, performing the action that each specifies. If a command specifies that another function is to be called, then this becomes a subtask to be completed as part of the main task. C++, Java, and Forth—imperative PLs—are similar. 19 Apr 2019 CS F331 / CSCE A331 Spring 2019 3 Review PL Feature: Execution II [3/6] Scheme code does not consist of commands to be carried out, but rather expressions to be evaluated. Scheme is a more declarative PL than those mentioned earlier. We do not tell the computer what to do, but what the values of things are. What drives Scheme execution (in the REPL) is the evaluation of an expression, which generally results in a procedure call. Strategy. Evaluate the expression that forms the body of this procedure and return its value. § If this expression involves calls to other procedures, then those evaluations are carried out as subtasks. § If the evaluation of any of these expressions produces side effects, then these are performed. 19 Apr 2019 CS F331 / CSCE A331 Spring 2019 4 Review PL Feature: Execution II [4/6] Like Scheme, Haskell code does not consist of commands to be carried out, but rather expressions to be evaluated. Haskell is very much a declarative PL. What drives Haskell execution is a function call—to Main.main or some other function. Strategy. As with Scheme, evaluate the expression that forms the body of this function. If this expression involves calls to other functions, then those evaluations are carried out as subtasks. Haskell is a pure PL—it has no side effects—so Haskell execution per se consists entirely of evaluation. 19 Apr 2019 CS F331 / CSCE A331 Spring 2019 5 Review PL Feature: Execution II [5/6] But there is another element to Haskell execution. I/O values are descriptions of side effects. These are returned to the runtime environment, which performs the side effects. Some I/O values may include code to be executed with the wrapped value. Such code is also executed. This is how things like the following are handled. do line <- getLine putStrLn $ reverse line The call to reverse can only happen after a side effect has been performed. Such code must be called by the runtime. 19 Apr 2019 CS F331 / CSCE A331 Spring 2019 6 Review PL Feature: Execution II [6/6] Prolog is another declarative language. But instead of telling it values, we tell it what is true. Given a query (essentially a question—yes/no or “What … ?”) Prolog attempts to unify the query with something in its knowledge base (program). Prolog execution is driven by a query. The execution strategy involves unification. To unify two constructions means to make them the same by setting values of variables as necessary. Unification, in full generality, includes expression evaluation as a special case. But unification can also do things that evaluation cannot do. Can we unify [A, 6] and [4, B]? Yes. Set A to 4, B to 6. 19 Apr 2019 CS F331 / CSCE A331 Spring 2019 7 PL Category: Logic PLs Background [1/4] In the late 1960s, Stanford researcher Cordell Green proposed representing a computer program in terms of logical statements. The result was a programming paradigm that became known as logic programming. In logic programming, a computer program can be thought of as a knowledge base. It typically contains two kinds of knowledge. § Facts. Statements that are known to be true. § Rules. Ways to find other true statements from those known. Execution is then driven by a query: essentially a question. The computer attempts to answer the question using facts and rules. Logic programming typically has no notion of falsehood. There are statements the computer can prove to be true, and others that it cannot. But some of those others might be true. Logic programming is thus not so much about truth as provability. 19 Apr 2019 CS F331 / CSCE A331 Spring 2019 8 PL Category: Logic PLs Background [2/4] Here are some facts (the lower-case names will be explained). § gil is a child of ernest. § glenn is a child of gil. Here is a rule. (A, B, and C are free variables.) § If A is a child of B, and B is a child of C, then A is a grandchild of C. Here are some queries. 1. Is glenn a grandchild of ernest? 2. Is ernest a grandchild of glenn? 3. Is glenn a grandchild of bob? The answer to query #1 is “yes”. This is provable. It is not provable what the answers to queries #2 and #3 are. These are real people, and the facts and the rule are actually true. The correct answer to query #2 is “no”, and to query #3 is “yes”. So truth and provability are different. 19 Apr 2019 CS F331 / CSCE A331 Spring 2019 9 PL Category: Logic PLs Background [3/4] In the 1970s, programming languages based on logic- programming concepts began to appear. These are logic programming languages. Foremost among those was (and still is) Prolog. Others include Mercury, Oz, Gödel, and clp(FD)*. Designing a practical PL based solely on logic has turned out to be difficult—or perhaps impossible. Logic PLs nearly always “cheat” by including constructions that are not logical in nature. Some people would say that the effort to create a true logic-based programming language has failed. I would say that the effort was worthwhile, but what resulted was not what was originally planned. (I will have more to say about this later.) *“clp(FD)” stands for Constraint Logic Programming over Finite Domains. 19 Apr 2019 CS F331 / CSCE A331 Spring 2019 10 PL Category: Logic PLs Background [4/4] Logic programming may be something that can be offered as a library—if a PL supports the necessary constructions well enough. PLs with sufficient support seem to have been rare until recently. But there are now a number of actively maintained logic- programming libraries for various mainstream PLs. In particular, several logic-programming packages for the Python programming language appear to be well spoken of (but I have no experience with any of them). One also sees logic- programming libraries for various Lisp-family PLs. 19 Apr 2019 CS F331 / CSCE A331 Spring 2019 11 PL Category: Logic PLs Typical Characteristics In a typical logic programming language: § A program consists of facts and rules. § Execution begins with a query, which establishes a goal. During execution, various subgoals may be established. § Execution is primarily interactive, based on a source file. § Execution is about finding what can be proven—not what is true. § So negation says something fails to be provable, not that it is false. § Unification is the primary proof tool. § Typing is dynamic and implicit. 19 Apr 2019 CS F331 / CSCE A331 Spring 2019 12 Introduction to Prolog History [1/2] In the early 1970s, a group at the U. of Aix-Marseille (France) began an effort to produce a logic programming language. The group was led by Alain Colmeraur, and Cordell Green’s ideas were their starting point. The PL was called Prolog, short for “PROgrammation en LOGique” (French for “programming in logic”). The first version of Prolog was released in 1972. Prolog was, and continues to be, the most important logic PL. There was a flurry of interest in Prolog in the 1970s. This has mostly faded, but an active—although relatively small— community remains. 19 Apr 2019 CS F331 / CSCE A331 Spring 2019 13 Introduction to Prolog History [2/2] In 1987, a group at the University of Amsterdam produced a prolog implementation called SWI-Prolog. It is now available free on all major platforms and is actively maintained. In 1995, an ISO standard for Prolog was released. In 2000, corrections and a module extension were published—but not a completely new standard. In 1996, work began on a free ISO Prolog implementation under the auspices of the GNU Project. Initially named “Calypso”, this implementation is now called gprolog; it is what we will use. Proprietary, non-ISO varieties of Prolog also exist. The most successful is probably Visual Prolog (formerly Turbo Prolog). Unlike most versions of Prolog, Visual Prolog has static typing and support for object-oriented programming. 19 Apr 2019 CS F331 / CSCE A331 Spring 2019 14 Introduction to Prolog Characteristics — General Prolog is a logic programming language. Prolog execution is driven by a query, which establishes a goal. The primary execution strategy involves unification. Prolog attempts to unify using backtracking search. Prolog has some support for reflection (which we will not look at closely). From here on, “Prolog” means “ISO Standard Prolog”, as implemented in gprolog. 19 Apr 2019 CS F331 / CSCE A331 Spring 2019 15 Introduction to Prolog Characteristics — Functions & Predicates Prolog does not really use functions, as we generally think of them—except that there are the usual numeric functions used in numeric computation.