Logic Programming with Clojure - Overview and Examples

cs152 n.w
1 / 25
Embed
Share

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.

  • Clojure
  • Logic Programming
  • Declarative
  • Development
  • Examples

Uploaded on | 0 Views


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


  1. CS152 Programming Paradigms Thaddeus Aid Department of Computer Science San Jose State University Spring 2016 1 Creative Commons Attribution-ShareAlike 4.0 International License

  2. Homework 1 Now Due Friday before 23:59 Creative Commons Attribution-ShareAlike 4.0 International License 2

  3. 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

  4. Preparing your Clojure Environment Creative Commons Attribution-ShareAlike 4.0 International License 4

  5. Preparing Cont Creative Commons Attribution-ShareAlike 4.0 International License 5

  6. Prep Cont [org.clojure/core.logic "0.8.10"] Creative Commons Attribution-ShareAlike 4.0 International License 6

  7. Prep Cont Creative Commons Attribution-ShareAlike 4.0 International License 7

  8. Prep cont Creative Commons Attribution-ShareAlike 4.0 International License 8

  9. 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

  10. 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

  11. 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

  12. 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

  13. Constraints Creative Commons Attribution-ShareAlike 4.0 International License 13

  14. 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

  15. Lvar Scope In normal Clojure variable scoping is done with let Core.logic uses fresh Creative Commons Attribution-ShareAlike 4.0 International License 15

  16. 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

  17. 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

  18. 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

  19. 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

  20. 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

  21. 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

  22. 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

  23. Relational Programming Creative Commons Attribution-ShareAlike 4.0 International License 23

  24. Getting Set up Creative Commons Attribution-ShareAlike 4.0 International License 24

  25. Relationships and Queries Creative Commons Attribution-ShareAlike 4.0 International License 25

Related


More Related Content