/usr/lib/swi-prolog/library/coinduction.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 162 163 164 165 166 167 168 169 | /* Part of SWI-Prolog
Author: Jan Wielemaker
E-mail: J.Wielemaker@cs.vu.nl
WWW: http://www.swi-prolog.org
Copyright (C): 2010, VU University 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(coinduction,
[ (coinductive)/1,
op(1150, fx, (coinductive))
]).
:- use_module(library(error)).
/** <module> Co-Logic Programming
This simple module implements the directive coinductive/1 as described
in "Co-Logic Programming: Extending Logic Programming with Coinduction"
by Luke Simon et al. The idea behind coinduction is that a goal succeeds
if it unifies to a parent goal. This enables some interesting programs,
notably on infinite trees (cyclic terms).
==
:- use_module(library(coinduction)).
:- coinductive p/1.
p([1|T]) :- p(T).
==
This predicate is true for any cyclic list containing only 1-s,
regardless of the cycle-length.
@bug Programs mixing normal predicates and coinductive predicates must
be _stratified_. The theory does not apply to normal Prolog calling
coinductive predicates, calling normal Prolog predicates, etc.
Stratification is not checked or enforced in any other way and thus
left as a responsibility to the user.
@see "Co-Logic Programming: Extending Logic Programming with Coinduction"
by Luke Simon et al.
*/
:- multifile
system:term_expansion/2,
coinductive_declaration/2. % Head, Module
%% head(+Term, -QHead) is semidet.
%
% Must be first to allow reloading!
head(Var, _) :-
var(Var), !, fail.
head((H:-_B), Head) :- !,
head(H, Head).
head(H, Head) :-
( H = _:_
-> Head = H
; prolog_load_context(module, M),
Head = M:H
).
%% coinductive(:Spec)
%
% The declaration :- coinductive name/arity, ... defines
% predicates as _coinductive_. The predicate definition is wrapped
% such that goals unify with their ancestors. This directive must
% preceed all clauses of the predicate.
coinductive(Spec) :-
throw(error(context_error(nodirective, coinductive(Spec)), _)).
expand_coinductive_declaration(Spec, Clauses) :-
prolog_load_context(module, Module),
phrase(expand_specs(Spec, Module), Clauses).
expand_specs(Var, _) -->
{ var(Var), !,
instantiation_error(Var)
}.
expand_specs(M:Spec, _) --> !,
expand_specs(Spec, M).
expand_specs((A,B), Module) --> !,
expand_specs(A, Module),
expand_specs(B, Module).
expand_specs(Head, Module) -->
{ valid_pi(Head, Name, Arity),
functor(GenHead, Name, Arity)
},
[ coinduction:coinductive_declaration(GenHead, Module) ].
valid_pi(Name/Arity, Name, Arity) :-
must_be(atom, Name),
must_be(integer, Arity).
%% wrap_coinductive(+Head, +Term, -Clauses) is det.
%
% Create a wrapper. The first clause deal with the case where we
% already created the wrapper. The second creates the wrapper and
% the first clause.
wrap_coinductive(Pred, Term, Clause) :-
current_predicate(_, Pred), !,
rename_clause(Term, 'coinductive ', Clause).
wrap_coinductive(Pred, Term, [Wrapper_1,Wrapper_2,FirstClause]) :-
Pred = M:Head,
functor(Head, Name, Arity),
length(Args, Arity),
GenHead =.. [Name|Args],
atom_concat('coinductive ', Name, WrappedName),
WrappedHead =.. [WrappedName|Args],
Wrapper_1 = (GenHead :-
prolog_current_frame(F),
prolog_frame_attribute(F, parent, FP),
prolog_frame_attribute(FP, parent_goal, M:GenHead)),
Wrapper_2 = (GenHead :- WrappedHead, coinduction:no_lco),
rename_clause(Term, 'coinductive ', FirstClause).
:- public no_lco/0.
no_lco. % true, but do not optimize away
%% rename_clause(+Clause, +Prefix, -Renamed) is det.
%
% Rename a clause by prefixing its old name wit h Prefix.
rename_clause((Head :- Body), Prefix, (NewHead :- Body)) :- !,
rename_clause(Head, Prefix, NewHead).
rename_clause(M:Head, Prefix, M:NewHead) :-
rename_clause(Head, Prefix, NewHead).
rename_clause(Head, Prefix, NewHead) :-
Head =.. [Name|Args],
atom_concat(Prefix, Name, WrapName),
NewHead =.. [WrapName|Args].
/*******************************
* EXPANSION HOOKS *
*******************************/
system:term_expansion((:- coinductive(Spec)), Clauses) :-
expand_coinductive_declaration(Spec, Clauses).
system:term_expansion(Term, Wrapper) :-
head(Term, Module:Head),
coinductive_declaration(Head, Module),
wrap_coinductive(Module:Head, Term, Wrapper).
|