-
Notifications
You must be signed in to change notification settings - Fork 1
/
prolog.cpp
125 lines (105 loc) · 5.13 KB
/
prolog.cpp
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
#include <iostream>
#include "tcalc.h"
using namespace std;
// symbolic & variable type declaration
namespace tcalc
{
DECLARE_SYM( child );
DECLARE_SYM( boy );
DECLARE_SYM( girl );
DECLARE_SYM( bill );
DECLARE_SYM( frank );
DECLARE_SYM( alice );
DECLARE_SYM( alex );
DECLARE_VAR( X );
DECLARE_VAR( Y );
DECLARE_VAR( Z );
DECLARE_VAR( G );
DECLARE_SYM( trude );
DECLARE_SYM( sally );
DECLARE_SYM( erica );
DECLARE_SYM( tom );
DECLARE_SYM( mike );
DECLARE_SYM( father_child );
DECLARE_SYM( mother_child );
DECLARE_SYM( parent_child );
DECLARE_SYM( sibling );
}
void TestProlog()
{
using namespace tcalc;
using namespace tcalc::container::list;
using namespace tcalc::prolog;
typedef Term< Value_boy, List< Value_bill, NullItem > > TermA;
typedef Term< Value_boy, List< Value_frank, NullItem > > TermB;
typedef Term< Value_boy, List< Value_X, NullItem > > TermX;
typedef Term< Value_boy, List< Value_G, NullItem > > TermG;
typedef Term< Value_child, List< Value_X, NullItem > > TermChildX; // child(X).
typedef Term< Value_boy, List< Value_X, NullItem > > TermBoyX; // boy(X).
typedef Term< Value_girl, List< Value_X, NullItem > > TermGirlX; // girl(X).
typedef Term< Value_boy, List< Value_alex, NullItem > > TermBoyAlex; // boy(alex).
typedef Term< Value_girl, List< Value_alice, NullItem > > TermGirlAlice; // girl(alice).
typedef Term< Value_child, List< Value_G, NullItem > > TermChildG; // child(G).
typedef Rule< TermBoyAlex, NullItem > RuleBoyAlex; // boy(alex) :- True.
typedef Rule< TermGirlAlice, NullItem > RuleGirlAlice; // girl(alice) :- True.
typedef Rule< TermChildX, List< TermBoyX, NullItem > > RuleChildBoy; // child(X) :- boy(x).
typedef Rule< TermChildX, List< TermGirlX, NullItem > > RuleChildGirl; // child(X) :- girl(x).
typedef List< RuleChildBoy, List< RuleChildGirl,
List< RuleBoyAlex, List< RuleGirlAlice, NullItem > > > > AllRules;
typedef SearchGoal< TermChildG, AllRules > Result;
//typedef SearchGoal< TermBoyX, AllRules > Result;
cout << Result::ret << endl;
PrintMap< Result::ResultEnv >::Print();
////////
// mother_child(trude, sally)
typedef Term< Value_mother_child, List< Value_trude, List< Value_sally, NullItem > > > TermMotherChildTS;
// father_child(tom, sally)
typedef Term< Value_father_child, List< Value_tom, List< Value_sally, NullItem > > > TermFatherChildTS;
// father_child(tom, erica)
typedef Term< Value_father_child, List< Value_tom, List< Value_erica, NullItem > > > TermFatherChildTE;
// father_child(mike, tom)
typedef Term< Value_father_child, List< Value_mike, List< Value_tom, NullItem > > > TermFatherChildMT;
// parent_child(Z, X)
typedef Term< Value_parent_child, List< Value_Z, List< Value_X, NullItem > > > TermParentChildZX;
// parent_child(Z, Y)
typedef Term< Value_parent_child, List< Value_Z, List< Value_Y, NullItem > > > TermParentChildZY;
// parent_child(X, Y)
typedef Term< Value_parent_child, List< Value_X, List< Value_Y, NullItem > > > TermParentChildXY;
// father_child(X, Y)
typedef Term< Value_father_child, List< Value_X, List< Value_Y, NullItem > > > TermFatherChildXY;
// mother_child(X, Y)
typedef Term< Value_mother_child, List< Value_X, List< Value_Y, NullItem > > > TermMotherChildXY;
// sibling(X, Y)
typedef Term< Value_sibling, List< Value_X, List< Value_Y, NullItem > > > TermSiblingXY;
// mother_child(trude, sally).
typedef Rule< TermMotherChildTS, NullItem > RuleMotherChildTS;
// father_child(tom, sally).
typedef Rule< TermFatherChildTS, NullItem > RuleFatherChildTS;
// father_child(tom, erica).
typedef Rule< TermFatherChildTE, NullItem > RuleFatherChildTE;
// father_child(mike, tom).
typedef Rule< TermFatherChildMT, NullItem > RuleFatherChildMT;
// sibling(X, Y) :- parent_child(Z, X), parent_child(Z, Y).
typedef Rule< TermSiblingXY, List< TermParentChildZX, List< TermParentChildZY, NullItem > > > RuleSibling;
// parent_child(X, Y) :- father_child(X, Y).
typedef Rule< TermParentChildXY, List< TermFatherChildXY, NullItem > > RuleParentChild1;
// parent_child(X, Y) :- mother_child(X, Y).
typedef Rule< TermParentChildXY, List< TermMotherChildXY, NullItem > > RuleParentChild2;
// sibling(sally, erica).
typedef Term< Value_sibling, List< Value_sally, List< Value_erica, NullItem > > > TermQuestion;
typedef List< RuleMotherChildTS,
List< RuleFatherChildTS,
List< RuleFatherChildTE,
List< RuleFatherChildMT,
List< RuleSibling,
List< RuleParentChild1,
List< RuleParentChild2, NullItem > > > > > > > AllRules2;
typedef SearchGoal< TermQuestion, AllRules2 > Result2;
cout << Result2::ret << endl;
PrintMap< Result2::ResultEnv >::Print();
}
int main()
{
TestProlog();
return 0;
}