Created
March 5, 2013 15:07
-
-
Save aspiwack/5090936 to your computer and use it in GitHub Desktop.
Circumventing OCaml's lack of empty pattern-matching cases when using gadts.
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
type void = { ex_falso : 'a.'a } | |
module Eq = struct | |
(** Leibniz equality. *) | |
type (_,_) t = Refl : ('a,'a) t | |
(** Variant of equality with a dummy case used to prove disequalities. *) | |
type (_,_) u = Refl' : ('a,'a) u | Dummy : void -> ('a,'b) u | |
let to_u : type a b. (a,b) t -> (a,b) u = function | |
| Refl -> Refl' | |
let of_u : type a b. (a,b) u -> (a,b) t = function | |
| Refl' -> Refl | |
| Dummy {ex_falso} -> ex_falso | |
end | |
type x = X | |
type y = Y | |
(** We should be able to [match x] with an empty list of cases, as | |
there are no valid case. However, there is no way to express such a | |
situation in OCaml. Instead we match on the dummy case. *) | |
let disequal_x_y : (x,y) Eq.t -> void = fun e -> | |
match Eq.to_u e with | |
| Eq.Dummy {ex_falso} -> ex_falso |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment