File: matching.lisp

package info (click to toggle)
acl2 8.3dfsg-2
  • links: PTS
  • area: main
  • in suites: bullseye
  • size: 309,408 kB
  • sloc: lisp: 3,311,842; javascript: 22,569; cpp: 9,029; ansic: 7,872; perl: 6,501; xml: 3,838; java: 3,738; makefile: 3,383; ruby: 2,633; sh: 2,489; ml: 763; python: 741; yacc: 721; awk: 260; csh: 186; php: 171; lex: 154; tcl: 49; asm: 23; haskell: 17
file content (669 lines) | stat: -rw-r--r-- 20,284 bytes parent folder | download | duplicates (2)
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
;;; matching.lisp
;;; Definition and correctness of a RULE BASED matching
;;; algorithm between systems of equations.
;;; This definition is a PATTERN for concrete matching
;;; algorithms. See subsumption.lisp for a concrete
;;; implementation.
;;; This is an alternative to a more classical formulation in
;;; matching.lisp
;;; Created: 9-10-99 Last revison: 05-01-2001
;;; =============================================================================

#| To certify this book:

(in-package "ACL2")

(certify-book "matching")

|#



(in-package "ACL2")
(include-book "terms")

;;; ********************************************************************
;;; DEFINITION AND VERIFICATION OF A RULE-BASED SUBSUMPTION ALGORITHM WE
;;; USE THIS BOOK TO PROVIDE A "PATTERN" FOR DEFINING A CONCRETE AND
;;; EXECUTABLE SUBSUMPTION ALGORITHM.
;;; ********************************************************************



;;; ============================================================================
;;; 1. The transformation rules
;;; ============================================================================


;;; ----------------------------------------------------------------------------
;;; 1.1 Definition.
;;; ----------------------------------------------------------------------------


;;; First, we introduce some kind of non-determinism, by partially
;;; defining a selection function a-pair, assuming only the property that
;;; a-pair selects an element of every non-empty list:

(encapsulate

 ((a-pair (lst) t))

 (local (defun a-pair (lst) (car lst)))

 (defthm a-pair-selected
   (implies (consp l) (member (a-pair l) l))))


;;; The rules of transformation. This function is intended to act on a
;;; pair of systems, S-match. The first one, S, has the equations to be
;;; solved, and the second one has the bindings done for the moment. An
;;; equation is selected from S, and according to this equation, a rule
;;; of transformation is applied.

(defun transform-subs-sel (S-match)
  (let* ((S (car S-match)) (match (cdr S-match))
	 (ecu (a-pair S))
	 (t1 (car ecu)) (t2 (cdr ecu))
	 (R (eliminate ecu S)))
    (cond
     ((variable-p t1)
      (let ((bound (assoc t1 match)))
	(if bound
	    (if (equal (cdr bound) t2)
		(cons R match)                        ;;; *** DELETE
	      nil)                                    ;;; *** FAIL-BIND
	  (cons R (cons (cons t1 t2) match)))))       ;;; *** BIND
     ((variable-p t2) nil)                            ;;; *** FAIL-VAR
     ((equal (car t1) (car t2))
      (mv-let (pair-args bool)
	      (pair-args (cdr t1) (cdr t2))
	      (if bool
		  (cons (append pair-args R) match)   ;;; *** DECOMPOSE
		nil)))                                ;;; *** CONFLICT-1
     (t nil))))                                       ;;; *** CONFLICT-2



;;; ----------------------------------------------------------------------------
;;; 1.2 Main properties of transform-subs-sel
;;; ----------------------------------------------------------------------------


;;; ············································································
;;; 1.2.1 How we deal with selection.
;;; ············································································


;;; Firs we prove that equal-set is a congruence w.r.t. the second
;;; argument of matcher:
(local
 (encapsulate
  ()

  (local
   (defthm member-matcher
     (implies (and (matcher sigma S)
		   (member equ S))
	      (equal (instance (car equ) sigma) (cdr equ)))))

  (local
   (defthm subsetp-matcher
     (implies (and (matcher sigma S)
		   (subsetp S1 S))
	      (matcher sigma S1))))


  (defcong equal-set iff (matcher x y) 2)))

