prolog-dif

Is “almost pure” Prolog expressive?

…衆ロ難τιáo~ 提交于 2021-01-13 09:37:11
问题 @false commented earlier: Yes, you can implement a Turing machine without dif/2 . But you cannot even implement intersection or similar predicates. Suppose we do extend pure Prolog (Horn FOL + CWA + UNA) with call/N , dif/2 , and (=)/3 , to be used in if_/3 , would there still be gaps in its expressiveness, i.e. things that are trivial to define in, say, Scheme, but are much harder to state in such extended (almost pure) Prolog? In particular, does such a Prolog allow manipulating Prolog

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

Get unique results with Prolog

独自空忆成欢 提交于 2020-01-05 10:28:14
问题 I have this Prolog code that returns: [[vincent,vincent],[vincent,marcellus],[marcellus,vincent],[marcellus,marcellus],[pumpkin,pumpkin],[honey_bunny,honey_bunny]] . :- initialization main. loves(vincent, mia). loves(marcellus, mia). loves(pumpkin, honey_bunny). loves(honey_bunny, pumpkin). jealous(X, Y) :- loves(X, Z), loves(Y, Z). main :- findall([X, Y], jealous(X, Y), L), write(L), halt. How to get the only results when X != Y? I tried the following code to get the same results as before.

Program to find every list of X in Prolog

青春壹個敷衍的年華 提交于 2020-01-03 16:01:30
问题 I am starting on learning Prolog. This program tries to get all occurrences of a given element: occurences(_, [], Res):- Res is []. occurences(X, [X|T], Res):- occurences(X,T,TMP), Res is [X,TMP]. occurences(X, [_|T], Res):- occurences(X,T,Res). But here is the error: ?- occurences(a,[a,b,c,a],Res). ERROR: is/2: Arithmetic: `[]/0' is not a function ^ Exception: (11) _G525 is [] ? creep Exception: (10) occurences(a, [], _G524) ? creep Exception: (9) occurences(a, [a], _G524) ? creep Exception:

Why does Prolog crash in this simple example?

旧城冷巷雨未停 提交于 2020-01-03 15:54:54
问题 likes(tom,jerry). likes(mary,john). likes(mary,mary). likes(tom,mouse). likes(jerry,jerry). likes(jerry,cheese). likes(mary,fruit). likes(john,book). likes(mary,book). likes(tom,john). likes(john,X):-likes(X,john), X\=john. Hi there, above is a very simple prolog file, with some facts and only one rule: John likes anyone who likes him. But after loading this file and ask Prolog the following query: likes(john,X). The program crashes. The reason is somehow prolog gets stuck at likes(john,john)

Difference between X\=Y and dif(X,Y)

人盡茶涼 提交于 2019-12-31 01:55:10
问题 What is the difference between this: X \= Y and this piece of code: dif(X, Y) I thought that they should behave the same, but they do not. Here's the example: n_puta(L, N, X) :- nputa(L, N, 0, X). nputa([], N, C, _) :- N = C. nputa([G|R], N, C, X) :- G = X, nputa(R, N, Y, X), C is Y - 1. nputa([G|R], N, C, X) :- dif(G,X), nputa(R, N, C, X). And here are some calls: ?- n_puta([a,a,b,b,b], 2, X). X = a ; false. ?- n_puta([a,a,b,a,b,b], 3, X). X = a ; X = b ; false. X should be the atom that

Remove unique elements only

蓝咒 提交于 2019-12-31 00:37:21
问题 There are many resources on how to remove duplicates and similar issues but I can't seem to be able to find any on removing unique elements. I'm using SWI-Prolog but I don't want to use built-ins to achieve this. That is, calling remove_unique([1, 2, 2, 3, 4, 5, 7, 6, 7], X). should happily result in X = [2, 2, 7, 7] . The obvious solution is as something along the lines of count(_, [], 0) :- !. count(E, [E | Es], A) :- S is A + 1, count(E, Es, S). count(E, [_ | Es], A) :- count(E, Es, A). is

Force Prolog to choose unique values of variables

早过忘川 提交于 2019-12-28 22:03:42
问题 OK I am new to Prolog, so excuse me if this is something trivial, but I can't seem to find a proper elegant answer to this. I am trying to work out the exercise here on learnprolognow.org, exercise 2.4 (the crossword). The exercise provides these facts: word(astante, a,s,t,a,n,t,e). word(astoria, a,s,t,o,r,i,a). word(baratto, b,a,r,a,t,t,o). word(cobalto, c,o,b,a,l,t,o). word(pistola, p,i,s,t,o,l,a). word(statale, s,t,a,t,a,l,e). And the solution I came up with to solve the crossword

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

How to prevent duplicates in generated sequences by using dif/2?

血红的双手。 提交于 2019-12-24 00:15:55
问题 This question came up while answering another question on StackOverflow on (generalizing a bit) generating all sequences formed out of a finite set of elements with no duplicate occurrences. As Boris rightly indicated in the comments, there are many existing solutions to this problem. However, I am interested in a solution that does not use an accumulator (i.e., a list of already picked elements against which a newly selected element is to be compared) but that uses dif/2 statements instead.