Skip to main content
Notice removed Authoritative reference needed by Abel
Bounty Ended with Yuras's answer chosen by Abel
Notice added Authoritative reference needed by Abel
Bounty Started worth 200 reputation by Abel
Question Protected by CommunityBot
Brought this question up to date wrt GHC versions, given all the attention it's received.
Source Link
% ghci
GHCi, version 7.48.23: http://www.haskell.org/ghc/  :? for help
...
Prelude> -- I'll be inserting line breaks and whitespace for clarity
Prelude> -- in all GHCi output.
Prelude> :set -XTemplateHaskell 
Prelude> import Language.Haskell.TH
Prelude Language.Haskell.TH> class C a where f :: a -> Int
Prelude Language.Haskell.TH> putStrLn $(stringE . show =<< reify ''C)
ClassI (ClassD [] :InteractiveGhci1.C [PlainTV a_1627394862]a_1627398388] []
               [SigD :InteractiveGhci1.f
                     (ForallT [PlainTV a_1627394862]a_1627398388]
                              [ClassP :InteractiveGhci1.C [VarT a_1627394862]]a_1627398388]]
                              (AppT (AppT ArrowT (VarT a_1627394862a_1627398388))
                                    (ConT GHC.Types.Int)))])
       []

However, adding an associated type syonymsynonym to the class causes no change (up to renaming) in the output:

Prelude Language.Haskell.TH> :set -XTypeFamilies 
Prelude Language.Haskell.TH> class C' a where type F a :: * ; f' :: a -> Int
Prelude Language.Haskell.TH> putStrLn $(stringE . show =<< reify ''C')
ClassI (ClassD [] :InteractiveGhci3.C' [PlainTV a_1627398570]a_1627405973] []
               [SigD :InteractiveGhci3.f'
                     (ForallT [PlainTV a_1627398570]a_1627405973]
                              [ClassP :InteractiveGhci3.C' [VarT a_1627398570]]a_1627405973]]
                              (AppT (AppT ArrowT (VarT a_1627398570a_1627405973))
                                    (ConT GHC.Types.Int)))])
       []
Prelude Language.Haskell.TH> putStrLn $(stringE . show =<< reify ''F)
FamilyI (FamilyD TypeFam
                 :InteractiveGhci3.F
                 [PlainTV a_1627398570]a_1627405973]
                 (Just (ArrowK StarK StarK)StarT))
        []
