Rationals.
The internal data structure for rational numbers is
rat(
Num,
Den)
. Den is always positive, i.e.
the sign of the rational number is the sign of Num. Further,
Num and Den are relative prime. Note that integer N
looks like rat(
N,1)
in this representation. You can
control printing of terms with user:portray/1
.
Partial Evaluation, Compilation.
Once one has a working solver, it is obvious and attractive to run the
constraints in a clause definition at read time or compile time and
proceed with the answer constraints in place of the original
constraints. This gets you constant folding and in fact the full
algebraic power of the solver applied to the avoidance of computations
at runtime. The mechanism to realize this idea is to use
dump/3
for the expansion of {}/1
, via the goal and
term expansion hook predicates.
Asserting with Constraints.
If you use the database, the clauses you assert might have
constraints associated with their variables. You should use
projecting_assert/1
instead of assert/1
in order to ensure
that only the relevant and projected constraints get stored in the
database.
| ?- {A+B=<33}, projecting_assert(test(A,B)). {A+B=<33} | ?- listing(test). test(A, B) :- {A+B=<rat(33,1)} | ?- test(A,B). {A+B=<33}