Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Asking for the type of an expression #3

Open
chris-martin opened this issue Sep 30, 2019 · 1 comment
Open

Asking for the type of an expression #3

chris-martin opened this issue Sep 30, 2019 · 1 comment
Labels
good for new contributors Pull requests welcome! new example This issue is about writing a new example program.

Comments

@chris-martin
Copy link
Member

We've already covered this once in using the REPL, but here's another quick way of asking for a type that's useful when you're using the text editor + ghcid setup.

If you don't know the type of some expression, leave an underscore (this is called a "hole") and the display in ghcid will tell you what type goes there.

loadConfig :: _
loadConfig = Data.Ini.readIniFile "config.ini"
    • Found type wildcard ‘_’
        standing for ‘IO (Either String Data.Ini.Ini)’

It's pretty magical that you can just type :: _ after an expression to make the type pop up in ghcid.

@chris-martin chris-martin added new example This issue is about writing a new example program. good for new contributors Pull requests welcome! labels Sep 30, 2019
@ammatsui
Copy link
Contributor

ammatsui commented Oct 1, 2019

Or in the middle of a function, when you don't know what to do. For example, when defining a Functor instance for Tree:

data Tree a = Leaf a | Node (Tree a) (Tree a)

instance Functor Tree where
  fmap f (Leaf a)   = Leaf (f a)
  fmap f (Node l r) = Node (_ f l) (_ f r)

Compiler would say:

• Found hole: _ :: (a -> b) -> Tree a -> Tree b
Valid substitutions include
    fmap :: forall (f :: * -> *).
            Functor f =>
            forall a b. (a -> b) -> f a -> f b

So one can use the compiler to get hints about some code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good for new contributors Pull requests welcome! new example This issue is about writing a new example program.
Projects
None yet
Development

No branches or pull requests

2 participants