1 ! Copyright (C) 2008 Slava Pestov
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: arrays kernel sequences sequences.deep splitting
4 accessors fry locals combinators namespaces lists lists.lazy
9 GENERIC# fmap 1 ( functor quot -- functor' )
13 ! Mixin type for monad singleton classes, used for return/fail only
16 GENERIC: monad-of ( mvalue -- singleton )
17 GENERIC: return ( value singleton -- mvalue )
18 GENERIC: fail ( value singleton -- mvalue )
19 GENERIC: >>= ( mvalue -- quot )
21 M: monad return monad-of return ;
22 M: monad fail monad-of fail ;
24 : bind ( mvalue quot -- mvalue' ) swap >>= call( quot -- mvalue ) ;
25 : bind* ( mvalue quot -- mvalue' ) '[ drop @ ] bind ;
26 : >> ( mvalue k -- mvalue' ) '[ drop _ ] bind ;
28 :: lift-m2 ( m1 m2 f monad -- m3 )
29 m1 [| x1 | m2 [| x2 | x1 x2 f monad return ] bind ] bind ;
31 :: apply ( mvalue mquot monad -- result )
34 value quot call( value -- mvalue ) monad return
38 M: monad fmap over '[ @ _ return ] bind ;
41 : do ( quots -- result ) unclip [ call( -- mvalue ) ] curry dip [ bind ] each ;
44 SINGLETON: identity-monad
45 INSTANCE: identity-monad monad
47 TUPLE: identity value ;
48 INSTANCE: identity monad
50 M: identity monad-of drop identity-monad ;
52 M: identity-monad return drop identity boa ;
53 M: identity-monad fail "Fail" throw ;
55 M: identity >>= value>> '[ _ swap call( x -- y ) ] ;
57 : run-identity ( identity -- value ) value>> ;
60 SINGLETON: maybe-monad
61 INSTANCE: maybe-monad monad
66 : just ( value -- just ) \ just boa ;
68 UNION: maybe just nothing ;
71 M: maybe monad-of drop maybe-monad ;
73 M: maybe-monad return drop just ;
74 M: maybe-monad fail 2drop nothing ;
76 M: nothing >>= '[ drop _ ] ;
77 M: just >>= value>> '[ _ swap call( x -- y ) ] ;
79 : if-maybe ( maybe just-quot nothing-quot -- )
80 pick nothing? [ 2nip call ] [ drop [ value>> ] dip call ] if ; inline
83 SINGLETON: either-monad
84 INSTANCE: either-monad monad
87 : left ( value -- left ) \ left boa ;
90 : right ( value -- right ) \ right boa ;
92 UNION: either left right ;
93 INSTANCE: either monad
95 M: either monad-of drop either-monad ;
97 M: either-monad return drop right ;
98 M: either-monad fail drop left ;
100 M: left >>= '[ drop _ ] ;
101 M: right >>= value>> '[ _ swap call( x -- y ) ] ;
103 : if-either ( value left-quot right-quot -- )
104 [ [ value>> ] [ left? ] bi ] 2dip if ; inline
107 SINGLETON: array-monad
108 INSTANCE: array-monad monad
109 INSTANCE: array monad
111 M: array-monad return drop 1array ;
112 M: array-monad fail 2drop { } ;
114 M: array monad-of drop array-monad ;
116 M: array >>= '[ _ swap map concat ] ;
119 SINGLETON: list-monad
120 INSTANCE: list-monad monad
123 M: list-monad return drop 1list ;
124 M: list-monad fail 2drop nil ;
126 M: list monad-of drop list-monad ;
128 M: list >>= '[ _ swap lazy-map lconcat ] ;
131 SINGLETON: state-monad
132 INSTANCE: state-monad monad
135 : state ( quot -- state ) \ state boa ;
137 INSTANCE: state monad
139 M: state monad-of drop state-monad ;
141 M: state-monad return drop '[ _ 2array ] state ;
142 M: state-monad fail "Fail" throw ;
144 : mcall ( x state -- y ) quot>> call( x -- y ) ;
146 M: state >>= '[ _ swap '[ _ mcall first2 @ mcall ] state ] ;
148 : get-st ( -- state ) [ dup 2array ] state ;
149 : put-st ( value -- state ) '[ drop _ f 2array ] state ;
151 : run-st ( state initial -- value ) swap mcall second ;
153 : return-st ( value -- mvalue ) state-monad return ;
156 SINGLETON: reader-monad
157 INSTANCE: reader-monad monad
160 : reader ( quot -- reader ) \ reader boa ;
161 INSTANCE: reader monad
163 M: reader monad-of drop reader-monad ;
165 M: reader-monad return drop '[ drop _ ] reader ;
166 M: reader-monad fail "Fail" throw ;
168 M: reader >>= '[ _ swap '[ dup _ mcall @ mcall ] reader ] ;
170 : run-reader ( reader env -- value ) swap quot>> call( env -- value ) ;
172 : ask ( -- reader ) [ ] reader ;
173 : local ( reader quot -- reader' ) swap '[ @ _ mcall ] reader ;
176 SINGLETON: writer-monad
177 INSTANCE: writer-monad monad
179 TUPLE: writer value log ;
180 : writer ( value log -- writer ) \ writer boa ;
182 M: writer monad-of drop writer-monad ;
184 M: writer-monad return drop { } writer ;
185 M: writer-monad fail "Fail" throw ;
187 : run-writer ( writer -- value log ) [ value>> ] [ log>> ] bi ;
189 M: writer >>= '[ [ _ run-writer ] dip '[ @ run-writer ] dip append writer ] ;
191 : pass ( writer -- writer' ) run-writer [ first2 ] dip swap call( x -- y ) writer ;
192 : listen ( writer -- writer' ) run-writer [ 2array ] keep writer ;
193 : tell ( seq -- writer ) f swap writer ;