prolog-toplevel

Prolog program returns false

∥☆過路亽.° 提交于 2020-06-09 06:26:29
问题 I implemented the following power program in Prolog: puissance(_,0,1). puissance(X,N,P) :- N>0,A is N-1, puissance(X,A,Z), P is Z*X. The code does what is supposed to do, but after the right answer it prints "false.". I don't understand why. I am using swi-prolog. 回答1: You can add a cut operator (i.e. ! ) to your solution, meaning prolog should not attempt to backtrack and find any more solutions after the first successful unification that has reached that point. (i.e. you're pruning the

Get multiple solutions in SWI-Prolog

白昼怎懂夜的黑 提交于 2020-01-14 10:32:21
问题 I'm beginner in SWI-Prolog (but have some experience in Borland Prolog), and I've faced with a strange behavior for the following test code: test(10). test(1). It is expected for query ?-test(A) to get 2 solutions, something like A = 10; A = 1 . However, only A = 10 is produced. I don't use the cut here. Maybe backtracking is off by default in SWI-Prolog? Thanks in advance 回答1: Sorry, the answer is very simple (see SWI-Prolog doc): The user can type the semi-colon (;) or spacebar, if (s)he

How to expand a resulting list in SWI-Prolog?

旧时模样 提交于 2019-12-30 07:04:31
问题 ?- length(L,25). L = [_G245, _G248, _G251, _G254, _G257, _G260, _G263, _G266, _G 269|...]. If I use write(L) following the length predicate then the interpreter prints the list twice, one expanded and the other not. 回答1: There is a limit on the depth to prevent too long output. You can change it with set_prolog_flag/1. ?- length(L, 25). L = [_G257, _G260, _G263, _G266, _G269, _G272, _G275, _G278, _G281|...]. ?- current_prolog_flag(toplevel_print_options, V). V = [quoted(true), portray(true),

How to expand a resulting list in SWI-Prolog?

别等时光非礼了梦想. 提交于 2019-12-30 07:04:09
问题 ?- length(L,25). L = [_G245, _G248, _G251, _G254, _G257, _G260, _G263, _G266, _G 269|...]. If I use write(L) following the length predicate then the interpreter prints the list twice, one expanded and the other not. 回答1: There is a limit on the depth to prevent too long output. You can change it with set_prolog_flag/1. ?- length(L, 25). L = [_G257, _G260, _G263, _G266, _G269, _G272, _G275, _G278, _G281|...]. ?- current_prolog_flag(toplevel_print_options, V). V = [quoted(true), portray(true),

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

What are the pros and cons of using manual list iteration vs recursion through fail

别来无恙 提交于 2019-12-28 06:56:46
问题 I come up against this all the time, and I'm never sure which way to attack it. Below are two methods for processing some season facts. What I'm trying to work out is whether to use method 1 or 2, and what are the pros and cons of each, especially large amounts of facts. methodone seems wasteful since the facts are available, why bother building a list of them (especially a large list). This must have memory implications too if the list is large enough ? And it doesn't take advantage of

up,down,left and right cannot be used when using SWI-Prolog in terminal

北城以北 提交于 2019-12-24 15:19:35
问题 I met a problem when I use prolog(swipl), after I type swipl in the terminal, code like '^[[A^[[B^[[D^[[C' shows when I press up, down, left, and right on my keyboard. Could somebody explain why? and how to fix it. Thanks in advance! 回答1: This means the readline/editline functionality isn't working. If you built it yourself, you probably have to install the development version of one of those libraries first and then rebuild. Or you can install rlwrap and do rlwrap swipl (which is helpful for

Prolog list not printing all the elements on console

陌路散爱 提交于 2019-12-24 03:07:39
问题 I am using SWI-PROLOG version 6.6.6 I want to print all the attributes of a particular predicate type. I have a predicate called law with arity 2. Some of the facts are law(borrow,'To borrow Money on the credit of the United States'). law(commerce,'To regulate Commerce with foreign Nations, and among the several States, and with the Indian Tribes'). law(unifomity,'To establish an uniform Rule of Naturalization, and uniform Laws on the subject of Bankruptcies throughout the United States').

Prolog. In a query, how to put a condition on a variable that I do not want in the results?

拟墨画扇 提交于 2019-12-23 09:10:53
问题 Imagine that I have the following knowledge base which gives for each person his first name and his age. person(mary, 39). person(john, 24). person(sandy, 17). Now, I want to retrieve all the persons that are older than 20 years. Furthermore, I just want to collect their first names and not their age. Here, I want to retrieve mary and john . How to do this generally in Prolog and more specifically in SWI-Prolog? If we use a variable which is not anonymous, like: ?- person(X, Y), Y > 20.

Understanding rules - false as answer

☆樱花仙子☆ 提交于 2019-12-22 08:29:47
问题 I am new in Prolog and I was just thinking that why this rule giving me false result after one true. likes(1,banana). likes(1,mango). test :- likes(1,banana),likes(1,mango). ?- test. true; false. I want to know the reason behind this false. 回答1: The way prolog works is by evaluating queries until a fail by negation. Here, you've established two facts: likes(1, banana). which says "1 likes banana" likes(1, mango). which says "1 likes mango" Then you have established a rule, which basically