Episteme.Dcg (Episteme v0.2.0)

Copy Markdown View Source

Definite Clause Grammar support: translates a DCG rule/body (-->, the "difference list" grammar-rule notation) into an ordinary Episteme clause/goal, threading two extra arguments -- the list before (S0) and after (S) whatever the rule/body consumes -- through every nonterminal call. phrase/2,3 and dcg_translate/2 (Episteme.Engine), a {:dcg, head, body} Episteme.Database.consult_forms/2 form, and DCG-rule support in assert/1 (a -->/2-shaped clause term, same as :-/2 is already special-cased) are all built on translate_body/3 below.

Deliberately a practical subset of real DCG translation, not all of it: terminals ([t1, ..., tn]), embedded plain goals ({Goal}, ISO's own {}/1 term shape), cut, and/2, or/2, if_then/2, if_then_else/3, not/1, a bare nonterminal (atom or compound, any other name), and a variable body (translated to phrase(Var, S0, S), letting a grammar body be decided at call time). No call//N pushback support (parameterized higher-order nonterminals) -- not yet needed.

Summary

Functions

Translates one DCG body term into an ordinary goal, given the incoming (s0) and outgoing (s) difference-list variables/terms.

Translates a whole DCG rule (Head --> Body, already parsed/built into its own head/body terms) into an ordinary {Head2, Body2} clause pair, ready for Episteme.Database.add_clause/2 -- Head's own two extra arguments (S0, S) are appended, and Body is translated via translate_body/3, threaded between them.

Functions

translate_body(list, s0, s)

@spec translate_body(term(), term(), term()) :: term()

Translates one DCG body term into an ordinary goal, given the incoming (s0) and outgoing (s) difference-list variables/terms.

translate_rule(head, body)

@spec translate_rule(term(), term()) :: {term(), term()}

Translates a whole DCG rule (Head --> Body, already parsed/built into its own head/body terms) into an ordinary {Head2, Body2} clause pair, ready for Episteme.Database.add_clause/2 -- Head's own two extra arguments (S0, S) are appended, and Body is translated via translate_body/3, threaded between them.