prolog-toplevel

What's the difference between “false” and “no” in Prolog

房东的猫 提交于 2019-12-10 14:47:21
问题 I started to learn Prolog following the book Programming in Prolog: Using the ISO Standard. At page 7 of the intro to the language they made the assertion : "In Prolog the answer no is used to mean nothing unifies with the question . It is important to remember that no is not the same as false ". So why SWI-Prolog uses the false and true statement instead of yes or no ? 回答1: To begin with, the ISO standard (ISO/IEC 13211-1:1995) does not define a toplevel loop. In 1 Scope it reads: NOTE —

Prolog gives error “undefined procedure” when trying to use :-

谁说我不能喝 提交于 2019-12-04 00:15:57
I'm using SWI-Prolog on Windows and am getting the following error: 14 ?- parent(X, Y) :- child(Y, X). ERROR: toplevel: Undefined procedure: (:-)/2 (DWIM could not correct) I'm not entirely sure what's going on, as this worked last week and I am just starting to learn Prolog. The FAQ says it all: http://www.swi-prolog.org/FAQ/ToplevelMode.html You need to create a file and write your program with rules there. The top level command line will only allow you to issue queries. You can try it this way 1 ?- assert(a(A,B):-A=B). true. 2 ?- a(B,c). B = c. 来源: https://stackoverflow.com/questions

Why does SWI-Prolog only give me the first answer?

孤人 提交于 2019-12-01 03:43:05
I'm new to Prolog. I'm just trying simple examples to learn. I have this .pl file with these lines: parent(pam,bob). parent(tom,bob). parent(tom,lio). parent(bob,ann). parent(bob,pat). parent(pat,jim). After consulting and testing, it only shows the first answer. For example: 5 ?- parent(X,Y). X = pam, Y = bob . Isn't it supposed to give all the combinations that satisfy the relation parent ? Do anyone have idea what the problem is? don't hit enter after your first results shows, use spacebar instead [Enter] stops execution even if the backtracking is not completed yet [Spacebar] or [;]

How to expand a resulting list in SWI-Prolog?

爱⌒轻易说出口 提交于 2019-11-30 23:36:52
?- 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. 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), max_depth(10), priority(699)]. ?- set_prolog_flag(toplevel_print_options, [quoted(true), portray(true), max

Why is this prolog query both true and false?

那年仲夏 提交于 2019-11-30 06:13:00
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? (Note: this answer is somewhat of a guess) Consider how Prolog determines whether f(a,c) is true or not. It checks the first rule, f(a,b) , and doesn't find a match,

After the first answer, Prolog shows the error “char_code/2: Cannot represent due to 'character_code'”

﹥>﹥吖頭↗ 提交于 2019-11-29 16:21:40
In normal situation, we can use ";" to show the next answer if there is one. But if I do this, it shows me error: char_code/2: Cannot represent due to 'character_code' In stead of ";" , I use "shift + ;" , and prolog gives me a prompt Unknown action: : (h for help) Action? then if I input ";" , the designable answers will be shown one by one. What is the problem? Use swipl-win.exe instead of swipl.exe can solve this problem.(I tried 6.x and 7.x) By the way, swipl works well under MacOS. 来源: https://stackoverflow.com/questions/23133168/after-the-first-answer-prolog-shows-the-error-char-code-2

Prolog anonymous variable

半世苍凉 提交于 2019-11-29 09:34:51
Here is what I have understood about Prolog variables. A single underscore stands for anonymous variable, which is like a new variable each time it occurs. A variable name starting with underscore like _W is not an anonymous variable. Or, the variable names generated inside Prolog, like _G189, is not considered anonymous: ?- append([1,2],X,Y). X = _G189 Y = [1, 2|_G189] Could you please help me understand? By the way, I got the above example from some tutorials, but when I run it in SWI-Prolog version 6, I get the following: ?- append([1,2],X,Y). Y = [1, 2|X]. Thanking you. Variables The

After the first answer, Prolog shows the error “char_code/2: Cannot represent due to 'character_code'”

放肆的年华 提交于 2019-11-28 10:05:46
问题 In normal situation, we can use ";" to show the next answer if there is one. But if I do this, it shows me error: char_code/2: Cannot represent due to 'character_code' In stead of ";" , I use "shift + ;" , and prolog gives me a prompt Unknown action: : (h for help) Action? then if I input ";" , the designable answers will be shown one by one. What is the problem? 回答1: Use swipl-win.exe instead of swipl.exe can solve this problem.(I tried 6.x and 7.x) By the way, swipl works well under MacOS.

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

可紊 提交于 2019-11-28 01:55:22
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 Prolog's natural backtracking feature. methodtwo takes advantage of backtracking to do the recursion for me,

Implementing “last” in Prolog

萝らか妹 提交于 2019-11-27 05:34:17
I am trying to get a feel for Prolog programming by going through Ulle Endriss' lecture notes . When my solution to an exercise does not behave as expected, I find it difficult to give a good explanation. I think this has to do with my shaky understanding of the way Prolog evaluates expressions. Exercise 2.6 on page 20 calls for a recursive implementation of a predicate last1 which behaves like the built-in predicate last . My attempt is as follows: last1([_ | Rest], Last) :- last1(Rest, Last). last1([Last], Last). It gives the correct answer, but for lists with more than one element, I have