Skip to content

Instantly share code, notes, and snippets.

@stedolan
Created March 11, 2021 13:14
Show Gist options
  • Save stedolan/54d851c41886f8b2fbacd03cf40b6ec7 to your computer and use it in GitHub Desktop.
Save stedolan/54d851c41886f8b2fbacd03cf40b6ec7 to your computer and use it in GitHub Desktop.
Failure of transitivity in OCaml's subtyping
(* OCaml's subtyping relation is not always transitive *)
type (_,_) eq = Refl : ('a, 'a) eq
type 'a inty = int
type s = < f : int -> int >
type t = < f : 'a . 'a inty -> 'a inty >
(* s and t are equal and subtype each other *)
let eq_s_t : (s, t) eq = Refl
let sub_s_t x = (x : s :> t)
let sub_t_s x = (x : t :> s)
(* But only one of them is a supertype of poly *)
type poly = < f : 'a . 'a -> 'a >
let sub_poly_s x = (x : poly :> s)
let sub_poly_t x = (x : poly :> t)
(* Error: Type poly = < f : 'a. 'a -> 'a > is not a subtype of
t = < f : 'a. int -> int >
Type int is not a subtype of 'a *)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment