1/*  Part of Extended Libraries for SWI-Prolog
    2
    3    Author:        Edison Mera Menendez
    4    E-mail:        efmera@gmail.com
    5    WWW:           https://github.com/edisonm/xlibrary
    6    Copyright (C): 2014, Process Design Center, Breda, The Netherlands.
    7    All rights reserved.
    8
    9    Redistribution and use in source and binary forms, with or without
   10    modification, are permitted provided that the following conditions
   11    are met:
   12
   13    1. Redistributions of source code must retain the above copyright
   14       notice, this list of conditions and the following disclaimer.
   15
   16    2. Redistributions in binary form must reproduce the above copyright
   17       notice, this list of conditions and the following disclaimer in
   18       the documentation and/or other materials provided with the
   19       distribution.
   20
   21    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   22    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   23    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   24    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   25    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   26    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   27    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   28    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   29    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   30    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   31    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   32    POSSIBILITY OF SUCH DAMAGE.
   33*/
   34
   35:- module(compound_expand,
   36          [op(1, fx, '$compound_expand') % Used to detect expansion modules
   37          ]).   38
   39/* This module allows to define compositional term and goal expansions,
   40   using this module in a module that already defines the predicates
   41   term_expansion/2/4 and goal_expansion/2/4 but don't export them.
   42
   43   The composition of expansions is instrumental to grammar and syntax
   44   extensions, which is the key point of Ciao Prolog, but not supported in SWI
   45   Prolog. We do not need to deal with all the complexity that the Ciao package
   46   system have, so with this helper the port of Ciao Packages to SWI Prolog can
   47   be achieved smoothly and such modules can be used in SWI Programs that do not
   48   requires the Ciao dialect.
   49*/
   50
   51:- use_module(library(expansion_module)).   52:- use_module(library(remove_dups)).   53
   54:- multifile
   55    system:term_expansion/4,
   56    system:goal_expansion/4.   57
   58:- public implemented_pi/1.   59:- meta_predicate implemented_pi(:).   60implemented_pi(M:F/A) :-
   61    functor(H, F, A),
   62    % Can not use current_module/1 at this stage: --EMM
   63    once(predicate_property(M:H, visible)),
   64    \+ predicate_property(M:H, imported_from(_)).
   65
   66collect_expansors(M, ExpansorName, ML) :-
   67    findall(EM-PI,
   68            ( expansion_module(M, EM),
   69              ( implemented_pi(EM:ExpansorName/4)
   70              ->PI=[ExpansorName/4|PIT],
   71                ( implemented_pi(EM:ExpansorName/2)
   72                ->PIT = [ExpansorName/2]
   73                ; PIT = []
   74                )
   75              ; PI=[ExpansorName/2]
   76              )
   77            ), MD),
   78    remove_dups(MD, ML).
   79
   80:- thread_local
   81    lock_expansion/1.   82
   83call_lock(Goal, ID) :-
   84    \+ lock_expansion(ID),
   85    setup_call_cleanup(assertz(lock_expansion(ID), Ref),
   86                       Goal,
   87                       erase(Ref)).
   88
   89type_expansors(term, term_expansion, call_term_expansion).
   90type_expansors(goal, goal_expansion, call_goal_expansion).
   91
   92do_compound_expansion(M, Type, Term1, Pos1, Term, Pos) :-
   93    type_expansors(Type, Expansor, Closure),
   94    collect_expansors(M, Expansor, ML),
   95    call('$expand':Closure, ML, Term1, Pos1, Term, Pos), !.
   96
   97do_compound_expansion(Type, Term1, Pos1, Term, Pos) :-
   98    '$current_source_module'(M),
   99    M \= user, % Compound expansions not supported in user module
  100    do_compound_expansion(M, Type, Term1, Pos1, Term, Pos).
  101
  102compound_expansion(Type, Term1, Pos1, Term, Pos) :-
  103    call_lock(do_compound_expansion(Type, Term1, Pos1, Term, Pos), Type).
  104
  105system:goal_expansion(Goal1, Pos1, Goal, Pos) :-
  106    do_compound_expansion(goal, Goal1, Pos1, Goal, Pos).
  107
  108:- thread_local compounding/0.  109
  110system:term_expansion(Term1, Pos1, Term, Pos) :-
  111    \+ compounding,
  112    compound_expansion(term, Term1, Pos1, Term2, Pos2),
  113    Term1 \== Term2,
  114    [Term1] \== Term2,
  115    % continue with other expansions:
  116    setup_call_cleanup(assertz(compounding),
  117                       ( system:term_expansion(Term2, Pos2, Term, Pos)
  118                       ->true
  119                       ; Term = Term2,
  120                         Pos  = Pos2
  121                       ),
  122                       retractall(compounding))