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, VU University 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(coinduction, 36 [ (coinductive)/1, 37 op(1150, fx, (coinductive)) 38 ]). 39:- autoload(library(error),[instantiation_error/1,must_be/2]). 40 41 42/** <module> Co-Logic Programming 43 44This simple module implements the directive coinductive/1 as described 45in "Co-Logic Programming: Extending Logic Programming with Coinduction" 46by Luke Simon et al. The idea behind coinduction is that a goal succeeds 47if it unifies to a parent goal. This enables some interesting programs, 48notably on infinite trees (cyclic terms). 49 50 == 51 :- use_module(library(coinduction)). 52 53 :- coinductive p/1. 54 55 p([1|T]) :- p(T). 56 == 57 58This predicate is true for any cyclic list containing only 1-s, 59regardless of the cycle-length. 60 61@bug Programs mixing normal predicates and coinductive predicates must 62 be _stratified_. The theory does not apply to normal Prolog calling 63 coinductive predicates, calling normal Prolog predicates, etc. 64 65 Stratification is not checked or enforced in any other way and thus 66 left as a responsibility to the user. 67@see "Co-Logic Programming: Extending Logic Programming with Coinduction" 68 by Luke Simon et al. 69*/ 70 71:- multifile 72 system:term_expansion/2, 73 coinductive_declaration/2. % Head, Module 74 75%! head(+Term, -QHead) is semidet. 76% 77% Must be first to allow reloading! 78 79head(Var, _) :- 80 var(Var), !, fail. 81head((H:-_B), Head) :- 82 !, 83 head(H, Head). 84head(H, Head) :- 85 ( H = _:_ 86 -> Head = H 87 ; prolog_load_context(module, M), 88 Head = M:H 89 ). 90 91%! coinductive(:Spec) 92% 93% The declaration :- coinductive name/arity, ... defines 94% predicates as _coinductive_. The predicate definition is wrapped 95% such that goals unify with their ancestors. This directive must 96% precede all clauses of the predicate. 97 98coinductive(Spec) :- 99 throw(error(context_error(nodirective, coinductive(Spec)), _)). 100 101expand_coinductive_declaration(Spec, Clauses) :- 102 prolog_load_context(module, Module), 103 phrase(expand_specs(Spec, Module), Clauses). 104 105expand_specs(Var, _) --> 106 { var(Var), 107 !, 108 instantiation_error(Var) 109 }. 110expand_specs(M:Spec, _) --> 111 !, 112 expand_specs(Spec, M). 113expand_specs((A,B), Module) --> 114 !, 115 expand_specs(A, Module), 116 expand_specs(B, Module). 117expand_specs(Head, Module) --> 118 { valid_pi(Head, Name, Arity), 119 functor(GenHead, Name, Arity) 120 }, 121 [ coinduction:coinductive_declaration(GenHead, Module) ]. 122 123 124valid_pi(Name/Arity, Name, Arity) :- 125 must_be(atom, Name), 126 must_be(integer, Arity). 127 128 129%! wrap_coinductive(+Head, +Term, -Clauses) is det. 130% 131% Create a wrapper. The first clause deal with the case where we 132% already created the wrapper. The second creates the wrapper and 133% the first clause. 134 135wrap_coinductive(Pred, Term, Clause) :- 136 current_predicate(_, Pred), 137 !, 138 rename_clause(Term, 'coinductive ', Clause). 139wrap_coinductive(Pred, Term, [Wrapper_1,Wrapper_2,FirstClause]) :- 140 Pred = M:Head, 141 functor(Head, Name, Arity), 142 length(Args, Arity), 143 GenHead =.. [Name|Args], 144 atom_concat('coinductive ', Name, WrappedName), 145 WrappedHead =.. [WrappedName|Args], 146 Wrapper_1 = (GenHead :- 147 prolog_current_frame(F), 148 prolog_frame_attribute(F, parent, FP), 149 prolog_frame_attribute(FP, parent_goal, M:GenHead)), 150 Wrapper_2 = (GenHead :- WrappedHead, coinduction:no_lco), 151 rename_clause(Term, 'coinductive ', FirstClause). 152 153:- public no_lco/0. 154 155no_lco. % true, but do not optimize away 156 157%! rename_clause(+Clause, +Prefix, -Renamed) is det. 158% 159% Rename a clause by prefixing its old name wit h Prefix. 160 161rename_clause((Head :- Body), Prefix, (NewHead :- Body)) :- 162 !, 163 rename_clause(Head, Prefix, NewHead). 164rename_clause(M:Head, Prefix, M:NewHead) :- 165 rename_clause(Head, Prefix, NewHead). 166rename_clause(Head, Prefix, NewHead) :- 167 Head =.. [Name|Args], 168 atom_concat(Prefix, Name, WrapName), 169 NewHead =.. [WrapName|Args]. 170 171 172 /******************************* 173 * EXPANSION HOOKS * 174 *******************************/ 175 176systemterm_expansion((:- coinductive(Spec)), Clauses) :- 177 expand_coinductive_declaration(Spec, Clauses). 178systemterm_expansion(Term, Wrapper) :- 179 head(Term, Module:Head), 180 coinductive_declaration(Head, Module), 181 wrap_coinductive(Module:Head, Term, Wrapper)