prolog

Prolog: define logical operator in Prolog as placeholder for other operator

↘锁芯ラ 提交于 2021-01-28 07:06:26
问题 my aim is to write a little prove assistant in prolog. My first step is to define the logical connectives as follows: :-op(800, fx, -). :-op(801, xfy, &). :-op(802, xfy, v). :-op(803, xfy, ->). :-op(804, xfy, <->). :-op(800, xfy, #). The last operator # just has the meaning to be the placeholder for & , v , -> or <-> . My problem is, I don't know how I it is possible to define this in prolog. I tried to solve my problem in the following way: X # Y :- X v Y; X & Y; X -> Y; X <-> Y. but the

Prolog: how to non-uniformly randomly select a element from a list?

佐手、 提交于 2021-01-28 05:53:53
问题 Does anyone have any Prolog code to non-uniformly select a random element from a list? I want to replicate the functionality of numpy.random.choice when given the probabilities associated with each entry in the input list. 回答1: I found nothing useful in library(random) . Here's my implementation choice(Xs, Ps, Y) : choice([X|_], [P|_], Cumul, Rand, X) :- Rand < Cumul + P. choice([_|Xs], [P|Ps], Cumul, Rand, Y) :- Cumul1 is Cumul + P, Rand >= Cumul1, choice(Xs, Ps, Cumul1, Rand, Y). choice([X]

Prolog- return index of an element

你。 提交于 2021-01-28 03:51:28
问题 I have been stumped for 3 hours now on this problem, I need to find the index of (A,B,C) where A is the index positions of B in list C (or -1 if not in the list). This is what I have so far, indexof(A,0,[A|_]). indexof(A,B,[_|C]):- Y is B-1, indexof(A,Y,C). it gives the element at the index spot B , which is not what I want. indexof(A,1,[1]). should return A=0;A=-1. I am horrible at Prolog, I've done Java my whole life, so please also provide explanations. 回答1: You can use the builtin

How to find the index of the permutation

匆匆过客 提交于 2021-01-28 02:51:01
问题 % index(+List, -Idx) Predicate will get List with permutation and I want to know index of permutation For example: ?- index([4,1,3,2],X). X= 19. My solution: index([],0). index([_],1). index([X,Y],2):- Y > X. index([H,X|T],Idx):-index([X|T],Idx+1),H > X. Why is it wrong? And how can I make incremention of Idx? 回答1: I found cleaner version of same idea so I show the code: permutation_index([X|Xs], I) :- prerequisite( ( sort([X|Xs], S), length([X|Xs], Len), length(S, Len) )), permutation_index

Is Prolog really based on the closed-world assumption?

落花浮王杯 提交于 2021-01-28 02:50:24
问题 Under the closed-world assumption, what is not currently known to be true, is false Prolog's semantics is often said to follow the closed-world assumption, for example, here: Prolog is based on closed world assumption (CWA) -- that is, if a proposition is not in the fact database and not derivable from the fact database, then it is not true. However, it doesn't quite behave this way. Under CWA, I'd expect ?- a. false. But instead, in SWI-Prolog, I get: ?- a. ERROR: Undefined procedure: a/0

How to find the index of the permutation

只谈情不闲聊 提交于 2021-01-28 00:00:52
问题 % index(+List, -Idx) Predicate will get List with permutation and I want to know index of permutation For example: ?- index([4,1,3,2],X). X= 19. My solution: index([],0). index([_],1). index([X,Y],2):- Y > X. index([H,X|T],Idx):-index([X|T],Idx+1),H > X. Why is it wrong? And how can I make incremention of Idx? 回答1: I found cleaner version of same idea so I show the code: permutation_index([X|Xs], I) :- prerequisite( ( sort([X|Xs], S), length([X|Xs], Len), length(S, Len) )), permutation_index

DCG : zero-or-more, zero-or-one , one-or-more occurrences?

守給你的承諾、 提交于 2021-01-27 21:33:54
问题 In DCG how do you implement : zero-or-more, zero-or-one , one-or-more occurrences ? I'm talking about the following in pseudo code : sentence --> word+ float --> int+, ['.'], int+ nilORa --> a? nilORaaaa --> a* 回答1: You use the or-nondeterminism offered by the clause set of a predicate (or, in this case, the set of DCG productions for the same DCG "nonterminal" - the DCG production is an alternative notation for a Horn clause) Move the production that should be performed first to the top. For

Why this dynamic version of Fibonacci program is incredibly faster then this other? Prolog solutions

*爱你&永不变心* 提交于 2021-01-27 16:11:48
问题 I am learning Prolog using SWI Prolog and I have a doubt about the followings two solutions of the Fibonacci number calculation program: The first one is this: fib(1,1). fib(2,1). fib(N,F) :- N > 2, N1 is N-1, fib(N1,F1), N2 is N-2, fib(N2,F2), F is F1+F2. It is pretty clear for me hw it work, it is very simple. Then I have this second version that, reading the code, seems to work as the previous one but after that it have calculate the Fibonacci number of N save it in the Prolog database by

Why this dynamic version of Fibonacci program is incredibly faster then this other? Prolog solutions

北城余情 提交于 2021-01-27 16:11:34
问题 I am learning Prolog using SWI Prolog and I have a doubt about the followings two solutions of the Fibonacci number calculation program: The first one is this: fib(1,1). fib(2,1). fib(N,F) :- N > 2, N1 is N-1, fib(N1,F1), N2 is N-2, fib(N2,F2), F is F1+F2. It is pretty clear for me hw it work, it is very simple. Then I have this second version that, reading the code, seems to work as the previous one but after that it have calculate the Fibonacci number of N save it in the Prolog database by

Why this dynamic version of Fibonacci program is incredibly faster then this other? Prolog solutions

你离开我真会死。 提交于 2021-01-27 15:13:39
问题 I am learning Prolog using SWI Prolog and I have a doubt about the followings two solutions of the Fibonacci number calculation program: The first one is this: fib(1,1). fib(2,1). fib(N,F) :- N > 2, N1 is N-1, fib(N1,F1), N2 is N-2, fib(N2,F2), F is F1+F2. It is pretty clear for me hw it work, it is very simple. Then I have this second version that, reading the code, seems to work as the previous one but after that it have calculate the Fibonacci number of N save it in the Prolog database by