;;; Then we define a rewrite rule w.r.t. the equivalence equal-set. This
;;; rule allows the prover to put the selected equation in front of the
;;; system, when we are talking of matchers:

(local
 (encapsulate
  ()
  (local
   (defthm equal-set-selection-and-eliminate-lemma
     (implies (member x S)
	      (equal-set (cons x (eliminate x S)) S))))

  (defthm equal-set-selection-and-eliminate
    (implies (consp (car S-match))
	     (equal-set (car S-match)
			(cons (a-pair (car S-match))
			      (eliminate (a-pair (car S-match))
					 (car S-match))))))))

;;; REMARK: Note why we use (car S-match) (instead of S) in the above rule:
;;; we avoid non-termination.



;;; ············································································
;;; 1.2.2 transform-subs-sel preserves the set of matchers.
;;; ············································································


(local
 (defthm matcher-append
   (equal (matcher sigma (append S1 S2))
	  (and (matcher sigma S1) (matcher sigma S2)))))


(local
 (encapsulate
  ()

  (local
   (defun induction-pair-args (l1 l2)
     (if (or (endp l1) (endp l2))
	 t
       (induction-pair-args (cdr l1) (cdr l2)))))

  (local
   (defthm matcher-pair-args
     (implies (second (pair-args l1 l2))
	      (equal (matcher sigma (car (pair-args l1 l2)))
		     (equal (apply-subst nil sigma l1) l2)))
     :hints (("Goal" :induct (induction-pair-args l1 l2)))))



  (local
   (defthm matcher-sigma-value
     (implies (and
	       (matcher sigma S)
	       (variable-p x)
	       (assoc x S))
	      (equal (val x sigma) (cdr (assoc x S))))))


  (local
   (defthm pair-args-apply-subst
     (second (pair-args term (apply-subst nil sigma term)))))

  (local
   (defthm matcher-topmost-function-symbol
     (implies (and (not (variable-p t1))
		   (not (equal (car t1) (car t2))))
	      (not (equal (instance t1 sigma) t2)))))


  (local
   (defthm matcher-variable-non-variable
     (implies (and (not (variable-p t1))
		   (variable-p t2))
	      (not (equal (instance t1 sigma) t2)))))


  (local
   (defthm matcher-not-pair-args
     (implies (and (not (variable-p t1))
		   (not (second (pair-args t1 t2))))
	      (not (equal (instance t1 sigma) t2)))))

  (local
   (defthm matcher-assoc
     (implies (and
	       (variable-p x)
	       (assoc x S)
	       (not (equal (val x sigma) (cdr (assoc x S)))))
	      (not (matcher sigma S)))))

;;; The three main theorems of this subsubsection
;;; ·············································

  (defthm transform-subs-sel-preserves-matchers-1
    (implies
     (and (not (normal-form-syst S-match))
	  (matcher sigma (union-systems S-match)))
     (matcher sigma (union-systems (transform-subs-sel S-match)))))

  (defthm transform-subs-sel-preserves-matchers-2
    (implies
     (and (not (normal-form-syst S-match))
	  (transform-subs-sel S-match)
	  (matcher sigma (union-systems (transform-subs-sel S-match))))
     (matcher sigma (union-systems S-match))))

  (defthm transform-subs-sel-fail
    (implies
     (and (not (normal-form-syst S-match))
	  (not (transform-subs-sel S-match)))
     (not (matcher sigma (union-systems S-match)))))))




;;; ············································································
;;; 1.2.3 (system-substitution (cdr S-match)) is an invariant in the rules
;;;     of transformation.
;;; ············································································



