0
votes

I've defined a haskell data type with constructors and a type synonym for it.I'm not able to use its data constructors.

Type definition :

module BinaryTreeModule(BinaryTree,..) where
data BinaryTree a = Empty | Node (BinaryTree a) a (BinaryTree a)

type synonym:

import BinaryTreeModule(BinaryTree)
type BST = BinaryTree

usage:

insert :: a -> BST a -> BST a
insert _ Empty = ...
insert _ Node .. = ...

I'm getting an error saying Empty and Node constructors not found.

I've also defined them in different files. If BST is just a synonym for BinaryTree shouldn't their constructors be the same ?

Also is there a better way to "inherit" the BinaryTree type to different types? Are typeclasses the correct solution here?

Edit: added how I'm importing between files

1
Are you exposing both the Empty and Node constructors from your BinaryTree module? - Chad Gilbert
and on top are you importing them as well import MyModule(BinaryTree) vs. import MyModule(BinaryTree(..)) - also note that import MyModule(BST) is the same as the first! - Random Dev
Okay, I'm importing like import MyModule(BinaryTree). How do i import the data constructors i.e. BinaryTree(..) - VigneshM
Thanks for the help! - VigneshM
Type synonyms are of only fairly limited utility in Haskell. There are some special situations in lens and pipes and probably elsewhere where they're valuable, but in typical circumstances, you probably just shouldn't use them. GHC's type families are much more often useful, but they're a more advanced feature. - dfeuer

1 Answers

2
votes

After @Carsten's comment, I realised I'm not importing the constructors from the module

import BinaryTreeModule(BinaryTree(Empty,Node)) fixed the issue