liveBook · Manning

:type

As explained in Learn Haskell by Example

:type is a GHCi command that displays the inferred type of an expression or name.

:type is a GHCi command for asking what type GHCi assigns to an expression or name. Use it when a value’s printed form hides the type you intended, or when type inference has supplied a type without an explicit annotation. section 2.3.1 section 2.2

At the GHCi prompt, enter :type followed by a name or expression. The shorter command :t does the same job. GHCi reports the result as a type expression, using :: to separate the name or expression from its type. section 2.3.1 section 2.2

Checking a type synonym

An Alphabet is defined as a synonym for [Char], so its values print like ordinary strings. Querying the names confirms that GHCi still reports the more meaningful Alphabet type: ghci> :type lowerAlphabet lowerAlphabet :: Alphabet ghci> :t digits digits :: Alphabet This lets a program use a readable domain name while retaining the underlying list-of-characters representation. section 2.3.1 section 2.3.1

Use nearby GHCi commands for related inspection and workflow tasks:

  • Use :info, or :i, to see how a type or value is defined; for example, FilePath is shown as a synonym for String. section 2.3.1
  • Use :reload, or :r, after saving module changes so the updated definitions are available in GHCi. Reloading removes locally entered definitions and values. section 2.3.1
  • Write explicit top-level type expressions when defining module functions and values, even though Haskell can often infer them. section 2.2
ghci> :type lowerAlphabet
lowerAlphabet :: Alphabet
ghci> :type upperAlphabet
upperAlphabet :: Alphabet
ghci> :type digits
digits :: Alphabet

Compare the values printed as strings with the types GHCi reports for the alphabet definitions. section 2.3.1

How Learn Haskell by Example covers it

How deep it goes
Demonstrates command output and shorthand, but defers type-inference mechanics beyond stating that Haskell infers types from context.
Examples
Checks lowerAlphabet, upperAlphabet, and digits after defining Alphabet as a [Char] synonym.
What it assumes
Assumes access to a Stack project and GHCi while introducing type expressions and inferred types from basic definitions.
What you can do after
Evaluate/understand inferred types for GHCi names and recognize readable type synonyms behind printed String values.
How it connects
Connects type expressions, type inference, type synonyms, :info, and :reload within GHCi.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Haskell by Example. The section links point to the parts of the book it draws on.