
Logic Programming with Clojure - Overview and Examples
Explore the world of logic programming using Clojure with tutorials, resources, and practical examples. Learn the fundamentals of declarative programming, how to prepare your development environment, and meet logical variables. Understand the concepts of run and run* operators to find solutions efficiently. Get ready to dive into the realm of logic-driven development in Clojure.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
CS152 Programming Paradigms Thaddeus Aid Department of Computer Science San Jose State University Spring 2016 1 Creative Commons Attribution-ShareAlike 4.0 International License
Homework 1 Now Due Friday before 23:59 Creative Commons Attribution-ShareAlike 4.0 International License 2
Logic Programming using Clojure Citations for this lecture https://www.youtube.com/watch?v=n2Wz9oKpZjs https://www.youtube.com/watch?v=irjP8BO1B8Y http://www.braveclojure.com/do-things/ https://github.com/clojure/core.logic/wiki/A-Core.logic-Primer#Introduction https://github.com/swannodette/logic-tutorial https://github.com/clojure/core.logic https://github.com/technomancy/leiningen Creative Commons Attribution-ShareAlike 4.0 International License 3
Preparing your Clojure Environment Creative Commons Attribution-ShareAlike 4.0 International License 4
Preparing Cont Creative Commons Attribution-ShareAlike 4.0 International License 5
Prep Cont [org.clojure/core.logic "0.8.10"] Creative Commons Attribution-ShareAlike 4.0 International License 6
Prep Cont Creative Commons Attribution-ShareAlike 4.0 International License 7
Prep cont Creative Commons Attribution-ShareAlike 4.0 International License 8
Meet [q] By convention the logical variable (lvar) is named q [q] will take on the 0+ answers that answer our queries Logic programming is declarative I declare a statement and it is either true or false A statement can be true in more than one way A pure function must only return 1 parameter Hence, logic is useful when more than one result is necessary Creative Commons Attribution-ShareAlike 4.0 International License 9
Run and run* The operator run is used to signify a logic operation (run 1 [q] (== q 1)) Means find 1 value for q that stratifies the declaration that q unifies with 1 The number element following run determines how many solutions for q the solution engine looks for (run* [q] (== q 1)) This means look for all of the solutions for q unifies with 1 Creative Commons Attribution-ShareAlike 4.0 International License 10
Unify Sort of equals (== q 1) This means that q must take the form that matches 1 (== q (+ 10 5)) This time q must take the form that satisfies q == 15 Creative Commons Attribution-ShareAlike 4.0 International License 11
Constraints When [q] can take many forms and we wish to limit those forms we can add additional constraints (run* [q] (some declaration) (some other declaration) (etc.)) The resulting [q] must make all declarations true Creative Commons Attribution-ShareAlike 4.0 International License 12
Constraints Creative Commons Attribution-ShareAlike 4.0 International License 13
Unbound lvars Some times an lvar can return a true statement for any value This is denoted as _.0 _.1 _.n where the lvar at position .x can take any value Creative Commons Attribution-ShareAlike 4.0 International License 14
Lvar Scope In normal Clojure variable scoping is done with let Core.logic uses fresh Creative Commons Attribution-ShareAlike 4.0 International License 15
More on Constraints A set of constraints acts as AND (run* [q] (constraint 1) (constraint 2) (constraint 3)) [q] must obey all constraints. Each expression is a Goal Creative Commons Attribution-ShareAlike 4.0 International License 16
Operators Fresh Introduces new lvars with limited scope Unify (== lvar1 lvar2) If lvar1 and lvar2 can be made to be equal it is a success otherwise fail Conde Logical OR Creative Commons Attribution-ShareAlike 4.0 International License 17
Conde (new* [q] (conde [goal1] [goal2] [goal2])) Find q where goal1 OR goal2 OR goal3 is true Creative Commons Attribution-ShareAlike 4.0 International License 18
More Complex Queries (new* [q] (conde [goal1 goal2] [goal3]) (goal4)) Find a q where ((goal1 AND goal2) OR goal3) AND goal4 is true Creative Commons Attribution-ShareAlike 4.0 International License 19
Conso Conso (conso x r s) Returns true where list (x r) == list (s) Where X is the head of a list R is the rest of a list S is a sequence Creative Commons Attribution-ShareAlike 4.0 International License 20
Resto Resto (resto l r) Where L is a list R is a match for the rest of the list Creative Commons Attribution-ShareAlike 4.0 International License 21
Membero Membero (member l x) Returns true when x is a member of l Where X is an element L is a list Creative Commons Attribution-ShareAlike 4.0 International License 22
Relational Programming Creative Commons Attribution-ShareAlike 4.0 International License 23
Getting Set up Creative Commons Attribution-ShareAlike 4.0 International License 24
Relationships and Queries Creative Commons Attribution-ShareAlike 4.0 International License 25