An Introduction to the CLIPS Programming Language
Total Page:16
File Type:pdf, Size:1020Kb
An Introduction to the CLIPS Programming Language Jack L. Watkin Department of Electrical and Computer Engineering University of Dayton Dayton, Ohio 45469–0232 USA [email protected] ABSTRACT Programming Languages We provide an introduction to clips—a declarative programming language for implementing expert systems. KEYWORDS Procedural Nonprocedural clips, expert systems, rule-based language. Imperative Functional Jack L. Watkin. 2017. An Introduction to the CLIPS Programming Nondeclarative Declarative Language. CPS 499-03: Emerging Languages, University of Dayton, c lisp Ohio 45469–0232 USA, Spring 2017, 4 pages. Articial Rule Based Logic Neural Systems 1 INTRODUCTION clips prolog Originally called nasa’s Articial Intelligence Language (nail), Figure 1: A hierarchy of programming paradigms and lan- clips started as a tool for creating expert systems at nasa in the guages (adapted from [2]). 1980s. clips stands for CLanguage Integrated Production System. In AI, a production system is a computer system which relies on facts and rules to guide its decision making. clips is a nonpro- Table 1: Essential clips shell commands. cedural, declarative, and rule-based language, which is dierent than more traditional programming languages like c or lisp. Fig. 1 Command Function situates clips in relation to other programming paradigms and (run) Run the inference engine. languages [2]. (facts) Retrieve the current fact-list. (clear) Restores CLIPS to start-up state. 2 CLIPS FROM THE COMMAND LINE (retract n) Retract fact n. The clips shell can be invoked in unix-based systems through (retract *) Retract all facts. the clips command. From within the shell, the user can assert (watch facts) Observe facts entering or exiting memory. facts, defrules, and (run) the inference engine. To load facts and rules from an external le, use the -f option (e.g., clips -f database.clp). Table 1 is a summary of commands to be used from ( d e f r u l e within the clips shell or that can be used in clips scripts. (weather raining) => 3 EXPERT SYSTEMS IN CLIPS (assert (carry umbrella))) An expert system is a computer program capable of modeling the The assert keyword denes facts, which are inserted in fifo or- knowledge of a human expert [3]. In clips expert systems, knowl- der into the fact-list. Facts can also be added to the fact-list edge is represented as facts and rules. Facts are axioms that are through the deffacts command. The following is the general syn- asserted as true. Rules are declarations expressed in the form of an tax [3]1 of a rule: IF THEN statement. For example, a fact may be ‘It is raining,’ and a rule could be ‘If it is raining, then I carry an umbrella.’ In clips this (defrule rule_name fact and rule could be written as: (pattern_1) ; IF Condition 1 (assert (weather raining)) (pattern_2) ; And Condition 2 . Permission to make digital or hard copies of part or all of this work for personal or (pattern_N) ; And Condition N classroom use is granted without fee provided that copies are not made or distributed for prot or commercial advantage and that copies bear this notice and the full citation => ; THEN on the rst page. Copyrights for third-party components of this work must be honored. (action_1) ; Perform Action 1 For all other uses, contact the owner/author(s). CPS 499-03: Emerging Languages, University of Dayton, Ohio 45469–0232 USA (action_2) ; And Action 2 © 2017 Copyright held by the owner/author(s). 1Note that ; begins a comment. CPS 499-03: Emerging Languages, Spring 2017, University of Dayton, Ohio 45469–0232 USA J.L. Watkin . multiple facts, where each fact is a slot in the template. Rules can . be pattern matched to templates based on a subset of a template’s (action_N)) ; And Action N slots. Below is a demonstration of the use of pattern matching to select specic data from a database of facts. When the user issues the (run) command, the clips inference engine pattern matches facts with rules. If all patterns are matched (deftemplate car within the rule, then the actions associated with that rule are red. ( s l o t make ( type SYMBOL ) 4 THEORY OF CLIPS ( allowed −symbols In logic programming languages like prolog, the inference engine truck compact) is given a goal that it then sets out to prove based on the system’s (default compact)) knowledge base. This approach is called backward chaining because (multislot name the system works backward from a goal to nd a path to prove ( type SYMBOL ) that the goal is true. clips works in the opposite direction. clips (default ?DERIVE))) takes asserted facts and attempts to match them to rules to make (deffacts cars inferences. This is known as forward chaining. The diagram shown in Fig. 2 illustrates the overall architecture of the clips system [4]. (car (make truck) The Match-Resolve-Act Cycle is the foundation of the clips in- (name Tundra)) ference engine which performs pattern matching between rules (car (make compact) and facts through the use of the Rete Algorithm. Once the clips (name Accord)) inference engine has matched all applicable rules, conict resolu- (car (make compact) tion occurs. Conict resolution is the process of scheduling rules (name Passat ))) that were matched at the same time. Once the actions have been performed, the inference engine returns to the pattern matching (defrule compactcar stage to search for new rules that may be matched as a result of the (car (make compact) previous actions. This continues until a xed point is reached. (name ?name)) 5 FEATURES => (printout t ?name crlf)) 5.1 Variables ? ?x Variables in clips are prexed with a (e.g., ). Variables need 5.3 Conditional Facts in Rules not be declared explicitly. However, variables must be bound to a value before they are used. Consider the following program that Pattern matching need not match an exact pattern. Rather logi- computes a factorial: cal operands can be applied to the patterns to support conditional matches. This is done through the or (|), and (&), and not (~) opera- (defrule factorial tors. The following rule demonstrates the use of these operators: (factrun ?x) (defrule walk => (light ~red&~yellow) ;if the light (assert (fact ?x 1))) ; isn ' t yellow and ; isn ' t red (defrule facthelper (cars none|stopped) ;no cars or stopped ( f a c t ? x ?y ) => (test (> ?x 0)) (printout t "Walk" crlf)) => (assert (fact ( − ? x 1 ) ( ∗ ? x ?y ) ) ) ) When the facts for the rule facthelper are pattern matched, ?x and 6 DECISION TREES, FLOW CHARTS, AND ?y are bound with values. Next, the bound value for ?x is used to STATE MACHINES evaluate the validity of the fact (test (> ?x 0)). When variables An application of clips is decision trees. More generally, clips are bound within a rule, that binding exists only within that rule. can be applied to graphs that represent a human decision-making For persistent global data, defglobal should be used as follows: process. Facts can be thought of as the edges of these graphs, while (defglobal ? ∗ var ∗ = " " ) rules can be thought of as the actions or states associated with each node of the graph. Assignment to global variables is done with the bind operator. One example of this decision making process is an expert sys- tem that emulates a doctor in treating, diagnosis, and explaining 5.2 Templates diabetes [1]. The patient asserts facts about herself including eating Templates are used to associate related data (e.g., facts) in a single habits, blood-sugar levels, and symptoms. The rules within this ex- package—similar to structs in c. Templates are containers for pert system match these facts and provide recommendations about An Introduction to the CLIPS Programming LanguageCPS499-03: Emerging Languages, Spring 2017, University of Dayton, Ohio 45469–0232 USA Figure 2: Overall architectural design of clips (adapted from [2]). managing diabetes in the same way a doctor may interact with a CLIPS> (reset) patient. CLIPS> (run) String? aabbba 7 EXERCISES Accepted The following are programming exercises that incorporate essential clips concepts: CLIPS > (1) Build a state machine using clips that accepts a language (2) Rewrite the factorial program in x 5.1 so that only the fact L consisting of strings in which the number of a’s in the with the nal result of the factorial rule is stored in the fact string is a multiple of three over an alphabet {a,b}. Hint: list. Hint: retract can be used to remove facts from the use the following state machine for L: fact list. Examples: CLIPS> (assert (fact_run 5)) CLIPS> (run) CLIPS> (facts) f −0 (fact_run 5) f −1 (fact 0 120) CLIPS > 8 CONCLUSION Clips is a language which allows for the implementation of ‘rule of thumb’ knowledge similar to that of a human expert. These forward-chaining systems help a computer make heuristic decisions by considering the facts (or lack of facts) of a situation and drawing conclusions. Because clips can handle arbitrarily large data, it has an advantage over human experts because humans are limited in Examples: their ability to consider many facts at once. CLIPS> (run) String? aaabba REFERENCES [1] M. Garcia, T. Gandhi, J. Singh, L. Duarte, R. Shen, M. Dantu, S. Ponder, and R e j e c t e d H. Ramirez. 2001. Esdiabetes (an Expert System in Diabetes). Consortium for Computing Sciences in Colleges. 166–175 pages. CPS 499-03: Emerging Languages, Spring 2017, University of Dayton, Ohio 45469–0232 USA J.L. Watkin [2] J.C. Giarratano and G. Riley. 1998. Expert systems principles and programming. [4] P. Lucas and L. van der Gaag. 1991. Principles of expert systems. Addison-Wesley, PWS Publishing Company, Boston, MA.