Skip to content

Instantly share code, notes, and snippets.

@GeoffChurch
Last active February 25, 2022 19:47
Show Gist options
  • Save GeoffChurch/0df64edbf3a708ad64e50b5b9077db1e to your computer and use it in GitHub Desktop.
Save GeoffChurch/0df64edbf3a708ad64e50b5b9077db1e to your computer and use it in GitHub Desktop.
DCG-ification of mi_list3 from Power of Prolog chapter on meta-interpreters
mi_dcg_clause, [] --> [natnum(0)].
mi_dcg_clause, [natnum(X)] --> [natnum(s(X))].
mi_dcg_clause, [always_infinite] --> [always_infinite].
mi_dcg --> [].
mi_dcg --> mi_dcg_clause, mi_dcg.
%% Original version from https://www.metalevel.at/acomip/ :
mi_ldclause(natnum(0), Rest, Rest).
mi_ldclause(natnum(s(X)), [natnum(X)|Rest], Rest).
mi_ldclause(always_infinite, [always_infinite|Rest], Rest).
mi_list3([]).
mi_list3([G0|Gs0]) :- mi_ldclause(G0, Remaining, Gs0), mi_list3(Remaining).
%% Here's what the dcgs expand into:
%% ?- listing(mi_dcg).
%% mi_dcg(A, A).
%% mi_dcg(A, B) :-
%% mi_dcg_clause(A, C),
%% mi_dcg(C, B).
%% ?- listing(mi_dcg_clause).
%% mi_dcg_clause([natnum(0)|A], A).
%% mi_dcg_clause([natnum(s(X))|A], [natnum(X)|A]).
%% mi_dcg_clause([always_infinite|A], [always_infinite|A]).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment