-
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.
✨ [bare-function] Contexutal implements bare functions
Problem: - Contextual does not provide a context for the environment monad implemented with bare functions. Solution: - Add a context for bare functions
- Loading branch information
Showing
5 changed files
with
337 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
(in-package :cl-user) | ||
|
||
(defpackage :contextual-bare-function | ||
(:use :cl :binding-syntax-helpers :contextual :contextual-utility) | ||
(:export #:make-bare-function-context | ||
#:+bare-function+ | ||
#:bf-run | ||
#:bf-fmap | ||
#:bf-pure | ||
#:bf-fapply | ||
#:bf-mreturn | ||
#:bf-flatmap | ||
#:bf-flatten | ||
#:bf-ask | ||
#:bf-asks | ||
#:bf-local | ||
#:let*-fun/bf | ||
#:let-fun/bf | ||
#:let-app/bf | ||
#:let*-mond/bf | ||
#:let-mon/bf)) | ||
|
||
(in-package :contextual-bare-function) | ||
|
||
(eval-when (:load-toplevel :compile-toplevel) | ||
(defun bf-run (e mx) | ||
"Run a bare function context with the environment `E'." | ||
(declare (type function mx)) | ||
(funcall mx e)) | ||
|
||
(defun bf-fmap (f mx) | ||
(lambda (e) | ||
(funcall f (bf-run e mx)))) | ||
|
||
(defun bf-pure (x) | ||
(lambda (e) | ||
(declare (ignore e)) | ||
x)) | ||
|
||
(defun bf-mreturn (x) | ||
(lambda (e) | ||
(declare (ignore e)) | ||
x)) | ||
|
||
(defun bf-fapply (mf mx) | ||
(lambda (e) (funcall (bf-run e mf) (bf-run e mx)))) | ||
|
||
(defun bf-product (mx my) | ||
(lambda (e) | ||
(list (bf-run e mx) (bf-run e my)))) | ||
|
||
(defun bf-flatmap (f mx) | ||
(lambda (e) | ||
(bf-run e (funcall f (bf-run e mx))))) | ||
|
||
(defun bf-flatten (mmx) | ||
(lambda (e) | ||
(bf-run e (bf-run e mmx)))) | ||
|
||
(defun bf-ask () | ||
#'identity) | ||
|
||
(defun bf-asks (f) | ||
"Return the environment" | ||
(declare (type function f)) | ||
(lambda (e) | ||
(funcall f e))) | ||
|
||
(defun bf-local (f mx) | ||
"Run a bare function with a locally modified environemnt" | ||
(lambda (e) | ||
(bf-run (funcall f e) mx))) | ||
|
||
(defun make-bare-function-context () | ||
"Return a monadic context for bare functions." | ||
(make-instance 'monad-operators | ||
:fmap #'bf-fmap | ||
:fapply #'bf-fapply | ||
:product #'bf-product | ||
:mreturn #'bf-mreturn | ||
:flatmap #'bf-flatmap | ||
:flatten #'bf-flatten))) | ||
|
||
(defmacro let*-fun/bf ((&rest bindings) &body body) | ||
(make-sequential-functor-binding | ||
'let*-fun/bf | ||
:fmap 'bf-fmap | ||
:bindings bindings | ||
:body body)) | ||
|
||
(defmacro let-fun/bf ((&rest bindings) &body body) | ||
(make-parallel-functor-binding-ez | ||
'let-fun/bf | ||
:let-sequential 'let*-fun/bf | ||
:bindings bindings | ||
:body body)) | ||
|
||
(defmacro let-app/bf ((&rest bindings) &body body) | ||
(make-parallel-applicative-binding-ez | ||
'let-app/bf | ||
:fmap 'bf-fmap | ||
:fapply 'bf-fapply | ||
:bindings bindings | ||
:body body)) | ||
|
||
(defmacro progn-mon/bf (&body body) | ||
(make-monad-progn-ez | ||
'progn-mon/bf | ||
:flatmap 'bf-flatmap | ||
:body body)) | ||
|
||
(defmacro let*-mon/bf ((&rest bindings) &body body) | ||
(make-sequential-monad-binding-ez | ||
'let*-mon/bf | ||
:flatmap 'bf-flatmap | ||
:monad-progn 'progn-mon/bf | ||
:bindings bindings | ||
:body body)) | ||
|
||
(defmacro let-mon/bf ((&rest bindings) &body body) | ||
(make-parallel-monad-binding | ||
'let-mon/bf | ||
:flatmap 'bf-flatmap | ||
:sequential-let-name 'let*-mon/bf | ||
:monad-progn 'progn-mon/bf | ||
:bindings bindings | ||
:body body)) | ||
|
||
|
||
(define-constant +bare-function+ | ||
(make-bare-function-context)) |
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,92 @@ | ||
(in-package :cl-user) | ||
|
||
(defpackage :contextual-bare-function-test | ||
(:use :cl :5am :contextual :contextual-bare-function) | ||
(:export #:run-all-tests!)) | ||
|
||
(in-package :contextual-bare-function-test) | ||
|
||
(defun run-all-tests! () | ||
(run! 'bare-function)) | ||
|
||
(def-suite bare-function) | ||
|
||
(in-suite bare-function) | ||
|
||
(test specific-context-functions | ||
(is (eq 'x (bf-run 'e (bf-pure 'x)))) | ||
(is (eq 'x (bf-run 'e (bf-mreturn 'x)))) | ||
(is (equal "X" (bf-run 'e (bf-fmap #'symbol-name (bf-pure 'x))))) | ||
(is (equal "E" (bf-run 'e (bf-fmap #'symbol-name (bf-ask))))) | ||
(is (equal "E" (bf-run 'e (bf-fapply (bf-pure #'symbol-name) (bf-ask))))) | ||
(is (equal "EX" (bf-run 'e | ||
(bf-flatmap | ||
(lambda (x) | ||
(let-app/bf ((e (bf-ask))) | ||
(concatenate 'string (symbol-name e) (symbol-name x)))) | ||
(bf-pure 'x))))) | ||
(is (eq 'e (bf-run 'e | ||
(bf-flatten (bf-mreturn (bf-ask))))))) | ||
|
||
(test generic-context-functions | ||
(let ((context (make-bare-function-context))) | ||
(is (eq 'x (bf-run 'e (ctx-run context (pure 'x))))) | ||
(is (eq 'e (bf-run 'e (ctx-run context (bf-ask))))) | ||
(is (equal "X" (bf-run 'e (ctx-run context (fmap #'symbol-name (pure 'x)))))) | ||
(is (equal '(x e) (bf-run 'e | ||
(ctx-run context | ||
(product (pure 'x) (bf-ask)))))) | ||
(is (equal "X" (bf-run 'e | ||
(ctx-run context | ||
(flatmap (lambda (x) | ||
(lambda (e) | ||
(declare (ignore e)) | ||
(symbol-name x))) | ||
(pure 'x)))))) | ||
(is (eq 'x (bf-run 'e | ||
(ctx-run context | ||
(flatten (pure (pure 'x))))))) | ||
(is (equal "E" | ||
(bf-run 'e | ||
(ctx-run context | ||
(bf-asks #'symbol-name))))) | ||
(is (equal "E" | ||
(bf-run 'e | ||
(ctx-run context | ||
(bf-local | ||
#'symbol-name | ||
(bf-ask)))))))) | ||
|
||
|
||
(test binding-syntax | ||
(flet ((lookup (name) (lambda (e) (cdr (assoc name e))))) | ||
(is (= 3 | ||
(bf-run '((x . 1) (y . 2)) | ||
(ctx-run +bare-function+ | ||
(flatten | ||
(let-fun ((x (bf-asks (lookup 'x))) | ||
(y (bf-asks (lookup 'y)))) | ||
(+ x y))))))) | ||
(is (= 3 | ||
(bf-run '((x . 1) (y . 2)) | ||
(ctx-run +bare-function+ | ||
(let-app ((x (bf-asks (lookup 'x))) | ||
(y (bf-asks (lookup 'y)))) | ||
(+ x y)))))) | ||
|
||
(is (= 3 | ||
(bf-run '((x . 1) (y . 2)) | ||
(ctx-run +bare-function+ | ||
(let-mon ((x (bf-asks (lookup 'x))) | ||
(y (bf-asks (lookup 'y)))) | ||
(mreturn (+ x y))))))) | ||
|
||
(is (= 5 | ||
(bf-run '((x . 1) (y . 2)) | ||
(ctx-run +bare-function+ | ||
(let-mon ((x (bf-asks (lookup 'x)))) | ||
(bf-local (lambda (e) (cons `(x . ,(+ x 2)) e)) | ||
(ctx-run +bare-function+ | ||
(let-app ((x (bf-asks (lookup 'x))) | ||
(y (bf-asks (lookup 'y)))) | ||
(+ x y))))))))))) |