-
Notifications
You must be signed in to change notification settings - Fork 2
/
Alternative.re
55 lines (50 loc) · 1.21 KB
/
Alternative.re
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
module type Alternative = {
type t('a);
open Applicative;
include Applicative with type t('a) := t('a);
let empty: t('a);
let (<|>): (t('a), t('a)) => t('a);
};
module AlternativeUtils = (A: Alternative) => {
include A;
module AppU = Applicative.ApplicativeUtils(A);
include (AppU: (module type of AppU) with type t('a) := AppU.t('a));
include Monoid;
module AltMonoid =
(T: GenericTypeConstuctor)
: (Monoid with type t = A.t(T.t)) => {
type t = A.t(T.t);
let empty = A.empty;
let append = A.(<|>);
};
let fail = empty;
let choose = (type a, ps) => {
module AM =
MonoidUtils(
(
AltMonoid(
{
type t = a;
},
)
),
);
AM.concat(ps);
};
};
module ListA_: Alternative with type t('a) = list('a) = {
include Applicative.ListApplicative;
let empty = [];
let (<|>) = (@);
};
module ListAlternative = AlternativeUtils(ListA_);
module OptionA_: Alternative with type t('a) = option('a) = {
include Applicative.OptionApplicative;
let empty = None;
let (<|>) = (x, k) =>
switch (x) {
| None => k
| x => x
};
};
module OptionAlternative = AlternativeUtils(OptionA_);