File: deriving_Functor.ml

package info (click to toggle)
ocaml-deriving-ocsigen 0.6.2-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 628 kB
  • ctags: 1,159
  • sloc: ml: 6,334; makefile: 63; sh: 18
file content (57 lines) | stat: -rw-r--r-- 1,227 bytes parent folder | download | duplicates (2)
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
open Deriving_monad

(* Copyright Jeremy Yallop 2007.
   This file is free software, distributed under the MIT license.
   See the file COPYING for details.
*)

module type Functor = 
sig
  type 'a f
  val map : ('a -> 'b) -> 'a f -> 'b f
end

module MonadFunctor (M : Monad) 
  : Functor with type 'a f = 'a M.m
=
struct
  open M
  type 'a f = 'a M.m
  let map  f x = x >>= (fun x -> return (f x))
end


module Functor_option = MonadFunctor(Monad_option)
module Functor_list = MonadFunctor(Monad_list)

module Functor_map (O : Map.OrderedType) 
  : Functor with type 'a f = 'a Map.Make(O).t =
struct 
  include Map.Make(O)
  type 'a f = 'a t
end

(*
NB: Instances for mutable types (including

   ref
   queue
   stack
   array
   stream
   buffer)

are deliberately omitted.  Since sharing is detectable for values of
these types we have two distinct design choices:

  1. Always create a new copy that shares no structure with the
     original.

  2. Always mutate the original copy

Neither of these seems like the right thing to do, so instead we
simply don't handle mutable types at all.

(Lazy.t is another example: we'd like map to be total and side-effect
free, which is impossible to guarantee if we handle lazy.
*)