% In general, mortgage(P, T, I, R, B) is true if B is the balance on a loan 
% after T years at I% interest and repayments of R on an initial drawing of P

mortgage(P, T, I, R, B) :-
% the balance of a loan of term T at interest rate I on a principal P after
% annual repayments R is B IF
        T = 0,
        % the term is zero AND
        B = P.
        % the balance is the principal

mortgage(P, T, I, R, B) :-
% the balance of a loan of term T at interest rate I on a principal P after
% annual repayments R is B IF
        T >= 1,
        % the term is at least 1 year AND
        NT = T - 1,
        % the remaining term 1 year later is NT AND
        NP = P + P * I - R,
        % the new principal amount after one year is the original principal
        % plus this year's interest (ie principal multiplied by interest rate)
        % less the annual repayment AND
        mortgage(NP, NT, I, R, B).
        % the balance of a loan of term NT at interest rate I on a principal NP
        % after annual repayments R is B