View source with raw comments or as raw
   1/*  Part of SWI-Prolog
   2
   3    Author:        Jan Wielemaker
   4    E-mail:        J.Wielemaker@vu.nl
   5    WWW:           www.swi-prolog.org
   6    Copyright (c)  2013-2015, University of Amsterdam
   7                              VU University Amsterdam
   8    All rights reserved.
   9
  10    Redistribution and use in source and binary forms, with or without
  11    modification, are permitted provided that the following conditions
  12    are met:
  13
  14    1. Redistributions of source code must retain the above copyright
  15       notice, this list of conditions and the following disclaimer.
  16
  17    2. Redistributions in binary form must reproduce the above copyright
  18       notice, this list of conditions and the following disclaimer in
  19       the documentation and/or other materials provided with the
  20       distribution.
  21
  22    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  25    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  26    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  27    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  28    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  29    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  30    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  31    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  32    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33    POSSIBILITY OF SUCH DAMAGE.
  34*/
  35
  36:- module('$dicts',
  37          [ '.'/3                               % +Left, +Right, -Result
  38          ]).
  39
  40%!  .(+R, +L, -Result)
  41%
  42%   Evaluate dot expressions. Note that  '$get_dict_ex' fails if the
  43%   first argument is not a dict or the second is not a valid key or
  44%   unbound.
  45
  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    ).
  59
  60
  61%!  eval_dict_function(+Func, +Tag, +Dict, -Value)
  62%
  63%   Test for predefined functions on dicts or evaluate a user-defined
  64%   function.
  65
  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).
  80
  81
  82%!  put_dict_path(+KeyPath, +Dict, +Value, -NewDict)
  83%
  84%   Add/replace  a  value  according  to  a  path  definition.  Path
  85%   segments are separated using '/'.
  86
  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                 /*******************************
 116                 *             REGISTER         *
 117                 *******************************/
 118
 119%!  system:term_expansion(+TermIn, -TermOut)
 120%
 121%   Support := syntax for defining new functions.
 122%
 123%   @tbd    Modify to term_expansion/4, including position
 124%           management
 125
 126% note that we need FHead because using a term there rewrites the
 127% clauses using function expansion.
 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.