deriving (Enum) now uses Deriving_Enum module [#1]
[deriving.git] / lib / functor.ml
blobef41d2e372b95776bc51a7e2efdd069bdd245a1f
1 open Monad
3 (* Copyright Jeremy Yallop 2007.
4 This file is free software, distributed under the MIT license.
5 See the file COPYING for details.
6 *)
8 module type Functor =
9 sig
10 type 'a f
11 val map : ('a -> 'b) -> 'a f -> 'b f
12 end
14 module MonadFunctor (M : Monad)
15 : Functor with type 'a f = 'a M.m
17 struct
18 open M
19 type 'a f = 'a M.m
20 let map f x = x >>= (fun x -> return (f x))
21 end
24 module Functor_option = MonadFunctor(Monad.Monad_option)
25 module Functor_list = MonadFunctor(Monad.Monad_list)
27 module Functor_map (O : Map.OrderedType)
28 : Functor with type 'a f = 'a Map.Make(O).t =
29 struct
30 include Map.Make(O)
31 type 'a f = 'a t
32 end
35 NB: Instances for mutable types (including
37 ref
38 queue
39 stack
40 array
41 stream
42 buffer)
44 are deliberately omitted. Since sharing is detectable for values of
45 these types we have two distinct design choices:
47 1. Always create a new copy that shares no structure with the
48 original.
50 2. Always mutate the original copy
52 Neither of these seems like the right thing to do, so instead we
53 simply don't handle mustable types at all.
55 (Lazy.t is another example: we'd like map to be total and side-effect
56 free, which is impossible to guarantee if we handle lazy.