Created
October 3, 2025 08:23
-
-
Save patham9/58b63d02717b034822f0bb92cced97fd to your computer and use it in GitHub Desktop.
Behavior of once(concurrent_and( vs. first_solution(
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| %Finding divisors | |
| 'find-divisor'(A, B, C) :- | |
| *(B, B, D), | |
| >(D, A, E), | |
| ( E==true | |
| -> C=A | |
| ; C=F, | |
| '%'(A, B, G), | |
| ==(0, G, H), | |
| ( H==true | |
| -> F=B | |
| ; F=I, | |
| +(B, 1, J), | |
| 'find-divisor'(A, J, I) | |
| ) | |
| ). | |
| %In order to check if a number is prime: | |
| 'prime?'(A, B) :- | |
| 'find-divisor'(A, 2, C), | |
| ==(A, C, B). | |
| %%% Arithmetic & Comparison: %%% | |
| '+'(A,B,R) :- R is A + B. | |
| '*'(A,B,R) :- R is A * B. | |
| '%'(A,B,R) :- R is A mod B. | |
| '>'(A,B,R) :- (A>B -> R=true ; R=false). | |
| '=='(A,B,R) :- (A==B -> R=true ; R=false). | |
| %%For test output: | |
| test(A,B,R) :- (A == B -> E = '✅' ; E = '❌'), | |
| format(string(R), "is ~w, should ~w. ~w ~n", [A, B, E]), | |
| writeln(R). | |
| %First approach: | |
| branch_to_goal(Out, (Goal,Res), (Goal, Out=Res)). | |
| maplist_firstsolution(A) :- | |
| maplist(branch_to_goal(B), | |
| [ ('prime?'(5353725700019, C), C), | |
| ('prime?'(5378181100003, D), D), | |
| ('prime?'(5421844300001, E), E), | |
| ('prime?'(5473443100001, F), F) | |
| ], | |
| G), | |
| first_solution(B, G, []), | |
| test(B, true, A). | |
| %Second approach: | |
| once_concurrentand(A) :- | |
| once(concurrent_and(member((B, C), | |
| [ ('prime?'(535372570000000063, D), D), | |
| ('prime?'(537818110000000001, E), E), | |
| ('prime?'(5421844300001, F), F), | |
| ('prime?'(547344310000000013, G), G) | |
| ]), | |
| (call(B), H=C))), | |
| test(H, true, A). | |
| %Invoke both: | |
| :- maplist_firstsolution(A), once_concurrentand(B). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment