An Introduction to Declarative Programming in CLIPS and PROLOG Jack L
Total Page:16
File Type:pdf, Size:1020Kb
University of Dayton eCommons Computer Science Faculty Publications Department of Computer Science 7-2019 An Introduction to Declarative Programming in CLIPS and PROLOG Jack L. Watkin University of Nebraska - Lincoln Adam C. Volk University of Nebraska - Lincoln Saverio Perugini University of Dayton, [email protected] Follow this and additional works at: https://ecommons.udayton.edu/cps_fac_pub Part of the Graphics and Human Computer Interfaces Commons, and the Other Computer Sciences Commons eCommons Citation Watkin, Jack L.; Volk, Adam C.; and Perugini, Saverio, "An Introduction to Declarative Programming in CLIPS and PROLOG" (2019). Computer Science Faculty Publications. 179. https://ecommons.udayton.edu/cps_fac_pub/179 This Conference Paper is brought to you for free and open access by the Department of Computer Science at eCommons. It has been accepted for inclusion in Computer Science Faculty Publications by an authorized administrator of eCommons. For more information, please contact [email protected], [email protected]. Int’l Conf. Scientific Computing j CSC’19 j 105 An Introduction to Declarative Programming in CLIPS and PROLOG Jack L. Watkin, Adam C. Volk1, and Saverio Perugini2 1Department of Mathematics, University of Nebraska Lincoln, Lincoln, Nebraska, USA 2Department of Computer Science, University of Dayton, Dayton, Ohio, USA Programming Languages Abstract— We provide a brief introduction to CLIPS—a declarative/logic programming language for implementing expert systems—and PROLOG—a declarative/logic program- ming language based on first-order, predicate calculus. Un- Procedural Non-procedural like imperative languages in which the programmer specifies how to compute a solution to a problem, in a declarative Imperative Functional language, the programmer specifies what they what to find, Non-declarative Declarative C LISP and the system uses a search strategy built into the language. Artificial Rule Based Logic We also briefly discuss applications of CLIPS and PROLOG. Neural Systems CLIPS PROLOG Fig. 1: A hierarchy of programming paradigms and lan- Keywords: CLIPS, declarative programming, first-order predicate guages (adapted from [6]). calculus, logic programming, production system, PROLOG 1. Introduction CLIPS1 is a declarative/logic programming language for both declarative programming language and, thus, involve implementing expert systems. Originally called NASA’s the declaration of facts and rules, they each use fundamen- Artificial Intelligence Language (NAIL), CLIPS started as a tally different search strategies. tool for creating expert systems at NASA in the 1980s. An expert system is a computer program capable of model- ing the knowledge of a human expert [1]. CLIPS stands 2.1 Horn Clauses for CLanguage Integrated Production System. In artificial intelligence, a production system is a computer system Logic programming in PROLOG is based on first-order, which relies on facts and rules to guide its decision mak- predicate calculus—a formal system of logic which uses ing. Programming in a declarative, rule-based language variables, predicates, quantifiers, and logical connectives like CLIPS is fundamentally different than programming to produce propositions involving clauses. Clausal form is in more traditional programming languages like C or Java a standard (and simplified) form for propositions: B1 _ and more resembles programming in PROLOG [2], [3], [4], B2 _···_ Bn ⊂ A1 ^ A2 ^ · · · ^ Am. The As and Bs [5]—a declarative/logic language to which we compare are terms. The left hand side is called the consequent, CLIPS throughout this paper.2 Fig.1 situates CLIPS (and while the right hand side is called the antecedent. The PROLOG) in relation to other programming paradigms and interpretation is: if all of the As are true, then at least languages [6]. one of the Bs must be true. Advantages of representing propositions in clausal form are (i) existential quantifiers are 2. Declarative/Logic Programming unnecessary; (ii) universal quantifiers are implicit in the use In the declarative paradigm of programming, rather than of variables in the atomic propositions; (iii) no operators describing how to compute a solution as done when pro- other than conjunction and disjunction are required; and gramming in, e.g., Java, the programmer describes what a (iv) all predicate calculus propositions can be converted to solution to a problem looks like through the declaration of clausal form. “When propositions are used for resolution,[— facts and rules describing it. While CLIPS and PROLOG are a rule of inference discussed below—]only a restricted kind of clausal form called a Horn clause can be used, which 1http://www.clipsrules.net/ further simplifies the resolution process” [7]. Horn clauses 2PROLOG, which stands for PROgramming in LOGic, is a declarative/- conform to one of the three forms detailed in Table1[8]. logic programming language developed in the early 1970s for artificial intelligence applications. The PROLOG interpreter assumed in this paper All expressions in PROLOG match one of these three forms is SWI-PROLOG—http://www.swi-prolog.org/. for Horn clauses. Cite as: Watkin, J.L., Volk, A.C., & Perugini, S. (2019). An introduction to declarative programming in CLIPS and PROLOG. In Arabnia, H.R., Deligiannidis, L., Grimaila, M.R., Hodson, D.D., & Tinetti, F.G. (Eds.), Proceedings of the International Conference on Scientific Computing (CSC), 105–111. USA: CSREA Press. (Publication of the World Congress in Computer Science, Computer Engineering, and Applied Computing (CSCE). Available at https://csce.ucmss.com/cr/books/2019/LFS/CSREA2019/CSC2488.pdf.) 106 Int’l Conf. Scientific Computing j CSC’19 j Table 1: Types of Horn clauses. Table 2: Essential CLIPS shell commands. Form Horn Clause Type PROLOG Name Command Function fg ⊂ B1 ^ ::: ^ Bn; n ≥ 1 Headless Goal (run) Run the inference engine. A ⊂ fg Headed Fact (facts) Retrieve the current fact-list. A ⊂ B1 ^ ::: ^ Bn; n ≥ 1 Headed Rule (clear) Restores CLIPS to start-up state. (retract n) Retract fact n. (retract *) Retract all facts. (watch facts) Observe facts entering or exiting memory. accessible from within the CLIPS shell and usable in CLIPS scripts. For purposes of comparison, we can declare the proposi- tion ‘It is raining’ in PROLOG with the fact: weather(raining). Fig. 2: Architectural design of CLIPS (adapted from [6]). Similarly, we can declare the rule ‘If it is raining, then I carry an umbrella’ in PROLOG with the rule. carry(umbrella):− weather(raining). 2.2 Asserting Facts and Rules Additionally, consider the following set of facts and rule in Like PROLOG, in CLIPS expert systems, knowledge is PROLOG: represented as facts and rules and, thus, a CLIPS or PROLOG color(red). program consists of facts and rules. A fact is an axiom that color(yellow). is asserted as true. A rule is a declaration expressed in the color(blue). form of an IF THEN statement. For example, a fact may lightcolor(X):− color(yellow). be ‘It is raining.’ In CLIPS this fact is written as: These facts assert that red, yellow, and blue are colors. The (assert (weather raining)) rule declares that yellow is a light color. The assert keyword defines facts, which are inserted in FIFO order into the fact-list. Facts can also be added 2.3 Resolution and Unification to the fact-list with the deffacts command. An The Match-Resolve-Act cycle is the foundation of the example rule is ‘If it is raining, then I carry an umbrella’: CLIPS inference engine which performs pattern matching be- (defrule ourrule tween rules and facts through the use of the Rete Algorithm. (weather raining) => Once the CLIPS inference engine has matched all applicable (assert (carry umbrella))) rules, conflict resolution occurs. Conflict resolution is the process of scheduling rules that were matched at the same The following is the general syntax of a rule [1]3: time. Once the actions have been performed, the inference (defrule rule_name (pattern_1); IF Condition 1 engine returns to the pattern matching stage to search for (pattern_2); And Condition 2 new rules that may be matched as a result of the previous . actions. This process continues until a fixed point is reached. (pattern_N); And Condition N In PROLOG, however, the user gives the inference engine => ; THEN (action_1); Perform Action 1 a goal that it then sets out to satisfy (i.e., prove) based on (action_2); And Action 2 the knowledge base of facts and rules. To run a program, . the user supplies one or more goals, each in the form of a (action_N)) ; And Action N headless Horn clause (see Table1). The activity of supplying a goal can be viewed as asking questions of the program The CLIPS shell can be invoked in UNIX-based systems or querying the system as one does with a database system. with the clips command. From within the CLIPS shell, the user can assert facts, defrules, and (run) the When a goal is given, the inference engine attempts to match inference engine. When the user issues the (run) com- the goal with the head of a headed Horn clause, which can mand, the inference engine pattern matches facts with rules. be either a fact or a rule. PROLOG works backward from If all patterns are matched within the rule, then the actions the goal using a rule of inference called resolution to find associated with that rule are fired. To load facts and rules a series of facts and rules which can be used to prove the from an external file, use the -f option (e.g., clips -f goal. This approach is called backward chaining because database.clp). Table2 is a summary of commands the system works backward from a goal to find a path to prove that the goal is true. CLIPS, on the other hand, works 3Note that ; begins a comment. in the opposite direction. CLIPS takes asserted facts and Int’l Conf. Scientific Computing j CSC’19 j 107 attempts to match them to rules to make inferences.