aboutsummaryrefslogtreecommitdiff
path: root/pl/atom_dcg.pl
blob: c5b9bfcc0cb8a360eae8b1ee5a066e51b9419828 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
:- module(atom_dcg, [compound_atom_codes/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.

% 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(G1, C),
	    atom_codes(A, C)
	;   atom_codes(A, 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)).