How to replace atoms sequentially with variables in Prolog?

I am writing a parser for SPARQL (semantic web query language) using DCG. I want to replace SPARQL variable names with Prolog variables. How can i do this?

I can generate new variables with length([NewVar], 1), but I can’t track existing assignments just by using a list of variable pairs. The operation member/2in the list will return a new variable, not the one stored in the list.

Is there an easy way to name variables in Prolog, for example '$VAR(Name)'?

+4
source share
2 answers

member / 2 will do what you want. Here is an example:

Welcome to SWI-Prolog (Multi-threaded, 64 bits, Version 7.3.25)
Copyright (c) 1990-2016 University of Amsterdam, VU Amsterdam

L=[a-X,b-Y,c-Z], member(b-V,L).
L = [a-X, b-V, c-Z],
Y = V 

, /1 /2, , .. :

  • - , _G <memloc>
  • - , memloc of V
    memloc Y

(@<)/2. - , , , , ,

, Y , V memloc of V /2.

Bye

+1

, .

atoms_to_vars(List,Output) :-
    atoms_to_vars(List,_,Output).
atoms_to_vars([],_,[]). 
atoms_to_vars([A1|List],Dict,[A2|Output]) :-
    (atom(A1),member(A1:A2,Dict);
    is_list(A1),atoms_to_vars(A1,Dict,A2);
    A1=A2),
    atoms_to_vars(List,Dict,Output).

SWI-Prolog:

?- atoms_to_vars(['a',1,['b','a']],Output).
Output = [_8828, 1, [_8858, _8828]] 
+1

All Articles