Advertisement
Guest User

Untitled

a guest
Sep 12th, 2014
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module type SHOW =
  2. sig
  3. type t
  4. val show : t -> string
  5. end
  6.  
  7. let print (implicit Show : SHOW) x = Show.show x
  8.  
  9. implicit module ShowInt = struct type t = int let show = string_of_int end
  10. implicit module ShowFloat = struct type t = float let show = string_of_float end
  11.  
  12. implicit functor ShowPair (Show1 : SHOW) (Show2 : SHOW) =
  13. struct
  14. type t = Show1.t * Show2.t
  15. let show (x, y) = "(" ^ Show1.show x ^ ", " ^ Show2.show y ^ ")"
  16. end
  17.  
  18. (* none of these work *)
  19.  
  20. (* this works *)
  21. let rec grow_pairs : 'a. (implicit Show : SHOW with type t = 'a) -> Show.t -> 'b =
  22. fun (type a) (implicit Show : SHOW with type t = a) x ->
  23. let module Show2 = ShowPair (Show) (Show) in
  24. grow_pairs (implicit Show2) (x, x)
  25.  
  26. (* but this doesn't, and the error message is strange *)
  27. let rec grow_pairs2 : 'a. (implicit Show : SHOW with type t = 'a) -> 'a -> 'b =
  28. fun (type a) (implicit Show : SHOW with type t = a) x ->
  29. let module Show2 = ShowPair (Show) (Show) in
  30. grow_pairs2 (implicit Show2) (x, x)
  31. (*
  32. Error: This expression has type
  33. (implicit Show : SHOW with type t = 'a) -> Show.t -> 'b
  34. but an expression was expected of type
  35. (implicit Show : SHOW with type t = 'a) -> 'a -> 'b
  36. The type variable 'a occurs inside 'a
  37. *)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement