prolog

Why is this prolog query both true and false?

旧时模样 提交于 2019-12-30 01:36:10
问题 My SWI-Prolog knowledge base contains the following two facts: f(a,b). f(a,c). Now if I pose the query ?- f(a,c). true. But ?- f(a,b). true ; false. Why is f(a,b) both true and false? This also happens when there are three facts in the KB. If I append f(a,d). to the KB, then f(a,d) is true (only), but f(a,b) and f(a,c) are both true and false. What's going on, and what can I do so that Prolog answers (only) true to these queries? 回答1: (Note: this answer is somewhat of a guess) Consider how

Combing generator results and writing result to stream

南楼画角 提交于 2019-12-29 09:07:49
问题 Currently I can generate expression trees. expression_tree([_|N_s],N_s, [number(0)]). expression_tree([_|N_s0],N_s1, [op(neg),[E1]]) :- expression_tree(N_s0,N_s1, E1). expression_tree([_|N_s0],N_s2, [op(add), [E1, E2]]) :- expression_tree(N_s0,N_s1, E1), expression_tree(N_s1,N_s2, E2). generate_expression(N_c, E) :- length(N, N_c), expression_tree(N,[], E). ?- generate_expression(N,E). N = 1, E = [number(0)] ; N = 2, E = [op(neg), [[number(0)]]] ; N = 3, E = [op(neg), [[op(neg), [[number(0)]]

Combing generator results and writing result to stream

只愿长相守 提交于 2019-12-29 09:07:28
问题 Currently I can generate expression trees. expression_tree([_|N_s],N_s, [number(0)]). expression_tree([_|N_s0],N_s1, [op(neg),[E1]]) :- expression_tree(N_s0,N_s1, E1). expression_tree([_|N_s0],N_s2, [op(add), [E1, E2]]) :- expression_tree(N_s0,N_s1, E1), expression_tree(N_s1,N_s2, E2). generate_expression(N_c, E) :- length(N, N_c), expression_tree(N,[], E). ?- generate_expression(N,E). N = 1, E = [number(0)] ; N = 2, E = [op(neg), [[number(0)]]] ; N = 3, E = [op(neg), [[op(neg), [[number(0)]]

Prolog: Split list at integer in list of lists

99封情书 提交于 2019-12-29 08:49:07
问题 I would like to split a list of words separated through integers into a list of lists. Sample query and expected result: ?- separatewords([h,e,l,l,o,1,o,v,e,r,3,t,h,e,r,e], X). X = [[h,e,l,l,o],[o,v,e,r],[t,h,e,r,e]]. The following things I already achieved: Splitting the list into one list before the first integer and one after the first integer: Sample query with result: ?- take1word([h,e,l,l,o,1,o,v,e,r,3,t,h,e,r,e], X, Y). X = [h,e,l,l,o], Y = [o,v,e,r,3,t,h,e,r,e]. % OK My code:

Simple nth1 predicate in Prolog

限于喜欢 提交于 2019-12-29 08:48:09
问题 With SWI Prolog, there's a predicate that finds the nth item in a list called nth1. I want to implement my own version of the predicate but SWI's is so complicated if you look at the listing(nth1) code. Is there a simpler way of doing it? Thank you :). 回答1: The SWI code is a bit complex because the predicate can be used to generate from a variable index: ?- nth1(Idx,[a,b,c],X). Idx = 1, X = a ; Idx = 2, X = b ; Idx = 3, X = c ; false. If you don't want that behavior, nth1/3 can be implemented

Simple nth1 predicate in Prolog

夙愿已清 提交于 2019-12-29 08:48:07
问题 With SWI Prolog, there's a predicate that finds the nth item in a list called nth1. I want to implement my own version of the predicate but SWI's is so complicated if you look at the listing(nth1) code. Is there a simpler way of doing it? Thank you :). 回答1: The SWI code is a bit complex because the predicate can be used to generate from a variable index: ?- nth1(Idx,[a,b,c],X). Idx = 1, X = a ; Idx = 2, X = b ; Idx = 3, X = c ; false. If you don't want that behavior, nth1/3 can be implemented

knight's tour efficient solution

為{幸葍}努か 提交于 2019-12-29 08:47:14
问题 I have build a code in prolog to find a series of legal moves in which the knight lands on each square of the chessboard(8x8) exactly once. I have used a logic like below: There 8 types of knight moves: right 1 down 2 left 1 down 2 right 2 down 1 left 2 down 1 right 1 up 2 left 1 up 2 right 2 up 1 left 2 up 1 right 1 down 2 moves: move(X,Y) :- C_X is X mod 8, R_X is X // 8, C_Y is C_X + 1, % 1 right C_Y < 8, R_Y is R_X + 2, % 2 down R_Y < 8, Y is R_Y * 8 + C_Y, Y >= 0, X >= 0, X < 64, Y < 64.

What is meant by “logical purity” in Prolog?

醉酒当歌 提交于 2019-12-29 08:33:10
问题 What is meant by "logical purity" (in the context of Prolog programming)? The logical-purity tag info says "programs using only Horn clauses" , but then, how would predicates like if_/3 qualify, using as much as it does the cut, and the various meta-logical (what's the proper terminology? var/1 and such) predicates, i.e. the low-level stuff. I get it that it achieves some "pure" effect, but what does this mean, precisely? For a more concrete illustration, please explain how does if_/3 qualify

What is meant by “logical purity” in Prolog?

≡放荡痞女 提交于 2019-12-29 08:33:03
问题 What is meant by "logical purity" (in the context of Prolog programming)? The logical-purity tag info says "programs using only Horn clauses" , but then, how would predicates like if_/3 qualify, using as much as it does the cut, and the various meta-logical (what's the proper terminology? var/1 and such) predicates, i.e. the low-level stuff. I get it that it achieves some "pure" effect, but what does this mean, precisely? For a more concrete illustration, please explain how does if_/3 qualify

Understanding CLP(FD) Prolog code of N-queens problem

亡梦爱人 提交于 2019-12-29 07:18:16
问题 I am trying to understand N-queens problem's solution as given below: :- use_module(library(clpfd)). n_queens(N, Qs) :- length(Qs, N), Qs ins 1..N, safe_queens(Qs). safe_queens([]). safe_queens([Q|Qs]) :- safe_queens(Qs, Q, 1), safe_queens(Qs). safe_queens([], _, _). safe_queens([Q|Qs], Q0, D0) :- Q0 #\= Q, abs(Q0 - Q) #\= D0, D1 #= D0 + 1, safe_queens(Qs, Q0, D1). I am not able to understand the below snippet: safe_queens([]). safe_queens([Q|Qs]) :- safe_queens(Qs, Q, 1), safe_queens(Qs).