/
extension.lisp
307 lines (230 loc) · 8.28 KB
/
extension.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
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
(in-package :geb.extension.spec)
;; Sadly no polynomial category to extend ☹
(defclass common-sub-expression (direct-pointwise-mixin meta-mixin cat-morph)
((obj :initarg :obj
:accessor obj)
(name :initarg :name
:accessor name))
(:documentation
"I represent common sub-expressions found throughout the code.
I implement a few categorical extensions. I am a valid
[CAT-MORPH][class] along with fulling the interface for the
[GEB.POLY.SPEC:<POLY>] category.
The name should come from an algorithm that automatically fines common
sub-expressions and places the appropriate names."))
(defclass opaque (cat-obj meta-mixin)
((name :initarg :name :accessor name)
(code :initarg :code :accessor code))
(:documentation
"I represent an object where we want to hide the implementation
details of what kind of [GEB:SUBSTOBJ][type] I am."))
(defclass reference (cat-obj cat-morph direct-pointwise-mixin meta-mixin)
((name :initarg :name :accessor name))
(:documentation
"I represent a reference to an [OPAQUE][class] identifier."))
(defclass opaque-morph (cat-morph meta-mixin)
((code :initarg :code
:accessor code
:documentation "the code that represents the underlying morphsism")
(dom :initarg :dom
:accessor dom
:documentation "The dom of the opaque morph")
(codom :initarg :codom
:accessor codom
:documentation "The codom of the opaque morph"))
(:documentation
"This represents a morphsim where we want to deal with an
[OPAQUE][class] that we know intimate details of"))
(defun make-common-sub-expression (&key obj name)
(make-instance 'common-sub-expression :obj obj :name name))
(defun reference (name)
(make-instance 'reference :name name))
(defun opaque-morph (code &key (dom (dom code)) (codom (codom code)))
(make-instance 'opaque-morph :code code :dom dom :codom codom))
(defun opaque (name code)
(make-instance 'opaque :name name :code code))
(defclass <natobj> (direct-pointwise-mixin meta-mixin cat-obj cat-morph) ()
(:documentation "the class corresponding to [NATOBJ], the extension of
[SUBSTOBJ][class] adding to Geb bit representation of natural numbers." ))
(deftype natobj ()
'(or nat-width))
(defclass <natmorph> (direct-pointwise-mixin meta-mixin cat-morph) ()
(:documentation "the class corresponding to [NATMORPH], the extension of
[SUBSTMORPH][class] adding to Geb basic operations on bit representations
of natural numbers"))
(deftype natmorph ()
'(or nat-add nat-inj nat-mult nat-sub nat-div nat-const nat-concat
one-bit-to-bool nat-decompose nat-eq nat-lt nat-mod))
(defclass nat-width (<natobj>)
((num :initarg :num
:accessor num
:documentation ""))
(:documentation
"the [NAT-WIDTH][class] object. Takes a non-zero natural
number [NUM] and produces an object standing for cardinality
2^([NUM]) corresponding to [NUM]-wide bit number.
The formal grammar of [NAT-WIDTH][class] is
```lisp
(nat-width num)
```
where [NAT-WIDTH][class] is the constructor, [NUM] the choice
of a natural number we want to be the width of the bits we
are to consder."))
(-> nat-width (fixnum) nat-width)
(defun nat-width (num)
(values
(make-instance 'nat-width :num num)))
(defclass nat-add (<natmorph>)
((num :initarg :num
:accessor num
:documentation ""))
(:documentation "Given a natural number [NUM] greater than 0, gives a morphsm
(nat-add num) : (nat-mod num) x (nat-mod num) -> (nat-mod num) representing
floored addition of two bits of length n.
The formal grammar of [NAT-ADD][class] is
```lisp
(nat-add num)
``` "))
(defclass nat-mult (<natmorph>)
((num :initarg :num
:accessor num
:documentation ""))
(:documentation "Given a natural number [NUM] greater than 0, gives a morphsm
(nat-mult num) : (nat-mod num) x (nat-mod num) -> (nat-mod n) representing floored
multiplication in natural numbers modulo n.
The formal grammar of [NAT-MULT][class] is
```lisp
(nat-mult num)
``` "))
(defclass nat-sub (<natmorph>)
((num :initarg :num
:accessor num
:documentation ""))
(:documentation "Given a natural number [NUM] greater than 0, gives a morphsm
(nat-sub sum) : (nat-mod num) x (nat-mod num) -> (nat-mod num) representing
floored subtraction of two bits of length n.
The formal grammar of [NAT-SUB][class] is
```lisp
(nat-sub num)
``` "))
(defclass nat-div (<natmorph>)
((num :initarg :num
:accessor num
:documentation ""))
(:documentation "Given a natural number [NUM] greater than 0, gives a morphsm
(nat-div num) : (nat-mod num) x (nat-mod num) -> (nat-mod num) representing
floored division in natural numbers modulo n.
The formal grammar of [NAT-DIV][class] is
```lisp
(nat-div num)
``` "))
(defclass nat-const (<natmorph>)
((num :initarg :num
:accessor num
:documentation "")
(pos :initarg :pos
:accessor pos
:documentation ""))
(:documentation "Given a NUM natural number, gives a morphsm
(nat-const num pos) : so1 -> (nat-width num).
That is, chooses the POS natural number as a NUM-wide bit number
The formal grammar of [NAT-ADD][class] is
```lisp
(nat-const num pos)
``` "))
(defclass nat-inj (<natmorph>)
((num :initarg :num
:accessor num
:documentation ""))
(:documentation "Given a nutural number [NUM] presents a [NUM]-wide bit number
as a ([NUM] + 1)-wide bit number via injecting.
The formal grammar of [NAT-INJ][class] is
```lisp
(nat-inj num)
```
In Geb, the injection presents itself as a morphism
(nat-width num) -> (nat-width (1 + num))"))
(defclass nat-concat (<natmorph>)
((num-left :initarg :num-left
:accessor num-left
:documentation "")
(num-right :initarg :num-right
:accessor num-right
:documentation ""))
(:documentation "Given two natural numbers of bit length n and m, concatenates
them in that order giving a bit of length n + m.
The formal grammar of [NAT-CONCAT][class] is
```lisp
(nat-concat num-left num-right)
```
In Geb this corresponds to a morphism
(nat-width num-left) x (nat-width num-right) -> (nat-width (n + m))
For a translation to SeqN simply take x of n width and y of m with and
take x^m + y"))
(defclass one-bit-to-bool (<natmorph>)
()
(:documentation "A map nat-width 1 -> bool sending #*0 to
false and #*1 to true"))
(defclass nat-decompose (<natmorph>)
((num :initarg :num
:accessor num
:documentation ""))
(:documentation "Morphism nat-width n -> (nat-width 1) x (nat-width (1- n))
splitting a natural number into the last and all but last collection of bits"))
(defclass nat-eq (<natmorph>)
((num :initarg :num
:accessor num
:documentation ""))
(:documentation "Morphism nat-width n x nat-width n -> bool
which evaluated to true iff both inputs are the same"))
(defclass nat-lt (<natmorph>)
((num :initarg :num
:accessor num
:documentation ""))
(:documentation "Morphism nat-width n x nat-width n -> bool
which evaluated to true iff the first input is less than the second"))
(defclass nat-mod (<natmorph>)
((num :initarg :num
:accessor num
:documentation ""))
(:documentation "Morphism nat-width n x nat-width n -> nat-width n
which takes a modulo of the left projection of a pair by the second
projection of a pari"))
(defun nat-add (num)
(values
(make-instance 'nat-add :num num)))
(defun nat-mult (num)
(values
(make-instance 'nat-mult :num num)))
(defun nat-sub (num)
(values
(make-instance 'nat-sub :num num)))
(defun nat-div (num)
(values
(make-instance 'nat-div :num num)))
(defun nat-const (num pos)
(values
(make-instance 'nat-const :num num :pos pos)))
(defun nat-inj (num)
(values
(make-instance 'nat-inj :num num)))
(defun nat-concat (num-left num-right)
(values
(make-instance 'nat-concat :num-left num-left :num-right num-right)))
(defun one-bit-to-bool ()
(make-instance 'one-bit-to-bool))
(defparameter *one-bit-to-bool*
(make-instance 'one-bit-to-bool))
(def one-bit-to-bool *one-bit-to-bool*)
(defun nat-decompose (num)
(make-instance 'nat-decompose :num num))
(defun nat-eq (num)
(make-instance 'nat-eq :num num))
(defun nat-lt (num)
(make-instance 'nat-lt :num num))
(defun nat-mod (num)
(make-instance 'nat-mod :num num))
(defgeneric num (obj))
(defgeneric pos (obj))
(defgeneric num-left (obj))
(defgeneric num-right (obj))