Source file monad.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
open Base

include Monad_intf

module Make2(M : S2) : T2 with type ('a, 'z) t := ('a, 'z) M.t = struct
  module S = M

  include S
    
  module EX = struct
    let fmap f t = bind t & fun x -> return (f x)
    let liftM = fmap
  
    let fmap2 f t u = bind t & fun t -> bind u & fun u -> return & f t u
    let liftM2 = fmap2
  
    let void a = bind a & fun _ -> return ()
  
    let rec seq = function
      | [] -> return []
      | x::xs ->
          bind x & fun x ->
          bind (seq xs) & fun xs ->
          return (x::xs)
  
    let rec seq_ = function
      | [] -> return ()
      | x::xs -> bind x & fun () -> seq_ xs
  
    let mapM f ls = seq (List.map f ls)
  
    let rec mapM_ f = function
      | [] -> return ()
      | x::xs -> bind (f x) & fun () -> mapM_ f xs
  
    let iteri f ls = seq_ (List.mapi f ls)
  
    let rec for_ i to_ f =
      if i > to_ then return ()
      else bind (f i) & fun () -> for_ (i+1) to_ f
      
    let join tt = bind tt & fun at -> at
  end

  include EX
    
  module Infix = struct
    let (>>=) = M.bind

    let (>>|) t f = fmap f t

    (* Applicative style *)
    let ( ^<$> ) f t = fmap f t 
  
    let ( /<*> ) = fun f a ->
      f >>= fun f -> 
      a >>= fun a ->
      return (f a)
  end

  include Infix
end
  
module Make1(M : S1) : T1 with type 'a t = 'a M.t = struct
  type 'a t = 'a M.t
  type ('a, 'dummy) m = 'a t
  module M2 = struct
    type nonrec ('a, _) t = 'a t
    include (M : S1 with type 'a t := 'a M.t) (* hiding 'a t *)
  end
  include (Make2(M2) : T2 with type ('a, 'dummy) t := ('a, 'dummy) m)
end

module Make = Make1