Coverage report: /home/runner/work/geb/geb/src/specs/extension.lisp
Kind | Covered | All | % |
expression | 30 | 78 | 38.5 |
branch | 0 | 0 | nil |
Key
Not instrumented
Conditionalized out
Executed
Not executed
Both branches taken
One branch taken
Neither branch taken
1
(in-package :geb.extension.spec)
3
;; Sadly no polynomial category to extend ☹
4
(defclass common-sub-expression (direct-pointwise-mixin meta-mixin cat-morph)
10
"I represent common sub-expressions found throughout the code.
12
I implement a few categorical extensions. I am a valid
13
[CAT-MORPH][class] along with fulling the interface for the
14
[GEB.POLY.SPEC:<POLY>] category.
16
The name should come from an algorithm that automatically fines common
17
sub-expressions and places the appropriate names."))
20
(defclass opaque (cat-obj meta-mixin)
21
((name :initarg :name :accessor name)
22
(code :initarg :code :accessor code))
24
"I represent an object where we want to hide the implementation
25
details of what kind of [GEB:SUBSTOBJ][type] I am."))
27
(defclass reference (cat-obj cat-morph direct-pointwise-mixin meta-mixin)
28
((name :initarg :name :accessor name))
30
"I represent a reference to an [OPAQUE][class] identifier."))
32
(defclass opaque-morph (cat-morph meta-mixin)
35
:documentation "the code that represents the underlying morphsism")
38
:documentation "The dom of the opaque morph")
39
(codom :initarg :codom
41
:documentation "The codom of the opaque morph"))
43
"This represents a morphsim where we want to deal with an
44
[OPAQUE][class] that we know intimate details of"))
46
(defun make-common-sub-expression (&key obj name)
47
(make-instance 'common-sub-expression :obj obj :name name))
49
(defun reference (name)
50
(make-instance 'reference :name name))
52
(defun opaque-morph (code &key (dom (dom code)) (codom (codom code)))
53
(make-instance 'opaque-morph :code code :dom dom :codom codom))
55
(defun opaque (name code)
56
(make-instance 'opaque :name name :code code))
58
(defclass <natobj> (direct-pointwise-mixin meta-mixin cat-obj cat-morph) ()
59
(:documentation "the class corresponding to [NATOBJ], the extension of
60
[SUBSTOBJ][class] adding to Geb bit representation of natural numbers." ))
65
(defclass <natmorph> (direct-pointwise-mixin meta-mixin cat-morph) ()
66
(:documentation "the class corresponding to [NATMORPH], the extension of
67
[SUBSTMORPH][class] adding to Geb basic operations on bit representations
71
'(or nat-add nat-inj nat-mult nat-sub nat-div nat-const nat-concat
72
one-bit-to-bool nat-decompose nat-eq nat-lt nat-mod))
74
(defclass nat-width (<natobj>)
79
"the [NAT-WIDTH][class] object. Takes a non-zero natural
80
number [NUM] and produces an object standing for cardinality
81
2^([NUM]) corresponding to [NUM]-wide bit number.
83
The formal grammar of [NAT-WIDTH][class] is
89
where [NAT-WIDTH][class] is the constructor, [NUM] the choice
90
of a natural number we want to be the width of the bits we
93
(-> nat-width (fixnum) nat-width)
94
(defun nat-width (num)
96
(make-instance 'nat-width :num num)))
98
(defclass nat-add (<natmorph>)
102
(:documentation "Given a natural number [NUM] greater than 0, gives a morphsm
103
(nat-add num) : (nat-mod num) x (nat-mod num) -> (nat-mod num) representing
104
floored addition of two bits of length n.
106
The formal grammar of [NAT-ADD][class] is
112
(defclass nat-mult (<natmorph>)
116
(:documentation "Given a natural number [NUM] greater than 0, gives a morphsm
117
(nat-mult num) : (nat-mod num) x (nat-mod num) -> (nat-mod n) representing floored
118
multiplication in natural numbers modulo n.
120
The formal grammar of [NAT-MULT][class] is
126
(defclass nat-sub (<natmorph>)
130
(:documentation "Given a natural number [NUM] greater than 0, gives a morphsm
131
(nat-sub sum) : (nat-mod num) x (nat-mod num) -> (nat-mod num) representing
132
floored subtraction of two bits of length n.
134
The formal grammar of [NAT-SUB][class] is
140
(defclass nat-div (<natmorph>)
144
(:documentation "Given a natural number [NUM] greater than 0, gives a morphsm
145
(nat-div num) : (nat-mod num) x (nat-mod num) -> (nat-mod num) representing
146
floored division in natural numbers modulo n.
148
The formal grammar of [NAT-DIV][class] is
154
(defclass nat-const (<natmorph>)
161
(:documentation "Given a NUM natural number, gives a morphsm
162
(nat-const num pos) : so1 -> (nat-width num).
164
That is, chooses the POS natural number as a NUM-wide bit number
166
The formal grammar of [NAT-ADD][class] is
172
(defclass nat-inj (<natmorph>)
176
(:documentation "Given a nutural number [NUM] presents a [NUM]-wide bit number
177
as a ([NUM] + 1)-wide bit number via injecting.
179
The formal grammar of [NAT-INJ][class] is
185
In Geb, the injection presents itself as a morphism
186
(nat-width num) -> (nat-width (1 + num))"))
188
(defclass nat-concat (<natmorph>)
189
((num-left :initarg :num-left
192
(num-right :initarg :num-right
195
(:documentation "Given two natural numbers of bit length n and m, concatenates
196
them in that order giving a bit of length n + m.
198
The formal grammar of [NAT-CONCAT][class] is
201
(nat-concat num-left num-right)
204
In Geb this corresponds to a morphism
205
(nat-width num-left) x (nat-width num-right) -> (nat-width (n + m))
207
For a translation to SeqN simply take x of n width and y of m with and
210
(defclass one-bit-to-bool (<natmorph>)
212
(:documentation "A map nat-width 1 -> bool sending #*0 to
213
false and #*1 to true"))
215
(defclass nat-decompose (<natmorph>)
219
(:documentation "Morphism nat-width n -> (nat-width 1) x (nat-width (1- n))
220
splitting a natural number into the last and all but last collection of bits"))
222
(defclass nat-eq (<natmorph>)
226
(:documentation "Morphism nat-width n x nat-width n -> bool
227
which evaluated to true iff both inputs are the same"))
229
(defclass nat-lt (<natmorph>)
233
(:documentation "Morphism nat-width n x nat-width n -> bool
234
which evaluated to true iff the first input is less than the second"))
236
(defclass nat-mod (<natmorph>)
240
(:documentation "Morphism nat-width n x nat-width n -> nat-width n
241
which takes a modulo of the left projection of a pair by the second
242
projection of a pari"))
246
(make-instance 'nat-add :num num)))
249
(defun nat-mult (num)
251
(make-instance 'nat-mult :num num)))
256
(make-instance 'nat-sub :num num)))
261
(make-instance 'nat-div :num num)))
264
(defun nat-const (num pos)
266
(make-instance 'nat-const :num num :pos pos)))
271
(make-instance 'nat-inj :num num)))
274
(defun nat-concat (num-left num-right)
276
(make-instance 'nat-concat :num-left num-left :num-right num-right)))
278
(defun one-bit-to-bool ()
279
(make-instance 'one-bit-to-bool))
281
(defparameter *one-bit-to-bool*
282
(make-instance 'one-bit-to-bool))
284
(def one-bit-to-bool *one-bit-to-bool*)
286
(defun nat-decompose (num)
287
(make-instance 'nat-decompose :num num))
290
(make-instance 'nat-eq :num num))
293
(make-instance 'nat-lt :num num))
296
(make-instance 'nat-mod :num num))
298
(defgeneric num (obj))
299
(defgeneric pos (obj))
300
(defgeneric num-left (obj))
301
(defgeneric num-right (obj))