CPS506 Lab 6 Haskell: Type classes and custom types Preamble In this lab you'll create a custom type in Haskell and make it an instance of several built-in Haskell type-classes. Getting Started The provided cabal project defines a module called Lab6. This module will define a custom type called DigitalTime. The DigitalTime type will be used to represent time as a 3-tuple of integers. One for hour, minute, and second, respectively. Thus, the type definition should look like this: data DigitalTime = DigitalTime (Int, Int, Int) Next, you will implement several type classes over this type. These type classes and their operations can be found below. Assume that the hour range is 1-12. Do not use 24h time. Type Classes to Implement Show: show DigitalTime (h, m, s) Print the time in the format "" Eq: (==) (DigitalTime (h1, m1, s1)) (DigitalTime (h2, m2, s2)) Two times are considered equal if they have the same hour, minute, and second values. Ord: (<=) (DigitalTime (h1, ml, s1)) (DigitalTime (h2, m2, s2)) Time are ordered first by hour, then by minute, then by second. Only compare minutes if the hours are the same, and only compare seconds if the minutes are the same. Num: (+) (DigitalTime (h1, ml, s1)) (DigitalTime (h2, m2, s2)) When adding times, first add the seconds. If the seconds overflow (>59), carry the minute. Then add the minutes. If the minutes would overflow (>59), carry the hour. If the hour overflows (>12), it simply wraps around. (-) (DigitalTime (h1, ml, s1)) (DigitalTime (h2, m2, 2)) Just like addition, but this time check for underflow. If subtracting seconds results in a negative number, wrap back around and carry to the minutes, and so on. fromInteger sec Assume that the integer sec is a value in seconds. Convert those seconds into a DigitalTime. For example, 10000 seconds would be two hours, 46 minutes, and 40 seconds, or DigitalTime (2, 46, 40). For the Num type class, you are only required to implement these three operations. Notice that this is not a complete definition, and the compiler will complain with a warning when you load your module into GHCI. This is generally not a good thing, but it's fine for the purposes of this lab. Submission Labs are to be completed and submitted individually. Submit your Lab6.hs source file on D2L under Lab #6.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

CPS506 Lab 6

Haskell: Type classes and custom types

Preamble

In this lab you'll create a custom type in Haskell and make it an instance of several built-in Haskell type-classes.

Getting Started

The provided cabal project defines a module called Lab6. This module will define a custom type called DigitalTime. The DigitalTime type will be used to represent time as a 3-tuple of integers. One for hour, minute, and second, respectively. Thus, the type definition should look like this:

data DigitalTime = DigitalTime (Int, Int, Int) Next, you will implement several type classes over this type. These type classes and their operations can be found below. Assume that the hour range is 1-12. Do not use 24h time.

Type Classes to Implement

Show:

show DigitalTime (h, m, s) Print the time in the format "<hh:mm:ss>"

Eq:

(==) (DigitalTime (h1, m1, s1)) (DigitalTime (h2, m2, s2)) Two times are considered equal if they have the same hour, minute, and second values.

Ord:

(<=) (DigitalTime (h1, ml, s1)) (DigitalTime (h2, m2, s2))

Time are ordered first by hour, then by minute, then by second. Only compare minutes if the hours are the same, and only compare seconds if the minutes are the same.
Num:

(+) (DigitalTime (h1, ml, s1)) (DigitalTime (h2, m2, s2))

When adding times, first add the seconds. If the seconds overflow (>59), carry the minute. Then add the minutes. If the minutes would overflow (>59), carry the hour. If the hour overflows (>12), it simply wraps around.

(-) (DigitalTime (h1, ml, s1)) (DigitalTime (h2, m2, 2)) Just like addition, but this time check for underflow. If subtracting seconds results in a negative number, wrap back around and carry to the minutes, and so on.

fromInteger sec

Assume that the integer sec is a value in seconds. Convert those seconds into a DigitalTime. For example, 10000 seconds would be two hours, 46 minutes, and 40 seconds, or DigitalTime (2, 46, 40).

For the Num type class, you are only required to implement these three operations. Notice that this is not a complete definition, and the compiler will complain with a warning when you load your module into GHCI. This is generally not a good thing, but it's fine for the purposes of this lab.

Submission

Labs are to be completed and submitted individually. Submit your Lab6.hs source file on D2L under Lab #6. 

Expert Solution
steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
Data members
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education