ciao-lang / ciao

Ciao is a modern Prolog implementation that builds up from a logic-based simple kernel designed to be portable, extensible, and modular.
https://ciao-lang.org
GNU Lesser General Public License v3.0
268 stars 20 forks source link

Error when trying to run the executable: ERROR: arithmetic:>/2, arg 1 . #38

Closed freeskaro closed 2 years ago

freeskaro commented 2 years ago

This is probably not an 'issue'. Simply, I'm new to Ciao and I can't seem to find the answer. My code works fine in interactive mode. When trying to run the executable I get the error:

    {ERROR: arithmetic:>/2, arg 1 - expected an arithmetically evaluable expression, found 3}
    ERROR: {Program ended with failure}

The code:

    #!/usr/bin/env ciao-shell
    % -*- mode: ciao; -*-

    main(N) :- facto(N,R),write(R),nl.

    facto(0,1).

    facto(N,R) :- N > 0, N1 is N-1, facto(N1,R1), R is R1*N.

my command for executing:

    ./facto.pl 3

Please help. This should be simple. If you could direct me to examples on how to run executables that would be helpful too.

jfmc commented 2 years ago

Hi! The problem here is that main/1 receives a list of atoms (not a unique number). You can use something like:

main([Na]) :- atom_codes(Na,Cs), number_codes(N,Cs), ... 

Does it fix the problem?

Atoms are non-numeric constants. You can distinguish between 3 as an atom and 3 as a number because one is reported as '3' while the other as 3.

Cheers,

freeskaro commented 2 years ago

Yes! That works. Thanks.

    #!/usr/bin/env ciao-shell
    % -*- mode: ciao; -*-

    main([Na]) :- atom_codes(Na,Cs), number_codes(N,Cs), facto(N,R), write(R), nl.

    facto(0,1).

    facto(N,R) :- N > 0, N1 is N-1, facto(N1,R1), R is R1*N.

But its just basic file I/O. It's pretty impossible to find any documentation on it or clear examples.

So Na is implicitly an atomic of N? How does Na become the first call of facto(N,R)?

jfmc commented 2 years ago

Indeed your example program is not doing any file I/O (the process arguments are passed as a list to the main/1 predicate). There is detailed documentation about main/0 and main/1 in the manual: https://ciao-lang.org/ciao/build/doc/ciao.html/ciaoc.html .

Ciao is a (more or less) superset of ISO Prolog. Any good Prolog book (e.g. The Art of Prolog) would be a good introduction to the language. If you are interested in some particular topic we will gladly point you some more examples.

freeskaro commented 2 years ago

I am not trying to be argumentative but I want to point out the link you sent me doesn't help one tiny bit in answering the question I had. Or at least its a needle in a haystack. I had indeed seen it the example of write_list and modelled my solution after it. My execute statement was correct and the file I already had worked in the interactive environment. All the other Ciao references have the same write_list example.

But my problem has numeric input and it is just impossible to divine that square brackets are needed for numbers read to separate out atomic or number. Reading in numbers from the screen is pretty common for the solution to be so obscure. Something like that is usually covered in chapter 2 of a programming language with many examples.

But I do very much appreciate your excellent technical knowledge and the effort to make timely replies. Now I am going to have lots of fun with Ciao! (I installed it on iSH on my cell).

And happy New Year!