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 -> ( (atomic(Func) ; var(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 ).
66eval_dict_function(get(Key), _, Dict, Value) :-
67 !,
68 get_dict(Key, Dict, Value).
69eval_dict_function(put(Key, Value), _, Dict, NewDict) :-
70 !,
71 ( atomic(Key)
72 -> put_dict(Key, Dict, Value, NewDict)
73 ; put_dict_path(Key, Dict, Value, NewDict)
74 ).
75eval_dict_function(put(New), _, Dict, NewDict) :-
76 !,
77 put_dict(New, Dict, NewDict).
78eval_dict_function(Func, Tag, Dict, Value) :-
79 call(Tag:Func, Dict, Value).
87put_dict_path(Key, Dict, Value, NewDict) :-
88 atom(Key),
89 !,
90 put_dict(Key, Dict, Value, NewDict).
91put_dict_path(Path, Dict, Value, NewDict) :-
92 get_dict_path(Path, Dict, _Old, NewDict, Value).
93
94get_dict_path(Path, _, _, _, _) :-
95 var(Path),
96 !,
97 '$instantiation_error'(Path).
98get_dict_path(Path/Key, Dict, Old, NewDict, New) :-
99 !,
100 get_dict_path(Path, Dict, OldD, NewDict, NewD),
101 ( get_dict(Key, OldD, Old, NewD, New),
102 is_dict(Old)
103 -> true
104 ; Old = _{},
105 put_dict(Key, OldD, New, NewD)
106 ).
107get_dict_path(Key, Dict, Old, NewDict, New) :-
108 get_dict(Key, Dict, Old, NewDict, New),
109 is_dict(Old),
110 !.
111get_dict_path(Key, Dict, _{}, NewDict, New) :-
112 put_dict(Key, Dict, New, NewDict).
113
114
115
128
129expand_dict_function((QFHead := V0 :- Body), (QHead :- Body, Eval)) :-
130 fqhead(QFHead, FHead, Head, QHead),
131 compound(FHead),
132 FHead =.. [.,R,M],
133 callable(M),
134 !,
135 '$expand':replace_functions(V0, Eval, V, _Ctx),
136 compound_name_arguments(M, Name, Args0),
137 '$append'(Args0, [R,V], Args),
138 compound_name_arguments(Head, Name, Args).
139expand_dict_function((QFHead := V0), (QHead :- Eval)) :-
140 fqhead(QFHead, FHead, Head, QHead),
141 compound(FHead),
142 FHead =.. [.,R,M],
143 callable(M),
144 !,
145 '$expand':replace_functions(V0, Eval, V, _Ctx),
146 compound_name_arguments(M, Name, Args0),
147 '$append'(Args0, [R,V], Args),
148 compound_name_arguments(Head, Name, Args).
149
150fqhead(M:FHead, FHead, Head, M:Head) :- !.
151fqhead(FHead, FHead, Head, Head).
152
153
154system:term_expansion(FDecl, Clause) :-
155 expand_dict_function(FDecl, Clause).
156system:term_expansion(M:FDecl, QClause) :-
157 expand_dict_function(FDecl, Clause),
158 !,
159 QClause = M:Clause