Fork me on GitHub
#off-topic
<
2020-02-10
>
Michael J Dorian15:02:04

Clojure/core.logic looks fun, and I'd love to be able to use it just to say I'd done some logic programming. But I'm not clear on what kinds of problems I would really solve with it. I've read that logic programming is good for querying problems, are there any other categories you would consider well suited? I know being a hammer looking for nails is silly, but what can you do 🤷

hindol15:02:46

Based on what I know, logic programming is ONLY good for querying problems. Other problems can be solved if and only if you can represent the problem as a querying problem.

Michael J Dorian15:02:47

I guess the question is then, are there loads of problems that can/should be phrased as querying problems and I just don't how how to think that way, or is this tool just pretty niche

hindol03:02:19

Honest answer is I don't know. I am of the opinion that logic programming is more academic although there are definitely a handful of practical uses. That is why we don't see it in mainstream programming so much.

Stefan07:02:16

Last year I gave a coding dojo on logic programming (using Clojure). I did it because I don’t know too much about logic programming myself, and I’m curious about it. How better to learn than by teaching others, right? 😉 Anyway, a few months later I was doing a couple of Advent of Code puzzles, and then I realized that I could solve one of the “part 2”s super simply by using logic programming. So bottom line: maybe it has to become part of your toolbox before you can identify the situations where it makes sense to use. (For the curious, it was day 2 part 2, and my code is here: https://github.com/svdo/advent-of-code-2019/blob/master/src/aoc/day2.clj).

hindol08:02:47

Another thing to add to your toolbox is https://github.com/clojure/core.match. It can lead to very succinct yet readable code when the situation is right. Here's FizzBuzz,

(require '[clojure.core.match :refer [match]])

(doseq [n (range 1 101)]
  (println
    (match [(mod n 3) (mod n 5)]
      [0 0] "FizzBuzz"
      [0 _] "Fizz"
      [_ 0] "Buzz"
      :else n)))

Stefan09:02:47

Ah yes that’s very nice.

Michael J Dorian15:02:33

Any queries on data I've needed to do can be written as a simple filter statement so far (hobby project) so I don't think it would be upholding simplicity to shoe horn it in there 😁

teodorlu15:02:45

How about K-queens?

teodorlu15:02:48

(re-reading your question, I'm getting the impression that you were asking more broadly than for a specific task)

Michael J Dorian15:02:00

That's an interesting puzzle! I was thinking broadly, somehow being able to use it for good in my existing hobby projects would be best, but I'm not apposed to just solving a weird puzzle as well simple_smile

hindol15:02:04

And maybe a Sudoku?

Mno15:02:30

So apparently Puzzles seems to be a category it would be good at

hindol15:02:16

Yeah, it is constraint programming (is this the right word?) where you define a whole bunch of constraints and ask it to search the solution space for one that satisfies all the constraints.

Michael J Dorian15:02:25

That's some cool stuff! The Sudoku program is so short.

hindol15:02:07

A Sudoku solver is short in Clojure either way, logic programming or not. The main benefit of core.logic is you only have to teach it the rules. You don't have to teach it HOW to reach the solution.

👍 4
p-himik15:02:34

A related question. Suppose you have a bunch of products of different levels of complexity and a bunch of recipes that tell you how to get a particular product from a set of other products, including how much time and resources it would take to make a single specific product and what byproducts will be created. Some recipes can output the same very thing they require, but perhaps a different quantity. The task: given a particular product, find all combinations of recipes that can create it. Ideally, also allow to optimize for some particular product consumption or for total time. How would you approach such problem?

cjsauer16:02:58

I can imagine how logic programming would be great at determining the recipe dependencies, because that’s very query-like (and recursive). I’m unsure tho how you’d express the optimization part of the problem...it’s an aggregate min/max.

jonahbenton20:02:05

My CS Theory is a little rusty but I remember this as Constraint Optimization. Lots of candidate solutions can be generated, then there are constraints that reduce the size of the solution space and order remaining candidates. Implementations of these algorithms are generally called "solvers": https://developers.google.com/optimization/cp In my youth I spent some time studying genetic programming, where the "recipes" are actual instruction sequences and the costs are e.g. execution time, memory use, etc. There is a professor who has a Clojure implementation of GP: https://github.com/lspector/Clojush

cjsauer15:02:58

Sounds like Factorio :)

👍 4
p-himik15:02:06

Because it is. :)