在Haskell的Type Classes中记录选择器

Woj*_*ilo 5 haskell types record typeclass

我想Type Class用几个默认方法实现一个,但我收到一个错误,我不能使用record selectors内部type classes定义.

下面的代码基本上创建type class了定义add函数的函数,它应该将元素添加到repr某些函数的记录中data type.这是代码:

import qualified Data.Graph.Inductive     as DG

class Graph gr a b where
    empty :: DG.Gr a b
    empty = DG.empty

    repr :: gr -> DG.Gr a b

    -- following function declaration does NOT work:
    add :: a -> gr -> gr
    add el g = g{repr = DG.insNode el $ repr g}
Run Code Online (Sandbox Code Playgroud)

编译器抛出错误:

repr is not a record selector
In the expression: g {repr = DG.insNode el $ repr g}
In an equation for add:
    add el g = g {repr = DG.insNode el $ repr g}
Run Code Online (Sandbox Code Playgroud)

是否可以在Haskell中声明这样的方法?

澄清

我需要这样的设计,因为我有一些data types,它以相似的方式表现.让我们说,我们得到了A,BC data types.他们每个人都应该有一个记录repr :: DG.Gr a b,其中ab是不同的每一个A,BC.

A,BC共享相同的功能,如adddelete(基本上添加或删除要记录的元素repr).如果这些数据类型共享许多函数,那么实现函数type class并生成其实例是有意义的type class- 这些函数将data type自动为每个函数实现.

另外我会喜欢其中的一些data types(假设我想要B)在调用add函数时表现略微不同.这是很容易做的时候,以实现这一行为instance的的type classB.

JJJ*_*JJJ 3

  1. 记录更新语法

     <record-instance> { <record-field-name> = ..., ... }
    
    Run Code Online (Sandbox Code Playgroud)

    当 是已知<record-instance>代数数据类型的实例/术语(因此它是已知字段)时才有效,在您的代码中它只是一些(临时)多态参数,因此您需要首先转换为,然后更新它,然后然后...<record-field-name>grgrGr

  2. 我认为grGr在某种意义上应该是等价的,即我们需要一个反函数repr,比如说iface,才能实现add

这是一个例子:

{-# LANGUAGE MultiParamTypeClasses, TypeSynonymInstances, FlexibleInstances #-}

data Gr a b = Gr { _internal :: [(a, b)] } deriving ( Show, Read )

class Graph gr a b where

  repr :: gr -> Gr a b
  iface :: Gr a b -> gr

  -- iface . repr == id {gr}
  -- repr . iface == id {Gr a b}

  -- add element via "interface" (get a representation via @repr@, update it, and then 
  -- return an interface back with @iface@)
  add :: (a, b) -> gr -> gr
  add el g = let r = repr g in iface r { _internal = el : _internal r }
  -- or
  add el = iface . insNode el . repr where
    insNode x (Gr xs) = Gr (x : xs) -- or whatever

instance Graph String Int Int where
  repr = read
  iface = show

test :: String
test = add (1 :: Int, 2 :: Int) "Gr { _internal = [] }"
-- test => "Gr {_internal = [(1,2)]}"
Run Code Online (Sandbox Code Playgroud)

如果某些数据类型AB 聚合 Gr a b(这样我们就无法编写 的逆repr),那么我们可以这样做:

{-# LANGUAGE MultiParamTypeClasses #-}

data Gr a b = Gr [(a, b)] deriving ( Show )

class Graph gr a b where

  repr :: gr -> Gr a b

  update :: gr -> (Gr a b -> Gr a b) -> gr
  -- 2: update :: gr -> Gr a b -> gr

  add :: (a, b) -> gr -> gr
  add el g = update g $ insNode el
    -- 2: update g (insNode el $ repr g)
    where insNode x (Gr xs) = Gr (x : xs)

data A = A { _aRepr :: Gr Char Char, _aRest :: Char } deriving ( Show )
data B = B { _bRepr :: Gr Int Int, _bRest :: Int } deriving ( Show )

instance Graph A Char Char where
  repr = _aRepr
  update r f = r { _aRepr = f $ _aRepr r }
  -- 2: update r g = r { _aRepr = g }

instance Graph B Int Int where
  repr = _bRepr
  update r f = r { _bRepr = f $ _bRepr r }
  -- 2: update r g = r { _bRepr = g }

testA :: A
testA = add ('1', '2') $ A (Gr []) '0'
-- => A {_aRepr = Gr [('1','2')], _aRest = '0'}

testB :: B
testB = add (1 :: Int, 2 :: Int) $ B (Gr []) 0
-- => B {_bRepr = Gr [(1,2)], _bRest = 0}
Run Code Online (Sandbox Code Playgroud)

也可以在这里使用镜头

{-# LANGUAGE MultiParamTypeClasses, TemplateHaskell #-}

import Control.Lens

data Gr a b = Gr [(a, b)] deriving ( Show )

insNode :: (a, b) -> Gr a b -> Gr a b
insNode x (Gr xs) = Gr (x : xs)

class Graph gr a b where
  reprLens :: Simple Lens gr (Gr a b)

add :: Graph gr a b => (a, b) -> gr -> gr
add el = reprLens %~ insNode el

data A = A { _aRepr :: Gr Char Char, _aRest :: Char } deriving ( Show )
data B = B { _bRepr :: Gr Int Int, _bRest :: Int } deriving ( Show )

makeLenses ''A
makeLenses ''B

instance Graph A Char Char where
  reprLens = aRepr

instance Graph B Int Int where
  reprLens = bRepr

main :: IO ()
main = do
  let a = A (Gr []) '0'
      b = B (Gr []) 0
  print $ add ('0', '1') a
  print $ add (0 :: Int, 1 :: Int) b
-- A {_aRepr = Gr [('0','1')], _aRest = '0'}
-- B {_bRepr = Gr [(0,1)], _bRest = 0}
Run Code Online (Sandbox Code Playgroud)