1

I am new to prolog. I have learned that ,though it is a declarative language, prolog can be used as a general purpose programming language, just like C. So, whatever problems you can solve in C, you can solve in prolog as well, even though its run-time may not be as good. Since there are no pointers in prolog (as far as i know), I am wondering if i can write an equivalent program in prolog for the following code written in C :-

#include <stdio.h>

int main()
{
    int a = 5;
    int *p;
    p = &a;
    printf("The address of a is %d.", p);
    return 0;
}
Guy Coder
  • 24,501
  • 8
  • 71
  • 136
  • 1
    No idea about prolog but since code shown is in `c` so want to inform that specifier `%d` is not correct for pointer. You should use `%p` or `%u`. – Dayal rai Aug 30 '13 at 12:36
  • 1
    Where did you learn that Prolog can be used "just like C"? – lurker Aug 30 '13 at 12:38
  • Thanks Dayal rai for the info – user2118622 Aug 30 '13 at 12:43
  • 1
    @Dayalrai `%u`, expecting an `unsigned int`, is just as incorrect as `%d`, expecting an `int`. `%p` is better but technically incorrect because it expects a `void*`. The correct idiom is `printf("%p", (void*)p)`. The details are in clause 6.5.2.2 of the C99 standard (*default argument promotions*). – Pascal Cuoq Aug 30 '13 at 12:45
  • @mbratch If prolog is not like C, is it possible to write in any way similar code in prolog? – user2118622 Aug 30 '13 at 12:46
  • 1
    Prolog does not support extracting the memory address of a variable. So there's no way that I know of to write a program like the one that you show. You can, however, mimick some procedural aspects of other programs, like a `for` or `while` loop. – lurker Aug 30 '13 at 12:47
  • @PascalCuoq I do not want to hijack this thread otherwise my question would be _why specific typcast in printf(that too void*) when specifier is there?_ Anyways Thanks for the link to standard i will search for the answer there. – Dayal rai Aug 30 '13 at 12:53
  • @Dayalrai Your question would be a valid one, but it has already been asked. The answer is that for `printf()` or any variadic function, the compiler does not look at the format to interpret the **type** of the arguments. http://stackoverflow.com/questions/12830052/wrong-format-specifiers-in-scanf-or-printf – Pascal Cuoq Aug 30 '13 at 12:56
  • "...prolog can be used as a general purpose programming language, just like C.", probably what you heard was an oversimplification of the fact that both Prolog and C are Turing-complete languages. This roughly means that they can solve the same computational problems. This doesn't absolutely mean that the way they solve them is similar or that they support the same programming paradigms, as you seem to imply. – LorenzoDonati4Ukraine-OnStrike Oct 30 '13 at 00:28

3 Answers3

3

You're trying to drive in a nail using a screwdriver, to use a popular analogy. Prolog is not C and solving problems in Prolog is fundamentally different from solving them in C.

Printing the value of a variable is easy to do, for example:

main :-
    X = 5,
    io:format("X = ~w~n", [X]).

but you can't get the address of X like you can in C. And why would you want to? The address could be different next time since Prolog has automatic garbage collection.

If you want to learn Prolog, forget about trying to write Prolog programs which look like C programs, and try to solve actual problems instead. You could try out the Project Euler series of problems, for example.

false
  • 10,264
  • 13
  • 101
  • 209
JesperE
  • 63,317
  • 21
  • 138
  • 197
2

Apart from the comments and the existing answer, here is more:

Ask yourself: what is the use of the C program that you have shown? What problem does it solve? I can't answer this question, and I suspect you can't answer it either. In isolation, this program has no useful application whatsoever! So despite C being a general purpose programming language, you can write programs without any purpose, general or domain-specific.

The same, of course, is true of Prolog.

To pointers in particular: they are a very thin abstraction over absolute memory addresses. You can use (and abuse) pointers in many ways, and, if your algorithms are correct for the problem you are currently solving, the compiler can generate very efficient machine code. The same, however, is true of Prolog. The paradigms, however, will be very different.

In summary, you have managed to write a question so devoid of meaning that you provoked me to answer it without any code.

P.S. Or you have just trolled us with moderate success.

1

Well, since you tagged your question, I can show the code I used to exchange Qt GUI objects (just pointers, you know...) with the Prolog engine.

/** get back an object passed by pointer to Prolog */
template<typename Obj> Obj* pq_cast(T ptr) {
  return static_cast<Obj*>(static_cast<void*>(ptr));
}

to be used, for instance in swipl-win, where _read_f is really a C callback:

/** fill the buffer */
ssize_t Swipl_IO::_read_f(void *handle, char *buf, size_t bufsize) {
    auto e = pq_cast<Swipl_IO>(handle);
    return e->_read_(buf, bufsize);

swipl-win has found its way as the new console in SWI-Prolog.

m09
  • 7,490
  • 3
  • 31
  • 58
CapelliC
  • 59,646
  • 5
  • 47
  • 90
  • sorry for some reason while I was browsing this answer I downvoted without noticing, I've done a dummy edit to remove the downvote ;( – m09 Sep 02 '13 at 16:41
  • @Mog: thanks, effectively my answer isn't very good, cause the use case doesn't show the round trip that's made possible from use of pointers... – CapelliC Sep 02 '13 at 19:55
  • oh I didn't mean to say that your answer was bad, it was really a misclick from me ;) Cheers! – m09 Sep 02 '13 at 20:01