Search code examples
ocamlalgebraic-data-types

OCaml not accepting type constructor for binary tree


I am trying to construct something like this

type ('a, 'b) btree = Empty | Node of ('a, 'b) * ('a, 'b) btree * ('a, 'b) btree

but it says syntax error around where the first asterisk is.


Solution

  • The type of a pair looks like 'a * 'b so you want this:

    type ('a, 'b) btree = 
      Empty |
      Node of 'a * 'b * ('a, 'b) btree * ('a, 'b) btree
    

    Or possibly this:

    type ('a, 'b) btree = 
      Empty |
      Node of ('a * 'b) * ('a, 'b) btree * ('a, 'b) btree