Skip to content

Instantly share code, notes, and snippets.

@Kakadu
Created April 16, 2016 16:34
Show Gist options
  • Save Kakadu/fd8535e7fa3d4f2ef80352a552913c0d to your computer and use it in GitHub Desktop.
Save Kakadu/fd8535e7fa3d4f2ef80352a552913c0d to your computer and use it in GitHub Desktop.
Test implicits with pairs.
➜ miniKanren-ob git:(ocamlbuild) ✗ cat test_pair.ml | nl -ba
1 open Printf
2
3 let make_buffer () = Buffer.create 20
4 let bprintf b = Printf.ksprintf (Buffer.add_string b)
5
6 module type SHOW = sig
7 type t
8 val show : t -> string
9 end
10
11 let show {S : SHOW} x = S.show x;;
12
13 implicit module Show_float = struct
14 type t = float
15 let show x = sprintf "%f" x
16 end
17
18 module Show_list_impl {X : SHOW} = struct
19 type t = X.t list
20 let show xs =
21 let b = Buffer.create 30 in
22 bprintf b "Show_list: [";
23 List.iter (fun x -> bprintf b "%s" @@ X.show x) xs;
24 bprintf b "]%!"
25 end
26
27 implicit module Show_list = Show_list_impl
28
29 implicit module Show_int = struct
30 type t = int
31 let show x = sprintf "%d" x
32 end
33
34 implicit module Show_string = struct
35 type t = string
36 let show x = x
37 end
38
39 implicit module Show_pair {X : SHOW} {Y : SHOW} = struct
40 type t = X.t * Y.t
41 let show (x,y) = sprintf "(%s,%s)" (X.show x) (Y.show y)
42 end
43
44
45 implicit module Show_string_int = struct
46 type t = string * int
47 let show (x,y) = sprintf "(%s,%d)" x y
48 end
49
50
51 let () = print_endline @@ show ([ ("a", 1)] : (string*int) list )
➜ miniKanren-ob git:(ocamlbuild) ✗ ocaml test_pair.ml
File "test_pair.ml", line 51, characters 26-30:
Error: No instance found for implicit S.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment