Question

How do I express the constraint that n variables take different values from a set of n values?

For example, maybe I want to search for the expression of the form

(op1 a (op2 b c))

with the largest value, when a, b and c should 1, 2 and 3 (in some order) while op1 and op2 can each be anything from * + - / exp?

I can see how to make a have a value from 1 2 3 (by using conde for example). And the same for b and c. But then how do I exclude equality? Do I need to loop over all combinations and explicitly exclude them?

Obviously I can do the exclusion "by hand", but I am wondering if there is a better (more efficient) way, or a support library that includes things like this (I have a little more experience with linear programming libs and typically they have a bunch of helper functions that address these kinds of common cases).

And generalising it to the case where some values can appear a certain number of times seems like it's going to be a drag...

And now I think about it, how do I search for a maximum? Is there a good book or set of notes on this?!

[I'm using clojure but my understanding is that clojure-core.logic and minikanren are pretty much identical]

Update: anyone reading this question looking for a good introduction, check out the Alvis paper I mention below in a comment.

Was it helpful?

Solution

(run* [q] 
  (fresh [a b c]
    (== q [a b c])
    (fresh [x y z]
      (rembero a '(* + / - exp) x)
      (rembero b x y)
      (rembero c y z))))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top