Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GADT definition

Tags:

ocaml

gadt

This is just a test so I'm not much concerned, but I have these definitions:

type z
type _ s
type (_, _, _) balance =
  | Less : (*∀'a.*) ('a, 'a s, 'a s) balance
  | Same : (*∀'b.*) ('b, 'b, 'b) balance
  | More : (*∀'a.*) ('a s, 'a, 'a s) balance
type _ aVL =
  | Leaf : z aVL
  | Node : (*∀'a, 'b, 'c.*)('a, 'b, 'c) balance * 'a aVL * int * 'b aVL ->
    ('c s) aVL

and I get the error for "type _ aVL =":

Error: In this definition, a type variable cannot be deduced
       from the type parameters.

What to do?

like image 651
lukstafi Avatar asked Dec 19 '13 22:12

lukstafi


Video Answer


1 Answers

H/T to Gabriel Scherer for answering at caml-list.

Don't use this kind of abstract type definition. Use instead (and export) concrete definitions (even if you don't use their constructors for anything)

type 'a s = S of 'a

(or just type 'a s = S)

They have "better" injectivity properties. We've mentioned in on the mailing-list a couple of time, and it's also the "easy take-away lesson" from Jacques Garrigue talk at the OCaml workshop in September.

Shame on me for not googling for the problem. Here the exact problem is addressed: GADTs : a type variable cannot be deduced

like image 61
lukstafi Avatar answered Oct 05 '22 12:10

lukstafi