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.

Comments

Re: Prolog Text desugar

Jan Burse, created Nov 12. 2011 There is an error in the ordering of the clauses, it would be ok to first do the user specific expansion and then the meta predicate expansion. This way expansions for meta predicates could also be defined. Will fix blog entry for release 0.9.2.