Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Produce solutions where the order doesn't matter #17

Open
L7R7 opened this issue Feb 15, 2022 · 1 comment
Open

Produce solutions where the order doesn't matter #17

L7R7 opened this issue Feb 15, 2022 · 1 comment

Comments

@L7R7
Copy link

L7R7 commented Feb 15, 2022

First off, thanks for your work on this library and for providing such extensive docs. It was super easy for me to get started!

I'm currently experimenting with a constellation where the order of the elements in the solution doesn't matter. I'm pretty sure I'm missing something here, so maybe you can help me out.

data M = A | B | C | D deriving (Bounded, Eq, Enum, Ord, Generic, Hashable, Show)

solve :: IO [[Defined M]]
solve = do
  let guesses = 3 `from` [A .. D]
  guesses `whenever` \solution -> and' [distinct solution]

run :: IO ()
run = solve >>= traverse_ print . take 3

This will print

[Exactly A,Exactly B,Exactly C]
[Exactly A,Exactly B,Exactly D]
[Exactly A,Exactly C,Exactly B]

However, I'd like to add a constraint that makes the solver treat the first and the third solution as equal solutions (and therefore delete one of them from the set of possible solutions).
I could map over the solution afterwards, but this doesn't help in making the computation run faster. That's why I want to include it in the constraints.

Is there a way to do that?

@L7R7
Copy link
Author

L7R7 commented Feb 15, 2022

I came up with this variant:

solve :: IO [[Defined (Set M)]]
solve = do
  let allValues = [A .. D]
  let setsOfThree = filter (\s -> S.size s == 3) $ nub $ (\a b c -> S.fromList [a, b, c]) <$> allValues <*> allValues <*> allValues
  let guesses = 1 `from` setsOfThree
  guesses `whenever` \solution -> and' []

which uses Sets as inputs instead of single elements. I don't know how well that will work well for data types with higher cardinality

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant