Created
May 29, 2013 05:43
-
-
Save UnkindPartition/5668198 to your computer and use it in GitHub Desktop.
SYB traversal for GADT
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE GADTs, EmptyDataDecls, MultiParamTypeClasses, TypeFamilies #-} | |
{-# LANGUAGE DeriveDataTypeable, StandaloneDeriving #-} | |
import Data.Generics | |
import Data.Typeable | |
import Data.Data | |
data HasHoles | |
data Complete | |
deriving instance Typeable HasHoles | |
deriving instance Data HasHoles | |
deriving instance Typeable Complete | |
deriving instance Data Complete | |
type family Holes a b :: * | |
canHaveHolesT :: a -> b -> Holes a b | |
canHaveHolesT _ _ = undefined | |
type instance Holes HasHoles Complete = HasHoles | |
type instance Holes Complete HasHoles = HasHoles | |
type instance Holes HasHoles HasHoles = HasHoles | |
type instance Holes Complete Complete = HasHoles | |
data Expression k a where | |
EQuote :: a -> String -> Expression HasHoles a | |
IntLit :: a -> Int -> Expression Complete a | |
EArith :: (Typeable k1, Typeable k2) | |
=> a -> ArithOp -> Expression k1 a -> Expression k2 a -> | |
Expression (Holes k1 k2) a | |
deriving instance Typeable2 (Expression) | |
data ArithOp = OpAdd | |
| OpSub | |
| OpMul | |
| OpDiv | |
deriving (Data, Typeable) | |
instance (Typeable k_a3db, Data a_a3dc) => Data (Expression k_a3db a_a3dc) where | |
gfoldl k_a3dj z_a3dk (EQuote a1_a3dl a2_a3dm) | |
= ((z_a3dk EQuote `k_a3dj` a1_a3dl) `k_a3dj` a2_a3dm) | |
gfoldl k_a3dn z_a3do (IntLit a1_a3dp a2_a3dq) | |
= ((z_a3do IntLit `k_a3dn` a1_a3dp) `k_a3dn` a2_a3dq) | |
gfoldl | |
k_a3dr | |
z_a3ds | |
(EArith a1_a3dt a2_a3du a3_a3dv a4_a3dw) | |
= ((((z_a3ds EArith `k_a3dr` a1_a3dt) `k_a3dr` a2_a3du) | |
`k_a3dr` a3_a3dv) | |
`k_a3dr` a4_a3dw) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment