Advertisement
Guest User

Untitled

a guest
Nov 8th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 2.43 KB | None | 0 0
  1. module Packed = struct
  2.   type t =
  3.     | A of int
  4.     | B of string
  5.     | C of int * int
  6. end
  7.  
  8. module Unpacked = struct
  9.  
  10.   type _ t =
  11.     | A : int t
  12.     | B : string t
  13.     | C : (int * int) t
  14.  
  15.   type 'b consumer = { apply : 'a. 'a t -> 'a -> 'b }
  16.   type packed = T : 'a t * 'a -> packed
  17. end
  18.  
  19. (* Here's some example client code. The client is
  20.  * writing a library that loops over user input and
  21.  * repeatedly hands off control to a callback with
  22.  * the appropriate variant. We can do this with
  23.  * allocation only in the C case.
  24.  *)
  25. let consume (f : unit Unpacked.consumer) =
  26.   let rec loop () =
  27.     begin
  28.       match read_line () with
  29.       | "a" -> f.apply A (read_int ())
  30.       | "b" -> f.apply B (read_line ())
  31.       | "c" ->
  32.           (* right-to-left, who gives a shit *)
  33.           f.apply C (read_int (), read_int ())
  34.       | _ -> assert false
  35.     end;
  36.     loop ()
  37.   in loop ()
  38.  
  39. (* And you can store it in a data structure like this: *)
  40. let () =
  41.   let open Unpacked in
  42.   let example =
  43.     [ T (A, 3)
  44.     ; T (B, "3")
  45.     ; T (C, (1, 2))
  46.     ] in
  47.   List.iter (function
  48.     | T (A, x) -> Printf.printf "%d\n" x
  49.     | T (B, x) -> Printf.printf "%s\n" x
  50.     | T (C, x) -> Printf.printf "%d %d\n" (fst x) (snd x))
  51.     example
  52.  
  53. module Unpacked2 = struct
  54.   type (_, 'b) t =
  55.     | A : (int -> 'b, 'b) t
  56.     | B : (string -> 'b, 'b) t
  57.     | C : (int -> int -> 'b, 'b) t
  58.  
  59.   type 'b consumer = { apply : 'a. ('a, 'b) t -> 'a }
  60.   type 'b a_little_packed = T : ('a, 'b) t * ('a -> 'b) -> 'b a_little_packed
  61.   type very_packed = { data : 'b. 'b a_little_packed }
  62. end
  63.  
  64. let consume (f : unit Unpacked2.consumer) =
  65.   let rec loop () =
  66.     begin
  67.       match read_line () with
  68.       | "a" -> f.apply A (read_int ())
  69.       | "b" -> f.apply B (read_line ())
  70.       (* Holly shit It's curreid. It's curried I mean. *)
  71.       | "c" -> f.apply C (read_int ()) (read_int ())
  72.       | _ -> assert false
  73.     end;
  74.     loop ()
  75.   in loop ()
  76.  
  77. (* But how the ****** do you store it in a data structure? *)
  78. let () =
  79.   let open Unpacked2 in
  80.   let example =
  81.     [ { data = T (A, fun f -> f 3) }
  82.     ; { data = T (B, fun f -> f "3") }
  83.     ; { data = T (C, fun f -> f 1 2) }
  84.     ]
  85.   in
  86.   List.iter (fun x ->
  87.     match (x.data : unit a_little_packed) with
  88.     | T (A, f) -> f (Printf.printf "%d\n")
  89.     | T (B, f) -> f (Printf.printf "%s\n")
  90.     (* ???? its curried?????? *)
  91.     | T (C, f) -> f (Printf.printf "%d %d\n"))
  92.     example
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement