failure-slice

Parsing an expression in Prolog and returning an abstract syntax

两盒软妹~` 提交于 2019-12-09 01:40:20
问题 I have to write parse(Tkns, T) that takes in a mathematical expression in the form of a list of tokens and finds T, and return a statement representing the abstract syntax, respecting order of operations and associativity. For example, ?- parse( [ num(3), plus, num(2), star, num(1) ], T ). T = add(integer(3), multiply(integer(2), integer(1))) ; No I've attempted to implement + and * as follows parse([num(X)], integer(X)). parse(Tkns, T) :- ( append(E1, [plus|E2], Tkns), parse(E1, T1), parse

Prolog: check transitivity for simple facts

£可爱£侵袭症+ 提交于 2019-12-08 07:26:43
问题 My intention was to implement a simple example (just for myself) of transitivity in Prolog. These are my facts: trust_direct(p1, p2). trust_direct(p1, p3). trust_direct(p2, p4). trust_direct(p2, p5). trust_direct(p5, p6). trust_direct(p6, p7). trust_direct(p7, p8). trust_direct(p100, p200). I've written this predicate to check whether A trusts C , which is true whenever there is a B that trusts C and A trusts this B : trusts(A, B) :- trust_direct(A, B). trusts(A, C) :- trusts(A, B), trusts(B,

Prolog: check transitivity for simple facts

天大地大妈咪最大 提交于 2019-12-07 15:08:30
My intention was to implement a simple example (just for myself) of transitivity in Prolog. These are my facts: trust_direct(p1, p2). trust_direct(p1, p3). trust_direct(p2, p4). trust_direct(p2, p5). trust_direct(p5, p6). trust_direct(p6, p7). trust_direct(p7, p8). trust_direct(p100, p200). I've written this predicate to check whether A trusts C , which is true whenever there is a B that trusts C and A trusts this B : trusts(A, B) :- trust_direct(A, B). trusts(A, C) :- trusts(A, B), trusts(B, C). The predicate returns true for trusts(p1, p2) or trusts(p1, p5) for example, but trusts(p5, p6)

How does recursion in Prolog works from inside. One example

巧了我就是萌 提交于 2019-12-01 21:07:41
问题 I've got here a small script, that converts list of elements into a set. For example list [1,1,2,3] -> set [1,2,3]. Can somebody explain to me, step by step, whats happening inside those procedures? Can you please use my [1,1,2,3] -> [1,2,3] example? list_to_set([],[]). list_to_set([A|X],[A|Y]):- list_to_set(X,Y), \+member(A,Y). list_to_set([A|X],Y):- list_to_set(X,Y), member(A,Y). 回答1: The procedural counterpart of a prolog program is called SLDNF. Query: list_to_set([1,1,2,3], Result) Now

How does recursion in Prolog works from inside. One example

寵の児 提交于 2019-12-01 18:23:20
I've got here a small script, that converts list of elements into a set. For example list [1,1,2,3] -> set [1,2,3]. Can somebody explain to me, step by step, whats happening inside those procedures? Can you please use my [1,1,2,3] -> [1,2,3] example? list_to_set([],[]). list_to_set([A|X],[A|Y]):- list_to_set(X,Y), \+member(A,Y). list_to_set([A|X],Y):- list_to_set(X,Y), member(A,Y). The procedural counterpart of a prolog program is called SLDNF. Query: list_to_set([1,1,2,3], Result) Now SLDNF tries to match [1,1,2,3] with [A|X] through a procedure called unification. In a few words, it checks

Reversible tree length relation

不想你离开。 提交于 2019-12-01 05:24:57
I'm trying to write reversible relations in "pure" Prolog (no is , cut, or similar stuff. Yes it's homework), and I must admit I don't have a clue how. I don't see any process to create such a thing. We are given "unpure" but reversible arithmetic relations (add,mult,equal,less,...) which we must use to create those relations. Right now I'm trying to understand how to create reversible functions by creating the relation tree(List,Tree) which is true if List is the list of the leaves of the binary tree Tree . To achieve such a thing I'm trying to create the tree_size(Tree,N) relation which is

Prolog - get the factors for a given number doesn't stop?

岁酱吖の 提交于 2019-12-01 05:05:53
问题 I need to find the factors of a given number , e.g : ?- divisors2(40,R). R = [40,20,10,8,5,4,2,1]. The code : % get all the numbers between 1-X range(I,I,[I]). range(I,K,[I|L]) :- I < K, I1 is I + 1, range(I1,K,L). % calc the modulo of each element with the given number : % any x%y=0 would be considered as part of the answer divisors1([],[],_). divisors1([H|T],S,X):-divisors1(T,W,X),Z is X mod H,Z==0,S=[H|W]. divisors1([_|T],S,X):-divisors1(T,S,X). divisors2(X,Result) :-range(1,X,Result1)

Parsing an expression in Prolog and returning an abstract syntax

╄→гoц情女王★ 提交于 2019-12-01 01:03:31
I have to write parse(Tkns, T) that takes in a mathematical expression in the form of a list of tokens and finds T, and return a statement representing the abstract syntax, respecting order of operations and associativity. For example, ?- parse( [ num(3), plus, num(2), star, num(1) ], T ). T = add(integer(3), multiply(integer(2), integer(1))) ; No I've attempted to implement + and * as follows parse([num(X)], integer(X)). parse(Tkns, T) :- ( append(E1, [plus|E2], Tkns), parse(E1, T1), parse(E2, T2), T = add(T1,T2) ; append(E1, [star|E2], Tkns), parse(E1, T1), parse(E2, T2), T = multiply(T1,T2)

Prevent backtracking after first solution to Fibonacci pair

北战南征 提交于 2019-11-30 10:08:19
The term fib(N,F) is true when F is the N th Fibonacci number. The following Prolog code is generally working for me: :-use_module(library(clpfd)). fib(0,0). fib(1,1). fib(N,F) :- N #> 1, N #=< F + 1, F #>= N - 1, F #> 0, N1 #= N - 1, N2 #= N - 2, F1 #=< F, F2 #=< F, F #= F1 + F2, fib(N1,F1), fib(N2,F2). When executing this query (in SICStus Prolog), the first (and correct) match is found for N (rather instantly): | ?- fib(X,377). X = 14 ? When proceeding (by entering ";") to see if there are any further matches (which is impossible by definition), it takes an enormous amount of time (compared

Prolog predicate - infinite loop

喜你入骨 提交于 2019-11-28 13:47:18
I need to create a Prolog predicate for power of 2, with the natural numbers. Natural numbers are: 0, s(0), s(s(0)) ans so on.. For example: ?- pow2(s(0),P). P = s(s(0)); false. ?- pow2(P,s(s(0))). P = s(0); false. This is my code: times2(X,Y) :- add(X,X,Y). pow2(0,s(0)). pow2(s(N),Y) :- pow2(N,Z), times2(Z,Y). And it works perfectly with the first example, but enters an infinite loop in the second.. How can I fix this? This happends because the of evaluation order of pow2. If you switch the order of pow2, you'll have the first example stuck in infinite loop.. So you can check first if Y is a