[Haskell-cafe] How to unify these three types with identical structures into one definition?

ducis ducis_cn at 126.com
Fri Dec 14 12:56:28 UTC 2018


Hello,

Say I have a certain type for parse trees:
data Expr = Var String
   | Enclosed String Expr String
   | Prefix Expr Expr
   | Ternary Expr String Expr String Expr
   deriving (Show,Eq,Ord,Generic)--,Typeable)


Then I want a new type where every occurence of "Expr" definition changed to "[Expr]":
data ExprL = VarL String

   | EnclosedL String [Expr] String
   | PrefixL [Expr] [Expr]
   | TernaryL [Expr] String [Expr] String [Expr]
   deriving (Show,Eq,Ord,Generic)


Sometimes, I also want to use a DList instead of list.

data ExprD = VarD String
   | EnclosedD String (DList Expr) String
   | PrefixD (DList Expr) (DList Expr)
   | TernaryD (DList Expr) String (DList Expr) String (DList Expr)
   deriving (Show,Eq,Ord,Generic)


They have exactly the same structure, is there a way to unify the three definitions into one?
Furthurmore, is it possible to generalise the latter two for all Functors ?

Thanks,
ducis
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20181214/6ed5cc67/attachment.html>


More information about the Haskell-Cafe mailing list