How to use GADT for modules in OCaml without warning?

I have two files: gadt1.ml and gadt2.ml, and the second depends on the first.

gadt1.ml:

type never
type _ t1 = A1 : never  t1 | B1 : bool t1
type _ t2 = A2 : string t2 | B2 : bool t2
let get1 : bool t1 -> bool = function B1 -> true
let get2 : bool t2 -> bool = function B2 -> true

gadt2.ml:

let get1 : bool Gadt1.t1 -> bool = function Gadt.B1 -> true
let get2 : bool Gadt1.t2 -> bool = function Gadt.B2 -> true

when I compile with ocaml 4.02.3 ( ocamlbuild gadt2.native), I get warning 8 that the Gadt2.get1 function is not exhaustive. I am very puzzled that Gadt2.get1triggers an alert, and Gadt1.get1and Gadt2.get2does not.

My assumption was that the empty type nevercannot be equal bool, so Gadt2.get1it should not cause a warning. On the other hand, if I call Gadt2.get1with an argument A1, I get a type error (optional). Is the warning an expected behavior or error? What did I miss?

, -principal .

+4
2

Gadt2 Gadt1, . :

type never
type _ t1 = A1 : never  t1 | B1 : bool t1
type _ t2 = A2 : string t2 | B2 : bool t2
val get1 : bool t1 -> bool
val get2 : bool t2 -> bool

, type never - , , RHS. , , gadt1.ml, type never = bool, A1 get1, get1 .

, string : , bool, A2 get2.

, , , never, , , , . , OCaml; , , , manual. " " .

+4

, , type t , . , , :

module One = struct
  type never = { impossible : 'a . 'a }
  type _ t1 = A1 : never  t1 | B1 : bool t1
  type _ t2 = A2 : string t2 | B2 : bool t2
  let get1 : bool t1 -> bool = function B1 -> true
  let get2 : bool t2 -> bool = function B2 -> true
end

module Two = struct
  let get1 : bool One.t1 -> bool = function One.B1 -> true
  let get2 : bool One.t2 -> bool = function One.B2 -> true
end

.

+2

Source: https://habr.com/ru/post/1611364/


All Articles