(local
 (encapsulate
  ()

  (local
   (defthm not-assoc-not-member-domain
     (implies  (and
		(system-substitution S)
		(not (assoc x S)))
	       (not (member x (domain S))))))


  (defthm transform-subs-sel-preserves-system-substitution
    (implies
     (and (not (normal-form-syst S-match))
	  (system-substitution (cdr S-match)))
     (system-substitution (cdr (transform-subs-sel S-match)))))))


;;; ············································································
;;; 1.2.4 Main property of the property system-substitution
;;; ············································································


(local
 (encapsulate
  ()

  (local
   (defthm matcher-extended
     (implies (and (matcher sigma s)
		   (not (member x (domain s)))
		   (system-substitution s))
	      (matcher (cons (cons x term) sigma) s))))

  (defthm system-substitutions-main-property
    (implies (system-substitution S)
	     (matcher S S)))))


;;; ----------------------------------------------------------------------------
;;; 1.3 Termination properties of transform-subs-sel
;;; ----------------------------------------------------------------------------

;;; Let's see that the rules of transformation decreases the length of
;;; the first system of the pair of systems

(encapsulate
 ()

 (local
  (defthm length-system-positive-if-consp
    (implies (consp S)
	     (< 0 (length-system S)))
    :rule-classes :linear))

 (local
  (defthm length-system-append
    (equal (length-system (append s1 s2))
	   (+ (length-system s1) (length-system s2)))))

 (local
  (defthm eliminate-not-member
    (implies (not (member x S))
	     (equal (length-system (eliminate x S))
		    (length-system S)))
    :rule-classes (:rewrite :linear)))

 (local
  (defthm length-system-eliminate
    (implies (member e S)
	     (< (length-system (eliminate e S))
		(length-system S)))
    :rule-classes :linear))

 (local
  (defthm length-system-eliminate-leq
    (<= (length-system (eliminate e S))
	(length-system S))
    :rule-classes :linear))

 (local
  (defthm length-system-selection-and-delete-one-lemma
    (implies (member x S)
 	     (equal (length-system (cons x (delete-one x S)))
 		    (length-system S)))
    :hints (("Goal" :in-theory
	     (disable (:META CANCEL_PLUS-EQUAL-CORRECT))))
    :rule-classes nil)) ;;; I needed this disable, surprisingly (20-8-2002)

 (local
  (defthm length-system-selection-and-delete-one
    (implies (consp (car S-match))
	     (equal (length-system (car S-match))
		    (length-system
		     (cons (a-pair (car S-match))
			   (delete-one (a-pair (car S-match))
				       (car S-match))))))
    :hints (("Goal" :use
	     (:instance length-system-selection-and-delete-one-lemma
			(S (car S-match))
			(x (a-pair (car S-match))))))))

;;; NOTE: This is a very similar technique to use
;;; equal-set-selection-and-eliminate. Note that the same is not true with
;;; eliminate. But the following lemma relates both.

  (local
   (defthm length-system-eliminate-delete-one-x
     (<= (length-system (eliminate x S)) (length-system (delete-one x S)))
     :rule-classes :linear))

  (local
   (defthm length-size-pair-args
     (implies (second (pair-args args1 args2))
	      (equal (length-system (first (pair-args args1 args2)))
		     (+ (length-term nil args1)
			(length-term nil args2))))))

;;; And the main result:
;;; ····················

  (defthm transform-subs-sel-decreases-length-of-first-system
    (implies (not (normal-form-syst S-match))
	     (o< (length-system (car (transform-subs-sel s-match)))
		 (length-system (car s-match))))))

;;; REMARK: This is non-local because will be used to prove admission of
;;; concrete subsumption algorithms by functional instantiation


;;; ----------------------------------------------------------------------------
;;; 1.4 Guard verification: system-p for the first system and
;;; substitution-p for the second system are preserved
;;; ----------------------------------------------------------------------------

