]>
Edgar Friendly <thelema314@gmail.com> writes:
> When one writes
>
> type row = int
> type col = int
>
> This allows one to use the type names "row" and "col" as synonyms of
> int. But it doesn't prevent one from using a value of type row in the
> place of a value of type col. OCaml allows us to enforce row as
> distinct from int two ways:
>
> 1) Variants:
> type row = Row of int
> type col = Col of int
>
> Downside: unnecessary boxing and tagging
> conversion from row -> int: (fun r -> match r with Row i -> i)
> conversion from int -> row: (fun i -> Row i)
I agree with Gerd, this is probably the least-effort solution. If I'm
not making mistake, even with the new feature introduced by Pierre (Btw,
really nice feature!), you still have to make the restriction at the
module level. I.e., only the outside world of current module will
benefit from this protection, and if you want to use this protection
right now, you still have to wrap type row/col as modules.
You may consider using variant with the syntax convention in Gerd's
post, or you can consider using record. E.g.
type row = {r:int} and col = {c:int}
then the "from" method are just record construction like {c=3} and "to"
method are just field access like x.r.
On efficiency, note that if your single variant (record) has multiple
fields, e.g. "type row = Row of int * int", this encoding doesn't
introduce more (un)boxing than "type row = int * int". The difference
happens when the single variant (record) has single field itself. There
was discussion on this topic before. I do agree that eliminating extra
(un)boxing on single variant type (and single immutable field record
type) would be nice.
> 2) Functors:
> module type RowCol =
> sig
> type row
> val int_of_row : row -> int
> val row_of_int : int -> row
> type col
> val int_of_col : col -> int
> val col_of_int : int -> col
> end
>
> module Main = functor (RC: RowCol) -> struct
> (* REST OF PROGRAM HERE *)
> end
>
If you want to make the protection effective in "current" module, you
anyway needs some module-level abstraction in current OCaml. The following
functor tries to do the simple wrapping for any type, and can be defined
once for all:
module type ANY = sig type t end
module type ABS = sig type t type nat val box: nat -> t val unbox: t -> nat end
module Abstr(T:ANY) : ABS with type nat = T.t = struct
type t = T.t and nat = T.t
let box x = x and unbox x = x
end
(* Test *)
# module Row = Abstr(struct type t = int end)
# module Col = Abstr(struct type t = int end)
# let x = Row.box 3 and y = Col.box 5;;
val x : Row.t = <abstr>
val y : Col.t = <abstr>
# let f x y = Row.unbox x + Col.unbox y;;
val f : Row.t -> Col.t -> int = <fun>
--
Zheng Li
http://www.pps.jussieu.fr/~li