prolog

Prolog - Reduce the knowledge base by deduction

心不动则不痛 提交于 2020-01-04 07:37:28
问题 I need to create a rule that will search for the facts that matches my_rule. These facts will be use to change the knowledge base. ( my_rule (Conclusion, Premise) ). I have this knowledge base to start with : :- dynamic( is/2 ). is( m1, house ). is( m1, thing ). is( m2, house ). is( m2, thing ). is( m3, niche ). is( m3, house ). is( m3, thing ). is( m4, car ). is( m4, mobile ). is( m4, house ). is( m4, thing ). my_rule( is( X, thing ), is( X, house ) ). my_rule( is( X, house ), is( X, niche )

Prolog : eliminating cycles from indirect relation

戏子无情 提交于 2020-01-04 06:21:06
问题 I have a list of user facts defined as: user(@michael). user(@ana). user(@bob). user(@george). user(@john). and so on. Furthermore, I have a set of facts as: follows(@michael,@ana). follows(@ana,@bob). follows(@bob,@michael). I am trying to write a relation indirect(user1,user1) which will tell me if user1 indirectly follows user2. However, I am not able to do away with cyclic relations. Like in the given example, michael -> ana -> bob -> michael will cause a cycle. What is the best way to

How can I assign multiple values to a variable (like a string) in Prolog?

*爱你&永不变心* 提交于 2020-01-04 05:55:11
问题 Earlier today I asked for help for building a database in prolog and how to search by parameters, and somebody came up with this: You can also add a list of terms to every processor, like: processor(pentium_g4400, [brand('intel'), family('pentium'), series('g4400'), clock(3.3), socket('lga1151'), ram('ddr4'), cores(2), threads(2)]). In that case you can query with: processor(Proc, Specs), member(family('pentium'), Specs). And it worked pretty well, but, the command "member" does not seem to

How does this compute ? I am trying to understand how the values of H get assigned in the list

你离开我真会死。 提交于 2020-01-04 05:20:49
问题 This predicate should print a list of size N containing possible permutations of 0 and 1 . My question is : does the value of H get carried over with each recursion or does the creation of the list with values of bit(H) take place in the backtracking phase? bit(0). bit(1). gen(0,[]). gen(N,[H|T]) :- N > 0, bit(H), N1 is N - 1, gen(N1,T). 回答1: Prolog execution is all about choice points. Here a choice point is left at each recursion step by the bit/1 predicate. When you ask Prolog to give you

Prolog, Determine if graph is acyclic

倖福魔咒の 提交于 2020-01-04 05:20:15
问题 I need to define a predicate acyclic/1 that takes a graph in as input and determine if that graph is acyclic. So from my understanding graph1(a,b). graph1(b,c). graph1(c,a). Will return no and graph2(a,b). graph2(b,c). will return yes I made a predicate to determine if 2 nodes in a graph are connected and if so they will return yes. isConnected(X,Y) :- a(X,Z), isConnected(Z,Y). is there a way that I can use this to determine if a graph is acyclic? I do not want to use any predefined

Why doesn't this clpfd query terminate until I add a redundant constraint?

假装没事ソ 提交于 2020-01-04 04:13:05
问题 I've written some predicates which take the length of a list and attaches some constraints to it (is this the right vocabulary to be using?): clp_length([], 0). clp_length([_Head|Rest], Length) :- Length #>= 0, Length #= Length1 + 1, clp_length(Rest, Length1). clp_length2([], 0). clp_length2([_Head|Rest], Length) :- Length #= Length1 + 1, clp_length2(Rest, Length1). The first terminates on this simple query, but the second doesn't: ?- Small in 1..2, clp_length(Little, Small). Small = 1,

Reading a user-input string in prolog

人盡茶涼 提交于 2020-01-04 02:49:05
问题 I am beginner in Prolog. I am using swi prolog(just started using it) and I need to split a user input string into a list. I tried the following code, but I get an error stating that 'Full stop in clause-body?Cannot redefine ,/2' write('Enter the String'),nl,read('I'). tokenize("",[]). tokenize(I,[H|T]):-fronttoken(I,H,X),tokenize(X,T). Can someone help me with this pls... 回答1: From your error message, it's apparent you're using SWI-Prolog. Then you can use its library support: ?- read_line

How to trace backtracks of clpfd in prolog?

自闭症网瘾萝莉.ら 提交于 2020-01-04 02:22:08
问题 I am making a sudoku solver with prolog using clpfd library . I have to trace the backtracks and every squares labeled with row and column and the number it gets in the following form: (1 ,1 ,1) (9 ,2 ,1) BT (5 ,2 ,1) My question is how can I get the above information from the algorithm? Another question: Does the algorithm observe arc-consistency rules by itself? 回答1: I don't think this is a particularly good idea, but here is something using SWI-Prolog's attributed variables that prints the

Prolog - Describe facts and rules

狂风中的少年 提交于 2020-01-04 02:03:49
问题 I want to describe in prolog the following facts and rules: Nick is programming in Java. Nick is programming in Python Nick is friend with anyone that is programming in Java and Python Jim is programming in all languages that Nick does. I found the solution for 1, 2 and 3 but not for the 4th, even though i would really appreciate a full solution. My solution: male(Nick). male(Jim). programming(Nick, java). programming(Nick, python). friends(X,Y):- programming(X,java), programming(X,python),

Accumulating while in recursion/backtracking

强颜欢笑 提交于 2020-01-04 01:55:07
问题 I run into this beginners problem, and i don't know how to solve this. Here is my code: worker( w1, d1, 2000 ) . worker( w2, d1, 2500 ) . worker( w2, d2, 1000 ) . worker( w3, d2, 2000 ) . worker( w4, d2, 4000 ) . % worker( W, D, S ) means that worker W works in department D and has salary S department( d1, w2 ) . department( d2, w4 ) . % department( D, B ) means that worker B is director of department D(this is not important in this case) I need to get sum of all salaries form one of