2

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

false
  • 10,264
  • 13
  • 101
  • 209
Jokester
  • 5,501
  • 3
  • 31
  • 39

1 Answers1

4

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)
    ...
CapelliC
  • 59,646
  • 5
  • 47
  • 90
  • Alternatively, one can incorporate the Key value as one of the arguments. – Alexander Serebrenik Apr 20 '12 at 09:53
  • @Alexander Serebrenik: do you mean `atom_concat(search_by_, Name, Callable),call(Callable, ...)` ? – CapelliC Apr 20 '12 at 10:07
  • No, I thought about renamimg search_by_name and search_by_point to search such that the first argument is either name or point. – Alexander Serebrenik Apr 20 '12 at 10:16
  • 1
    @CapelliC Is there a meaningful difference between `C =.. [Key, Id, Value], call(C),…` and `C =.. [Key, Id, Value], C,…`? – Daniel Lyons Feb 02 '13 at 06:31
  • @DanielLyons: back to 80s, only some system implemented the second form, the first was ubiquitous. Anyway, call/N it's more efficient. – CapelliC Feb 02 '13 at 07:26
  • 1
    You need systems that implement HiLog (see ["HiLog- A Foundation for Higher-Order Logic Programming"](http://citeseer.ist.psu.edu/viewdoc/summary?doi=10.1.1.52.7860) by Weidong Chen, Michael Kifer, David S. Warren, 1996) like XSB. An excellent read on higher-order logic programming NOT using HiLog is ["Higher-order logic programming in Prolog"](https://www.csee.umbc.edu/courses/771/papers/mu_96_02.pdf) by Lee Naish (1996) – David Tonhofer Nov 08 '16 at 18:44