0

I have declared the following types

data MonthData = Jan | Feb | Mar | Apr | May | Jun | Jul | Aug | Sep | Oct | Nov | Dec deriving ( Eq, Show, Enum, Ord )
type Year = Int
type Month = ( MonthData, Year )



type Gap = Int
type Average = Double
type HistoryElem = ( Date, Gap, Average )
type History = [ HistoryElem ]

Then, I have declared the following function

event_tests = [ ( ( 28, ( Nov, 2016 ) ), 0, 0.0 ), ( ( 27, ( Nov, 2016 ) ), 0, 0.0 ) ]
history :: Int -> HistoryElem
history 0 = head( event_tests )

When I try to load my file, I have the following error.

ERROR "ass16-1.hs":90 - Type error in explicitly typed binding
*** Term           : history
*** Type           : Int -> ((Integer,(MonthData,Integer)),Integer,Double)
*** Does not match : Int -> HistoryElem

It seems that it doesn't consider that HistoryElem has been previously defined because if we look carefuly, we can see that

((Integer,(MonthData,Integer)),Integer,Double) is the same definition than HistoryElem

Can't figure out what I'm doing wrong.

2
  • 7
    Where is Date defined? Commented Nov 15, 2016 at 16:57
  • 2
    When getting a type error, adding type signatures to all the top-level bindings can greatly simplify it. Most Haskellers do that as a best practice. Indeed, sometimes the error is found later, in a surprising place where everything looks fine -- but the real error was much earlier and was not found only because the compiler inferred a different type than the one you would expect. Using a type annotation makes the compiler report errors where they are. Commented Nov 15, 2016 at 17:41

1 Answer 1

3

Adding a type signature for event_tests should do the trick:

eventTests :: History
event_tests = [ ( ( 28, ( Nov, 2016 ) ), 0, 0.0 ), ( ( 27, ( Nov, 2016 ) ), 0, 0.0 ) ]

Part of the issue is that, as the type error suggests...

ERROR "ass16-1.hs":90 - Type error in explicitly typed binding
*** Term           : history
*** Type           : Int -> ((Integer,(MonthData,Integer)),Integer,Double)
*** Does not match : Int -> HistoryElem

... Int and Integer are not the same type. As section 6.4 of the Haskell Report specifies, an Int is a fixed size integer, while an Integer can have arbitrary size. This affects your code because, if there is no type signature anywhere to specify which type an integer literal should be, it will default to Integer (see section 4.3.4 of the report for details). That being so, integers such as 28 and 2016 in your event_tests default to Integer unless you specify some other type (e.g. through your History type synonym, which indirectly makes them Ints).

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

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.