2

I am trying to write a Prolog program such that given a list of numbers like [1, 2, 3], it will convert them into a list of words representing those numbers ['one', 'two', 'three'].

My code so far:

totext(0, 'zero').
totext(1, 'one').
totext(2, 'two').
totext(3, 'three').
totext(4, 'four').
totext(5, 'five').
totext(6, 'six').
totext(7, 'seven').
totext(8, 'eight').
totext(9, 'nine').

translate([], []).
translate([Head|Rest], [TranslatedHead|TranslatedRest]) :-
   totext(Head, TranslatedHead),
   translate(Rest, TranslatedRest).

When I load up gprolog and consult the file, if I do:

translate([], X).

I correctly get:

X = []
yes

But when I do

translate([1,2], X).

I get:

uncaught exception: error(existence_error(procedure,totext/0),translate/0)

I am very new to Prolog (this is my first Prolog program), and I have no idea what is going wrong here. Any ideas? Thanks.

false
  • 10,264
  • 13
  • 101
  • 209
  • 3
    When I load your code into `gprolog` and do `translate([1,2], X).` I get `X = [one, two]`. I'll bet you put a space after `translate`. Note the `translate/0` in the error, meaning "no arguments" for `translate`. Did you type `translate ([1,,2], X).`? That won't work. An aside, you can use `maplist` for this kind of list processing: `maplist(totext, Numbers, Names)`. Try `maplist(totext, [1,2,3], Names)`. – lurker Feb 22 '19 at 19:08
  • Is this the full program? Looks like something is missing? – Willem Van Onsem Feb 22 '19 at 19:09
  • @WillemVanOnsem nothing is missing. It works as-is. – lurker Feb 22 '19 at 19:09
  • This is the full program, I have it saved as "test.pl". I open gprolog then do consult('test.pl'). and try it and it doesn't work. –  Feb 22 '19 at 19:09
  • @lurker: what I mean is that, since we do not get the error, the two look not in sync: the program that produces the error, and the one shared here. – Willem Van Onsem Feb 22 '19 at 19:10
  • @Kos read my updated first comment. – lurker Feb 22 '19 at 19:11
  • @WillemVanOnsem I think he's typing his query with a space after `translate` (not shown in his question): `translate ([1,2], X).` would definitely result in Prolog saying that `translate/0` is not defined. – lurker Feb 22 '19 at 19:14
  • This is exactly what I'm typing, as in the question, with no spaces: translate([1,2], X). How are you loading the file? Do you save it as a .pl and consult it from gprolog? –  Feb 22 '19 at 19:15
  • @Kos,I copied your code from your question into a file (`foo.pl`) and I consulted it in `gprolog`. I ran the query and it worked. So what you have in this question somehow does not match what you're running. The error message you gave (assuming it's still the error you are getting) indicates that, somewhere, you have `translate (...)` (space after `translate` - a call to `translate` with no arguments - that's what `translate/0` means). Check your code for any case where you may have a space before the left parenthesis for arguments. – lurker Feb 22 '19 at 19:16
  • What version of gprolog are you running? I'm on 1.4.5. I am running the code from the question and the correct query without spaces, yet it is not working for me. It must be gprolog itself. –  Feb 22 '19 at 19:19
  • I'm running 1.4.4. – lurker Feb 22 '19 at 19:21
  • @Kos hah! You're right! I just installed 1.4.5 and I get the error! Seems like a bug in 1.4.5. I tried renaming `translate` just to make sure there wasn't some odd name collision with a new library or something, but it always fails. – lurker Feb 22 '19 at 19:25

1 Answers1

2

Your code is corrected but your build of GNU Prolog is broken. Recompiling GNU Prolog from sources should fix it. The common symptom of a broken GNU Prolog build is predicate existence errors where the predicates, always reported as having arity zero, don't usually exist in the code being called.

P.S. The broken GNU Prolog installations seem to occur usually when using Ubuntu. Can you confirm that's also your case?

Paulo Moura
  • 18,373
  • 3
  • 23
  • 33
  • 1
    Yep I'm running Ubuntu. I got gprolog by doing sudo apt install gprolog. I will try to get it some other way –  Feb 22 '19 at 19:24
  • 1
    Same here. I'm on Ubuntu and had done a fresh build from source. – lurker Feb 22 '19 at 19:34
  • 1
    Rebuilding gprolog 1.4.5 from source on Ubuntu fixed this issue and the code works. Thanks! –  Feb 22 '19 at 19:40