35
36:- module('$dicts',
37 [ '.'/3 38 ]).
46.(Data, Func, Value) :-
47 ( '$get_dict_ex'(Func, Data, V0)
48 *-> Value = V0
49 ; is_dict(Data, Tag)
50 -> eval_dict_function(Func, Tag, Data, Value)
51 ; is_list(Data)
52 -> ( is_dict_func(Func)
53 -> dict_create(Dict, _, Data),
54 '$get_dict_ex'(Func, Dict, Value)
55 ; '$type_error'(atom, Func)
56 )
57 ; '$type_error'(dict, Data)
58 ).
59
60is_dict_func(Key) :- atomic(Key), !.
61is_dict_func(Var) :- var(Var), !.
62is_dict_func(get(_)).
63is_dict_func(get(_,_)).
64is_dict_func(put(_)).
65is_dict_func(put(_,_)).
73eval_dict_function(get(Key), _, Dict, Value) :-
74 !,
75 ( atomic(Key)
76 -> get_dict(Key, Dict, Value)
77 ; var(Key)
78 -> get_dict(Key, Dict, Value)
79 ; get_dict_path(Key, Dict, Value)
80 ).
81eval_dict_function(get(Key, Default), _, Dict, Value) :-
82 !,
83 ( ( atomic(Key)
84 -> get_dict(Key, Dict, Value0)
85 ; var(Key)
86 -> get_dict(Key, Dict, Value0)
87 ; get_dict_path(Key, Dict, Value0)
88 )
89 *-> Value = Value0
90 ; Value = Default
91 ).
92eval_dict_function(put(Key, Value), _, Dict, NewDict) :-
93 !,
94 ( atomic(Key)
95 -> put_dict(Key, Dict, Value, NewDict)
96 ; put_dict_path(Key, Dict, Value, NewDict)
97 ).
98eval_dict_function(put(New), _, Dict, NewDict) :-
99 !,
100 put_dict(New, Dict, NewDict).
101eval_dict_function(Func, Tag, Dict, Value) :-
102 call(Tag:Func, Dict, Value).
110put_dict_path(Key, Dict, Value, NewDict) :-
111 atom(Key),
112 !,
113 put_dict(Key, Dict, Value, NewDict).
114put_dict_path(Path, Dict, Value, NewDict) :-
115 put_dict_path(Path, Dict, _Old, NewDict, Value).
116
117put_dict_path(Path, _, _, _, _) :-
118 var(Path),
119 !,
120 '$instantiation_error'(Path).
121put_dict_path(Path/Key, Dict, Old, NewDict, New) :-
122 !,
123 put_dict_path(Path, Dict, OldD, NewDict, NewD),
124 ( get_dict(Key, OldD, Old, NewD, New),
125 is_dict(Old)
126 -> true
127 ; Old = _{},
128 put_dict(Key, OldD, New, NewD)
129 ).
130put_dict_path(Key, Dict, Old, NewDict, New) :-
131 get_dict(Key, Dict, Old, NewDict, New),
132 is_dict(Old),
133 !.
134put_dict_path(Key, Dict, _{}, NewDict, New) :-
135 put_dict(Key, Dict, New, NewDict).
139get_dict_path(Path, Dict, Value) :-
140 compound(Path),
141 Path = (Path0/Key),
142 !,
143 get_dict_path(Path0, Dict, Dict1),
144 get_dict(Key, Dict1, Value).
145get_dict_path(Key, Dict, Value) :-
146 get_dict(Key, Dict, Value).
147
148
149
162
163expand_dict_function((QFHead := V0 :- Body), (QHead :- Body, Eval)) :-
164 fqhead(QFHead, FHead, Head, QHead),
165 compound(FHead),
166 FHead =.. [.,R,M],
167 callable(M),
168 !,
169 '$expand':replace_functions(V0, Eval, V, _Ctx),
170 compound_name_arguments(M, Name, Args0),
171 '$append'(Args0, [R,V], Args),
172 compound_name_arguments(Head, Name, Args).
173expand_dict_function((QFHead := V0), (QHead :- Eval)) :-
174 fqhead(QFHead, FHead, Head, QHead),
175 compound(FHead),
176 FHead =.. [.,R,M],
177 callable(M),
178 !,
179 '$expand':replace_functions(V0, Eval, V, _Ctx),
180 compound_name_arguments(M, Name, Args0),
181 '$append'(Args0, [R,V], Args),
182 compound_name_arguments(Head, Name, Args).
183
184fqhead(M:FHead, FHead, Head, M:Head) :- !.
185fqhead(FHead, FHead, Head, Head).
186
187
188system:term_expansion(FDecl, Clause) :-
189 expand_dict_function(FDecl, Clause).
190system:term_expansion(M:FDecl, QClause) :-
191 expand_dict_function(FDecl, Clause),
192 !,
193 QClause = M:Clause