dcg

Prolog recursion grammar

我是研究僧i 提交于 2019-12-17 20:37:18
问题 at the moment I am having a problem with looping back to noun_phrase from np2 . I was wondering if someone can help me loop back to noun_phrase . Here is some code: noun_phrase([X|T],(det(X), NP2),Rem):- det(X), np2(T,NP2,Rem). np2([H|T],np2(adj(H),Rest),NP) :- adj(H), np2(T,Rest,Rem), noun_phrase(NP,Rem,_). I want to loop from np2 back to noun_phrase . I think the code for np2 is wrong as I just hacked it together. 回答1: Encoding a grammar directly in Prolog is a quite cumbersome process. Yes

DCG Expansion: Is Steadfastness ignored?

自古美人都是妖i 提交于 2019-12-17 19:59:54
问题 Assume I have the following DCG rule: factor(X) --> "(", expr(X), ")". Normally this would be translated to: factor(X, A, B) :- [40|C] = A, expr(X, C, D), [41|B] = D. Would a Prolog system be allowed to translate it as follows, i.e. to merge the unifications into the head and the goal? factor(X, [40|A], B) :- expr(X, A, [41|B]). If DCG expansion would not be steadfast, it wouldn't be allowed to put [41|B] in the third argument of the expr call. But I guess steadfastness is in place, so

How do you do a search and replace of a list with another sublist in Prolog?

风流意气都作罢 提交于 2019-12-17 17:03:25
问题 I'm trying to modify a list by search and replace, was wondering how do I search through a list with the search term as a list as well? Lets say I have a list [1,2,3,4] I want to single out the 2 and 3 and replace it with 5,6 so ideally I could have a predicate: search_and_replace(Search_Term, Replace_Term, Target_List, Result_List). eg. search_and_replace([2,3], [5,6], [1,2,3,4], Result_List), write(Result_List). 回答1: You can use append/2 as follows : replace(ToReplace, ToInsert, List,

Recursive Prolog predicate for reverse / palindrome

我的未来我决定 提交于 2019-12-17 13:55:44
问题 Can I get a recursive Prolog predicate having two arguments, called reverse, which returns the inverse of a list: Sample query and expected result: ?- reverse([a,b,c], L). L = [c,b,a]. A recursive Prolog predicate of two arguments called palindrome which returns true if the given list is palindrome. Sample query with expected result: ?- palindrome([a,b,c]). false. ?- palindrome([b,a,c,a,b]). true. 回答1: Ad 1: It is impossible to define reverse/2 as a ( directly edit thx to @repeat: tail)

Read a file line by line in Prolog

为君一笑 提交于 2019-12-17 06:48:06
问题 I'd like to read a plain text file and apply a predicate to each line (the predicates contain write which does the output). How would I do that? 回答1: In SWI-Prolog, the cleanest solution is to write a DCG that describes what a "line" is, then call a predicate for each line. Use library(pio) to apply the DCG to a file. EDIT : As requested, consider: :- use_module(library(pio)). lines([]) --> call(eos), !. lines([Line|Lines]) --> line(Line), lines(Lines). eos([], []). line([]) --> ( "\n" ; call

DCG for file output

梦想的初衷 提交于 2019-12-13 01:27:59
问题 If I have a program like this and I want the last item for a person not to have a comma following it, how do I do this? Is it best to use a DCG? How would that work? male(bob). male(dave). male(fred). male(dereck). likes(bob,cake). likes(bob, pie). likes(bob, apple). likes(dave, fish). likes(dave, meat). likes(dave, potato). likes(dave, pear). likes(fred, water). likes(fred, beer). likes(dereck, wine). likes(dereck, cake). print:- forall( male(Person), ( format("~w, ",[Person]), forall( likes

Read from file and display the content using Prolog DCG rule

蓝咒 提交于 2019-12-13 01:26:46
问题 i am a newbie in prolog, i want to read a file which actually contains CLASS Definition - using Prolog DCG Rule. But i am stuck in between now.. My input text (linessample.txt) class component { attributes Real V, I, R; constraints V = I * R; constructors component(V1, I1, R1) { V = V1; I = I1; R = R1; } } I want to read the above sample text using DCG Rule in prolog... I have written one sample code.. but i am not getting the first word "class" in the output Code :- use_module(library(pio)).

Prolog: Swap first and last elements in list

你说的曾经没有我的故事 提交于 2019-12-13 00:33:48
问题 I'm trying to write a program that swaps the 1st and last elements. The function takes 2 parameters. A list and a variable that's displayed as the newly swapped list. I thought I was doing it the lazy way, but it's turning out to be just as hard for me. I was going to grab the head, put it aside -- grab the last element of the tail, put it aside -- take the tail, remove the last element, put it aside also, then append all 3 together to make a list I'm having trouble removing the last element

Relationship between integers and their names - Prolog

非 Y 不嫁゛ 提交于 2019-12-12 22:00:55
问题 I'm trying to explore the concepts of natural numbers, Peano numbers, arithmetic, etc. in Prolog. Right now I'm trying to create a predicate that will allow me to enter the name of any number and get it's numerical value (or vice versa). How would I go about this? My idea was that I would translate the numbers given and them add them together using the plus function (e.g. one hundred and forty-five: one, hundred = 100, and = 0, forty = 40, five = 5 -> 100 + 0 + 40 + 5 = 145. Here's an example

how to do Arithmetic Operations in DCG in prolog

为君一笑 提交于 2019-12-12 18:18:58
问题 verb_phrase(X,P)--> trans_verb(X,X+1,P1), noun_phrase(X+1,P1,P). For the code above, if X=1, I will get (...1+1...). "..."means not important code. but I really want to get 2 instead of 1+1. Could someone tell me how to do it? 回答1: If you are reasoning over integers, the cleanest way is to use CLP(FD) constraints for arithmetic. You can use {}/1 within DCGs to embed Prolog goals. For example: :- use_module(library(clpfd)). verb_phrase(X0, P)--> { X #= X0 + 1 }, trans_verb(X0, X, P1), noun