-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ [thunk] Contextual has a thunk context
Problem: - Contextual does not have a context for thunks Solution: - Add a trivial context for thunks
- Loading branch information
Showing
9 changed files
with
133 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
(in-package :cl-user) | ||
|
||
(defpackage :contextual-thunk | ||
(:use :cl :contextual) | ||
(:export #:thunk #:thunk-wrap #:thunk-unwrap #:make-thunk-context)) | ||
|
||
(in-package :contextual-thunk) | ||
|
||
(defmacro thunk (&rest exprs) | ||
`(lambda () ,@exprs)) | ||
|
||
(defun thunk-wrap (x) | ||
(thunk x)) | ||
|
||
(defun thunk-unwrap (thunk) | ||
(funcall thunk)) | ||
|
||
(defun make-thunk-context () | ||
(make-instance 'trivial-operators | ||
:wrap #'thunk-wrap | ||
:unwrap #'thunk-unwrap)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
(in-package :cl-user) | ||
|
||
(defpackage :contextual-thunk-test | ||
(:use :cl :5am :contextual :contextual-thunk) | ||
(:export #:run-all-tests!)) | ||
|
||
(in-package :contextual-thunk-test) | ||
|
||
|
||
(def-suite thunk) | ||
|
||
(defun run-all-tests! () | ||
(run! 'thunk)) | ||
|
||
(in-suite thunk) | ||
|
||
(test thunk-construction | ||
(let ((mx (thunk (+ 1 2)))) | ||
(is-true (functionp mx)) | ||
(is (= 3 (funcall mx))))) | ||
|
||
(test fmap | ||
(let ((context (make-thunk-context))) | ||
(is (equalp "x" | ||
(ctx-run context | ||
(unwrap (fmap #'symbol-name (thunk 'x)))))))) | ||
(test pure/mreturn/wrap | ||
(let ((context (make-thunk-context))) | ||
(is (eq 'x (funcall (ctx-run context (pure 'x))))) | ||
(is (eq 'x (funcall (ctx-run context (mreturn 'x))))) | ||
(is (eq 'x (funcall (ctx-run context (wrap 'x))))))) | ||
|
||
(test fapply | ||
(let ((context (make-thunk-context))) | ||
(is (equal "X" (funcall (ctx-run context (fapply (pure #'symbol-name) (thunk 'x)))))))) | ||
|
||
(test flatmap | ||
(let ((context (make-thunk-context))) | ||
(is (equal "X" (funcall (ctx-run context (flatmap (lambda (x) (thunk (symbol-name x))) (pure 'x)))))))) | ||
|
||
(test flatten/unwrap | ||
(let ((context (make-thunk-context))) | ||
(is (eq 'x (funcall (ctx-run context (flatten (thunk (thunk 'x))))))) | ||
(is (eq 'x (funcall (ctx-run context (unwrap (thunk (thunk 'x))))))))) | ||
|
||
(test binding-syntax | ||
(let ((context (make-thunk-context))) | ||
(is (= 3 | ||
(funcall | ||
(ctx-run context | ||
(let-mon ((x (thunk 1)) | ||
(y (thunk 2))) | ||
(mreturn (+ x y))))))) | ||
(is (= 4 | ||
(funcall | ||
(ctx-run context | ||
(let*-mon ((x (thunk 1)) | ||
(y (thunk (+ x 2)))) | ||
(mreturn (+ x y))))))) | ||
(is (= 3 | ||
(funcall | ||
(ctx-run context | ||
(let-app ((x (thunk 1)) | ||
(y (thunk 2))) | ||
(+ x y)))))) | ||
(is (= 3 | ||
(funcall | ||
(ctx-run context | ||
(flatten | ||
(let-fun ((x (thunk 1)) | ||
(y (thunk 2))) | ||
(+ x y))))))) | ||
(is (= 4 | ||
(funcall | ||
(ctx-run context | ||
(flatten | ||
(let*-fun ((x (thunk 1)) | ||
(y (thunk (+ x 2)))) | ||
(+ x y))))))))) |