dcg

Translation to DCG Semicontext not working

末鹿安然 提交于 2019-12-02 09:06:35
问题 Since this question uses list, I wanted to solve it using DCG. In the process I realized that semicontext could be used. (DCG Primer) The original problem is to return count of items in a list but if two identical items are next to each other then don't increment the count. While my code works for some of the test cases, it fails for others. It is just one clause that is failing. In looking at the code with a debugger it appears that the second state variable, the returned list, is bound upon

Prolog- translating English to C

柔情痞子 提交于 2019-12-02 06:39:36
We have a relatively simple assignment that I understand in theory but I think I just don't quite understand Prolog's syntax enough to get that into code. Basically, we have a list of English notations that represent operations in C. They're stored as a list when they're passed to our Prolog program. For example: add 4 to 3 is [add, 4, to, 3] We need to write a function that takes that list an returns the equivalent. So if I called english2C([add,4,to,3], C). C = 4+3 It would bind C to the result. So the data structure itself would be something like +(4(3)). We have a list of such English

Translation to DCG Semicontext not working - follow on

断了今生、忘了曾经 提交于 2019-12-02 04:50:50
问题 As a follow up to this question which poses the problem Return count of items in a list but if two identical items are next to each other then don't increment the count. This code is the closest I came to solving this with DCG and semicontext. lookahead(C),[C] --> [C]. % empty list % No lookahead needed because last item in list. count_dcg(N,N) --> []. % single item in list % No lookahead needed because only one in list. count_dcg(N0,N) --> [_], \+ [_], { N is N0 + 1 }. % Lookahead needed

What's wrong with my prolog program for solving the 3 jugs of water puzzle?

瘦欲@ 提交于 2019-12-02 04:10:54
问题 Can anyone find why I can't have any true answers with my 'go' at this code? For example, I write go(7,3,l) and I suppose that it should move 3 litres of water to the second jug, but it is false according to prolog. What's wrong? :- dynamic go/3. :- dynamic cur_state/1,init/5. :- dynamic end_state/1, final/5. cur_state(State):-State = state(10,0,0,7,l). end_state(State):-State = state(0,3,3,0,r). pour(state(D1,D2,D3,n,l),move(D,C,r),state(D,C,D3,n,r)) :- D is D1-n, C is D2+n. pour(state(D1,D2

Reading a file in prolog [duplicate]

无人久伴 提交于 2019-12-02 02:27:55
Possible Duplicate: Read a file line by line in Prolog I found the following prolog code which reads one character at a time and prints out. process(File) :- open('C:/Users/BHARAT/Desktop/a.txt', read, In), get_char(In, Char1), process_stream(Char1, In), close(In). process_stream(end_of_file, _) :- !. process_stream(Char, In) :- print(Char), get_char(In, Char2), process_stream(Char2, In). But if the file has multiple lines is there a way to read 1 whole line at a time so that it will be easy for tokenizing. You say you want to tokenize the input - the best way to do this are definite clause

Translation to DCG Semicontext not working - follow on

旧巷老猫 提交于 2019-12-02 00:51:04
As a follow up to this question which poses the problem Return count of items in a list but if two identical items are next to each other then don't increment the count. This code is the closest I came to solving this with DCG and semicontext. lookahead(C),[C] --> [C]. % empty list % No lookahead needed because last item in list. count_dcg(N,N) --> []. % single item in list % No lookahead needed because only one in list. count_dcg(N0,N) --> [_], \+ [_], { N is N0 + 1 }. % Lookahead needed because two items in list and % only want to remove first item. count_dcg(N0,N) --> [C1], lookahead(C2), {

Applying semicontext notation for passing additional arguments

偶尔善良 提交于 2019-12-01 21:36:45
This is a follow-on question from an earlier question from Mat's answer Starting with this e([number(0)] , t1 , Uc0 , Uc0, Bc0 , Bc0) --> []. e([number(1)] , t2 , Uc0 , Uc0, Bc0 , Bc0) --> []. e([number(2)] , t3 , Uc0 , Uc0, Bc0 , Bc0) --> []. e([op(neg),[Arg]] , u1(E) , [_|Uc0], Uc1, Bc0 , Bc1) --> [_], e(Arg , E , Uc0, Uc1, Bc0, Bc1). e([op(ln),[Arg]] , u2(E) , [_|Uc0], Uc1, Bc0 , Bc1) --> [_], e(Arg , E , Uc0, Uc1, Bc0, Bc1). e([op(add),[Left,Right]], b1(E0,E1) , Uc0 , Uc2, [_|Bc0], Bc2) --> [_,_], e(Left, E0, Uc0, Uc1, Bc0, Bc1), e(Right, E1, Uc1, Uc2, Bc1, Bc2). e([op(sub),[Left,Right]],

Prolog, reconstruct BST trees from inorder list

佐手、 提交于 2019-12-01 18:30:50
问题 We well know inorder implementation for BST tree. inorder(nil, []). inorder(t(Root, L, R), List) :- inorder(L, ListLeft), inorder(R, ListRight), append(ListLeft, [Root|ListRight], List). However, is it possible to do it for list ? I mean reconstruct all possible BST trees, for example: inorder(X, [1,2,3]). X = t(1, nil, t(2, nil, t(3, nil, nil)))); X = t(3, t(2, t(1, nil, nil), nil), nil), nil); X = t(2, t(1, nil, nil), t(3, nil, nil)); false. It seems to be impossible for me. 回答1: First, let

Prolog, reconstruct BST trees from inorder list

和自甴很熟 提交于 2019-12-01 18:00:21
We well know inorder implementation for BST tree. inorder(nil, []). inorder(t(Root, L, R), List) :- inorder(L, ListLeft), inorder(R, ListRight), append(ListLeft, [Root|ListRight], List). However, is it possible to do it for list ? I mean reconstruct all possible BST trees, for example: inorder(X, [1,2,3]). X = t(1, nil, t(2, nil, t(3, nil, nil)))); X = t(3, t(2, t(1, nil, nil), nil), nil), nil); X = t(2, t(1, nil, nil), t(3, nil, nil)); false. It seems to be impossible for me. First, let us use a Definite Clause Grammar ( dcg ) for relating trees to lists: inorder(nil) --> []. inorder(t(Root,

DCG prolog testing several sentences

妖精的绣舞 提交于 2019-12-01 11:15:34
问题 If I have the below piece of code, how would I make it produce Answer= 5 and Answer2= 10? . I run the goal ?- test(Data),lpsolve(Data, [Answer1,Answer2]). :-use_module(library(clpfd)). test([the, variable, X, is, five,fullstop, the,variable, Y, is, ten, fullstop]). lpsolve(Data, [Answer,Answer2]):- sentence(Answer, Data,[]). sentence(X) --> nounphrase, verbphrase(X). nounphrase --> [the], [variable]. verbphrase(X) --> [X], [is], [five],[fullstop], {X = 5}. sentence(Y) --> nounphrase,