clpfd

I don't understand what label does in Prolog

烈酒焚心 提交于 2019-12-07 00:30:24
问题 I went through the manual and documentation but still don't understand. I'm trying to implement a sudoku solution where after writing out all the other rules of the game, I've added label(Board) according to my teacher's instructions. However I still don't get how it works or what it's doing. Shouldn't the other constraints(I have checks saying numbers have to be 1..9, row has to be all different,etc) give me the answer by themselves? 回答1: If you want to learn Prolog and CLP(FD) rapidly, use

Recursive multiplication in prolog

自古美人都是妖i 提交于 2019-12-06 09:13:20
问题 New to prolog. edit: using swi-prolog I want to recursively do what the multiplication method already does in prolog without actually using the multiplication method. The algorithm I want to implement it looks something like: multn(N1, N2, output){ if (n2 <=0) return output; else multn(N1, (N2-1), output + N1) } Ex: 4*4 = 4+4+4+4 = 16 edit*: only passing in positive numbers for this algo. my knowledge db looks like: multn(Num1, 0, Result) :- Result is 0. multn(Num1, Num2, Result) :- NewNum2 =

Prolog; try to make fibonacci more effective?

别等时光非礼了梦想. 提交于 2019-12-05 19:05:01
问题 This logic programming is really making a lap dance on my imperative programming skills. This is homework, so please just don't drop me the answer. This is what I have: fibo(N,1) :- N < 2, !. fibo(N,R) :- N1 is N-1, N2 is N-2, fibo(N1,R1), fibo(N2,R2), R is R1+R2. I'm suppose to make another function that looks like this; fib(N,Value,LastValue) . N is the n'th number, and value is the return value. I don't understand how I can rewrite this using accumulation. And since it counts backwards I

Mandatory reification when using the 'mod' operator together with 'or'?

*爱你&永不变心* 提交于 2019-12-05 12:33:50
I have written a CSP program using CLP(FD) and SWI-Prolog. I think I need to improve my constraints' writing when I use the mod operator together with #\/ in my predicates. A short example : :- use_module(library(clpfd)). constr(X,Y,Z) :- X in {1,2,3,4,5,6,7}, Y in {3,5,7}, Z in {1,2}, ((X #= 3)) #==> ((Y mod 3 #= 0) #\/ (Y mod 7 #= 0)), ((Z #= 1)) #<==> ((Y mod 3 #= 0) #\/ (Y mod 7 #= 0)). If I call constr(3,Y,Z). , I get Z #= 1 or Z #= 2 . This is because some intermediate variables (relative to the mod expressions) still need to be evaluated. Of course the ideal would be to only obtain Z #=

Solving the Zebra puzzle (aka. Einstein puzzle) using the clpfd Prolog library

孤街醉人 提交于 2019-12-04 21:45:20
问题 I have been given an exercise to solve the zebra puzzle using a constraint solver of my choice, and I tried it using the Prolog clpfd library. I am aware that there are other more idiomatic ways to solve this problem in Prolog, but this question is specifically about the clpfd package! So the specific variation of the puzzle (given that there are many of them) I'm trying to solve is this one: There are five houses The Englishman lives in the red house The Swedish own a dog The Danish likes to

Recursive multiplication in prolog

爷,独闯天下 提交于 2019-12-04 15:47:22
New to prolog. edit: using swi-prolog I want to recursively do what the multiplication method already does in prolog without actually using the multiplication method. The algorithm I want to implement it looks something like: multn(N1, N2, output){ if (n2 <=0) return output; else multn(N1, (N2-1), output + N1) } Ex: 4*4 = 4+4+4+4 = 16 edit*: only passing in positive numbers for this algo. my knowledge db looks like: multn(Num1, 0, Result) :- Result is 0. multn(Num1, Num2, Result) :- NewNum2 = Num2 - 1, multn(Num1, NewNum2, NewResult), Result is Num1 + NewResult. However, when I call: ?- multn

Mini sudoku solver in Prolog stops partway through

好久不见. 提交于 2019-12-04 10:07:45
I'm working through 'Seven Languages in Seven Weeks', and I'm just trying to get an example from the book working. It solves a mini sudoku grid (4x4). The author is using gprolog, but I am using swi-prolog (I couldn't get gprolog to work on my VM for whatever reason, but swi-prolog worked first try). I am running Ubuntu 10.04 in VirtualBox 4.0.4 r70112 (hopefully that's not too relevant!) Here is the code in my prolog file: :- use_module(library(clpfd)). valid([]). valid([Head|Tail]) :- all_different(Head), % in the book, this is 'fd_all_different' valid(Tail). % beginning of sudoku rule

SWI-Prolog and constraints, library CLP(FD)

懵懂的女人 提交于 2019-12-04 03:31:49
问题 I'm playing around with constraints in (swi) prolog using the clpfd library. I'm trying to identify when one set of constraints encapsulates or subsumes the other, e.g. X<4 encapsulates X<7 as whenever the former is true, the latter is true. This can be easily represented using logical implication. However, I couldn't get the #==> operator to give me the result I wanted, so I resorted to using not(Co1 #/\ #\Co2) where Co1 and Co2 are constraints. This is fine for individual constraints, but I

Simple prolog program. Getting error: >/2: Arguments are not sufficiently instantiated

喜夏-厌秋 提交于 2019-12-04 02:57:45
I made a Prolog predicate posAt(List1,P,List2) that tests whether the element at position P of List1 and List2 are equal: posAt([X|Z], 1, [Y|W]) :- X = Y. posAt([Z|X], K, [W|Y]) :- K > 1, Kr is K - 1, posAt(X, Kr, Y). When testing: ?- posAt([1,2,3], X, [a,2,b]). I expected an output of X = 2 but instead I got the following error: ERROR: >/2: Arguments are not sufficiently instantiated Why am I getting this error? CapelliC A Prolog predicate is a relation between arguments, and your statement the element at position P of List1 and List2 are equal is clearly an example where multiple solutions

Prolog; try to make fibonacci more effective?

烂漫一生 提交于 2019-12-04 02:41:51
This logic programming is really making a lap dance on my imperative programming skills. This is homework, so please just don't drop me the answer. This is what I have: fibo(N,1) :- N < 2, !. fibo(N,R) :- N1 is N-1, N2 is N-2, fibo(N1,R1), fibo(N2,R2), R is R1+R2. I'm suppose to make another function that looks like this; fib(N,Value,LastValue) . N is the n'th number, and value is the return value. I don't understand how I can rewrite this using accumulation. And since it counts backwards I don't see how it can "know" a last value before it calculates anything. :s Any input is appreciated. I