YAP
7.1.0
Toggle main menu visibility
Main Page
Related Pages
Modules
Classes
Class List
Class Index
Class Hierarchy
Class Members
All
a
c
d
e
f
g
h
i
k
l
m
n
o
p
q
r
s
t
u
v
y
~
Functions
a
c
d
e
f
g
h
i
l
m
n
o
p
q
r
s
t
u
v
y
~
Variables
a
c
e
f
g
i
k
m
n
o
p
q
r
s
t
v
Files
File List
File Members
All
a
b
c
d
e
f
g
h
i
j
l
m
n
o
p
r
s
t
u
v
w
y
Functions
c
e
m
y
Variables
Typedefs
Enumerations
Enumerator
a
b
c
d
e
f
g
h
i
j
l
m
n
o
p
r
s
t
u
v
w
y
Macros
•
All
Classes
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Macros
Modules
Pages
exo_interval.yap
Go to the documentation of this file.
1
/**
2
* @file exo_interval.yap
3
* @author VITOR SANTOS COSTA <vsc@VITORs-MBP.lan>
4
* @date 2013
5
*
6
* @brief This file implements a very simple interval solver
7
* designed to interact with the exo
8
* data-base.
9
* It assumes simple queries and a contiguous interval,
10
* and does not really expect to do non-trivial
11
* constraint propagation and solving.
12
*
13
*
14
*/
15
:- module(
exo_interval
,
16
[
max/2
,
17
min/2
,
18
any/2,
19
max/1
,
20
min/1
,
21
maximum/1,
22
minimum/1,
23
any/1,
24
(#<)
/
2
,
25
(#>)
/
2
,
26
(#=<)
/
2
,
27
(#>=)
/
2
,
28
(#=)
/
2
,
29
op(
700
, xfx, (#>)),
30
op(
700
, xfx, (#<)),
31
op(
700
, xfx, (#>=)),
32
op(
700
, xfx, (#=<)),
33
op(
700
, xfx, (#=))]).
34
35
36
/**
37
38
@defgroup exo_interval Exo Intervals
39
@ingroup YAPLibrary
40
@{
41
42
This package assumes you use exo-compilation, that is, that you loaded
43
the pedicate using the `exo` option to load_files/2, In this
44
case, YAP includes a package for improved search on intervals of
45
integers.
46
47
The package is activated by `udi` declarations that state what is
48
the argument of interest:
49
50
```
51
:- udi(diagnoses(exo_interval,?,?)).
52
53
:- load_files(db, [consult(exo)]).
54
```
55
It is designed to optimise the following type of queries:
56
57
```
58
?- max(X, diagnoses(X, 9, Y), X).
59
60
?- min(X, diagnoses(X, 9, 36211117), X).
61
62
?- X #< Y, min(X, diagnoses(X, 9, 36211117), X ), diagnoses(Y, 9, _).
63
```
64
The first argument gives the time, the second the patient, and the
65
third the condition code. The first query should find the last time
66
the patient 9 had any code reported, the second looks for the first
67
report of code 36211117, and the last searches for reports after this
68
one. All queries run in constant or log(n) time.
69
70
71
*/
72
73
/** @pred max( _X_, _Vs_)
74
First Argument is the greatest element of a list.
75
76
+ lex_order( _Vs_)
77
All elements must be ordered.
78
79
80
81
The following predicates control search:
82
83
84
*/
85
/** @pred max(+ _Expression_)
86
Maximizes _Expression_ within the current constraint store. This is
87
the same as computing the supremum and equating the expression to that
88
supremum.
89
90
91
*/
92
/** @pred min( _X_, _Vs_)
93
First Argument is the least element of a list.
94
95
96
*/
97
/** @pred min(+ _Expression_)
98
Minimizes _Expression_ within the current constraint store. This is
99
the same as computing the infimum and equation the expression to that
100
infimum.
101
102
103
*/
104
:-
meta_predicate
max
(?,
0
),
min
(?,
0
),
any(?,
0
).
105
106
max
(
X
,
G
)
:-
107
insert_atts(
X
, i(
_
,
_
,max)),
108
call
(
G
).
109
110
min
(
X
,
G
)
:-
111
insert_atts(
X
, i(
_
,
_
,min)),
112
call
(
G
).
113
114
max
(
X
)
:-
115
insert_atts(
X
, i(
_
,
_
,max)).
116
117
maximum(
X
)
:-
118
insert_atts(
X
, i(
_
,
_
,maximum)).
119
120
any(
X
)
:-
121
insert_atts(
X
, i(
_
,
_
,any)).
122
123
min
(
X
)
:-
124
insert_atts(
X
, i(
_
,
_
,min)).
125
126
minimum(
X
)
:-
127
insert_atts(
X
, i(
_
,
_
,minimum)).
128
129
least(
X
)
:-
130
insert_atts(
X
, i(
_
,
_
,least)).
131
132
X
insert_atts
Y
:-
133
(
var
(
X
)
->
insert_atts(
X
, i(
Y
,
_
,
_
))
134
;
135
(
var
(
Y
)
->
insert_atts(
Y
, i(
_
,
X
,
_
) )
;
136
insert_atts
137
)
138
;
139
var
(
Y
)
->
insert_atts(
Y
, i(
_
,
X
,
_
))
140
;
141
X
>
Y
142
).
143
144
X
#>=
Y
:-
145
(
var
(
X
)
->
insert_atts(
X
, i(
Y
-
1
,
_
,
_
))
146
;
147
X
>=
Y
148
).
149
150
X
#<
Y
:-
151
(
var
(
X
)
->
insert_atts(
X
, i(
_
,
Y
,
_
))
152
;
153
X
<
Y
154
).
155
156
X
#=<
Y
:-
157
(
var
(
X
)
->
insert_atts(
X
, i(
Y
+
1
,
_
,
_
))
158
;
159
X
=<
Y
160
).
161
162
X
#=
Y
:-
163
(
var
(
X
)
->
insert_atts(
X
, i(
Y
-
1
,
Y
+
1
,
_
))
;
164
X
=:=
Y
165
).
166
167
168
attribute_goals(
X
)
-->
169
{
get_attr
(
X
, exo_interval,
Op
) },
170
( {
Op
=
max }
->
[
max
(
X
)]
;
171
{
Op
=
}
->
[
min
(
X
)]
;
172
{
Op
=
'>'(
Y
) }
->
[
X
Y
]
;
173
{
Op
=
'<'(
Y
) }
->
[
X
Y
]
;
174
{
Op
=
range(
A
,
B
,
C
) }
->
175
range_min(
A
,
X
),
176
range_max(
B
,
X
),
177
range_op(
C
,
X
)
178
).
179
180
range_min(
Y
,
_X
)
-->
181
{
var
(
Y
) }, !,
182
[].
183
range_min(
Y
,
X
)
-->
184
[
X
#>
Y
].
185
186
range_max(
Y
,
_X
)
-->
187
{
var
(
Y
) }, !,
188
[].
189
range_max(
Y
,
X
)
-->
190
[
X
#<
Y
].
191
192
range_op(
Y
,
_X
)
-->
193
{
var
(
Y
) }, !,
194
[].
195
range_op(
Y
,
X
)
-->
196
{
Op
=..
[
Y
,
X
] },
197
[
Op
].
198
199
insert_atts(
V
,
Att
)
:-
200
(
nonvar
(
V
)
->
201
throw
( error(uninstantion_error(
V
), exo_interval) )
202
;
attvar
(
V
)
->
203
get_attr
(
V
, exo_interval,
Att0
),
204
expand_atts(
Att
,
Att0
,
NAtt
)
205
;
206
NAtt
=
Att
207
),
208
put_attr
(
V
, exo_interval,
NAtt
).
209
210
expand_atts(i(
A1
,
B1
,
C1
), i(
A2
,
B2
,
C2
), i(
A3
,
B3
,
C3
))
:-
211
expand_min(
A1
,
A2
,
A3
),
212
expand_max(
B1
,
B2
,
B3
),
213
expand_op(
C1
,
C2
,
C3
).
214
215
expand_min(
A1
,
A2
,
A3
)
:-
216
(
var
(
A1
)
->
A3
=
A2
;
217
var
(
A2
)
->
A3
=
A1
;
218
ground
(
A1
),
ground
(
A2
)
->
A3
is
max
(
A1
,
A2
)
;
219
A3
=
max
(
A1
,
A2
)
220
).
221
222
expand_max(
A1
,
A2
,
A3
)
:-
223
(
var
(
A1
)
->
A3
=
A2
;
224
var
(
A2
)
->
A3
=
A1
;
225
ground
(
A1
),
ground
(
A2
)
->
A3
is
min
(
A1
,
A2
)
;
226
A3
=
min
(
A1
,
A2
)
227
).
228
229
expand_op(
A1
,
A2
,
A3
)
:-
230
(
var
(
A1
)
->
A3
=
A2
;
231
var
(
A2
)
->
A3
=
A1
;
232
A1
==
A2
->
A3
=
A1
;
233
A1
==
var
->
A3
=
var
;
234
A2
==
var
->
A3
=
var
;
235
A2
==
var,
A1
=
var
->
A3
=
var
;
236
A1
==
var,
A2
=
var
->
A3
=
var
;
237
A1
==
var
->
A3
=
var
;
A2
==
var
->
A3
=
var
;
238
A1
==
var
->
A3
=
var
;
A2
==
var
->
A3
=
var
;
239
A3
=
var
240
).
241
%% @}
242
243
throw/1
throw(+ Ball)
attvar/1
attvar( -Var)
get_attr/3
get_attr( + Var,+ Module,- Value)
put_attr/3
put_attr(+ Var,+ Module,+ Value)
call/1
call( 0:P )
ground/1
ground( T)
nonvar/1
nonvar( T)
var/1
var( T)
max/1
max(+ Expression)
max/2
max( X, Vs)
min/1
min(+ Expression)
min/2
min( X, Vs)
library
exo_interval.yap
Generated by
1.9.3