exercism / prolog

Exercism exercises in Prolog.
https://exercism.org/tracks/prolog
MIT License
29 stars 38 forks source link

Sieve exercise test broken #336

Closed deosjr closed 4 months ago

deosjr commented 4 months ago

Hi, I have a solution for the newly released Sieve exercise that doesn't pass the tests on exercism.org but when I run it locally it passes just fine. I will paste the solution and test invocation below, so spoilers I suppose?

:- use_module(library(clpfd)).

:- dynamic([prime/1, composite/1]).

primes(Limit, Primes) :-
    Limit #>= 2,
    find_primes(2, Limit),
    findall(X, prime(X), Primes).

primes(Limit, []) :-
    Limit #< 2.

find_primes(P, Limit) :-
    P #> Limit.

find_primes(P, Limit) :-
    P #=< Limit,
    (
        (prime(P);composite(P))
    ->
        true
    ;
        assertz(prime(P)),
        assert_composite(P, P, Limit)
    ),
    NewP #= P+1,
    find_primes(NewP, Limit).

assert_composite(N, _, Limit) :-
    N #> Limit.
assert_composite(N, Incr, Limit) :-
    N #=< Limit,
    (
        (prime(N);composite(N))
    ->
        true
    ;
        assertz(composite(N))
    ),
    NewN #= N + Incr,
    assert_composite(NewN, Incr, Limit).

And invocation on local machine:

$ swipl --version
SWI-Prolog version 9.0.4 for x86_64-darwin
$ swipl -l test.pl -g run_tests,halt
% PL-Unit: sieve .
A TEST IS PENDING!

A TEST IS PENDING!

A TEST IS PENDING!

A TEST IS PENDING!
 passed 0.001 sec
% test passed

Compare with exercism editor test output:

% PL-Unit: sieve .
Warning: 
    [[ EXCEPTION while printing message url('/mnt/exercism-iteration/sieve_tests.plt':12)
       with arguments []:
       raised: type_error(text,url('/mnt/exercism-iteration/sieve_tests.plt':12))
    ]]
:
    PL-Unit: Test find_first_prime: Test succeeded with choicepoint
ERROR: 
    [[ EXCEPTION while printing message url('/mnt/exercism-iteration/sieve_tests.plt':15)
       with arguments []:
       raised: type_error(text,url('/mnt/exercism-iteration/sieve_tests.plt':15))
    ]]
:
    test find_primes_up_to_10: failed

ERROR: 
    [[ EXCEPTION while printing message url('/mnt/exercism-iteration/sieve_tests.plt':18)
       with arguments []:
       raised: type_error(text,url('/mnt/exercism-iteration/sieve_tests.plt':18))
    ]]
:
    test limit_is_prime: failed

ERROR: 
    [[ EXCEPTION while printing message url('/mnt/exercism-iteration/sieve_tests.plt':21)
       with arguments []:
       raised: type_error(text,url('/mnt/exercism-iteration/sieve_tests.plt':21))
    ]]
:
    test find_primes_up_to_1000: failed

 done
% 3 tests failed
% 2 tests passed
ERROR: -g run_tests,halt: false

Happy to help out with the Prolog track in general btw, just might need some pointers on how best to contribute.

github-actions[bot] commented 4 months ago

Hello. Thanks for opening an issue on Exercism 🙂

At Exercism we use our Community Forum, not GitHub issues, as the primary place for discussion. That allows maintainers and contributors from across Exercism's ecosystem to discuss your problems/ideas/suggestions without them having to subscribe to hundreds of repositories.

