Skip to content

Commit

Permalink
Derive: refactor typeConstructors
Browse files Browse the repository at this point in the history
  • Loading branch information
rudymatela committed Jan 26, 2024
1 parent 94febdf commit b2970e4
Showing 1 changed file with 13 additions and 15 deletions.
28 changes: 13 additions & 15 deletions src/Test/LeanCheck/Derive.hs
Original file line number Diff line number Diff line change
Expand Up @@ -284,24 +284,22 @@ typeArity t = fmap arity $ reify t
-- > data Point = Pt Int Int
-- > typeConstructors ''Point = Q [('Pt,[ConT Int, ConT Int])]
typeConstructors :: Name -> Q [(Name,[Type])]
typeConstructors t = do
ti <- reify t
return . map simplify $ case ti of
typeConstructors t = fmap (map normalize . cons) $ reify t
where
#if __GLASGOW_HASKELL__ < 800
TyConI (DataD _ _ _ cs _) -> cs
TyConI (NewtypeD _ _ _ c _) -> [c]
cons (TyConI (DataD _ _ _ cs _)) = cs
cons (TyConI (NewtypeD _ _ _ c _)) = [c]
#else
TyConI (DataD _ _ _ _ cs _) -> cs
TyConI (NewtypeD _ _ _ _ c _) -> [c]
cons (TyConI (DataD _ _ _ _ cs _)) = cs
cons (TyConI (NewtypeD _ _ _ _ c _)) = [c]
#endif
_ -> errorOn "typeConstructors"
$ "neither newtype nor data: " ++ show t
where
simplify (NormalC n ts) = (n,map snd ts)
simplify (RecC n ts) = (n,map trd ts)
simplify (InfixC t1 n t2) = (n,[snd t1,snd t2])
simplify _ = errorOn "typeConstructors"
$ "unexpected unhandled case when called with " ++ show t
cons _ = errorOn "typeConstructors"
$ "neither newtype nor data: " ++ show t
normalize (NormalC n ts) = (n,map snd ts)
normalize (RecC n ts) = (n,map trd ts)
normalize (InfixC t1 n t2) = (n,[snd t1,snd t2])
normalize _ = errorOn "typeConstructors"
$ "unexpected unhandled case when called with " ++ show t
trd (x,y,z) = z

isTypeSynonym :: Name -> Q Bool
Expand Down

0 comments on commit b2970e4

Please sign in to comment.