0

I want to write a class like this:

class C c where
    op :: c -> c -> Bool

class A b => B b where
    func :: C c => b -> c -- ^ type 'c' is random(forall). 
    func2 :: b -> b -> Bool
    func2 x y = func b `op` func c

Here, c is a type restricted by C and this restriction will be used in func2. But this cannot be compiler. Type c is not a real type. I try to add forall or using TypeFamilies, but none of them can do this. TypeFamilies looks good, but it cannot use with restriction in funcion definition like C c => b -> c or `type X x :: C * => *.

Must I use (A b, C c) => B b c to define this class? I have another class using with B like B b => D d b. If adding a param for class B, the D class needs one more param as well. In fact, Seq a will be used with class D, which cannot match D d b.

EDIT: one more description.

{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FlexibleInstances #-}
module Main where

type Ta = (Integer, Integer)
newtype Tb t = Tb { tb :: [t] } deriving Show

class Eq a => A a where
    a1f :: Ord b => a -> b
    a2f :: a -> a -> Bool
    a2f x y = a1f x >= a1f y

instance A Ta where
    a1f (_, y) = y

class A a => B b a where
    op :: b a -> b a

instance B Tb Ta where
    op x = x

main :: IO ()
main = putStrLn $ show $ op $ (Tb [(1, 1)] :: Tb Ta)

Compiler will complain with the line a2f :: b -> Bool:

    • Could not deduce (Ord a0) arising from a use of ‘>=’
      from the context: A a
        bound by the class declaration for ‘A’ at test.hs:10:15
      The type variable ‘a0’ is ambiguous
      These potential instances exist:
        instance Ord Ordering -- Defined in ‘GHC.Classes’
        instance Ord Integer
          -- Defined in ‘integer-gmp-1.0.2.0:GHC.Integer.Type’
        instance Ord a => Ord (Maybe a) -- Defined in ‘GHC.Maybe’
        ...plus 22 others
        ...plus four instances involving out-of-scope types
        (use -fprint-potential-instances to see them all)
    • In the expression: a1f x >= a1f y
      In an equation for ‘a2f’: a2f x y = a1f x >= a1f y

EDIT2: Use type families

...
class Eq a => A a where
    type AT a :: *
    a1f :: Ord (AT a) => a -> AT a
    a2f :: a -> a -> Bool
    a2f x y = a1f x >= a2f y

instance A Ta where
    type AT Ta = Integer
    a1f (_, y) = y
...

It will show error with:

    • Could not deduce (Ord (AT a)) arising from a use of ‘>=’
      from the context: A a
        bound by the class declaration for ‘A’ at test.hs:10:15
    • In the expression: a1f x >= a1f y
      In an equation for ‘a2f’: a2f x y = a1f x >= a1f y
4

2 回答 2

1

在您的代码中,问题只是cin func b `op` func cis ambiguous。这并不是什么大问题:只需使用本地签名确定选择即可。例如

func2 x y = func x `op` (func y :: Int)

但这可能不是你真正想要的。c真的应该是类的类型参数,func还是整个实例的类型参数?在后一种情况下,MPTC 将是正确的方法。

{-# LANGUAGE MultiParamTypeClasses, AllowAmbiguousTypes, TypeApplications #-}

class ∀ b c . (A b, C c) => B b c where
  func :: b -> c
  func2 :: b -> b -> Bool
  func2 x y = func @b @c b `op` func c

或者,如果对于每个实例,只有一个c有意义,那么您需要一个类型系列或基金。

{-# LANGUAGE TypeFamilies #-}

class A b => B b where
  type Ct b :: *
  func :: b -> Ct b
  func2 :: b -> b -> Bool
  func2 x y = func b `op` func c
于 2019-09-23T12:32:19.183 回答
1

对您的类型族代码进行编译的最小修复是将Ord约束的需求从产生它的地方移到消费它的地方:

{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeSynonymInstances #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE ConstrainedClassMethods #-}

type Ta = (Integer, Integer)

class Eq a => A a where
    type AT a :: *
    a1f :: a -> AT a
    a2f :: Ord (AT a) => a -> a -> Bool
    a2f x y = a1f x >= a1f y

instance A Ta where
    type AT Ta = Integer
    a1f (_, y) = y

如果您只想Ord (AT a)在使用默认实现时要求,您可以使用DefaultSignatures(并消除ConstrainedClassMethods):

{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeSynonymInstances #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE DefaultSignatures #-}

type Ta = (Integer, Integer)

class Eq a => A a where
    type AT a :: *
    a1f :: a -> AT a
    a2f :: a -> a -> Bool
    default a2f :: Ord (AT a) => a -> a -> Bool
    a2f x y = a1f x >= a1f y

instance A Ta where
    type AT Ta = Integer
    a1f (_, y) = y

但是,这种类型类结构非常奇怪和单一。(当我读到它时它提出了一些危险信号:那个Eq约束在那里做什么?为什么只有一个实例的类?为什么a2f在类内部而不是外部?为什么不a1f只是一个非类多态函数?为什么我们应该相信每种类型只有一个规范选择函数?)

我想重申一下,您应该告诉我们更多关于您要通过此实现的目标,而不是谈论您为实现该目标而提议的类型类。这种架构的很多内容都在尖叫“初学者尝试像 OO 语言使用类的方式一样使用类型类”,这将成为阻抗不匹配和剪纸的持续来源。我强烈怀疑你根本不应该定义一个新的类型类。

于 2019-09-24T15:19:43.063 回答