1{-# OPTIONS_GHC -Wall #-}
2module Reporting.Result where
4import Control.Monad.Except (Except, runExcept)
6import qualified Reporting.Annotation as A
7import qualified Reporting.Bag as Bag
8import qualified Reporting.Region as R
15data Result info warning error result =
18 , _warnings :: LBag warning
19 , _answer :: Answer (LBag error) result
23data Answer e a = Ok a | Err e
30data One a = None | One a
37ok :: (Monoid i) => a -> Result i w e a
39 Result mempty Bag.empty (Ok value)
42throw :: (Monoid i) => R.Region -> e -> Result i w e a
44 Result mempty Bag.empty (Err (Bag.singleton (A.A region err)))
47throwMany :: (Monoid i) => [A.Located e] -> Result i w e a
49 Result mempty Bag.empty (Err (Bag.fromList errors))
52accumulate :: (Monoid i) => i -> a -> Result i w e a
53accumulate info value =
54 Result info Bag.empty (Ok value)
57warn :: (Monoid i) => R.Region -> w -> a -> Result i w e a
58warn region warning a =
59 Result mempty (Bag.singleton (A.A region warning)) (Ok a)
66from :: (Monoid i) => (e -> e') -> Except [A.Located e] a -> Result i w e' a
68 case runExcept except of
73 throwMany (map (A.map f) errors)
76mapError :: (e -> e') -> Result i w e a -> Result i w e' a
77mapError f (Result info warnings rawResult) =
78 Result info warnings $
84 Err (Bag.map (A.map f) msgs)
87format :: (Monoid i) => (e -> e') -> Result () w e a -> Result i w e' a
88format func (Result _ warnings answer) =
89 mapError func (Result mempty warnings answer)
92answerToEither :: (A.Located e -> e') -> (a -> a') -> Answer (LBag e) a -> Either [e'] a'
93answerToEither onErr onOk answer =
99 Left (Bag.toList onErr errors)
102oneToValue :: b -> (a -> b) -> One a -> b
103oneToValue fallback onOne one =
113-- EXTRA FANCY HELPERS
116instance Functor (Result i w e) where
117 fmap func (Result info warnings rawResult) =
120 Result info warnings (Ok (func a))
123 Result info warnings (Err msgs)
126instance (Monoid i) => Applicative (Result i w e) where
130 (<*>) (Result info warnings resultFunc) (Result info' warnings' resultVal) =
131 Result (mappend info info') (Bag.append warnings warnings') $
132 case (resultFunc, resultVal) of
136 (Err msgs, Err msgs') ->
137 Err (Bag.append msgs msgs')
146instance (Monoid i) => Monad (Result i w e) where
150 (>>=) (Result info warnings rawResult) callback =
153 Result info warnings (Err msg)
157 (Result info' warnings' rawResult') =
162 (Bag.append warnings warnings')
166instance Monoid (One a) where
171 case (left, right) of
179 error "There should only be one!"
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list