使用仿函数参数化的数据类型
Working with data types parameterized by a functor
我最近定义了一个类型,我可能无法计算其字段:
data Foo = Foo {x, y :: Int, others :: NonEmpty Int}
data Input
computeX, computeY :: Input -> Maybe Int
computeOthers :: Input -> Maybe (NonEmpty Int)
现在,我可能会做的一件显而易见的事情就是使用 liftA3
:
foo :: Input -> Maybe Foo
foo i = liftA3 Foo (computeX i) (computeY i) (computeOthers i)
这很好用,但我认为将 Foo
概括为也包含 Maybe
,然后将一种类型的 Foo
转换为另一种类型可能会很有趣。在一些类似的情况下,我可以给 Foo
类型一个类型参数并派生 Traversable。然后在创建 Foo (Maybe Int)
之后,我可以用 sequenceA :: Foo (Maybe Int) -> Maybe (Foo Int)
立即反转整个事情。但这在这里不起作用,因为我的函数没有给我 NonEmpty (Maybe Int)
,它给了我 Maybe (NonEmpty Int)
.
所以我想我会尝试通过仿函数进行参数化:
data Foo f = Foo {x, y :: f Int, others :: f (NonEmpty Int)}
但接下来的问题是,如何将 Foo Maybe
变成 Maybe (Foo Identity)
?显然我可以手动编写该函数:它与上面的 liftA3
同构。但是对于这种高阶类型是否有一些与 Traversable 类似的东西,这样我就可以对这个问题应用更通用的函数而不是用定制函数重新做它?
此类数据类型称为“高等数据”(HKD)。操作它们通常使用泛型或模板 Haskell.
有像higgledy
which provide built-in functionality for HKD. I believe construct
这样的库是你要找的函数:
{-# LANGUAGE DeriveGeneric #-}
import Data.Generic.HKD
import GHC.Generics
import Data.Monoid
data Foo = Foo { x, y :: Int, z :: [Int] }
deriving (Generic, Show)
emptyFoo :: HKD Foo Last
emptyFoo = mempty
sampleFoo :: HKD Foo Last
sampleFoo = deconstruct (Foo 1 2 [3])
emptyFoo' :: Last Foo
emptyFoo' = construct emptyFoo
sampleFoo' :: Last Foo
sampleFoo' = construct sampleFoo
main = do
print emptyFoo'
print sampleFoo'
这将打印:
Last {getLast = Nothing}
Last {getLast = Just (Foo {x = 1, y = 2, z = [3])}
编辑:我刚刚发现一个更受欢迎的库是 barbies
(higgledy also depends on barbies). The function that you are looking for is also present in that library as an application of btraverse
:
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE UndecidableInstances #-}
import Data.List.NonEmpty
import Barbies
import GHC.Generics
import Data.Functor.Identity
data Foo f = Foo {x, y :: f Int, others :: f (NonEmpty Int)}
deriving (Generic, FunctorB, TraversableB, ConstraintsB)
deriving instance AllBF Show f Foo => Show (Foo f)
f :: Applicative f => Foo f -> f (Foo Identity)
f = btraverse (fmap Identity)
main :: IO ()
main = do
print (f (Foo (Just 1) (Just 2) (Just (3 :| []))))
这会打印:
Just (Foo {x = Identity 1, y = Identity 2, others = Identity (3 :| [])})
我最近定义了一个类型,我可能无法计算其字段:
data Foo = Foo {x, y :: Int, others :: NonEmpty Int}
data Input
computeX, computeY :: Input -> Maybe Int
computeOthers :: Input -> Maybe (NonEmpty Int)
现在,我可能会做的一件显而易见的事情就是使用 liftA3
:
foo :: Input -> Maybe Foo
foo i = liftA3 Foo (computeX i) (computeY i) (computeOthers i)
这很好用,但我认为将 Foo
概括为也包含 Maybe
,然后将一种类型的 Foo
转换为另一种类型可能会很有趣。在一些类似的情况下,我可以给 Foo
类型一个类型参数并派生 Traversable。然后在创建 Foo (Maybe Int)
之后,我可以用 sequenceA :: Foo (Maybe Int) -> Maybe (Foo Int)
立即反转整个事情。但这在这里不起作用,因为我的函数没有给我 NonEmpty (Maybe Int)
,它给了我 Maybe (NonEmpty Int)
.
所以我想我会尝试通过仿函数进行参数化:
data Foo f = Foo {x, y :: f Int, others :: f (NonEmpty Int)}
但接下来的问题是,如何将 Foo Maybe
变成 Maybe (Foo Identity)
?显然我可以手动编写该函数:它与上面的 liftA3
同构。但是对于这种高阶类型是否有一些与 Traversable 类似的东西,这样我就可以对这个问题应用更通用的函数而不是用定制函数重新做它?
此类数据类型称为“高等数据”(HKD)。操作它们通常使用泛型或模板 Haskell.
有像higgledy
which provide built-in functionality for HKD. I believe construct
这样的库是你要找的函数:
{-# LANGUAGE DeriveGeneric #-}
import Data.Generic.HKD
import GHC.Generics
import Data.Monoid
data Foo = Foo { x, y :: Int, z :: [Int] }
deriving (Generic, Show)
emptyFoo :: HKD Foo Last
emptyFoo = mempty
sampleFoo :: HKD Foo Last
sampleFoo = deconstruct (Foo 1 2 [3])
emptyFoo' :: Last Foo
emptyFoo' = construct emptyFoo
sampleFoo' :: Last Foo
sampleFoo' = construct sampleFoo
main = do
print emptyFoo'
print sampleFoo'
这将打印:
Last {getLast = Nothing}
Last {getLast = Just (Foo {x = 1, y = 2, z = [3])}
编辑:我刚刚发现一个更受欢迎的库是 barbies
(higgledy also depends on barbies). The function that you are looking for is also present in that library as an application of btraverse
:
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE UndecidableInstances #-}
import Data.List.NonEmpty
import Barbies
import GHC.Generics
import Data.Functor.Identity
data Foo f = Foo {x, y :: f Int, others :: f (NonEmpty Int)}
deriving (Generic, FunctorB, TraversableB, ConstraintsB)
deriving instance AllBF Show f Foo => Show (Foo f)
f :: Applicative f => Foo f -> f (Foo Identity)
f = btraverse (fmap Identity)
main :: IO ()
main = do
print (f (Foo (Just 1) (Just 2) (Just (3 :| []))))
这会打印:
Just (Foo {x = Identity 1, y = Identity 2, others = Identity (3 :| [])})