| 1 | % (c) 2009-2026 Lehrstuhl fuer Softwaretechnik und Programmiersprachen, | |
| 2 | % Heinrich Heine Universitaet Duesseldorf | |
| 3 | % This software is licenced under EPL 1.0 (http://www.eclipse.org/org/documents/epl-v10.html) | |
| 4 | ||
| 5 | :- module(kernel_card_arithmetic,[is_inf_or_overflow_card/1, is_finite_card/1, | |
| 6 | safe_pow2/2, safe_pown/3, | |
| 7 | safe_add_card/3, blocking_add_card/3, | |
| 8 | safe_mul/3, | |
| 9 | safe_minus/3, blocking_factorial/2, blocking_factorial_k/3, | |
| 10 | choose_nk/3, | |
| 11 | blocking_nr_iseq/2, blocking_nr_iseq1/2, | |
| 12 | total_relation_card/3, partial_bijection_card/3, | |
| 13 | partial_surjection_card/3, total_surjection_card/3, | |
| 14 | partial_injection_card/3, | |
| 15 | total_bijection_card/3]). | |
| 16 | ||
| 17 | ||
| 18 | :- use_module(module_information). | |
| 19 | :- module_info(group,kernel). | |
| 20 | :- module_info(description,'This module contains cardinality arithmetic that also supports the symbol inf and inf_overflow.'). % in future we may support other ordinals to distinguish countable and uncountable sets | |
| 21 | ||
| 22 | :- use_module(error_manager). | |
| 23 | :- use_module(debug). | |
| 24 | :- use_module(self_check). | |
| 25 | ||
| 26 | :- assert_must_succeed((safe_pow2(3,R),R==8)). | |
| 27 | :- assert_must_succeed((safe_pow2(inf,R),R==inf)). | |
| 28 | :- assert_must_succeed((safe_pow2(inf_overflow,R),R==inf_overflow)). | |
| 29 | :- assert_must_succeed((safe_pow2(3072,R),R==inf_overflow)). | |
| 30 | :- assert_must_succeed((safe_pow2(18446744073709551616,R),R==inf_overflow)). | |
| 31 | :- assert_must_succeed((safe_pow2(500,X), safe_pow2(501,X2), X2 is 2*X)). | |
| 32 | % :- assert_must_succeed((kernel_objects:safe_pow2(1022,X), kernel_objects:safe_pow2(1023,X2), X2 is 2*X)). | |
| 33 | ||
| 34 | set_inf_due_to_overflow(Res) :- debug_println(9,'** OVERFLOW (cardinality)**'),Res=inf_overflow. | |
| 35 | %set_inf_due_to_overflow(Res) :- Res=inf. | |
| 36 | ||
| 37 | safe_pow2(Exp,Res) :- | |
| 38 | (Exp==inf -> Res=inf | |
| 39 | ; Exp==inf_overflow -> Res=inf_overflow | |
| 40 | ; Exp>1023 /* the limit where SICStus 4.2.1 reported inf; 4.2.3 goes further but uses a huge amount of memory */ | |
| 41 | -> set_inf_due_to_overflow(Res) | |
| 42 | ; Res is 2^Exp % ^ integer exponentiation operator new in SICStus 4.3 | |
| 43 | ). | |
| 44 | ||
| 45 | :- assert_must_succeed((safe_pown(3,2,R),R==9)). | |
| 46 | :- assert_must_succeed((safe_pown(2,3072,R),R==inf_overflow)). | |
| 47 | :- assert_must_succeed((safe_pown(3,647,R),R==inf_overflow)). | |
| 48 | :- assert_must_succeed((safe_pown(2,500,X), safe_pown(2,501,X2), X2 is 2*X)). | |
| 49 | :- assert_must_succeed((safe_pown(2,500,X), safe_pow2(500,X))). | |
| 50 | :- assert_must_succeed((safe_pown(2,1022,X), safe_pown(2,1023,X2), X2 is 2*X)). | |
| 51 | :- assert_must_succeed((safe_pown(3,500,X), safe_pown(3,501,X3), X3 is 3*X)). | |
| 52 | :- assert_must_succeed((safe_pown(1000,102,R),R =:= 10^306)). | |
| 53 | :- assert_must_succeed((safe_pown(1000,103,R),R==inf_overflow)). | |
| 54 | :- assert_must_succeed((safe_pown(1,1000000,R),R==1)). | |
| 55 | :- assert_must_succeed((safe_pown(-1,1000001,R),R== -1)). | |
| 56 | :- assert_must_succeed((safe_pown(0,1000000,R),R==0)). | |
| 57 | :- assert_must_succeed((safe_pown(inf,2,R),R==inf)). | |
| 58 | :- assert_must_succeed((safe_pown(inf,inf,R),R==inf)). | |
| 59 | :- assert_must_succeed((safe_pown(inf_overflow,2,R),R==inf_overflow)). | |
| 60 | :- assert_must_succeed((safe_pown(2,inf,R),R==inf)). | |
| 61 | :- assert_must_succeed((safe_pown(2,inf_overflow,R),R==inf_overflow)). | |
| 62 | :- assert_must_succeed((safe_pown(inf_overflow,inf,R),R==inf)). | |
| 63 | :- assert_must_succeed((safe_pown(inf_overflow,inf_overflow,R),R==inf_overflow)). | |
| 64 | safe_pown(Base,Exp,Res) :- | |
| 65 | ( Base=inf -> (Exp=0 -> Res=1 ; Res=Base) | |
| 66 | ; Exp=inf -> (Base=0 -> Res=0 ; Base=1 -> Res=1 ; Res=Exp) | |
| 67 | ; Base=inf_overflow -> (Exp=0 -> Res=1 ; Res=Base) | |
| 68 | ; Exp=inf_overflow -> (Base=0 -> Res=0 ; Base=1 -> Res=1 ; Res=Exp) | |
| 69 | ; infinite_pown(Base,Exp) /* SICStus 4.2.1 reported inf; 4.2.3 goes further but uses a huge amount of memory */ | |
| 70 | -> set_inf_due_to_overflow(Res) | |
| 71 | ; Res is Base ^ Exp % ^ integer exponentiation operator new in SICStus 4.3 | |
| 72 | ). | |
| 73 | ||
| 74 | /* SICStus 4.2.1 reported inf; 4.2.3 goes further but uses a huge amount of memory */ | |
| 75 | % we treat abs(Base)^Exp >= 2^1024 as overflow (like safe_pow2), independently of the size of Base | |
| 76 | % abs(Base) >= 2^msb(abs(Base)), hence the result has at least Exp*msb(abs(Base)) + 1 bits | |
| 77 | % and at most Exp*(msb(abs(Base))+1) bits; only in between do we have to compute the power | |
| 78 | :- assert_must_succeed(infinite_pown(2,1024)). | |
| 79 | :- assert_must_fail(infinite_pown(2,1023)). | |
| 80 | :- assert_must_fail(infinite_pown(3,646)). | |
| 81 | :- assert_must_succeed(infinite_pown(3,647)). | |
| 82 | :- assert_must_fail(infinite_pown(5,441)). | |
| 83 | :- assert_must_succeed(infinite_pown(5,442)). | |
| 84 | :- assert_must_succeed(infinite_pown(1000,103)). % previously no overflow was detected below exponent 442 | |
| 85 | :- assert_must_fail(infinite_pown(1000,102)). | |
| 86 | :- assert_must_succeed(infinite_pown(-3,647)). | |
| 87 | :- assert_must_fail(infinite_pown(1,100000)). | |
| 88 | :- assert_must_fail(infinite_pown(0,100000)). | |
| 89 | :- assert_must_fail(infinite_pown(-1,100000)). | |
| 90 | :- assert_must_succeed(infinite_pown(2,1000000000000000000000000)). | |
| 91 | infinite_pown(Base,Exp) :- | |
| 92 | Exp > 0, AB is abs(Base), AB >= 2, | |
| 93 | M is msb(AB), % AB >= 2^M | |
| 94 | (Exp*M >= 1024 -> true % AB^Exp >= 2^(M*Exp) >= 2^1024 | |
| 95 | ; Exp*(M+1) < 1024 -> fail % AB^Exp < 2^((M+1)*Exp) < 2^1024 | |
| 96 | ; AB^Exp >= 2^1024). % the result has at most 2047 bits here | |
| 97 | ||
| 98 | is_inf_or_overflow_card(inf). | |
| 99 | is_inf_or_overflow_card(inf_overflow). | |
| 100 | ||
| 101 | is_finite_card(Card) :- number(Card). | |
| 102 | ||
| 103 | ||
| 104 | :- assert_must_succeed((safe_mul(3,2,R),R==6)). | |
| 105 | :- assert_must_succeed((safe_mul(inf,2,R),R==inf)). | |
| 106 | :- assert_must_succeed((safe_mul(2,inf,R),R==inf)). | |
| 107 | :- assert_must_succeed((safe_mul(0,inf,R),R==0)). | |
| 108 | :- assert_must_succeed((safe_mul(inf,0,R),R==0)). | |
| 109 | % safe_multiplication for positive numbers | |
| 110 | % note: we use == rather than head unification so that unbound arguments are not instantiated; | |
| 111 | % calling safe_mul with unbound arguments is an error (raised by is_overflowcheck) | |
| 112 | :- assert_must_succeed((catch(safe_mul(X,2,_),error(instantiation_error,_),true), var(X))). | |
| 113 | :- assert_must_succeed((catch(safe_mul(2,X,_),error(instantiation_error,_),true), var(X))). | |
| 114 | safe_mul(X,Y,R) :- | |
| 115 | (X==0 -> R=0 % true for cartesian product: card({}*INTEGER)=0 | |
| 116 | ; Y==0 -> R=0 % ditto | |
| 117 | ; X==inf -> R=inf | |
| 118 | ; Y==inf -> R=inf | |
| 119 | ; X==inf_overflow -> R=inf_overflow | |
| 120 | ; Y==inf_overflow -> R=inf_overflow | |
| 121 | ; is_overflowcheck(R,X*Y)). | |
| 122 | ||
| 123 | :- assert_must_succeed((safe_add_card(3,100,R),R==103)). | |
| 124 | :- assert_must_succeed((safe_add_card(inf,100,R),R==inf)). | |
| 125 | :- assert_must_succeed((safe_add_card(inf,inf_overflow,R),R==inf)). | |
| 126 | :- assert_must_succeed((safe_add_card(inf_overflow,inf,R),R==inf)). | |
| 127 | :- assert_must_succeed((safe_add_card(inf_overflow,2,R),R==inf_overflow)). | |
| 128 | % note: as for safe_mul, unbound arguments are not instantiated | |
| 129 | :- assert_must_succeed((catch(safe_add_card(X,1,_),error(instantiation_error,_),true), var(X))). | |
| 130 | :- assert_must_succeed((catch(safe_add_card(1,X,_),error(instantiation_error,_),true), var(X))). | |
| 131 | safe_add_card(X,Y,R) :- | |
| 132 | (X==inf -> R=inf | |
| 133 | ; Y==inf -> R=inf | |
| 134 | ; X==inf_overflow -> R=inf_overflow | |
| 135 | ; Y==inf_overflow -> R=inf_overflow | |
| 136 | ; is_overflowcheck(R,X+Y)). | |
| 137 | ||
| 138 | ||
| 139 | /* is with overflow check */ | |
| 140 | % only arithmetic errors are treated as overflow (SICStus integer arithmetic itself never overflows); | |
| 141 | % other errors (e.g., instantiation or type errors) indicate a bug and are passed on | |
| 142 | :- assert_must_succeed((is_overflowcheck(R,3*4),R==12)). | |
| 143 | :- assert_must_succeed((is_overflowcheck(R,inf),R==inf)). | |
| 144 | :- assert_must_succeed((catch(is_overflowcheck(_,_*4),error(instantiation_error,_),true))). | |
| 145 | :- assert_must_succeed((catch(is_overflowcheck(_,foo*4),error(type_error(_,_),_),true))). | |
| 146 | is_overflowcheck(Var,Expr) :- | |
| 147 | (Expr=inf -> Var=inf | |
| 148 | ; catch(Var is Expr, error(Err,_), | |
| 149 | (is_arithmetic_overflow_error(Err) -> set_inf_due_to_overflow(Var) % optionally add enumeration warning in scope | |
| 150 | ; throw(error(Err,_))) | |
| 151 | ) | |
| 152 | ). | |
| 153 | ||
| 154 | is_arithmetic_overflow_error(evaluation_error(_)). | |
| 155 | is_arithmetic_overflow_error(representation_error(_)). | |
| 156 | is_arithmetic_overflow_error(resource_error(_)). | |
| 157 | ||
| 158 | :- assert_must_succeed((blocking_add_card(3,100,R),R==103)). | |
| 159 | :- assert_must_succeed((blocking_add_card(inf,100,R),R==inf)). | |
| 160 | :- assert_must_succeed((blocking_add_card(inf,inf_overflow,R),R==inf)). | |
| 161 | :- assert_must_succeed((blocking_add_card(inf_overflow,inf,R),R==inf)). | |
| 162 | :- assert_must_succeed((blocking_add_card(inf_overflow,2,R),R==inf_overflow)). | |
| 163 | ||
| 164 | % add two cardinalities together: can be number or inf | |
| 165 | :- block blocking_add_card(-,-,?). | |
| 166 | blocking_add_card(X,Y,R) :- X==0,!,R=Y. | |
| 167 | blocking_add_card(X,Y,R) :- Y==0,!,R=X. | |
| 168 | blocking_add_card(X,_,R) :- X==inf,!,R=inf. | |
| 169 | blocking_add_card(_,Y,R) :- Y==inf,!,R=inf. | |
| 170 | blocking_add_card(X,Y,R) :- add_card2(X,Y,R). | |
| 171 | ||
| 172 | :- block add_card2(-,?,?), add_card2(?,-,?). | |
| 173 | add_card2(A,B,R) :- safe_add_card(A,B,R). | |
| 174 | ||
| 175 | ||
| 176 | ||
| 177 | :- assert_must_succeed((safe_minus(506,501,R),R==5)). | |
| 178 | :- assert_must_succeed((safe_minus(inf,10,R),R==inf)). | |
| 179 | :- assert_must_succeed((safe_minus(10,inf,R),R==0)). | |
| 180 | :- assert_must_succeed((safe_minus(10,inf,R),R==0, | |
| 181 | (debug:silent_mode(on) -> true ; error_manager:logged_message(safe_minus,_,_,_)), | |
| 182 | retractall(error_manager:logged_message(safe_minus,_,_,_)))). | |
| 183 | % subtraction of two positive numbers, supposing first number >= second one | |
| 184 | :- block safe_minus(-,?,?), safe_minus(?,-,?). | |
| 185 | safe_minus(X,inf,R) :- !, add_message(safe_minus,'Cannot compute X - inf, assuming 0 for X = ',X), R=0. | |
| 186 | safe_minus(inf,_,R) :- !, R=inf. | |
| 187 | safe_minus(X,inf_overflow,R) :- !, add_message(safe_minus,'Cannot compute X - inf_overflow, assuming 0 for X = ',X), R=0. | |
| 188 | safe_minus(inf_overflow,_,R) :- !, R=inf_overflow. | |
| 189 | safe_minus(X,Y,R) :- R is X-Y. | |
| 190 | ||
| 191 | ||
| 192 | /* | |
| 193 | % this code below would sometimes fail in spld generated code: | |
| 194 | is_overflowcheck_old(Var,Expr) :- print(is_with_overflow(Var,Expr)),nl, | |
| 195 | (Expr=inf -> Var=inf | |
| 196 | ; catch( | |
| 197 | catch( | |
| 198 | Var is Expr, | |
| 199 | error(evaluation_error(float_overflow),_), | |
| 200 | (print(float_overflow),nl,Var = inf)), | |
| 201 | error(type_error(evaluable,inf/0),_), | |
| 202 | (print(evaluable_inf),nl,Var=inf))). | |
| 203 | % catches any exception (e.g. also representation_error when converting inf to integer) | |
| 204 | % could be unsafe with timeout !! | |
| 205 | is_errc(Var,Expr) :- | |
| 206 | (Expr=inf -> Var=inf | |
| 207 | ; safe_on_exception(_E, Var is Expr, Var = inf)). | |
| 208 | */ | |
| 209 | ||
| 210 | ||
| 211 | ||
| 212 | :- assert_must_succeed((blocking_factorial(5,R),R==120)). | |
| 213 | :- assert_must_succeed((blocking_factorial(0,R),R==1)). | |
| 214 | :- assert_must_succeed((blocking_factorial(1,R),R==1)). | |
| 215 | :- assert_must_succeed((blocking_factorial(3,R),R==6)). | |
| 216 | :- assert_must_succeed((blocking_factorial(inf,R),R==inf)). | |
| 217 | :- block blocking_factorial(-,?). | |
| 218 | blocking_factorial(inf,R) :- !, R=inf. | |
| 219 | blocking_factorial(inf_overflow,R) :- !, R=inf_overflow. | |
| 220 | blocking_factorial(X,R) :- X<0,!, add_error(blocking_factorial,'Negative arg: ',blocking_factorial(X,R)),R=0. | |
| 221 | blocking_factorial(X,R) :- X>1000,!, | |
| 222 | % here we overapproximate: throw finite warning in Disprover mode ? or when checking for finite | |
| 223 | set_inf_due_to_overflow(R). | |
| 224 | % Note: blocking_safe_pow2(X,R) reports inf for X>1023; for X=171 blocking_factorial gives a result | |
| 225 | % which is greater than 2**1024 : | |
| 226 | % X=171,blocking_factorial(X,R),blocking_safe_pow2(1023,R2), R>2*R2. | |
| 227 | % debug:time(fact(10000,1,_)) --> 170 ms | |
| 228 | % debug:time(fact(1000,1,_)) --> 0 ms | |
| 229 | blocking_factorial(A,Res) :- fact(A,1,Res). | |
| 230 | ||
| 231 | fact(0,Acc,Res) :- !,Res=Acc. | |
| 232 | fact(N,Acc,Res) :- (is_inf_or_overflow_card(Acc) -> Res=Acc | |
| 233 | ; safe_mul(N,Acc,NAcc), N1 is N-1, fact(N1,NAcc,Res)). | |
| 234 | ||
| 235 | ||
| 236 | ||
| 237 | :- assert_must_succeed((blocking_factorial_k(5,5,R),R==120)). | |
| 238 | :- assert_must_succeed((blocking_factorial_k(5,2,R),R==20)). | |
| 239 | :- assert_must_succeed((blocking_factorial_k(5,1,R),R==5)). | |
| 240 | :- assert_must_succeed((blocking_factorial_k(5,0,R),R==1)). | |
| 241 | :- assert_must_succeed((blocking_factorial_k(5,3,R),R==60)). | |
| 242 | :- assert_must_succeed((blocking_factorial_k(5,4,R),R==120)). | |
| 243 | :- assert_must_succeed((blocking_factorial_k(5,6,R),R==0)). | |
| 244 | :- assert_must_succeed((blocking_factorial_k(0,0,R),R==1)). | |
| 245 | :- assert_must_succeed((blocking_factorial_k(0,1,R),R==0)). | |
| 246 | :- assert_must_succeed((blocking_factorial_k(1,1,R),R==1)). | |
| 247 | :- assert_must_succeed((blocking_factorial_k(2,1,R),R==2)). | |
| 248 | :- assert_must_succeed((blocking_factorial_k(2,2,R),R==2)). | |
| 249 | :- assert_must_succeed((blocking_factorial_k(3,3,R),R==6)). | |
| 250 | :- assert_must_succeed((blocking_factorial_k(inf,1,R),R==inf)). | |
| 251 | :- assert_must_succeed((blocking_factorial_k(inf,0,R),R==1)). | |
| 252 | :- assert_must_succeed((blocking_factorial_k(inf,inf,R),R==inf)). | |
| 253 | :- assert_must_succeed((blocking_factorial_k(2,inf,R),R==0)). | |
| 254 | % blocking_factorial_k(N,K,Res) :: Res = N*(N-1)*...*(N-K+1) | |
| 255 | % Number of ways to choose sequences of K different objects amongst a pool of N objects | |
| 256 | :- block blocking_factorial_k(-,?,?),blocking_factorial_k(?,-,?). | |
| 257 | blocking_factorial_k(_X,0,R) :- !, R=1. | |
| 258 | blocking_factorial_k(inf,_,R) :- !, R=inf. | |
| 259 | blocking_factorial_k(_N,K,R) :- K=inf,!, R=0. % only finitely many _N | |
| 260 | blocking_factorial_k(inf_overflow,_,R) :- !, R=inf_overflow. | |
| 261 | blocking_factorial_k(_N,K,R) :- K=inf_overflow,!, R=0. | |
| 262 | blocking_factorial_k(N,K,R) :- K>N,!, R=0. | |
| 263 | blocking_factorial_k(A,K,Res) :- fact_k(A,K,1,Res). | |
| 264 | ||
| 265 | fact_k(0,_,Acc,Res) :- !, Res=Acc. | |
| 266 | fact_k(_,K,Acc,Res) :- K<1,!,Res=Acc. | |
| 267 | fact_k(N,K,Acc,Res) :- (is_inf_or_overflow_card(Acc) -> Res=Acc | |
| 268 | ; safe_mul(N,Acc,NAcc), N1 is N-1, K1 is K-1, fact_k(N1,K1,NAcc,Res)). | |
| 269 | ||
| 270 | ||
| 271 | ||
| 272 | :- assert_must_succeed((choose_nk(3,3,R),R==1)). | |
| 273 | :- assert_must_succeed((choose_nk(3,2,R),R==3)). | |
| 274 | :- assert_must_succeed((choose_nk(3,1,R),R==3)). | |
| 275 | :- assert_must_succeed((choose_nk(3,0,R),R==1)). | |
| 276 | :- assert_must_succeed((choose_nk(3,5,R),R==0)). | |
| 277 | :- assert_must_succeed((choose_nk(0,1,R),R==0)). | |
| 278 | :- assert_must_succeed((choose_nk(3,-1,R),R==0)). | |
| 279 | :- assert_must_succeed((choose_nk(10,4,R),R==210)). | |
| 280 | :- block choose_nk(-,?,?), choose_nk(?,-,?). | |
| 281 | choose_nk(N,K,Res) :- (K=0;K=N),!,Res=1. | |
| 282 | choose_nk(N,K,Res) :- (K<0 ; K>N),!,Res=0. % no way to choose K elements | |
| 283 | choose_nk(N,K,Res) :- K > N/2,!, KN is N-K, choose_nk2(N,KN,Res). | |
| 284 | choose_nk(N,K,Res) :- choose_nk2(N,K,Res). | |
| 285 | choose_nk2(N,K,Res) :- blocking_factorial_k(N,K,NK), blocking_factorial(K,KF), | |
| 286 | safe_div(NK,KF,Res). | |
| 287 | ||
| 288 | :- assert_must_succeed((safe_div(inf,10,R),R==inf)). | |
| 289 | :- assert_must_succeed((safe_div(inf_overflow,10,R),R==inf_overflow)). | |
| 290 | :- assert_must_succeed((safe_div(inf_overflow,inf,R),R==0)). | |
| 291 | :- assert_must_succeed((safe_div(inf,inf_overflow,R),R==inf)). | |
| 292 | :- assert_must_succeed((safe_div(10,inf,R),R==0)). | |
| 293 | :- assert_must_succeed((safe_div(10,5,R),R==2)). | |
| 294 | :- assert_must_succeed((safe_div(10,6,R),R==1)). | |
| 295 | :- assert_must_succeed((safe_div(10,0,R),R==inf, error_manager:get_error(internal_error(_),_), | |
| 296 | error_manager:reset_errors, error_manager:reset_real_error_occurred)). | |
| 297 | :- block safe_div(-,?,?), safe_div(?,-,?). | |
| 298 | safe_div(X,0,R) :- !, add_internal_error('Division by zero: ',safe_div(X,0,R)), R=inf. | |
| 299 | safe_div(inf,K,R) :- | |
| 300 | (number(K) -> true ; K=inf_overflow -> true ; add_message(safe_div,'Cannot compute division precisely, assuming inf: ',safe_div(inf,K))), !, R=inf. | |
| 301 | safe_div(_N,inf,R) :- !, R=0. | |
| 302 | safe_div(inf_overflow,K,R) :- | |
| 303 | (number(K) -> true ; add_message(safe_div,'Cannot compute division precisely, assuming inf_overflow: ',safe_div(inf_overflow,K))), !, R=inf_overflow. | |
| 304 | safe_div(_N,inf_overflow,R) :- !, R=0. | |
| 305 | safe_div(X,Y,Z) :- Z is X//Y. | |
| 306 | ||
| 307 | ||
| 308 | ||
| 309 | :- assert_must_succeed((blocking_nr_iseq(2,R),R==5)). | |
| 310 | :- assert_must_succeed((blocking_nr_iseq(0,R),R==1)). | |
| 311 | :- assert_must_succeed((blocking_nr_iseq(1,R),R==2)). | |
| 312 | :- assert_must_succeed((blocking_nr_iseq(3,R),R==16)). | |
| 313 | :- assert_must_succeed((blocking_nr_iseq(inf,R),R==inf)). | |
| 314 | :- assert_must_succeed((blocking_nr_iseq(inf_overflow,R),R==inf_overflow)). | |
| 315 | % number of injective sequences | |
| 316 | :- block blocking_nr_iseq(-,?). | |
| 317 | blocking_nr_iseq(inf,R) :- !, R=inf. | |
| 318 | blocking_nr_iseq(inf_overflow,R) :- !, R=inf_overflow. | |
| 319 | blocking_nr_iseq(X,R) :- X<0,!, add_error(blocking_nr_iseq,'Negative arg: ',blocking_nr_iseq(X,R)),R=0. | |
| 320 | blocking_nr_iseq(X,R) :- X>500,!, set_inf_due_to_overflow(R). | |
| 321 | % Note: blocking_safe_pow2(X,R) reports inf for X>1023; for X=171 blocking_nr_iseq gives a result | |
| 322 | % which is greater than 2**1024 : | |
| 323 | % X=171,blocking_nr_iseq(X,R),blocking_safe_pow2(1023,R2), R>2*R2. | |
| 324 | % debug:time(nr_iseq(10000,_)) --> 250 ms | |
| 325 | % debug:time(nr_iseq(1000,_)) --> 10 ms | |
| 326 | % debug:time(nr_iseq(500,_)) --> 0 ms | |
| 327 | blocking_nr_iseq(A,Res) :- nr_iseq(A,Res). % , print(nr_iseq(A,Res)),nl. | |
| 328 | % number of injective sequences: f(N) = 1+N*(f(N-1)) | |
| 329 | nr_iseq(0,Res) :- !,Res=1. | |
| 330 | nr_iseq(N,Res) :- N1 is N-1, nr_iseq(N1,N1Res), | |
| 331 | safe_mul(N,N1Res,NAcc), safe_add_card(NAcc,1,Res). | |
| 332 | ||
| 333 | :- assert_must_succeed((blocking_nr_iseq1(2,R),R==4)). | |
| 334 | :- assert_must_succeed((blocking_nr_iseq1(inf,R),R==inf)). | |
| 335 | :- assert_must_succeed((blocking_nr_iseq1(inf_overflow,R),R==inf_overflow)). | |
| 336 | :- public blocking_nr_iseq1/2. % used in card_for_member_closure | |
| 337 | % number of non-empty injective sequences | |
| 338 | :- block blocking_nr_iseq1(-,?). | |
| 339 | blocking_nr_iseq1(inf,R) :- !, R=inf. | |
| 340 | blocking_nr_iseq1(inf_overflow,R) :- !, R=inf_overflow. | |
| 341 | blocking_nr_iseq1(A,Res1) :- blocking_nr_iseq(A,Res) , safe_add_card(Res,-1,Res1). %, print(nr_iseq1(A,Res1)),nl. | |
| 342 | ||
| 343 | ||
| 344 | ||
| 345 | :- assert_must_succeed((total_relation_card(0,inf,R),R==1)). | |
| 346 | :- assert_must_succeed((total_relation_card(2,inf,R),R==inf)). | |
| 347 | :- assert_must_succeed((total_relation_card(inf,0,R),R==0)). | |
| 348 | :- assert_must_succeed((total_relation_card(inf,1,R),R==1)). | |
| 349 | :- assert_must_succeed((total_relation_card(inf,2,R),R==inf)). | |
| 350 | :- assert_must_succeed((total_relation_card(inf,inf,R),R==inf)). | |
| 351 | :- assert_must_succeed((total_relation_card(2,2,R),R==9)). | |
| 352 | :- assert_must_succeed((total_relation_card(2,3,R),R==49)). | |
| 353 | ||
| 354 | :- block total_relation_card(-,?,?), total_relation_card(?,-,?). | |
| 355 | total_relation_card(0,_RanC,Card) :- !, Card=1. % empty set is a solution | |
| 356 | total_relation_card(_DomC,0,Card) :- !, Card=0. % no element to choose from in the range | |
| 357 | total_relation_card(inf,RanC,Card) :- !, (RanC=1 -> Card=1 ; Card=inf). | |
| 358 | total_relation_card(_,inf,Card) :- !, Card=inf. | |
| 359 | total_relation_card(DomC,RanC,Card) :- safe_pow2(RanC,SubRan), | |
| 360 | safe_minus(SubRan,1,S1), % as we have a total relation we cannot choose empty set | |
| 361 | safe_pown(S1,DomC,Card). | |
| 362 | ||
| 363 | ||
| 364 | ||
| 365 | :- assert_must_succeed((partial_bijection_card(2,inf,R),R==0)). | |
| 366 | :- assert_must_succeed((partial_bijection_card(inf,2,R),R==inf)). | |
| 367 | :- assert_must_succeed((partial_bijection_card(inf,inf,R),R==inf)). | |
| 368 | :- assert_must_succeed((partial_bijection_card(3,2,R),R==6)). | |
| 369 | :- assert_must_succeed((partial_bijection_card(2,2,R),R==2)). | |
| 370 | :- assert_must_succeed((partial_bijection_card(1,2,R),R==0)). | |
| 371 | :- assert_must_succeed((partial_bijection_card(2,1,R),R==2)). | |
| 372 | :- assert_must_succeed((partial_bijection_card(5,3,R),R==60)). | |
| 373 | :- assert_must_succeed((partial_bijection_card(6,4,R),R==360)). | |
| 374 | :- assert_must_succeed((partial_bijection_card(10,1,R),R==10)). | |
| 375 | :- assert_must_succeed((partial_bijection_card(10,2,R),R==90)). | |
| 376 | :- assert_must_succeed((partial_bijection_card(inf_overflow,5,R),R==inf_overflow)). | |
| 377 | :- assert_must_succeed((partial_bijection_card(inf_overflow,inf_overflow,R),R==inf_overflow)). | |
| 378 | :- assert_must_succeed((partial_bijection_card(inf_overflow,0,R),R==1)). | |
| 379 | :- assert_must_succeed((partial_bijection_card(inf_overflow,inf,R),R==0)). | |
| 380 | :- assert_must_succeed((partial_bijection_card(5,inf_overflow,R),R==0)). | |
| 381 | ||
| 382 | :- use_module(inf_arith,[infless/2]). | |
| 383 | ||
| 384 | :- block partial_bijection_card(-,?,?), partial_bijection_card(?,-,?). | |
| 385 | partial_bijection_card(_X,0,R) :- !, R=1. % the empty set is a solution | |
| 386 | partial_bijection_card(inf,X,R) :- !, | |
| 387 | print_inf_warning(X,'>+>>'), % we could have : INTEGER >+>> POW(INTEGER); TO DO: use omega(inf) for POW ? | |
| 388 | R=inf. % we know _X>0 | |
| 389 | partial_bijection_card(DomCard,RanCard,Res) :- | |
| 390 | infless(DomCard,RanCard),!,Res=0. % -> no way to cover all elements in range | |
| 391 | partial_bijection_card(inf_overflow,_,R) :- !, R=inf_overflow. % choose_nk cannot deal with inf_overflow | |
| 392 | partial_bijection_card(DomCard,RanCard,XCard) :- | |
| 393 | choose_nk(DomCard,RanCard,CNK), % ways to choose domain of size RanCard | |
| 394 | blocking_factorial(RanCard,Card), | |
| 395 | safe_mul(CNK,Card,XCard). | |
| 396 | ||
| 397 | print_inf_warning(inf,FUN) :- !, | |
| 398 | add_message(kernel_card_arithmetic,'Assuming that a function of this kind exists between two infinite sets: ',FUN). | |
| 399 | print_inf_warning(_,_). | |
| 400 | ||
| 401 | ||
| 402 | ||
| 403 | ||
| 404 | :- assert_must_succeed((partial_surjection_card(0,2,R),R==0)). | |
| 405 | :- assert_must_succeed((partial_surjection_card(0,0,R),R==1)). | |
| 406 | :- assert_must_succeed((partial_surjection_card(2,0,R),R==1)). | |
| 407 | :- assert_must_succeed((partial_surjection_card(1,1,R),R==1)). | |
| 408 | :- assert_must_succeed((partial_surjection_card(inf,2,R),R==inf)). | |
| 409 | :- assert_must_succeed((partial_surjection_card(2,inf,R),R==0)). | |
| 410 | :- assert_must_succeed((partial_surjection_card(inf,inf,R),R==inf)). | |
| 411 | :- assert_must_succeed((partial_surjection_card(inf_overflow,inf_overflow,R),R==inf_overflow)). | |
| 412 | :- assert_must_succeed((partial_surjection_card(inf_overflow,10,R),R==inf_overflow)). | |
| 413 | :- assert_must_succeed((partial_surjection_card(3,2,R),R==12)). | |
| 414 | :- block partial_surjection_card(-,?,?), partial_surjection_card(?,-,?). | |
| 415 | partial_surjection_card(_X,0,R) :- !, R=1. % the empty set is a solution | |
| 416 | partial_surjection_card(inf,X,R) :- !,print_inf_warning(X,'+->>'),R=inf. % we know _X>0 | |
| 417 | partial_surjection_card(DomCard,RanCard,R) :- | |
| 418 | infless(DomCard,RanCard), !, R=0. % -> no way to cover all elements in range | |
| 419 | partial_surjection_card(inf_overflow,_,R) :- !, R=inf_overflow. | |
| 420 | partial_surjection_card(A,B,Res) :- surj_card(A,B,1,Res). % 1 because we can choose to leave the function undefined | |
| 421 | ||
| 422 | :- assert_must_succeed((total_surjection_card(0,2,R),R==0)). | |
| 423 | :- assert_must_succeed((total_surjection_card(0,0,R),R==1)). | |
| 424 | :- assert_must_succeed((total_surjection_card(2,0,R),R==0)). | |
| 425 | :- assert_must_succeed((total_surjection_card(inf,0,R),R==0)). | |
| 426 | :- assert_must_succeed((total_surjection_card(inf,1,R),R==1)). | |
| 427 | :- assert_must_succeed((total_surjection_card(inf,2,R),R==inf)). | |
| 428 | :- assert_must_succeed((total_surjection_card(2,inf,R),R==0)). | |
| 429 | :- assert_must_succeed((total_surjection_card(0,inf,R),R==0)). | |
| 430 | :- assert_must_succeed((total_surjection_card(inf,inf,R),R==inf)). | |
| 431 | :- assert_must_succeed((total_surjection_card(inf,inf_overflow,R),R==inf)). | |
| 432 | :- assert_must_succeed((total_surjection_card(inf_overflow,inf_overflow,R),R==inf_overflow)). | |
| 433 | :- assert_must_succeed((total_surjection_card(inf_overflow,100,R),R==inf_overflow)). | |
| 434 | :- assert_must_succeed((total_surjection_card(inf_overflow,inf,R),R==0)). | |
| 435 | :- assert_must_succeed((total_surjection_card(3,2,R),R==6)). | |
| 436 | :- block total_surjection_card(-,?,?), total_surjection_card(?,-,?). | |
| 437 | total_surjection_card(0,Range,R) :- !,(Range=0 -> R=1 % the empty set is a solution | |
| 438 | ; R=0). % no solution | |
| 439 | total_surjection_card(_X,0,R) :- !, R=0. % as _X>0 there is no way to construct a total function | |
| 440 | total_surjection_card(_X,1,R) :- !, R=1. % we only have one choice for every element of _X | |
| 441 | total_surjection_card(inf,X,R) :- !,print_inf_warning(X,'-->>'),R=inf. % we know _X>0 | |
| 442 | total_surjection_card(DomCard,RanCard,R) :- | |
| 443 | infless(DomCard,RanCard), !, R=0. % -> no way to cover all elements in range, applicable if RanCard=inf | |
| 444 | total_surjection_card(inf_overflow,_,R) :- !, R=inf_overflow. | |
| 445 | total_surjection_card(A,B,Res) :- surj_card(A,B,0,Res). | |
| 446 | ||
| 447 | % surj_card(N,K,Extra,Res): number of functions from a set of size N to a set of size K+Extra | |
| 448 | % which cover all K elements (Extra=0: total surjections; Extra=1: partial surjections, | |
| 449 | % the extra element stands for being able to leave the function undefined) | |
| 450 | % computed by inclusion-exclusion: sum_{i=0}^{K} (-1)^i * C(K,i) * (K+Extra-i)^N | |
| 451 | % (a previous recursive version took time exponential in K, e.g., about 8 seconds for N=28, K=14) | |
| 452 | :- assert_must_succeed((surj_card(3,2,0,R),R==6)). | |
| 453 | :- assert_must_succeed((surj_card(3,2,1,R),R==12)). | |
| 454 | :- assert_must_succeed((surj_card(4,2,0,R),R==14)). | |
| 455 | :- assert_must_succeed((surj_card(5,3,0,R),R==150)). | |
| 456 | :- assert_must_succeed((surj_card(4,4,0,R),R==24)). | |
| 457 | :- assert_must_succeed((surj_card(2,3,0,R),R==0)). | |
| 458 | :- assert_must_succeed((surj_card(2,3,1,R),R==0)). | |
| 459 | :- assert_must_succeed((surj_card(0,0,0,R),R==1)). | |
| 460 | :- assert_must_succeed((surj_card(5,1,1,R),R==31)). | |
| 461 | :- assert_must_succeed((surj_card(1023,2,0,R),R is 2^1023-2)). | |
| 462 | :- assert_must_succeed((surj_card(1024,2,0,R),R==inf_overflow)). | |
| 463 | surj_card(N,K,_,Res) :- N<K,!, Res=0. % no way to cover all K elements | |
| 464 | surj_card(N,_,_,Res) :- N>1023,!, | |
| 465 | % we have K>=1 and N>=K; the result is at least 2^N-2 (and at least N! if K=N), which is larger than 2^1023 (cf. safe_pow2) | |
| 466 | set_inf_due_to_overflow(Res). | |
| 467 | surj_card(N,K,Extra,Res) :- surj_sum(0,K,N,Extra,1,0,Res). | |
| 468 | ||
| 469 | % Binom = C(K,I), Acc is the sum of the terms for 0..I-1 | |
| 470 | surj_sum(I,K,_,_,_,Acc,Res) :- I>K,!, Res=Acc. | |
| 471 | surj_sum(I,K,N,Extra,Binom,Acc,Res) :- | |
| 472 | Term is Binom * (K+Extra-I)^N, | |
| 473 | (I mod 2 =:= 0 -> Acc1 is Acc+Term ; Acc1 is Acc-Term), | |
| 474 | I1 is I+1, | |
| 475 | Binom1 is (Binom*(K-I)) // I1, | |
| 476 | surj_sum(I1,K,N,Extra,Binom1,Acc1,Res). | |
| 477 | ||
| 478 | ||
| 479 | ||
| 480 | :- assert_must_succeed((partial_injection_card(1,1,R),R==2)). | |
| 481 | :- assert_must_succeed((partial_injection_card(1,2,R),R==3)). | |
| 482 | :- assert_must_succeed((partial_injection_card(1,100,R),R==101)). | |
| 483 | :- assert_must_succeed((partial_injection_card(0,100,R),R==1)). | |
| 484 | :- assert_must_succeed((partial_injection_card(1,inf,R),R==inf)). | |
| 485 | :- assert_must_succeed((partial_injection_card(0,inf,R),R==1)). | |
| 486 | :- assert_must_succeed((partial_injection_card(5,0,R),R==1)). | |
| 487 | :- assert_must_succeed((partial_injection_card(inf,0,R),R==1)). | |
| 488 | :- assert_must_succeed((partial_injection_card(inf,1,R),R==inf)). | |
| 489 | :- assert_must_succeed((partial_injection_card(5,1,R),R==6)). | |
| 490 | ||
| 491 | :- block partial_injection_card(-,?,?), partial_injection_card(?,-,?). | |
| 492 | partial_injection_card(Dom,Ran,R) :- (Dom=0;Ran=0),!, R=1. % only one solution: empty function | |
| 493 | partial_injection_card(Dom,1,R) :- !, | |
| 494 | (Dom=inf -> R=inf ; Dom=inf_overflow -> R=inf_overflow | |
| 495 | ; R is Dom+1). % empty function + for every domain element one function | |
| 496 | partial_injection_card(inf,inf,Card) :- !, print_inf_warning(inf,'>+>'), Card=inf. | |
| 497 | partial_injection_card(inf_overflow,inf_overflow,Card) :- !, print_inf_overflow_warning('>+>'), Card=inf_overflow. | |
| 498 | partial_injection_card(Dom,Ran,R) :- (Dom=inf;Ran=inf),!, R=inf. | |
| 499 | partial_injection_card(Dom,Ran,R) :- (Dom=inf_overflow;Ran=inf_overflow),!, R=inf_overflow. | |
| 500 | partial_injection_card(Dom,Ran,R) :- Dom>1000,Ran>1000,!, set_inf_due_to_overflow(R). % TO DO: refine this | |
| 501 | % would give a result larger than blocking_factorial(1000) | |
| 502 | partial_injection_card(DCard,RCard,Card) :- | |
| 503 | pinj_card(1,DCard,RCard,1,Card). | |
| 504 | ||
| 505 | % TO DO: can we compute this directly rather than using recursion ? | |
| 506 | pinj_card(I,M,N,Acc,Res) :- (I>M ; I>N ; Acc=inf),!, Res=Acc. | |
| 507 | pinj_card(I,M,N,Acc,Res) :- | |
| 508 | choose_nk(M,I,MI), % ways to choose domain of size I | |
| 509 | blocking_factorial_k(N,I,NI), % ways to map the domain of size I to N | |
| 510 | safe_mul(MI,NI,MINI), safe_add_card(MINI,Acc,NewAcc), | |
| 511 | I1 is I+1, pinj_card(I1,M,N,NewAcc,Res). | |
| 512 | ||
| 513 | :- assert_must_succeed((total_bijection_card(3,3,R),R==6)). | |
| 514 | :- assert_must_succeed((total_bijection_card(0,0,R),R==1)). | |
| 515 | :- assert_must_succeed((total_bijection_card(3,2,R),R==0)). | |
| 516 | :- assert_must_succeed((total_bijection_card(inf,5,R),R==0)). | |
| 517 | :- assert_must_succeed((total_bijection_card(2000,2000,R),R==inf_overflow)). | |
| 518 | % used by custom_explicit_sets:is_a_relation for the cardinality of >->> | |
| 519 | :- block total_bijection_card(-,?,?), total_bijection_card(?,-,?). | |
| 520 | total_bijection_card(inf,inf,Card) :- !, print_inf_warning(inf,'>->>'), Card=inf. | |
| 521 | total_bijection_card(inf_overflow,inf_overflow,Card) :- !, print_inf_overflow_warning('>->>'), Card=inf_overflow. | |
| 522 | total_bijection_card(DCard,RCard,Card) :- DCard \= RCard,!,Card=0. | |
| 523 | total_bijection_card(RCard,RCard,Card) :- blocking_factorial(RCard,Card). | |
| 524 | ||
| 525 | ||
| 526 | print_inf_overflow_warning(FUN) :- !, | |
| 527 | add_message(kernel_card_arithmetic,'Assuming that a function of this kind exists between two very large sets: ',FUN). | |
| 528 | % TODO: probably produce enumeration_warning or virtual time-out or similar |