prolog

How to catch exception to backtrack(or Delimited Continuations) in Prolog?

痴心易碎 提交于 2020-01-24 06:21:30
问题 I'm new to Prolog and trying to know some example for catch/3. The code I want to run is here. (Using swi-prolog v8.0.3) mylib.pl mylib([1,2]). mylib(1). test(X):- mylib(X), length([1],X). then consult it in swipl then run the following command. catch(test(X),error(Err,_Context),format('catch')). So here I want to catch the typeError exception and get X = 1 . But the result is catching the exception and nothing continues. So what should I do to reach my target? Running messages here. catch

Time limit in Prolog user input (read)

蹲街弑〆低调 提交于 2020-01-24 04:14:45
问题 I'm writing an interpreter for a game. User enters its move to the interpreter and program executes that move. Now I want to implement a time limit for each decision. Player shouldn't be able to think more than 30 seconds to write a move and press enter. call_with_time_limit seemed relevant but it doesnt work properly as such: call_with_time_limit( 30, read(X) ), Problem, write(Problem). In this case, it waits for input, and when input is entered, timer starts afterwards. But I want the timer

Why double negation doesn't bind in Prolog

前提是你 提交于 2020-01-24 02:53:28
问题 Say I have the following theory: a(X) :- \+ b(X). b(X) :- \+ c(X). c(a). It simply says true, which is of course correct, a(X) is true because there is no b(X) (with negation as finite failure). Since there is only a b(X) if there is no c(X) and we have c(a) , one can state this is true. I was wondering however why Prolog does not provide the answer X = a ? Say for instance I introduce some semantics: noOrphan(X) :- \+ orphan(X). orphan(X) :- \+ parent(_,X). parent(david,michael). Of course

Why double negation doesn't bind in Prolog

守給你的承諾、 提交于 2020-01-24 02:53:10
问题 Say I have the following theory: a(X) :- \+ b(X). b(X) :- \+ c(X). c(a). It simply says true, which is of course correct, a(X) is true because there is no b(X) (with negation as finite failure). Since there is only a b(X) if there is no c(X) and we have c(a) , one can state this is true. I was wondering however why Prolog does not provide the answer X = a ? Say for instance I introduce some semantics: noOrphan(X) :- \+ orphan(X). orphan(X) :- \+ parent(_,X). parent(david,michael). Of course

prolog: maximally repeated element in a list

妖精的绣舞 提交于 2020-01-23 13:02:53
问题 Any ideas how to retrieve the maximally repeated element in a list. i.e. something like below, ?- maxRepeated([1,2,7,3,6,1,2,2,3],M). M = 2. 回答1: This solution sorts the list, granting elements to appear sequentially -- there's no need to maintain all elements, once they're not repeating later. Your prolog interpreter must have the function msort() , which sorts a list maintaining duplicated entries. maxRepeated([], []). maxRepeated(L, E) :- msort(L, [H|T]), maxRepeated(T, H, H, 1, 0, E).

Prolog length of a list

依然范特西╮ 提交于 2020-01-21 13:27:01
问题 How can I calculate the length of a list ?- size_sub([[b,a,g], [9,3,7,4], [6]], X). X = [3, 4, 1]. ?- size_sub([[c,g,e,w], [7]], X). X = [4, 1]. ?- size_sub([], X). X = []. 回答1: To map length/2 over a list of lists, we can use the meta-predicate maplist/3 like this: size_sub(Xss,Ls):- maplist(length,Xss,Ls). 回答2: Ok you need to start with the base case which is the last answer so size_sub([],X). is true if X=[] so first you write that as a rule. size_sub([],[]). Then you need to do the

Prolog length of a list

喜欢而已 提交于 2020-01-21 13:26:22
问题 How can I calculate the length of a list ?- size_sub([[b,a,g], [9,3,7,4], [6]], X). X = [3, 4, 1]. ?- size_sub([[c,g,e,w], [7]], X). X = [4, 1]. ?- size_sub([], X). X = []. 回答1: To map length/2 over a list of lists, we can use the meta-predicate maplist/3 like this: size_sub(Xss,Ls):- maplist(length,Xss,Ls). 回答2: Ok you need to start with the base case which is the last answer so size_sub([],X). is true if X=[] so first you write that as a rule. size_sub([],[]). Then you need to do the

Applying semicontext notation for passing additional arguments

拜拜、爱过 提交于 2020-01-21 12:17:08
问题 This is a follow-on question from an earlier question from Mat's answer Starting with this e([number(0)] , t1 , Uc0 , Uc0, Bc0 , Bc0) --> []. e([number(1)] , t2 , Uc0 , Uc0, Bc0 , Bc0) --> []. e([number(2)] , t3 , Uc0 , Uc0, Bc0 , Bc0) --> []. e([op(neg),[Arg]] , u1(E) , [_|Uc0], Uc1, Bc0 , Bc1) --> [_], e(Arg , E , Uc0, Uc1, Bc0, Bc1). e([op(ln),[Arg]] , u2(E) , [_|Uc0], Uc1, Bc0 , Bc1) --> [_], e(Arg , E , Uc0, Uc1, Bc0, Bc1). e([op(add),[Left,Right]], b1(E0,E1) , Uc0 , Uc2, [_|Bc0], Bc2) -

Is there a way or an algorithm to convert DCG into normal definite clauses in Prolog?

不想你离开。 提交于 2020-01-21 07:15:33
问题 I am a newbie in Prolog, and I am trying to understand how a grammar can be translated into a normal definite clause from a DCG. I understood that DCG notation is just syntactic sugar for normal definite clauses in Prolog. I started to depict some similarities between the normal definite grammars, and the DCGs, but failed to apply the same pattern, so I am asking is there some rules that I am missing or an algorithm of conversion that might work. Here is the grammar that I am working on, and

Hanoi Tower(Towers of Hanoi)

十年热恋 提交于 2020-01-20 06:59:08
问题 I'm trying to do the Towers of Hanoi problem, what I have tried so far: move(1,[H|T],B,C,A1,B1,C) :- A1 = T, B1 = [H|B]. move(N,A,B,C,A1,B1,C) :- N>1, M is N-1, move(M,[H|T],C,B,A1,B1,C), move(1,[H|T],B,_,A1,B1,C), move(M,C,B,[H|T],A1,B1,C). but this code does not work, I need get the result is looks like this: ?-move(3,[1,2,3],[],[],A1,B1,C). and the results: A1=[]. B1=[1,2,3] C=[]. can someone help me fix my code up and can get the result like that? This is very important for me, I really