(local
 (encapsulate
  ()
;;; ***
  (local
   (defthm system-s-p-variable-s-p
     (implies (and (system-s-p S)
 		   (member equ S)
 		   (variable-p (car equ)))
 	      (variable-s-p (car equ)))))

  (local
   (defthm termp-s-p-member-system-s-p
     (implies (and (system-s-p S)
		   (member equ S))
	      (term-s-p (cdr equ)))))


;;; Closure properties:

  (defthm transform-subs-sel-preserves-system-s-p
    (implies
     (and (not (normal-form-syst S-match))
	  (system-s-p (first S-match)))
     (system-s-p (first (transform-subs-sel S-match)))))


  (defthm transform-subs-sel-sel-preserves-substitution-s-p
    (implies
     (and (not (normal-form-syst S-match))
	  (system-s-p (first S-match))
	  (substitution-s-p (cdr S-match)))
     (substitution-s-p (cdr (transform-subs-sel S-match)))))))

;;; -----------------


;;; ******* We have just "extracted" all the required knowledge of
;;; ******* transform-subs-sel

(in-theory (disable transform-subs-sel))

;;; Other necessary disable:

(local (in-theory (disable union-systems))) ; we will enable later




;;; ============================================================================
;;; 2. Applying transformation rules until a solution (or nil) is found
;;; ============================================================================


;;; ----------------------------------------------------------------------------
;;; 2.1 Definition of subs-system-sel
;;; ----------------------------------------------------------------------------

(defun subs-system-sel (S-match)
  (declare (xargs :measure (length-system (car S-match))))
  (if (normal-form-syst S-match)
      S-match
    (subs-system-sel (transform-subs-sel S-match))))



;;; ----------------------------------------------------------------------------
;;; 2.2 Main properties of subs-system-sel
;;; ----------------------------------------------------------------------------

;;; subs-system-sel inherits the properties of transform-subs-sel

;;; Technical lemma:

(local
 (defthm if-matchable-transform-subs-sel-success
  (implies (subs-system-sel (transform-subs-sel S-match))
	   (transform-subs-sel S-match))))


;;; Three lemmas to state that the set of matchers is preserved by
;;; subs-system-sel
;;; ··········································································

(local
 (defthm subs-system-sel-preserves-matchers-1
   (implies (matcher sigma (union-systems S-match))
	    (matcher sigma (union-systems (subs-system-sel S-match))))))


(local
 (defthm subs-system-sel-preserves-matchers-2
  (implies (and (subs-system-sel S-match)
		(matcher sigma (union-systems (subs-system-sel S-match))))
	   (matcher sigma (union-systems S-match)))))


(local
 (defthm subs-system-sel-fail
  (implies (and (not (subs-system-sel S-match))
		(consp S-match))
	   (not (matcher sigma (union-systems S-match))))))


;;; REMARK: we don't need (consp S-match), but the proofs are shorter.


(in-theory (enable union-systems))

;;; subs-system-sel preserves the property of being a system-substitution.
;;; ··································································

(local
 (defthm subs-system-sel-preserves-system-substitution
  (implies (system-substitution (cdr S-match))
	   (system-substitution (cdr (subs-system-sel S-match))))))

;;; Guard verification: a substitution-s-p is returned
;;; ···················································

(local
 (defthm
   subs-system-sel-substitution-s-p
   (let* ((S (first S-match))  (match (cdr S-match))
	  (subs-system-sel (subs-system-sel S-match))
	  (matcher (cdr subs-system-sel)))
      (implies (and (system-s-p S) (substitution-s-p match))
	       (substitution-s-p matcher)))
      :hints (("Goal" :induct (subs-system-sel S-match)))))



;;; ============================================================================
;;; 3. A (non-deterministic) matching algorithm for systems of equations
;;; ============================================================================

;;; ----------------------------------------------------------------------------
;;; 3.1 Definition of match-sel
;;; ----------------------------------------------------------------------------

(defun match-sel (S)
  (let ((subs-system-sel (subs-system-sel (cons S nil))))
    (if subs-system-sel (list (cdr subs-system-sel)) nil)))

