diff options
author | John Ankarström <john@ankarstrom.se> | 2022-04-05 02:37:24 +0200 |
---|---|---|
committer | John Ankarström <john@ankarstrom.se> | 2022-04-05 02:37:24 +0200 |
commit | 96f4b3a127e027364c77416f1ee8e5a471c5d783 (patch) | |
tree | fa4c1fddc4114f5f39c08f7e84bbe89522ff7099 | |
parent | 89cc2518f313afc5da650f327af838fa4e1b79ff (diff) | |
download | EpisodeBrowser-96f4b3a127e027364c77416f1ee8e5a471c5d783.tar.gz |
Generalize atom_phrase/2.
-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)). |