Last active
December 17, 2015 12:29
-
-
Save WillNess/5610322 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
this is not a specific question. it is too broad; hard to say what is been asked here. I can answer | |
with a general reply, but I don't know how helpful it will be. This is not how questions are supposed | |
to be asked on SO, I think. Questions should be clear and specific, so that they, and the answers, can | |
be helpful to others as well. On SO, please ask one specific question. | |
___ | |
Each DCG rule `rule(...)` corresponds to a Prolog predicate `rule(... , L, Z)`, where `L, Z` form a | |
difference-list. Your program is thus equivalent to: | |
<!-- language: lang-prolog --> | |
sentence2(VP, L, Z):- noun_phrase2(Actor, L, L2), | |
verb_phrase2(Actor, VP, L2, Z). | |
noun_phrase2(Name, L, Z):- properName(Name, L, Z). | |
verb_phrase2(Actor, VP, L, Z):- intrans_verb(Actor, VP, L, Z). | |
verb_phrase2(Somebody, VP, L, Z):- trans_verb(Somebody, Something, VP, L, L2), | |
noun_phrase2(Something, L2, Z). | |
properName(john, L, Z):- | |
L = [john | Z]. %// 'john' is present in the input stream | |
properName(mary, L, Z):- | |
L = [mary | Z]. %// 'mary' is present in the input stream | |
/* an alternative definition | |
properName(X) --> [X], { member(X, [john, mary]) }. | |
%% would be translated as | |
properName(X, L, Z):- L = [X | Z], member(X, [john, mary]). | |
*/ | |
intrans_verb(Actor, paints(Actor), L, Z):- | |
L = [paints | Z]. %// 'paints' is present in the input stream | |
trans_verb(Somebody, Something, likes(Somebody, Something), L, Z):- | |
L = [likes | Z]. %// 'likes' is present in the input stream | |
This is a straightforward Prolog program. You can interpret it as you would any other Prolog program. | |
DCG rules are easier and less error-prone, because the implementation takes care of the inner variables' | |
equalities: i.e. it puts those `L2`s in proper places automatically. | |
___ | |
sentence2(VP) --> noun_phrase2(Actor),verb_phrase2(Actor, VP). | |
can be read as | |
* *an input is classified as `sentence2(VP)` if its initial part is classified as | |
`noun_phrase2(Actor)`, and the following part is classified as | |
`verb_phrase2(Actor, VP)`.* | |
OR, | |
* *a relation `sentence2(VP, L, Z)` holds for a portion of list `L` before `Z`, if | |
a relation `noun_phrase2(Actor, L, L2)` holds for a portion of list `L` before `L2`, | |
and relation `verb_phrase2(Actor, VP, L2, Z)` holds for a portion of list `L2` | |
before `Z`.* | |
etc. | |
noun_phrase2(Name) --> properName(Name). | |
verb_phrase2(Actor, VP) --> intrans_verb(Actor, VP). | |
verb_phrase2(Somebody, VP) --> trans_verb(Somebody, Something, VP), | |
noun_phrase2(Something). | |
properName(john) --> [john]. | |
properName(mary) --> [mary]. | |
intrans_verb(Actor, paints(Actor)) --> [paints]. | |
trans_verb(Somebody, Something, likes(Somebody, Something)) --> [likes]. | |
This can generate all possible sentences, too: | |
3 ?- sentence2(X,Y,[]). | |
X = paints(john), | |
Y = [john, paints] ; | |
X = likes(john, john), | |
Y = [john, likes, john] ; | |
X = likes(john, mary), | |
Y = [john, likes, mary] ; | |
X = paints(mary), | |
Y = [mary, paints] ; | |
X = likes(mary, john), | |
Y = [mary, likes, john] ; | |
X = likes(mary, mary), | |
Y = [mary, likes, mary]. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment