😫 stumped why a type alias won't check in my tree data structure


#1

Do I need (type a) somewhere?

Code

Reason Playground

Offender

let parent = branch(children);

Error

We've found a bug for you!
OCaml preview 23:21-28

This has type:
  int Tree.General.tree list
But somewhere wanted:
  'a Tree.General.children

Given

The following recursive type definition implementing the Tree signature:

type tree('a) =
  | Leaf('a)
  | Branch(children('a))
and children('a) = list(tree('a));

Workaround

Replacing type children('a) with list(tree('a)) compiles but I can’t implement a module Tree.Binary without it.

:cherries: pretty please enlighten me!


#2

My suggestion when you’re just getting started is not to write down the signatures explicitly; many get it wrong. Remove it, hover over the module to see the actual inferred signature, then gradually remove what you wanted to hide


#3

Removing the signature does fix it. Now I understand. The definition of children is opaque in Tree.Tree. I think I need a functor to pass in children type and a polymorphic variant to share tree type. Thank you @chenglou!!!


#4

My solution if anyone cares to critique.