forked from coalton-lang/coalton
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharith.lisp
698 lines (589 loc) · 21 KB
/
arith.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
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
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
;;;; arith.lisp
;;;;
;;;; Number types and basic arithmetic.
(coalton-library/utils::defstdlib-package #:coalton-library/arith
(:use
#:coalton
#:coalton-library/builtin
#:coalton-library/classes)
(:local-nicknames
(#:bits #:coalton-library/bits))
(:export
#:Dividable
#:general/
#:/
#:Quantization
#:Quantizable #:quantize
#:integer->single-float
#:integer->double-float
#:single-float->integer
#:double-float->integer
#:negate
#:abs
#:sign
#:expt
#:ash
#:mkFraction
#:numerator
#:denominator
#:reciprocal
#:floor
#:ceiling
#:round
#:save/
#:exact/
#:inexact/
#:floor/
#:ceiling/
#:round/
#:single/
#:double/
#:1+
#:1-))
#+coalton-release
(cl:declaim #.coalton-impl:*coalton-optimize-library*)
(cl:in-package #:coalton-library/arith)
(coalton-toplevel
;;
;; Dividable
;;
(define-class (Dividable :arg-type :res-type)
"The representation of a type such that division within that type possibly results in another type. For instance,
(Dividable Integer Fraction)
establishes that division of two `Integer`s can result in a `Fraction`, whereas
(Dividable Single-Float Single-Float)
establishes that division of two `Single-Float`s can result in a `Single-Float`.
Note that `Dividable` does *not* establish a default result type; you must constrain the result type yourself.
The function / is partial, and will error produce a run-time error if the divisor is zero.
"
;; This is a type that is more pragmatic and less mathematical in
;; nature. It expresses a division relationship between one input
;; type and one output type.
(general/ (:arg-type -> :arg-type -> :res-type)))
(declare / ((Dividable :a :a) => (:a -> :a -> :a)))
(define / general/)
;;
;; Quantizable
;;
(define-type (Quantization :a)
"Represents an integer quantization of `:a`. See the `Quantizable` typeclass.
The fields are defined as follows:
1. A value of type `:a`.
2. The greatest integer less than or equal to a particular value.
3. The remainder of this as a value of type `:a`.
4. The least integer greater than or equal to a particular value.
5. The remainder of this as a value of type `:a`.
"
(Quantization :a Integer :a Integer :a))
(define-class ((Ord :a) (Num :a) => Quantizable :a)
"The representation of a type that allows \"quantizing\", \"snapping to integers\", or \"rounding.\" (All of these concepts are roughly equivalent.)
"
;; Given a X of type :A, (QUANTIZE X) will return the least
;; integer greater or equal to X, and the greatest integer less
;; than or equal to X, along with their respective remainders
;; expressed as values of type :T.
(quantize (:a -> (Quantization :a)))))
(cl:declaim (cl:inline %unsigned->signed))
(cl:defun %unsigned->signed (bits x)
;; This is the two's complement conversion of X (interpreted as BITS
;; bits) to a signed integer (as a Lisp object).
(cl:-
(cl:ldb (cl:byte (cl:1- bits) 0) x)
(cl:dpb 0 (cl:byte (cl:1- bits) 0) x)))
(cl:defmacro %define-overflow-handler (name bits)
`(cl:defun ,name (value)
(cl:typecase value
((cl:signed-byte ,bits) value)
(cl:otherwise
(cl:cerror "Continue, wrapping around."
,(cl:format cl:nil "Signed value overflowed ~D bits." bits))
(%unsigned->signed ,bits (cl:mod value ,(cl:expt 2 bits)))))))
(cl:eval-when (:compile-toplevel :load-toplevel)
(cl:defparameter +fixnum-bits+
#+sbcl sb-vm:n-fixnum-bits
#-sbcl (cl:1+ (cl:floor (cl:log cl:most-positive-fixnum 2))))
(cl:defparameter +unsigned-fixnum-bits+
(cl:1- +fixnum-bits+)))
(%define-overflow-handler %handle-8bit-overflow 8)
(%define-overflow-handler %handle-16bit-overflow 16)
(%define-overflow-handler %handle-32bit-overflow 32)
(%define-overflow-handler %handle-64bit-overflow 64)
(%define-overflow-handler %handle-fixnum-overflow #.+fixnum-bits+)
(cl:defmacro %define-number-stuff (coalton-type)
`(coalton-toplevel
(define-instance (Eq ,coalton-type)
(define (== a b)
(lisp Boolean (a b)
(to-boolean (cl:= a b)))))
(define-instance (Ord ,coalton-type)
(define (<=> a b)
(lisp Ord (a b)
(cl:cond
((cl:< a b)
LT)
((cl:> a b)
GT)
(cl:t
EQ)))))))
(%define-number-stuff U8)
(%define-number-stuff U16)
(%define-number-stuff U32)
(%define-number-stuff U64)
(%define-number-stuff I8)
(%define-number-stuff I16)
(%define-number-stuff I32)
(%define-number-stuff I64)
(%define-number-stuff Integer)
(%define-number-stuff Fraction)
(%define-number-stuff IFix)
(%define-number-stuff UFix)
(%define-number-stuff Single-Float)
(%define-number-stuff Double-Float)
(coalton-toplevel
(define-instance (Num I8)
(define (+ a b)
(lisp I8 (a b)
(%handle-8bit-overflow (cl:+ a b))))
(define (- a b)
(lisp I8 (a b)
(%handle-8bit-overflow (cl:- a b))))
(define (* a b)
(lisp I8 (a b)
(%handle-8bit-overflow (cl:* a b))))
(define (fromInt x)
(lisp I8 (x)
(%handle-8bit-overflow x))))
(define-instance (Num I16)
(define (+ a b)
(lisp I16 (a b)
(%handle-16bit-overflow (cl:+ a b))))
(define (- a b)
(lisp I16 (a b)
(%handle-16bit-overflow (cl:- a b))))
(define (* a b)
(lisp I16 (a b)
(%handle-16bit-overflow (cl:* a b))))
(define (fromInt x)
(lisp I16 (x)
(%handle-16bit-overflow x))))
(define-instance (Num I32)
(define (+ a b)
(lisp I32 (a b)
(%handle-32bit-overflow (cl:+ a b))))
(define (- a b)
(lisp I32 (a b)
(%handle-32bit-overflow (cl:- a b))))
(define (* a b)
(lisp I32 (a b)
(%handle-32bit-overflow (cl:* a b))))
(define (fromInt x)
(lisp I32 (x)
(%handle-32bit-overflow x))))
(define-instance (Num I64)
(define (+ a b)
(lisp I64 (a b)
(%handle-64bit-overflow (cl:+ a b))))
(define (- a b)
(lisp I64 (a b)
(%handle-64bit-overflow (cl:- a b))))
(define (* a b)
(lisp I64 (a b)
(%handle-64bit-overflow (cl:* a b))))
(define (fromInt x)
(lisp I64 (x)
(%handle-64bit-overflow x))))
(define-instance (Num IFix)
(define (+ a b)
(lisp IFix (a b)
(%handle-fixnum-overflow (cl:+ a b))))
(define (- a b)
(lisp IFix (a b)
(%handle-fixnum-overflow (cl:- a b))))
(define (* a b)
(lisp IFix (a b)
(%handle-fixnum-overflow (cl:* a b))))
(define (fromInt x)
(lisp IFix (x)
(%handle-fixnum-overflow x)))))
(cl:defmacro %define-signed-instances (coalton-type bits cl:&rest supers)
(cl:declare (cl:ignore bits))
`(coalton-toplevel
(define-instance (Into Integer ,coalton-type)
(define (into x) (fromInt x)))
,@(cl:loop :for super :in supers :collecting
`(define-instance (Into ,coalton-type ,super)
(define (into x)
(lisp ,super (x)
x))))))
(%define-signed-instances I8 8 Integer I64 I32 I16)
(%define-signed-instances I16 16 Integer I64 I32)
(%define-signed-instances I32 32 Integer I64)
(%define-signed-instances I64 64 Integer)
(%define-signed-instances IFix #.+fixnum-bits+ Integer)
(cl:defmacro %define-unsigned-num-instance (coalton-type bits cl:&rest supers)
`(coalton-toplevel
(define-instance (Num ,coalton-type)
(define (+ a b)
(lisp ,coalton-type (a b)
(cl:values (cl:mod (cl:+ a b) ,(cl:expt 2 bits)))))
(define (- a b)
(lisp ,coalton-type (a b)
(cl:values (cl:mod (cl:- a b) ,(cl:expt 2 bits)))))
(define (* a b)
(lisp ,coalton-type (a b)
(cl:values (cl:mod (cl:* a b) ,(cl:expt 2 bits)))))
(define (fromInt x)
(lisp ,coalton-type (x)
(cl:values (cl:mod x ,(cl:expt 2 bits))))))
(define-instance (Into Integer ,coalton-type)
(define (into x) (fromInt x)))
,@(cl:loop :for super :in supers :collecting
`(define-instance (Into ,coalton-type ,super)
(define (into x)
(lisp ,super (x)
x))))
(define-instance (Into ,coalton-type Single-Float)
(define (into x)
(lisp Single-Float (x)
(cl:coerce x 'cl:single-float))))
(define-instance (Into ,coalton-type Double-Float)
(define (into x)
(lisp Double-Float (x)
(cl:coerce x 'cl:double-float))))))
(%define-unsigned-num-instance U8 8 Integer U64 I64 U32 I32 U16 I16)
(%define-unsigned-num-instance U16 16 Integer U64 I64 U32 I32)
(%define-unsigned-num-instance U32 32 Integer U64 I64)
(%define-unsigned-num-instance U64 64 Integer)
(%define-unsigned-num-instance UFix #.+unsigned-fixnum-bits+ Integer)
(coalton-toplevel
(declare integer->single-float (Integer -> Single-Float))
(define (integer->single-float z)
(lisp Single-Float (z)
(cl:let ((x (cl:ignore-errors
(cl:coerce z 'cl:single-float))))
(cl:if (cl:null x)
float-features:single-float-nan
x))))
(declare integer->double-float (Integer -> Double-Float))
(define (integer->double-float z)
(lisp Double-Float (z)
(cl:let ((x (cl:ignore-errors
(cl:coerce z 'cl:double-float))))
(cl:if (cl:null x)
float-features:double-float-nan
x))))
(declare single-float->integer (Single-Float -> Optional Integer))
(define (single-float->integer x)
"Round a Single-Float to the nearest Integer."
(lisp (Optional Integer) (x)
(cl:if (cl:or (float-features:float-infinity-p x)
(float-features:float-nan-p x))
None
(Some (cl:round x)))))
(declare double-float->integer (Double-Float -> Optional Integer))
(define (double-float->integer x)
"Round a Double-Float to the nearest Integer."
(lisp (Optional Integer) (x)
(cl:if (cl:or (float-features:float-infinity-p x)
(float-features:float-nan-p x))
None
(Some (cl:round x)))))
(define-instance (Num Integer)
(define (+ a b)
(lisp Integer (a b) (cl:+ a b)))
(define (- a b)
(lisp Integer (a b) (cl:- a b)))
(define (* a b)
(lisp Integer (a b) (cl:* a b)))
(define (fromInt x)
x))
(define-instance (Num Single-Float)
(define (+ a b)
(lisp Single-Float (a b) (cl:+ a b)))
(define (- a b)
(lisp Single-Float (a b) (cl:- a b)))
(define (* a b)
(lisp Single-Float (a b) (cl:* a b)))
(define (fromInt x)
(integer->single-float x)))
(define-instance (Num Double-Float)
(define (+ a b)
(lisp Double-Float (a b) (cl:+ a b)))
(define (- a b)
(lisp Double-Float (a b) (cl:- a b)))
(define (* a b)
(lisp Double-Float (a b) (cl:* a b)))
(define (fromInt x)
(integer->double-float x)))
(declare negate (Num :a => :a -> :a))
(define (negate x)
(- 0 x))
(declare abs ((Ord :a) (Num :a) => :a -> :a))
(define (abs x)
"Absolute value of X."
(if (< x 0)
(negate x)
x))
(declare sign ((Ord :a) (Num :a) => :a -> Integer))
(define (sign x)
"The sign of X."
(if (< x 0)
-1
1))
(declare expt (Integer -> Integer -> Integer))
(define (expt base power)
"Exponentiate BASE to a non-negative POWER."
(if (< power 0)
(error "Can't exponentiate with a negative exponent.")
(lisp Integer (base power) (cl:expt base power))))
(declare ash (Integer -> Integer -> Integer))
(define (ash x n)
"Compute the \"arithmetic shift\" of X by N. "
(lisp Integer (x n) (cl:ash x n))))
(coalton-toplevel
;; We avoid "Rational" or "Ratio" since those might be a more
;; generic concept than a humble fraction of integers. This
;; fraction is always assumed to be in reduced terms.
(declare mkFraction (Integer -> Integer -> Fraction))
(define (mkFraction a b)
(lisp Fraction (a b)
(cl:/ a b)))
(declare numerator (Fraction -> Integer))
(define (numerator q)
"The numerator of a fraction."
(lisp Integer (q)
(cl:numerator q)))
(declare denominator (Fraction -> Integer))
(define (denominator q)
"The denominator of a fraction."
(lisp Integer (q)
(cl:denominator q)))
(declare reciprocal ((Dividable :a :a) (Num :a) => :a -> :a))
(define (reciprocal x)
"The multiplicative inverse of a number."
(/ 1 x))
(specialize reciprocal reciprocal-frac (Fraction -> Fraction))
(declare reciprocal-frac (Fraction -> Fraction))
(define (reciprocal-frac q)
"The reciprocal of a fraction."
(lisp Fraction (q)
(cl:/ q)))
(define-instance (Num Fraction)
(define (+ p q)
(lisp Fraction (p q)
(cl:+ p q)))
(define (- p q)
(lisp Fraction (p q)
(cl:- p q)))
(define (* p q)
(lisp Fraction (p q)
(cl:* p q)))
(define (fromInt z)
(lisp Fraction (z) z))))
(coalton-toplevel
(define-instance (Dividable Fraction Fraction)
(define (general/ a b)
(lisp Fraction (a b)
(cl:/ a b))))
(define-instance (Dividable Single-Float Single-Float)
(define (general/ x y)
(lisp Single-Float (x y)
(cl:/ x y))))
(define-instance (Dividable Double-Float Double-Float)
(define (general/ x y)
(lisp Double-Float (x y)
(cl:/ x y))))
(define-instance (Dividable Integer Fraction)
(define (general/ x y)
(mkFraction x y)))
(define-instance (Dividable Integer Single-Float)
(define (general/ x y)
(lisp Single-Float (x y)
(cl:coerce (cl:/ x y) 'cl:single-float))))
(define-instance (Dividable Integer Double-Float)
(define (general/ x y)
(lisp Double-Float (x y)
(cl:coerce (cl:/ x y) 'cl:double-float)))))
(coalton-toplevel
(define-instance (Into Integer String)
(define (into z)
(lisp String (z)
(cl:format cl:nil "~D" z))))
(define-instance (TryInto String Integer)
(define (tryInto s)
(lisp (Result String Integer) (s)
(cl:let ((z (cl:ignore-errors (cl:parse-integer s))))
(cl:if (cl:null z)
(Err "String doesn't have integer syntax.")
(Ok z)))))))
;;;; `Bits' instances
;;; signed
(cl:defmacro define-signed-bit-instance (type handle-overflow)
(cl:flet ((lisp-binop (op)
`(lisp ,type (left right)
(,op left right))))
`(coalton-toplevel
(define-instance (bits:Bits ,type)
(define (bits:and left right)
,(lisp-binop 'cl:logand))
(define (bits:or left right)
,(lisp-binop 'cl:logior))
(define (bits:xor left right)
,(lisp-binop 'cl:logxor))
(define (bits:not bits)
(lisp ,type (bits) (cl:lognot bits)))
(define (bits:shift amount bits)
(lisp ,type (amount bits)
(,handle-overflow (cl:ash bits amount))))))))
(define-signed-bit-instance I8 %handle-8bit-overflow)
(define-signed-bit-instance I16 %handle-16bit-overflow)
(define-signed-bit-instance I32 %handle-32bit-overflow)
(define-signed-bit-instance I64 %handle-64bit-overflow)
(define-signed-bit-instance IFix %handle-fixnum-overflow)
(define-signed-bit-instance Integer cl:identity)
;;; unsigned
(cl:declaim (cl:inline unsigned-lognot)
(cl:ftype (cl:function (cl:unsigned-byte cl:unsigned-byte)
(cl:values cl:unsigned-byte cl:&optional))
unsigned-lognot))
(cl:defun unsigned-lognot (int n-bits)
(cl:- (cl:ash 1 n-bits) int 1))
(cl:declaim (cl:inline handle-unsigned-overflow)
(cl:ftype (cl:function (cl:unsigned-byte cl:unsigned-byte)
(cl:values cl:unsigned-byte cl:&optional))
handle-unsigned-overflow))
(cl:defun handle-unsigned-overflow (int n-bits)
(cl:logand (cl:1- (cl:ash 1 n-bits))
int))
(cl:defmacro define-unsigned-bit-instance (type width)
(cl:flet ((define-binop (coalton-name lisp-name)
`(define (,coalton-name left right)
(lisp ,type (left right)
(,lisp-name left right)))))
`(coalton-toplevel
(define-instance (bits:Bits ,type)
,(define-binop 'bits:and 'cl:logand)
,(define-binop 'bits:or 'cl:logior)
,(define-binop 'bits:xor 'cl:logxor)
(define (bits:not bits)
(lisp ,type (bits) (unsigned-lognot bits ,width)))
(define (bits:shift amount bits)
(lisp ,type (amount bits)
(cl:logand (cl:ash bits amount)
(cl:1- (cl:ash 1 ,width)))))))))
(define-unsigned-bit-instance U8 8)
(define-unsigned-bit-instance U16 16)
(define-unsigned-bit-instance U32 32)
(define-unsigned-bit-instance U64 64)
(define-unsigned-bit-instance UFix #.+unsigned-fixnum-bits+)
;;;; `Hash' instances
(define-sxhash-hasher I8)
(define-sxhash-hasher I16)
(define-sxhash-hasher I32)
(define-sxhash-hasher I64)
(define-sxhash-hasher U8)
(define-sxhash-hasher U16)
(define-sxhash-hasher U32)
(define-sxhash-hasher U64)
(define-sxhash-hasher Integer)
(define-sxhash-hasher IFix)
(define-sxhash-hasher UFix)
(define-sxhash-hasher Single-Float)
(define-sxhash-hasher Double-Float)
;;; `Quantization'
(coalton-toplevel
(define-instance (Quantizable Integer)
(define (quantize x)
(Quantization x x 0 x 0))))
(cl:macrolet ((define-integer-quantizations (cl:&rest int-types)
`(coalton-toplevel
,@(cl:loop :for ty :in int-types :collect
`(define-instance (Quantizable ,ty)
(define (quantize x)
(let ((n (into x)))
(Quantization x n 0 n 0))))))))
(define-integer-quantizations I32 I64 U8 U32 U64))
(coalton-toplevel
(define-instance (Quantizable Single-Float)
(define (quantize f)
(lisp (Quantization Single-Float) (f)
(uiop:nest
(cl:multiple-value-bind (fl-quo fl-rem) (cl:floor f))
(cl:multiple-value-bind (ce-quo ce-rem) (cl:ceiling f))
(Quantization f fl-quo fl-rem ce-quo ce-rem)))))
(define-instance (Quantizable Double-Float)
(define (quantize f)
(lisp (Quantization Double-Float) (f)
(uiop:nest
(cl:multiple-value-bind (fl-quo fl-rem) (cl:floor f))
(cl:multiple-value-bind (ce-quo ce-rem) (cl:ceiling f))
(Quantization f fl-quo fl-rem ce-quo ce-rem)))))
(define-instance (Quantizable Fraction)
(define (quantize q)
(let ((n (numerator q))
(d (denominator q)))
(lisp (Quantization Fraction) (n d)
;; Not the most efficient... just relying on CL to do the
;; work.
(cl:flet ((to-frac (f)
(mkFraction (cl:numerator f) (cl:denominator f))))
(cl:let ((f (cl:/ n d)))
(uiop:nest
(cl:multiple-value-bind (fl-quo fl-rem) (cl:floor f))
(cl:multiple-value-bind (ce-quo ce-rem) (cl:ceiling f))
(Quantization f
fl-quo (to-frac fl-rem)
ce-quo (to-frac ce-rem)))))))))
(define (floor x)
"Return the greatest integer less than or equal to X."
(match (quantize x)
((Quantization _ z _ _ _) z)))
(define (ceiling x)
"Return the least integer greater than or equal to X."
(match (quantize x)
((Quantization _ _ _ z _) z)))
(define (round x)
"Return the nearest integer to X, with ties breaking toward positive infinity."
(match (quantize x)
((Quantization _ a ar b br)
(match (<=> (abs ar) (abs br))
((LT) a)
((GT) b)
((EQ) (max a b))))))
(declare safe/ ((Num :a) (Dividable :a :b) => (:a -> :a -> (Optional :b))))
(define (safe/ x y)
"Safely divide X by Y, returning None if Y is zero."
(if (== y 0)
None
(Some (general/ x y))))
(declare exact/ (Integer -> Integer -> Fraction))
(define (exact/ a b)
"Exactly divide two integers and produce a fraction."
(general/ a b))
(declare inexact/ (Integer -> Integer -> Double-Float))
(define (inexact/ a b)
"Compute the quotient of integers as a double-precision float.
Note: This does *not* divide double-float arguments."
(general/ a b))
(declare floor/ (Integer -> Integer -> Integer))
(define (floor/ a b)
"Divide two integers and compute the floor of the quotient."
(floor (exact/ a b)))
(declare ceiling/ (Integer -> Integer -> Integer))
(define (ceiling/ a b)
"Divide two integers and compute the ceiling of the quotient."
(ceiling (exact/ a b)))
(declare round/ (Integer -> Integer -> Integer))
(define (round/ a b)
"Divide two integers and round the quotient."
(round (exact/ a b))))
;;; `Num' extensions
(coalton-toplevel
(declare 1+ ((Num :num) => :num -> :num))
(define (1+ num)
(+ num (fromInt 1)))
(declare 1- ((Num :num) => :num -> :num))
(define (1- num)
(- num (fromInt 1))))
#+sb-package-locks
(sb-ext:lock-package "COALTON-LIBRARY/ARITH")