Skip to content

Commit

Permalink
WIP: Monad tests pass FFS!
Browse files Browse the repository at this point in the history
  • Loading branch information
drewc committed Aug 22, 2024
1 parent c604a66 commit 0f3a317
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 28 deletions.
14 changes: 7 additions & 7 deletions src/std/monad-test.ss
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
:std/error
:std/interactive
:srfi/13
:std/instance #;"instance"
:std/monad/interface #;"monad/interface"
:std/monad/identity #;"monad/identity"
:std/monad/list #;"monad/list"
:std/monad/state #;"monad/state"
:std/monad/syntax #;"monad/syntax"
:std/monad/error #;"monad/error"
:std/instance
:std/monad/interface
:std/monad/identity
:std/monad/list
:std/monad/state
:std/monad/syntax
:std/monad/error
(only-in :std/sugar hash try)
(only-in :gerbil/core error-object? with-catch))
(export monad-test)
Expand Down
35 changes: 20 additions & 15 deletions src/std/monad/README.org
Original file line number Diff line number Diff line change
Expand Up @@ -568,13 +568,13 @@ Even though, or perhaps because =:t= does "nothing" there is a simple

(instance (m Monad) (st stateT)
((return a)
(using (inner st.inner : Monad) (lambda (s) (inner.return [a . s]))))
(using (inner st.inner : Monad) (lambda (s) (inner.return [a . s]))))
((>>= ma f)
(using (inner st.inner : Monad)
(lambda (s)
(du inner
pair <- (ma s)
(with ((cons v s!) pair) ((f v) s!)))))))
(using (inner st.inner : Monad)
(lambda (s)
(du inner
pair <- (ma s)
(with ((cons v s!) pair) ((f v) s!)))))))

#+end_src

Expand Down Expand Up @@ -839,7 +839,13 @@ Making the handler is easy.
(du (inner et.inner :- Monad)
val <- exp
(if (me.error? val) (handler val) (inner.return val)))))


(instance Fail (et errorT) ((fail) (using (i et.inner : Fail) (i.fail))))
(instance Or (et errorT) ((or a b) (using (i et.inner : Or) (i.or a b))))
(instance Plus (et errorT) ((plus a b) (using (i et.inner : Plus) (i.plus a b))))
(instance Run (et errorT) ((run fn arg) (using (i et.inner : Run) (i.run fn arg))))
(instance Zero (et errorT) ((zero) (using (i et.inner : Zero) (i.zero))))

#+end_src

Testing it makes it clear.
Expand Down Expand Up @@ -1107,14 +1113,13 @@ This is after state in the train of thought so is defined here.
:std/error
:std/interactive
:srfi/13
; :std/instance
"instance"
"monad/interface"
"monad/identity"
"monad/list"
"monad/state"
"monad/syntax"
"monad/error"
:std/instance
:std/monad/interface
:std/monad/identity
:std/monad/list
:std/monad/state
:std/monad/syntax
:std/monad/error
(only-in :std/sugar hash try)
(only-in :gerbil/core error-object? with-catch))
(export monad-test)
Expand Down
6 changes: 6 additions & 0 deletions src/std/monad/error.ss
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@
val <- exp
(if (me.error? val) (handler val) (inner.return val)))))

(instance Fail (et errorT) ((fail) (using (i et.inner : Fail) (i.fail))))
(instance Or (et errorT) ((or a b) (using (i et.inner : Or) (i.or a b))))
(instance Plus (et errorT) ((plus a b) (using (i et.inner : Plus) (i.plus a b))))
(instance Run (et errorT) ((run fn arg) (using (i et.inner : Run) (i.run fn arg))))
(instance Zero (et errorT) ((zero) (using (i et.inner : Zero) (i.zero))))

(instance MonadState (et errorT)
((get) (du (inner et.inner :- MonadState) (inner.get)))
((put! s) (du (inner et.inner :- MonadState) (inner.put! s)))
Expand Down
12 changes: 6 additions & 6 deletions src/std/monad/state.ss
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@

(instance (m Monad) (st stateT)
((return a)
(using (inner st.inner : Monad) (lambda (s) (inner.return [a . s]))))
(using (inner st.inner : Monad) (lambda (s) (inner.return [a . s]))))
((>>= ma f)
(using (inner st.inner : Monad)
(lambda (s)
(du inner
pair <- (ma s)
(with ((cons v s!) pair) ((f v) s!)))))))
(using (inner st.inner : Monad)
(lambda (s)
(du inner
pair <- (ma s)
(with ((cons v s!) pair) ((f v) s!)))))))

(instance Run (st stateT)
((run mv (state (void))) (mv state)))
Expand Down

0 comments on commit 0f3a317

Please sign in to comment.