2
;;; Definition of a particular rule-based unification algorithm.
3
;;; This is an executable instance of the general pattern verified in
4
;;; unification-pattern.lisp
5
;;; Created 17-10-99. Last revision: 10-12-2000
6
;;; =================================================================
8
#| To certify this book:
12
(certify-book "unification")
21
(include-book "subsumption-subst")
22
(local (include-book "unification-pattern"))
23
(set-well-founded-relation e0-ord-<)
26
;;; *************************************************
27
;;; A CONCRETE AND EXECUTABLE UNIFICATION ALGORITHM
28
;;; *************************************************
30
;;; Here we show how we can obtain a correct and executable unification
31
;;; algorithm from the "pattern" verified in unification-definition.lisp:
32
;;; - We define a particular selection function.
33
;;; - We introduce multi-values to deal with the pair of systems
34
;;; S-sol and with the returned values, to improve efficiency.
35
;;; - Some other improvements concerning efficency are done.
36
;;; - Guards are verified, allowing execution in Common Lisp.
38
;;; ============================================================================
39
;;; 1. The unification algorithm
40
;;; ============================================================================
42
;;; ----------------------------------------------------------------------------
43
;;; 1.1 A particular version of transform-mm-sel
44
;;; ----------------------------------------------------------------------------
46
;;; ����������������������������������������������������������������������������
47
;;; 1.1.1 A particular "selection" function. If we detect an inmediate fail,
49
;;; ����������������������������������������������������������������������������
52
(declare (xargs :guard (and (consp S) (system-p S))))
58
(cond ((or (variable-p t1) (variable-p t2))
60
((eql (car t1) (car t2))
64
;;; Main property, needed to instantiate from unification-definition.lisp:
66
(defthm sel-unif-select-a-pair
68
(member (sel-unif S) S))))
72
;;; ����������������������������������������������������������������������������
73
;;; 1.1.2 Some lemmas needed for guard verification:
74
;;; ����������������������������������������������������������������������������
80
(defthm sel-unif-consp
81
(implies (and (alistp S)
85
:rule-classes :type-prescription))
87
(defthm termp-p-sel-unif-system-p
88
(implies (and (consp S)
91
(term-p (car (sel-unif S)))
92
(term-p (cdr (sel-unif S))))))
95
(defthm term-p-true-listp-arguments
96
(implies (and (term-p term) (not (variable-p term)))
97
(true-listp (cdr term)))))
100
(defthm system-p-eqlable-function-symbols
101
(implies (and (system-p S)
103
(eqlablep (cadr equ)))))
105
(defthm system-p-eqlablep-car
106
(implies (and (system-p S)
108
(variable-p (car (sel-unif S))))
109
(eqlablep (car (sel-unif S)))))
111
;;; ����������������������������������������������������������������������������
112
;;; 1.1.3 The function transform-mm
113
;;; ����������������������������������������������������������������������������
116
(defun transform-mm (S sol)
117
(declare (xargs :guard (and (system-p S)
120
(let* ((ecu (sel-unif S))
121
(t1 (car ecu)) (t2 (cdr ecu))
122
(R (eliminate ecu S)))
123
(cond ((equal t1 t2) (mv R sol t)) ;;; *DELETE*
125
(if (member t1 (variables t t2))
126
(mv nil nil nil) ;;; *CHECK*
128
(substitute-syst t1 t2 R)
130
(substitute-range t1 t2 sol))
133
(mv (cons (cons t2 t1) R) sol t)) ;;; *ORIENT*
134
((not (eql (car t1) (car t2)))
135
(mv nil nil nil)) ;;; *CONFLICT*
136
(t (mv-let (pairs bool)
137
(pair-args (cdr t1) (cdr t2))
139
(mv (append pairs R) sol t) ;;; *DESCOMPOSE*
140
(mv nil nil nil)))))))) ;;; *NOT-PAIR*
144
;;; ----------------------------------------------------------------------------
145
;;; 1.2 Some lemmas needed for functional instantiation (first part)
146
;;; ----------------------------------------------------------------------------
148
;;; Here we define some lemmas to show that the efficiency improvements
149
;;; done in transform-mm does not affect the logic:
152
(defthm substitute-var-apply-subst
153
(equal (apply-subst flg (list equ) term)
154
(substitute-var (car equ) (cdr equ) flg term))))
158
(defthm substitute-syst-apply-syst
159
(equal (apply-syst (list equ) S)
160
(substitute-syst (car equ) (cdr equ) S))))
163
(defthm substitute-range-apply-range
164
(equal (apply-range (list equ) S)
165
(substitute-range (car equ) (cdr equ) S))))
168
;;; transform-mm-sel of unification-definition.lisp cannot be
169
;;; instantiated by transform-mm, since they different signatures, due
170
;;; to the use of multi values in transform-mm. Instead, we will
171
;;; instantiate transform-mm-sel for the following function:
174
(defun transform-mm-bridge (S-sol)
175
(mv-let (S1 sol1 bool1)
176
(transform-mm (car S-sol) (cdr S-sol))
177
(if bool1 (cons S1 sol1) nil))))
180
;;; ----------------------------------------------------------------------------
181
;;; 1.3 Termination properties of transform-mm
182
;;; ----------------------------------------------------------------------------
184
;;; The theorem to justify the definition. This lemma is easily obtained
185
;;; by functional instantiation:
190
;;; A technical lemma, to make the proof shorter:
193
(equal (unification-measure '(nil))
194
(unification-measure nil))))
196
(defthm unification-measure-decreases-instance
197
(let ((transform-mm (transform-mm S sol)))
200
(unification-measure (cons (first transform-mm)
201
(second transform-mm)))
202
(unification-measure (cons S sol)))))
203
:hints (("Goal" :use (:functional-instance
204
(:instance unification-measure-decreases
205
(S-sol (cons S sol)))
206
(transform-mm-sel transform-mm-bridge)
208
("Subgoal 3" :in-theory (disable unification-measure
209
(unification-measure)))))))
213
(local (in-theory (disable unification-measure)))
217
;;; ----------------------------------------------------------------------------
218
;;; 1.4 Guard verification
219
;;; ----------------------------------------------------------------------------
221
;;; Some lemmas needed for guard verification of mgu-mv
224
(defthm system-p-substitute-var
225
(implies (and (term-p t1)
226
(term-p-aux flg term))
227
(term-p-aux flg (substitute-var x t1 flg term)))))
230
(defthm system-p-substitute-syst
231
(implies (and (system-p S)
233
(system-p (substitute-syst x term S)))))
236
(defthm system-p-substitute-range
237
(implies (and (system-p S)
239
(system-p (substitute-range x term S)))))
242
;;; ----------------------------------------------------------------------------
243
;;; 1.5 The unification algorithm
244
;;; ----------------------------------------------------------------------------
246
;;; Appling transform-mm until a normal form is found:
248
(defun solve-system (S sol bool)
251
:guard (and (system-p S)
253
:measure (unification-measure (cons S sol))
255
(("Goal" :in-theory (disable transform-mm)))))
256
(if (or (not bool) (not (consp S)))
258
(mv-let (S1 sol1 bool1)
260
(solve-system S1 sol1 bool1))))
264
;;; Most general solutions of systems of equations
267
(declare (xargs :guard (system-p S)))
268
(mv-let (S1 sol1 bool1)
269
(solve-system S nil t)
270
(declare (ignore S1))
273
;;; The unification algorithm
275
(defun mgu-mv (t1 t2)
276
(declare (xargs :guard (and (term-p t1) (term-p t2))))
277
(mgs-mv (list (cons t1 t2))))
281
;;; We also define as functions the property of being unifiable and the
282
;;; umg substitution:
283
(defun unifiable (t1 t2)
284
(declare (xargs :guard (and (term-p t1) (term-p t2))))
285
(mv-let (mgu unifiable)
287
(declare (ignore mgu))
291
(declare (xargs :guard (and (term-p t1) (term-p t2))))
292
(mv-let (mgu unifiable)
294
(declare (ignore unifiable))
298
;;; REMARK: mgu-mv will be used to compute unifiability and most general
299
;;; unifier at the same time. The functions unifiable and mgu are
300
;;; defined to be used in the statements of theorems.
302
;;; ============================================================================
303
;;; 2. Fundamental properties of mgu-mv, unifiable and mgu
304
;;; ============================================================================
306
;;; ----------------------------------------------------------------------------
307
;;; 2.1 Some lemmas needed for the functional instantiation (second part)
308
;;; ----------------------------------------------------------------------------
311
(defthm booleanp-third-solve-system
312
(implies (booleanp bool)
313
(booleanp (third (solve-system S sol bool))))
314
:rule-classes :type-prescription))
317
(defthm nil-third-implies-nil-second-solve-system
318
(implies (not (third (solve-system S sol t)))
319
(not (second (solve-system S sol t))))))
322
;;; solve-system-sel of unification-pattern.lisp cannot be
323
;;; instantiated by solve-system, since they different signatures, due
324
;;; to the use of multi values in transform-mm. Instead, we will
325
;;; instantiate solve-system-sel for the following function:
328
(defun solve-system-bridge (S-sol)
329
(if (not (consp S-sol))
331
(mv-let (S1 sol1 bool1)
332
(solve-system (car S-sol) (cdr S-sol) t)
333
(if bool1 (cons S1 sol1) nil)))))
335
;;; The same happens with mgs-sel and mgu-sel
338
(defun mgs-mv-bridge (S)
339
(let ((solve-system-bridge (solve-system-bridge (cons S nil))))
340
(if solve-system-bridge (list (cdr solve-system-bridge)) nil))))
343
(defun unifiable-bridge (t1 t2)
344
(mgs-mv-bridge (list (cons t1 t2)))))
347
(defun mgu-bridge (t1 t2)
348
(first (unifiable-bridge t1 t2))))
351
;;; ----------------------------------------------------------------------------
352
;;; 2.2 The properties
353
;;; ----------------------------------------------------------------------------
354
;;; Most of these properties are obtained by functional instantiation.
359
(defthm mgu-completeness
360
(implies (equal (instance t1 sigma)
368
((:functional-instance
369
unifiable-sel-completeness
371
(transform-mm-sel transform-mm-bridge)
372
(solve-system-sel solve-system-bridge)
373
(mgs-sel mgs-mv-bridge)
374
(unifiable-sel unifiable-bridge))))))
376
;;; The hint is not necessary, but makes the proof shorter.
382
(defthm mgu-soundness
383
(implies (unifiable t1 t2)
384
(equal (instance t1 (mgu t1 t2))
385
(instance t2 (mgu t1 t2))))
389
((:functional-instance
390
unifiable-sel-soundness
392
(transform-mm-sel transform-mm-bridge)
393
(solve-system-sel solve-system-bridge)
394
(mgs-sel mgs-mv-bridge)
395
(unifiable-sel unifiable-bridge)
396
(mgu-sel mgu-bridge))))))
402
(defthm mgu-idempotent
403
(idempotent (mgu t1 t2))
407
((:functional-instance
410
(transform-mm-sel transform-mm-bridge)
411
(solve-system-sel solve-system-bridge)
412
(mgs-sel mgs-mv-bridge)
413
(unifiable-sel unifiable-bridge)
414
(mgu-sel mgu-bridge))))))
418
;;; Generality of the unifier
419
;;; �������������������������
421
(defthm mgu-most-general-unifier
422
(implies (equal (instance t1 sigma)
424
(subs-subst (mgu t1 t2) sigma))
428
((:functional-instance
429
mgu-sel-most-general-unifier
431
(transform-mm-sel transform-mm-bridge)
432
(solve-system-sel solve-system-bridge)
433
(mgs-sel mgs-mv-bridge)
434
(unifiable-sel unifiable-bridge)
435
(mgu-sel mgu-bridge))))))
439
;;; Substitution-s-p (closure property of mgu)
440
;;; ����������������������������������������������
443
(defthm mgu-substitution-s-p
444
(implies (and (term-s-p t1) (term-s-p t2))
445
(substitution-s-p (mgu t1 t2)))
449
((:functional-instance
450
mgu-sel-substitution-s-p
452
(transform-mm-sel transform-mm-bridge)
453
(solve-system-sel solve-system-bridge)
454
(mgs-sel mgs-mv-bridge)
455
(unifiable-sel unifiable-bridge)
456
(mgu-sel mgu-bridge))))))
460
;;; Substitution-p (needed for guard verification)
461
;;; ����������������������������������������������
464
(defthm mgu-substitution-p
465
(implies (and (term-p t1) (term-p t2))
466
(substitution-p (mgu t1 t2)))
467
:hints (("Goal" :use (:functional-instance
469
(signat (lambda (x n) (eqlablep x)))
470
(term-s-p-aux term-p-aux)
471
(substitution-s-p substitution-p)))))
475
;;; We disable mgu-mv, unifiable and mgu and their executable
476
;;; counter-parts, to be sure that ONLY the above two properties are
477
;;; used in the sequel. But before doind this, we state the relations
478
;;; between mgu-mv and unifiable and mgu.
480
(defthm mgu-mv-unifiable
481
(equal (second (mgu-mv t1 t2)) (unifiable t1 t2)))
484
(equal (first (mgu-mv t1 t2)) (mgu t1 t2)))
487
(in-theory (disable mgu-mv (mgu-mv) mgu (mgu) unifiable (unifiable)))
490
;;; ============================================================================
492
;;; ============================================================================
494
;;; ACL2 !>(mgu '(h u) '(h (b)))
498
;;; ACL2 !>(mgu '(f (g x y) (g y x)) '(f u u))
499
;;; ((X . Y) (U G Y Y))
500
;;; ACL2 !>(unifiable '(f (g x y) (g y x)) '(f x x))
502
;;; ACL2 !>(mgu '(f (h z) (h (h z))) '(f u (h (h (g v v)))))
503
;;; ((Z G V V) (U H (G V V)))
504
;;; ACL2 !>(mgu '(f x (g (a) y)) '(f x (g y x)))
506
;;; ACL2 !>(mgu '(f x (g a y)) '(f x (g y x)))
507
;;; ((A . X) (Y . X))
508
;;; ACL2 !>(mgu '(f x (g (a) y)) '(f x (g y x)))
510
;;; ACL2 !>(mgu '(f x (g a y)) '(f x (g y x)))
511
;;; ((A . X) (Y . X))