clpfd

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

爱⌒轻易说出口 提交于 2019-12-03 15:20:52
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 drink tea The green house is left to the white house The owner of the green house drinks coffee The

PROLOG Print numbers that end in 7 and the sum of its digits is greater than 100

元气小坏坏 提交于 2019-12-02 03:53:20
问题 I need to make a predicate that receives a numeric list and print only the numbers that end in 7 and that the sum of its digits is greater than 100 I made the predicates for separated but I need help making a union of the two predicates, I mean that the two predicates go into one only predicate, this is what I did so far: %sum of digits greater than 100 multi(X):- 0 is X mod 100 sum([],0). sum([P|Q],Z). multi(P), sum(Q,Z1), Z is P + Z1. sum([P|Q],Z). not multi(P), sum(Q,Z). %print the numbers

Prolog arithmetic syntax

情到浓时终转凉″ 提交于 2019-12-01 07:36:36
How to define a as a integer/float number ? I want to find the results of a+b+c+d=10 where a,b,c,d is integer and >=0 . Here is a simple, modern, pure Prolog, non-CLP-library solution: range(X):- member(X,[0,1,2,3,4,5,6,7,8,9,10]). ten(A,B,C,D):- range(A), range(B), range(C), range(D), 10 =:= A + B + C + D. with SWI-Prolog you can use CLP(FD) library 1 ?- use_module(library(clpfd)). % library(error) compiled into error 0.00 sec, 9,764 bytes % library(clpfd) compiled into clpfd 0.05 sec, 227,496 bytes true. 2 ?- Vars=[A,B,C,D],Vars ins 0..10,sum(Vars,#=,10),label(Vars). Vars = [0, 0, 0, 10], A

Is it possible to declare an ascending list?

荒凉一梦 提交于 2019-12-01 06:36:38
I can make lists of ascending integer like so: ?- findall(L,between(1,5,L),List). I know I can also generate values using: ?- length(_,X). But I don't think I can use this in a findall, as things like the following loop: ?- findall(X,(length(_,X),X<6),Xs). I can also generate a list using clpfd . :- use_module(library(clpfd)). list_to_n(N,List) :- length(List,N), List ins 1..N, all_different(List), once(label(List)). or list_to_n2(N,List) :- length(List,N), List ins 1..N, chain(List,#<), label(List). The last method seems best to me as it is the most declarative, and does not use once/1 or

Reversible tree length relation

不想你离开。 提交于 2019-12-01 05:24:57
I'm trying to write reversible relations in "pure" Prolog (no is , cut, or similar stuff. Yes it's homework), and I must admit I don't have a clue how. I don't see any process to create such a thing. We are given "unpure" but reversible arithmetic relations (add,mult,equal,less,...) which we must use to create those relations. Right now I'm trying to understand how to create reversible functions by creating the relation tree(List,Tree) which is true if List is the list of the leaves of the binary tree Tree . To achieve such a thing I'm trying to create the tree_size(Tree,N) relation which is

Is it possible to declare an ascending list?

佐手、 提交于 2019-12-01 04:05:20
问题 I can make lists of ascending integer like so: ?- findall(L,between(1,5,L),List). I know I can also generate values using: ?- length(_,X). But I don't think I can use this in a findall, as things like the following loop: ?- findall(X,(length(_,X),X<6),Xs). I can also generate a list using clpfd. :- use_module(library(clpfd)). list_to_n(N,List) :- length(List,N), List ins 1..N, all_different(List), once(label(List)). or list_to_n2(N,List) :- length(List,N), List ins 1..N, chain(List,#<), label

Example channelling constraints ECLiPSe

徘徊边缘 提交于 2019-11-30 19:07:11
Can someone provide a simple example of channelling constraints? Channelling constraints are used to combine viewpoints of a constraint problem. Handbook of Constraint Programming gives a good explanation of how it works and why it can be useful: The search variables can be the variables of one of the viewpoints, say X1 (this is discussed further below). As search proceeds, propagating the constraints C1 removes values from the domains of the variables in X1. The channelling constraints may then allow values to be removed from the domains of the variables in X2. Propagating these value

Prevent backtracking after first solution to Fibonacci pair

北战南征 提交于 2019-11-30 10:08:19
The term fib(N,F) is true when F is the N th Fibonacci number. The following Prolog code is generally working for me: :-use_module(library(clpfd)). fib(0,0). fib(1,1). fib(N,F) :- N #> 1, N #=< F + 1, F #>= N - 1, F #> 0, N1 #= N - 1, N2 #= N - 2, F1 #=< F, F2 #=< F, F #= F1 + F2, fib(N1,F1), fib(N2,F2). When executing this query (in SICStus Prolog), the first (and correct) match is found for N (rather instantly): | ?- fib(X,377). X = 14 ? When proceeding (by entering ";") to see if there are any further matches (which is impossible by definition), it takes an enormous amount of time (compared

prolog convert numbers into roman numerals

限于喜欢 提交于 2019-11-30 09:13:41
问题 i have this code that converts integers into roman numerals i need to add a function that compares an integer with a roman numeral input and show if it's try or false, for example: roman(v,5). true toroman(0). toroman(N) :- N < 4, put("I"), M is N - 1, toroman(M). toroman(N) :- N = 4, put("I"), put("V"). toroman(N) :- N = 5, put("V"). toroman(N) :- N < 9, put("V"), M is N - 5, toroman(M). toroman(N) :- N = 9, put("I"), put("X"). toroman(N) :- N < 40, put("X"), M is N - 10, toroman(M). toroman

Counter-intuitive behavior of min_member/2

巧了我就是萌 提交于 2019-11-30 02:51:11
问题 min_member(-Min, +List) True when Min is the smallest member in the standard order of terms. Fails if List is empty. ?- min_member(3, [1,2,X]). X = 3. The explanation is of course that variables come before all other terms in the standard order of terms, and unification is used. However, the reported solution feels somehow wrong. How can it be justified? How should I interpret this solution? EDIT: One way to prevent min_member/2 from succeeding with this solution is to change the standard