-
Notifications
You must be signed in to change notification settings - Fork 2
/
unify.m
172 lines (85 loc) · 3.86 KB
/
unify.m
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
168
169
170
(* Copyright E.M.Clarke and Xudong Zhao, Jan 22, 1991 *)
(* UNIFICATION AND SUBSTITUTION FOR VARIABLES *)
(* H1 & ... & Hn --> C1 | ... | Cm is true if some Cj matches some Hi or
some given fact. *)
(* Check if one of the disjuncts of the second argument unifies with a
conjunct of the first. *)
disjunct[h_, or[a_, b__]] := orelse[disjunct[h, a], disjunct[h, or[b]]];
(* check if "c" matches a conjunct of "h" or some fact similar to "c". *)
disjunct[h_, c_] :=
orelse[(MatchingState = hypothesis; conjunct[h, c]),
(MatchingState = Facts; conjunct[Facts[Head[c]], c ])];
(* Check if the second argument matches a conjunct of the first. *)
conjunct[and[a_, b__], c_] :=
orelse[conjunct[a, c], conjunct[and[b], c]];
conjunct[h_, c_] := Block[{u},
(* If the conclusion matches a fact, set the current lemma. *)
If[MatchingState === Facts, CurrentLemma = h];
(* If unification fails, return False. *)
If[FalseQ[u = unify[h, c]], Return[False]];
(* Otherwise, print out the lemma used and the unifier. *)
SucceedWith[
If[MatchingState === hypothesis,
PrintResult["matching hypothesis with", u],
(print["matching lemma"];
PrintLemma[CurrentLemma];
PrintResult["with", u])];
(* Now try to prove other branches of the theorem. *)
TryOtherBranches[u]]
];
(* Check if two formulas unify. If so, return the substitution.
If the formulas do not unify, return False. *)
unify[a_, a_] := substitution[];
(* Unification of a variable and a term *)
unify[a_, Var[n_]] :=
substitution[{Var[n] -> a}] /; FreeQ[a, n];
unify[Var[n_], a_] :=
substitution[{Var[n] -> a}] /; FreeQ[a, n];
unify[Var[n_] + a_, 0] :=
substitution[{Var[n] -> -a}] /; FreeQ[a, n];
unify[-Var[n_] + a_, 0] :=
substitution[{Var[n] -> a}] /; FreeQ[a, n];
(* Unification of atomic formulas *)
unify[a_ == b_, c_ == d_] := unify[a - b, c - d];
unify[a_ <= b_, c_ <= d_] := unify[a - b, c - d];
unify[a_ < b_, c_ < d_] := unify[a - b, c - d];
(* Unification of arithmetical expressions. *)
unify[a_ + b_., a_ + c_.] := unify[b, c];
unify[a_ b_., a_ c_.] := unify[b, c];
unify[a_ + b_, c_ + d_] :=
Block[{u}, substitution[u, unify[apply[u, b], apply[u, d]]] /;
!FalseQ[u = unify[a, c]]];
unify[a_ b_, c_ d_] :=
Block[{u}, substitution[u, unify[apply[u, b], apply[u, d]]] /;
!FalseQ[u = unify[a, c]]];
unify[1/Var[n_], a_] := substitution[{Var[n] -> 1/a}] /; FreeQ[a, n];
unify[a_, 1/Var[n_]] := substitution[{Var[n] -> 1/a}] /; FreeQ[a, n];
(* Unification of compound expressions.*)
unify[f_[a___], f_[b___]] := unifylist[{a}, {b}];
(* Default case--arguments do not unify. *)
unify[_, _] := False;
(* Check if two lists of formulas unify. *)
unifylist[_, False] := False;
unifylist[False, _] := False;
unifylist[{}, {}] := substitution[];
unifylist[{}, {__}] := False;
unifylist[{__}, {}] := False;
unifylist[{a_}, {b_}] := unify[a, b];
unifylist[{a_, x___}, {b_, y___}] :=
Block[{u},
u = unify[a, b];
substitution[u,
unifylist[apply[u, {x}], apply[u, {y}]]]
];
(* Rules for simplifying substitutions. *)
substitution[] = True;
substitution[___, False, ___] := False;
substitution[a___, True, b___] := substitution[a, b];
substitution[a___, substitution[b__], c___] := substitution[a, b, c];
substitution[{a__}, {b__}, c___] := substitution[Union[{a}, {b}], c];
substitution[{a___, b_, c___, b_, d___}, e___] :=
substitution[{a, b, c, d}, e];
(* Apply a substitution to a formula. *)
apply[False, a_] := False;
apply[substitution[], a_] := a;
apply[substitution[s_], term_] := term //. s;