Coccinelle release 0.2.5-rc3
[bpt/coccinelle.git] / commons / interfaces.ml
CommitLineData
34e49164
C
1open Common.BasicType
2
3(*****************************************************************************)
4(* TypeClass via module signature. *)
5(*****************************************************************************)
6(*
7 * Use this not so much for functors, I hate functors, but
ae4735db
C
8 * more to force me to have consistent naming of stuff.
9 *
34e49164 10 * It's related to objet.ml in some way, but use a different scheme.
ae4735db
C
11 *
12 * src: (strongly) inspired by Jane Street core lib, which in turn
34e49164
C
13 * may have been strongly inspired by Java Interfaces or Haskell
14 * TypeClass.
ae4735db
C
15 *
16 *
17 *
34e49164 18 * Example of use in .mli:
ae4735db 19 *
34e49164
C
20 * open Interfaces
21 * include Stringable with type stringable = t
22 * include Comparable with type comparable = t
ae4735db 23 *
34e49164 24 * Example of use in .ml:
ae4735db 25 *
34e49164
C
26 * type xxx
27 * type stringable = xxx
28 * let of_string = bool_of_string
29 * let to_string = string_of_bool
ae4735db
C
30 *
31 *
34e49164 32 * No this file is not about (graphical) user interface. See gui.ml for that.
ae4735db
C
33 *
34 *
35 * todo? but as in type class, or object, can not have default method
34e49164
C
36 * with this scheme ?
37 *)
38
39
40
41(*****************************************************************************)
42(* Basic *)
43(*****************************************************************************)
44
45(* note: less need for cloneable, copyable as in Java. Only needed
46 * when use ref, but refs should be avoided anyway so better not to
47 * encourage it.
ae4735db 48 *
34e49164 49 * Often found this in haskell:
ae4735db 50 *
34e49164 51 * data x = ... deriving (Read, Show, Eq, Ord, Enum, Bounded)
ae4735db 52 *
34e49164
C
53 * Apparently this is what is considered basic by haskell.
54 *)
55
56
57module type Check_able = sig
58 type checkable
59 val invariant: checkable -> unit (* raise exception *)
60end
61
62
63
64(* Normally should not use the '=' of ocaml. cf common.mli on this issue. *)
65module type Eq_able = sig
66 type eqable
67 val equal : eqable -> eqable -> bool
68 (* Jane Street have far more (complex) stuff for this typeclass *)
69
70 val (=*=): eqable -> eqable -> bool
71end
72
73
74
ae4735db 75(* Same, should not use compare normally, dangerous when evolve code.
34e49164
C
76 * Called Ord in haskell. Inherit Eq normally.
77 *)
78module type Compare_able = sig
79 type compareable
80 val compare: compareable -> compareable -> bool
81end
82(* Jane street have also some binable, sexpable *)
83
84
ae4735db 85(* Haskell have lots of related type class after Num such as
34e49164
C
86 * Real, Fractional, Integral, RealFrac, Floating, RealFloat
87 *)
88module type Num_able = sig
89 type numable
90 (* +, -, etc *)
91end
92
93
94
95(*****************************************************************************)
96(* Show/read related *)
97(*****************************************************************************)
98
99
100(* Called show/read in haskell *)
101module type String_able = sig
102 type stringable
103 val of_string : string -> stringable
104 val to_string : stringable -> string
105end
106
107module type Debug_able = sig
108 type debugable
109 val debug: debugable -> string
110end
111
112
113module type XML_able = sig
114 type xmlable
115 val of_xml: string -> xmlable
116 val to_xml: xmlable -> string
117end
118(* Jane street have also some BIN_able, and SEXP_able (but no sex_able) *)
119
120module type File_able = sig
121 type fileable
122 val load: filename -> fileable
123 val save: fileable -> filename -> unit
124end
125
126(* a.k.a Marshall_able *)
127module type Serialize_able = sig
128 type serializeable
129 val serialize: serializeable -> string
130 val unserialize: string -> serializeable
131end
132
133
134module type Open_able = sig
135 type openable
136 val openfile: filename -> openable
137 val close: openable -> unit
138end
139
140(*****************************************************************************)
141(* Other *)
142(*****************************************************************************)
143
144(* This is related to ocollection.ml in some way, but use a different scheme *)
145
146(* Require Constructor class ? So can not do it ? apparently can. Note the
147 * 'b which is not declareted but seems to pose no problem to ocamlc.
148 *)
149module type Map_able = sig
150 type 'a mapable
151 val map: ('a -> 'b) -> 'a mapable -> 'b mapable
152end
153
154module type Iter_able = sig
155 type 'a iterable
156 val iter: ('a -> unit) -> 'a iterable -> unit
157end
158
159
160(* testable ? actionable ? *)
161
162(* *)
163
164(* monad ? functor *)
165
166
167
168(*****************************************************************************)
169(* Idea taken from Jane Street Core library, slightly changed.
ae4735db
C
170 *
171 * It's another way to organize data structures, module instead of objects.
172 * It's also the Java way.
173 *
34e49164 174 * It makes some code looks a little bit like Haskell* typeclass.
ae4735db 175 *
34e49164
C
176 *)
177
178(* In Jane Street they put each interface in its own file but then have to
179 * do that:
ae4735db 180 *
34e49164
C
181 * module type Stringable = Stringable.S
182 * module type Comparable = Comparable.S
183 * module type Floatable = Floatable.S
184 * module type Hashable = Hashable.S
185 * module type Infix_comparators = Comparable.Infix
186 * module type Monad = Monad.S
187 * module type Robustly_comparable = Robustly_comparable.S
188 * module type Setable = Setable.S
189 * module type Sexpable = Sexpable.S
190 * module type Binable = Binable.S
ae4735db 191 *
34e49164
C
192 * And I dont like having too much files, especially as all those xxable
193 * end with able, not start, so don't see them together in the directory.
194 *)