Construct a Haskell type using GeneralizedNewtypeDeriving to inherit Num instance for a…
- User
Construct a Haskell type using
GeneralizedNewtypeDerivingto inheritNuminstance for a wrapper typeUSD. Assistant
Construct a Haskell type using GeneralizedNewtypeDeriving to inherit Num instance for a wrapper type USD.
1. Understand the Goal: The user wants a Haskell code snippet.
* Core Task: Construct a wrapper type named USD.
* Mechanism: Use the GeneralizedNewtypeDeriving (GND) language extension.
* Target: Inherit/derive the Num instance for USD from the underlying type (presumably Double or Integer or Rational).
Identify the necessary components:
{-# LANGUAGE GeneralizedNewtypeDeriving #-}.newtype USD = USD <UnderlyingType>.deriving (Num, ...).Double is common for simple examples, though Rational or Decimal are better for real finance. For the sake of a standard example, Double is the easiest to demonstrate, but Float or Int work too. Let's stick with Double or Int. Double is the standard "number" type. Actually, Num works on any concrete numeric type.Drafting the Code:
Step 1: Pragma.
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
Step 2: Definition.
newtype USD = USD Double
Wait, record syntax is often nicer: newtype USD = USD { unUSD :: Double }. Let's stick to simple syntax first, maybe add record syntax as a refinement.
Step 3: Deriving.
deriving (Show, Num)
Note: Show is helpful for demonstration. Eq is usually good too.
Refining the Code:
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
newtype USD = USD Double
deriving (Show, Eq, Num)
Addressing the "Why" and "How" (Explanation):
newtype to reuse the instance of the wrapped type if the instance signature matches Coercible rules (roughly).(+) (USD a) (USD b) = USD (a + b) for every operator).Constructing a Full Example:
Define the type.
Write a main function to show it in action (addition, multiplication by a scalar - wait, Num implies multiplication is USD -> USD -> USD. That's semantically wrong for currency usually, but syntactically correct for Haskell's Num class. Real world usage might prefer just AdditiveGroup or similar, but the prompt specifically asked for Num. I should mention this semantic quirk).
Self-Correction on Semantics: While Num is what was asked for, deriving Num for currency allows USD 10 * USD 5 which results in USD 50. Dimensional analysis says this should be . However, to get fromInteger (so we can write 10 :: USD) and +, Num is the standard class in Haskell. I will stick to the prompt but perhaps add a small note about semantics if I feel chatty, but usually, direct answers are best.
Final Code Structure:
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
-- We wrap a Double.
-- We automatically derive Show, Eq, Ord, and Num.
newtype USD = USD Double
deriving (Show, Eq, Ord, Num)
main :: IO ()
main = do
let price1 = USD 10.50
let price2 = USD 5.25
-- Addition works because of the derived Num instance
print (price1 + price2)
-- Multiplication also works (semantics of currency * currency is debatable,
-- but this is how Num behaves)
print (price1 * 2) -- This actually requires fromInteger coercion
Reviewing the Prompt Constraints:
GeneralizedNewtypeDeriving": Check.Num instance": Check.USD": Check.Formatting the Output: Present the code block clearly, followed by a brief explanation of the mechanism.
newtype.Double).deriving (Num).instance Num USD where (USD a) + (USD b) = USD (a + b).Final Polish: Ensure the code compiles. newtype USD = USD Double deriving (Show, Num) is valid GHC Haskell.
(Self-Correction: Should I use Integer? Double is usually better for currency representation in simple tutorials, though Scientific or Rational is better for production. I'll stick to Double for simplicity.)
Let's write the response. Here is the Haskell code to achieve this.
To use GeneralizedNewtypeDeriving, you must enable the language pragma. The compiler will then generate the Num instance for USD by coercing the operations to the underlying Double type.
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
-- We define USD as a wrapper around Double.
-- We add 'deriving (Num)' to inherit the Num logic automatically.
newtype USD = USD { unUSD :: Double }
deriving (Show, Eq, Num)
main :: IO ()
main = do
let price1 = USD 10.5
let price2 = USD 5.5
-- We can now use standard Num operators (+, -, *, fromInteger)
-- directly on USD values.
print (price1 + price2) -- Output: USD {unUSD = 16.0}
print (price1 * 2) -- Output: USD {unUSD = 21.0}
print (20 :: USD) -- Output: USD {unUSD = 20.0}
newtype: Ensures that USD has zero runtime overhead compared to a plain Double.deriving (Num): The compiler generates an instance that looks roughly like this behind the scenes:
instance Num USD where
USD x + USD y = USD (x + y)
USD x * USD y = USD (x * y)
fromInteger i = USD (fromInteger i)
-- ... etc