Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undefined type constructor "Int" in Haskell

Tags:

haskell

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.

like image 421
Wyvern666 Avatar asked Jul 06 '26 21:07

Wyvern666


2 Answers

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, (+)).

like image 188
shree.pat18 Avatar answered Jul 08 '26 17:07

shree.pat18


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.

like image 41
dfeuer Avatar answered Jul 08 '26 18:07

dfeuer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!