Skip to content

Instantly share code, notes, and snippets.

@GeoffChurch
Last active March 23, 2022 14:49
Show Gist options
  • Save GeoffChurch/d1aadcb76ced9c62cd9c31105f88f32c to your computer and use it in GitHub Desktop.
Save GeoffChurch/d1aadcb76ced9c62cd9c31105f88f32c to your computer and use it in GitHub Desktop.
Reified DCG utils
:- use_module(library(reif)).
phrase_t(P, I, O, T) :- phrase(call(P, T), I, O).
if_(If, Then, Else, I, O) :-
if_(phrase_t(If, I, M),
phrase(Then, M, O),
phrase(Else, M, O)).
% Example:
eos([], []).
maximal_munch(_) --> eos.
maximal_munch(P) --> if_(P, maximal_munch(P), []).
oneX(X, false), [Y] --> [Y], {dif(X, Y)}.
oneX(X, true) --> [X].
:- phrase(maximal_munch(oneX(a)), _, _).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment