haskelltemplate-haskelltype-level-computation# How to bring type information into value level in Haskell?

I'm looking for ways of way to bring type information into value level in Haskell.

One way I know to represent any type information as a value is the `Language.Haskell.TH.Type`

.
Is there any way to implement a function that takes `Proxy a`

and returns `Language.Haskell.TH.Type`

(or alternative type to represent any type) of the `a`

type as follows?

If you have a better idea to bring type information as a value that does not use `Language.Haskell.TH.Type`

, please tell me that, too.

```
import Data.Proxy (Proxy)
import Language.Haskell.TH (Type, TypeQ)
-- |
-- >>> amazing (Proxy :: Proxy Bool)
-- ConT GHC.Types.Bool
--
-- >>> amazing (Proxy :: Proxy [String])
-- AppT ListT (ConT GHC.Base.String)
amazing :: Proxy a -> Type
amazing p = undefined
-- |
-- Or if above is impossible, how about this?
amazingQ :: Proxy a -> TypeQ
amazingQ p = undefined
```

Solution

It boils down to finding what you want to do with that type information. In either case, the modules you will probably be looking at are `Data.Typeable`

and `Data.Data`

. At the center of these modules are the two typeclasses (deriveable via `-XDeriveDataTypeable`

)

```
class Typeable a where {...}
class Typeable a => Data a where {..}
```

As @chi mentions, the former of these lets you poke a type to find out information about it. That said there is the caveat that you need instances of `Typeable`

for this to work (although you can start making orphan instances of these if you really need to...). In particular, there is `typeRep`

:

```
ghci> import Data.Typeable
ghci> typeRep (Proxy :: Proxy (Either (Maybe [Integer]) ((), Bool, Int)))
Either (Maybe [Integer]) ((), Bool, Int)
```

However, if you ever decide you want to use the type information to try to lookup the value representations (ie. constructors) it has, you'll want to look at `Data.Data`

.

- Haskell fails to infer the return type of a monad after using the sequence operator
- Does extracting values from a multiple Value return in Haskell invoke the function more than once?
- Why do I get "Unexpected reply type" from notify-send when using this Haskell notification server?
- Don't understand notation of morphisms in Monoid definition
- Foldln in haskell
- Is this property of a functor stronger than a monad?
- How to Instantiate a Custom Data Type with Record Syntax and Multiple Constructors
- How do I make a minimal working example for the a DBus server?
- Is it safe to downgrade Haskell stack version?
- Haskell, list of natural number
- unfamiliar syntax in Haskell / Clash function type signature
- foldM with monad State does not type check
- Why does my Runge-Kutta implementation oscillate to 0?
- How do I get the desired behavior in my TCP server?
- Why does the Haskell PVP describe new functions as non-breaking?
- How do I correctly use toLower in Haskell?
- How can I write a notification server in Haskell?
- Every Lens' is a Traversal'... how?
- How do I crate a value of type a{sv} for a call to org.freedesktop.Notifications.Notify via DBus?
- Web Scraping With Haskell
- Double exclamation marks in Haskell
- Haskell Servant POST FormUrlEncoded for (Vector String) field
- Confusion about list types in Haskell
- Idiomatic way to define new vs persisted types in Haskell
- Why does Cabal, unlike GHC, not automatically enable GeneralizedNewtypeDeriving if I explicitly enabled DerivingStrategies?
- What is the proper way of wrapping an Int (not a general type) in another type if type safety is the only motive?
- Parsing inside `between` with Megaparsec
- takeWhile implementation in JavaScript - Looking for better ideas
- How to setup MINGW environment variables for Haskell language server in vscode?
- mysql-haskell no invoke of Right case of try function