[Haskell-beginners] Showing Types

David McBride toad3k at gmail.com
Mon Mar 7 18:27:31 UTC 2016


You can use the Data.Typeable module

import Data.Typeable

:t typeOf True
typeOf True :: TypeRep

:t show (typeOf True)
show (typeOf True) :: String

typeOf 1 == typeOf 1
True

>typeOf 1 == typeOf True
False

But it works on concrete types, not functions.  Sorry.

let len = sum . map (const 1)
typeOf len

<interactive>:17:1:
    No instance for (Typeable b0) arising from a use of ‘typeOf’
    In the expression: typeOf len
    In an equation for ‘it’: it = typeOf len

On Mon, Mar 7, 2016 at 1:06 PM, Dániel Arató <exitconsole at gmail.com> wrote:

> How does GHCi show types?
>
> Is there a magic function (showType :: a -> String) that does that? Or
> is this feature buried somewhere deep in the compiler?
>
> Can I show, compare and reason about types in a Haskell program?
>
> This would be cool:
>
> ```haskell
> test = do
> let len = sum . map (const 1)
> when (isInfixOf "Integer" (showType len)) $ putStrLn "restrictive type
> inferred; maybe try turning off monomorphism restriction"
> ```
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20160307/88e39219/attachment.html>


More information about the Beginners mailing list