Café,
I've tried several things already, but I am not including any of them for
now.
My question is, how would you define the 'transform' function for a GADT,
say the one in the linked gist: http://gist.github.com/492364 (also attached
to this e-mail)
To be concise, I want 'transform' to apply the transformation function (its
first parameter) to every immidiate child of its second parameter as long as
the types match. Similar to what the 'tranform' function of Uniplate does
for normal ADTs. (But just one level, so I guess it is more similar to the
'descend' of Uniplate. See
http://hackage.haskell.org/packages/archive/uniplate/1.2.0.1/doc/html/Data-Generics-UniplateStr.html
)
I think I got closest to a sensible solution using multi-param type classes,
and defining many instances for different combinations of ExprType's but
still there were problems.
Waiting for suggestions and/or insights.
Best,
Ozgur Akgun
{-# LANGUAGE GADTs #-}
{-# LANGUAGE StandaloneDeriving #-}
module GADTExpr where
import Data.Function(on)
import Data.Ord(comparing)
class (Eq a, Ord a, Show a) => ExprType a where
toExpr :: a -> Expr a
instance ExprType Bool where toExpr = Bin
instance ExprType Int where toExpr = Num
data Expr a where
Bin :: Bool -> Expr Bool
Num :: Int -> Expr Int
(:+:) :: Expr Int -> Expr Int -> Expr Int
(:-:) :: Expr Int -> Expr Int -> Expr Int
(:=:) :: ExprType a => Expr a -> Expr a -> Expr Bool
(:<:) :: ExprType a => Expr a -> Expr a -> Expr Bool
(:/\:) :: Expr Bool -> Expr Bool -> Expr Bool
(:\/:) :: Expr Bool -> Expr Bool -> Expr Bool
instance Eq (Expr a) where (==) = (==) `on` show
instance Ord (Expr a) where compare = comparing show
deriving instance Show (Expr a)
eval :: ExprType a => Expr a -> Expr a
eval = toExpr . evalExpr
evalExpr :: ExprType a => Expr a -> a
evalExpr (Bin i) = i
evalExpr (Num i) = i
evalExpr (a :+: b) = evalExpr a + evalExpr b
evalExpr (a :-: b) = evalExpr a - evalExpr b
evalExpr (a :=: b) = evalExpr a == evalExpr b
evalExpr (a :<: b) = evalExpr a < evalExpr b
transform :: (ExprType a, ExprType b) => (Expr b -> Expr b) -> Expr a -> Expr a
transform = undefined
_______________________________________________
Haskell-Cafe mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/haskell-cafe