Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changing sleep to accept (Rational :a) types #1305

Merged
merged 1 commit into from
Oct 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions library/system.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#:coalton
#:coalton-library/builtin
#:coalton-library/classes)
(:local-nicknames
(#:math #:coalton-library/math))
(:export
#:gc
#:time
Expand Down Expand Up @@ -61,12 +63,17 @@ While the result will always contain microseconds, some implementations may retu
(cl:* 1000000 (cl:- end start))
cl:internal-time-units-per-second)))))

(declare sleep (Integer -> Unit))
(declare sleep ((math:Rational :a) => :a -> Unit))
Izaakwltn marked this conversation as resolved.
Show resolved Hide resolved
(define (sleep n)
"Sleep for `n` seconds."
(lisp Unit (n)
(cl:sleep n)
Unit)))
"Sleep for `n` seconds, where `n` can be of any type with an instance of `Rational`.

Sleep uses type class `Rational`'s `best-approx` instead of `Real`'s `real-approx` because it handles the approximation without arbitrary precision. The only `Real` type excluded by this decision is `CReal`."
(if (math:negative? n)
(error "sleep must be a nonnegative number.")
(let ((frac (math:best-approx n)))
(lisp Unit (frac)
(cl:sleep frac)
Unit)))))

;;;
;;; Pofiling
Expand Down
Loading