Source file profunctor.ml
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
77
78
79
80
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
open Base
include Profunctor_intf.Interfaces
module Record_builder_internal
(F : S) (T : sig
type 'a profunctor_term
val prj : ('a, 'a) F.t -> 'a profunctor_term
val inj : 'a profunctor_term -> ('a, 'a) F.t
end) =
struct
include T
type ('b, 'a) profunctor = ('b, 'a) F.t
module Bare = Record_builder.Make_2 (F)
let field term field = Bare.field (F.contra_map (inj term) ~f:(Field.get field)) field
let build_for_record f = prj (Bare.build_for_record f)
end
module Record_builder (F : S) =
Record_builder_internal
(F)
(struct
type 'a profunctor_term = ('a, 'a) F.t
let prj = Fn.id
let inj = Fn.id
end)
module Fn_with_id = struct
module T = struct
type ('b, 'a) t =
| Id : ('a, 'a) t
| Apply : ('a -> 'b) -> ('b, 'a) t
let map (type a b c) (x : (b, a) t) ~(f : b -> c) : (c, a) t =
match x with
| Id -> Apply f
| Apply g -> Apply (Fn.compose f g)
;;
let contra_map (type a b c) (x : (c, b) t) ~(f : a -> b) : (c, a) t =
match x with
| Id -> Apply f
| Apply g -> Apply (Fn.compose g f)
;;
let as_fn' (type a b) (x : (b, a) t) : a -> b =
match x with
| Id -> Fn.id
| Apply f -> f
;;
let both l r =
let l = as_fn' l
and r = as_fn' r in
Apply (fun x -> l x, r x)
;;
end
include T
let split (type a b c d) (l : (b, a) t) (r : (d, c) t) : (b * d, a * c) t =
match l, r with
| Id, Id -> Id
| _, _ ->
let l = as_fn' l
and r = as_fn' r in
Apply (fun (x, y) -> l x, r y)
;;
let id = Id
let of_fn x = Apply x
let as_fn t = Staged.stage (as_fn' t)
let compose (type a b c) (g : (c, b) t) (f : (b, a) t) : (c, a) t =
match g, f with
| Id, Id -> Id
| Id, f -> f
| g, Id -> g
| Apply g, Apply f -> Apply (Fn.compose g f)
;;
module Of_record = Record_builder (T)
end
module Of_applicative (F : Applicative.S) = struct
module T = struct
type ('b, 'a) t = 'b F.t
let contra_map x ~f:_ = x
let map = F.map
let both = F.both
end
include T
module Of_record =
Record_builder_internal
(T)
(struct
type 'a profunctor_term = 'a F.t
let inj = Fn.id
let prj = Fn.id
end)
end
module Of_conv_based (F : Conv_based) = struct
module T = struct
type ('c, 'a) t =
| Embed :
{ pre_map : ('b, 'a) Fn_with_id.t
; inner : 'b F.t
; post_map : ('c, 'b) Fn_with_id.t
}
-> ('c, 'a) t
let contra_map (Embed x) ~f =
Embed { x with pre_map = Fn_with_id.contra_map x.pre_map ~f }
;;
let map (Embed x) ~f = Embed { x with post_map = Fn_with_id.map x.post_map ~f }
let both (Embed l) (Embed r) =
Embed
{ pre_map = Fn_with_id.both l.pre_map r.pre_map
; inner = F.both l.inner r.inner
; post_map = Fn_with_id.split l.post_map r.post_map
}
;;
end
include T
let inj inner = Embed { pre_map = Fn_with_id.Id; inner; post_map = Fn_with_id.Id }
let prj (Embed x) =
F.conv x.inner (Fn_with_id.as_fn' x.post_map) (Fn_with_id.as_fn' x.pre_map)
;;
module Of_record =
Record_builder_internal
(T)
(struct
type 'a profunctor_term = 'a F.t
let inj = inj
let prj = prj
end)
end