diff options
Diffstat (limited to 'pl/atom_dcg.pl')
-rw-r--r-- | pl/atom_dcg.pl | 81 |
1 files changed, 76 insertions, 5 deletions
diff --git a/pl/atom_dcg.pl b/pl/atom_dcg.pl index 74689d5..c5b9bfc 100644 --- a/pl/atom_dcg.pl +++ b/pl/atom_dcg.pl @@ -1,11 +1,82 @@ -:- module(atom_dcg, [atom_phrase/2]). +:- module(atom_dcg, [compound_atom_codes/2]). -:- meta_predicate atom_phrase(2, ?). +% with_atoms/1-9 automatically convert atoms for the caller to codes +% for the callee, primarily to make atoms easier to use with +% definite-clause grammars. atom_phrase/2 is defined as a shortcut. -atom_phrase(G, A) :- +% with_codes/1-9, conversely, convert codes for the caller to atoms +% for the callee. + +:- meta_predicate user:with_atoms(0). +:- meta_predicate user:with_atoms(0,0). +:- meta_predicate user:with_atoms(0,0,0). +:- meta_predicate user:with_atoms(0,0,0,0). +:- meta_predicate user:with_atoms(0,0,0,0,0). +:- meta_predicate user:with_atoms(0,0,0,0,0,0). +:- meta_predicate user:with_atoms(0,0,0,0,0,0,0). +:- meta_predicate user:with_atoms(0,0,0,0,0,0,0,0). +:- meta_predicate user:with_atoms(0,0,0,0,0,0,0,0,0). +user:with_atoms(A) :- atom_dcg:compound_atom_codes(A, A1), call(A1). +user:with_atoms(A, B) :- maplist(user:with_atoms, [A,B]). +user:with_atoms(A, B, C) :- maplist(user:with_atoms, [A,B,C]). +user:with_atoms(A, B, C, D) :- maplist(user:with_atoms, [A,B,C,D]). +user:with_atoms(A, B, C, D, E) :- maplist(user:with_atoms, [A,B,C,D,E]). +user:with_atoms(A, B, C, D, E, F) :- maplist(user:with_atoms, [A,B,C,D,E,F]). +user:with_atoms(A, B, C, D, E, F, G) :- maplist(user:with_atoms, [A,B,C,D,E,F,G]). +user:with_atoms(A, B, C, D, E, F, G, H) :- maplist(user:with_atoms, [A,B,C,D,E,F,G,H]). +user:with_atoms(A, B, C, D, E, F, G, H, I) :- maplist(user:with_atoms, [A,B,C,D,E,F,G,H,I]). + +:- meta_predicate user:with_codes(0). +:- meta_predicate user:with_codes(0,0). +:- meta_predicate user:with_codes(0,0,0). +:- meta_predicate user:with_codes(0,0,0,0). +:- meta_predicate user:with_codes(0,0,0,0,0). +:- meta_predicate user:with_codes(0,0,0,0,0,0). +:- meta_predicate user:with_codes(0,0,0,0,0,0,0). +:- meta_predicate user:with_codes(0,0,0,0,0,0,0,0). +:- meta_predicate user:with_codes(0,0,0,0,0,0,0,0,0). +user:with_codes(A) :- atom_dcg:compound_atom_codes(A1, A), call(A1). +user:with_codes(A, B) :- maplist(user:with_codes, [A,B]). +user:with_codes(A, B, C) :- maplist(user:with_codes, [A,B,C]). +user:with_codes(A, B, C, D) :- maplist(user:with_codes, [A,B,C,D]). +user:with_codes(A, B, C, D, E) :- maplist(user:with_codes, [A,B,C,D,E]). +user:with_codes(A, B, C, D, E, F) :- maplist(user:with_codes, [A,B,C,D,E,F]). +user:with_codes(A, B, C, D, E, F, G) :- maplist(user:with_codes, [A,B,C,D,E,F,G]). +user:with_codes(A, B, C, D, E, F, G, H) :- maplist(user:with_codes, [A,B,C,D,E,F,G,H]). +user:with_codes(A, B, C, D, E, F, G, H, I) :- maplist(user:with_codes, [A,B,C,D,E,F,G,H,I]). + +:- meta_predicate user:atom_phrase(2, ?). +user:atom_phrase(G, A) :- + atom_dcg:compound_atom_codes(G, G1), ( var(A) - -> phrase(G, C), + -> phrase(G1, C), atom_codes(A, C) ; atom_codes(A, C), - phrase(G, C) + phrase(G1, C) + ). + +compound_atom_codes(Module:G, Module:G1) :- !, compound_atom_codes(G, G1). +compound_atom_codes(G, G1) :- + ( nonvar(G) + -> G =.. [F|Args0], + maplist(maybe_atom_codes, Args0, Args), + G1 =.. [F|Args] + ; G1 =.. [F|Args], + maplist(maybe_atom_codes, Args0, Args), + G =.. [F|Args0] ). + +maybe_atom_codes(A, C) :- + (compound(A) ; compound(C)), !, + compound_atom_codes(A, C). + +maybe_atom_codes(A, C) :- + nonvar(A), !, + ( atom(A) + -> atom_codes(A, C) + ; C = A + ). + +maybe_atom_codes(A, C) :- + var(A), !, + when((ground(A) ; ground(C)), catch(atom_codes(A, C), _, A = C)). |