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' ) inline
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 ;
25 : >> ( mvalue k -- mvalue' ) '[ drop _ ] bind ;
27 :: lift-m2 ( m1 m2 f monad -- m3 )
28 m1 [| x1 | m2 [| x2 | x1 x2 f monad return ] bind ] bind ;
30 :: apply ( mvalue mquot monad -- result )
33 value quot call monad return
37 M: monad fmap over '[ @ _ return ] bind ;
40 : do ( quots -- result ) unclip dip [ bind ] each ;
43 SINGLETON: identity-monad
44 INSTANCE: identity-monad monad
46 TUPLE: identity value ;
47 INSTANCE: identity monad
49 M: identity monad-of drop identity-monad ;
51 M: identity-monad return drop identity boa ;
52 M: identity-monad fail "Fail" throw ;
54 M: identity >>= value>> '[ _ swap call ] ;
56 : run-identity ( identity -- value ) value>> ;
59 SINGLETON: maybe-monad
60 INSTANCE: maybe-monad monad
65 : just ( value -- just ) \ just boa ;
67 UNION: maybe just nothing ;
70 M: maybe monad-of drop maybe-monad ;
72 M: maybe-monad return drop just ;
73 M: maybe-monad fail 2drop nothing ;
75 M: nothing >>= '[ drop _ ] ;
76 M: just >>= value>> '[ _ swap call ] ;
78 : if-maybe ( maybe just-quot nothing-quot -- )
79 pick nothing? [ 2nip call ] [ drop [ value>> ] dip call ] if ; inline
82 SINGLETON: either-monad
83 INSTANCE: either-monad monad
86 : left ( value -- left ) \ left boa ;
89 : right ( value -- right ) \ right boa ;
91 UNION: either left right ;
92 INSTANCE: either monad
94 M: either monad-of drop either-monad ;
96 M: either-monad return drop right ;
97 M: either-monad fail drop left ;
99 M: left >>= '[ drop _ ] ;
100 M: right >>= value>> '[ _ swap call ] ;
102 : if-either ( value left-quot right-quot -- )
103 [ [ value>> ] [ left? ] bi ] 2dip if ; inline
106 SINGLETON: array-monad
107 INSTANCE: array-monad monad
108 INSTANCE: array monad
110 M: array-monad return drop 1array ;
111 M: array-monad fail 2drop { } ;
113 M: array monad-of drop array-monad ;
115 M: array >>= '[ _ swap map concat ] ;
118 SINGLETON: list-monad
119 INSTANCE: list-monad monad
122 M: list-monad return drop 1list ;
123 M: list-monad fail 2drop nil ;
125 M: list monad-of drop list-monad ;
127 M: list >>= '[ _ swap lazy-map lconcat ] ;
130 SINGLETON: state-monad
131 INSTANCE: state-monad monad
134 : state ( quot -- state ) \ state boa ;
136 INSTANCE: state monad
138 M: state monad-of drop state-monad ;
140 M: state-monad return drop '[ _ 2array ] state ;
141 M: state-monad fail "Fail" throw ;
143 : mcall ( state -- ) quot>> call ;
145 M: state >>= '[ _ swap '[ _ mcall first2 @ mcall ] state ] ;
147 : get-st ( -- state ) [ dup 2array ] state ;
148 : put-st ( value -- state ) '[ drop _ f 2array ] state ;
150 : run-st ( state initial -- ) swap mcall second ;
152 : return-st ( value -- mvalue ) state-monad return ;
155 SINGLETON: reader-monad
156 INSTANCE: reader-monad monad
159 : reader ( quot -- reader ) \ reader boa ;
160 INSTANCE: reader monad
162 M: reader monad-of drop reader-monad ;
164 M: reader-monad return drop '[ drop _ ] reader ;
165 M: reader-monad fail "Fail" throw ;
167 M: reader >>= '[ _ swap '[ dup _ mcall @ mcall ] reader ] ;
169 : run-reader ( reader env -- ) swap mcall ;
171 : ask ( -- reader ) [ ] reader ;
172 : local ( reader quot -- reader' ) swap '[ @ _ mcall ] reader ;
175 SINGLETON: writer-monad
176 INSTANCE: writer-monad monad
178 TUPLE: writer value log ;
179 : writer ( value log -- writer ) \ writer boa ;
181 M: writer monad-of drop writer-monad ;
183 M: writer-monad return drop { } writer ;
184 M: writer-monad fail "Fail" throw ;
186 : run-writer ( writer -- value log ) [ value>> ] [ log>> ] bi ;
188 M: writer >>= '[ [ _ run-writer ] dip '[ @ run-writer ] dip append writer ] ;
190 : pass ( writer -- writer' ) run-writer [ first2 ] dip swap call writer ;
191 : listen ( writer -- writer' ) run-writer [ 2array ] keep writer ;
192 : tell ( seq -- writer ) f swap writer ;