<commit_before>module Control.IOExcept

-- An IO monad with exception handling

data IOExcept : Type -> Type -> Type where
     IOM : IO (Either err a) -> IOExcept err a

instance Functor (IOExcept e) where
     map f (IOM fn) = IOM (map (map f) fn)

instance Applicative (IOExcept e) where
     pure x = IOM (pure (pure x))
     (IOM f) <*> (IOM a) = IOM [| f <*> a |]

instance Monad (IOExcept e) where
     (IOM x) >>= k = IOM (do x' <- x;
                             case x' of
                                  Right a => let (IOM ka) = k a in
                                                 ka
                                  Left err => return (Left err))

ioe_lift : IO a -> IOExcept err a
ioe_lift op = IOM $ map Right op

ioe_fail : err -> IOExcept err a
ioe_fail e = IOM $ pure (Left e)

ioe_run : IOExcept err a -> (err -> IO b) -> (a -> IO b) -> IO b
ioe_run (IOM act) err ok = either err ok !act<commit_msg>Turn IOException into a record to avoid the let-binding patmatch.<commit_after>module Control.IOExcept

-- An IO monad with exception handling

record IOExcept err a where
     constructor IOM
     runIOExcept : IO (Either err a)

instance Functor (IOExcept e) where
     map f (IOM fn) = IOM (map (map f) fn)

instance Applicative (IOExcept e) where
     pure x = IOM (pure (pure x))
     (IOM f) <*> (IOM a) = IOM [| f <*> a |]

instance Monad (IOExcept e) where
     (IOM x) >>= f = IOM $ x >>= either (pure . Left) (runIOExcept . f)

ioe_lift : IO a -> IOExcept err a
ioe_lift op = IOM $ map Right op

ioe_fail : err -> IOExcept err a
ioe_fail e = IOM $ pure (Left e)

ioe_run : IOExcept err a -> (err -> IO b) -> (a -> IO b) -> IO b
ioe_run (IOM act) err ok = either err ok !act
{'fraction_non_alphanumeric': 0.1129976580796253, 'fraction_numerical': 0.0, 'mean_word_length': 2.0792792792792794, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5191926', 'n_tokens_mistral': 617, 'n_tokens_neox': 581, 'n_words': 246}