listMonad.ml 865 Bytes
Newer Older
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
type 'a m = 'a list

let return x = 
  [ x ] 

let bind l f = 
  List.flatten (List.map f l)

let ( >>= ) l f = 
  bind l f

(* 
   1. (return x) >>= f == f x

   bind [ x ] f 
   = List.flatten (List.map f [ x ]) 
   = f x 

   2. m >>= return == m

   bind l return 
   = List.flatten (List.map (fun x -> [ x ]) (x1::x2::..::xn))
   = List.flatten ([x1]::...::[xn]) 
   = x1::...::xn
   = l

   3. (m >>= f) >>= g == m >>= (\x -> f x >>= g)
   
   bind (bind l f) g
   = List.flatten (List.map g (List.flatten (List.map f (x1::...::xn))))
   = List.flatten (List.map g (f x1 :: f x2 :: ... :: f xn))
   = List.flatten (List.map g ([fx1_1; fx1_2 ... ] :: [fx2_1; ... ] :: ...))
   = List.flatten ([ g fx1_1; g fx_1_2 ... ] :: [ g fx_2_1; ... ] ...)
   = List.flatten (List.map (fun x -> List.flatten (List.map g (f x))) l)
   = bind l (fun x -> bind (f x) g)

*)