YAP 7.1.0
arithpreds.yap
Go to the documentation of this file.
1/*************************************************************************
2* *
3* YAP Prolog *
4* *
5* Yap Prolog was developed at NCCUP - Universidade do Porto *
6* *
7 * Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-1997 *
8* *
9**************************************************************************
10* *
11* File: arithpreds.yap *
12* Last rev: *
13* mods: *
14* comments: arithmetical predicates *
15* *
16*************************************************************************/
17
18/**
19 @file arithpreds.yap
20
21 @addtogroup arithmetic Arithmetic Predicates and Functions
22
23@{
24
25*/
26
27:- plus/3succ/2system_module(arithmetic_predicates, [
28 ,
29 ], []).
30
31:- '$do_error'/2use_system_module( '$_errors', []).
32
33/** @pred succ(? _Int1_:int, ? _Int2_:int) is det
34 *
35
36 True if _Int2_ = _Int1_ + 1 and _Int1_ >= 0. At least
37 one of the arguments must be instantiated to a natural number. This
38 predicate raises the domain-error not_less_than_zero if called with
39 a negative integer. E.g. `succ(X, 0)` fails silently and `succ(X, -1)`
40 raises a domain-error. The behaviour to deal with natural numbers
41 only was defined by Richard O'Keefe to support the common
42 count-down-to-zero in a natural way.
43
44 */
45
46% M and N nonnegative integers, N is the successor of M
47succ(M,N) :-
48 (
49 var(M)
50 ->
51 (
52 integer(N),
53 N > 0
54 ->
55 '$plus'(N,-1,M)
56 ;
57 '$succ_error'(M,N)
58 )
59 ;
60 integer(M),
61 M >= 0
62 ->
63 (
64 var(N)
65 ->
66 '$plus'(M,1,N)
67 ;
68 integer(N),
69 N > 0
70 ->
71 '$plus'(M,1,N)
72 ;
73 '$succ_error'(M,N)
74 )
75 ;
76 '$succ_error'(M,N)
77 ).
78
79'$succ_error'(M,N) :-
80 var(M),
81 var(N), var,
82 '$do_error'(instantiation_error,succ(M,N)).
83'$succ_error'(M,N) :-
84 nonvar(M),
85 \+ integer(M),
86 '$do_error'(type_error(integer, M),succ(M,N)).
87'$succ_error'(M,N) :-
88 nonvar(M),
89 M < 0,
90 '$do_error'(domain_error(not_less_than_zero, M),succ(M,N)).
91'$succ_error'(M,N) :-
92 nonvar(N),
93 \+ integer(N),
94 '$do_error'(type_error(integer, N),succ(M,N)).
95'$succ_error'(M,N) :-
96 nonvar(N),
97 N < 0,
98 '$do_error'(domain_error(not_less_than_zero, N),succ(M,N)).
99
100/** @pred plus(? _Int1_:int, ? _Int2_:int, ? _Int3_:int) is det
101
102 True if _Int3_ = _Int1_ + _Int2_. At least two of the
103 three arguments must be instantiated to integers.
104
105 */
106
107plus(X, Y, Z) :-
108 (
109 var(X)
110 ->
111 (
112 integer(Y), integer(Z)
113 ->
114 '$minus'(Z,Y,X)
115 ;
116 '$plus_error'(X,Y,Z)
117 )
118 ;
119 integer(X)
120 ->
121 (
122 var(Y)
123 ->
124 (
125 integer(Z)
126 ->
127 '$minus'(Z,X,Y)
128 ;
129 '$plus_error'(X,Y,Z)
130 )
131 ;
132 integer(Y)
133 ->
134 (
135 integer(Z)
136 ->
137 '$minus'(Z,Y,X)
138 ;
139 var(Z)
140 ->
141 '$plus'(X,Y,Z)
142 ;
143 '$plus_error'(X,Y,Z)
144 )
145 ;
146 '$plus_error'(X,Y,Z)
147 )
148 ;
149 '$plus_error'(X,Y,Z)
150 ).
151
152'$plus_error'(X,Y,Z) :-
153 nonvar(X),
154 \+ integer(X),
155 '$do_error'(type_error(integer, X),plus(X,Y,Z)).
156'$plus_error'(X,Y,Z) :-
157 nonvar(Y),
158 \+ integer(Y),
159 '$do_error'(type_error(integer, Y),plus(X,Y,Z)).
160'$plus_error'(X,Y,Z) :-
161 nonvar(Z),
162 \+ integer(Z),
163 '$do_error'(type_error(integer, Z),plus(X,Y,Z)).
164'$plus_error'(X,Y,Z) :-
165 '$do_error'(instantiation_error,plus(X,Y,Z)).
166
167%% @}
168-
169
integer( T)
nonvar( T)
var( T)
plus(? Int1:int, ? Int2:int, ? Int3:int)
succ(? Int1:int, ? Int2:int)