-
Notifications
You must be signed in to change notification settings - Fork 0
/
clos-method-combination-redefinition.impure.lisp
199 lines (161 loc) · 7.29 KB
/
clos-method-combination-redefinition.impure.lisp
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
;;;; testing method combination redefinition
;;;; This software is part of the SBCL system. See the README file for
;;;; more information.
;;;;
;;;; While most of SBCL is derived from the CMU CL system, the test
;;;; files (like this one) were written from scratch after the fork
;;;; from CMU CL.
;;;;
;;;; This software is in the public domain and is provided with
;;;; absolutely no warranty. See the COPYING and CREDITS files for
;;;; more information.
(defpackage "CLOS-METHOD-COMBINATION-REDEFINITION"
(:use "COMMON-LISP" "TEST-UTIL"))
(in-package "CLOS-METHOD-COMBINATION-REDEFINITION")
;;;; long-form method combination redefinition
;;; first, define a method combination
(define-method-combination long-or ()
((primary () :required t))
`(or ,@(reverse (mapcar (lambda (m) `(call-method ,m)) primary))))
;;; GF has standard method combination
(defgeneric long-or-test (x)
(:method ((x fixnum)) 'fixnum)
(:method ((x integer)) 'integer)
(:method ((x number)) 'number))
(with-test (:name (:method-combination standard))
(assert (eql (long-or-test 3) 'fixnum))
(assert (eql (long-or-test (1+ most-positive-fixnum)) 'integer))
(assert (eql (long-or-test 3.2) 'number)))
;;; add the method combination
(defgeneric long-or-test (x)
(:method ((x fixnum)) 'fixnum)
(:method ((x integer)) 'integer)
(:method ((x number)) 'number)
(:method-combination long-or))
(with-test (:name (:method-combination :long-or-reverse))
(assert (eql (long-or-test 3) 'number))
(assert (eql (long-or-test (1+ most-positive-fixnum)) 'number))
(assert (eql (long-or-test 3.2) 'number)))
;;; redefine the method combination
(define-method-combination long-or ()
((primary () :required t))
`(or ,@(mapcar (lambda (m) `(call-method ,m)) primary)))
(with-test (:name (:method-combination :long-or))
(assert (eql (long-or-test 3) 'fixnum))
(assert (eql (long-or-test (1+ most-positive-fixnum)) 'integer))
(assert (eql (long-or-test 3.2) 'number)))
;;;; short-form method-combination redefiniton
;;; define a method-combination
(define-method-combination div :operator /)
(defgeneric short-div (x)
(:method-combination div :most-specific-first)
(:method div ((x number)) 4)
(:method div ((x fixnum)) 8))
(with-test (:name (:method-combination :short-div))
(assert (= (short-div 3) 2))
(assert (= (short-div 3.0) 1/4)))
;;; check that changing method-combination options works
(defgeneric short-div (x)
(:method-combination div :most-specific-last)
(:method div ((x number)) 4)
(:method div ((x fixnum)) 8))
(with-test (:name (:method-combination :short-div :most-specific-last))
(assert (= (short-div 3) 1/2))
(assert (= (short-div 3.0) 1/4)))
;;; check that choosing new short-form options works
(define-method-combination div :operator / :identity-with-one-argument t)
(with-test (:name (:method-combination :short-div :identity-with-one-argument))
(assert (= (short-div 3) 1/2))
(assert (= (short-div 3.0) 4)))
;;; check that changing method-combination options works (deletion of :most-specific-last)
(defgeneric short-div (x)
(:method-combination div)
(:method div ((x number)) 4)
(:method div ((x fixnum)) 8))
(with-test (:name (:method-combination :short-div :identity-with-one-argument :most-specific-first))
(assert (= (short-div 3) 2))
(assert (= (short-div 3.0) 4)))
;;; check that changing operator works
(define-method-combination div :operator -)
(with-test (:name (:method-combination :short-div :operator -))
(assert (= (short-div 3) 4))
(assert (= (short-div 3.0) -4)))
;;;; modifying the need for args-lambda-list
;;; define a fancy method combination. (Happens to implement a finite state machine)
(define-method-combination fsm (default-start)
((primary *))
`(let ((state ',default-start))
(restart-bind
(,@(mapcar (lambda (m) `(,(first (method-qualifiers m))
(lambda ()
(setq state (call-method ,m))
(if (and (typep state '(and symbol (not null)))
(find-restart state))
(invoke-restart state)
state))))
primary))
(invoke-restart state))))
(defclass parse-state ()
((string :initarg :string)
(index :initform 0)))
;;; use the finite state machine to recognize strings with an even
;;; number of #\a characters
(defgeneric even-as (state &key &allow-other-keys)
(:method-combination fsm yes)
(:method yes (state &key)
(with-slots ((s string) (i index)) state
(cond ((= i (length s)) t) ((char= (char s i) #\a) (incf i) 'no) (t (incf i) 'yes))))
(:method no (state &key)
(with-slots ((s string) (i index)) state
(cond ((= i (length s)) nil) ((char= (char s i) #\a) (incf i) 'yes) (t (incf i) 'no)))))
;;; test. (The non-functional :START argument tests are to contrast
;;; with what happens next)
(with-test (:name (:method-combination :finite-state-machine))
(assert (even-as (make-instance 'parse-state :string "abcbab")))
(assert (even-as (make-instance 'parse-state :string "abcbab") :start 'no))
(assert (not (even-as (make-instance 'parse-state :string "abcbabab"))))
(assert (not (even-as (make-instance 'parse-state :string "abcbabab") :start 'no))))
;;; generalize: if we allow the call site to specify the initial
;;; state, our FSM method combination is more expressive.
(define-method-combination fsm (default-start)
((primary *))
(:arguments &key start)
`(let ((state (or ,start ',default-start)))
(restart-bind
(,@(mapcar (lambda (m) `(,(first (method-qualifiers m))
(lambda ()
(setq state (call-method ,m))
(if (and (typep state '(and symbol (not null)))
(find-restart state))
(invoke-restart state)
state))))
primary))
(invoke-restart state))))
(with-test (:name (:method-combination :finite-state-machine :redefinition :args-lambda-list))
(assert (even-as (make-instance 'parse-state :string "abcbab")))
(assert (not (even-as (make-instance 'parse-state :string "abcbab") :start 'no)))
(assert (not (even-as (make-instance 'parse-state :string "abcbabab"))))
(assert (even-as (make-instance 'parse-state :string "abcbabab") :start 'no)))
;;;; changing between short- and long-form method combination
(define-method-combination maximum :operator max)
(defgeneric maxx (x)
(:method-combination maximum)
(:method maximum ((x symbol)) 3)
(:method maximum ((x list)) 4)
(:method maximum ((x null)) 5))
(with-test (:name (:method-combination :maximum))
(assert (= (maxx nil) 5))
(assert (= (maxx '(3 4)) 4))
(assert (= (maxx t) 3)))
(define-method-combination maximum ()
((maximum (maximum)))
`(min ,@(mapcar (lambda (m) `(call-method ,m)) maximum)))
(with-test (:name (:method-combination :maximum :redefined-long))
(assert (= (maxx nil) 3))
(assert (= (maxx '(3 4)) 4))
(assert (= (maxx t) 3)))
(define-method-combination maximum :operator -)
(with-test (:name (:method-combination :maximum :redefined-short))
(assert (= (maxx nil) -2))
(assert (= (maxx '(3 4)) -4))
(assert (= (maxx t) -3)))