-
Notifications
You must be signed in to change notification settings - Fork 0
/
unifier.c
executable file
·70 lines (58 loc) · 1.49 KB
/
unifier.c
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
#include "functions.h"
//find, if possible, the most general unifier
bool unifier(tree t1, tree t2){
bool result, var1, var2;
tree aux;
int i;
//see if there is already a tree associated with the variable
i = findVar(t1 -> label);
if(i != -1 && variables[i].t != NULL)
t1 = variables[i].t;
i = findVar(t2 -> label);
if(i != -1 && variables[i].t != NULL)
t2 = variables[i].t;
//set boolean variables
result = true;
var1 = isVariable(t1 -> label);
var2 = isVariable(t2 -> label);
//comparing step
if(strcmp(t1 -> label, t2 -> label) == 0)
goto advance;
//substitution step
else{
//neither of the two labels are variables
if(!var1 && !var2)
return false;
//if the node on t1 is a variable
else if(var1 && !var2){
//founded variable with the same name on t2
if(findLabel(t2, t1 -> label) != NULL)
return false;
//associate t2 with the variable
else{
treeToVar(t2, t1 -> label);
t1 = t2;
}
}
//if the node on t2 is a variable
else{
//founded variable with the same name on t1
if(findLabel(t1, t2 -> label) != NULL)
return false;
//associate t1 with the variable
else{
treeToVar(t1, t2 -> label);
t2 = t1;
}
}
}
advance:
if(t1 -> n_childs == 0 && t2 -> n_childs == 0)
return true;
else{
//call for the next childs on both trees
for(i = 0; result && (i < t1 -> n_childs) && (i < t1 -> n_childs); i++)
result = result && unifier(t1 -> childs[i], t2 -> childs[i]);
return result;
}
}