3 | import Data.List.Lazy
8 | import Control.Monad.Error.Interface
9 | import Control.Monad.Identity
10 | import Control.Monad.Trans
11 | import public Control.MonadRec
12 | import Control.WellFounded
14 | import public Language.Implicits.IfUnsolved
23 | data Swirl : (Type -> Type) -> (error, result, output : Type) -> Type where
24 | Done : r -> Swirl m e r o
25 | Fail : e -> Swirl m e r o
26 | Yield : o -> Lazy (Swirl m e r o) -> Swirl m e r o
27 | Effect : m (assert_total $
Swirl m e r o) -> Swirl m e r o
28 | BindR : Lazy (Swirl m e r' o) -> (r' -> Swirl m e r o) -> Swirl m e r o
29 | BindE : Lazy (Swirl m e' r o) -> (e' -> Swirl m e r o) -> Swirl m e r o
30 | Ensure : Lazy (Swirl m Void r' Void) -> Lazy (Swirl m e r o) -> Swirl m e (r', r) o
32 | %name Swirl
sw, sv, su
34 | public export %inline
35 | Swirlie : (Type -> Type) -> (error, output : Type) -> Type
36 | Swirlie m e = Swirl m e ()
83 | Functor m => Bifunctor (Swirl m e) where
84 | bimap fr fo $
Done x = Done $
fr x
85 | bimap fr fo $
Fail x = Fail x
86 | bimap fr fo $
Yield o sw = Yield (fo o) (bimap fr fo sw)
87 | bimap fr fo $
Effect msw = Effect $
msw <&> assert_total bimap fr fo
88 | bimap fr fo $
BindR x f = BindR (bimap id fo x) (bimap fr fo . f)
89 | bimap fr fo $
BindE x h = BindE (bimap fr fo x) (bimap fr fo . h)
90 | bimap fr fo $
Ensure l x = Ensure l (bimap id fo x) `BindR` Done . fr
92 | %transform "swirl: mapFst id"
mapFst {f=Swirl m e} (\x => x) x = x
93 | %transform "swirl: mapSnd id"
mapSnd {f=Swirl m e} (\x => x) x = x
94 | %transform "swirl: bimap id id"
bimap {f=Swirl m e} (\x => x) (\x => x) x = x
97 | mapCtx : Functor m => (forall a. m a -> n a) -> Swirl m e r o -> Swirl n e r o
98 | mapCtx f $
Done x = Done x
99 | mapCtx f $
Fail x = Fail x
100 | mapCtx f $
Yield x sw = Yield x $
mapCtx f sw
101 | mapCtx f $
Effect msw = Effect $
f $
msw <&> assert_total mapCtx f
102 | mapCtx f $
BindR x g = BindR (mapCtx f x) (mapCtx f . g)
103 | mapCtx f $
BindE x h = BindE (mapCtx f x) (mapCtx f . h)
104 | mapCtx f $
Ensure l x = Ensure (mapCtx f l) (mapCtx f x)
107 | mapError : (e -> e') -> Swirl m e r o -> Swirl m e' r o
108 | mapError f $
Done x = Done x
109 | mapError f $
Fail x = Fail $
f x
110 | mapError f $
BindE x h = BindE x (mapError f . h)
111 | mapError f sw = BindE sw (Fail . f)
115 | namespace ComposeResults
118 | concat' : Functor m => (resultComp : rl -> rr -> r) -> Swirl m el rl o -> Lazy (Swirl m er rr o) -> Swirl m (Either el er) r o
119 | concat' fr (Done x) sv = mapError Right . mapFst (fr x) $
sv
120 | concat' fr (Fail x) sv = Fail $
Left x
121 | concat' fr sw sv = BindR (mapError Left sw) $
\x => mapError Right $
mapFst (fr x) sv
124 | concat : Functor m => (resultComp : rl -> rr -> r) -> Swirl m e rl o -> Lazy (Swirl m e rr o) -> Swirl m e r o
125 | concat fr (Done x) sv = mapFst (fr x) sv
126 | concat fr (Fail x) sv = Fail x
127 | concat fr sw sv = BindR sw $
\x => mapFst (fr x) sv
130 | (++) : Functor m => Semigroup r => Swirl m e r o -> Lazy (Swirl m e r o) -> Swirl m e r o
131 | (++) = concat (<+>)
134 | andThen : Swirl m e () o -> Lazy (Swirl m e r o) -> Swirl m e r o
135 | andThen sw sv = BindR sw $
const sv
145 | hasSucceeded : (0 _ : IfUnsolved m' m) =>
146 | (0 _ : IfUnsolved e' Void) =>
147 | (0 _ : IfUnsolved o' Void) =>
148 | Swirl m e r o -> Maybe $
Swirl m' e' r o'
149 | hasSucceeded $
Done x = Just $
Done x
150 | hasSucceeded _ = Nothing
155 | forgetO : Functor m => (0 _ : IfUnsolved o Void) => Swirl m e r a -> Swirl m e r o
156 | forgetO $
Done x = Done x
157 | forgetO $
Fail e = Fail e
158 | forgetO $
Yield x sw = forgetO sw
159 | forgetO $
Effect msw = Effect $
msw <&> assert_total forgetO
160 | forgetO $
BindR sw g = BindR (forgetO sw) (forgetO . g)
161 | forgetO $
BindE sw h = BindE (forgetO sw) (forgetO . h)
162 | forgetO $
Ensure l x = Ensure (forgetO l) (forgetO x)
165 | forgetR : Functor m => Monoid r => (0 _ : IfUnsolved r ()) => Swirl m e r' a -> Swirl m e r a
166 | forgetR $
Done x = Done neutral
167 | forgetR $
Fail e = Fail e
168 | forgetR sw = BindR sw $
const $
Done neutral
175 | (.by) : Functor m => (x -> Swirl m e r o) -> m x -> Swirl m e r o
176 | f.by = Effect . map f
179 | by : Functor m => (x -> Swirl m e r o) -> m x -> Swirl m e r o
187 | preEmitTo : (0 _ : IfUnsolved e Void) =>
188 | Lazy (Swirl m e r o) ->
191 | preEmitTo = flip Yield
195 | (0 _ : IfUnsolved e Void) =>
196 | (0 _ : IfUnsolved r ()) =>
198 | emit = preEmitTo $
Done neutral
201 | preEmitsTo : (0 _ : IfUnsolved e Void) =>
202 | Lazy (Swirl m e r o) ->
205 | preEmitsTo = foldrLazy Yield
208 | emits : Monoid r =>
209 | (0 _ : IfUnsolved e Void) =>
210 | (0 _ : IfUnsolved r ()) =>
213 | emits = preEmitsTo $
Done neutral
216 | preEmitsTo' : Foldable f =>
217 | (0 _ : IfUnsolved e Void) =>
218 | (0 _ : IfUnsolved f List) =>
219 | Lazy (Swirl m e r o) ->
222 | preEmitsTo' = force .: foldr (delay .: Yield)
225 | emits' : Monoid r =>
227 | (0 _ : IfUnsolved e Void) =>
228 | (0 _ : IfUnsolved f List) =>
229 | (0 _ : IfUnsolved r ()) =>
232 | emits' = preEmitsTo' $
Done neutral
245 | succeed : (0 _ : IfUnsolved e Void) =>
246 | (0 _ : IfUnsolved o Void) =>
253 | fail : (0 _ : IfUnsolved r ()) =>
254 | (0 _ : IfUnsolved o Void) =>
261 | succeedOrFail : (0 _ : IfUnsolved o Void) =>
262 | Either e r -> Swirl m e r o
263 | succeedOrFail = either fail succeed
268 | emitOrFail : Monoid r =>
269 | (0 _ : IfUnsolved r ()) =>
270 | Either e o -> Swirl m e r o
271 | emitOrFail = either fail emit
276 | emitRes' : Functor m => (r -> r') -> Swirl m e r Void -> Swirl m e r' r
277 | emitRes' f $
Done x = Yield x $
Done $
f x
278 | emitRes' f $
Fail x = Fail x
279 | emitRes' f sw = mapSnd absurd sw `BindR` \r => Yield r $
Done $
f r
282 | emitRes : Functor m =>
284 | (0 _ : IfUnsolved r ()) =>
285 | Swirl m e r' Void -> Swirl m e r r'
286 | emitRes = emitRes' $
const neutral
289 | swallowM : Functor m => Swirl m e r (m o) -> Swirl m e r o
290 | swallowM $
Done x = Done x
291 | swallowM $
Fail e = Fail e
292 | swallowM $
Yield x sw = Effect $
x <&> \x' => Yield x' $
swallowM sw
293 | swallowM $
Effect msw = Effect $
msw <&> assert_total swallowM
294 | swallowM $
BindR sw g = BindR (swallowM sw) (swallowM . g)
295 | swallowM $
BindE sw h = BindE (swallowM sw) (swallowM . h)
296 | swallowM $
Ensure l x = Ensure l $
swallowM x
303 | foldlRO : Functor m => (0 _ : IfUnsolved o'' Void) => (o' -> o -> o') -> o' -> (o' -> r -> r') -> Swirl m e r o -> Swirl m e r' o''
304 | foldlRO fo inito fr $
Done x = Done $
fr inito x
305 | foldlRO fo inito fr $
Fail e = Fail e
306 | foldlRO fo inito fr $
Yield x sw = foldlRO fo (fo inito x) fr sw
307 | foldlRO fo inito fr $
Effect msw = Effect $
msw <&> assert_total foldlRO fo inito fr
308 | foldlRO fo inito fr $
BindR x f = BindR (foldlRO fo inito (,) x) $
\(into, r') => foldlRO fo into fr $
f r'
309 | foldlRO fo inito fr $
BindE x h = BindE (foldlRO fo inito fr x) (foldlRO fo inito fr . h)
310 | foldlRO fo inito fr $
Ensure l x = Ensure l (foldlRO fo inito (,) x) `BindR` \(r', o', r) => Done $
fr o' (r', r)
313 | foldlRO' : Functor m => (0 _ : IfUnsolved o' Void) => (o -> o -> o) -> (o -> r -> r) -> Swirl m e r o -> Swirl m e r o'
314 | foldlRO' f g = foldlRO (Just .: maybe id f) Nothing (maybe id g)
317 | foldRO : Functor m => Semigroup o => (0 _ : IfUnsolved o' Void) => Swirl m e o o -> Swirl m e o o'
318 | foldRO = foldlRO' (<+>) (<+>)
321 | foldlO : Functor m =>
322 | (0 _ : IfUnsolved o' Void) =>
327 | foldlO f x = foldlRO f x const
330 | foldO : Functor m =>
332 | (0 _ : IfUnsolved o' Void) =>
335 | foldO = foldRO . mapFst (const neutral)
338 | outputs : Functor m =>
339 | (0 _ : IfUnsolved o' Void) =>
341 | Swirl m e (SnocList o) o'
342 | outputs = foldlO (:<) [<]
346 | foldlO'R : Functor m => (o' -> o -> o') -> o' -> Swirl m e r o -> Swirl m (e, o') (r, o') Void
347 | foldlO'R op init $
Done x = Done (x, init)
348 | foldlO'R op init $
Fail e = Fail (e, init)
349 | foldlO'R op init $
Yield x sw = foldlO'R op (init `op` x) sw
350 | foldlO'R op init $
Effect msw = Effect $
msw <&> assert_total foldlO'R op init
351 | foldlO'R op init $
BindR x f = BindR (foldlO'R op init x) $
\(r', into) => foldlO'R op into $
f r'
352 | foldlO'R op init $
BindE x h = BindE (foldlO'R op init x) $
\(e, into) => foldlO'R op into $
h e
353 | foldlO'R op init $
Ensure l x = Ensure l (foldlO'R op init x) `BindR` \(r', r, o') => Done ((r', r), o')
357 | foldlO' : Functor m => (o' -> o -> o') -> o' -> Swirl m e r o -> Swirl m e r o'
359 | (`BindE` \(e, o') => Yield o' $
Fail e) .
360 | (`BindR` \(x, o') => Yield o' $
Done x) .
361 | mapSnd absurd . foldlO'R f i
365 | foldlO : Functor m => (o' -> o -> o') -> o' -> Swirl m e r o -> Swirl m e r o'
367 | (`BindR` \(x, o') => Yield o' $
Done x) .
368 | mapError fst . mapSnd absurd . foldlO'R f i
371 | foldO : Functor m => Monoid o => Swirl m e r o -> Swirl m e r o
372 | foldO = foldlO (<+>) neutral
375 | outputs : Functor m => Swirl m e r o -> Swirl m e r (SnocList o)
376 | outputs = foldlO (:<) [<]
380 | mergeSemi : Semigroup r => (r, Maybe r) -> r
381 | mergeSemi (x, Nothing) = x
382 | mergeSemi (x, Just y) = x <+> y
384 | namespace ComposeResults
387 | mergeCtxs' : Applicative m => Applicative n => (r'' -> r' -> r'') -> r'' -> Swirl m e r (Swirl n e' r' o) -> Swirl (m . n) (Either e e') (r, r'') o
388 | mergeCtxs' ff fi $
Done x = Done (x, fi)
389 | mergeCtxs' ff fi $
Fail e = Fail $
Left e
390 | mergeCtxs' ff fi $
Yield x sw = mapCtx pure (mapError Right x) `BindR` \lr => mergeCtxs' ff (fi `ff` lr) sw
391 | mergeCtxs' ff fi $
Effect msw = Effect $
msw <&> pure . assert_total mergeCtxs' ff fi
392 | mergeCtxs' ff fi $
BindR x f = BindR (mergeCtxs' ff fi x) $
\(y, ys) => mergeCtxs' ff ys $
f y
393 | mergeCtxs' ff fi $
BindE x h = BindE (mergeCtxs' ff fi x) $
either (mergeCtxs' ff fi . h) (Fail . Right)
394 | mergeCtxs' ff fi $
Ensure l x = Ensure (mapCtx (map pure) l) (mergeCtxs' ff fi x) `BindR` \(rl, rr, rs) => succeed ((rl, rr), rs)
397 | mergeCtxs : Applicative m => Applicative n => Semigroup r => Swirl m e r (Swirl n e r o) -> Swirl (m . n) e r o
398 | mergeCtxs = (`BindR` Done . mergeSemi) . mapError fromEither . mergeCtxs' (\a, x => (a <+> Just x) @{Deep}) Nothing
400 | namespace ComposeResults
403 | squashOuts' : Functor m => (r'' -> r' -> r'') -> r'' -> Swirl m e r (Swirl m e' r' o) -> Swirl m (Either e e') (r, r'') o
404 | squashOuts' ff fi $
Done x = Done (x, fi)
405 | squashOuts' ff fi $
Fail e = Fail $
Left e
406 | squashOuts' ff fi $
Yield x sw = mapError Right x `BindR` \lr => squashOuts' ff (fi `ff` lr) sw
407 | squashOuts' ff fi $
Effect msw = Effect $
msw <&> assert_total squashOuts' ff fi
408 | squashOuts' ff fi $
BindR x f = BindR (squashOuts' ff fi x) $
\(y, ys) => squashOuts' ff ys $
f y
409 | squashOuts' ff fi $
BindE x h = BindE (squashOuts' ff fi x) $
either (squashOuts' ff fi . h) (Fail . Right)
410 | squashOuts' ff fi $
Ensure l x = Ensure l (squashOuts' ff fi x) `BindR` \(rl, rr, rs) => succeed ((rl, rr), rs)
413 | squashOuts' : Functor m => Swirl m e r (Swirl m e' () o) -> Swirl m (Either e e') r o
414 | squashOuts' = mapFst fst . squashOuts' (const id) ()
416 | squashOuts : Functor m => Semigroup r => Swirl m e r (Swirl m e r o) -> Swirl m e r o
417 | squashOuts = mapFst mergeSemi . mapError fromEither . squashOuts' (\a, x => (a <+> Just x) @{Deep}) Nothing
421 | bindR : Swirl m e r' o -> (r' -> Swirl m e r o) -> Swirl m e r o
422 | bindR (Done x) f = f x
423 | bindR (Fail e) _ = Fail e
424 | bindR sw f = BindR sw f
427 | handleRes : (r' -> Swirl m e r o) -> Swirl m e r' o -> Swirl m e r o
428 | handleRes = flip bindR
430 | squashRes : Swirl m e (Swirl m e r o) o -> Swirl m e r o
431 | squashRes sw = sw `bindR` id
436 | mapEither' : Functor m =>
437 | (0 _ : IfUnsolved e Void) =>
438 | (0 _ : IfUnsolved r ()) =>
439 | (o -> Either e' o') ->
441 | Swirl m (Either e e') r o'
442 | mapEither' f = squashOuts' . mapSnd (emitOrFail . f)
445 | mapEither : Functor m => (o -> Either e o') -> Swirl m e r o -> Swirl m e r o'
446 | mapEither = mapError fromEither .: mapEither'
450 | mapMaybe : Functor m => (o -> Maybe o') -> Swirl m e r o -> Swirl m e r o'
451 | mapMaybe f $
Done x = Done x
452 | mapMaybe f $
Fail e = Fail e
453 | mapMaybe f $
Yield x sw = case f x of
454 | Nothing => mapMaybe f sw
455 | Just y => Yield y $
mapMaybe f sw
456 | mapMaybe f $
Effect msw = Effect $
msw <&> assert_total mapMaybe f
457 | mapMaybe f $
BindR sw g = BindR (mapMaybe f sw) (mapMaybe f . g)
458 | mapMaybe f $
BindE sw h = BindE (mapMaybe f sw) (mapMaybe f . h)
459 | mapMaybe f $
Ensure l x = Ensure l (mapMaybe f x)
462 | filter : Functor m => (a -> Bool) -> Swirl m e r a -> Swirl m e r a
463 | filter f $
Done x = Done x
464 | filter f $
Fail e = Fail e
465 | filter f $
Yield x sw = case f x of
466 | False => filter f sw
467 | True => Yield x $
filter f sw
468 | filter f $
Effect msw = Effect $
msw <&> assert_total filter f
469 | filter f $
BindR sw g = BindR (filter f sw) (filter f . g)
470 | filter f $
BindE sw h = BindE (filter f sw) (filter f . h)
471 | filter f $
Ensure l x = Ensure l (filter f x)
475 | namespace ComposeResults
496 | Functor m => Functor (Swirl m e r) where
500 | Functor m => Monoid r => Applicative (Swirl m e r) where
501 | pure x = Yield x $
Done neutral
502 | fs <*> xs = squashOuts $
fs <&> flip map xs
505 | Functor m => Monoid r => Monad (Swirl m e r) where
509 | infixl 1 :>>=
, =<<:
, :>>
512 | (=<<:) : Functor m => (o' -> Swirl m e () o) -> Swirl m e r o' -> Swirl m e r o
513 | (=<<:) = mapError fromEither .: squashOuts' .: mapSnd
516 | (:>>=) : Functor m => Swirl m e r o' -> (o' -> Swirl m e () o) -> Swirl m e r o
517 | (:>>=) = flip (=<<:)
520 | (:>>) : Functor m => Swirl m e r () -> Lazy (Swirl m e () o) -> Swirl m e r o
521 | (:>>) sw sv = mapError fromEither $
squashOuts' $
mapSnd (const sv) sw
524 | HasIO io => Monoid r => HasIO (Swirl io e r) where
525 | liftIO = emit.by . liftIO
528 | Monoid r => MonadTrans (\m => Swirl m e r) where
532 | Functor m => Monoid r => MonadError e (Swirl m e r) where
534 | catchError = BindE . delay
541 | [ByResult] Functor m => Functor (\r => Swirl m e r o) where
544 | namespace Applicative
547 | [ByResult] Functor m => Applicative (\r => Swirl m e r o)
548 | using Functor.ByResult where
550 | fs <*> xs = fs `bindR` (`mapFst` xs)
555 | [ByResult] Functor m => Monad (\r => Swirl m e r o)
556 | using Applicative.ByResult where
563 | [ByResult] HasIO io => HasIO (\r => Swirl io e r o)
564 | using Monad.ByResult where
565 | liftIO = succeed.by . liftIO
567 | namespace MonadTrans
570 | [ByResult] MonadTrans (\m, r => Swirl m e r o) where
573 | namespace MonadError
576 | [ByResult] Functor m => MonadError e (\r => Swirl m e r o)
577 | using Monad.ByResult where
579 | catchError = BindE . delay
584 | handleError : (0 _ : IfUnsolved e' Void) =>
585 | (e -> Swirl m e' r o) -> Swirl m e r o -> Swirl m e' r o
586 | handleError = flip $
BindE . delay
591 | withFinally' : Functor m => Swirl m Void r' Void -> Swirl m e r o -> Swirl m e (r', r) o
592 | withFinally' l x = Ensure l x
595 | withFinally : Functor m => Swirl m Void () Void -> Swirl m e r o -> Swirl m e r o
596 | withFinally = mapFst snd .: withFinally'
599 | bracket' : Functor m =>
600 | (0 _ : IfUnsolved e Void) =>
601 | (0 _ : IfUnsolved o Void) =>
602 | (init : Swirl m e res o) ->
603 | (cleanup : res -> Swirl m Void r' Void) ->
604 | (action : res -> Swirl m e r o) ->
605 | Swirl m e (r', r) o
606 | bracket' init cleanup action = init `bindR` \res => withFinally' (cleanup res) (action res)
609 | bracket : Functor m =>
610 | (0 _ : IfUnsolved e Void) =>
611 | (0 _ : IfUnsolved o Void) =>
612 | (init : Swirl m e res o) ->
613 | (cleanup : res -> Swirl m Void () Void) ->
614 | (action : res -> Swirl m e r o) ->
616 | bracket init cleanup action = init `bindR` \res => withFinally (cleanup res) (action res)
619 | bracketO : Functor m =>
621 | (0 _ : IfUnsolved e Void) =>
622 | (0 _ : IfUnsolved r ()) =>
623 | (init : Swirl m e res Void) ->
624 | (cleanup : res -> Swirl m Void r Void) ->
626 | bracketO init cleanup = bimap fst snd $
emitRes' id $
bracket' init cleanup succeed
631 | repeat : Functor m =>
632 | (0 _ : IfUnsolved e Void) =>
633 | Swirl m e () o -> Swirl m e Void o
634 | repeat sw = forgetR sw `andThen` repeat sw
638 | tickUntil : Functor m =>
640 | (0 _ : IfUnsolved r ()) =>
641 | (0 _ : IfUnsolved e Void) =>
642 | Swirl m e Bool Void -> Swirl m e r ()
643 | tickUntil $
Done True = Done neutral
644 | tickUntil sw@(Done False) = Yield () $
tickUntil sw
645 | tickUntil $
Fail e = Fail e
646 | tickUntil sw = map absurd sw `bindR` \stop => if stop then succeed neutral else Yield () $
tickUntil sw
650 | take' : Functor m => Nat -> Swirl m e r o -> Swirl m (Nat, e) (Nat, r) o
651 | take' Z sw = mapError (Z,) $
mapFst (Z,) $
forgetO sw
652 | take' (S k) $
Yield x sw = Yield x $
take' k sw
653 | take' n $
Done x = Done (n, x)
654 | take' n $
Fail e = Fail (n, e)
655 | take' n $
Effect msw = Effect $
msw <&> assert_total take' n
656 | take' n $
BindR x f = BindR (take' n x) $
\(n', r') => take' n' $
f r'
657 | take' n $
BindE x h = BindE (take' n x) $
\(n', e') => take' n' $
h e'
658 | take' n $
Ensure l x = Ensure l (take' n x) `BindR` \(r', n', r) => Done (n, r', r)
661 | take : Functor m => Nat -> Swirl m e r o -> Swirl m e r o
662 | take = mapError snd .: mapFst snd .: take'
666 | twCont : Functor m =>
668 | (succMap : Lazy (Swirl m (Bool, e) (Bool, r) o) -> Swirl m (Bool, e) (Bool, r) o) ->
671 | Swirl m (Bool, e) (Bool, r) o
672 | takeWhile' : Functor m => (o -> Bool) -> Swirl m e r o -> Swirl m (Bool, e) (Bool, r) o
673 | takeWhile' _ $
Done x = Done (True, x)
674 | takeWhile' _ $
Fail e = Fail (True, e)
675 | takeWhile' w $
Yield x sw = twCont (w x) (Yield x) w sw
676 | takeWhile' w $
Effect msw = Effect $
msw <&> assert_total takeWhile' w
677 | takeWhile' w $
BindR x f = BindR (takeWhile' w x) $
\(ct, r') => twCont ct force w $
f r'
678 | takeWhile' w $
BindE x h = BindE (takeWhile' w x) $
\(ct, e') => twCont ct force w $
h e'
679 | takeWhile' w $
Ensure l x = Ensure l (takeWhile' w x) `BindR` \(r', b', r) => Done (b', r', r)
680 | twCont False s _ sw = mapError (False,) $
mapFst (False,) $
forgetO sw
681 | twCont True s w sw = s $
delay $
takeWhile' w sw
684 | takeWhile : Functor m => (o -> Bool) -> Swirl m e r o -> Swirl m e r o
685 | takeWhile = mapError snd .: mapFst snd .: takeWhile'
687 | drop' : Functor m => Nat -> Swirl m e r o -> Swirl m (Nat, e) (Nat, r) o
688 | drop' Z sw = mapError (Z,) $
mapFst (Z,) sw
689 | drop' (S k) $
Yield x sw = drop' k sw
690 | drop' n $
Done x = Done (n, x)
691 | drop' n $
Fail e = Fail (n, e)
692 | drop' n $
Effect msw = Effect $
msw <&> assert_total drop' n
693 | drop' n $
BindR x f = BindR (drop' n x) $
\(n', r') => drop' n' $
f r'
694 | drop' n $
BindE x h = BindE (drop' n x) $
\(n', e') => drop' n' $
h e'
695 | drop' n $
Ensure l x = Ensure l (drop' n x) `BindR` \(r', n', r) => Done (n, r', r)
698 | drop : Functor m => Nat -> Swirl m e r o -> Swirl m e r o
699 | drop = mapError snd .: mapFst snd .: drop'
703 | dwCont : Functor m =>
705 | (succMap : Lazy (Swirl m e r o) -> Swirl m e r o) ->
708 | Swirl m (Bool, e) (Bool, r) o
709 | dropWhile' : Functor m => (o -> Bool) -> Swirl m e r o -> Swirl m (Bool, e) (Bool, r) o
710 | dwCont True s w sw = dropWhile' w sw
711 | dwCont False s _ sw = mapError (False,) $
mapFst (False,) $
s sw
712 | dropWhile' _ $
Done x = Done (True, x)
713 | dropWhile' _ $
Fail e = Fail (True, e)
714 | dropWhile' w $
Yield x sw = dwCont (w x) (Yield x) w sw
715 | dropWhile' w $
Effect msw = Effect $
msw <&> assert_total dropWhile' w
716 | dropWhile' w $
BindR x f = BindR (dropWhile' w x) $
\(ct, r') => dwCont ct force w $
f r'
717 | dropWhile' w $
BindE x h = BindE (dropWhile' w x) $
\(ct, e') => dwCont ct force w $
h e'
718 | dropWhile' w $
Ensure l x = Ensure l (dropWhile' w x) `BindR` \(r', b', r) => Done (b', r', r)
721 | dropWhile : Functor m => (o -> Bool) -> Swirl m e r o -> Swirl m e r o
722 | dropWhile = mapError snd .: mapFst snd .: dropWhile'
726 | intersperseOuts' : Functor m => (sep : Swirl m e () o) -> Swirl m e r o -> Swirl m (Bool, e) (Bool, r) o
727 | %inline prepO : Functor m => Bool -> (sep : Swirl m e () o) -> Swirl m e r o -> Swirl m (Bool, e) (Bool, r) o
728 | prepO True sep sw = mapError ((True,) . fromEither) $
mapFst (True,) $
squashOuts' $
map (\x' => concat const sep $
emit x') sw
729 | prepO False sep sw = intersperseOuts' sep sw
730 | intersperseOuts' sep $
Done x = Done (False, x)
731 | intersperseOuts' sep $
Fail e = Fail (False, e)
732 | intersperseOuts' sep $
Yield x sw = Yield x $
prepO True sep sw
733 | intersperseOuts' sep $
Effect msw = Effect $
msw <&> assert_total intersperseOuts' sep
734 | intersperseOuts' sep $
BindR x f = BindR (intersperseOuts' sep x) $
\(fwas, r') => prepO fwas sep $
f r'
736 | intersperseOuts' sep $
BindE x h = BindE (assert_total intersperseOuts' (mapError Right sep) (mapError Left x)) $
\(fwas, ee) =>
738 | Left e' => prepO fwas sep $
h e'
739 | Right e => Fail (fwas, e)
740 | intersperseOuts' sep $
Ensure l x = Ensure l (intersperseOuts' sep x) `BindR` \(r', b, r) => Done (b, r', r)
743 | intersperseOuts : Functor m => (sep : Swirl m e () o) -> Swirl m e r o -> Swirl m e r o
744 | intersperseOuts = mapError snd .: mapFst snd .: intersperseOuts'
747 | iterateAlong : Functor m => (i -> i) -> i -> Swirl m e r o -> Swirl m e r (i, o)
748 | iterateAlong next = mapError snd .: mapFst snd .: go where
749 | go : forall e, r. i -> Swirl m e r o -> Swirl m (i, e) (i, r) (i, o)
750 | go n $
Done x = Done (n, x)
751 | go n $
Fail e = Fail (n, e)
752 | go n $
Yield x sw = Yield (n, x) $
go (next n) sw
753 | go n $
Effect msw = Effect $
msw <&> assert_total go n
754 | go n $
BindR x f = go n x `BindR` \(n', r') => go n' $
f r'
755 | go n $
BindE x h = go n x `BindE` \(n', e') => go n' $
h e'
756 | go n $
Ensure l x = Ensure l (go n x) `BindR` \(r', i, r) => Done (i, r', r)
759 | zipWithIndex : Functor m => Swirl m e r o -> Swirl m e r (Nat, o)
760 | zipWithIndex = iterateAlong S Z
767 | toLazyList' : Swirl Identity e r o -> (Either e r, Lazy (LazyList o))
768 | toLazyList' $
Done x = (Right x, [])
769 | toLazyList' $
Fail e = (Left e, [])
770 | toLazyList' $
Yield x sw = (x ::) <$> toLazyList' sw
771 | toLazyList' $
Effect $
Id sw = toLazyList' sw
772 | toLazyList' $
BindR x f = let (ir, ll) = toLazyList' x in
774 | Left e => (Left e, ll)
775 | Right r => toLazyList' (f r) <&> \rr => ll ++ rr
776 | toLazyList' $
BindE x h = let (ir, ll) = toLazyList' x in
778 | Left e => toLazyList' (h e) <&> \rr => ll ++ rr
779 | Right r => (Right r, ll)
780 | toLazyList' $
Ensure l x = let (Right r', Delay []) = toLazyList' l in
781 | map (r',) `mapFst` toLazyList' x
784 | toLazyList : Swirl Identity Void () o -> LazyList o
785 | toLazyList = force . snd . toLazyList'
789 | data Ctx : (Type -> Type) -> (inE, inR, outE, outR : Type) -> Type where
790 | Nil : Ctx m e r e r
791 | BiR : (r' -> Swirl m e r Void) -> Ctx m e r e0 r0 -> Ctx m e r' e0 r0
792 | BiE : (e' -> Swirl m e r Void) -> Ctx m e r e0 r0 -> Ctx m e' r e0 r0
793 | Ens : Swirl m Void r' Void -> Ctx m e (r', r) e0 r0 -> Ctx m e r e0 r0
795 | data St : (Type -> Type) -> (finalE, finalR : Type) -> Type where
796 | AtCtx : Swirl m e' r' Void -> Ctx m e' r' e r -> St m e r
798 | data StLT : St m e r -> St m e r -> Type where
799 | BiRLT : (sw `AtCtx` ctx) `StLT` (sv `AtCtx` BiR f ctx)
800 | BiELT : (sw `AtCtx` ctx) `StLT` (sv `AtCtx` BiE f ctx)
801 | EnsLT : (sw `AtCtx` ctx) `StLT` (sv `AtCtx` Ens l ctx)
803 | SwBR : (Force sw `AtCtx` BiR f ctx) `StLT` (BindR sw f `AtCtx` ctx)
804 | SwBE : (Force sw `AtCtx` BiE f ctx) `StLT` (BindE sw f `AtCtx` ctx)
805 | SwEn : (Force sw `AtCtx` Ens (Force l) ctx) `StLT` (Ensure l sw `AtCtx` ctx)
807 | EfLT : (sw `AtCtx` ctx) `StLT` (Effect msw `AtCtx` ctx)
811 | WellFounded (St m e r) StLT where
812 | wellFounded st = wellFounded st
817 | runSwirlE : MonadRec m => Swirl m e r Void -> m $
Either e r
818 | runSwirlE sw = trWellFounded (sw `AtCtx` []) () $
\sw, () => case sw of
820 | Done x `AtCtx` [] => pure $
Done $
Right x
821 | Done x `AtCtx` BiR f ct => pure $
Cont (f x `AtCtx` ct) BiRLT ()
822 | Done x `AtCtx` BiE _ ct => pure $
Cont (Done x `AtCtx` ct) BiELT ()
823 | Done x `AtCtx` Ens sv ct => pure $
Cont ((mapError absurd $
mapFst (,x) $
sv) `AtCtx` ct) EnsLT ()
825 | Fail e `AtCtx` [] => pure $
Done $
Left e
826 | Fail e `AtCtx` BiR _ ct => pure $
Cont (Fail e `AtCtx` ct) BiRLT ()
827 | Fail e `AtCtx` BiE h ct => pure $
Cont (h e `AtCtx` ct) BiELT ()
828 | Fail e `AtCtx` Ens sv ct => pure $
Cont ((mapError absurd (forgetR sv) `andThen` Fail e) `AtCtx` ct) EnsLT ()
830 | Effect msw `AtCtx` ct => msw <&> \sw => Cont (sw `AtCtx` ct) EfLT ()
832 | BindR sw f `AtCtx` ct => pure $
Cont (sw `AtCtx` BiR f ct) SwBR ()
833 | BindE sw h `AtCtx` ct => pure $
Cont (sw `AtCtx` BiE h ct) SwBE ()
834 | Ensure l sw `AtCtx` ct => pure $
Cont (sw `AtCtx` Ens l ct) SwEn ()
837 | runSwirl : MonadRec m => Swirl m Void a Void -> m a
838 | runSwirl = map (\(Right x) => x) . runSwirlE
845 | data Whirl : (Type -> Type) -> (error, output, result : Type) -> Type where
846 | MkWhirl : Swirl m e r o -> Whirl m e o r
848 | %name Whirl
wh, wi, wj
850 | public export %inline
851 | Whirlie : (Type -> Type) -> (error, result : Type) -> Type
852 | Whirlie m e = Whirl m e Void
855 | toWhirl : Swirl m e r o -> Whirl m e o r
859 | toSwirl : Whirl m e o r -> Swirl m e r o
860 | toSwirl $
MkWhirl sw = sw
863 | mapAsSwirl : (Swirl m e r o -> Swirl m' e' r' o') -> Whirl m e o r -> Whirl m' e' o' r'
864 | mapAsSwirl f = toWhirl . f . toSwirl
867 | apAsSwirl : (Swirl m1 e1 r1 o1 -> Swirl m2 e2 r2 o2 -> Swirl m e r o) -> Whirl m1 e1 o1 r1 -> Whirl m2 e2 o2 r2 -> Whirl m e o r
868 | apAsSwirl f x y = toWhirl $
f (toSwirl x) (toSwirl y)
871 | Functor m => Bifunctor (Whirl m e) where
872 | bimap = mapAsSwirl .: flip bimap
875 | Functor m => Functor (Whirl m e o) where
876 | map = mapAsSwirl . map @{ByResult}
879 | Functor m => Applicative (Whirl m e o) where
880 | pure = toWhirl . pure @{ByResult}
881 | (<*>) = apAsSwirl $
(<*>) @{ByResult}
884 | Functor m => Monad (Whirl m e o) where
885 | join = mapAsSwirl (join @{ByResult}) . map toSwirl
886 | x >>= f = toWhirl $
(>>=) @{ByResult} (toSwirl x) (toSwirl . f)
889 | HasIO m => HasIO (Whirl m e o) where
890 | liftIO = toWhirl . liftIO @{ByResult}
893 | MonadTrans (\m => Whirl m e o) where
894 | lift = toWhirl . lift @{ByResult}
897 | Functor m => MonadError e (Whirl m e o) where
898 | throwError = toWhirl . throwError @{ByResult}
899 | catchError wh h = toWhirl $
catchError @{ByResult} (toSwirl wh) (toSwirl . h)