-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathgeneric_util_monoid.ml
More file actions
76 lines (65 loc) · 1.28 KB
/
Copy pathgeneric_util_monoid.ml
File metadata and controls
76 lines (65 loc) · 1.28 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
(** Monoid type and instances. *)
(** A monoid is given by a neutral element and a binary
operation that should satisfy the laws:
{[
mappend x mempty = x
mappend mempty x = x
]}
*)
module T = struct
type 't monoid =
{ mempty : 't
; mappend : 't -> 't -> 't
}
end
include T
type 't t = 't monoid
(** Additive monoid. *)
let int_sum =
{ mempty = 0
; mappend = ( + )
}
(** Multiplicative monoid. *)
let int_prod =
{ mempty = 1
; mappend = ( * )
}
(** Additive monoid. *)
let float_sum =
{ mempty = 0.0
; mappend = ( +. )
}
(** Multiplicative monoid. *)
let float_prod =
{ mempty = 1.0
; mappend = ( *. )
}
(** Boolean monoid under conjunction. *)
let all =
{ mempty = true
; mappend = ( && )
}
(** Boolean monoid under disjunction. *)
let any =
{ mempty = false
; mappend = ( || )
}
(** Option monoid returning the leftmost non-Nothing value. *)
let first =
{ mempty = None
; mappend = fun x y -> match x , y with
| None , x -> x
| Some _ as x , _ -> x
}
(** Option monoid returning the rightmost non-Nothing value. *)
let last =
{ mempty = None
; mappend = fun x y -> match x , y with
| x , None -> x
| _ , (Some _ as x) -> x
}
(** List monoid *)
let list =
{ mempty = []
; mappend = (fun x y -> x @ y)
}