This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
yap-6.3/library/clauses.yap

97 lines
2.0 KiB
Plaintext
Raw Normal View History

2015-11-18 15:06:25 +00:00
/**
* @file clauses.yap
* @author VITOR SANTOS COSTA <vsc@VITORs-MBP.lan>
* @date Tue Nov 17 14:51:30 2015
*
* @brief Utilities for clause manipulation.
*
*
*/
2015-03-04 09:52:06 +00:00
:- module(clauses,
[list2conj/2,
conj2list/2,
clauselength/2]).
%% @{
2015-03-04 09:52:06 +00:00
/**
2015-11-18 15:06:25 +00:00
* @defgroup clauses Clause Manipulation
* @ingroup library
2015-03-04 09:52:06 +00:00
This library supports a number of useful utilities that come up over and
over again when manipulating Prolog programs. This will include
operations and conversion to other structures.
@author Vitor Santos Costa
*/
/** conj2list( +Conj, -List) is det
Generate a list from a conjunction of literals.
It is often easier to apply operations on lists than on clauses
*/
conj2list( M:Conj, List ) :-
conj2list_( Conj, M, List, [] ).
2015-03-04 09:52:06 +00:00
conj2list( Conj, List ) :-
conj2list_( Conj, List, [] ).
2015-03-04 09:52:06 +00:00
conj2list_( C ) -->
2015-03-04 09:52:06 +00:00
{ var(C) },
!,
[C].
conj2list_( true ) --> !.
conj2list_( (C1, C2) ) -->
2015-03-04 09:52:06 +00:00
!,
conj2list_( C1 ),
conj2list_( C2 ).
conj2list_( C ) -->
2015-03-04 09:52:06 +00:00
[C].
conj2list_( C, M ) -->
{ var(C) },
!,
[M: C].
conj2list_( true , _) --> !.
conj2list_( (C1, C2), M ) -->
!,
conj2list_( C1, M ),
conj2list_( C2, M ).
conj2list_( C, M ) -->
{ strip_module(M:C, NM, NC) },
[NM:NC].
/** list2conj( +List, -Conj) is det
2015-03-04 09:52:06 +00:00
Generate a conjunction from a list of literals.
Notice Mthat this relies on indexing within the list to avoid creating
2015-03-04 09:52:06 +00:00
choice-points.
*/
list2conj([], true).
list2conj([Last], Last).
list2conj([Head,Next|Tail], (Head,Goals)) :-
list2conj([Next|Tail], Goals).
/** clauselength( +Clause, -Length) is det
Count the number of literals in a clause (head counts as one).
Notice that this is 1+length(conj2list), as we ignore disjunctions.
*/
clauselength( (_Head :- Conj), Length ) :-
clauselength( Conj, Length, 1 ).
clauselength( C, I1, I ) :-
{ var(C) },
!,
I1 is I+1.
clauselength( (C1, C2), I2, I ) :- !,
clauselength( C1, I1, I ),
clauselength( C2, I2, I1 ).
clauselength( _C, I1, I ) :-
I1 is I+1.
%%@}