Skip to content

Instantly share code, notes, and snippets.

@m2ym
Created April 8, 2015 09:01
Show Gist options
  • Save m2ym/70475023c03e6d298449 to your computer and use it in GitHub Desktop.
Save m2ym/70475023c03e6d298449 to your computer and use it in GitHub Desktop.
Try to implement Haskell's show in OCaml using ppx_overload
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
@m2ym
Copy link
Author

m2ym commented Apr 8, 2015

なるほど、 with type a = A.a * B.a でうまく関連できるのですね。ぜんぜん分かっていませんでした。 show (1, 2.3)

let x : ('a, 'b) = (1, 2.3) in
let module M =
  Tuple
    (struct
       type t = 'a
       let show x = Show.show x
     end)
    (struct
       type t = 'b
       let show x = Show.show x
     end)
in M.show x

みたいにできれば勝利ですが、外部から 'a'b は入れられないんですよね。 (type a) とかで型パラメータをうまく導入してやればいけるかもしれません。もう少し考えてみます。

@camlspotter
Copy link

ちょっと違う件ですが、 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)

@m2ym
Copy link
Author

m2ym commented Apr 9, 2015

なるほど。参考になります。

Copy link

ghost commented May 6, 2015

Why not use english:(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment