Empty tree type in OCaml

I am trying to create a type that represents an empty binary tree (basically just its skeleton). Variables of this type are then designed to be re-matched against the pattern.

I understand how to instantiate fixed from polymorphic variant for standard types ( int

, string

etc.) - see below int_tree

. However, it is not clear if it is possible to make an empty variant from polymorphic (the line empty_tree

below is not executed with SyntaxError during the compilation process).

The code looks like this:

type 'a binary_tree =                                                                                                                
  | Leaf of 'a                                                                                                                       
  | Node of 'a binary_tree * 'a * 'a binary_tree                                                                                     

type int_tree = int binary_tree;;                                                                                                    

type empty_tree = () binary_tree;;  

      

+3


source to share


1 answer


()

is not a type, but the only value of a type unit

.

Recording () binary_tree

is like recording 0 binary_tree

(instead of int binary_tree

).



Your empty tree must be of type unit binary_tree

.

+5


source







All Articles