Question

I am wondering if there is functionality that exists within GHCi (or elsewhere) to expand type synonyms and families out of an arbitrary type expression.

For example, if I have these types,

data A = A
data B = B

data F a = F a
data G a = G a
data H a b = H a b

type S a b = H (F a) (G b)
type T a = S (a) (H B a)

type family R a :: *
type instance R (H a b) = H b a

then I would like to be able to get this kind of output in a ghci session.

> :t undefined :: T (S B A)
undefined :: T (S B A) :: T (S B A)

> :texpand undefined :: T (S B A)
undefined :: T (S B A) :: H (F ((H (F B) (G A)))) (G (H B (H (F B) (G A))))

> :texpand undefined :: R (T (S B A))
undefined :: R (T (S B A)) :: H (G (H B (H (F B) (G A)))) (F ((H (F B) (G A)))) 

As far as I can tell, GHCi doesn't actually provide anything like a :texpand command, and I'm not sure that it would be the best interface for that information anyway. However, it seems likely that expanded types can be extracted from GHC somehow, and I would really like to be able to see them interactively.

Hacks, documentation links, speculative discussion about future additions to GHCi all welcome.

Was it helpful?

Solution

:kind! will do that:

λ> :kind! T (S B A)
T (S B A) :: *
= H (F (H (F B) (G A))) (G (H B (H (F B) (G A))))
λ> :kind! R (T (S B A))
R (T (S B A)) :: *
= H (G (H B (H (F B) (G A)))) (F (H (F B) (G A)))

(For convenience, you can put something like :def k! \x -> return (":kind! " ++ x) in your .ghci.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top