2

This work ok:

increment :: Int -> Int
increment i = i + 1

But why this not:

module MyFile where
import Prelude (Show)

increment :: Int -> Int
increment i = i + 1

Hugs say: Undefined type constructor "Int"

I suppose i need to import more things.

1
  • 5
    You don't have to explicitly import Prelude at all, you know. Those functions and types are imported by default unless overridden. Unless you're writing functions with names that conflict with those in Prelude, you can just use them without an import statement. Commented Apr 8, 2014 at 3:51

2 Answers 2

12

Int is defined in Prelude, but in your case you only import Show from Prelude. Therefore, the definition of Int cannot be found. To rectify, modify as import Prelude (Show, Int, (+)).

Sign up to request clarification or add additional context in comments.

2 Comments

Remark: Edit notes should be put in the edit summary, not in the actual answer.
@Zeta: Thanks for the heads up. Will edit accordingly.
6

In many Haskell programs, you want to leave the Prelude out of your imports and just let it be imported automatically.

In almost all the rest, you want to import most of the Prelude, but leave some pieces out. This can be done using, e.g.,

import Prelude hiding (head, tail, (++))

In some cases, that will be accompanied by an import to get access to the hidden parts:

import qualified Prelude as P

In virtually all remaining cases, you will be using some alternative prelude instead of the standard one. The only times I can imagine writing anything that doesn't import most of some prelude are when actually implementing the base modules for a Haskell system, or writing an alternative prelude.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.