Func.dcl 1.81 KB
Newer Older
1
definition module Data.Func
2

Camil Staps's avatar
Camil Staps committed
3
from Data.Functor import class Functor
4
from Control.Applicative import class pure, class <*>, class Applicative
Camil Staps's avatar
Camil Staps committed
5
from Control.Monad import class Monad
6
from Data.Monoid import class Semigroup, class Monoid
Camil Staps's avatar
Camil Staps committed
7 8

instance Functor ((->) r)
9
instance pure ((->) r) where pure :: !a -> b -> a
10
instance <*> ((->) r)
Camil Staps's avatar
Camil Staps committed
11
instance Monad ((->) r)
Camil Staps's avatar
Camil Staps committed
12

13 14 15
instance Semigroup (a -> b) | Semigroup b
instance Monoid (a -> b) | Monoid b

Camil Staps's avatar
Camil Staps committed
16 17 18 19
/**
 * Function application.
 * @type (a -> b) a -> b
 */
Camil Staps's avatar
Camil Staps committed
20 21 22
($) infixr 0
($) f :== f

Mart Lubbers's avatar
Mart Lubbers committed
23 24 25 26 27 28
/**
 * If function
 * @type Bool a a -> a
 */
if` c t e :== if c t e

Camil Staps's avatar
Camil Staps committed
29 30 31 32
/**
 * Function application.
 * @type a -> a
 */
Camil Staps's avatar
Camil Staps committed
33 34
app f :== f

Camil Staps's avatar
Camil Staps committed
35 36 37 38 39 40 41 42 43
/**
 * Apply a state function to a list of values.
 * See also {{`mapSt`}}.
 *
 * @param The function.
 * @param The list of values.
 * @param The initial state.
 * @result The final state.
 */
Jurriën Stutterheim's avatar
Jurriën Stutterheim committed
44
seqSt        :: !(a .st -> .st)       ![a] !.st -> .st
Camil Staps's avatar
Camil Staps committed
45 46 47 48 49 50 51 52 53 54

/**
 * Apply a state function to a list of values and return the results.
 * See also {{`seqSt`}}.
 *
 * @param The function.
 * @param The list of values.
 * @param The initial state.
 * @result The value results and the final state.
 */
55
mapSt        :: !(a .st -> (b,.st)) ![a] !.st -> (![b],.st)
Camil Staps's avatar
Camil Staps committed
56 57 58
/**
 * The fixed point combinator, reducing `fix f` to `f (fix f)`.
 */
59
fix          :: !(a -> a) -> a
Camil Staps's avatar
Camil Staps committed
60 61 62 63 64

/**
 * Apply a binary function on another domain.
 *
 * Typical usage: `sortBy (on (<) toInt) :: [a] -> [a] | toInt a`
Camil Staps's avatar
Camil Staps committed
65
 * Or infix: `sortBy ((<) `on` toInt) :: [a] -> [a] | toInt a`
Camil Staps's avatar
Camil Staps committed
66
 */
Jurriën Stutterheim's avatar
Jurriën Stutterheim committed
67
on           :: (b b -> c) (a -> b) -> (a a -> c)
Mart Lubbers's avatar
Mart Lubbers committed
68

Camil Staps's avatar
Camil Staps committed
69 70
/**
 * Infix version of {{`on`}}.
Camil Staps's avatar
Camil Staps committed
71
 * @type (b b -> c) (a -> b) -> (a a -> c)
Camil Staps's avatar
Camil Staps committed
72 73 74 75
 */
(`on`) infixl 0
(`on`) :== on

Camil Staps's avatar
Camil Staps committed
76 77 78
/**
 * Completely evaluate an expression (not just to head normal form like strictness).
 */
79
hyperstrict  :: !.a -> .a