-
-
Save dredozubov/8335d170d07783781dc5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE DataKinds, GADTs, KindSignatures #-} | |
import Data.Fixed (Centi) | |
data Currency = USD | |
| RUB | |
data Money :: Currency -> * where | |
USDAmount :: Centi -> Money 'USD | |
RUBAmount :: Centi -> Money 'RUB | |
-- (1) | |
addMoney | |
:: Money a | |
-> Money a | |
-> Money a | |
addMoney (USDAmount vol1) (USDAmount vol2) = | |
USDAmount $ vol1 + vol2 | |
addMoney (RUBAmount vol1) (RUBAmount vol2) = | |
RUBAmount $ vol1 + vol2 | |
-- with (1) i get: | |
-- | |
-- Pattern match(es) are non-exhaustive | |
-- In an equation for ‘addMoney’: | |
-- Patterns not matched: | |
-- (USDAmount _) (RUBAmount _) | |
-- (RUBAmount _) (USDAmount _) | |
-- vs | |
-- (2) | |
addMoney | |
:: forall a b. (a ~ Money b) | |
=> a | |
-> a | |
-> a | |
addMoney (USDAmount vol1) (USDAmount vol2) = | |
USDAmount $ vol1 + vol2 | |
addMoney (RUBAmount vol1) (RUBAmount vol2) = | |
RUBAmount $ vol1 + vol2 | |
-- no warnings with (2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment