prolog

Putting all results of a query in a list in Prolog

瘦欲@ 提交于 2021-02-17 21:33:05
问题 I'd like to know how to make a predicate that puts all results obtained from some query (so I get a result and press semicolon until I get False) in a list. For example if I write foo(X,[1,2,3]). in some Prolog listener, let's say the result is X=[11]; X=[22]; False. I would like to get all those results in a list, so something like the following would happen. ?-another_foo(X,[1,2,3]). X=[[11],[22]]. another_foo would somehow use foo to create a list with all the results from foo. I just don

`less/2` relation in Peano arithmetic

眉间皱痕 提交于 2021-02-16 14:48:07
问题 This less-than predicate in Peano arithmetic less(0, s(_)). less(s(X), s(Y)) :- less(X, Y). loops when ?- less(X, Y), X=s(0), Y=0. Is there a better way to write less/2 (using Horn clauses only)? 回答1: You can use when/2 . Making it not anymore an infinitely enumerating predicate and still keeping it 100% pure. The when/2 modifies the S (selection rule) in SLD-Resolution, an idea that can be traced back to Alain Colmerauer. less(X, Y) :- when((nonvar(X),nonvar(Y)), less2(X,Y)). less2(0, s(_)).

Bracket Abstraction in Prolog

前提是你 提交于 2021-02-16 14:47:20
问题 The algorithm "A" according to Antoni Diller looks fairly simple: http://www.cantab.net/users/antoni.diller/brackets/intro.html Can we do this in Prolog? 回答1: % associate formulas to left associate_left(F, A, B) :- append(A, [B], F). % apply algorithm reduce(b(_, []), []). reduce(b(A, B), 'K'(B)) :- atom(B), dif(A, B). reduce(b(A, A), 'I'). reduce(b(A, [F]), R) :- reduce(b(A, F), R). % uncessary paranthesis case reduce(b(A, F), 'S'(Pr, Qr)) :- associate_left(F, P, Q), reduce(b(A, P), Pr),

De Bruijn index based substitution in Prolog

梦想与她 提交于 2021-02-15 07:18:55
问题 The Dutch mathematician Nicolaas Govert de Bruijn invented these indexes for representing terms of lambda calculus without naming the bound variables. Lets take this lambda expression: K = λx.λy.x With de Bruijn indexes it reads, when we use the convention, as here, that the zero de Bruijn index refers to the first enclosing lambda binder: K = λλ1 The de Bruijn index 1 refers to the second lambda binder enclosing the de Bruijn index. How would one write Prolog code for a substitution, so that

Trouble with finding length of list in Prolog

。_饼干妹妹 提交于 2021-02-11 17:51:57
问题 I'm having trouble finding the length of a list. I know how to deal with lists such as say [a,b,c,d] or [a,b,[],d] or even [a,[[[]]],c,[]] each of which have a length of 4. The problem I'm having is trying to figure out the length of the list in a case like this [a,[b,c],d] . There are 4 elements, but when I run my code, it'll print 3. It considers the inner list [b,c] as a single element and I'm not sure how to count those separately. Here's what I have: % this is the base case mylen([],0).

Trouble with finding length of list in Prolog

偶尔善良 提交于 2021-02-11 17:51:30
问题 I'm having trouble finding the length of a list. I know how to deal with lists such as say [a,b,c,d] or [a,b,[],d] or even [a,[[[]]],c,[]] each of which have a length of 4. The problem I'm having is trying to figure out the length of the list in a case like this [a,[b,c],d] . There are 4 elements, but when I run my code, it'll print 3. It considers the inner list [b,c] as a single element and I'm not sure how to count those separately. Here's what I have: % this is the base case mylen([],0).

Controlling parenthesis while working with operators

懵懂的女人 提交于 2021-02-11 15:18:30
问题 When working with user-defined operators, Prolog sometimes adds / removes parenthesis to expressions involving those operators automatically. Is there a way to control this? Many thanks 回答1: The parens are just added by portray. They're not part of the internal representation of the code. You can override portray to change how things are printed. see portray_text 回答2: An operator is defined like so: :- op(Precedence, Associativity, Operator). This answer to a similar question explains this,

Controlling parenthesis while working with operators

孤人 提交于 2021-02-11 15:16:18
问题 When working with user-defined operators, Prolog sometimes adds / removes parenthesis to expressions involving those operators automatically. Is there a way to control this? Many thanks 回答1: The parens are just added by portray. They're not part of the internal representation of the code. You can override portray to change how things are printed. see portray_text 回答2: An operator is defined like so: :- op(Precedence, Associativity, Operator). This answer to a similar question explains this,

Shuffling a list in PROLOG

倾然丶 夕夏残阳落幕 提交于 2021-02-11 15:03:26
问题 Simple question, how can I shuffle a list in PROLOG, so that A1 is my new list? shuffle([1,1,1,2,3,4],A1), I've tried a few predicates I found on the web but none of them seems to be working. Also found this but apparently it's not available anymore, according to SWI-PROLOG. 回答1: You can use random_permutation/2 . It is available in SWI-Prolog. ?- random_permutation([1,2,3],L). L = [1, 3, 2]. 来源: https://stackoverflow.com/questions/27431281/shuffling-a-list-in-prolog

Prolog: How does delete/3 works?

允我心安 提交于 2021-02-11 15:02:16
问题 for sure all Prolog fans heard about the delete or remove predicate. del(X,[X|Tail],Tail). del(X,[H|Old],[H|New]) :- del(X,Old,New). I have problems to understand whats going on. The first row will be used, if the first element in the list is the searched element. Then the list will be cut in Head (X) and the Tail and Tail is the result. But at which point the first X will be compared with the X from the X|Tail? At the second row, i even don't understand, when the X and H will be compared, if