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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
|
open! Import
module Test_applicative_s (A : Applicative.S with type 'a t := 'a Or_error.t) :
Applicative.S with type 'a t := 'a Or_error.t = struct
let error = Or_error.error_string
let return = A.return
let%expect_test _ =
print_s [%sexp (return "okay" : string Or_error.t)];
[%expect {| (Ok okay) |}]
;;
let apply = A.apply
let%expect_test _ =
let test x y = print_s [%sexp (apply x y : string Or_error.t)] in
test (Ok String.capitalize) (Ok "okay");
[%expect {| (Ok Okay) |}];
test (error "not okay") (Ok "okay");
[%expect {| (Error "not okay") |}];
test (Ok String.capitalize) (error "not okay");
[%expect {| (Error "not okay") |}];
test (error "no fun") (error "no arg");
[%expect {| (Error ("no fun" "no arg")) |}]
;;
let ( <*> ) = A.( <*> )
let%expect_test _ =
let test x y = print_s [%sexp (x <*> y : string Or_error.t)] in
test (Ok String.capitalize) (Ok "okay");
[%expect {| (Ok Okay) |}];
test (error "not okay") (Ok "okay");
[%expect {| (Error "not okay") |}];
test (Ok String.capitalize) (error "not okay");
[%expect {| (Error "not okay") |}];
test (error "no fun") (error "no arg");
[%expect {| (Error ("no fun" "no arg")) |}]
;;
let ( *> ) = A.( *> )
let%expect_test _ =
let test x y = print_s [%sexp (x *> y : string Or_error.t)] in
test (Ok ()) (Ok "kay");
[%expect {| (Ok kay) |}];
test (error "not okay") (Ok "kay");
[%expect {| (Error "not okay") |}];
test (Ok ()) (error "not okay");
[%expect {| (Error "not okay") |}];
test (error "no fst") (error "no snd");
[%expect {| (Error ("no fst" "no snd")) |}]
;;
let ( <* ) = A.( <* )
let%expect_test _ =
let test x y = print_s [%sexp (x <* y : string Or_error.t)] in
test (Ok "okay") (Ok ());
[%expect {| (Ok okay) |}];
test (error "not okay") (Ok ());
[%expect {| (Error "not okay") |}];
test (Ok "okay") (error "not okay");
[%expect {| (Error "not okay") |}];
test (error "no fst") (error "no snd");
[%expect {| (Error ("no fst" "no snd")) |}]
;;
let both = A.both
let%expect_test _ =
let test x y = print_s [%sexp (both x y : (string * string) Or_error.t)] in
test (Ok "o") (Ok "kay");
[%expect {| (Ok (o kay)) |}];
test (error "not okay") (Ok "kay");
[%expect {| (Error "not okay") |}];
test (Ok "o") (error "not okay");
[%expect {| (Error "not okay") |}];
test (error "no fst") (error "no snd");
[%expect {| (Error ("no fst" "no snd")) |}]
;;
let map = A.map
let%expect_test _ =
let test x = print_s [%sexp (map x ~f:String.capitalize : string Or_error.t)] in
test (Ok "okay");
[%expect {| (Ok Okay) |}];
test (error "not okay");
[%expect {| (Error "not okay") |}]
;;
let ( >>| ) = A.( >>| )
let%expect_test _ =
let test x = print_s [%sexp (x >>| String.capitalize : string Or_error.t)] in
test (Ok "okay");
[%expect {| (Ok Okay) |}];
test (error "not okay");
[%expect {| (Error "not okay") |}]
;;
let map2 = A.map2
let%expect_test _ =
let test x y = print_s [%sexp (map2 x y ~f:( ^ ) : string Or_error.t)] in
test (Ok "o") (Ok "kay");
[%expect {| (Ok okay) |}];
test (error "not okay") (Ok "kay");
[%expect {| (Error "not okay") |}];
test (Ok "o") (error "not okay");
[%expect {| (Error "not okay") |}];
test (error "no fst") (error "no snd");
[%expect {| (Error ("no fst" "no snd")) |}]
;;
let map3 = A.map3
let%expect_test _ =
let test x y z =
print_s [%sexp (map3 x y z ~f:(fun a b c -> a ^ b ^ c) : string Or_error.t)]
in
test (Ok "o") (Ok "k") (Ok "ay");
[%expect {| (Ok okay) |}];
test (error "not okay") (Ok "k") (Ok "ay");
[%expect {| (Error "not okay") |}];
test (Ok "o") (error "not okay") (Ok "ay");
[%expect {| (Error "not okay") |}];
test (Ok "o") (Ok "k") (error "not okay");
[%expect {| (Error "not okay") |}];
test (error "no 1st") (error "no 2nd") (error "no 3rd");
[%expect {| (Error ("no 1st" "no 2nd" "no 3rd")) |}]
;;
let all = A.all
let%expect_test _ =
let test list = print_s [%sexp (all list : string list Or_error.t)] in
test [];
[%expect {| (Ok ()) |}];
test [ Ok "okay" ];
[%expect {| (Ok (okay)) |}];
test [ Ok "o"; Ok "kay" ];
[%expect {| (Ok (o kay)) |}];
test [ Ok "o"; Ok "k"; Ok "ay" ];
[%expect {| (Ok (o k ay)) |}];
test [ error "oh no!" ];
[%expect {| (Error "oh no!") |}];
test [ error "oh no!"; Ok "okay" ];
[%expect {| (Error "oh no!") |}];
test [ Ok "okay"; error "oh no!" ];
[%expect {| (Error "oh no!") |}];
test [ error "oh no!"; Ok "o"; Ok "kay" ];
[%expect {| (Error "oh no!") |}];
test [ Ok "o"; error "oh no!"; Ok "aay" ];
[%expect {| (Error "oh no!") |}];
test [ Ok "o"; Ok "kay"; error "oh no!" ];
[%expect {| (Error "oh no!") |}];
test [ error "oh"; error "no"; error "!" ];
[%expect {| (Error (oh no !)) |}]
;;
let all_unit = A.all_unit
let%expect_test _ =
let test list = print_s [%sexp (all_unit list : unit Or_error.t)] in
test [];
[%expect {| (Ok ()) |}];
test [ Ok () ];
[%expect {| (Ok ()) |}];
test [ Ok (); Ok () ];
[%expect {| (Ok ()) |}];
test [ Ok (); Ok (); Ok () ];
[%expect {| (Ok ()) |}];
test [ error "oh no!" ];
[%expect {| (Error "oh no!") |}];
test [ error "oh no!"; Ok () ];
[%expect {| (Error "oh no!") |}];
test [ Ok (); error "oh no!" ];
[%expect {| (Error "oh no!") |}];
test [ error "oh no!"; Ok (); Ok () ];
[%expect {| (Error "oh no!") |}];
test [ Ok (); error "oh no!"; Ok () ];
[%expect {| (Error "oh no!") |}];
test [ Ok (); Ok (); error "oh no!" ];
[%expect {| (Error "oh no!") |}];
test [ error "oh"; error "no"; error "!" ];
[%expect {| (Error (oh no !)) |}]
;;
module Applicative_infix = A.Applicative_infix
end
let%test_module "Make" =
(module Test_applicative_s (Applicative.Make (struct
type 'a t = 'a Or_error.t
let return = Or_error.return
let apply = Or_error.apply
let map = `Define_using_apply
end)))
;;
let%test_module "Make" =
(module Test_applicative_s (Applicative.Make_using_map2 (struct
type 'a t = 'a Or_error.t
let return = Or_error.return
let map2 = Or_error.map2
let map = `Define_using_map2
end)))
;;
let%test_module "Make" =
(module Test_applicative_s (Applicative.Make_using_map2_local (struct
type 'a t = 'a Or_error.t
let return x = Ok x
let map2 = Or_error.map2
let map = `Define_using_map2
end)))
;;
(* While law-abiding applicatives shouldn't be relying functions being called
the minimal number of times, it is good for performance that things be this
way. For many applicatives this will not matter very much, but for others,
like Bonsai, it is a little more significant, since extra calls construct
more Incremental nodes, yielding more strain on the Incremental stabilizer.
The point is that we should not assume that the input applicative instance
can be frivolous in creating nodes in the applicative call-tree.
*)
let%expect_test _ =
let module A = struct
type 'a t =
| Other of string
| Return : 'a -> 'a t
| Map : ('a -> 'b) * 'a t -> 'b t
| Map2 : ('a -> 'b -> 'c) * 'a t * 'b t -> 'c t
include Applicative.Make_using_map2 (struct
type nonrec 'a t = 'a t
let return x = Return x
let map2 a b ~f = Map2 (f, a, b)
let map = `Custom (fun a ~f -> Map (f, a))
end)
let rec sexp_of_t : type a. a t -> Sexp.t = function
| Other x -> Atom x
| Return _ -> Atom "Return"
| Map (_, a) -> List [ Atom "Map"; sexp_of_t a ]
| Map2 (_, a, b) -> List [ Atom "Map2"; sexp_of_t a; sexp_of_t b ]
;;
end
in
let open A in
let test x = print_s [%sexp (x : A.t)] in
let a, b, c, d = Other "A", Other "B", Other "C", Other "D" in
test (map2 a b ~f:(fun a b -> a, b));
[%expect {| (Map2 A B) |}];
test (both a b);
[%expect {| (Map2 A B) |}];
test (all_unit [ a; b; c; d ]);
[%expect {| (Map2 (Map2 (Map2 (Map2 Return A) B) C) D) |}];
test (a *> b);
[%expect {| (Map2 A B) |}]
;;
(* These functors serve only to check that the signatures for various Foo and Foo2 module
types don't drift apart over time. *)
module _ = struct
open Applicative
(* Applicative_infix to Applicative_infix2 *)
module _ (X : Applicative_infix) : Applicative_infix2 with type ('a, 'e) t = 'a X.t =
struct
include X
type ('a, 'e) t = 'a X.t
end
(* Applicative_infix2 to Applicative_infix *)
module _ (X : Applicative_infix2) : Applicative_infix with type 'a t = ('a, unit) X.t =
struct
include X
type 'a t = ('a, unit) X.t
end
(* Applicative_infix2 to Applicative_infix3 *)
module _ (X : Applicative_infix2) :
Applicative_infix3 with type ('a, 'd, 'e) t = ('a, 'd) X.t = struct
include X
type ('a, 'd, 'e) t = ('a, 'd) X.t
end
(* Applicative_infix3 to Applicative_infix2 *)
module _ (X : Applicative_infix3) :
Applicative_infix2 with type ('a, 'd) t = ('a, 'd, unit) X.t = struct
include X
type ('a, 'd) t = ('a, 'd, unit) X.t
end
(* Let_syntax to Let_syntax2 *)
module _ (X : Let_syntax) : Let_syntax2 with type ('a, 'e) t = 'a X.t = struct
include X
type ('a, 'e) t = 'a X.t
end
(* Let_syntax2 to Let_syntax *)
module _ (X : Let_syntax2) : Let_syntax with type 'a t = ('a, unit) X.t = struct
include X
type 'a t = ('a, unit) X.t
end
(* Let_syntax2 to Let_syntax3 *)
module _ (X : Let_syntax2) : Let_syntax3 with type ('a, 'd, 'e) t = ('a, 'd) X.t =
struct
include X
type ('a, 'd, 'e) t = ('a, 'd) X.t
end
(* Let_syntax3 to Let_syntax2 *)
module _ (X : Let_syntax3) : Let_syntax2 with type ('a, 'd) t = ('a, 'd, unit) X.t =
struct
include X
type ('a, 'd) t = ('a, 'd, unit) X.t
end
end
|