line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Language::Prolog::Yaswi; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
our $VERSION = '0.23'; |
4
|
|
|
|
|
|
|
|
5
|
3
|
|
|
3
|
|
275581
|
use strict; |
|
3
|
|
|
|
|
30
|
|
|
3
|
|
|
|
|
74
|
|
6
|
3
|
|
|
3
|
|
12
|
use warnings; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
458
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
require Exporter; |
9
|
|
|
|
|
|
|
our @ISA = qw(Exporter); |
10
|
|
|
|
|
|
|
our %EXPORT_TAGS = ( 'query' => [ qw( swi_set_query |
11
|
|
|
|
|
|
|
swi_set_query_module |
12
|
|
|
|
|
|
|
swi_result |
13
|
|
|
|
|
|
|
swi_next |
14
|
|
|
|
|
|
|
swi_var |
15
|
|
|
|
|
|
|
swi_vars |
16
|
|
|
|
|
|
|
swi_query |
17
|
|
|
|
|
|
|
swi_cut |
18
|
|
|
|
|
|
|
swi_find_all |
19
|
|
|
|
|
|
|
swi_find_one |
20
|
|
|
|
|
|
|
swi_call |
21
|
|
|
|
|
|
|
swi_parse |
22
|
|
|
|
|
|
|
swi_eval )], |
23
|
|
|
|
|
|
|
'load' => [ qw( swi_inline |
24
|
|
|
|
|
|
|
swi_inline_module |
25
|
|
|
|
|
|
|
swi_consult |
26
|
|
|
|
|
|
|
swi_use_modules )], |
27
|
|
|
|
|
|
|
'assert' => [ qw( swi_assert |
28
|
|
|
|
|
|
|
swi_asserta |
29
|
|
|
|
|
|
|
swi_assertz |
30
|
|
|
|
|
|
|
swi_facts |
31
|
|
|
|
|
|
|
swi_retractall )], |
32
|
|
|
|
|
|
|
'interactive' => [ qw( swi_toplevel )], |
33
|
|
|
|
|
|
|
'context' => [ qw( *swi_module |
34
|
|
|
|
|
|
|
*swi_temp_dir |
35
|
|
|
|
|
|
|
*swi_converter) ], |
36
|
|
|
|
|
|
|
'run' => [ qw( swi_init |
37
|
|
|
|
|
|
|
swi_cleanup )] ); |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
our @EXPORT_OK = ( @{$EXPORT_TAGS{query}}, |
40
|
|
|
|
|
|
|
@{$EXPORT_TAGS{assert}}, |
41
|
|
|
|
|
|
|
@{$EXPORT_TAGS{interactive}}, |
42
|
|
|
|
|
|
|
@{$EXPORT_TAGS{context}}, |
43
|
|
|
|
|
|
|
@{$EXPORT_TAGS{run}}, |
44
|
|
|
|
|
|
|
@{$EXPORT_TAGS{load}}); |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
our @EXPORT = qw(); |
47
|
|
|
|
|
|
|
|
48
|
3
|
|
|
3
|
|
17
|
use Carp; |
|
3
|
|
|
|
|
4
|
|
|
3
|
|
|
|
|
177
|
|
49
|
|
|
|
|
|
|
our @CARP_NOT=qw( Prolog::Language::Yaswi::Low |
50
|
|
|
|
|
|
|
Prolog::Language::Types ); |
51
|
|
|
|
|
|
|
|
52
|
3
|
|
|
3
|
|
1846
|
use File::Temp; |
|
3
|
|
|
|
|
49904
|
|
|
3
|
|
|
|
|
191
|
|
53
|
3
|
|
|
3
|
|
406
|
use Language::Prolog::Types qw(:util F L C V isF isL isV isN); |
|
3
|
|
|
|
|
6204
|
|
|
3
|
|
|
|
|
368
|
|
54
|
3
|
|
|
3
|
|
1111
|
use Language::Prolog::Yaswi::Low; |
|
3
|
|
|
|
|
9
|
|
|
3
|
|
|
|
|
4098
|
|
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
our $swi_module = undef; |
58
|
|
|
|
|
|
|
our $swi_temp_dir = undef; |
59
|
|
|
|
|
|
|
our $swi_debug = undef; |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
sub swi_init; |
63
|
|
|
|
|
|
|
*swi_init=\&init; |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
sub swi_cleanup(); |
66
|
|
|
|
|
|
|
*swi_cleanup=\&cleanup; |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
sub swi_toplevel(); |
69
|
|
|
|
|
|
|
*swi_toplevel=\&toplevel; |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
*swi_converter=*converter; |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
sub swi_set_query_module { |
74
|
31
|
|
|
31
|
1
|
174
|
@{&openquery(@_)} |
|
31
|
|
|
|
|
33361
|
|
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
sub swi_cut(); |
78
|
|
|
|
|
|
|
*swi_cut=\&cutquery; |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
sub swi_set_query { |
82
|
31
|
|
|
31
|
1
|
91
|
return swi_set_query_module(C(',', @_), |
83
|
|
|
|
|
|
|
$swi_module); |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
sub swi_next() { |
87
|
|
|
|
|
|
|
package main; |
88
|
46
|
|
|
46
|
1
|
1987
|
Language::Prolog::Yaswi::Low::nextsolution(); |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
sub swi_query { |
92
|
0
|
|
|
0
|
1
|
0
|
testquery(); |
93
|
0
|
|
|
|
|
0
|
getquery(); |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
sub swi_var($) { |
97
|
2
|
|
|
2
|
1
|
8
|
testquery(); |
98
|
2
|
|
|
|
|
7
|
getvar($_[0]); |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
sub swi_result() { |
102
|
0
|
|
|
0
|
1
|
0
|
testquery(); |
103
|
0
|
|
|
|
|
0
|
getallvars(); |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
sub swi_vars { |
107
|
28
|
|
|
28
|
1
|
63
|
testquery(); |
108
|
|
|
|
|
|
|
my @res=map { |
109
|
28
|
0
|
|
|
|
45
|
isV($_) ? getvar($_) : |
|
30
|
0
|
|
|
|
65
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
110
|
|
|
|
|
|
|
isL($_) ? L(swi_vars(prolog_list2perl_list($_))) : |
111
|
|
|
|
|
|
|
isF($_) ? F($_->functor => swi_vars($_->fargs)) : |
112
|
|
|
|
|
|
|
($_ eq '*') ? getquery() : |
113
|
|
|
|
|
|
|
isN($_) ? $_ : |
114
|
|
|
|
|
|
|
(ref($_) eq '') ? $_ : |
115
|
|
|
|
|
|
|
croak "invalid mapping '$_'"; |
116
|
|
|
|
|
|
|
} @_; |
117
|
28
|
50
|
|
|
|
79
|
wantarray ? @res : $res[0] |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
sub swi_find_all ($;@) { |
121
|
10
|
|
|
10
|
1
|
3605
|
my @r; |
122
|
10
|
|
|
|
|
22
|
swi_set_query(shift); |
123
|
10
|
|
|
|
|
148
|
while (swi_next) { |
124
|
|
|
|
|
|
|
# warn "new solution found\n"; |
125
|
13
|
|
|
|
|
30
|
push @r, swi_vars(@_); |
126
|
|
|
|
|
|
|
} |
127
|
10
|
50
|
|
|
|
45
|
return wantarray ? @r : $r[0] |
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
sub swi_find_one ($;@) { |
131
|
16
|
|
|
16
|
1
|
5326
|
swi_set_query(shift); |
132
|
16
|
100
|
|
|
|
371
|
if (swi_next) { |
133
|
15
|
|
|
|
|
135
|
my @r=swi_vars(@_); |
134
|
15
|
|
|
|
|
64
|
swi_cut; |
135
|
15
|
100
|
|
|
|
66
|
return wantarray ? @r : $r[0]; |
136
|
|
|
|
|
|
|
} |
137
|
1
|
|
|
|
|
66
|
return (); |
138
|
|
|
|
|
|
|
} |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
sub swi_call { |
141
|
4
|
|
|
4
|
1
|
153
|
swi_set_query(@_); |
142
|
4
|
50
|
|
|
|
77
|
if (swi_next) { |
143
|
4
|
|
|
|
|
25
|
swi_cut; |
144
|
4
|
|
|
|
|
10
|
return 1; |
145
|
|
|
|
|
|
|
} |
146
|
0
|
|
|
|
|
0
|
return undef; |
147
|
|
|
|
|
|
|
} |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
sub swi_assertz { |
150
|
1
|
|
|
1
|
1
|
138
|
my $head=shift; |
151
|
1
|
50
|
|
|
|
4
|
defined $head or croak "swi_assertz called without head"; |
152
|
1
|
|
|
|
|
4
|
swi_call F(assertz => C(':-' => $head, C(',', @_))) |
153
|
|
|
|
|
|
|
} |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
*swi_assert=\&swi_assertz; |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
sub swi_asserta { |
158
|
0
|
|
|
0
|
1
|
0
|
my $head=shift; |
159
|
0
|
0
|
|
|
|
0
|
defined $head or croak "swi_asserta called without head"; |
160
|
0
|
|
|
|
|
0
|
swi_call F(asserta => C(':-' => $head, C(',', @_))) |
161
|
|
|
|
|
|
|
} |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
sub swi_retractall { |
164
|
0
|
|
|
0
|
1
|
0
|
for my $head (@_) { |
165
|
0
|
|
|
|
|
0
|
swi_call F(retractall => $head); |
166
|
|
|
|
|
|
|
} |
167
|
|
|
|
|
|
|
} |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
sub swi_facts { |
170
|
1
|
|
|
1
|
1
|
64
|
return swi_call C(',', (map { F(assertz => $_) } @_)); |
|
3
|
|
|
|
|
32
|
|
171
|
|
|
|
|
|
|
} |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
sub swi_consult { |
174
|
0
|
|
|
0
|
1
|
0
|
return swi_call([@_]); |
175
|
|
|
|
|
|
|
} |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
sub swi_use_modules { |
178
|
0
|
|
|
0
|
1
|
0
|
swi_call F(use_module => $_) for @_ |
179
|
|
|
|
|
|
|
} |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
sub swi_parse { |
182
|
2
|
|
|
2
|
1
|
938
|
my @r; |
183
|
2
|
|
|
|
|
6
|
for my $atom (@_) { |
184
|
2
|
|
|
|
|
6
|
my ($t, $b) = swi_find_one(F(atom_to_term => $atom, V('T'), V('B')), |
185
|
|
|
|
|
|
|
V('T'), V('B')); |
186
|
2
|
50
|
|
|
|
21
|
if (isL $b) { |
187
|
2
|
|
|
|
|
14
|
for my $pair (@{$b}) { |
|
2
|
|
|
|
|
52
|
|
188
|
4
|
|
|
|
|
26
|
my $var = $pair->farg(1); |
189
|
4
|
|
|
|
|
63
|
$var->rename($pair->farg(0)) |
190
|
|
|
|
|
|
|
} |
191
|
|
|
|
|
|
|
} |
192
|
2
|
|
|
|
|
24
|
push @r, $t |
193
|
|
|
|
|
|
|
} |
194
|
2
|
50
|
|
|
|
7
|
return wantarray ? @r : $r[0] |
195
|
|
|
|
|
|
|
} |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
sub swi_eval { |
198
|
0
|
|
|
0
|
1
|
0
|
swi_call(C(',' => swi_parse(@_))) |
199
|
|
|
|
|
|
|
} |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
sub swi_inline { |
202
|
1
|
|
|
1
|
1
|
4
|
_swi_inline(load_files => @_) |
203
|
|
|
|
|
|
|
} |
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
sub swi_inline_module { |
206
|
1
|
|
|
1
|
1
|
522
|
_swi_inline(use_module => @_) |
207
|
|
|
|
|
|
|
} |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
sub _swi_inline { |
210
|
2
|
|
|
2
|
|
4
|
my $action = shift; |
211
|
2
|
50
|
|
|
|
13
|
my $tmp=File::Temp->new(TEMPLATE => 'swi_inline_XXXXXXXX', SUFFIX => '.swi', |
212
|
|
|
|
|
|
|
((defined $swi_temp_dir) ? |
213
|
|
|
|
|
|
|
(DIR => $swi_temp_dir) : ())); |
214
|
2
|
50
|
|
|
|
763
|
defined ($tmp) or croak "unable to create temporal prolog source file"; |
215
|
2
|
|
|
|
|
6
|
my $fn=$tmp->filename; |
216
|
|
|
|
|
|
|
|
217
|
2
|
|
|
|
|
19
|
$tmp->print(@_, "\n"); |
218
|
2
|
|
|
|
|
33
|
$tmp->close; |
219
|
|
|
|
|
|
|
|
220
|
2
|
|
|
|
|
86
|
eval { swi_call F($action => $fn) }; |
|
2
|
|
|
|
|
8
|
|
221
|
2
|
|
|
|
|
103
|
unlink $fn; |
222
|
2
|
50
|
|
|
|
19
|
die $@ if $@; |
223
|
|
|
|
|
|
|
} |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
package Language::Prolog::Yaswi::HASH; |
227
|
|
|
|
|
|
|
our @ISA=qw(Language::Prolog::Types::Opaque::Auto); |
228
|
|
|
|
|
|
|
|
229
|
0
|
|
|
0
|
|
|
sub new { return bless {}; } |
230
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
1; |
233
|
|
|
|
|
|
|
__END__ |