Własna definicja klasy w Haskellu

0

Chciałem w Haskellu zdefiniować własny typ danych, Day:

data Day = Mon | Tue | Wed | Thu | Fri | Sat | Sun

Następnie wiem, że będę potrzebował zamiany: Day -> Int oraz Int -> Day, więc w tym celu stworzyłem instancję:

instance Enum Day where
 toEnum Mon = 1
 toEnum Tue = 2
 toEnum Wed = 3
 toEnum Thu = 4
 toEnum Fri = 5
 toEnum Sat = 6
 toEnum Sun = 7
 fromEnum 1 = Mon
 fromEnum 2 = Tue
 fromEnum 3 = Wed
 fromEnum 4 = Thu
 fromEnum 5 = Fri
 fromEnum 6 = Sat
 fromEnum 7 = Sun

WinGHCi wywala mi takie błędy:

Ksiazka.hs9:
Couldn't match expected type Int' with actual type Day'
In the pattern: Mon
In an equation for toEnum': toEnum Mon = 1 In the instance declaration for Enum Day'

Ksiazka.hs11:
No instance for (Eq Day)
arising from the literal 1' Possible fix: add an instance declaration for (Eq Day) In the pattern: 1 In an equation for fromEnum': fromEnum 1 = Mon
In the instance declaration for `Enum Day'

Ksiazka.hs11:
No instance for (Num Day)
arising from the literal 1' Possible fix: add an instance declaration for (Num Day) In the pattern: 1 In an equation for fromEnum': fromEnum 1 = Mon
In the instance declaration for `Enum Day'

Ksiazka.hs15:
Couldn't match expected type Int' with actual type Day'
In the expression: Mon
In an equation for fromEnum': fromEnum 1 = Mon In the instance declaration for Enum Day

Próbowałem też wywalić definicję instancji i typ Day zdefiniować tak:

data Day = Mon | Tue | Wed | Thu | Fri | Sat | Sun deriving (Enum)

i niby się kompiluje, ale definiując następnie funkcję z metodami toEnum i fromEnum:

dayAfter :: Day -> Day
dayAfter d = fromEnum((toEnum d + 1) `mod` 7) 

dostaję błąd kompilacji:

Couldn't match expected type Day' with actual type Int'
In the return type of a call of fromEnum' In the expression: fromEnum ((toEnum d + 1) mod7) In an equation fordayAfter':
dayAfter d = fromEnum ((toEnum d + 1) mod 7)

Niestety, ja tych komunikatów nie rozumiem i nie wiem jak sobie poradzić.

0

Spokojnie, spójrz:

toEnum   :: Enum a => Int -> a
fromEnum :: Enum a => a -> Int

Pomyliłeś znaczenia, zamień toEnum na fromEnum, pozostałe fromEnum na toEnum i będzie dobrze ;]

0

Nie wiem czemu w książce R. Bird'a jest na odwrót to wszystko (drugie wydanie) ? Nie sądzę, żeby pozwolono aż na taki duży błąd.

Przy okazji, w jaki sposób mógłbym zdefiniować własny typ zwany trójką?

1 użytkowników online, w tym zalogowanych: 0, gości: 1