(************************************************************ cast.ml Created : Fri Sep 13 10:35:47 2002 Last modified: Fri Sep 13 11:05:17 2002 Compile: ocamlopt.opt cast.ml -o cast # Documentation: ocamldoc.opt -html -d doc cast.ml # ************************************************************) (** 多相型からsignature適用によって外的に特定の型へ @author Takashi Masuyama *) module type SIGNATURE = sig type t (* opaque *) val int_of_t: t -> int val t_of_int: int -> t end module Cast = struct type t = int (* SIGNATUREで制約するから型はかかないや。 *) (* どちらも型を明示しないと同じ式になる *) let int_of_t a = a let t_of_int a = a end module A = (Cast : SIGNATURE) (** セッション例 {v # #use "cast.ml";; module type SIGNATURE = sig type t val int_of_t : t -> int val t_of_int : int -> t end module Cast : sig type t = int val int_of_t : 'a -> 'a val t_of_int : 'a -> 'a end module A : SIGNATURE # A.int_of_t 1;; Characters 11-12: A.int_of_t 1;; ^ This expression has type int but is here used with type A.t # A.int_of_t (1:A.t);; (* 外から抽象型を乗っとることはできない *) Characters 12-13: A.int_of_t (1:A.t);; ^ This expression has type int but is here used with type A.t # A.t_of_int 1;; (* int -> t *) A.t = # A.int_of_t (A.t_of_int 1);; (* もとに戻す *) int = 1 v} *) (* - : A.t = などでOcamldocがエラー。なぜ? *)