Home > Archive > Prolog > June 2005 > problem with anonymous variables
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
problem with anonymous variables
|
|
| ouyang.jie@gmail.com 2005-05-27, 8:57 pm |
| hi, i have a problem with counting non-anonymous variables in a list of
variables. the straitforward way i come up with is to remove all
anonymous variables from the list and then count the length of the
resulted list. Anybody has a better idea or how to remove anonymous
variables? thx in advance.
| |
| Bart Demoen 2005-05-27, 8:57 pm |
| ouyang.jie@gmail.com wrote:
> hi, i have a problem with counting non-anonymous variables in a list of
> variables.
What is an "anonymous" variable in a list ?
Is it a variable that occurs only once in the list ?
Cheers
Bart Demoen
| |
| ouyang.jie@gmail.com 2005-05-27, 8:57 pm |
| an anonymous variable is representated by an underscore "_". for
instance , f(_,A,_) has 2 anonymous variables. given a list [_,A,B,_],
i want to have the list [A,B] returned or 2 as the number of
non-anonymous variables returned. any idea?
| |
| Brian Hulley 2005-05-27, 8:57 pm |
|
ouyang.jie@gmail.com wrote:
> an anonymous variable is representated by an underscore "_". for
> instance , f(_,A,_) has 2 anonymous variables. given a list [_,A,B,_],
> i want to have the list [A,B] returned or 2 as the number of
> non-anonymous variables returned. any idea?
As far as Prolog terms are concerned there is no difference between
[_,A,B,_] and [X,Y,Z,W] since in all cases you have 4 variables all
distinct from each other.
Ie the anonymous variable is a convenience only (so you don't have to
think up names for variables you don't care about and so that you can
get the singleton-variable warning when a non-anonymous variable is
only used once), and although implementations can make use of the fact
that an anonymous variable has been used to improve performance, the
language itself has no way to distinguish an anonymous variable from
any other variable.
| |
| Anders Lindén 2005-05-29, 3:59 am |
| A list doesnt contain variables, only constants and functors.
When you unify a list with a predicate term, that predicate can refer to those
constants and functors in an anonymous way, and _that_ is by the use of variables.
if you do this query: pred([1,2,3],c)
that will unify with the predicate
pred([A|B],c) :- foo(A)
so that foo(1) will remain in the resolvent. Note that the variables A and B are only having the life length of the unification
process.
The same query would also match this predicate:
pred([_|_],c).
But here we see that the predicate doesnt save the unification results in variables like A and B.
The unification will result in a fact, because we doesnt demand that a foo/1 predicate must be true
as in the example before.
/Anders
<ouyang.jie@gmail.com> wrote in message news:1117218983.597268.155000@g44g2000cwa.googlegroups.com...
> hi, i have a problem with counting non-anonymous variables in a list of
> variables. the straitforward way i come up with is to remove all
> anonymous variables from the list and then count the length of the
> resulted list. Anybody has a better idea or how to remove anonymous
> variables? thx in advance.
>
| |
| Torkel Franzen 2005-05-29, 3:59 am |
| "Anders Lindén" <xxxx@xxx.xx> writes:
> A list doesnt contain variables, only constants and functors.
The Herbrand universe doesn't contain variables, but lists, like
other Prolog structures, can be incomplete and contain unbound
variables.
| |
| Anders Lindén 2005-05-29, 3:57 pm |
| yeah, thats actually right, I understood what he meant after I had
replied to his posting :)
Sorry!
/Anders
"Torkel Franzen" <torkel@sm.luth.se> wrote in message news:vcbmzqe52zx.fsf@beta19.sm.ltu.se...
> "Anders Lindén" <xxxx@xxx.xx> writes:
>
>
> The Herbrand universe doesn't contain variables, but lists, like
> other Prolog structures, can be incomplete and contain unbound
> variables.
| |
| Anders Lindén 2005-05-29, 3:57 pm |
| Ok, here is an attempt
shrink([],[]).
shrink([A|B],C):- var(A),!, shrink(B,C).
shrink([A|B],[A|C]):- nonvar(A), shrink(B,C).
| ?- shrink([4,5,B,d(r,e)],C).
C = [4,5,d(r,e)] ? ;
no
| |
| ouyang.jie@gmail.com 2005-05-31, 4:01 pm |
| thank you for all your guys' reply. actually i wanted to distinguish a
non-anonymous variable, eg. A,B,..., from an anonymous variable,eg, _.
However i realized that it is unable to make it according to how prolog
works. i am trying to walk around this problem
Anders Lind=E9n wrote:
> Ok, here is an attempt
>
> shrink([],[]).
> shrink([A|B],C):- var(A),!, shrink(B,C).
> shrink([A|B],[A|C]):- nonvar(A), shrink(B,C).
>=20
>=20
> | ?- shrink([4,5,B,d(r,e)],C).
> C =3D [4,5,d(r,e)] ? ;
> no
| |
| John Fletcher 2005-06-01, 3:57 am |
| <ouyang.jie@gmail.com> wrote in message
news:1117555181.352717.206360@g49g2000cwa.googlegroups.com...
thank you for all your guys' reply. actually i wanted to distinguish a
non-anonymous variable, eg. A,B,..., from an anonymous variable,eg, _.
However i realized that it is unable to make it according to how prolog
works. i am trying to walk around this problem
<snipped>
Prolog can distinguish anonymous variables when reading terms.
Look up read_term/[2,3] in your manual, specifically the 'variable_names'
option.
Rgds
| |
| ouyang.jie@gmail.com 2005-06-01, 8:57 pm |
| thx, however that's not my situation. while i am not sure if read_term
can be redirect to read from internal database.
| |
| Roy Haddad 2005-06-02, 8:58 pm |
| John Fletcher wrote:
> The term "anonymous variable" doesn't make sense _except_ when reading
> terms. An anonymous variable is just a way to tell the compiler not to
> report a singleton variable warning, because variable names are not
> preserved in the internal database.
Well, they are also potentially useful because you know the variable is
not repeated elsewhere, so any binding to it need not be remembered for
future reference.
| |
| Joachim Schimpf 2005-06-02, 8:58 pm |
| Roy Haddad wrote:
> John Fletcher wrote:
>
>
>
> Well, they are also potentially useful because you know the variable is
> not repeated elsewhere, so any binding to it need not be remembered for
> future reference.
Yes, but your Prolog system can _see_ when a variable occurs only once in
a clause, regardless of the variable name. It doesn't need your help for
doing such an optimisation.
-- Joachim
| |
| ouyang.jie@gmail.com 2005-06-03, 8:57 pm |
| maybe i missed something in my project at some other points. actually i
am using parma polyhedra library with prolog. so every "named" variable
in a clause corresponds to a dimension of the polyhedra space. Every
singleton varialbe in a clause is represented by an underscore
automatically and i have both underscore and named variables in a
single clause in some case. In order to restrict the space dimensions
of the polyhedra space, it would be good to know the actual number of
"named" variables since every singleton variable has no constraint with
it. Hopfully i made the above clear. :)
| |
| Roberto Bagnara 2005-06-06, 3:57 am |
| Let me see if I understand: you are writing some sort of Prolog
program analysis using the Parma Polyhedra Library. For that
analysis, singleton variables (variables that occur only once in a
clause) can be disregarded and you would like not to assign a space
dimension to them. Is this correct?
In this case, it would seem you simply need a predicate returning the
list of non-singleton variables that occur in a term. The following
is some code that you may find useful. Please note that it has not
been tested.
non_singleton_variables_of(Term, NSV) :-
variable_occurrences(Term, Occurrences),
msort(Occurrences, Sorted_Occurrences),
drop_singletons_and_repetitions(Sorted_O
ccurrences, NSV).
variable_occurrences(Term, Occurrences) :-
variable_occurrences(Term, [], Occurrences).
variable_occurrences(Term, So_Far, Occurrences) :-
(var(Term) ->
Occurrences = [Term|So_Far]
;
(atomic(Term) ->
Occurrences = So_Far
;
Term =.. [_Functor|Arguments],
variable_occurrences_in_list(Arguments, So_Far, Occurrences)
)
).
variable_occurrences_in_list([], Occurrences, Occurrences).
variable_occurrences_in_list([Term|Other
_Terms], So_Far, Occurrences)
:-
variable_occurrences(Term, So_Far, Tmp),
variable_occurrences_in_list(Other_Terms
, Tmp, Occurrences).
drop_singletons_and_repetitions([], []).
drop_singletons_and_repetitions([Term|Mo
re_Terms], Non_Singletons) :-
(More_Terms = [Other_Term|Yet_More_Terms] ->
(Term == Other_Term ->
drop_prefix(Yet_More_Terms, Term, Different_Terms),
Non_Singletons = [Term|More_Non_Singletons],
drop_singletons_and_repetitions(Differen
t_Terms,
More_Non_Singletons)
;
drop_singletons_and_repetitions(More_Ter
ms, Non_Singletons)
)
;
Non_Singletons = []
).
drop_prefix([], _, []).
drop_prefix([H|T], X, L) :-
(H == X ->
drop_prefix(T, X, L)
;
L = [H|T]
).
All the best,
Roberto Bagnara
--
Prof. Roberto Bagnara
Computer Science Group
Department of Mathematics, University of Parma, Italy
http://www.cs.unipr.it/~bagnara/
mailto:bagnara@cs.unipr.it
| |
| ouyang.jie@gmail.com 2005-06-11, 3:57 am |
| many thanks, i will try it out.
Roberto Bagnara wrote:
> Let me see if I understand: you are writing some sort of Prolog
> program analysis using the Parma Polyhedra Library. For that
> analysis, singleton variables (variables that occur only once in a
> clause) can be disregarded and you would like not to assign a space
> dimension to them. Is this correct?
>
> In this case, it would seem you simply need a predicate returning the
> list of non-singleton variables that occur in a term. The following
> is some code that you may find useful. Please note that it has not
> been tested.
>
>
> non_singleton_variables_of(Term, NSV) :-
> variable_occurrences(Term, Occurrences),
> msort(Occurrences, Sorted_Occurrences),
> drop_singletons_and_repetitions(Sorted_O
ccurrences, NSV).
>
> variable_occurrences(Term, Occurrences) :-
> variable_occurrences(Term, [], Occurrences).
>
> variable_occurrences(Term, So_Far, Occurrences) :-
> (var(Term) ->
> Occurrences = [Term|So_Far]
> ;
> (atomic(Term) ->
> Occurrences = So_Far
> ;
> Term =.. [_Functor|Arguments],
> variable_occurrences_in_list(Arguments, So_Far, Occurrences)
> )
> ).
>
> variable_occurrences_in_list([], Occurrences, Occurrences).
> variable_occurrences_in_list([Term|Other
_Terms], So_Far, Occurrences)
> :-
> variable_occurrences(Term, So_Far, Tmp),
> variable_occurrences_in_list(Other_Terms
, Tmp, Occurrences).
>
> drop_singletons_and_repetitions([], []).
> drop_singletons_and_repetitions([Term|Mo
re_Terms], Non_Singletons) :-
> (More_Terms = [Other_Term|Yet_More_Terms] ->
> (Term == Other_Term ->
> drop_prefix(Yet_More_Terms, Term, Different_Terms),
> Non_Singletons = [Term|More_Non_Singletons],
> drop_singletons_and_repetitions(Differen
t_Terms,
> More_Non_Singletons)
> ;
> drop_singletons_and_repetitions(More_Ter
ms, Non_Singletons)
> )
> ;
> Non_Singletons = []
> ).
>
> drop_prefix([], _, []).
> drop_prefix([H|T], X, L) :-
> (H == X ->
> drop_prefix(T, X, L)
> ;
> L = [H|T]
> ).
>
>
> All the best,
>
> Roberto Bagnara
>
> --
> Prof. Roberto Bagnara
> Computer Science Group
> Department of Mathematics, University of Parma, Italy
> http://www.cs.unipr.it/~bagnara/
> mailto:bagnara@cs.unipr.it
|
|
|
|
|