我正在尝试实现一个 CurrencyQty 类型,它的作用类似于编译时标记的数字:
data Currency = Usd | Eur | Gbp
data CurrencyQty (a :: Currency) = CurrencyQty Double deriving (Num)
现在我想实现一个动态查找汇率的通用转换函数。假设我有一些功能
currentExchangeRate :: Currency -> Currency -> IO Double
我想写
inUsd :: CurrencyQty a -> IO (CurrencyQty Usd)
inUsd (CurrencyQty Usd x) = return x
inUsd (CurrencyQty Eur x) = fmap (*x) $ currentExchangeRate Usd Eur
inUsd (CurrencyQty Gbp x) = fmap (*x) $ currentExchangeRate Usd Gbp
或者也许以某种方式
inUsd :: CurrencyQty a -> IO (CurrencyQty Usd)
inUsd (CurrencyQty a x) = fmap (*x) $ currentExchangeRate Usd a
我使用的语法显然是无效的哈斯克尔...有没有办法做到这一点?
您不能为此使用幻影。幻像类型在运行时消失,您需要一些运行时信息来执行inUsd
函数。
通常的方法是使用 GADT 和单例类型。
-- the "index" type
data Currency = Usd | Eur | Gbp
-- the "singleton" type
-- If you want to autogenerate this, check out the singletons
-- package on Hackage
data SCurrency (a :: Currency) where
SUsd :: Scurrency Usd
SEur :: Scurrency Eur
SGbp :: Scurrency Gbp
-- the "indexed" type
data CurrencyQty (a :: Currency) where
CurrencyQty :: SCurrency a -> Double -> CurrencyQty a
instance Num (CurrencyQty a) where
... -- you have to manually write this, I guess?
inUsd :: CurrencyQty a -> IO (CurrencyQty Usd)
inUsd (CurrencyQty SUsd x) = return x
inUsd (CurrencyQty SEur x) = fmap (*x) $ currentExchangeRate Usd Eur
inUsd (CurrencyQty SGbp x) = fmap (*x) $ currentExchangeRate Usd Gbp
让我补充一点,你的代码没问题。如果Haskell具有完全依赖类型,则可以使用您的代码进行细微调整,并避免使用额外的单例类型。然而,目前,Haskell无法避免这一点,需要更多的努力。