-
I have heard before that using :- use_module(library(dcgs)).
:- use_module(library(clpz)).
:- use_module(library(time)).
:- use_module(library(lambda)).
:- use_module(library(clpz)).
:- use_module(library(lists)).
?- _+\(length(Xs, 100000), length(Ys, 100000), time(append(Xs,Ys,Zs))).
% CPU time: 0.024s, 100_034 inferences
true.
?- _+\(length(Xs,100000),length(Ys,100000), time(phrase((Xs,Ys), Zs))).
% CPU time: 0.036s, 200_102 inferences
true.
?- _+\(length(Xs,1),length(Ys,100000), time(phrase((Xs,Ys), Zs))).
% CPU time: 0.025s, 100_103 inferences
true.
?- _+\(length(Xs, 1), length(Ys, 100000), time(append(Xs,Ys,Zs))).
% CPU time: 0.000s, 35 inferences
true.
?- _+\(length(Xs,100000),length(Ys,1), time(phrase((Xs,Ys), Zs))).
% CPU time: 0.028s, 100_103 inferences
true.
?- _+\(length(Xs, 100000), length(Ys, 1), time(append(Xs,Ys,Zs))).
% CPU time: 0.025s, 100_034 inferences
true.
?- Last+\(bagof(X, (X in 0..10000, indomain(X)), Xs), time(append(_, [Last], Xs))).
% CPU time: 0.003s, 20_034 inferences
Last = 10000
; % CPU time: 0.000s, 4 inferences
false.
?- Last+\(bagof(X, (X in 0..10000, indomain(X)), Xs), time(phrase((...,[Last]), Xs))).
% CPU time: 0.006s, 60_048 inferences
Last = 10000
; % CPU time: 0.000s, 8 inferences
false.
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| | append/3 | phrase/2 |
| xs/ys | | |
|---------+----------+----------|
| 1/1e5 | 1 | 1e5 |
| 1e5/1 | 1e5 | 1e5 |
| 1e5/1e5 | 1e5 | 2e5 |
|---------+----------+----------|
| last-of | | |
| 1e5 | 2e4 | 6e4 |
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ Is Edit: I wish I could select more than one answers, the response from @UWN is fantastic! |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 12 replies
-
Frequent use of For example, consider the task: "Does the list ?- phrase((...,"e",...,"e",...,"e",...), "hello there"). true ; false. With Multiple uses of Even a single use of ?- Ts = "ello", Rs = [h|Ts]. Ts = "ello", Rs = "hello". No Other than that, |
Beta Was this translation helpful? Give feedback.
-
It starts with incomplete and thus misleading documentation. Take any Prolog book or system and look up how
A fully declarative description is quite contorted and makes Contrast this to the non-terminal
Concerning your examples you cannot compare The next problem is the operationalizing name which suggests that someone is appending here something. While this is true sometimes, in many uses it not true at all. And it's not just the inverse, like splitting a list in two parts. How do you call this here:
The power of names is often underestimated. They exert their full power when we are weak. Like in the moment when we "just fixing" this little issue not really worth our attention. The most iconic anti-pattern use of
But no matter what refinement of resolution they tried, no matter how they reordered the goals, it was clear that parsing was extremely costly. And to generate sentences, another resolution method was needed. Still today one can see such feeble attempts to encode grammars. Only when the encoding using a pair of lists (or markers) was understood what we now call a difference, was Prolog born in the summer of 1972. |
Beta Was this translation helpful? Give feedback.
-
@haijinSk, you are permanently posting and quickly removing your questions. This is quite irritating. Why prepare an answer, if you remove your post anyway? |
Beta Was this translation helpful? Give feedback.
Frequent use of
append/3
in the same program or predicate may indicate that a portion of the code could benefit from DCGs.For example, consider the task: "Does the list
"hello there"
contain the subsequence"eee"
? Easy to express with DCGs:With
append/3
, it is also possible, though significantly more tedious and prone to errors. (Try it!)Multiple uses of
append/3
in a row can also lead to significantly increased asymptotic complexity compared to using DCGs. As an example, consider describing a longer text as a list of characters or tokens from multiple parts with DCGs, and contrast it to various different ways …