├── run-circleci.lisp ├── .circleci └── config.yml ├── auto-restart.asd ├── README.md ├── auto-restart.lisp ├── test-auto-restart.lisp └── LICENSE /run-circleci.lisp: -------------------------------------------------------------------------------- 1 | (load "~/quicklisp/setup.lisp") 2 | 3 | (push #P "./" asdf:*central-registry*) 4 | 5 | (ql:quickload :auto-restart/tests) 6 | 7 | (unless (fiveam:run-all-tests) 8 | (uiop:quit 1)) 9 | 10 | (uiop:quit 0) 11 | -------------------------------------------------------------------------------- /.circleci/config.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | jobs: 3 | build: 4 | docker: 5 | - image: cimg/base:2021.04 6 | steps: 7 | - checkout 8 | - run: 9 | name: Install SBCL 10 | command: sudo apt-get update && sudo apt-get install -y sbcl 11 | - run: 12 | name: Install quicklisp 13 | command: | 14 | curl -O https://beta.quicklisp.org/quicklisp.lisp 15 | sbcl --load quicklisp.lisp --eval '(quicklisp-quickstart:install)' 16 | - run: 17 | name: Run tests 18 | command: sbcl --script run-circleci.lisp 19 | -------------------------------------------------------------------------------- /auto-restart.asd: -------------------------------------------------------------------------------- 1 | ;;;; Copyright 2018-Present Modern Interpreters Inc. 2 | ;;;; 3 | ;;;; This Source Code Form is subject to the terms of the Mozilla Public 4 | ;;;; License, v. 2.0. If a copy of the MPL was not distributed with this 5 | ;;;; file, You can obtain one at https://mozilla.org/MPL/2.0/. 6 | 7 | (defsystem :auto-restart 8 | :author "Arnold Noronha " 9 | :license "Apache License, Version 2.0" 10 | :version "0.0.1" 11 | :description "automatically generate restart-cases for the most common use cases, and also use the restart for automatic retries " 12 | :serial t 13 | :depends-on (:iterate) 14 | :components ((:file "auto-restart")) 15 | :in-order-to ((test-op (test-op :auto-restart/tests)))) 16 | 17 | (defsystem :auto-restart/tests 18 | :serial t 19 | :depends-on (:auto-restart 20 | :fiveam) 21 | :components ((:file "test-auto-restart")) 22 | :perform (test-op (o c) 23 | (unless 24 | (symbol-call '#:fiveam '#:run! 25 | :test-auto-restart) 26 | (error "Some tests were failed!")))) 27 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # AUTO-RESTART 2 | 3 | [![tdrhq](https://circleci.com/gh/tdrhq/auto-restart.svg?style=shield)](https://app.circleci.com/pipelines/github/tdrhq/auto-restart?branch=main) 4 | 5 | Auto-restart makes it easy to create restarts for the most common 6 | situation of just retrying a function. To summarize, this is how 7 | you'll use it: 8 | 9 | ```lisp 10 | (with-auto-restart () 11 | (defun foo(arg1 &key other-options) 12 | (... do-stuff ...))) 13 | ``` 14 | 15 | If something fails inside foo, you'll be presented with a restart to 16 | RETRY-FOO. Once you've pushed this to production you might that the 17 | function FOO is flaky, possibly because it hits the network or some 18 | other unreliable resource. In that case, you can automate calling the 19 | restart like so: 20 | 21 | ```lisp 22 | (with-auto-restart (:retries 2 :sleep 1) 23 | (defun foo(arg1 &key other-options) 24 | (... do-stuff ...))) 25 | ``` 26 | 27 | This is really it, but you might have some questions as to why the API 28 | is as it is. Let's build up the reasoning for this. 29 | 30 | ## Motivation 31 | 32 | So say you write a complex, slow running function FOO: 33 | 34 | ```lisp 35 | (defun foo (...args ...) 36 | (... do-stuff ...)) 37 | ``` 38 | 39 | It's part of a slow job, say a crawler, so if the function FOO fails, 40 | you don't want to restart the job from scratch. Instead, you probably 41 | just want to restart the FOO function. You might consider doing 42 | something like this: 43 | 44 | ```lisp 45 | (defun foo (...args...) 46 | (labels ((actual-foo () 47 | (restart-case 48 | (...do-stuff...) 49 | (retry-foo () 50 | (actual-foo))))) 51 | (actual-foo) 52 | ``` 53 | 54 | Now, when an error happens you'll get a restart to RETRY-FOO. But 55 | there's a catch! If you make changes to `(...do stuff...)`, those 56 | changes won't show up even if you RETRY-FOO. 57 | 58 | So we'll do something like this instead: 59 | 60 | ```lisp 61 | (defun foo (...args...) 62 | (restart-case 63 | (...do-stuff...) 64 | (retry-foo () 65 | (foo ...args...)))) 66 | ``` 67 | 68 | This version works great. Applying `...args...` on the last line needs 69 | to be done carefully. You need to account for `&optional`, `&key` 70 | etc. Also, any changes to the args needs to be correctly kept in sync 71 | on the last line. So it's error prone. 72 | 73 | `with-auto-restart` just does this for you automatically. 74 | 75 | ## Retries 76 | 77 | In the process of developing, you'll end up testing that the restart 78 | works correctly. (Restarts need to be verified too! It's just code 79 | after all! A restart might fail, if for example some global state has 80 | changed beforethe condition was signaled). 81 | 82 | But you might find that the `FOO` function keeps failing for 83 | legitimate reasons. Perhaps it's hitting the network, or some other 84 | resource that's not always available. Using `:retries` makes it 85 | trivial to update the existing function to automatically call the 86 | retry for you multiple times. 87 | 88 | ## Future work 89 | 90 | One thing I would like to do, but I don't know if this is possible, is 91 | to be able to enter the debugger, but if no action was chosen with a 92 | set amount of time, then automatically pick a restart. 93 | 94 | The lambda-list parsing is also very primitive. If you have some 95 | complex lambda-list keywords, we may or may not get it right. Do look 96 | at the macroexpansion to make sure it's doing the right thing. We 97 | should handle `&optional`, `&key` and `&rest` at a minimum. 98 | 99 | 100 | ## Authors 101 | 102 | Built by Arnold Noronha 103 | 104 | ## License 105 | 106 | Licensed under the Mozilla Public License, v2. 107 | -------------------------------------------------------------------------------- /auto-restart.lisp: -------------------------------------------------------------------------------- 1 | ;;;; Copyright 2018-Present Modern Interpreters Inc. 2 | ;;;; 3 | ;;;; This Source Code Form is subject to the terms of the Mozilla Public 4 | ;;;; License, v. 2.0. If a copy of the MPL was not distributed with this 5 | ;;;; file, You can obtain one at https://mozilla.org/MPL/2.0/. 6 | 7 | (defpackage :auto-restart 8 | (:use #:cl 9 | #:iterate) 10 | (:export 11 | #:with-auto-restart 12 | #:*global-enable-auto-retries-p* 13 | #:exponential-backoff)) 14 | (in-package :auto-restart) 15 | 16 | 17 | (define-condition restart-already-defined (error) 18 | ((restart-name :initarg :restart-name)) 19 | (:documentation "When calling with-auto-restart, we expect the 20 | restart to be defined inside the body, not before it.")) 21 | 22 | (defvar *frame* nil) 23 | 24 | (defvar *global-enable-auto-retries-p* t 25 | "Globally enable or disable automatic retries. Useful for unit tests.") 26 | 27 | (defun exponential-backoff (base) 28 | `(lambda (attempt) 29 | (expt ,base attempt))) 30 | 31 | (defstruct frame 32 | attempt 33 | error-handler 34 | restart-handler) 35 | 36 | (defun %is-error-of-type (e error-classes) 37 | (loop for class in error-classes 38 | if (typep e class) 39 | return t 40 | finally 41 | (return nil))) 42 | 43 | (defun %add-noise (time &key noise) 44 | (* 45 | time 46 | (+ 1 (- (random (* 2 noise)) noise)))) 47 | 48 | (defmacro with-auto-restart ((&key (retries 0) 49 | (sleep (exponential-backoff 2)) 50 | (sleep-noise 0.1) 51 | (attempt (gensym "attempt")) 52 | (restart-name) 53 | (auto-restartable-errors ''(error))) 54 | &body body) 55 | "Enable auto-restarts for a DEFUN. 56 | 57 | ATTEMPT will be a variable name that will be bound to the current 58 | attempt. Attempts will be 1-indexed (1, 2, 3 ... ). 59 | 60 | AUTO-RESTARTABLE-ERRORS is evaluated to get a list of error classes 61 | for which we're allowed to auto-restart. It defaults to `'(ERROR)`. 62 | " 63 | (assert (= 1 (length body))) 64 | 65 | (let* ((body (car body)) 66 | (sleep (cond 67 | ((numberp sleep) 68 | `(lambda (attempt) 69 | (declare (ignore attempt)) 70 | ,sleep)) 71 | (t 72 | sleep))) 73 | (sleep-var (gensym "sleep")) 74 | (retries-var (gensym "retries")) 75 | (args-pos (position-if #'listp body)) 76 | (before-args (subseq body 0 (1+ args-pos))) 77 | (fn-name (cadr before-args)) 78 | (fn-args (elt body args-pos )) 79 | (var-names (loop for var in fn-args 80 | if (listp var) 81 | collect (car var) 82 | else 83 | collect var)) 84 | (decls-onwards (subseq body (1+ args-pos))) 85 | (restart-name (or 86 | restart-name 87 | (intern (format nil "RETRY-~a" fn-name) *package*)))) 88 | 89 | ;; quick validation 90 | (loop for arg in var-names 91 | do 92 | (assert (symbolp arg))) 93 | (multiple-value-bind (body decls doc) 94 | (uiop:parse-body decls-onwards :documentation t) 95 | `(let ((,sleep-var ,sleep) 96 | (,retries-var ,retries)) 97 | (declare (ignorable ,retries-var)) 98 | (,@before-args 99 | ,@(when doc (list doc)) 100 | ,@decls 101 | (flet ((top-level-body () 102 | (flet ((body (,attempt) 103 | (declare (ignorable ,attempt)) 104 | ,@body)) 105 | (let ((attempt (frame-attempt *frame*))) 106 | (setf (frame-error-handler *frame*) 107 | (lambda (e) 108 | (cond 109 | ((and *global-enable-auto-retries-p* (< attempt ,retries) 110 | (%is-error-of-type e ',(eval auto-restartable-errors))) 111 | (let ((sleep-time (%add-noise (funcall ,sleep-var attempt) 112 | :noise ,sleep-noise))) 113 | (unless (= 0 sleep-time) 114 | (sleep sleep-time))) 115 | (invoke-restart ',restart-name))))) 116 | (setf (frame-restart-handler *frame*) 117 | (lambda () 118 | (apply #',fn-name ,@ (fix-args-for-funcall var-names)))) 119 | ;; If we make a call to another auto-restart 120 | ;; it should not see the same frame 121 | (let ((*frame* nil)) 122 | (body attempt)))))) 123 | (cond 124 | ((null *frame*) 125 | (let* ((frame (make-frame 126 | :attempt 1 127 | :error-handler nil 128 | :restart-handler (lambda () 129 | ;; The first time we call we just call the function 130 | (top-level-body)))) 131 | (*frame* frame)) 132 | (block success 133 | (loop 134 | (restart-case 135 | (handler-bind ((error (lambda (e) 136 | (funcall (frame-error-handler frame) e)))) 137 | (return-from success (funcall (frame-restart-handler frame)))) 138 | (,restart-name () 139 | ;; Retry in our loop 140 | (values))))))) 141 | (t 142 | (incf (frame-attempt *frame*)) 143 | (top-level-body))))))))) 144 | 145 | (defun fix-args-for-funcall (var-names) 146 | (let ((state :default)) 147 | (iter (for var in (append var-names 148 | ;; fake ending point 149 | '(&rest nil) )) 150 | (cond 151 | ((eql :end state) 152 | #| do nothing |#) 153 | ((eql '&optional var) 154 | (setf state :optional)) 155 | ((eql '&key var) 156 | (setf state :key)) 157 | ((eql '&rest var) 158 | (setf state :rest)) 159 | ((eql #\& (elt (string var) 0)) 160 | (error "Unsupported lambda-list specifier: ~a" var)) 161 | ((not (symbolp var)) 162 | ;; TODO(arnold): this isn't hard to handle if you do need it. 163 | (error "Unsupported variable name: ~a, probably because of a 164 | keyword arg?" var)) 165 | (t 166 | (case state 167 | (:key 168 | (appending `(,(intern (string var) "KEYWORD") 169 | ,var))) 170 | (:rest 171 | ;; this one is interesting, we can stop looking at anything 172 | ;; that comes after this. 173 | (collect var) 174 | (setf state :end)) 175 | (otherwise 176 | (collect var)))))))) 177 | 178 | (defun call-with-auto-restart (restart-name fn) 179 | (when (find-restart restart-name) 180 | (error 'restart-already-defined :restart-name restart-name)) 181 | (funcall fn :attempt 0)) 182 | -------------------------------------------------------------------------------- /test-auto-restart.lisp: -------------------------------------------------------------------------------- 1 | ;;;; Copyright 2018-Present Modern Interpreters Inc. 2 | ;;;; 3 | ;;;; This Source Code Form is subject to the terms of the Mozilla Public 4 | ;;;; License, v. 2.0. If a copy of the MPL was not distributed with this 5 | ;;;; file, You can obtain one at https://mozilla.org/MPL/2.0/. 6 | 7 | (defpackage :test-auto-restart 8 | (:use #:cl 9 | #:fiveam) 10 | (:import-from #:auto-restart 11 | #:%add-noise 12 | #:exponential-backoff 13 | #:*global-enable-auto-retries-p* 14 | #:restart-already-defined 15 | #:with-auto-restart)) 16 | (in-package :test-auto-restart) 17 | 18 | 19 | (fiveam:def-suite* :test-auto-restart) 20 | 21 | (test simple-function 22 | (with-auto-restart () 23 | (defun foo () 24 | 2)) 25 | (is (equal 2 (foo)))) 26 | 27 | (test function-has-restart-defined 28 | (with-auto-restart () 29 | (defun foo () 30 | (find-restart 'retry-foo))) 31 | 32 | (is-true (foo))) 33 | 34 | (test calling-the-restart-works 35 | (let ((res nil)) 36 | (with-auto-restart () 37 | (defun foo () 38 | (cond 39 | ((not (>= (length res) 3)) 40 | (push t res) 41 | (invoke-restart 'retry-foo)) 42 | (t 43 | res))))) 44 | (is (equal '(t t t) 45 | (foo)))) 46 | 47 | 48 | (test function-with-args 49 | (let ((res nil)) 50 | (with-auto-restart () 51 | (defun foo (a b c) 52 | (cond 53 | ((not (>= (length res) 3)) 54 | (push (list a b c) res) 55 | (invoke-restart 'retry-foo)) 56 | (t 57 | res))))) 58 | (is (equal '((1 2 3) (1 2 3) (1 2 3)) 59 | (foo 1 2 3)))) 60 | 61 | 62 | (test method-with-args 63 | (let ((res nil)) 64 | (with-auto-restart () 65 | (defmethod dummy-method ((a symbol) b) 66 | (cond 67 | ((not (>= (length res) 3)) 68 | (push (list a b) res) 69 | (invoke-restart 'retry-dummy-method)) 70 | (t 71 | res))))) 72 | (is (equal '((:foo 1) (:foo 1) (:foo 1)) 73 | (dummy-method :foo 1)))) 74 | 75 | (test optional-args 76 | (let ((res nil)) 77 | (with-auto-restart () 78 | (defun foo (a &optional (b :bar)) 79 | (cond 80 | ((not (>= (length res) 3)) 81 | (push (list a b) res) 82 | (invoke-restart 'retry-foo)) 83 | (t 84 | res))))) 85 | (is (equal '((:foo :bar) (:foo :bar) (:foo :bar)) 86 | (foo :foo)))) 87 | 88 | (test keyword-arg 89 | (let ((res nil)) 90 | (with-auto-restart () 91 | (defun foo (a &key (b :bar)) 92 | (cond 93 | ((not (>= (length res) 3)) 94 | (push (list a b) res) 95 | (invoke-restart 'retry-foo)) 96 | (t 97 | res))))) 98 | (is (equal '((:foo :bar) (:foo :bar) (:foo :bar)) 99 | (foo :foo)))) 100 | 101 | (test keyword-arg-with-value 102 | (let ((res nil)) 103 | (with-auto-restart () 104 | (defun foo (a &key (b :bar)) 105 | (cond 106 | ((not (>= (length res) 3)) 107 | (push (list a b) res) 108 | (invoke-restart 'retry-foo)) 109 | (t 110 | res))))) 111 | (is (equal '((:foo 3) (:foo 3) (:foo 3)) 112 | (foo :foo :b 3)))) 113 | 114 | 115 | (test rest-args 116 | (let ((res nil)) 117 | (with-auto-restart () 118 | (defun foo (a &rest b) 119 | (cond 120 | ((not (>= (length res) 3)) 121 | (push (list a b) res) 122 | (invoke-restart 'retry-foo)) 123 | (t 124 | res))))) 125 | (is (equal '((:foo (1 2)) (:foo (1 2)) (:foo (1 2))) 126 | (foo :foo 1 2)))) 127 | 128 | (test rest-args-and-keywords 129 | (let ((res nil)) 130 | (with-auto-restart () 131 | (defun foo (a &rest b &key bar) 132 | (cond 133 | ((not (>= (length res) 3)) 134 | (push (list a b bar) res) 135 | (invoke-restart 'retry-foo)) 136 | (t 137 | res))))) 138 | (is (equal '((:foo (:bar 2) 2) (:foo (:bar 2) 2) (:foo (:bar 2) 2)) 139 | (foo :foo :bar 2)))) 140 | 141 | (test rest-args-and-keywords-with-default-keyword 142 | (let ((res nil)) 143 | (with-auto-restart () 144 | (defun foo (a &rest b &key (bar 10)) 145 | (cond 146 | ((not (>= (length res) 3)) 147 | (push (list a b bar) res) 148 | (invoke-restart 'retry-foo)) 149 | (t 150 | res))))) 151 | (is (equal '((:foo nil 10) (:foo nil 10) (:foo nil 10)) 152 | (foo :foo)))) 153 | 154 | (define-condition test-error (error) 155 | ()) 156 | 157 | (test actually-do-automatic-retries 158 | (let ((res nil)) 159 | (with-auto-restart (:retries 3 :sleep 0.1) 160 | (defun foo () 161 | "dfdf" 162 | (declare (inline)) 163 | (cond 164 | ((not (>= (length res) 10)) 165 | (push t res) 166 | (error 'test-error)) 167 | (t 168 | res)))) 169 | 170 | (signals test-error 171 | (foo)) 172 | (is (equal '(t t t) 173 | res)))) 174 | 175 | (test but-we-dont-retry-if-the-global-flag-is-off 176 | (let ((res nil) 177 | (*global-enable-auto-retries-p* nil)) 178 | (with-auto-restart (:retries 3) 179 | (defun foo () 180 | "dfdf" 181 | (declare (inline)) 182 | (cond 183 | ((not (>= (length res) 10)) 184 | (push t res) 185 | (error 'test-error)) 186 | (t 187 | res)))) 188 | 189 | (signals test-error 190 | (foo)) 191 | (is (equal '(t) 192 | res)))) 193 | 194 | (test actually-do-automatic-retries-with-fake-sleep-happy-path 195 | (let ((res nil)) 196 | (with-auto-restart (:retries 3 :sleep 0) 197 | (defun foo () 198 | "dfdf" 199 | (declare (inline)) 200 | (cond 201 | ((not (>= (length res) 10)) 202 | (push t res) 203 | (error 'test-error)) 204 | (t 205 | res)))) 206 | 207 | (signals test-error 208 | (foo)) 209 | (is (equal '(t t t) 210 | res)))) 211 | 212 | (test actually-do-automatic-retries-with-fake-sleep-fn-happy-path 213 | (let ((res nil)) 214 | (with-auto-restart (:retries 3 :sleep (lambda (attempt) 0)) 215 | (defun foo () 216 | "dfdf" 217 | (declare (inline)) 218 | (cond 219 | ((not (>= (length res) 10)) 220 | (push t res) 221 | (error 'test-error)) 222 | (t 223 | res)))) 224 | 225 | (signals test-error 226 | (foo)) 227 | (is (equal '(t t t) 228 | res)))) 229 | 230 | 231 | (test doc-and-declarations 232 | (with-auto-restart () 233 | (defun foo (x) 234 | "hello world" 235 | (declare (optimize debug) 236 | ;; this next declare expr ensures this is 237 | ;; part of a function 238 | (inline)) 239 | t)) 240 | (is (equal "hello world" (documentation #'foo 'function)))) 241 | 242 | (test maybe-invoke-restart 243 | (let ((count 0) 244 | (seen nil)) 245 | (with-auto-restart (:attempt attempt) 246 | (defun foo () 247 | (push attempt seen) 248 | (incf count) 249 | (assert (< count 10)) 250 | (when (< attempt 3) 251 | (invoke-restart 'retry-foo)))) 252 | (foo) 253 | (is (eql 3 count)) 254 | (is (equal (list 3 2 1) seen)))) 255 | 256 | (define-condition my-error (error) 257 | ()) 258 | 259 | (with-auto-restart (:retries 1000 :sleep 0) 260 | (defun crashes-after-many-attempts () 261 | (error 'my-error))) 262 | 263 | (test tail-call-optimized 264 | (signals my-error 265 | (crashes-after-many-attempts))) 266 | 267 | 268 | (with-auto-restart (:attempt attempt) 269 | (defun nested-inner () 270 | (unless (= attempt 1) 271 | (error "inner function didn't reset attempts, got ~a" attempt)))) 272 | 273 | (with-auto-restart (:attempt attempt) 274 | (defun nested-outer () 275 | (assert (= attempt 1)) 276 | (nested-inner))) 277 | 278 | (test nested-calls-maintains-attempts 279 | (finishes 280 | (nested-outer))) 281 | 282 | (with-auto-restart () ;; this auto-restart is important for the test 283 | ;; we're trying to test. 284 | (defun nested-inner-with (counter) 285 | (unless (= counter 3) 286 | (error "inner function still failing, got ~a" counter)))) 287 | 288 | (with-auto-restart (:retries 3 :attempt attempt :sleep 0) 289 | (defun nested-outer-with-inner-failures () 290 | (nested-inner-with attempt) 291 | attempt)) 292 | 293 | (test nested-calls-maintains-attempts 294 | (is (eql 295 | 3 296 | (nested-outer-with-inner-failures)))) 297 | 298 | (defvar *nested-inner-2-counter*) 299 | 300 | (with-auto-restart (:retries 5 :sleep 0) ;; this auto-restart is important for the test 301 | ;; we're trying to test. 302 | (defun nested-inner-with-2 (counter) 303 | (incf *nested-inner-2-counter*) 304 | (unless (= counter 3) 305 | (error "inner function still failing, got ~a" counter)))) 306 | 307 | (with-auto-restart (:retries 3 :attempt attempt :sleep 0) 308 | (defun nested-outer-with-inner-failures-2 () 309 | (nested-inner-with-2 attempt) 310 | attempt)) 311 | 312 | (test nested-calls-maintains-attempts-2 313 | (let ((*nested-inner-2-counter* 0)) 314 | (is (eql 315 | 3 316 | (nested-outer-with-inner-failures-2))) 317 | (is (eql 318 | 11 *nested-inner-2-counter*)))) 319 | 320 | (define-condition dummy-error (error) 321 | ()) 322 | 323 | (with-auto-restart (:retries 3 :sleep 0 :auto-restartable-errors '(dummy-error)) 324 | (defun call-with-crash (fn) 325 | (funcall fn))) 326 | 327 | (test specify-auto-restartable-errors 328 | (let ((count 0)) 329 | (signals dummy-error 330 | (call-with-crash 331 | (lambda () 332 | (incf count) 333 | (error 'dummy-error)))) 334 | (is (eql 3 count)))) 335 | 336 | (test ignore-errors-that-are-not-specified-as-auto-restartable 337 | (let ((count 0)) 338 | (signals error 339 | (call-with-crash 340 | (lambda () 341 | (incf count) 342 | (error 'error)))) 343 | (is (eql 1 count)))) 344 | 345 | (auto-restart:with-auto-restart () 346 | (defun %%%can-use-declarations%%% () 347 | (declare (optimize (debug 3))) 348 | :foo)) 349 | 350 | (test can-use-declarations 351 | (is (eql :foo (%%%can-use-declarations%%%)))) 352 | 353 | (test exponential-backoff-happy-path 354 | (let ((fn (eval (exponential-backoff 0.01)))) 355 | (finishes (funcall fn 1)))) 356 | 357 | (test add-noise 358 | (dotimes (i 10) 359 | (let ((result (%add-noise 1000 :noise 0.1))) 360 | (is (<= result 1100)) 361 | (is (>= result 900))))) 362 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Mozilla Public License Version 2.0 2 | ================================== 3 | 4 | 1. Definitions 5 | -------------- 6 | 7 | 1.1. "Contributor" 8 | means each individual or legal entity that creates, contributes to 9 | the creation of, or owns Covered Software. 10 | 11 | 1.2. "Contributor Version" 12 | means the combination of the Contributions of others (if any) used 13 | by a Contributor and that particular Contributor's Contribution. 14 | 15 | 1.3. "Contribution" 16 | means Covered Software of a particular Contributor. 17 | 18 | 1.4. "Covered Software" 19 | means Source Code Form to which the initial Contributor has attached 20 | the notice in Exhibit A, the Executable Form of such Source Code 21 | Form, and Modifications of such Source Code Form, in each case 22 | including portions thereof. 23 | 24 | 1.5. "Incompatible With Secondary Licenses" 25 | means 26 | 27 | (a) that the initial Contributor has attached the notice described 28 | in Exhibit B to the Covered Software; or 29 | 30 | (b) that the Covered Software was made available under the terms of 31 | version 1.1 or earlier of the License, but not also under the 32 | terms of a Secondary License. 33 | 34 | 1.6. "Executable Form" 35 | means any form of the work other than Source Code Form. 36 | 37 | 1.7. "Larger Work" 38 | means a work that combines Covered Software with other material, in 39 | a separate file or files, that is not Covered Software. 40 | 41 | 1.8. "License" 42 | means this document. 43 | 44 | 1.9. "Licensable" 45 | means having the right to grant, to the maximum extent possible, 46 | whether at the time of the initial grant or subsequently, any and 47 | all of the rights conveyed by this License. 48 | 49 | 1.10. "Modifications" 50 | means any of the following: 51 | 52 | (a) any file in Source Code Form that results from an addition to, 53 | deletion from, or modification of the contents of Covered 54 | Software; or 55 | 56 | (b) any new file in Source Code Form that contains any Covered 57 | Software. 58 | 59 | 1.11. "Patent Claims" of a Contributor 60 | means any patent claim(s), including without limitation, method, 61 | process, and apparatus claims, in any patent Licensable by such 62 | Contributor that would be infringed, but for the grant of the 63 | License, by the making, using, selling, offering for sale, having 64 | made, import, or transfer of either its Contributions or its 65 | Contributor Version. 66 | 67 | 1.12. "Secondary License" 68 | means either the GNU General Public License, Version 2.0, the GNU 69 | Lesser General Public License, Version 2.1, the GNU Affero General 70 | Public License, Version 3.0, or any later versions of those 71 | licenses. 72 | 73 | 1.13. "Source Code Form" 74 | means the form of the work preferred for making modifications. 75 | 76 | 1.14. "You" (or "Your") 77 | means an individual or a legal entity exercising rights under this 78 | License. For legal entities, "You" includes any entity that 79 | controls, is controlled by, or is under common control with You. For 80 | purposes of this definition, "control" means (a) the power, direct 81 | or indirect, to cause the direction or management of such entity, 82 | whether by contract or otherwise, or (b) ownership of more than 83 | fifty percent (50%) of the outstanding shares or beneficial 84 | ownership of such entity. 85 | 86 | 2. License Grants and Conditions 87 | -------------------------------- 88 | 89 | 2.1. Grants 90 | 91 | Each Contributor hereby grants You a world-wide, royalty-free, 92 | non-exclusive license: 93 | 94 | (a) under intellectual property rights (other than patent or trademark) 95 | Licensable by such Contributor to use, reproduce, make available, 96 | modify, display, perform, distribute, and otherwise exploit its 97 | Contributions, either on an unmodified basis, with Modifications, or 98 | as part of a Larger Work; and 99 | 100 | (b) under Patent Claims of such Contributor to make, use, sell, offer 101 | for sale, have made, import, and otherwise transfer either its 102 | Contributions or its Contributor Version. 103 | 104 | 2.2. Effective Date 105 | 106 | The licenses granted in Section 2.1 with respect to any Contribution 107 | become effective for each Contribution on the date the Contributor first 108 | distributes such Contribution. 109 | 110 | 2.3. Limitations on Grant Scope 111 | 112 | The licenses granted in this Section 2 are the only rights granted under 113 | this License. No additional rights or licenses will be implied from the 114 | distribution or licensing of Covered Software under this License. 115 | Notwithstanding Section 2.1(b) above, no patent license is granted by a 116 | Contributor: 117 | 118 | (a) for any code that a Contributor has removed from Covered Software; 119 | or 120 | 121 | (b) for infringements caused by: (i) Your and any other third party's 122 | modifications of Covered Software, or (ii) the combination of its 123 | Contributions with other software (except as part of its Contributor 124 | Version); or 125 | 126 | (c) under Patent Claims infringed by Covered Software in the absence of 127 | its Contributions. 128 | 129 | This License does not grant any rights in the trademarks, service marks, 130 | or logos of any Contributor (except as may be necessary to comply with 131 | the notice requirements in Section 3.4). 132 | 133 | 2.4. Subsequent Licenses 134 | 135 | No Contributor makes additional grants as a result of Your choice to 136 | distribute the Covered Software under a subsequent version of this 137 | License (see Section 10.2) or under the terms of a Secondary License (if 138 | permitted under the terms of Section 3.3). 139 | 140 | 2.5. Representation 141 | 142 | Each Contributor represents that the Contributor believes its 143 | Contributions are its original creation(s) or it has sufficient rights 144 | to grant the rights to its Contributions conveyed by this License. 145 | 146 | 2.6. Fair Use 147 | 148 | This License is not intended to limit any rights You have under 149 | applicable copyright doctrines of fair use, fair dealing, or other 150 | equivalents. 151 | 152 | 2.7. Conditions 153 | 154 | Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted 155 | in Section 2.1. 156 | 157 | 3. Responsibilities 158 | ------------------- 159 | 160 | 3.1. Distribution of Source Form 161 | 162 | All distribution of Covered Software in Source Code Form, including any 163 | Modifications that You create or to which You contribute, must be under 164 | the terms of this License. You must inform recipients that the Source 165 | Code Form of the Covered Software is governed by the terms of this 166 | License, and how they can obtain a copy of this License. You may not 167 | attempt to alter or restrict the recipients' rights in the Source Code 168 | Form. 169 | 170 | 3.2. Distribution of Executable Form 171 | 172 | If You distribute Covered Software in Executable Form then: 173 | 174 | (a) such Covered Software must also be made available in Source Code 175 | Form, as described in Section 3.1, and You must inform recipients of 176 | the Executable Form how they can obtain a copy of such Source Code 177 | Form by reasonable means in a timely manner, at a charge no more 178 | than the cost of distribution to the recipient; and 179 | 180 | (b) You may distribute such Executable Form under the terms of this 181 | License, or sublicense it under different terms, provided that the 182 | license for the Executable Form does not attempt to limit or alter 183 | the recipients' rights in the Source Code Form under this License. 184 | 185 | 3.3. Distribution of a Larger Work 186 | 187 | You may create and distribute a Larger Work under terms of Your choice, 188 | provided that You also comply with the requirements of this License for 189 | the Covered Software. If the Larger Work is a combination of Covered 190 | Software with a work governed by one or more Secondary Licenses, and the 191 | Covered Software is not Incompatible With Secondary Licenses, this 192 | License permits You to additionally distribute such Covered Software 193 | under the terms of such Secondary License(s), so that the recipient of 194 | the Larger Work may, at their option, further distribute the Covered 195 | Software under the terms of either this License or such Secondary 196 | License(s). 197 | 198 | 3.4. Notices 199 | 200 | You may not remove or alter the substance of any license notices 201 | (including copyright notices, patent notices, disclaimers of warranty, 202 | or limitations of liability) contained within the Source Code Form of 203 | the Covered Software, except that You may alter any license notices to 204 | the extent required to remedy known factual inaccuracies. 205 | 206 | 3.5. Application of Additional Terms 207 | 208 | You may choose to offer, and to charge a fee for, warranty, support, 209 | indemnity or liability obligations to one or more recipients of Covered 210 | Software. However, You may do so only on Your own behalf, and not on 211 | behalf of any Contributor. You must make it absolutely clear that any 212 | such warranty, support, indemnity, or liability obligation is offered by 213 | You alone, and You hereby agree to indemnify every Contributor for any 214 | liability incurred by such Contributor as a result of warranty, support, 215 | indemnity or liability terms You offer. You may include additional 216 | disclaimers of warranty and limitations of liability specific to any 217 | jurisdiction. 218 | 219 | 4. Inability to Comply Due to Statute or Regulation 220 | --------------------------------------------------- 221 | 222 | If it is impossible for You to comply with any of the terms of this 223 | License with respect to some or all of the Covered Software due to 224 | statute, judicial order, or regulation then You must: (a) comply with 225 | the terms of this License to the maximum extent possible; and (b) 226 | describe the limitations and the code they affect. Such description must 227 | be placed in a text file included with all distributions of the Covered 228 | Software under this License. Except to the extent prohibited by statute 229 | or regulation, such description must be sufficiently detailed for a 230 | recipient of ordinary skill to be able to understand it. 231 | 232 | 5. Termination 233 | -------------- 234 | 235 | 5.1. The rights granted under this License will terminate automatically 236 | if You fail to comply with any of its terms. However, if You become 237 | compliant, then the rights granted under this License from a particular 238 | Contributor are reinstated (a) provisionally, unless and until such 239 | Contributor explicitly and finally terminates Your grants, and (b) on an 240 | ongoing basis, if such Contributor fails to notify You of the 241 | non-compliance by some reasonable means prior to 60 days after You have 242 | come back into compliance. Moreover, Your grants from a particular 243 | Contributor are reinstated on an ongoing basis if such Contributor 244 | notifies You of the non-compliance by some reasonable means, this is the 245 | first time You have received notice of non-compliance with this License 246 | from such Contributor, and You become compliant prior to 30 days after 247 | Your receipt of the notice. 248 | 249 | 5.2. If You initiate litigation against any entity by asserting a patent 250 | infringement claim (excluding declaratory judgment actions, 251 | counter-claims, and cross-claims) alleging that a Contributor Version 252 | directly or indirectly infringes any patent, then the rights granted to 253 | You by any and all Contributors for the Covered Software under Section 254 | 2.1 of this License shall terminate. 255 | 256 | 5.3. In the event of termination under Sections 5.1 or 5.2 above, all 257 | end user license agreements (excluding distributors and resellers) which 258 | have been validly granted by You or Your distributors under this License 259 | prior to termination shall survive termination. 260 | 261 | ************************************************************************ 262 | * * 263 | * 6. Disclaimer of Warranty * 264 | * ------------------------- * 265 | * * 266 | * Covered Software is provided under this License on an "as is" * 267 | * basis, without warranty of any kind, either expressed, implied, or * 268 | * statutory, including, without limitation, warranties that the * 269 | * Covered Software is free of defects, merchantable, fit for a * 270 | * particular purpose or non-infringing. The entire risk as to the * 271 | * quality and performance of the Covered Software is with You. * 272 | * Should any Covered Software prove defective in any respect, You * 273 | * (not any Contributor) assume the cost of any necessary servicing, * 274 | * repair, or correction. This disclaimer of warranty constitutes an * 275 | * essential part of this License. No use of any Covered Software is * 276 | * authorized under this License except under this disclaimer. * 277 | * * 278 | ************************************************************************ 279 | 280 | ************************************************************************ 281 | * * 282 | * 7. Limitation of Liability * 283 | * -------------------------- * 284 | * * 285 | * Under no circumstances and under no legal theory, whether tort * 286 | * (including negligence), contract, or otherwise, shall any * 287 | * Contributor, or anyone who distributes Covered Software as * 288 | * permitted above, be liable to You for any direct, indirect, * 289 | * special, incidental, or consequential damages of any character * 290 | * including, without limitation, damages for lost profits, loss of * 291 | * goodwill, work stoppage, computer failure or malfunction, or any * 292 | * and all other commercial damages or losses, even if such party * 293 | * shall have been informed of the possibility of such damages. This * 294 | * limitation of liability shall not apply to liability for death or * 295 | * personal injury resulting from such party's negligence to the * 296 | * extent applicable law prohibits such limitation. Some * 297 | * jurisdictions do not allow the exclusion or limitation of * 298 | * incidental or consequential damages, so this exclusion and * 299 | * limitation may not apply to You. * 300 | * * 301 | ************************************************************************ 302 | 303 | 8. Litigation 304 | ------------- 305 | 306 | Any litigation relating to this License may be brought only in the 307 | courts of a jurisdiction where the defendant maintains its principal 308 | place of business and such litigation shall be governed by laws of that 309 | jurisdiction, without reference to its conflict-of-law provisions. 310 | Nothing in this Section shall prevent a party's ability to bring 311 | cross-claims or counter-claims. 312 | 313 | 9. Miscellaneous 314 | ---------------- 315 | 316 | This License represents the complete agreement concerning the subject 317 | matter hereof. If any provision of this License is held to be 318 | unenforceable, such provision shall be reformed only to the extent 319 | necessary to make it enforceable. Any law or regulation which provides 320 | that the language of a contract shall be construed against the drafter 321 | shall not be used to construe this License against a Contributor. 322 | 323 | 10. Versions of the License 324 | --------------------------- 325 | 326 | 10.1. New Versions 327 | 328 | Mozilla Foundation is the license steward. Except as provided in Section 329 | 10.3, no one other than the license steward has the right to modify or 330 | publish new versions of this License. Each version will be given a 331 | distinguishing version number. 332 | 333 | 10.2. Effect of New Versions 334 | 335 | You may distribute the Covered Software under the terms of the version 336 | of the License under which You originally received the Covered Software, 337 | or under the terms of any subsequent version published by the license 338 | steward. 339 | 340 | 10.3. Modified Versions 341 | 342 | If you create software not governed by this License, and you want to 343 | create a new license for such software, you may create and use a 344 | modified version of this License if you rename the license and remove 345 | any references to the name of the license steward (except to note that 346 | such modified license differs from this License). 347 | 348 | 10.4. Distributing Source Code Form that is Incompatible With Secondary 349 | Licenses 350 | 351 | If You choose to distribute Source Code Form that is Incompatible With 352 | Secondary Licenses under the terms of this version of the License, the 353 | notice described in Exhibit B of this License must be attached. 354 | 355 | Exhibit A - Source Code Form License Notice 356 | ------------------------------------------- 357 | 358 | This Source Code Form is subject to the terms of the Mozilla Public 359 | License, v. 2.0. If a copy of the MPL was not distributed with this 360 | file, You can obtain one at https://mozilla.org/MPL/2.0/. 361 | 362 | If it is not possible or desirable to put the notice in a particular 363 | file, then You may include the notice in a location (such as a LICENSE 364 | file in a relevant directory) where a recipient would be likely to look 365 | for such a notice. 366 | 367 | You may add additional accurate notices of copyright ownership. 368 | 369 | Exhibit B - "Incompatible With Secondary Licenses" Notice 370 | --------------------------------------------------------- 371 | 372 | This Source Code Form is "Incompatible With Secondary Licenses", as 373 | defined by the Mozilla Public License, v. 2.0. 374 | --------------------------------------------------------------------------------