0
votes

I have the following purescript code:

class Node a where
  parentNode :: forall b. (Node b) => a -> b

but when compiling this I get the following error:

A cycle appears in the definition of type synonym Node
Cycles are disallowed because they can lead to loops in the type checker.
Consider using a 'newtype' instead.

I am trying to write a function parentNode that returns the parent node of a node. The only guarantee for the parent node is that it is also a Node b. I do not care what the actual type for b is.

I am basically trying to say, parentNode should be a function that returns a value that also implements the Node typeclass. Is something like this possible with type classes or is there some other idiomatic way to do this type of thing?

1

1 Answers

0
votes

The type of your parentNode function says that the caller can choose the type b of the parent, but I think this is incorrect. The type should be determined by what's in the DOM, so you need an existential type.

The technical issue here is that type classes cannot currently refer to themselves.

However, in this case, I think there is a simpler solution which doesn't use classes. Why not something like this?

class IsNode a where
  toNode :: a -> Node

foreign import data Node :: *

foreign import parentNode :: Node -> Node