# Using clp(QR)
This example is extracted from the [OFAI clp(Q,R) Manual](http://www.ofai.at/cgi-bin/get-tr?download=1&paper=oefai-tr-95-09.pdf) by Christian Holzbaur. It defines the _mortgage_ relation between the following arguments:
- `P` is the balance at _T0_
- `T` is the number of interest periods (e.g., years)
- `I` is the interest ratio where e.g., `0.1` means 10%
- `B` is the balance at the end of the period
- `MP` is the withdrawal amount for each interest period.
@see [Programming with Constraints: An Introduction - Kim Marriott, Peter J. Stuckey](https://books.google.nl/books?id=jBYAleHTldsC&lpg=PA184&ots=QifwRiYDxL&dq=mortgage%20relation%20prolog&pg=PA177#v=onepage&q=mortgage%20relation%20prolog&f=false) which descibes the same example with more background.
:- use_module(library(clpr)).
mg(P,T,I,B,MP):-
{ T = 1,
B + MP = P * (1 + I)
}.
mg(P,T,I,B,MP):-
{ T > 1,
P1 = P * (1 + I) - MP,
T1 = T - 1
},
mg(P1, T1, I, B, MP).
We can use the above program to compute the balance if we get an interest of 5% over a period of 30 years.
mg(1000, 30, 5/100, B, 0).
We can however also compute the linear relation between the initial balance, the final balance and the withdrawal:
mg(B0, 30, 5/100, B, MP).
In the above we used the _imprecise_ library `clpr`. In the queries we used a rational number for the interest. You can replace `clpr` with `clpq` to get exact results.