0

Basically I am trying to simulate C pointer dereference using gnu-prolog.

Here is the code:

Prolog

:-foreign(fun(+integer,-integer)).

% p = &b;
testfun(Var, Val) :- fun(Val, Var).

main :-
A is 1,
testfun(A, P),
write(P),

C:

#include <gprolog.h>
#include <string.h>

PlBool fun(int ptr, int* res){
    *res = &ptr;                  // this is wrong
    if(res==NULL){
      return PL_FALSE;
    }else{
      return PL_TRUE;
    }
}

So basically it is wrong, because ptr is just a temp variable on the stack, and its memory will be deallocated after the calling fun.

So my question is, is it possible to get the variable's memory addresses in gnu prolog (For example, in this case, it is the address of A, not the address of ptr)?

lllllllllllll
  • 8,519
  • 9
  • 45
  • 80
  • I think if it's strictly an input argument, it's passed by value and you can't get its pointer from within the function. – lurker Apr 13 '14 at 01:32
  • @lurker But can we get the memory address of "A" by other ways? – lllllllllllll Apr 13 '14 at 01:38
  • 1
    Why do you want the address of `A`? I don't think Prolog gives access to its internal addresses, particularly through a C function call. Even in the case where you have an output argument like `int *res`, the GNU guide says, *...the integer stored at this location is unified with Arg.* so it doesn't appear to be a direct pointer to the variable Prolog maintains internally. – lurker Apr 13 '14 at 01:46
  • @lurker Well....basically I want to use A simulate variables in C, and I have to deal with pointer issues.. – lllllllllllll Apr 13 '14 at 02:20
  • For example, I have P and A, and I want to use P to represent a pointer in C language, so I have to find a way to make P store the memory address of A. – lllllllllllll Apr 13 '14 at 02:21
  • @lurker, if it is not possible, then I guess I have to use a map or something to simulate the memory reference relation... – lllllllllllll Apr 13 '14 at 02:22
  • I see, yes that may be the right approach. – lurker Apr 13 '14 at 02:35
  • @lurker Well currently the problem is that for trivial cases, it is relatively easy , but some cases like use a pointer with (pointer++) to iter a array the pointer pointed to, then it seems undoable... – lllllllllllll Apr 15 '14 at 22:08
  • What is it you are trying to achieve overall? For simulating a CPU or a computational language, I wouldn't consider Prolog a language of choice for that job unless it somehow has a lot of choice-based decision heuristics involved. Anything is doable probably, but it could be very innefficient. – lurker Apr 15 '14 at 22:16
  • By "transfer" do you mean convert a C program automatically into Prolog? Does your boss know Prolog? How complete does this conversion of the C language need to be? This could take a very long time. And since Prolog is ill-suited for an imperative programming task of this magnitude, it will be arduous and the results will be inefficient. – lurker Apr 15 '14 at 22:23
  • @lurker Yes, we tried to make it automatically... I think he know Prolog somehow... Well it could be arduous and inefficient, but we still could get some benefit from this transform. – lllllllllllll Apr 15 '14 at 22:26
  • @lurker As for the "complete", we currently just target on some simple GNU coreutils code. – lllllllllllll Apr 15 '14 at 22:26
  • Hard work always leads to some kind of learning. :) You're going from a language that gives your close connection to the hardware into a language that's much more highly abstracted and doesn't make that connection. There are no pointers or addresses of variables, etc, in Prolog. So that leads down the path of simulating the entire architecture of addresses and then have variables that can hold those address, and some kind of structure that represents memory locations (e.g., a list of numbers perhaps). Fun. :) – lurker Apr 15 '14 at 22:31
  • @lurker Yes, Thank you for your kind advice! As long as I can learn something from the whole process, it is meaningful:) – lllllllllllll Apr 15 '14 at 22:37

1 Answers1

2

Gnu Prolog is pretty easy to extend by writing C routines and linking them into an executable. but if you are trying to "simulate the memory reference relation", then I'm doubtful that hacking in an actual memory address function would be useful.

Instead, as @lurker suggests, you likely want to "simulate" a memory/computer architecture and then some C-like language that "executes" on that. It sounds a little ambitious, but the simulation of a CPU in Prolog has been asked about already, and more recently here asked about by you, with Answer by lurker.

Community
  • 1
  • 1
hardmath
  • 8,753
  • 2
  • 37
  • 65
  • Hi hardmath, thank you for your answer! Because currently the only issue is that I don't know the memory address of Prolog "variables", so I think I can just maintain a map of Prolog "pointer" and "variables" them pointed... It seems to be able to solve the problem.. Am I right? – lllllllllllll Apr 13 '14 at 17:15
  • To my humble opinion, to "simulate" a memory/computer architecture could be overkilled.. – lllllllllllll Apr 13 '14 at 17:15
  • 1
    Your ability to know the memory address of Prolog variables is subject to some caveats. As you are doubtless aware, all Prolog variables exist within a stackframe that is subject to reuse. While you can call a C function that returns the "address" of a Prolog variable as a numeric value, not only can that address become obsolete, even before that I fail to understand what you could use it for in the Prolog code of your program. Pointers are useful in C in large part because of their role in recursion, and Prolog handles recursion without a need for pointers. – hardmath Apr 13 '14 at 17:23
  • 1
    If the goal is just to say, I got the address of a Prolog variable, take a look at the reference I gave for linking Prolog and C. I've used this, and it was not at all difficult (given a good background in both languages). – hardmath Apr 13 '14 at 17:24