prologb-prolog

Can I subtitude functor with variable in a predicate


I am new to prolog, and using BProlog.

I have been reading some example program to execute query on group of related data. But in order to infer from facts with similar structure, they wrote many predicates like search_by_name,search_by_point, which are partly duplicated.

% working search in example
search_by_name(Key,Value) :-
    Key == name,
    sname(ID,Value),
    point(ID,Point),
    write(Value),write(Point),nl.

And when I try to replace them with a more general version like this:

% a more general search I want to write
% but not accepted by BProlog
search_by_attr(Key,Value) :-
    Key(ID,Value),
    sname(ID,Name),
    point(ID,Point),
    write(Name),write(Point),nl.

error arised:

| ?- consult('students.pl')
consulting::students.pl
** Syntax error (students.pl, 17-21)
search_by_attr(Key,Value) :-
        Key<<HERE>>(ID,Value),
        sname(ID,Name),
        point(ID,Point),
        write(Name),write(Point),nl.

1 error(s)

Am I doing it the wrong way, or is such subtitution impossible in prolog?

code and example data can be found at https://gist.github.com/2426119


Solution

  • I don't know any Prolog that accept variables functors. There is call/N, or univ+call/1.

    search_by_attr(Key,Value) :-
        call(Key, ID, Value), % Key(ID,Value)
        ...
    

    or

    search_by_attr(Key,Value) :-
        C =.. [Key, ID, Value], % univ
        call(C),                % Key(ID,Value)
        ...