View source with formatted 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    ->  (   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(_,_)).
   66
   67
   68%!  eval_dict_function(+Func, +Tag, +Dict, -Value)
   69%
   70%   Test for predefined functions on dicts or evaluate a user-defined
   71%   function.
   72
   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).
  103
  104
  105%!  put_dict_path(+KeyPath, +Dict, +Value, -NewDict)
  106%
  107%   Add/replace  a  value  according  to  a  path  definition.  Path
  108%   segments are separated using '/'.
  109
  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).
  136
  137%!  get_dict_path(+Path, +Dict, -Value)
  138
  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                 /*******************************
  150                 *             REGISTER         *
  151                 *******************************/
  152
  153%!  system:term_expansion(+TermIn, -TermOut)
  154%
  155%   Support := syntax for defining new functions.
  156%
  157%   @tbd    Modify to term_expansion/4, including position
  158%           management
  159
  160% note that we need FHead because using a term there rewrites the
  161% clauses using function expansion.
  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