Debian Codegraph @

test/test.ml

raw symbols SCIP
1 module Monad_example = struct
2 module X : sig
3 type 'a t
4
5 module Let_syntax : sig
6 val return : 'a -> 'a t
7
8 module Let_syntax : sig
9 val return : 'a -> 'a t
10 val bind : 'a t -> f:('a -> 'b t) -> 'b t
11 val map : 'a t -> f:('a -> 'b) -> 'b t
12 val both : 'a t -> 'b t -> ('a * 'b) t
13
14 module Open_on_rhs : sig
15 val return : 'a -> 'a t
16 end
17 end
18 end
19 end = struct
20 type 'a t = 'a
21
22 let return x = x
23 let bind x ~f = f x
24 let map x ~f = f x
25 let both x y = x, y
26
27 module Let_syntax = struct
28 let return = return
29
30 module Let_syntax = struct
31 let return = return
32 let bind = bind
33 let map = map
34 let both = both
35
36 module Open_on_rhs = struct
37 let return = return
38 end
39 end
40 end
41 end
42
43 open X.Let_syntax
44
45 let _mf a : _ X.t =
46 let%bind_open x = a in
47 return (x + 1)
48 ;;
49
50 let _mf' a b c : _ X.t =
51 let%bind_open x = a
52 and y = b
53 and u, v = c in
54 return (x + y + (u * v))
55 ;;
56
57 let _mg a : _ X.t =
58 let%map x : int X.t = a in
59 x + 1
60 ;;
61
62 let _mg' a b c : _ X.t =
63 let%map x = a
64 and y = b
65 and u, v = c in
66 x + y + (u * v)
67 ;;
68
69 let _mh a : _ X.t =
70 match%bind_open a with
71 | 0 -> return true
72 | _ -> return false
73 ;;
74
75 let _mi a : _ X.t =
76 match%map a with
77 | 0 -> true
78 | _ -> false
79 ;;
80
81 let _mif a : _ X.t = if%bind_open a then return true else return false
82 let _mif' a : _ X.t = if%map a then true else false
83
84 let _mj : int X.t -> bool X.t =
85 function%bind
86 | 0 -> return true
87 | _ -> return false
88 ;;
89
90 let _mk : int X.t -> bool X.t =
91 function%map
92 | 0 -> true
93 | _ -> false
94 ;;
95 end
96
97 module Applicative_example = struct
98 module X : sig
99 type 'a t
100
101 module Let_syntax : sig
102 val return : 'a -> 'a t
103
104 module Let_syntax : sig
105 val return : 'a -> 'a t
106 val map : 'a t -> f:('a -> 'b) -> 'b t
107 val both : 'a t -> 'b t -> ('a * 'b) t
108
109 module Open_on_rhs : sig
110 val flag : int t
111 val anon : int t
112 end
113 end
114 end
115 end = struct
116 type 'a t = 'a
117
118 let return x = x
119 let map x ~f = f x
120 let both x y = x, y
121
122 module Let_syntax = struct
123 let return = return
124
125 module Let_syntax = struct
126 let return = return
127 let map = map
128 let both = both
129
130 module Open_on_rhs = struct
131 let flag = 66
132 let anon = 77
133 end
134 end
135 end
136 end
137
138 open X.Let_syntax
139
140 (* {[
141 let _af a : _ X.t =
142 let%bind x = a in (* "Error: Unbound value Let_syntax.bind" *)
143 return (x + 1)
144 ]} *)
145
146 (* {[
147 let _af' a b c : _ X.t =
148 let%bind x = a and y = b and (u, v) = c in (* "Error: Unbound value Let_syntax.bind" *)
149 return (x + y + (u * v))
150 ]} *)
151
152 let _ag a : _ X.t =
153 let%map x = a in
154 x + 1
155 ;;
156
157 let _ag' a b c : _ X.t =
158 let%map x = a
159 and y = b
160 and u, v = c in
161 x + y + (u * v)
162 ;;
163
164 (* {[
165 let _ah a : _ X.t =
166 match%bind a with (* "Error: Unbound value Let_syntax.bind" *)
167 | 0 -> return true
168 | _ -> return false
169 ]} *)
170
171 let _ai a : _ X.t =
172 match%map a with
173 | 0 -> true
174 | _ -> false
175 ;;
176 end
177
178 module Example_without_open = struct
179 let _ag a : _ Applicative_example.X.t =
180 let%map.Applicative_example.X x = a in
181 x + 1
182 ;;
183 end
184
185 module Example_with_mapn = struct
186 module Let_syntax = struct
187 let return = Monad_example.X.Let_syntax.return
188
189 module Let_syntax = struct
190 include Monad_example.X.Let_syntax.Let_syntax
191
192 let map2 a b ~f = map (both a b) ~f:(fun (a, b) -> f a b)
193 let map3 a b c ~f = map2 (both a b) c ~f:(fun (a, b) c -> f a b c)
194 let map4 a b c d ~f = map2 (both a b) (both c d) ~f:(fun (a, b) (c, d) -> f a b c d)
195 end
196 end
197
198 let _x =
199 let open Let_syntax in
200 let%mapn a = return 1
201 and b = return "hi"
202 and c = return 2.34
203 and d = return true in
204 Printf.sprintf "%d %s %f %b" a b c d
205 ;;
206 end
207
208 let () =
209 (* Use this code to test if :MerlinTypeOf behaves properly. In particular,
210 [lhs_*] variables should have type 'a instead of 'a Monad_example.X.t. *)
211 let open Monad_example.X.Let_syntax in
212 let rhs_a = return 1 in
213 let rhs_b = return 1. in
214 let rhs_c = return 'c' in
215 let (_ : _) =
216 (* Non-parallel sequence of binds. *)
217 let%bind lhs_a = rhs_a in
218 let%bind lhs_b = rhs_b in
219 let%bind lhs_c = rhs_c in
220 return (lhs_a, lhs_b, lhs_c)
221 in
222 let tuple =
223 (* Parallel bind. *)
224 let%bind lhs_a = rhs_a
225 and lhs_b = rhs_b
226 and lhs_c = rhs_c in
227 return (lhs_a, lhs_b, lhs_c)
228 in
229 let (_ : _) =
230 (* Destructuring parallel bind. *)
231 let%bind lhs_a, lhs_b, lhs_c = tuple
232 and lhs_a', lhs_b', lhs_c' = tuple in
233 return (lhs_a, lhs_b, lhs_c, lhs_a', lhs_b', lhs_c')
234 in
235 let (_ : _) =
236 (* Body is a function *)
237 let%map lhs_a, lhs_b, lhs_c = tuple in
238 fun () -> lhs_c, lhs_b, lhs_a
239 in
240 ()
241 ;;