Skip to content

Instantly share code, notes, and snippets.

@alpmestan
Created October 7, 2017 00:39
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alpmestan/50a0a00ebf4208c8ae898f84d6b41e07 to your computer and use it in GitHub Desktop.
Save alpmestan/50a0a00ebf4208c8ae898f84d6b41e07 to your computer and use it in GitHub Desktop.
Adding :kind!! to ghci, for expanding type families _AND_ type synonyms
diff --git a/ghc/GHCi/UI.hs b/ghc/GHCi/UI.hs
index 32e581a10d..b320ef42ce 100644
--- a/ghc/GHCi/UI.hs
+++ b/ghc/GHCi/UI.hs
@@ -76,6 +76,7 @@ import Linker
import Maybes ( orElse, expectJust )
import NameSet
import Panic hiding ( showException )
+import Type ( expandTypeSynonyms )
import Util
import qualified GHC.LanguageExtensions as LangExt
@@ -185,8 +186,9 @@ ghciCommands = map mkCmd [
("info", keepGoing' (info False), completeIdentifier),
("info!", keepGoing' (info True), completeIdentifier),
("issafe", keepGoing' isSafeCmd, completeModule),
- ("kind", keepGoing' (kindOfType False), completeIdentifier),
- ("kind!", keepGoing' (kindOfType True), completeIdentifier),
+ ("kind", keepGoing' (kindOfType JustKind), completeIdentifier),
+ ("kind!", keepGoing' (kindOfType NormaliseTFs), completeIdentifier),
+ ("kind!!", keepGoing' (kindOfType NormaliseTFsAndSynonyms), completeIdentifier),
("load", keepGoingPaths loadModule_, completeHomeModuleOrFile),
("load!", keepGoingPaths loadModuleDefer, completeHomeModuleOrFile),
("list", keepGoing' listCmd, noCompletion),
@@ -1967,13 +1969,24 @@ showRealSrcSpan spn = concat [ fp, ":(", show sl, ",", show sc
ec = srcSpanEndCol spn
-----------------------------------------------------------------------------
--- | @:kind@ command
+-- | @:kind[!!]@ commands
-kindOfType :: Bool -> String -> InputT GHCi ()
+data KindNormalise
+ = JustKind
+ | NormaliseTFs
+ | NormaliseTFsAndSynonyms
+
+kindOfType :: KindNormalise -> String -> InputT GHCi ()
kindOfType norm str = handleSourceError GHC.printException $ do
- (ty, kind) <- GHC.typeKind norm str
+ let norm' = case norm of
+ JustKind -> False
+ otherwise -> True
+ (ty, kind) <- GHC.typeKind norm' str
+ let ty' = case norm of
+ NormaliseTFsAndSynonyms -> expandTypeSynonyms ty
+ _ -> ty
printForUser $ vcat [ text str <+> dcolon <+> pprTypeForUser kind
- , ppWhen norm $ equals <+> pprTypeForUser ty ]
+ , ppWhen norm' $ equals <+> pprTypeForUser ty' ]
@alpmestan
Copy link
Author

Based on suggestions from https://ghc.haskell.org/trac/ghc/ticket/13795. Here it is in action:

[nix-shell:~/ghc]$ inplace/bin/ghc-stage2 --interactive
GHCi, version 8.3.20171006: http://www.haskell.org/ghc/  :? for help
Prelude> :kind String
String :: *
Prelude> :kind! String
String :: *
= String
Prelude> :kind!! String
String :: *
= [Char]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment