Better line//1 and lines//1 #2260
Replies: 3 comments 23 replies
-
It's awesome to have a more correct and more general implementation of such a frequently needed non-terminal, thank you a lot for working on this! Is this a good candidate for inclusion in |
Beta Was this translation helpful? Give feedback.
-
What about |
Beta Was this translation helpful? Give feedback.
-
It does fix the order because |
Beta Was this translation helpful? Give feedback.
-
The definitions of the
line//1
andlines//1
non-terminals from the DCG Primer (which I will be callingline_cut//1
andlines_cut//1
here) bugged me since the first time I've seen them. Because they use cuts, they aren't monotonic. For example, in the general case they lose a lot of answers:I've been stuck at day 1 of Advent of Code 2023 for the entire month because of this. I've been striving to do actual application code with fully monotonic high level Prolog, and those definitions were breaking this goal. Part of the motive for doing everything like that is to experience first hand the hardships that people encounter when trying to write "real world" monotonic Prolog, and then this puts me in a position to try to potentially improve that experience with new building blocks. So here my implementation of these predicates:
With this I introduce
if_//3
andif__//3
as two different liftings ofif_/3
to a non-terminal, andeos_t//1
for use withif__//3
. These new definitions are monotonic and can be used to reason about lines in many different ways:The version with cut gives the wrong answer for all of those. All of this comes with an performance hit, obviously. In my measures,
line//1
is about 50-60x slower thanline_cut//1
, andlines//1
is about 10x slower thanlines_cut//1
in the mode of parsing. This would probably improve whenif_/3
is optimized with goal expansion, but in many use cases this doesn't actually matter that much, and in a few use cases you really need the more general monotonic functionality anyway.Beta Was this translation helpful? Give feedback.
All reactions