/
README
168 lines (139 loc) · 4.29 KB
/
README
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
NAME
AI::Logic - Continuation based logic programming.
VERSION
Version 0.01
ALPHA CODE
Warning: this is not yet documented or tested well. I'm just making this
publicly available because some folks wanted to see it. It probably has
lots of bugs.
SYNOPSIS
{
package My::Database;
use AI::Logic::Database variables => [ 'Person' ],
predicates => [
qw{
male/1
female/1
married/2
wife/1
insane/1
strange_husband/1
}
];
male { 'frank' };
male { 'barney' };
male { 'timothy' };
male { 'ovid' };
male { 'Sam' };
female { 'Sarah' };
female { 'Leila' };
female { 'Samantha' };
married { 'frank', 'Sarah' };
married { 'Sam', 'Samantha' };
married { 'Bill', 'John' };
Rule {
wife { Person } =>
married { Any, Person },
female { Person };
};
}
use AI::Logic 'My::Database';
# Are any of these names known males?
my @names;
foreach my $name (qw/frank judy barney/) {
male( $name, sub { push @names => $name; } );
}
# @names will contain 'frank' and 'barney'
my $male = Var;
@names = ();
male( $male, sub { push @names => $male->value } );
# @names will contain all known males
# who is frank married to?
my $husband = Var 'frank';
my $wife = Var;
married(
$husband, $wife,
sub {
print 'frank is married to '.$wife->value."\n";
}
);
# print all married couples
$husband->unbind;
$wife->unbind;
my @spouses;
married(
$husband, $wife,
sub {
print $husband->value .' is married to '.$wife->value."\n";
}
);
my $var = Var 'Sarah';
my $is_wife;
wife( $var, sub { $is_wife = 1 });
if ( $is_wife ) {
print "Yes\n";
}
else {
print "No\n";
}
# You can create rules on the fly, but it's not fun and they'll conflict with
# other rules of the same name.
sub strange_husband {
my ($person, $continuation) = @_;
insane(
$person,
sub {
married(
$person, Any,
sub {
male( $person, $continuation );
}
);
}
);
}
$husband = Var;
strange_husband($husband, sub { print $husband->value });
# equivalent to the following declaration in a database:
Rule {
strange_husband { Person } =>
insane { Person },
married { Person, Any },
male { Person },
}
EXPORT
The following functions are exported automatically:
*"Var"
Used to declare logic variables. These variables may be bound or
unbound.
my $person = Var 'ovid'; # bound
my $person = Var; # unbound
* "Any"
Used to declare a logic variable whose value you do not care about.
All predicates declared in your database will be exported as functions
to your code.
AUTHOR
Curtis "Ovid" Poe, "<ovid at cpan.org>"
BUGS
Please report any bugs or feature requests to "bug-ai-logic at
rt.cpan.org", or through the web interface at
<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=AI-Logic>. I will be
notified, and then you'll automatically be notified of progress on your
bug as I make changes.
SUPPORT
You can find documentation for this module with the perldoc command.
perldoc AI::Logic
You can also look for information at:
* RT: CPAN's request tracker
<http://rt.cpan.org/NoAuth/Bugs.html?Dist=AI-Logic>
* AnnoCPAN: Annotated CPAN documentation
<http://annocpan.org/dist/AI-Logic>
* CPAN Ratings
<http://cpanratings.perl.org/d/AI-Logic>
* Search CPAN
<http://search.cpan.org/dist/AI-Logic/>
ACKNOWLEDGEMENTS
COPYRIGHT & LICENSE
Copyright 2009 Curtis "Ovid" Poe, all rights reserved.
This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.