;;; REMARK: To distinguish unsolvability (nil) from the empty
;;; substitution, we "pack" the result in a list.

;;; ----------------------------------------------------------------------------
;;; 3.2 Main properties of match-sel
;;; ----------------------------------------------------------------------------

;;; Technical lemma:

(local
 (defthm
   in-normal-forms-S-is-matchable-by-any-sigma
   (matcher sigma (first (subs-system-sel S-sol)))))


;;; The following properties are inmediate consequence of those given in
;;; 2.2 for subs-system-sel

(defthm match-sel-soundness
  (implies (match-sel S)
	   (matcher (first (match-sel S)) S))
  :rule-classes nil
  :hints
  (("Goal" :use
    (:instance subs-system-sel-preserves-matchers-2
	       (S-match (cons S nil))
	       (sigma (first (match-sel S)))))))


(defthm match-sel-completeness
  (implies (matcher sigma S)
	   (match-sel S))
  :rule-classes nil
  :hints
  (("Goal" :use
    (:instance subs-system-sel-fail (S-match (cons S nil))))))


;;; Closure property: This theorem is needed for guard verification.


(defthm match-sel-substitution-s-p
  (implies (system-s-p S)
	   (substitution-s-p (first (match-sel S))))
  :hints (("Goal" :use
	   (:instance
	    subs-system-sel-substitution-s-p
	    (S-match (cons S nil))))))



;;; We disable match-sel

(local (in-theory (disable match-sel)))

;;; ============================================================================
;;; 4. A (non-deterministic) subsumption algorithm (matching of two terms)
;;; ============================================================================

;;; ----------------------------------------------------------------------------
;;; 4.1 Definition of matching-sel and subs-sel.
;;; ----------------------------------------------------------------------------


(defun subs-sel (t1 t2)
  (match-sel (list (cons t1 t2))))



(defun matching-sel (t1 t2)
  (first (subs-sel t1 t2)))

;;; REMARK: subs-sel checks the subsumption relation between to
;;; terms. If non-nil is a list containing a matching substitution as
;;; its firs element.


;;; ----------------------------------------------------------------------------
;;; 4.2 Soundness and completeness of the subsumption algorithm
;;; ----------------------------------------------------------------------------

;;; Soundness
;;; ·········


(defthm subs-sel-soundness
  (implies (subs-sel t1 t2)
	   (equal (instance t1 (matching-sel t1 t2)) t2))

  :rule-classes nil
  :hints (("Goal" :use
	   (:instance match-sel-soundness (S (list (cons t1 t2)))))))



;;; Completeness
;;; ············


(defthm subs-sel-completeness
  (implies (equal (instance t1 sigma) t2)
	   (subs-sel t1 t2))
  :rule-classes nil
  :hints (("Goal" :use
	   (:instance match-sel-completeness (S (list (cons t1 t2)))))))



;;; Closure property:
;;; (the following theorem is needed for guard verification)
;;; ························································


(defthm matching-sel-substitution-s-p
  (implies (and (term-s-p t1) (term-s-p t2))
	   (substitution-s-p (matching-sel t1 t2))))


;;; SOME CONCLUSIONS:
;;; =================

;;; A comparison is made w.r.t. the more classical definition of a
;;; subsumption algorithm given in the book subsumption-definition-v0.lisp

;;; ADVANTAGES of this rule-based approach:

;;; 1) Several selection strategies could be implemented. A family of
;;; algorithms has been verified.
;;; 2) Less intermediate concepts are needed (extension,
;;; coincide,...). This proof seems easier (although I'm now more
;;; trained with the prover).
;;; 3) A common methodlogy with the verification of a rule-based
;;; unification algorithm is used.


;;; ADVANTAGES of the classical recursive definition:

;;; 1) At the same time, the algorithm for terms and for lists of terms
;;; is verified (nevertheless, the rule-based algorithm can be adapted
;;; to deal with subsumption of lists of terms).