Skip to content

Commit fd5d5e9

Browse files
committed
add types for new numeric primitives
- positive-integer? - negative-integer? - nonpositive-integer? - nonnegative-integer? - natural?
1 parent d32218a commit fd5d5e9

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

typed-racket-lib/typed-racket/base-env/base-env-numeric.rkt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1902,6 +1902,15 @@
19021902
[nan? (make-pred-ty (list -Real) B -InexactRealNan)]
19031903

19041904
[infinite? (make-pred-ty (list -Real) B (Un -PosInfinity -NegInfinity))]
1905+
[positive-integer? (asym-pred Univ B (-PS (-is-type 0 (Un -PosInt -PosFlonum -PosSingleFlonum))
1906+
(-not-type 0 -PosInt)))]
1907+
[negative-integer? (asym-pred Univ B (-PS (-is-type 0 (Un -NegInt -NegFlonum -NegSingleFlonum))
1908+
(-not-type 0 -NegInt)))]
1909+
[nonpositive-integer? (asym-pred Univ B (-PS (-is-type 0 (Un -NonPosInt -NonPosFlonum -NonPosSingleFlonum))
1910+
(-not-type 0 -NonPosInt)))]
1911+
[nonnegative-integer? (asym-pred Univ B (-PS (-is-type 0 (Un -Nat -NonNegFlonum -NonNegSingleFlonum))
1912+
(-not-type 0 -Nat)))]
1913+
[natural? (make-pred-ty -Nat)]
19051914

19061915
;; racket/fixnum
19071916
[fx+ (fx+-type)]

typed-racket-test/unit-tests/typecheck-tests.rkt

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4374,6 +4374,54 @@
43744374
(void))
43754375
#:ret (ret -Void #f #f)
43764376
#:msg #rx"type mismatch"]
4377+
4378+
;; pr615 : positive-integer?
4379+
[tc-e/t (let: ([x : Exact-Rational 3/2])
4380+
(if (positive-integer? x) x 0))
4381+
-Nat]
4382+
[tc-e/t (let: ([x : Flonum 1.0])
4383+
(if (positive-integer? x) x 2.0))
4384+
-PosFlonum]
4385+
[tc-e/t (let: ([x : (Un Flonum Positive-Integer) 1.0])
4386+
(if (not (positive-integer? x)) x 1.0))
4387+
-Flonum]
4388+
;; pr615 : negative-integer?
4389+
[tc-e/t (let: ([x : Exact-Rational -3/2])
4390+
(if (negative-integer? x) x -5))
4391+
-NegInt]
4392+
[tc-e/t (let: ([x : Flonum 1.0])
4393+
(if (negative-integer? x) x -2.0))
4394+
-NegFlonum]
4395+
[tc-e/t (let: ([x : (Un Flonum Negative-Integer) -1.0])
4396+
(if (not (negative-integer? x)) x -1.0))
4397+
-Flonum]
4398+
;; pr615 : nonpositive-integer?
4399+
[tc-e/t (let: ([x : Exact-Rational -3/2])
4400+
(if (nonpositive-integer? x) x 0))
4401+
-NonPosInt]
4402+
[tc-e/t (let: ([x : Flonum -1.0])
4403+
(if (nonpositive-integer? x) x 0.0))
4404+
-NonPosFlonum]
4405+
[tc-e/t (let: ([x : (Un Flonum Negative-Integer) -1.0])
4406+
(if (not (nonpositive-integer? x)) x -1.0))
4407+
-Flonum]
4408+
;; pr615 : nonnegative-integer?
4409+
[tc-e/t (let: ([x : Exact-Rational 3/2])
4410+
(if (nonnegative-integer? x) x 0))
4411+
-Nat]
4412+
[tc-e/t (let: ([x : Flonum 1.0])
4413+
(if (nonnegative-integer? x) x 2.0))
4414+
-NonNegFlonum]
4415+
[tc-e/t (let: ([x : (Un Flonum Natural) 1.0])
4416+
(if (not (nonnegative-integer? x)) x 1.0))
4417+
-Flonum]
4418+
;; pr615 : natural?
4419+
[tc-e/t (let: ([x : Real 1])
4420+
(if (natural? x) x 1))
4421+
-Nat]
4422+
[tc-e/t (let: ([x : (Un Flonum Natural) 0.0])
4423+
(if (not (natural? x)) x 1.0))
4424+
-Flonum]
43774425
)
43784426

43794427
(test-suite

0 commit comments

Comments
 (0)