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