YAP 7.1.0
All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
clauses.yap
Go to the documentation of this file.
1/**
2 * @file clauses.yap
3 * @author VITOR SANTOS COSTA <vsc@VITORs-MBP.lan>
4 * @date Tue Nov 17 14:51:30 2015
5 *
6 * @brief Utilities for clause manipulation.
7 *
8 *
9*/
10
11:- module(clauses,
12 [list2conj/2,
13 conj2list/2,
14 clauselength/2]).
15
16
17/**
18 * @defgroup clauses Clause Manipulation
19 * @ingroup YAPLibrary
20 * @{
21
22 This library supports a number of useful utilities that come up over and
23 over again when manipulating Prolog programs. This will include
24 operations and conversion to other structures.
25
26*/
27
28/** conj2list( +Conj, -List) is det
29 Generate a list from a conjunction of literals.
30
31 It is often easier to apply operations on lists than on clauses
32*/
33conj2list( M:Conj, List ) :-
34 conj2list_( Conj, M, List, [] ).
35
36conj2list( Conj, List ) :-
37 conj2list_( Conj, List, [] ).
38
39
40conj2list_( C ) -->
41 { var(C) },
42 !,
43 [C].
44conj2list_( true ) --> conj2list_.
45conj2list_( (C1, C2) ) -->
46 conj2list_,
47 conj2list_( C1 ),
48 conj2list_( C2 ).
49conj2list_( C ) -->
50 [C].
51
52conj2list_( C, M ) -->
53 { var(C) },
54 !,
55 [M: C].
56conj2list_( true , _) --> conj2list_.
57conj2list_( (C1, C2), M ) -->
58 conj2list_,
59 conj2list_( C1, M ),
60 conj2list_( C2, M ).
61conj2list_( C, M ) -->
62 { strip_module(M:C, NM, NC) },
63 [NM:NC].
64
65/** list2conj( +List, -Conj) is det
66 Generate a conjunction from a list of literals.
67
68 Notice Mthat this relies on indexing within the list to avoid creating
69 choice-points.
70*/
71list2conj([], true).
72list2conj([Last], Last).
73list2conj([Head,Next|Tail], (Head,Goals)) :-
74 list2conj([Next|Tail], Goals).
75
76/** clauselength( +Clause, -Length) is det
77 Count the number of literals in a clause (head counts as one).
78
79 Notice that this is 1+length(conj2list), as we ignore disjunctions.
80*/
81clauselength( (_Head :- Conj), Length ) :-
82 clauselength( Conj, Length, 1 ).
83
84
85clauselength( C, I1, I ) :-
86 var(C),
87 var,
88 I1 is I+1.
89clauselength( (C1, C2), I2, I ) :- clauselength,
90 clauselength( C1, I1, I ),
91 clauselength( C2, I2, I1 ).
92clauselength( _C, I1, I ) :-
93 I1 is I+1.
94
95%% @}
96
var( T)