prolog

Solving Einstein Riddle in Prolog

雨燕双飞 提交于 2020-01-02 05:48:18
问题 I am trying to solve Einstein Riddle in Prolog. I am having a difficulty with the program I wrote, the basic method was to add all constraints and let Prolog figure out the only possible solutions. The problem is that Prolog finds 0 solutions. I have isolated the constraint that makes the program go from a given solution to no solutions, but I don't understand why. /*There are five houses*/ exists(A, list(A,_,_,_,_)). exists(A, list(_,A,_,_,_)). exists(A, list(_,_,A,_,_)). exists(A, list(_,_,

Turning off warnings in swi-prolog

不打扰是莪最后的温柔 提交于 2020-01-02 02:40:07
问题 How Can I turn off warnings in swi-prolog. Clauses of XXX/AA are not together in the source-file is very annoying. 回答1: Instead, you could fix the warning. The discontiguous directive is needed when the clauses of a static (compiled) predicate cannot be compiled as a single unit. This happens when the clause definitions are: Not contiguous Exceed the maximum number of clauses 回答2: You can turn off these warnings with style_check using :-style_check(-discontiguous). . For example, you can also

Prolog: how to do “check(a++b++c++d equals d++a++c++b) -> yes”

拟墨画扇 提交于 2020-01-01 12:05:29
问题 Let's define custom operators - let it be ++ , equals :- op(900, yfx, equals). :- op(800, xfy, ++). And fact: check(A equals A). I try to make predicate, let it be check/1 , that will return true in all following situations: check( a ++ b ++ c ++ d equals c ++ d ++ b ++ a ), check( a ++ b ++ c ++ d equals d ++ a ++ c ++ b), check( a ++ b ++ c ++ d equals d ++ b ++ c ++ a ), % and all permutations... of any amount of atoms check( a ++ ( b ++ c ) equals (c ++ a) ++ b), % be resistant to any

How can lexing efficiency be improved?

旧城冷巷雨未停 提交于 2020-01-01 11:25:24
问题 In parsing a large 3 gigabyte file with DCG, efficiency is of importance. The current version of my lexer is using mostly the or predicate ;/2 but I read that indexing can help. Indexing is a technique used to quickly select candidate clauses of a predicate for a specific goal. In most Prolog systems, indexing is done (only) on the first argument of the head. If this argument is instantiated to an atom, integer, float or compound term with functor, hashing is used to quickly select all

Query the relation between two people in a Prolog Family Tree

瘦欲@ 提交于 2020-01-01 10:27:25
问题 Suppose I have the below code in my familyTree.pl file: male(tom). male(bob). female(lisa). female(emily). parent(tom, bob). parent(lisa, bob). morethanfriends(emily, bob). father(X,Y) :- male(X), parent(X,Y). mother(X,Y) :- female(X), parent(X,Y). girlfriend(X,Y) :- female(X), (morethanfriends(X,Y); morethanfriends(Y,X)). boyfriend(X,Y) :- male(X), (morethanfriends(X,Y); morethanfriends(Y,X)). Now, I want to get the answer to the questions like: What is the relationship between Tom and Bob ?

How to create a list of numbers that add up to a specific number

泪湿孤枕 提交于 2020-01-01 09:37:12
问题 I need some help writing a predicate in Prolog that, given a number as input, returns a list of lists with numbers that add up to it. Let's call the predicate addUpList/2 , it should work like this: ?- addUpList(3,P). P = [[1,2], [2,1], [1,1,1]]. % expected result I'm having so much trouble figuring this out I'm beginning to think it's impossible. Any ideas? Thanks in advance. 回答1: Try this: condense([], Rs, Rs). condense([X|Xs], Ys, Zs) :- condense(Xs, [X|Ys], Zs). condense([X, Y|Xs], Ys, Zs

adding a search Path in SWI prolog

笑着哭i 提交于 2020-01-01 08:58:12
问题 In many Prolog systems it is easy to add a new search path for consulting file. In Yap for example, the predicate I know it is add_to_path(NewPath). Is there a way to do the same in SWI Prolog ?. My question is specifically about adding one path to the already existing paths, I am aware of the file_search_path/2 predicate for declaring directories, and the cd/1 predicate for changing the current directory, but I would like to know if there is an alternative method, like the one I found in Yap

Prolog: Clauses are not together in source-file

风格不统一 提交于 2020-01-01 07:32:28
问题 I have this piece of code: % Family tree female(pen). male(tom). male(bob). female(liz). female(pat). female(ann). male(jim). parent(pam, bob). parent(tom, bob). parent(tom, liz). parent(bob, ann). parent(bob, pat). parent(pat, jim). I get this error: Warning: Clauses of female/1 are not together in source-file Warning: Clauses of male/1 are not together in source-file What is the purpose of this error? I mean, file does compile and run just fine and I am aware of the meaning of the error.

More compact definition

别说谁变了你拦得住时间么 提交于 2020-01-01 07:29:12
问题 Given word/1 , word(W) :- abs(ABs), ABs = W. abs([]). abs([AB|ABs]) :- abs(ABs), ab(AB). ab(a). ab(b). ?- word(W). W = [] ; W = [a] ; W = [b] ; W = [a,a] ; W = [b,a] ; W = [a,b] ; W = [b,b] ; W = [a,a,a] ; W = [b,a,a] ; W = [a,b,a] ; W = [b,b,a] ; W = [a,a,b] ; W = [b,a,b] ; W = [a,b,b] ; W = [b,b,b] ; W = [a,a,a,a] ... how does a more compact definition of word/1 look like, otherwise identical w.r.t. termination and the set of solutions, fairness, with the following constraints: No use of

Stream reasoning / Reactive programming in prolog?

给你一囗甜甜゛ 提交于 2020-01-01 05:26:13
问题 I was wondering if you know of any way to use prolog for stream processing, that is, some kind of reactive programming, or at least to let a query run on a knowledge base that is continuously updated (effectively a stream), and continuously output the output of the reasoning? Anything implemented in the popular "prologs", such as SWI-prolog? 回答1: You can use Logtalk's support for event-driven programming to define monitors that watch for knowledge base update events and react accordingly. You