Prolog的双胞胎将重复回答

时间:2017-01-02 20:28:24

标签: prolog

我们说我有3个孩子

//Start.
editor1 = new Wt::WText(wt_root);
                editor1->setText("Testing for the highlight.");
                editor1->setInline(false);




                //REQUIREMENT FOR THE ACEEDITOR FILE INPUTED.
                Wt::WApplication::instance()->require(std::string("AceFiles/ace.js"));


                //CONFIG FOR THE EDITOR THAT WILL SUPPORT TEXT.
                editor = new Wt::WContainerWidget(wt_root);
                editor->resize(500, 500);

                range = new Wt::WContainerWidget(wt_root);



                //editor_ref IS THE STRING THAT THE USER IS WRITTING.
                std::string editor_ref = editor->jsRef();
                std::string range_ref = range->jsRef();
                std::string command =
                    editor_ref + ".editor = ace.edit(" + editor_ref + ");" +
                    range_ref + ".range = ace.require('ace/range')." + range_ref + ";" +
                    editor_ref + ".editor.setTheme(\"ace / theme / github\");" +
                    editor_ref + ".editor.getSession().setMode(\"ace/mode/assembly_x86\");" +
                    editor_ref + ".editor.session.addMarker(new Range(1, 0, 15, 0), \"fullLine\");";

                editor->doJavaScript(command);

                //CONFIG. FOR THE JSIGNAL USED.
                //BEING THE CONNECTION BETWEEN THE C++ DOC AND THE JAVA SCRIPT. 
                jsignal = new Wt::JSignal<std::string>(editor, "textChanged");
                jsignal->connect(this, &Ui_AceEditor::textChanged);





                //CONFIG FOR THE BUTTON.
                b = new Wt::WPushButton("Save", wt_root);

                command = "function(object, event) {" +
                    jsignal->createCall(editor_ref + ".editor.getValue()") +
                    ";}";

                b->clicked().connect(command);

我想得到这些孩子的双胞胎

person(pet,fox,date(5,may,2004),unemployed).  
person(jim,fox,date(5,may,2004),unemployed).  
person(george,fox,date(9,december,2002),unemployed).  

child(X):-X=person(pet,fox,date(5,may,2004),unemployed).  
child(X):-X=person(jim,fox,date(5,may,2004),unemployed).  
child(X):-X=person(george,fox,date(9,december,2002),unemployed).  

问题是我问prolog双胞胎(X,Y)。我会再次得到一些答案。如何防止?

twins(Child1,Child2) :-
    child(Child1),child(Child2),
    Child1=person(Fname1,_,Date1,_),
    Child2=person(Fname2,_,Date2,_),
    Fname1\=Fname2,
    Date1=Date2.

1 个答案:

答案 0 :(得分:1)

您可以通过对该元素的元素强加非对称顺序来阻止所有排列的生成:

?- twins(X, Y), X @< Y.
X = person(jim, fox, date(5, may, 2004), unemployed),
Y = person(pet, fox, date(5, may, 2004), unemployed) ;
false.

这只会从此查询中删除不需要的解决方案,但要在每次调用twins/2时实现此目的,您只需在\=的{​​{1}}定义中替换twins/2。 (@<关系中的两个术语在@<意义上自动不相等,除非其中一个是变量。)

作为另一个评论,将数据库复制为\=个事实以及person/4子句中的数据是奇怪的,不必要的,并且非常容易出错。您可以使用引用您的child/1数据库的单个子句替换child/1的三子句定义:

person/4