<div class="notebook open-fullscreen"> <div class="nb-cell markdown" name="md1"> # Welcome to Prolog! ## History Prolog is a logic programming language associated with artificial intelligence and computational linguistics. Prolog was developed in **France** (where else?), in 1972 by Alain Colmerauer with Philippe Roussel. The name comes from *programmation en logique*. The first implementation of Prolog was an interpreter written in Fortran by Gerard Battani and Henri Meloni. David H. D. Warren took this interpreter to Edinburgh, and there implemented an alternative front-end, which came to define the *Edinburgh Prolog* syntax used by most modern implementations. European AI researchers favored Prolog while Americans favored Lisp, reportedly causing many nationalistic debates on the merits of the languages. ## Introduction In most programming languages we define data, data structures and control flow. Lots and *lots* of control flow... Control flow could be specified using procedures, machine code, functions, streams -- as the programmer you have to define how data is transformed into knowledge and actions (side-effects). In contrast, in Prolog we define facts and rules. We can then run queries over our facts (a knowledge base) and the runtime takes care of the control flow using logical inference and search! Magic. Key terms : 1. Unification : matching a fact to a rule. A rule may be partically unified. 2. Backtracking : When a query fails, prolog traces backwards and tries to satisfy previous queries. 3. Recursion : Recursion is the basis for all search ## Declaring Facts </div> <div class="nb-cell program" data-background="true" name="p1"> employee(dan, "CTO"). employee(diana, "Lead Front-End Engineer"). employee(michael, "UX Lead"). employee(matt, "Director of Engineering"). employee(fletch, "Head of Solution Engineering"). employee(jerome, "Chief Scientist"). employee(adriaan, "Chief Architect"). employee(peter, "CEO"). employee(mike, "CRO"). employee(parsa, "BD and Operations"). employee(petr, "Fullstack Software Engineer"). employee(jolene, "Fullstack Software Engineer"). employee(stefan, "Middleware Software Engineer"). </div> <div class="nb-cell markdown" name="md2"> ## Running Queries Once we have some facts, we can run some queries. Let's start simple. Who is the CTO? </div> <div class="nb-cell query" name="q1"> employee(X, "CTO") </div> <div class="nb-cell markdown" name="md16"> Or, what job does Dan do? </div> <div class="nb-cell query" name="q12"> employee(dan, Role). </div> <div class="nb-cell markdown" name="md3"> Who are the Fullstack Software Engineers? </div> <div class="nb-cell query" data-chunk="5" name="q2"> employee(Name, "Fullstack Software Engineer") </div> <div class="nb-cell markdown" name="md4"> ## Writing Rules Now let's start declaring some more interesting facts and rules. First let's define the organization structure. </div> <div class="nb-cell program" data-background="true" name="p2"> manages(matt, [petr, jerome, diana, adriaan, stefan, jolene, michael ]). manages(dan, [fletch, jerome, adriaan, matt]). manages(peter, [michael]). </div> <div class="nb-cell markdown" name="md5"> Who does Matt manage? </div> <div class="nb-cell query" name="q3"> manages(matt, X) </div> <div class="nb-cell markdown" name="md6"> Now let's declare that if you manage someone, then they report to you. First, because people manage a **LIST** of people, we need to define what it means for an item to be *contained* in a list. A list CONTAINS an item if the first item of the list is the ITEM. **OR** The tail of the list contains the ITEM. Recursion! </div> <div class="nb-cell program" data-background="true" name="p3"> contains(Item,[Item|_]). contains(Item,[_|Tail]):- contains(Item,Tail). reportsTo(Employee, Manager) :- manages(Manager,Reports), contains(Employee,Reports). </div> <div class="nb-cell markdown" name="md7"> Who does petr report to? </div> <div class="nb-cell query" data-chunk="5" name="q4"> reportsTo(petr, Manager). </div> <div class="nb-cell markdown" name="md8"> Who does dan manage? </div> <div class="nb-cell query" data-chunk="5" name="q5"> reportsTo( Employee, dan ). </div> <div class="nb-cell markdown" name="md9"> Let's get crazy... </div> <div class="nb-cell query" data-chunk="50" name="q6"> reportsTo(Employee, Manager). </div> <div class="nb-cell markdown" name="md11"> Who reports to Matt? </div> <div class="nb-cell program" data-background="true" name="p5"> reports(Manager, Employee) :- reportsTo(Employee, Manager). </div> <div class="nb-cell query" data-chunk="10" name="q7"> reports(matt, Employee). </div> <div class="nb-cell markdown" name="md12"> Who has 2 managers? </div> <div class="nb-cell program" data-background="true" name="p6"> twoManagers(Employee) :- reportsTo(Employee, ManagerA), reportsTo(Employee, ManagerB), ManagerA @> ManagerB. </div> <div class="nb-cell query" data-chunk="10" name="q8"> twoManagers(Employee). </div> <div class="nb-cell markdown" name="md13"> Who is based in a location? </div> <div class="nb-cell program" data-background="true" name="p4"> location(la, petr). location(nyc, diana). location(nyc, jerome). location(nyc, michael). location(nyc, parsa). location(nyc, jolene). location(winchester, dan). location(winchester, fletch). location(south_moreton, matt). location(london, adriaan). location(london, stefan). location(dallas, peter). location(sanfrancisco, mike). </div> <div class="nb-cell program" data-background="true" name="p7"> basedAt(Location, Employees) :- findall(Employee, location(Location, Employee), Employees). </div> <div class="nb-cell query" name="q9"> basedAt(london, Employees). </div> <div class="nb-cell markdown" name="md14"> Who is colocated and who is alone? </div> <div class="nb-cell program" data-background="true" name="p8"> colocated(Employee) :- location(Location, Employee), location( Location, EmployeeB), Employee \= EmployeeB. alone(Employee) :- location(Location, Employee), basedAt(Location, Employees), length(Employees, 1). </div> <div class="nb-cell query" data-chunk="5" name="q10"> alone(Employee). </div> <div class="nb-cell markdown" name="md15"> Who manages an employee that is alone and where are they based? </div> <div class="nb-cell query" data-chunk="3" name="q11"> alone(Employee), location(Location, Employee), reportsTo(Employee, Manager). </div> <div class="nb-cell markdown" name="md10"> Summary --- Prolog is ideally suited to projects that require knowledge representation and reasoning. We've only scratched the surface here, there are many important features left for you to explore: - Asserting new facts - Retracting facts - Using constraints - Negation as failure - Defining grammars and language processing - Optimisations to control search (cut) - ... </div> </div>