Avoiding the Y combinator for self-referencing types

Suppose I've got some named objects which reference other objects by name:
data NodeS = NodeS {nameS :: String, refsS :: [String]}
Through name resolution, the strings are translated to the actual nodes they denote:
data Node = Node {name :: String, refs :: [Node]} resolve :: [NodeS] -> Map String Node
NodeS and Node are quite similar, so they should probably be the same parametrized type. However, if I turn the type of the references into a type parameter, I need the type-level Y combinator to avoid an infinite type, and manipulating nodes turns a bit more tedious because of increased syntactic overhead. Is there a third choice, beyond manually expanding the type definition or using Y?

Not sure if that's what you need:
data NodeF f = Node {name :: String, refs :: [f (NodeF f)]}
newtype Const a b = Const a
newtype Id a = Id a
type NodeS = NodeF (Const String)
type Node = NodeF Id
Отправлено с iPhone
Dec 12, 2010, в 20:54, Florian Weimer
Suppose I've got some named objects which reference other objects by name:
data NodeS = NodeS {nameS :: String, refsS :: [String]}
Through name resolution, the strings are translated to the actual nodes they denote:
data Node = Node {name :: String, refs :: [Node]} resolve :: [NodeS] -> Map String Node
NodeS and Node are quite similar, so they should probably be the same parametrized type. However, if I turn the type of the references into a type parameter, I need the type-level Y combinator to avoid an infinite type, and manipulating nodes turns a bit more tedious because of increased syntactic overhead.
Is there a third choice, beyond manually expanding the type definition or using Y?
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

* Miguel Mitrofanov:
Not sure if that's what you need:
data NodeF f = Node {name :: String, refs :: [f (NodeF f)]}
newtype Const a b = Const a newtype Id a = Id a
type NodeS = NodeF (Const String) type Node = NodeF Id
Thanks for the suggestion. Yes, the resulting syntax looks better, and it is more obvious to me what is going on.
participants (2)
-
Florian Weimer
-
Miguel Mitrofanov