Last active
December 13, 2016 15:12
-
-
Save keigoi/9ce156cc49e0832c02b526e8e71012bd to your computer and use it in GitHub Desktop.
This file contains 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
(* try this with 4.02.0+modular-implicits *) | |
module type Show = sig | |
type t | |
val show : t -> string | |
end | |
implicit module Lam{X:Show} = struct | |
type t = [`Var of string | `App of X.t * X.t | `Abs of string * X.t] | |
let show : t -> string = function | |
|`Var(x) -> x | |
|`App(t1,t2) -> "(" ^ X.show t1 ^ " " ^ X.show t2 ^ ")" | |
|`Abs(x,t) -> "(λ" ^ x ^ "." ^ X.show t ^ ")" | |
end | |
let print {M:Show} x = print_endline (M.show x) | |
;; | |
type t = [`Var of string | `App of 'a * 'a | `Abs of string * 'a] as 'a | |
let rec v : t = `Abs("x",`App(v,`Var("x")));; | |
let _ = print v;; (* Error: Termination check failed when searching for implicit M. *) | |
(* try this on http://andrewray.github.io/iocamljs/modimp_show.html *) | |
module type Show = sig | |
type t | |
val show : t -> unit | |
end | |
let print (implicit S : Show) x = S.show x | |
module Lam(X:Show) = struct | |
type t = [`Var of string | `App of X.t * X.t | `Abs of string * X.t] | |
let show = function | |
|`Var(x) -> print_string x | |
|`App(t1,t2) -> print_string "(" ; X.show t1 ; print_string " " ; X.show t2 ; print_string ")" | |
|`Abs(x,t) -> print_string "(λ" ; print_string x ; print_string "." ; X.show t ; print_string ")" | |
end | |
module rec LamF : Show | |
with type t = [`Var of string | `App of 'a * 'a | `Abs of string * 'a] as 'a = | |
Lam(LamF) | |
implicit module LamF_ = LamF;; | |
print (`Var("x") : LamF.t);; (* OK *) | |
print (`Var("x"));; (* FAIL *) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment