/usr/lib/swi-prolog/library/rewrite.pl is in swi-prolog-nox 6.6.6-5.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 | /* Part of SWI-Prolog
Author: Jan Wielemaker
E-mail: J.Wielemaker@uva.nl
WWW: http://www.swi-prolog.org
Copyright (C): 2010, University of Amsterdam
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
As a special exception, if you link this library with other files,
compiled with a Free Software compiler, to produce an executable, this
library does not by itself cause the resulting executable to be covered
by the GNU General Public License. This exception does not however
invalidate any other reasons why the executable file might be covered by
the GNU General Public License.
*/
:- module(rewrite,
[ rewrite/2, % +Rule, +Input
rew_term_expansion/2,
rew_goal_expansion/2,
op(1200, xfx, (::=))
]).
:- use_module(library(quintus)).
:- meta_predicate
rewrite(1, +).
/*******************************
* COMPILATION *
*******************************/
rew_term_expansion((Rule ::= RuleBody), (Head :- Body)) :-
translate(RuleBody, Term, Body0),
simplify(Body0, Body),
Rule =.. [Name|List],
Head =.. [Name,Term|List].
rew_goal_expansion(rewrite(To, From), Goal) :-
nonvar(To),
To = \Rule,
callable(Rule),
Rule =.. [Name|List],
Goal =.. [Name,From|List].
/*******************************
* TOPLEVEL *
*******************************/
%% rewrite(:To, +From)
%
% Invoke the term-rewriting system
rewrite(M:T, From) :-
( var(T)
-> From = T
; T = \Rule
-> Rule =.. [Name|List],
Goal =.. [Name,From|List],
M:Goal
; match(T, M, From)
).
match(Rule, M, From) :-
translate(Rule, From, Code),
M:Code.
translate(Var, Var, true) :-
var(Var), !.
translate((\Command, !), Var, (Goal, !)) :- !,
( callable(Command),
Command =.. [Name|List]
-> Goal =.. [Name,Var|List]
; Goal = rewrite(\Command, Var)
).
translate(\Command, Var, Goal) :- !,
( callable(Command),
Command =.. [Name|List]
-> Goal =.. [Name,Var|List]
; Goal = rewrite(\Command, Var)
).
translate(Atomic, Atomic, true) :-
atomic(Atomic), !.
translate(C, _, Cmd) :-
command(C, Cmd), !.
translate((A, B), T, Code) :-
( command(A, Cmd)
-> !, translate(B, T, C),
Code = (Cmd, C)
; command(B, Cmd)
-> !, translate(A, T, C),
Code = (C, Cmd)
).
translate(Term0, Term, Command) :-
functor(Term0, Name, Arity),
functor(Term, Name, Arity),
translate_args(0, Arity, Term0, Term, Command).
translate_args(N, N, _, _, true) :- !.
translate_args(I0, Arity, T0, T1, (C0,C)) :-
I is I0 + 1,
arg(I, T0, A0),
arg(I, T1, A1),
translate(A0, A1, C0),
translate_args(I, Arity, T0, T1, C).
command(0, _) :- !, % catch variables
fail.
command({A}, A).
command(!, !).
/*******************************
* SIMPLIFY *
*******************************/
%% simplify(+Raw, -Simplified)
%
% Get rid of redundant `true' goals generated by translate/3.
simplify(V, V) :-
var(V), !.
simplify((A0,B), A) :-
B == true, !,
simplify(A0, A).
simplify((A,B0), B) :-
A == true, !,
simplify(B0, B).
simplify((A0, B0), C) :- !,
simplify(A0, A),
simplify(B0, B),
( ( A \== A0
; B \== B0
)
-> simplify((A,B), C)
; C = (A,B)
).
simplify(X, X).
/*******************************
* XREF *
*******************************/
:- multifile
prolog:called_by/2.
prolog:called_by(rewrite(Spec, _Term), Called) :-
findall(G+1, sub_term(\G, Spec), Called).
|