Reputation: 3407
I am writing a parser for SPARQL (Semantic Web query language) using DCG. I want to replace SPARQL variable names with Prolog variables. How would I go about this?
I can generate new variables using length([NewVar], 1)
, but I cannot keep track of existing assignments by simply using a list of name-variable pairs. A member/2
operation on the list will return a new variable, not the one stored in the list.
Is there an easy way for naming variables in Prolog, e.g., '$VAR(Name)'
?
Upvotes: 6
Views: 1055
Reputation:
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
But you might get problems if you interleave write/1 with member/2, since a variable might change its identity, i.e. the write symbol in the following circumstances:
Same problem with (@<)/2. One way out is to use attribute variables, which at least puts an end to aliasing, since attribute variables are usually unified last,
so in the above example if Y is an attribute variable and V is an ordinary variable you would never see the memloc of V after calling member/2.
Further you can also mitigate the problem by using ISO core standard variable_names/1 write option, to write out a variablified term. The variable_names/1 write option is immune to garbage collection or aliasing.
Bye
Upvotes: 2