1/* Part of Refactoring Tools for SWI-Prolog 2 3 Author: Edison Mera 4 E-mail: efmera@gmail.com 5 WWW: https://github.com/edisonm/refactor 6 Copyright (C): 2013, Process Design Center, Breda, The Netherlands. 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(gcb, [greatest_common_binding/7, 36 greatest_common_binding/8]). 37 38:- use_module(library(substitute), [substitute_value/4]). 39 40greatest_common_binding(Term1, Into1, Term, Into, Skip) --> 41 greatest_common_binding(Term1, Term1, Into1, Term, Into, Skip). 42 43greatest_common_binding(SubTerm1, Term1, Into1, Term, Into, Skip) --> 44 ( { \+memberchk(SubTerm1, Skip), 45 substitute_value(SubTerm1, Var, Into1, Into2), 46 Into1\==Into2 47 } 48 ->{substitute_value(SubTerm1, Var, Term1, Term2)}, 49 [Var=SubTerm] 50 ; {Term2=Term1, Into2=Into1 } 51 ), 52 ( {compound(SubTerm1)}, 53 greatest_common_binding(1, SubTerm1, Term2, Into2, SubTerm, Term, Into, Skip), 54 {Into2\==Into} 55 ->[] 56 ; {SubTerm=SubTerm1, Term=Term2, Into=Into2} 57 ). 58 59greatest_common_binding(N, SubTerm1, Term1, Into1, SubTerm, Term, Into, Skip) --> 60 {arg(N, SubTerm1, Arg)}, 61 !, 62 greatest_common_binding(Arg, SubTerm1-Term1, Into1, SubTerm2-Term2, Into2, Skip), 63 {succ(N, N1)}, 64 greatest_common_binding(N1, SubTerm2, Term2, Into2, SubTerm, Term, Into, Skip). 65greatest_common_binding(_, SubTerm, Term, Into, SubTerm, Term, Into, _) --> []. 66 67/* 68% Fixpoint algorithm: 69substitute_olist(SubstList, Term1, Term) :- 70 ( substitute_olist_(SubstList, Term1, Term2), 71 Term1 \== Term2 -> 72 substitute_olist(SubstList, Term2, Term) 73 ; Term1 = Term 74 ). 75 76substitute_olist_(Tail) --> {var(Tail)}, !. 77substitute_olist_([Var=Val|Tail]) --> 78 substitute_value(Val, Var), 79 substitute_olist_(Tail). 80 81pick_tail(Tail, Tail, Tail). 82 83greatest_common_binding(SubTerm1, SubTerm, Term1, Into1, Term, Into, Skip) --> 84 ( { % nonvar(SubTerm1), 85 % nonvar(Into1), 86 \+memberchk(SubTerm1, Skip)} 87 ->( { substitute_value(SubTerm1, Var, Into1, Into2), 88 Into1\==Into2} 89 ->{substitute_value(SubTerm1, Var, Term1, Term2)}, 90 [Var=SubTerm] 91 ; {Term2=Term1, Into2 = Into1 } 92 ), 93 ( {compound(SubTerm1)}, 94 greatest_common_binding(1, SubTerm1, SubTerm, Term2, Into2, Term, Into, Skip), 95 {Into2\==Into} 96 ->[] 97 ; {SubTerm=SubTerm1, Term=Term2, Into=Into2 } 98 ) 99 ; {SubTerm=SubTerm1, Term=Term1, Into=Into1 } 100 ). 101 102greatest_common_binding(N, SubTerm1, SubTerm, Term1, Into1, Term, Into, Skip) --> 103 {arg(N, SubTerm1, Arg)}, 104 !, 105 pick_tail(Tail), 106 greatest_common_binding(Arg, _, Term1, Into1, Term2, Into2, Skip), 107 {substitute_olist(Tail, Term2, Term3), 108 substitute_olist(Tail, SubTerm1, SubTerm2), 109 succ(N, N1)}, 110 greatest_common_binding(N1, SubTerm2, SubTerm, Term3, Into2, Term, Into, Skip). 111greatest_common_binding(_, SubTerm, SubTerm, Term, Into, Term, Into, _) --> []. 112 113% Fixpoint algorithm: 114substitute_olist(SubstList, Term1, Term) :- 115 ( substitute_olist_(SubstList, Term1, Term2), 116 Term1 \== Term2 -> 117 substitute_olist(SubstList, Term2, Term) 118 ; Term1 = Term 119 ). 120 121substitute_olist_(Tail) --> {var(Tail)}, !. 122substitute_olist_([Var=Val|Tail]) --> 123 substitute_value(Val, Var), 124 substitute_olist_(Tail). 125 126pick_tail(Tail, Tail, Tail). 127*/