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:           http://www.swi-prolog.org
    6    Copyright (c)  2010-2020, University of Amsterdam
    7                              CWI, 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(rewrite_term,
   37          [ rewrite_term/2,                  % +Rule, +Input
   38            rew_term_expansion/2,
   39            rew_goal_expansion/2,
   40
   41            op(1200, xfx, (::=))
   42          ]).   43:- autoload(library(occurs),[sub_term/2]).   44
   45:- meta_predicate
   46    rewrite_term(1, +).   47
   48                 /*******************************
   49                 *          COMPILATION         *
   50                 *******************************/
   51
   52rew_term_expansion((Rule ::= RuleBody), (Head :- Body)) :-
   53    translate(RuleBody, Term, Body0),
   54    simplify(Body0, Body),
   55    Rule =.. [Name|List],
   56    Head =.. [Name,Term|List].
   57
   58rew_goal_expansion(rewrite_term(To, From), Goal) :-
   59    nonvar(To),
   60    To = \Rule,
   61    callable(Rule),
   62    Rule =.. [Name|List],
   63    Goal =.. [Name,From|List].
   64
   65
   66                 /*******************************
   67                 *            TOPLEVEL          *
   68                 *******************************/
 rewrite_term(:To, +From)
Invoke the term-rewriting system
   74rewrite_term(M:T, From) :-
   75    (   var(T)
   76    ->  From = T
   77    ;   T = \Rule
   78    ->  Rule =.. [Name|List],
   79        Goal =.. [Name,From|List],
   80        M:Goal
   81    ;   match(T, M, From)
   82    ).
   83
   84match(Rule, M, From) :-
   85    translate(Rule, From, Code),
   86    M:Code.
   87
   88translate(Var, Var, true) :-
   89    var(Var),
   90    !.
   91translate((\Command, !), Var, (Goal, !)) :-
   92    !,
   93    (   callable(Command),
   94        Command =.. [Name|List]
   95    ->  Goal =.. [Name,Var|List]
   96    ;   Goal = rewrite(\Command, Var)
   97    ).
   98translate(\Command, Var, Goal) :-
   99    !,
  100    (   callable(Command),
  101        Command =.. [Name|List]
  102    ->  Goal =.. [Name,Var|List]
  103    ;   Goal = rewrite(\Command, Var)
  104    ).
  105translate(Atomic, Atomic, true) :-
  106    atomic(Atomic),
  107    !.
  108translate(C, _, Cmd) :-
  109    command(C, Cmd),
  110    !.
  111translate((A, B), T, Code) :-
  112    (   command(A, Cmd)
  113    ->  !, translate(B, T, C),
  114        Code = (Cmd, C)
  115    ;   command(B, Cmd)
  116    ->  !, translate(A, T, C),
  117        Code = (C, Cmd)
  118    ).
  119translate(Term0, Term, Command) :-
  120    functor(Term0, Name, Arity),
  121    functor(Term, Name, Arity),
  122    translate_args(0, Arity, Term0, Term, Command).
  123
  124translate_args(N, N, _, _, true) :- !.
  125translate_args(I0, Arity, T0, T1, (C0,C)) :-
  126    I is I0 + 1,
  127    arg(I, T0, A0),
  128    arg(I, T1, A1),
  129    translate(A0, A1, C0),
  130    translate_args(I, Arity, T0, T1, C).
  131
  132command(0, _) :-                       % catch variables
  133    !,
  134    fail.
  135command({A}, A).
  136command(!, !).
  137
  138                 /*******************************
  139                 *            SIMPLIFY          *
  140                 *******************************/
 simplify(+Raw, -Simplified)
Get rid of redundant `true' goals generated by translate/3.
  146simplify(V, V) :-
  147    var(V),
  148    !.
  149simplify((A0,B), A) :-
  150    B == true,
  151    !,
  152    simplify(A0, A).
  153simplify((A,B0), B) :-
  154    A == true,
  155    !,
  156    simplify(B0, B).
  157simplify((A0, B0), C) :-
  158    !,
  159    simplify(A0, A),
  160    simplify(B0, B),
  161    (   (   A \== A0
  162        ;   B \== B0
  163        )
  164    ->  simplify((A,B), C)
  165    ;   C = (A,B)
  166    ).
  167simplify(X, X).
  168
  169                 /*******************************
  170                 *             XREF             *
  171                 *******************************/
  172
  173:- multifile
  174    prolog:called_by/2.  175
  176prolog:called_by(rewrite(Spec, _Term), Called) :-
  177    findall(G+1, sub_term(\G, Spec), Called)