dcg

Structure (Difference Lists) Prolog

前提是你 提交于 2019-12-10 15:02:01
问题 This question refers to the material in chapter 3 of the book: Programming in Prolog, Clocksin and Mellish, Ed 5 In page 72 of this book, a program using difference list is displayed: partsOf(X,P):- partsacc(X,P,Hole) , Hole=[]. partsacc(X,[X|Hole],Hole):-basicpart(X). partsacc(X,P,Hole):- assembly(X,Subparts), partsacclist(Subparts, P, Hole). partsacclist([],Hole,Hole). partsacclist([P|T], Total, Hole):- partsacc(P,Total,Hole1), partsacclist(T,Hole1,Hole). In many tutorials online, the

Prolog DCG: find last element

女生的网名这么多〃 提交于 2019-12-10 14:17:22
问题 I am trying to understand the use of DCGs better. In order to do this, I tried to translate some exercises in the LearnPrologNow book to DCG notation. However, I am failing miserably. What I tried to write a program that simply names the last element in a list. That's all. I just can't think of the right DCG syntax to do this. I think I figured out the 'base case' which should be: last --> [X|[]]. Where X is the last element. How do I make Prolog go down the list recursively? Or am I thinking

Question - formal language in prolog

这一生的挚爱 提交于 2019-12-10 13:35:05
问题 I am trying to build a DCG which recognizes all lists which match this form : a^n b^2m c^2m d^n . I have written up the following rules: s --> []. s --> ad. ad --> a, ad, d. ad --> bc. bc --> b, b, bc, c, c. bc --> []. a --> [a]. b --> [b]. c --> [c]. d --> [d]. When I try to evaluate a string with those specifications, like the list [a,b,b,c,c,d] , it works. But when I try to evaluate the query phrase(s, X) so that I can see all the possible strings returned by this grammar, it loops to

Prolog predicate calling

送分小仙女□ 提交于 2019-12-10 10:47:35
问题 In the following tutorial: http://www.csupomona.edu/~jrfisher/www/prolog_tutorial/7_3.html There is the part: test_parser :- repeat, write('?? '), read_line(X), ( c(F,X,[]) | q(F,X,[]) ), nl, write(X), nl, write(F), nl, fail. Now I'm extremely confused about the c(F,X,[]) and q(F,X,[]) part because it doesn't seem to match any thing that I have seen, c only takes one parameter from what I can tell and these parameters don't seem to make sense for q. Please help me understand what is going on

Recognize A^n B^n language in Prolog with no arithmetics

可紊 提交于 2019-12-10 10:07:55
问题 How to recognize A^n B^n language in Prolog without arithmetics and for any A, B where A != B? With known A = a and B = b we could write % For each 'a' save 'b' in a list, then check % whether constructed list is equal to the rest of input list anbn(L) :- anbn(L, []). anbn(L, L). anbn([a|L],A) :- anbn(L, [b|A]). For any A and B I was thinking of a solution starting with anbn(L) :- anbn(L, []). anbn([H|L],[]) :- anbn(L,[H]). % save an element anbn([H|L], [H|A]) :- anbn(L, [H,H|A]). % make sure

Remove incorrect subsequent solutions without once

巧了我就是萌 提交于 2019-12-10 01:50:32
问题 I have a predicate that finds the correct solution but then goes on to find solutions which are not right. ?- data(D),data_threshold_nonredundantbumps(D,5,Bs),write(D). [3,6,7,8,2,4,5,6,9,4,7,3] D = [3, 6, 7, 8, 2, 4, 5, 6, 9|...], Bs = [bump([11], [7]), bump([8, 9], [6, 9]), bump([2, 3, 4], [6, 7, 8])] ; [3,6,7,8,2,4,5,6,9,4,7,3] D = [3, 6, 7, 8, 2, 4, 5, 6, 9|...], Bs = [bump([8, 9], [6, 9]), bump([2, 3, 4], [6, 7, 8])] ; [3,6,7,8,2,4,5,6,9,4,7,3] D = [3, 6, 7, 8, 2, 4, 5, 6, 9|...], Bs =

How to convert prolog parse tree back to a logical sentence

你说的曾经没有我的故事 提交于 2019-12-09 18:27:39
问题 I managed to build the parse tree for given sentence and here it is, for the sentence: "The man went home." T = s(np(det(the), n(man)), vp(v(went), np(n(home)))) 1) How to use phrase/2 on this? How to translate a sentence in a logical language using prolog? - is similar to what I need, but it's solution doesn't work on me. 2)I want to map this with grammar pattern and get the words tag. Det=the , N(Subject)=man , V=went , N(Object)=home Is there a way to map this tree with given set tree

Prolog - DCG parser with input from file

强颜欢笑 提交于 2019-12-09 16:12:06
问题 As part of a project I need to write a parser that can read a file and parse into facts I can use in my program. The file structure looks as follows: property = { el1 , el2 , ... }. What I want in the end is: property(el1). property(el2). ... I read my file like this: main :- open('myFile.txt', read, Str), read_file(Str,Lines), close(Str), write(Lines), nl. read_file(Stream,[]) :- at_end_of_stream(Stream). read_file(Stream,[X|L]) :- \+ at_end_of_stream(Stream), read(Stream,X), parse(X), %

Check if string is substring in Prolog

送分小仙女□ 提交于 2019-12-09 03:12:21
问题 Is there a way to check if a string is a substring of another string in Prolog? I tried converting the string to a list of chars and subsequently checking if the first set is a subset of the second that that doesn't seem to be restrictive enough. This is my current code: isSubstring(X,Y):- stringToLower(X,XLower), stringToLower(Y,YLower), isSubset(XLower,YLower). isSubset([],_). isSubset([H|T],Y):- member(H,Y), select(H,Y,Z), isSubset(T,Z). stringToLower([],[]). stringToLower([Char1|Rest1],

Parsing an expression in Prolog and returning an abstract syntax

两盒软妹~` 提交于 2019-12-09 01:40:20
问题 I have to write parse(Tkns, T) that takes in a mathematical expression in the form of a list of tokens and finds T, and return a statement representing the abstract syntax, respecting order of operations and associativity. For example, ?- parse( [ num(3), plus, num(2), star, num(1) ], T ). T = add(integer(3), multiply(integer(2), integer(1))) ; No I've attempted to implement + and * as follows parse([num(X)], integer(X)). parse(Tkns, T) :- ( append(E1, [plus|E2], Tkns), parse(E1, T1), parse