Created
April 8, 2015 09:01
-
-
Save m2ym/70475023c03e6d298449 to your computer and use it in GitHub Desktop.
Try to implement Haskell's show in OCaml using ppx_overload
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module type Show = sig | |
val show : 'a -> string | |
end | |
module Show = struct | |
external show : 'a -> string = "%OVERLOADED" | |
module Int = struct | |
let show = string_of_int | |
end | |
module Float = struct | |
let show = string_of_float | |
end | |
(* too ambiguous *) | |
module Tuple2 = struct | |
let show (a, b) = show a ^ ", " ^ show b | |
end | |
(* functor? *) | |
module Tuple2 (A : Show) (B : Show) = struct | |
let show (a, b) = A.show a ^ ", " ^ B.show b | |
end | |
end |
ちょっと違う件ですが、 ppx_overload だけでもこんなの書けます
module type Num = sig
type a
val (+) : a -> a -> a
end
module Num = struct
external (+) : 'a -> 'a -> 'a = "%OVERLOADED"
module Int = struct
type a = int
let (+) = Pervasives.(+)
end
module Float = struct
type a = float
let (+) = Pervasives.(+.)
end
end
let () = assert Num.(1.2 + 3.4 = 4.6)
(* Trial of double : fails due to "too ambiguous" *)
(* let double x = Num.(x + x) *)
let double (type a) a (x : a) =
(* Extend Num with a *)
let module Num = struct
include Num
module A = (val a : Num with type a = a)
end in
Num.(x + x) (* No need to tell which (+) we should use. Nice! *)
(* We need explicit application of the instance of Num float... *)
let () = assert (double (module Num.Float) 1.2 = 2.4)
なるほど。参考になります。
Why not use english:(
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
なるほど、
with type a = A.a * B.a
でうまく関連できるのですね。ぜんぜん分かっていませんでした。show (1, 2.3)
をみたいにできれば勝利ですが、外部から
'a
や'b
は入れられないんですよね。(type a)
とかで型パラメータをうまく導入してやればいけるかもしれません。もう少し考えてみます。