默认值受haskell中的存在约束

时间:2016-03-21 12:45:13

标签: haskell typeclass existential-type

考虑以下用于定义类型对的类型类:

class Constraint a b where
  g :: a -> b

对于所有约束实例,我们可以派生一组类型a,本质上是一个隐式类型类,我们称之为A。对于类型类A的每个实例,还有另一个隐式类型类B,其中包含b的所有可能类型Constraint A b

所以这是一段代码。

{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE MultiParamTypeClasses #-}
import Debug.Trace

-- Contraining class
class (Show a, Show b) => QandA a b where
  g :: a -> b

-- Some data types
data A = A1 | A2 deriving (Show, Eq)
data B = B1 | B2 deriving (Show, Eq)
data C = C1 | C2 deriving (Show, Eq)

instance QandA A B where
  g A1 = B1
  g A2 = B2

instance QandA A C where
  g A1 = C1
  g A2 = C2

-- We want to define a set of types that includes all the types that
-- have Constraint a b given a. This can be done via an intermediate
-- type.
data DefaultAnswer q = forall a . (DefaultingQuestion q, QandA q a) => DefaultAnswer {answer :: a};

-- Polymorphism
class DefaultingQuestion q where
  def :: DefaultAnswer q

instance DefaultingQuestion A where
  def = DefaultAnswer C1

哪个类似于ghci

> (def :: DefaultAnswer A)
(def :: DefaultAnswer A) :: DefaultAnswer A

但是

> answer (def :: DefaultAnswer A)
<interactive>:574:1:
    Cannot use record selector "answer" as a function due to escaped type variables
    Probable fix: use pattern-matching syntax instead
    In the expression: answer (def :: DefaultAnswer A)
    In an equation for "it": it = answer (def :: DefaultAnswer A)

现在我理解它的方式是,因为我使用存在类型,GHC并不真正寻找answer的类型,它只是确保即使它无法搞清楚也可能存在它是哪一个。那么当我真的想要运行answer时,它无法弄清楚如何处理它。

所以我的问题是:有没有办法为实现DefaultingQuestion的每种类型定义默认答案

1 个答案:

答案 0 :(得分:1)

为什么不:

import Data.Proxy

class DefaultingQuestion q a where
  def :: Proxy q -> a

instance DefaultingQuestion A where
  def _ = C1