Skip to content

Instantly share code, notes, and snippets.

@dredozubov
Last active August 28, 2015 11:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dredozubov/8335d170d07783781dc5 to your computer and use it in GitHub Desktop.
Save dredozubov/8335d170d07783781dc5 to your computer and use it in GitHub Desktop.
{-# 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