代码之家  ›  专栏  ›  技术社区  ›  Luc Touraille

查找与原子相关的所有子句

  •  3
  • Luc Touraille  · 技术社区  · 15 年前

    这可能是一个非常愚蠢的问题(我几个小时前才开始学习prolog),但是否有可能找到所有与原子相关的子句?例如,假设以下知识库:

    cat(tom).
    animal(X) :- cat(X).
    

    ,是否有办法获得关于tom的所有可能信息(或至少是在基础中明确说明的所有事实)?我知道这样的查询是不可能的:

    ?- Pred(tom).
    

    所以我想我可以写一条规则来推断正确的信息:

    meta(Object, Predicate) :-
        Goal =.. [Predicate, Object],
        call(Goal).
    

    这样我就可以编写如下查询

    ?- meta(tom, Predicate).
    

    但这不起作用,因为 call 没有充分实例化。所以基本上我的问题是:这是可能的,还是prolog不是用来提供这种信息的?如果不可能,为什么?

    1 回复  |  直到 15 年前
        1
  •  1
  •   Michael    15 年前

    您可以使用iso谓词“current_predicate/1”来找出可以调用的内容。 下面是一个示例程序:

    cat(tom).
    animal(X) :- cat(X).
    
    info(Arg,Info) :- current_predicate(PredName/1),
         Info =.. [PredName,Arg], call(Info).
    all_info(Arg,L) :- findall(I,info(Arg,I),L).
    

    您可以使用以下程序(我使用的是sicstus prolog btw):

    | ?- info(tom,X).
    X = animal(tom) ? ;
    X = cat(tom) ? ;
    no
    | ?- all_info(tom,X).
    X = [animal(tom),cat(tom)] ? 
    yes
    

    一般来说,你可以使用

    current_predicate
    如下:
    | ?- current_predicate(X).
    X = info/2 ? ;
    X = animal/1 ? ;
    X = cat/1 ? ;
    X = all_info/2 ? ;
    no
    
    推荐文章