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