gpt4 book ai didi

haskell - 如何约束关联类型同义词?

转载 作者:行者123 更新时间:2023-12-04 15:00:57 25 4
gpt4 key购买 nike

我(相对)是 Haskell 的新手,想编写一些数学代码。例如,阿贝尔群。我想编写以下代码:

{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE TypeSynonymInstances #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeFamilyDependencies #-}

module Structures where

class Eq g => AbelianGroup g where
type family AbelianGroupElement g = e | e -> g
add :: AbelianGroupElement g -> AbelianGroupElement g -> AbelianGroupElement g
inv :: AbelianGroupElement g -> AbelianGroupElement g
unit :: g -> AbelianGroupElement g
parent :: AbelianGroupElement g -> g

data Z = Z deriving Eq
data ZElement = ZElement Z Int deriving Eq

instance AbelianGroup Z where
type instance AbelianGroupElement Z = ZElement
add (ZElement z1 x1) (ZElement z2 x2)
| z1 == z2 = (ZElement z1 (x1+x2))
| otherwise = error "elements from different groups"
inv (ZElement z x) = (ZElement z (-x))
unit z = ZElement z 0
parent (ZElement z x) = z

data ProductAbGrp g1 g2 =
ProductAbGrp g1 g2 deriving Eq
data ProductAbGrpEl g1 g2 =
ProductAbGrpEl (ProductAbGrp g1 g2) (AbelianGroupElement g1) (AbelianGroupElement g2) deriving Eq
编译上面给了我错误
No instance for (Eq (AbelianGroupElement g1))
arising from the second field of `ProductAbGrpEl'
(type `AbelianGroupElement g1')
这是有道理的;我还没有确保 (AbelianGroupElement g1) 总是为它定义 Eq 。但是,我不确定如何才能做到这一点。我可以将以上更改为
{-# LANGUAGE FlexibleContexts #-} 
...
class (Eq g, Eq (AbelianGroupElement g)) => AbelianGroup g where
但这无济于事。 (类型系列可能是错误的方式;我最初是从 MultiParamTypeClasses 和 FunctionalDependencies 开始的,但遇到了其他问题,并得到了类型系列更好的印象)。
感谢您阅读本文;任何帮助,将不胜感激。

最佳答案

您可以使用 StandaloneDeriving 基本上得到你想要的东西,虽然我承认它不像简单地写 deriving Eq 那样漂亮:

{-# LANGUAGE StandaloneDeriving #-}

deriving instance (Eq g1, Eq g2, Eq (AbelianGroupElement g1), Eq (AbelianGroupElement g2)) => Eq (ProductAbGrpEl g1 g2)

关于haskell - 如何约束关联类型同义词?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66958140/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com