Is it possible to display the results of applying a Haskell type family function?
Asked Answered
P

2

6

For example, if I have these weird types:

{-# LANGUAGE TypeFamilies #-}
type family WeirdFamily a
type instance WeirdFamily () = Int
type instance WeirdFamily (a, b) = (a, WeirdFamily b)

Can I display (e.g. in GHCi) the result of WeirdFamily (Bool, (Char, ())) by typing something like:

:t WeirdFamily (Bool, (Char, ()))

into GHCi?

Pence answered 19/7, 2020 at 10:58 Comment(0)
M
8

Use kind!.

:kind! WeirdFamily (Bool, (Char, ()))
WeirdFamily (Bool, (Char, ())) :: *
= (Bool, (Char, Int))
Meniscus answered 19/7, 2020 at 11:16 Comment(3)
Perfect! Looking at the help for kind! it says "also print the normalised type" - is there some documentation for what a "normalised type" is somewhere?Pence
@Pence "normalized" here stands for "simplified using equations (applied left-to-right) as much as possible".Scrivner
This is great, but how do I normalize a parameterized type? For example StM (ExceptT e m) a (which should normalize to StM m (Either e a).Ambriz
P
1

So I have figured out an answer. Type this into GHCi:

f :: WeirdFamily (Bool, (Char, ())); f = undefined
:t f

gives f :: (Bool, (Char, Int))

But it feels like there should be a "cleaner" way. Is there?

Pence answered 19/7, 2020 at 11:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.