dcg

gnu Prolog powerset modification

天涯浪子 提交于 2019-11-26 17:25:45
问题 So i got this for powerset: powerset([], []). powerset([H|T], P) :- powerset(T,P). powerset([H|T], [H|P]) :- powerset(T,P). This generates all sets of a list. Is it possible to generate all sets in list order. Example: List = [a,b,c] I want to get [a],[a,b],[a,b,c],[b],[b,c],[c] Note there is no [a,c] in this list of subsets since these are subsets starting from the left and going to the right. I've tried using a combination of append and recursion, but that didn't work out as i wanted it to.

Building an Expression Tree in Prolog

蹲街弑〆低调 提交于 2019-11-26 14:56:03
问题 I'm looking for a way to build an Expression Tree in Prolog. I already did some experiments and came up with the following working code (that will only handle constants and the plus expression): const(_). plus(_, _). eval(const(R), R). eval(plus(A, B), R) :- number(A), number(B), R is A+B. eval(plus(A, B), R) :- number(A), eval(B, B_R), R is A+B_R. eval(plus(A, B), R) :- eval(A, A_R), number(B), R is A_R+B. eval(plus(A, B), R) :- eval(A, A_R), eval(B, B_R), R is A_R+B_R. Is there any simpler

Stack overflow in Prolog DCG grammar rule: how to handle large lists efficiently or lazily

筅森魡賤 提交于 2019-11-26 12:32:23
I'm parsing a fairly simple file format consisting of a series of lines, each line having some space separated fields, that looks like this: l 0x9823 1 s 0x1111 3 l 0x1111 12 ⋮ I'm using SWI-Prolog. This is the DCG I have so far: :- consult(library(pure_input)). load_trace(Filename, Traces) :- phrase_from_file(trace_file_phrase(Traces), Filename). trace_file_phrase([]) --> []. trace_file_phrase([T|Ts]) --> trace_phrase(T), trace_file_phrase(Ts). trace_phrase(access(Type, Address, SinceLast)) --> access_type(Type), space, address(Address), space, nat(SinceLast), newline. access_type(load) -->

list of the values in the leaf nodes of binary tree T

僤鯓⒐⒋嵵緔 提交于 2019-11-26 08:35:49
问题 List is the list of values in leaf nodes of a binary tree and I am trying to figure out how to output just that. This is giving me all the nodes but I need just the leaves. lea(nil,[]). lea(t(X,L,R),[X|L]) :- lea(L,L1), lea(R,L2), append(L1,L2,L). Running this gives me: ?- lea(t(a,t(b,t(d,nil,nil),t(e,nil,nil)),t(c,nil,t(f,t(g,nil,nil),nil))), List). List = [a, b, d, e, c, f, g] but I need List = [d, e,g] Is it possible. 回答1: Let's use a DCG - a Definite Clause Grammar. We start with your

Flatten a list in Prolog

三世轮回 提交于 2019-11-26 07:47:09
问题 I\'ve only been working with Prolog for a couple days. I understand some things but this is really confusing me. I\'m suppose to write a function that takes a list and flattens it. ?- flatten([a,[b,c],[[d],[],[e]]],Xs). Xs = [a,b,c,d,e]. % expected result The function takes out the inner structures of the list. This is what I have so far: flatten2([],[]). flatten2([Atom|ListTail],[Atom|RetList]) :- atom(Atom), flatten2(ListTail,RetList). flatten2([List|ListTail],RetList) :- flatten2(List

Stack overflow in Prolog DCG grammar rule: how to handle large lists efficiently or lazily

…衆ロ難τιáo~ 提交于 2019-11-26 02:59:32
问题 I\'m parsing a fairly simple file format consisting of a series of lines, each line having some space separated fields, that looks like this: l 0x9823 1 s 0x1111 3 l 0x1111 12 ⋮ I\'m using SWI-Prolog. This is the DCG I have so far: :- consult(library(pure_input)). load_trace(Filename, Traces) :- phrase_from_file(trace_file_phrase(Traces), Filename). trace_file_phrase([]) --> []. trace_file_phrase([T|Ts]) --> trace_phrase(T), trace_file_phrase(Ts). trace_phrase(access(Type, Address, SinceLast)

What is the difference between ' and " in Prolog?

≡放荡痞女 提交于 2019-11-25 22:07:31
问题 I am new to Prolog and noticed that \' and \" give different behavior, but am curious as to why. Specifically, when loading a file, ?- [\'test1.pl\']. works, while ?- [\"test1.pl\"]. doesn\'t. 回答1: Single quoted items are always atoms. The meaning of double quotes depends on the Prolog flag double_quotes : atom — with this value "a" = a . Nowadays, this is rarely used. But you will find Prolog books where ["abc.pl"] is written. codes — a list of character codes. This is frequently the default