Prelude Language.Haskell.TH> instance C' [a] where type F [a] = a ; f' = length
Prelude Language.Haskell.TH> f' "Haskell"
7
Prelude Language.Haskell.TH> 42 :: F [Integer]
42
Prelude Language.Haskell.TH> putStrLn $(stringE . show =<< reify ''C')
ClassI (ClassD [] :InteractiveGhci3.C' [PlainTV a_1627398570]a_1627405973] []
               [SigD :InteractiveGhci3.f'
                     (ForallT [PlainTV a_1627398570]a_1627405973]
                              [ClassP :InteractiveGhci3.C' [VarT a_1627398570]]a_1627405973]]
                              (AppT (AppT ArrowT (VarT a_1627398570a_1627405973))
                                    (ConT GHC.Types.Int)))])
       [InstanceD []
                  (AppT (ConT :InteractiveGhci3.C')
                        (AppT ListT (VarT a_1627398694a_1627406161)))
                  []]

I'm runningThis problem is present in GHC 7.8.3 with version 2.9.0.0 of the template-haskell package; it was also present in GHC 7.4.2, with version 2.7.0.0 of the template-haskell package. If reify or something simple works (I didn't check on a later version, that's good to know, but I'm stuck withGHC 7.46.2 for this project*, but I imagine it was present there too.) I'm interested in solutions for the foreseeable futureany version of GHC (including "this was only fixed in GHC version V").

% ghci
GHCi, version 7.4.2: http://www.haskell.org/ghc/  :? for help
...
Prelude> :set -XTemplateHaskell 
Prelude> import Language.Haskell.TH
Prelude Language.Haskell.TH> class C a where f :: a -> Int
Prelude Language.Haskell.TH> putStrLn $(stringE . show =<< reify ''C)
ClassI (ClassD [] :Interactive.C [PlainTV a_1627394862] []
               [SigD :Interactive.f
                     (ForallT [PlainTV a_1627394862]
                              [ClassP :Interactive.C [VarT a_1627394862]]
                              (AppT (AppT ArrowT (VarT a_1627394862))
                                    (ConT GHC.Types.Int)))])
       []

However, adding an associated type syonym to the class causes no change (up to renaming) in the output:

Prelude Language.Haskell.TH> :set -XTypeFamilies 
Prelude Language.Haskell.TH> class C' a where type F a :: * ; f' :: a -> Int
Prelude Language.Haskell.TH> putStrLn $(stringE . show =<< reify ''C')
ClassI (ClassD [] :Interactive.C' [PlainTV a_1627398570] []
               [SigD :Interactive.f'
                     (ForallT [PlainTV a_1627398570]
                              [ClassP :Interactive.C' [VarT a_1627398570]]
                              (AppT (AppT ArrowT (VarT a_1627398570))
                                    (ConT GHC.Types.Int)))])
       []
Prelude Language.Haskell.TH> putStrLn $(stringE . show =<< reify ''F)
FamilyI (FamilyD TypeFam
                 :Interactive.F
                 [PlainTV a_1627398570]
                 (Just (ArrowK StarK StarK)))
        []
Prelude Language.Haskell.TH> instance C' [a] where type F [a] = a ; f' = length
Prelude Language.Haskell.TH> f' "Haskell"
7
Prelude Language.Haskell.TH> 42 :: F [Integer]
42
Prelude Language.Haskell.TH> putStrLn $(stringE . show =<< reify ''C')
ClassI (ClassD [] :Interactive.C' [PlainTV a_1627398570] []
               [SigD :Interactive.f'
                     (ForallT [PlainTV a_1627398570]
                              [ClassP :Interactive.C' [VarT a_1627398570]]
                              (AppT (AppT ArrowT (VarT a_1627398570))
                                    (ConT GHC.Types.Int)))])
       [InstanceD []
                  (AppT (ConT :Interactive.C')
                        (AppT ListT (VarT a_1627398694)))
                  []]

I'm running GHC version 7.4.2, with version 2.7.0.0 of the template-haskell package. If reify or something simple works on a later version, that's good to know, but I'm stuck with 7.4.2 for this project for the foreseeable future.

% ghci
GHCi, version 7.8.3: http://www.haskell.org/ghc/  :? for help
...
Prelude> -- I'll be inserting line breaks and whitespace for clarity
Prelude> -- in all GHCi output.
Prelude> :set -XTemplateHaskell 
Prelude> import Language.Haskell.TH
Prelude Language.Haskell.TH> class C a where f :: a -> Int
Prelude Language.Haskell.TH> putStrLn $(stringE . show =<< reify ''C)
ClassI (ClassD [] Ghci1.C [PlainTV a_1627398388] []
               [SigD Ghci1.f
                     (ForallT [PlainTV a_1627398388]
                              [ClassP Ghci1.C [VarT a_1627398388]]
                              (AppT (AppT ArrowT (VarT a_1627398388))
                                    (ConT GHC.Types.Int)))])
       []

However, adding an associated type synonym to the class causes no change (up to renaming) in the output:

Prelude Language.Haskell.TH> :set -XTypeFamilies 
Prelude Language.Haskell.TH> class C' a where type F a :: * ; f' :: a -> Int
Prelude Language.Haskell.TH> putStrLn $(stringE . show =<< reify ''C')
ClassI (ClassD [] Ghci3.C' [PlainTV a_1627405973] []
               [SigD Ghci3.f'
                     (ForallT [PlainTV a_1627405973]
                              [ClassP Ghci3.C' [VarT a_1627405973]]
                              (AppT (AppT ArrowT (VarT a_1627405973))
                                    (ConT GHC.Types.Int)))])
       []
Prelude Language.Haskell.TH> putStrLn $(stringE . show =<< reify ''F)
FamilyI (FamilyD TypeFam
                 Ghci3.F
                 [PlainTV a_1627405973]
                 (Just StarT))
        []
Prelude Language.Haskell.TH> instance C' [a] where type F [a] = a ; f' = length
Prelude Language.Haskell.TH> f' "Haskell"
7
Prelude Language.Haskell.TH> 42 :: F [Integer]
42
Prelude Language.Haskell.TH> putStrLn $(stringE . show =<< reify ''C')
ClassI (ClassD [] Ghci3.C' [PlainTV a_1627405973] []
               [SigD Ghci3.f'
                     (ForallT [PlainTV a_1627405973]
                              [ClassP Ghci3.C' [VarT a_1627405973]]
                              (AppT (AppT ArrowT (VarT a_1627405973))
                                    (ConT GHC.Types.Int)))])
       [InstanceD []
                  (AppT (ConT Ghci3.C')
                        (AppT ListT (VarT a_1627406161)))
                  []]

This problem is present in GHC 7.8.3 with version 2.9.0.0 of the template-haskell package; it was also present in GHC 7.4.2 with version 2.7.0.0 of the template-haskell package. (I didn't check on GHC 7.6.*, but I imagine it was present there too.) I'm interested in solutions for any version of GHC (including "this was only fixed in GHC version V").

Rollback to Revision 1
Source Link

Can Template HaskellHaskell find out the names and/or the declarations of the associated type synonyms declared in a type class? I expected reify would do what I want, but it doesn't seem to provide all the necessary information. It works for getting function type signatures:

Can Template Haskell find out the names and/or the declarations of the associated type synonyms declared in a type class? I expected reify would do what I want, but it doesn't seem to provide all the necessary information. It works for getting function type signatures:

Can Template Haskell find out the names and/or the declarations of the associated type synonyms declared in a type class? I expected reify would do what I want, but it doesn't seem to provide all the necessary information. It works for getting function type signatures:

added 4 characters in body
Source Link
Hossein Narimani Rad
  • 32.7k
  • 19
  • 92
  • 121

Can Template HaskellHaskell find out the names and/or the declarations of the associated type synonyms declared in a type class? I expected reify would do what I want, but it doesn't seem to provide all the necessary information. It works for getting function type signatures:

Can Template Haskell find out the names and/or the declarations of the associated type synonyms declared in a type class? I expected reify would do what I want, but it doesn't seem to provide all the necessary information. It works for getting function type signatures:

Can Template Haskell find out the names and/or the declarations of the associated type synonyms declared in a type class? I expected reify would do what I want, but it doesn't seem to provide all the necessary information. It works for getting function type signatures:

Notice removed Draw attention by CommunityBot
Bounty Ended with no winning answer by CommunityBot
Notice added Draw attention by Aify
Bounty Started worth 50 reputation by Aify
Notice removed Draw attention by CommunityBot
Bounty Ended with no winning answer by CommunityBot
Notice added Draw attention by Petr
Bounty Started worth 50 reputation by Petr
Source Link
Loading