prolog

Define parts of a predicate across several modules

我的梦境 提交于 2019-12-23 12:47:22
问题 I'm trying to write a predicate move/3 which handles several kinds of terms, each kind of which is defined in a separate file. I'm trying to use modules for this, because the files contain other predicates which should be namespaced appropriately. So, I've created a module cat.prolog with contents: :- module(cat, [move/3]). :- multifile(move/3). move(cat(C), P, cat(C2)) :- ... Similarly for dog.prolog . And main.prolog with: :- use_module(['cat.prolog'], [move/3]). :- use_module(['dog.prolog'

Is this Prolog terminology correct?

泪湿孤枕 提交于 2019-12-23 12:46:13
问题 Getting the terminology correct is part of the success to communicating a concept and when the wrong terminology is used here at SO with the Prolog tag the respondents nicely point out the mistake. In reading "Clause and Effect - Prolog Programming for the Working Programmer" by William F. Clocksin in 1997 (WorldCat) is the paragraph A Prolog program consists of a collection of procedures . Each procedure defines a particular predicate , being a certain relationship between its arguments . A

higher-order “solutions” predicate

余生颓废 提交于 2019-12-23 12:26:06
问题 I am using a higher order Prolog variant that lacks findall . There is another question on implementing our own findall here: Getting list of solutions in Prolog. The inefficient implementation is: parent(pam, bob). %pam is a parent of bob parent(george, bob). %george is a parent of bob list_parents(A, Es, [X|Xs]) :- parent(X, A), \+ member(X, Es), list_parents(A, [X|Es], Xs). list_parents(A, Es, []). The efficient one need a "solutions" higher-order predicate: list_parents(X, Ys) :-

How to identify wasteful representations of Prolog terms

青春壹個敷衍的年華 提交于 2019-12-23 12:25:43
问题 What is the Prolog predicate that helps to show wasteful representations of Prolog terms? Supplement In a aside of an earlier Prolog SO answer, IIRC by mat, it used a Prolog predicate to analyze a Prolog term and show how it was overly complicated. Specifically for a term like [op(add),[[number(0)],[op(add),[[number(1)],[number(1)]]]]] it revealed that this has to many [] . I have searched my Prolog questions and looked at the answers twice and still can't find it. I also recall that it was

Anomaly in GNU Prolog `delete/3` predicate?

こ雲淡風輕ζ 提交于 2019-12-23 12:25:40
问题 I found an inconsistency in behavior in the GNU Prolog (version 1.4.2) delete/3 predicate: | ?- delete([a,b,c], b, R). R = [a,c] yes | ?- delete([(a,_), (b,_), (c,_)], (b,_), R). R = [(a,_),(b,_),(c,_)] yes | ?- member( (b,_), [(a,_), (b,_), (c,_)] ). true ? ; no | ?- select((b,_), [(a,_), (b,_), (c,_)], R). R = [(a,_),(c,_)] ? ; no | ?- All of the above results I expected, except for that of, delete([(a,_), (b,_), (c,_)], (b,_), R). . If you run the same set of queries in SWI Prolog, for

How to get Prolog to explain your result beyond the true statement

China☆狼群 提交于 2019-12-23 12:17:42
问题 I have the following facts and rules: flight(sea,msp). flight(msp,jfk). route(A,B) :- flight(A,B). route(B,A) :- flight(A,B). route(A,C) :- flight(A,B) , flight(B,C). when query for route(sea,jfk) I get a result true but what I wish to get is the explination: sea-->msp-->jfk this way I can tell not only that it's true but also how it's true. 回答1: You keep track of what nodes in your graph that you've already visited. You need to do this anyway, as you need to detect cycles in your graph lest

A combination of a list given a length followed by a permutation in Prolog?

☆樱花仙子☆ 提交于 2019-12-23 12:04:20
问题 I need a predicate to return a list with all combinations of the input list, and the list result size is in the second param, the predicate would be like this permutInListN( +inputList, +lengthListResult, -ListResult), example: permutInListN([1,2,3],2,L). ? L=[1,2]. ? L=[2,1]. ? L=[1,3]. ? L=[3,1]. ? L=[2,3]. ? L=[3,2]. Combinations of [1,2,3] in a list L with length 2 . with no repetitions maybe using setoff. this is my code but it doesn't work at all , no generate all solutions

Prolog, how to show multiple output in write()

久未见 提交于 2019-12-23 11:44:38
问题 go :- match(Mn,Fn), write('--Matching Result--'), nl, write(Mn), write(' match with '), write(Fn), match(Mn1,Fn1). person(may,female,25,blue). person(rose,female,20,blue). person(hock,male,30,blue). person(ali,male,24,blue). match(Mn,Fn):-person(Fn,'female',Fage,Fatt), person(Mn,'male',Mage,Matt), Mage>=Fage, Fatt=Matt. Hi,this is my code...but it's only can show the 1 output...but there are 3 pair of matching in match(X,Y).how to show them all in my go function. Thank you 回答1: You get all

Prolog Design Pattern to extend module predicates

微笑、不失礼 提交于 2019-12-23 10:56:44
问题 Imagine we have the familytree module below ( simple example ) : :- module(familytree, [ father/2, mother/2, %[...] ]). father(X,Y) :- male(X),parent(X,Y). father(unknown, _) :- male(unknown). mother(X,Y) :- female(X),parent(X,Y). mother(unknown, _) :- female(unknown). sister(X,Y) :- female(X),parent(Z,X),parent(Z,Y), X \= Y. %[... other relation predicates ... ] I want to use this module predicates with different "dbs", for examples with : :- module(familytree_xyz, []). male(james). male

Prolog: missing feature?

ぐ巨炮叔叔 提交于 2019-12-23 10:56:09
问题 Any programmer with some experience in Prolog knows the advantages of use unary notation for numbers. By example, if we represent a number as list of 1" ("4" is list "[1,1,1,1]" and so on), we can define: unary_succ(X,[1|X]). the following queries does what is expected: ?- X=[1,1],unary_succ(X,Y). X = [1, 1], Y = [1, 1, 1]. ?- unary_succ(X,Y),X=[1,1]. X = [1, 1], Y = [1, 1, 1]. ?- unary_succ(X,Y),Y=[1,1]. X = [1], Y = [1, 1]. In this way, statement unary_succ(X,Y) "binds" X and Y in a way