%% list_util.mod %% %% Copyright (c) 1991,1994,1995 by Carnegie Mellon University %% %% Copyright (c) 1994,1995 by AT&T Bell Laboratories %% %% Copyright (c) 1995,1996 by the University of Pennsylvania %% %% For information about the distributaion, copying, and modification %% %% of this software, please read the file COPYING located in the root %% %% directory of this distribnutaion. If you did not receive the file %% %% COPYING write to Philip Wickline at the address below. %% %% 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 file %% %% COPYING for more details. %% %% For information about the structure of the Terzo lambdaProlog %% %% implementation, see the file src/README in this distribution. Please %% %% address any questions about this code or the Terzo lambdaProlog %% %% implementation to Philip Wickline at . %% %% Authors: %% %% Amy Felty %% %% Frank Pfenning %% %% Philip Wickline %% module ListUtil. accumulate List. type memb A -> list A -> o. % memb X L % Succeeds if X is a member of L. In contrast to member, % this will succeed as often as X unifies with members of L. memb X (X :: L). memb X (Y :: L) :- memb X L. type member A -> list A -> o. % member X L % succeeds if X is a member of L. In contrast to memb, % this will succeed only once: for the first unifier of % X with a member of L. member X (X :: L) :- !. member X (Y :: L) :- member X L. type append list A -> list A -> list A -> o. % append L K M % Succeeds if M is the result of appending K to L. append nil K K. append (X :: L) K (X :: M) :- append L K M. type join list A -> list A -> list A -> o. % join L K M % Join is similar to append except that members of L that % already appear in K are not appended to form M. Note % that this `check' will do unification! join nil K K. join (X :: L) K M :- memb X K, !, join L K M. join (X :: L) K (X :: M) :- join L K M. type memb_and_rest A -> list A -> list A -> o. % memb_and_rest X L M % Succeeds for every occurrence of X in L, where M is the % result of removing that occurrence from L. memb_and_rest A (A :: Rest) Rest. memb_and_rest A (B :: Tail) (B :: Rest) :- memb_and_rest A Tail Rest. type nth_item int -> A -> list A -> o. % nth_item N X L % Succeeds if the Nth item of L is X, where counting starts % at 1. For N = 0 it behaves like memb. nth_item 0 A List :- !, memb A List. nth_item 1 A (B::Rest) :- !, A = B. nth_item N A (B::Tail) :- M is (N - 1), nth_item M A Tail. type nth_and_rest int -> A -> list A -> list A -> o. % nth_and_rest N X L Rest % Succeeds if the Nth item of L is X, and Rest is the rest % of L. Counting starts at 1. For N = 0, it behaves like % memb_and_rest. nth_and_rest 0 A List Rest :- !, memb_and_rest A List Rest. nth_and_rest 1 A (B::Rest) Rest :- !, A = B. nth_and_rest N A (B::Tail) (B::Rest) :- M is (N - 1), nth_and_rest M A Tail Rest. %%%%%%% My Utilities! type sel A -> list A -> list A -> o. % alias for memb_and_rest sel A (A :: Rest) Rest. sel A (B :: Tail) (B :: Rest) :- sel A Tail Rest. type if_then_else o -> o -> o -> o. % if_then_else Cond LeftGoal RightGoal if_then_else C L R :- C,!,L. if_then_else C L R :- R. type if o -> o -> o -> o. % if Cond type then o -> o. % then LeftGoal type else o -> o. % else RightGoal if C (then T) (else E) :- C,!,T. if C (then T) (else E) :- E. type write A -> o. write T :- term_to_string T S, print S. type msg string -> A -> o. type generic_spy string -> list o -> o. type print_spy_terms list o -> o. print_spy_terms nil. print_spy_terms ((msg A B)::Rest) :- print A ,write B ,print "\n", print_spy_terms Rest. generic_spy Title List:- print "-----------------------\n", print Title ,print "\n" , print_spy_terms List.