on master help sign in
Merge pull request #2776 from LeifW/ioexcept_golf

Ioexcept golf

commit76b1675cd45f45bed2e0dd47e5c4d42ca6c62959 authorEdwin Brady <edwin.brady@gmail.com> dateSun 8 Nov 2015, 10:55 (11 years ago) parent12f79aa272efea
4 files changed+16−20
libs/base/Control/Catchable.idr+2−2 libs/base/Control/IOExcept.idr+8−17 libs/effects/Effect/Exception.idr+1−1 libs/effects/Effects.idr+5
libs/base/Control/Catchable.idr+2−2
@@ -16,14 +16,14 @@ instance Catchable (Either a) a where
1616 catch (Left err) h = h err
1717 catch (Right x) h = (Right x)
1818
19− throw x = Left x
19+ throw = Left
2020
2121instance Catchable (IOExcept err) err where
2222 catch (IOM prog) h = IOM (do p' <- prog
2323 case p' of
2424 Left e => let IOM he = h e in he
2525 Right val => return (Right val))
26− throw x = IOM (return (Left x))
26+ throw = ioe_fail
2727
2828instance Catchable List () where
2929 catch [] h = h ()
libs/base/Control/IOExcept.idr+8−17
@@ -2,34 +2,25 @@ module Control.IOExcept
22
33-- An IO monad with exception handling
44
5−data IOExcept : Type -> Type -> Type where
6− IOM : IO (Either err a) -> IOExcept err a
5+record IOExcept err a where
6+ constructor IOM
7+ runIOExcept : IO (Either err a)
78
89instance Functor (IOExcept e) where
910 map f (IOM fn) = IOM (map (map f) fn)
1011
1112instance Applicative (IOExcept e) where
1213 pure x = IOM (pure (pure x))
13− (IOM f) <*> (IOM a) = IOM (do f' <- f; a' <- a
14− return (f' <*> a'))
14+ (IOM f) <*> (IOM a) = IOM [| f <*> a |]
1515
1616instance Monad (IOExcept e) where
17− (IOM x) >>= k = IOM (do x' <- x;
18− case x' of
19− Right a => let (IOM ka) = k a in
20− ka
21− Left err => return (Left err))
17+ (IOM x) >>= f = IOM $ x >>= either (pure . Left) (runIOExcept . f)
2218
2319ioe_lift : IO a -> IOExcept err a
24−ioe_lift op = IOM (do op' <- op
25− return (Right op'))
20+ioe_lift op = IOM $ map Right op
2621
2722ioe_fail : err -> IOExcept err a
28−ioe_fail e = IOM (return (Left e))
23+ioe_fail e = IOM $ pure (Left e)
2924
3025ioe_run : IOExcept err a -> (err -> IO b) -> (a -> IO b) -> IO b
31−ioe_run (IOM act) err ok = do act' <- act
32− case act' of
33− Left e => err e
34− Right v => ok v
35−
26+ioe_run (IOM act) err ok = either err ok !act
libs/effects/Effect/Exception.idr+1−1
@@ -18,7 +18,7 @@ instance Show a => Handler (Exception a) IO where
1818 believe_me (exit 1)
1919
2020instance Handler (Exception a) (IOExcept a) where
21− handle _ (Raise e) k = IOM (return (Left e))
21+ handle _ (Raise e) k = ioe_fail e
2222
2323instance Handler (Exception a) (Either a) where
2424 handle _ (Raise e) k = Left e
libs/effects/Effects.idr+5
@@ -245,6 +245,11 @@ pureM = Value
245245(<*>) prog v = do fn <- prog
246246 arg <- v
247247 return (fn arg)
248+
249+(*>) : EffM m a xs (\v => xs) ->
250+ EffM m b xs (\v => xs) -> EffM m b xs (\v => xs)
251+a *> b = do a
252+ b
248253
249254new : Handler e' m => (e : EFFECT) -> resTy ->
250255 {auto prf : e = MkEff resTy e'} ->