2014-04-21 11:14:18 +01:00
|
|
|
/*************************************************************************
|
|
|
|
* *
|
|
|
|
* YAP Prolog *
|
|
|
|
* *
|
|
|
|
* Yap Prolog was developed at NCCUP - Universidade do Porto *
|
|
|
|
* *
|
2014-12-24 15:32:29 +00:00
|
|
|
* Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-1997 *
|
2014-04-21 11:14:18 +01:00
|
|
|
* *
|
|
|
|
**************************************************************************
|
|
|
|
* *
|
|
|
|
* File: arithpreds.yap *
|
|
|
|
* Last rev: *
|
|
|
|
* mods: *
|
|
|
|
* comments: arithmetical predicates *
|
|
|
|
* *
|
|
|
|
*************************************************************************/
|
|
|
|
|
2015-01-04 23:58:23 +00:00
|
|
|
%% @{
|
|
|
|
|
2014-04-21 11:14:18 +01:00
|
|
|
/**
|
|
|
|
@file arithpreds.yap
|
|
|
|
|
|
|
|
@addtogroup arithmetic_preds
|
|
|
|
|
2015-11-05 17:20:25 +00:00
|
|
|
|
2014-04-21 11:14:18 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
:- system_module(arithmetic_predicates, [
|
|
|
|
plus/3,
|
|
|
|
succ/2], []).
|
|
|
|
|
|
|
|
:- use_system_module( '$_errors', ['$do_error'/2]).
|
|
|
|
|
|
|
|
|
2015-11-06 18:22:31 +00:00
|
|
|
a :-
|
|
|
|
a,
|
|
|
|
b,
|
|
|
|
z,
|
|
|
|
za.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-12-24 15:32:29 +00:00
|
|
|
/** @pred succ(? _Int1_:int, ? _Int2_:int) is det
|
2014-04-21 11:14:18 +01:00
|
|
|
*
|
|
|
|
|
|
|
|
True if _Int2_ = _Int1_ + 1 and _Int1_ \>= 0. At least
|
|
|
|
one of the arguments must be instantiated to a natural number. This
|
|
|
|
predicate raises the domain-error not_less_than_zero if called with
|
|
|
|
a negative integer. E.g. `succ(X, 0)` fails silently and `succ(X, -1)`
|
|
|
|
raises a domain-error. The behaviour to deal with natural numbers
|
|
|
|
only was defined by Richard O'Keefe to support the common
|
2015-11-05 17:20:25 +00:00
|
|
|
count-down-to-zero in a natural way.
|
2014-04-21 11:14:18 +01:00
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
% M and N nonnegative integers, N is the successor of M
|
|
|
|
succ(M,N) :-
|
|
|
|
(
|
|
|
|
var(M)
|
|
|
|
->
|
|
|
|
(
|
|
|
|
integer(N),
|
|
|
|
N > 0
|
|
|
|
->
|
|
|
|
'$plus'(N,-1,M)
|
|
|
|
;
|
|
|
|
'$succ_error'(M,N)
|
|
|
|
)
|
|
|
|
;
|
|
|
|
integer(M),
|
|
|
|
M >= 0
|
|
|
|
->
|
|
|
|
(
|
|
|
|
var(N)
|
|
|
|
->
|
|
|
|
'$plus'(M,1,N)
|
|
|
|
;
|
|
|
|
integer(N),
|
|
|
|
N > 0
|
|
|
|
->
|
|
|
|
'$plus'(M,1,N)
|
|
|
|
;
|
|
|
|
'$succ_error'(M,N)
|
|
|
|
)
|
|
|
|
;
|
|
|
|
'$succ_error'(M,N)
|
|
|
|
).
|
|
|
|
|
|
|
|
'$succ_error'(M,N) :-
|
|
|
|
var(M),
|
|
|
|
var(N), !,
|
|
|
|
'$do_error'(instantiation_error,succ(M,N)).
|
|
|
|
'$succ_error'(M,N) :-
|
|
|
|
nonvar(M),
|
|
|
|
\+ integer(M),
|
|
|
|
'$do_error'(type_error(integer, M),succ(M,N)).
|
|
|
|
'$succ_error'(M,N) :-
|
|
|
|
nonvar(M),
|
|
|
|
M < 0,
|
|
|
|
'$do_error'(domain_error(not_less_than_zero, M),succ(M,N)).
|
|
|
|
'$succ_error'(M,N) :-
|
|
|
|
nonvar(N),
|
|
|
|
\+ integer(N),
|
|
|
|
'$do_error'(type_error(integer, N),succ(M,N)).
|
|
|
|
'$succ_error'(M,N) :-
|
|
|
|
nonvar(N),
|
|
|
|
N < 0,
|
|
|
|
'$do_error'(domain_error(not_less_than_zero, N),succ(M,N)).
|
|
|
|
|
2014-12-24 15:32:29 +00:00
|
|
|
/** @pred plus(? _Int1_:int, ? _Int2_:int, ? _Int3_:int) is det
|
2014-04-21 11:14:18 +01:00
|
|
|
|
|
|
|
True if _Int3_ = _Int1_ + _Int2_. At least two of the
|
|
|
|
three arguments must be instantiated to integers.
|
|
|
|
|
|
|
|
@}
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
plus(X, Y, Z) :-
|
|
|
|
(
|
|
|
|
var(X)
|
|
|
|
->
|
|
|
|
(
|
|
|
|
integer(Y), integer(Z)
|
|
|
|
->
|
|
|
|
'$minus'(Z,Y,X)
|
|
|
|
;
|
|
|
|
'$plus_error'(X,Y,Z)
|
|
|
|
)
|
|
|
|
;
|
|
|
|
integer(X)
|
|
|
|
->
|
|
|
|
(
|
|
|
|
var(Y)
|
|
|
|
->
|
|
|
|
(
|
|
|
|
integer(Z)
|
|
|
|
->
|
|
|
|
'$minus'(Z,X,Y)
|
|
|
|
;
|
|
|
|
'$plus_error'(X,Y,Z)
|
|
|
|
)
|
|
|
|
;
|
|
|
|
integer(Y)
|
|
|
|
->
|
|
|
|
(
|
|
|
|
integer(Z)
|
|
|
|
->
|
|
|
|
'$minus'(Z,Y,X)
|
|
|
|
;
|
|
|
|
var(Z)
|
|
|
|
->
|
|
|
|
'$plus'(X,Y,Z)
|
|
|
|
;
|
|
|
|
'$plus_error'(X,Y,Z)
|
|
|
|
)
|
|
|
|
;
|
|
|
|
'$plus_error'(X,Y,Z)
|
|
|
|
)
|
|
|
|
;
|
|
|
|
'$plus_error'(X,Y,Z)
|
|
|
|
).
|
|
|
|
|
|
|
|
'$plus_error'(X,Y,Z) :-
|
|
|
|
nonvar(X),
|
|
|
|
\+ integer(X),
|
|
|
|
'$do_error'(type_error(integer, X),plus(X,Y,Z)).
|
|
|
|
'$plus_error'(X,Y,Z) :-
|
|
|
|
nonvar(Y),
|
|
|
|
\+ integer(Y),
|
|
|
|
'$do_error'(type_error(integer, Y),plus(X,Y,Z)).
|
|
|
|
'$plus_error'(X,Y,Z) :-
|
|
|
|
nonvar(Z),
|
|
|
|
\+ integer(Z),
|
|
|
|
'$do_error'(type_error(integer, Z),plus(X,Y,Z)).
|
|
|
|
'$plus_error'(X,Y,Z) :-
|
|
|
|
'$do_error'(instantiation_error,plus(X,Y,Z)).
|