prolog

Representing logical disjunctions in Constraint Handling Rules

夙愿已清 提交于 2019-12-25 06:26:49
问题 I am writing a constraint solver in Prolog that implements a simple logical formula: "(alive(A) and animal(A)) iff (awake(A) or asleep(A))" . I found one way to implement it in Constraint Handling Rules, but it is much more verbose than the original formula: :- use_module(library(chr)). :- chr_constraint is_true/1. is_true(A) \ is_true(A) <=> true. is_true(alive(A)),is_true(animal(A)) ==> is_true(awake(A));is_true(asleep(A)). is_true(awake(A)) ==> is_true(animal(A)),is_true(alive(A)). is_true

Prolog riddle solving

六眼飞鱼酱① 提交于 2019-12-25 04:48:10
问题 The statement : Four couples in all Attended a costume ball. 2 The lady dressed as a cat Arrived with her husband Matt. 3 Two couples were already there, One man dressed like a bear. 4 First to arrive wasn't Vince, But he got there before the Prince. 5 The witch (not Sue) is married to Chuck, Who was dressed as Donald Duck. 6 Mary came in after Lou, Both were there before Sue. 7 The Gipsy arrived before Ann, Neither is wed to Batman. 8 If Snow White arrived after Tess, Then how was each

Prolog riddle solving

▼魔方 西西 提交于 2019-12-25 04:48:05
问题 The statement : Four couples in all Attended a costume ball. 2 The lady dressed as a cat Arrived with her husband Matt. 3 Two couples were already there, One man dressed like a bear. 4 First to arrive wasn't Vince, But he got there before the Prince. 5 The witch (not Sue) is married to Chuck, Who was dressed as Donald Duck. 6 Mary came in after Lou, Both were there before Sue. 7 The Gipsy arrived before Ann, Neither is wed to Batman. 8 If Snow White arrived after Tess, Then how was each

Prolog query to find largest element in database?

心不动则不痛 提交于 2019-12-25 04:42:33
问题 If I have defined all the digits in a Prolog database, such as dig(0), dig(1), ..., dig(9) . What query can I use for Prolog to return the largest digit -- in this case, 9? I tried something like: ?- dig(N), dig(M), N > M. But that just returns the first possibility, not the largest number. 回答1: To find out the largest number you should write an appropriate query, namely one that: Instantiate a digit Checks whether that digit is the largest (i.e. no other digit is larger) So you might want to

How do I make a recursive list that checks company rankings?

泪湿孤枕 提交于 2019-12-25 04:35:21
问题 I have a set of companies in rank order. I want my rule to check if the companies in a specified list are in rank order, and for the rule to recur until all companies in the list have been checked. I currently have the following: isOrder([]). isOrder([COM1,COM2|T]) :- rank(COM1,D), rank(COM2,E), D<E, print("in order"), isOrder([COM2|T]). However, this does not seem to work. Sometimes, the recursion goes on forever without ending, and sometimes the recursion doesn't work at all. This is when I

Find the shortest path between two nodes in a graph in (Prolog) and display the result as array

自作多情 提交于 2019-12-25 04:34:07
问题 [ ] How can I write (using print_path ) a rule to print the path from one node to another if exists Print_path(a, c, Res) ---> Res=[a, f, c] What I did was : path(a,b). %there is a path from a to b path(a,f). path(b,c). path(c,d). path(c,e). path(e,d). path(f,g). path(f,c). path(f,e).` I do not know what to do next. 来源: https://stackoverflow.com/questions/41112132/find-the-shortest-path-between-two-nodes-in-a-graph-in-prolog-and-display-the

Max Independent Set in Prolog

孤街浪徒 提交于 2019-12-25 04:28:09
问题 I am trying to implement a Prolog predicate that gets a binary tree (represented as t(Left, Root, Right)) and returns a list that is the Maximal Independent Set (MIS) of this tree, and its size. I first understood that MIS(T) is the maximum between the MIS with root and the MIS without the root. Then, I used two theorems, stating that the MIS with root is the unification of the MIS's without the root for all the subtrees, and that MIS without root is the unification of the MIS's of all the

Max Independent Set in Prolog

北慕城南 提交于 2019-12-25 04:28:01
问题 I am trying to implement a Prolog predicate that gets a binary tree (represented as t(Left, Root, Right)) and returns a list that is the Maximal Independent Set (MIS) of this tree, and its size. I first understood that MIS(T) is the maximum between the MIS with root and the MIS without the root. Then, I used two theorems, stating that the MIS with root is the unification of the MIS's without the root for all the subtrees, and that MIS without root is the unification of the MIS's of all the

Prolog recrusive Algorithm

帅比萌擦擦* 提交于 2019-12-25 04:12:01
问题 foo(0,Y,Z) :- Z is Y. foo(X,0,Z) :- Z is X. foo(X,Y,Z) :- X>=Y, M1 is X-2, foo(M1, Y, Zx), Z is Zx + Y. foo(X,Y,Z) :- Y<X, N1 is Y-3, foo(X, N1, Zx), Z is Zx + X. So this is my program and this is what i'm trying to accomplish 𝑓𝑜𝑜(𝑥, 𝑦) = { 𝑥 𝑖𝑓 𝑦 ≤ 0 𝑦 𝑖𝑓 𝑥 ≤ 0 𝑥 + 𝑓𝑜𝑜(𝑥 − 2, 𝑦) 𝑖𝑓 𝑥 ≥ 𝑦 𝑦 + 𝑓𝑜𝑜(𝑥, 𝑦 − 3) 𝑖𝑓 𝑥 < 𝑦 } Why does my program not output anything? This is what i think i'm saying - If X = 0, foo(0,Y,Z), than return Z as Y. If Y = 0, foo(0,Y,Z), than return Z as X. if X>=Y, than do

Sort a list by polar angle in counterclockwise order in Prolog

一世执手 提交于 2019-12-25 03:43:25
问题 I have a List L with some terms like range(2,3), where the first element is the term with the lowest Y, now I need to sort the other elements of the list L (the first element will remain unchanged), I need to sort them by polar angle in counterclockwise order. If polar angle of two points is same, then I need to put the nearest term first. I defined the predicate angle2d/2: angle2d(A, B, R) :- A =.. [_,Ax,Ay], B =.. [_,Bx,By], R is atan2((Ay-By),(Ax-Bx)). that give me the counter clockwise