This issue will be automatically closed. Please use this link.%0D%0A%0D%0A:-%20dynamic(%5Bprime/1,%20composite/1%5D).%0D%0A%0D%0Aprimes(Limit,%20Primes)%20:-%0D%0A%20%20%20%20Limit%20#%3E=%202,%0D%0A%20%20%20%20find_primes(2,%20Limit),%0D%0A%20%20%20%20findall(X,%20prime(X),%20Primes).%0D%0A%0D%0Aprimes(Limit,%20%5B%5D)%20:-%0D%0A%20%20%20%20Limit%20#%3C%202.%0D%0A%0D%0Afind_primes(P,%20Limit)%20:-%0D%0A%20%20%20%20P%20#%3E%20Limit.%0D%0A%0D%0Afind_primes(P,%20Limit)%20:-%0D%0A%20%20%20%20P%20#=%3C%20Limit,%0D%0A%20%20%20%20(%0D%0A%20%20%20%20%20%20%20%20(prime(P);composite(P))%0D%0A%20%20%20%20-%3E%0D%0A%20%20%20%20%20%20%20%20true%0D%0A%20%20%20%20;%0D%0A%20%20%20%20%20%20%20%20assertz(prime(P)),%0D%0A%20%20%20%20%20%20%20%20assert_composite(P,%20P,%20Limit)%0D%0A%20%20%20%20),%0D%0A%20%20%20%20NewP%20#=%20P+1,%0D%0A%20%20%20%20find_primes(NewP,%20Limit).%0D%0A%0D%0Aassertcomposite(N,%20,%20Limit)%20:-%0D%0A%20%20%20%20N%20#%3E%20Limit.%0D%0Aassert_composite(N,%20Incr,%20Limit)%20:-%0D%0A%20%20%20%20N%20#=%3C%20Limit,%0D%0A%20%20%20%20(%0D%0A%20%20%20%20%20%20%20%20(prime(N);composite(N))%0D%0A%20%20%20%20-%3E%0D%0A%20%20%20%20%20%20%20%20true%0D%0A%20%20%20%20;%0D%0A%20%20%20%20%20%20%20%20assertz(composite(N))%0D%0A%20%20%20%20),%0D%0A%20%20%20%20NewN%20#=%20N%20+%20Incr,%0D%0A%20%20%20%20assert_composite(NewN,%20Incr,%20Limit).%0D%0A%60%60%60%0D%0AAnd%20invocation%20on%20local%20machine:%0D%0A%60%60%60%0D%0A$%20swipl%20--version%0D%0ASWI-Prolog%20version%209.0.4%20for%20x86_64-darwin%0D%0A$%20swipl%20-l%20test.pl%20-g%20run_tests,halt%0D%0A%25%20PL-Unit:%20sieve%20.%0D%0AA%20TEST%20IS%20PENDING!%0D%0A%0D%0AA%20TEST%20IS%20PENDING!%0D%0A%0D%0AA%20TEST%20IS%20PENDING!%0D%0A%0D%0AA%20TEST%20IS%20PENDING!%0D%0A%20passed%200.001%20sec%0D%0A%25%20test%20passed%0D%0A%60%60%60%0D%0ACompare%20with%20exercism%20editor%20test%20output:%0D%0A%60%60%60%0D%0A%25%20PL-Unit:%20sieve%20.%0D%0AWarning:%20%0D%0A%20%20%20%20%5B%5B%20EXCEPTION%20while%20printing%20message%20url('/mnt/exercism-iteration/sieve_tests.plt':12)%0D%0A%20%20%20%20%20%20%20with%20arguments%20%5B%5D:%0D%0A%20%20%20%20%20%20%20raised:%20type_error(text,url('/mnt/exercism-iteration/sieve_tests.plt':12))%0D%0A%20%20%20%20%5D%5D%0D%0A:%0D%0A%09PL-Unit:%20Test%20find_first_prime:%20Test%20succeeded%20with%20choicepoint%0D%0AERROR:%20%0D%0A%20%20%20%20%5B%5B%20EXCEPTION%20while%20printing%20message%20url('/mnt/exercism-iteration/sieve_tests.plt':15)%0D%0A%20%20%20%20%20%20%20with%20arguments%20%5B%5D:%0D%0A%20%20%20%20%20%20%20raised:%20type_error(text,url('/mnt/exercism-iteration/sieve_tests.plt':15))%0D%0A%20%20%20%20%5D%5D%0D%0A:%0D%0A%09test%20find_primes_up_to_10:%20failed%0D%0A%0D%0AERROR:%20%0D%0A%20%20%20%20%5B%5B%20EXCEPTION%20while%20printing%20message%20url('/mnt/exercism-iteration/sieve_tests.plt':18)%0D%0A%20%20%20%20%20%20%20with%20arguments%20%5B%5D:%0D%0A%20%20%20%20%20%20%20raised:%20type_error(text,url('/mnt/exercism-iteration/sieve_tests.plt':18))%0D%0A%20%20%20%20%5D%5D%0D%0A:%0D%0A%09test%20limit_is_prime:%20failed%0D%0A%0D%0AERROR:%20%0D%0A%20%20%20%20%5B%5B%20EXCEPTION%20while%20printing%20message%20url('/mnt/exercism-iteration/sieve_tests.plt':21)%0D%0A%20%20%20%20%20%20%20with%20arguments%20%5B%5D:%0D%0A%20%20%20%20%20%20%20raised:%20type_error(text,url('/mnt/exercism-iteration/sieve_tests.plt':21))%0D%0A%20%20%20%20%5D%5D%0D%0A:%0D%0A%09test%20find_primes_up_to_1000:%20failed%0D%0A%0D%0A%20done%0D%0A%25%203%20tests%20failed%0D%0A%25%202%20tests%20passed%0D%0AERROR:%20-g%20run_tests,halt:%20false%0D%0A%60%60%60%0D%0AHappy%20to%20help%20out%20with%20the%20Prolog%20track%20in%20general%20btw,%20just%20might%20need%20some%20pointers%20on%20how%20best%20to%20contribute.&category=prolog ) to copy your GitHub Issue into a new topic on the forum, where we look forward to chatting with you!

If you're interested in learning more about this auto-responder, please read this blog post.