prolog

prolog relation and comparison

孤街浪徒 提交于 2020-01-13 19:39:11
问题 Let said I have these relations drive(adam, van). drive(betty, tank). drive(adam, truck). drive(adam, convertible). how do I write a condition to find out adam drives three different vehicles? I tried this, but this didn't work. drivethree(A):- drive(A, X), drive(A, Y), drive(A, Z), X = not(Y), X = not(Z), Y = not(Z). 回答1: At first, you might want to understand why your program does not work. After all, this might not be the only program that causes problems. Identify incorrect test cases The

Replacing parts of expression in prolog

ⅰ亾dé卋堺 提交于 2020-01-13 13:13:08
问题 I need to simplify identities in prolog (e.g. x+0 = x , x-x=0 , etc.). For this I need to replace parts of the expression (say x+0 by x ). Can you please help me in doing the replacement? 回答1: A neat thing about Prolog is that you can deconstruct an arithmetic expression pretty easily. Your basic template is going to look like this: simplify(X, X) :- number(X) ; atom(X) ; var(X). simplify(X+Y, X1+Y1) :- simplify(X, X1), simplify(Y, Y1). simplify(X-Y, X1-Y1) :- simplify(X, X1), simplify(Y, Y1)

How to use JPL (bidirectional Java/Prolog interface) on windows?

不羁的心 提交于 2020-01-13 10:17:14
问题 I'm interested in embedding a Prolog interpreter in Java. One option is using JPL, but the download links on the JPL site are broken, and the installation page mentions a jpl.zip that I can't find. I downloaded SWI-Prolog which seems to include JPL (it lists it as a component when installing), but I'm still not sure how I'd use it along with Java. Any ideas on how to use JPL on Windows? Is there another library I could use to achieve the same thing? I've come across a few but they don't seem

Prolog DCG set_prolog_flag double_quotes source code directive location matters; documentation?

北慕城南 提交于 2020-01-13 09:22:09
问题 I learned the hard way that with SWI-Prolog the location for the Prolog directive set_prolog_flag matters in a source code file. The only documentation I found of value about loading source code files with directives was in Loading Prolog source files A directive is an instruction to the compiler. Directives are used to set (predicate) properties (see section 4.15), set flags (see set_prolog_flag/2) and load files (this section). Directives are terms of the form :- <term>. Is there

What are the problems associated to Best First Search in Artificial intelligence?

一曲冷凌霜 提交于 2020-01-13 08:39:11
问题 I Know general issues include local maxima and plateaus however I am curious if there is any more issues associated to this specific search and what my best course of action would be in order to overcome these issues. Can someone also give me an example of which sort of problem this search would be good to use for? 回答1: Problems with best first search: It is greedy. In many cases it leads to a very quick solution (because your number of developed nodes does not increase exponentially, it is

Check if any element's frequency is above a limit

痴心易碎 提交于 2020-01-13 05:17:10
问题 I want to solve a problem that is I have a Prolog list of elements. If the any of the element frequency is greater than N then false is return. My expectation like below. ?- frequency([1,2,2,2,5],3). true. ?- frequency([1,2,2,2,2,5],3). false. I have a code for get particular element frequency. Any idea for the problem. count(_, [], 0) :- !. count(X, [X|T], N) :- count(X, T, N2), N is N2 + 1. count(X, [Y|T], N) :- X \= Y, count(X, T, N). 回答1: Use clpfd! :- use_module(library(clpfd)). If we

Getting all the solutions to a predicate in Prolog

独自空忆成欢 提交于 2020-01-13 02:55:49
问题 I'm writing a text adventure game in Prolog, and I am printing out room exits. I have code that does: exits_from(Room) :- connected(Room, X), write(X), write(' '). where connected/2 is: connected(X, Y) :- path(X, Y). connected(X, Y) :- path(Y, X). and path is: path(room, hallway). path(hallway, foyer). and so on. When I am printing the exits for a room though, it gets the first, then wants a ';' to say that I want another solution. Is there anyway to force a predicate to compute the result

Has the notion of 'semidet' in Prolog settled?

非 Y 不嫁゛ 提交于 2020-01-12 06:45:25
问题 Being new to Prolog, I came across a very interesting discussion which happened in late 2012. What I noticed was that there were, at the time, two notions of 'semidet' in Prolog community, namely: A computation which succeeds at most once. A computation which, upon success, leaves no choice points open. Clearly the second one implies the first, but not vice versa. Reading the thread, I understood that the first one was Dr.Neumerkel's notion, and the second was Drs.Wielemaker, O'Keefe, and

How read a file and write another file in prolog

徘徊边缘 提交于 2020-01-11 13:42:28
问题 I would like to read a file, modify lines and write the results to another file. readtofile :- open('inputfile.txt', read, Str), read_file(Str,Lines), close(Str). read_file(Stream) :- at_end_of_stream(Stream). read_file(Stream) :- \+ at_end_of_stream(Stream), read(Stream), modify(Stream,Stream2), write_file(Stream2), read_file(Stream). write_file('outputfile.txt', Phrase) :- open('outputfile.txt', write, Stream), writeln(Stream, Phrase), close(Stream). 回答1: I would write something like

Prolog: eliminate repetitions in query

对着背影说爱祢 提交于 2020-01-11 13:33:10
问题 I've been trying to write a simple code, that would behave in this manner: | ?- hasCoppiesOf(X,[a,b,a,b,a,b,a,b]). X = [a,b] ? ; X = [a,b,a,b] ? ; X = [a,b,a,b,a,b,a,b] ? ; And | ?- hasCoppiesOf([a,b,a,b,a,b,a,b], X). X = [] ? ; X = [a,b,a,b,a,b,a,b] ? ; X = [a,b,a,b,a,b,a,b,a,b,a,b,a,b,a,b] ? ; X = ... This desire resulted in next piece of code: hasCoppiesOf(A,[]). hasCoppiesOf([H1|T1], [H1|T2]) :- append(T1, [H1], X), hasCoppiesOf([H1|T1], X, T2). hasCoppiesOf(A, A, B) :- hasCoppiesOf(A, B)