Haskell中的多态类型族实例

时间:2019-02-16 19:58:07

标签: haskell polymorphism type-families

我正在尝试编写代码来模拟随机变量,并且我想让事情尽可能保持多态。这可能涉及使用类型家族,这对我来说是全新的。

这是我的代码的简化版本:

{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE InstanceSigs #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}


data TrivialDist a = Trivial a

getVal :: TrivialDist a -> a
getVal (Trivial x) = x

class JointDist d a where
    toTrivialDist :: d a -> TrivialDist [a]

data TrivialJointDist a = TrivialJoint [a]

instance JointDist TrivialJointDist a where
    toTrivialDist :: TrivialJointDist a -> TrivialDist [a]
    toTrivialDist (TrivialJoint xs) = Trivial xs

class Simulable s a where
    type Samp s a :: *
    -- generates infinite stream of random samples from a distribution
    samples :: s a -> IO [Samp s a]
    -- generates a single random sample from a distribution
    sample :: s a -> IO (Samp s a)
    sample = fmap head . samples

instance Simulable TrivialDist a where
    type Samp TrivialDist a = a
    samples :: TrivialDist a -> IO [a]
    samples (Trivial x) = return $ repeat x

instance (JointDist d a) => Simulable d a where
    type Samp d a = [a]
    samples :: d a -> IO [[a]]
    samples = samples . toTrivialDist

将其加载到ghci中时,出现此错误:

test.hs:30:10: error:
    Conflicting family instance declarations:
      Samp TrivialDist a = a -- Defined at test.hs:30:10
      Samp d a = [a] -- Defined at test.hs:40:10
   |
30 |     type Samp TrivialDist a = a
   |          ^^^^^^^^^^^^^^^^^^^^^^
Failed, 0 modules loaded.

这个问题似乎与找到的here类似(解释似乎是GHC无法根据约束条件来区分类型),但未提出解决方案。

如果将最后一个实例声明更改为:

,我可以获得编译的代码。

instance Simulable TrivialJointDist a where
    type Samp TrivialJointDist a = [a]
    samples :: TrivialJointDist a -> IO [[a]]
    samples = samples . toTrivialDist

但是,我然后失去了多态性的优势,因为我需要为JointDist d a的每个子类型分别进行实例声明。

非常感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

经过一段时间的摸索,我认为我找到了一个使用相等约束而不是显式类型族的解决方案:

<div class="css-slider-wrapper">

</div>指示符指示GHC允许class Simulable s a b where -- generates infinite stream of random samples from a distribution samples :: s a -> IO [b] -- generates a single random sample from a distribution sample :: s a -> IO b sample = fmap head . samples instance (b ~ a) => Simulable TrivialDist a b where samples :: TrivialDist a -> IO [a] samples (Trivial x) = return $ repeat x instance {-# OVERLAPPABLE #-} (JointDist d a, b ~ [a]) => Simulable d a b where samples :: d a -> IO [[a]] samples = samples . toTrivialDist 实例与{-# OVERLAPPABLE #-}实例重叠(否则我们会收到错误)。