prolog

Prolog recursion and building output from recursive calls

别说谁变了你拦得住时间么 提交于 2019-12-19 04:21:59
问题 I am learning Prolog via http://www.learnprolognow.org and I am having some trouble understanding how to recursively build up a variable with results from another recursive call, as per Practical Session 3.4, question 3. The initial problem is a straight-forward recursive call to determine if a route is feasible. But the follow-on problem asks you to show the actual path to get to the end of the route. We are given the following knowledge base of travel information: byCar(auckland,hamilton).

Verifying a signature chain SWI-Prolog

那年仲夏 提交于 2019-12-19 04:08:08
问题 This question is related to Opening and checking a Pem file in SWI-Prolog Once I have downloaded and opened the certificates how do I verify the signature chain? I have: :-use_module(library(http/http_client)). url('https://s3.amazonaws.com/echo.api/echo-api-cert-4.pem'). url_data1(Url,Certs):- http_open(Url,Stream,[]), all_certs(Stream,Certs), forall(member(C,Certs),my_validate(C)), close(Stream). all_certs(Stream,[C1|Certs]):- catch(load_certificate(Stream,C1),_,fail), all_certs(Stream

Extension to CFG, what is it?

限于喜欢 提交于 2019-12-19 02:22:19
问题 Consider the following extension to context-free grammars that permits rules to have in the left-hand side, one (or more) terminal on the right side of the non-terminal. That is, rules of the form: A b -> ... The right-hand side may be anything, like in context-free grammars. In particular, it is not required, that the right-hand side will have exactly the same terminal symbol at the end. In that case, this extension would be context-sensitive. But the terminal is not just a context.

Prolog: How to check if a predicate exists?

百般思念 提交于 2019-12-18 21:55:38
问题 How can I check if a predicate exists in a Prolog program? That would be an exists/1 , like: ?- exists(some_predicate). false. ?- assert(some_predicate). true. ?- exists(some_predicate). true. 回答1: You can use current_predicate/1, current_predicate/2 or predicate_property/2 (for the last you will probably need functor/3): ?- current_predicate(a/1). false. ?- functor(A,a,1),predicate_property(A,visible). false. ?- functor(A,a,1),current_predicate(_,A). false. ?- assert(a(42)). true. ?- current

Prolog: How to check if a predicate exists?

爷,独闯天下 提交于 2019-12-18 21:55:17
问题 How can I check if a predicate exists in a Prolog program? That would be an exists/1 , like: ?- exists(some_predicate). false. ?- assert(some_predicate). true. ?- exists(some_predicate). true. 回答1: You can use current_predicate/1, current_predicate/2 or predicate_property/2 (for the last you will probably need functor/3): ?- current_predicate(a/1). false. ?- functor(A,a,1),predicate_property(A,visible). false. ?- functor(A,a,1),current_predicate(_,A). false. ?- assert(a(42)). true. ?- current

Prolog: How to tell if a predicate is deterministic or not

回眸只為那壹抹淺笑 提交于 2019-12-18 15:54:31
问题 So from what I understand about deterministic predicates: Deterministic predicate = 1 solution Non-deterministic predicate = multiple solutions Are there any type of rules as to how you can detect if the predicate is one or the other? Like looking at the search tree, etc. 回答1: There is no clear, generally accepted consensus about these notions. However, they are usually based rather on the observed answers and not based on the number of solutions. In certain contexts the notions are very

Max out of values defined by prolog clauses

北城余情 提交于 2019-12-18 13:48:37
问题 I know how to iterate over lists in Prolog to find the maximum, but what if each thing is a separate clause? For example if I had a bunch of felines and their ages, how would I find the oldest kitty? cat(sassy, 5). cat(misty, 3). cat(princess, 2). My first thought was "hmm, the oldest cat is the one for which no older exists". But I couldn't really translate that well to prolog. oldest(X) :- cat(X, AgeX), cat(Y, AgeY), X \= Y, \+ AgeX < AgeY, print(Y). This still errorenously matches "misty".

How to access list permutations in prolog?

风格不统一 提交于 2019-12-18 13:38:30
问题 I want to access list permutation and pass it as argument to other functions. This is the permutation code: takeout(X,[X|R],R). takeout(X,[F|R],[F|S]) :- takeout(X,R,S), write(S). perm([X|Y],Z) :- perm(Y,W), takeout(X,Z,W). perm([],[]). 回答1: To start with, let's redefine your predicates so they don't do any unnecessary I/O: takeout(X,[X|R],R). takeout(X,[F |R],[F|S]) :- takeout(X,R,S). perm([X|Y],Z) :- perm(Y,W), takeout(X,Z,W). perm([],[]). Now you have what could be considered a "pure"

Prevent backtracking after first solution to Fibonacci pair

天大地大妈咪最大 提交于 2019-12-18 13:36:31
问题 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

Beginner in PROLOG [closed]

大憨熊 提交于 2019-12-18 13:35:35
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 7 years ago . What book would you recommend for a beginner in PROLOG? I currently know Perl. 回答1: Clocksin, Mellish – Programming in Prolog And some