I'm still very new to prolog, and am trying to wrap my head around why math constraints don't seem to work the same way logical ones do.
It seems like there's enough information to solve this:
f(A, B) :- A = (B xor 2).
But when I try f(C, 3)
, I get back C = 3 xor 2.
which isn't very helpful. Even less useful is the fact that it simply can't find a solution if the inputs are reversed. Using is
instead of =
makes the example input return the correct answer, but the reverse refuses to even attempt anything.
From my earlier experimentation, it seems that I could write a function that did this logically using the binary without trouble, and it would in fact go both ways. What makes the math different?
For reference, my first attempt at solving my problem looks like this:
f(Input, Output) :- A is Input xor (Input >> 11), B is A xor ((A >> 7) /\ 2636928640), C is B xor ((B << 15) /\ 4022730752), Output is C xor (C >> 18).
This works fine going from input to output, but not the other way around. If I switch the is
to =
, it produces a long logical sequence with values substituted but can't find a numerical solution.
I'm using swi-prolog which has xor
built in, but it could just as easily be defined. I was hoping to be able to use prolog to work this function in both directions, and really don't want to have to implement the logical behaviors by hand. Any suggestions about how I might reformulate the problem are welcome.