Prolog Text desugar

/**
* Prolog code for the term and goal expansion.
*
* Covers only vanilla Prolog and vanilla DCG.
*
* Prolog Vanilla Prolog
* --------------------------------
* expand_term desugar_term
* term_expansion term_desugaring
* Head :- Body. Head :- Body.
* expand_goal desugar_goal
* goal_expansion goal_desugaring
* Body, Body Body, Body
* Body; Body N/A
* \+ Body N/A
* Etc.. Etc..
*
* Copyright 2011 XLOG Technologies GmbH, Switzerland
* Jekejeke Prolog 0.9.1 (a fast and small prolog interpreter)
*/

/* goal expansion mechanism for vanilla Prolog */
desugar_term((A :- B), (A :- C)) :- !, desugar_goal(B, C).
desugar_term(A, C) :- term_desugaring(A, B), !, desugar_term(B, C).
desugar_term(A, A).

desugar_goal((A, B), (C, D)) :- !, desugar_goal(A, C), desugar_goal(B, D).
desugar_goal(A, C) :- goal_desugaring(A, B), !, desugar_goal(B, C).
desugar_goal(A, A).

/* expansion rules for vanilla DCG */
append([], X, X).
append([X | Y], Z, [X | T]) :- append(Y, Z, T).

extend(P, I, O, Q) :- P =.. [F | L], append(L, [I, O], R), Q =.. [F | R].

:- sys_op(1200, xfx, ==>).

connects([X | Y], X, Y).

term_desugaring((P ==> B), (Q :- phrase(B, I, O))) :- extend(P, I, O, Q).

goal_desugaring(phrase((A, B), I, O), (phrase(A, I, H), phrase(B, H, O))).
goal_desugaring(phrase([A], I, O), connects(I, A, O)).
goal_desugaring(phrase([], I, I), true).
goal_desugaring(phrase(P, I, O), Q) :- extend(P, I, O, Q).

/* conversion helper */
convert :- (A ==> B), desugar_term((A ==> B), C), assertz(C), fail; true.

Kommentare

Aw: Prolog Text desugar

Jan Burse, erstellt 12. Nov 2011 Es hat einen Fehler in der Reihenfolge der Klauseln, es sollte Ok sein zuerst die Benutzerspezifische Expansion zu machen und dann die Meta Prädikat Expansion. Auf diese Art können auch Expansionen für Meta Prädikate definiert werden. Wir werden diese Tagebucheintrag mit Release 0.9.2 in Ordnung bringen.