Error.dcl 3.58 KB
Newer Older
1
definition module Data.Error
2

3 4 5 6
from Data.Functor import class Functor
from Control.Monad import class Monad
from Control.Applicative import class Applicative
from Data.Maybe import :: Maybe
Jeroen Henrix's avatar
Jeroen Henrix committed
7

Camil Staps's avatar
Camil Staps committed
8 9 10 11 12
/**
 * A type representing something that may have failed.
 * @var The error type.
 * @var The value type.
 */
13
:: MaybeError a b = Error a | Ok b 
Jeroen Henrix's avatar
Jeroen Henrix committed
14

Camil Staps's avatar
Camil Staps committed
15 16 17 18
/**
 * Like {{`MaybeError`}} but with {{`String`}} as error type.
 * @var The value type.
 */
19 20
:: MaybeErrorString a :== MaybeError String a

21
instance Functor (MaybeError a)
22 23
instance Applicative (MaybeError a)
instance Monad (MaybeError a)
Jeroen Henrix's avatar
Jeroen Henrix committed
24 25 26 27

/**
 * Return True when the argument is an Ok value and return False otherwise.
 */
28
isOk			:: !(MaybeError a b) -> Bool
Jeroen Henrix's avatar
Jeroen Henrix committed
29 30 31
/**
 * Return True when the argument is an Error value and return False otherwise.
 */
32
isError			:: !(MaybeError a b) -> Bool
Jeroen Henrix's avatar
Jeroen Henrix committed
33 34 35 36

/**
 * Return the contents of an Ok value and abort at run-time otherwise.
 */
37
fromOk			:: !(MaybeError .a .b) -> .b
Jeroen Henrix's avatar
Jeroen Henrix committed
38 39 40 41

/**
 * Return the contents of an Error value and abort at run-time otherwise.
 */
42
fromError		:: !(MaybeError .a .b) -> .a
Jeroen Henrix's avatar
Jeroen Henrix committed
43 44 45 46 47

/**
 * Lifts a (MaybeError a b) to another MaybeError
 * @precondition: isError x == True
 */
48 49 50 51 52 53 54 55 56
liftError 		:: !(MaybeError .a .b) -> (MaybeError .a .c)

/**
 * Converts a Maybe value into a MaybeError value.
 *
 * @param The error used if the input is Nothing
 * @param The Maybe value to be converted
 * @return The resulting MaybeError value
 */
Jurriën Stutterheim's avatar
Jurriën Stutterheim committed
57 58 59 60 61 62
mb2error		:: !e !(Maybe a) -> MaybeError e a

/**
 * Returns st on Error, state continuation on OK
 */
okSt :: *st (.a *st -> *st) (MaybeError .e .a) -> *st
63

64 65 66 67 68 69 70 71
/**
* Converts a MaybeError value into a Maybe value.
* 
* @param The MaybeError value
* @return The converted value. Ok maps to Just and Error maps to Nothing.
*/
error2mb   			:: !(MaybeError e a) -> Maybe a

72 73 74 75 76
/**
 * Sequences an operation on a MaybeError value.
 * If the input is already an Error the operation is not performed.
 *
 * @param The input
77
 * @param The operation on the value (performed if input is Ok)
78 79
 * @return The error of the input or the result of the operation
 */
80
seqErrors :: !(MaybeError e a) (a -> MaybeError e b) -> MaybeError e b
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112

/**
 * Combines two MaybeError values.
 * If one of the input is an Error, this Error is given as result (If both are, the first is given).
 *
 * @param The first input
 * @param The second input
 * @param A combination function for the inputs if they are Ok
 * @return The error of one of the inputs or the result of the combination
 */
combineErrors :: !(MaybeError e a) (MaybeError e b) (a b -> MaybeError e c) -> MaybeError e c

/**
 * Sequences two operations on a state, yielding MaybeError values.
 * If the first operation already yields an error, the second is not performed.
 *
 * @param The first operation
 * @param The second operation, getting the result of the first as input
 * @return The Error of the first or the second operation
 */
seqErrorsSt :: !(.st -> (MaybeError e a,!.st)) (a .st -> u:(!MaybeError e b, !.st)) !.st -> v:(MaybeError e b, !.st), [u <= v]	

/**
 * Combines two MaybeError values, resulting from two operations on a state.
 * If one of the operations yields an Error, this Error is given as result (If both are, the first is given).
 *
 * @param The first operation
 * @param The second operation
 * @param A combination function for the inputs if they are Ok
 * @return The error of one of the operations or the result of the combination
 */
combineErrorsSt :: !(.st -> (!MaybeError e a, !.st)) (.st -> (!MaybeError e b, !.st)) (a b -> MaybeError e c) !.st -> (!MaybeError e c, !.st)