gpt4 book ai didi

haskell hlist hnil 模式匹配 hfoldl

转载 作者:行者123 更新时间:2023-12-02 21:32:53 24 4
gpt4 key购买 nike

我一直在尝试获取一些与 Data.HList 一起使用的代码。我知道我可以单独使用 ADT 做我需要做的事情,但我想看看它如何与 HList 一起工作,所以我正在尝试。但我在编译我编写的代码时遇到了问题。

{-# LANGUAGE GADTs #-}

module TestHList where

import Data.HList.CommonMain

data MyType1 = MyType1 { x::Int, y::Int } deriving (Show)
data MyType2 = MyType2 { text::String, slen::Int } deriving (Show)
data MyType3 = MyType3 { dval1::Int, dval2::String } deriving (Show)

test1 = HCons (MyType2 { text = "Hello", slen=5 })
(HCons (MyType1 { x=1, y=2 })
(HCons (MyType3 { dval1=3, dval2="World" })
HNil))

test2 = HCons (MyType1 { x=4, y=5 })
(HCons (MyType1 { x=6, y=7 })
(HCons (MyType2 { text="Again.", slen=6 })
HNil))

addType1 ls1 ls2 = hAppendList ls1 ls2


class MyTypesInt a where
sumIt :: a -> Int

instance MyTypesInt MyType1 where
sumIt val = (x val) + (y val)

instance MyTypesInt MyType2 where
sumIt val = slen val

instance MyTypesInt MyType3 where
sumIt val = (dval1 val) * 2

sumTest1 v = sumIt v
sumTest2 ls = sumIt (hHead ls)

foldTest ls = hFoldl (\(v1,v2) -> v1 + (sumIt v2)) 0 ls
sumTest3 = foldTest test1

sumAll HNil = 0
sumAll ls = (sumIt (hHead ls)) + (sumAll (hTail ls))

{-
sumAll3 xs
| xs == HNil = 0
| otherwise = (sumIt (hHead xs)) + (sumAll3 (hTail xs))
-}

该代码没有做任何有用的事情,它只是为了帮助我理解如何使用 HList。该代码声明了 3 种独立的数据类型,并创建了一个类并定义了这 3 种类型的实例。我的目标是设置一个列表,然后根据为列表中定义的实例对列表中的每个元素执行类函数 sumIt。我知道 test1、test2 addType1、sumTest1 和 sumTest2 可以工作。我得到的编译错误是针对foldTest 和sumAll 函数的。我想我需要定义函数声明但不知道如何定义。这是编译错误。

TestHList.hs:39:1:
Could not deduce (MyTypesInt a0)
arising from the ambiguity check for `foldTest'
from the context (Num z,
HFoldl ((Int, a) -> Int) z xs r,
MyTypesInt a)
bound by the inferred type for `foldTest':
(Num z, HFoldl ((Int, a) -> Int) z xs r, MyTypesInt a) =>
HList xs -> r
at TestHList.hs:39:1-55
The type variable `a0' is ambiguous
Possible fix: add a type signature that fixes these type variable(s)
Note: there are several potential instances:
instance MyTypesInt MyType3 -- Defined at TestHList.hs:33:10
instance MyTypesInt MyType2 -- Defined at TestHList.hs:30:10
instance MyTypesInt MyType1 -- Defined at TestHList.hs:27:10
When checking that `foldTest'
has the inferred type `forall z (xs :: [*]) r a.
(Num z, HFoldl ((Int, a) -> Int) z xs r, MyTypesInt a) =>
HList xs -> r'
Probable cause: the inferred type is ambiguous

TestHList.hs:42:8:
Couldn't match type `(':) * e0 l0' with '[] *
Inaccessible code in
a pattern with constructor
HNil :: HList ('[] *),
in an equation for `sumAll'
In the pattern: HNil
In an equation for `sumAll': sumAll HNil = 0

TestHList.hs:43:49:
Occurs check: cannot construct the infinite type: l0 = (':) * e0 l0
Expected type: HList ((':) * e0 ((':) * e0 l0))
Actual type: HList ((':) * e0 l0)
In the first argument of `hTail', namely `ls'
In the first argument of `sumAll', namely `(hTail ls)'
In the second argument of `(+)', namely `(sumAll (hTail ls))'

我的问题是有人知道我需要做什么来修复代码才能使其正常工作吗?我做了很多搜索来找到答案。我可能在搜索过程中看到了答案,但我只是不理解它。

谢谢

更新:

在研究我得到的答案中的想法时,我遇到了这个链接:http://en.wikibooks.org/wiki/Haskell/Existentially_quantified_types

读完这篇文章后,很容易实现我想要做的事情。我不会改变答案。我的问题具体是关于如何让我的代码与 Data.HList 一起使用,并且提供的答案做得很好。但我的目的是弄清楚如何设置和使用异构列表,当时我认为 Data.HList 就是实现这一目标的方法。下面的代码对我来说更容易理解,所以我想提供它以防其他人发现它有用。

{-# LANGUAGE ExistentialQuantification #-}

module TestHeterList where

data MyType1 = MyType1 { x::Int, y::Int } deriving (Show)
data MyType2 = MyType2 { text::String, slen::Int } deriving (Show)
data MyType3 = MyType3 { dval1::Int, dval2::String } deriving (Show)

class MyTypesInt a where
sumIt :: a -> Int

instance MyTypesInt MyType1 where
sumIt val = (x val) + (y val)

instance MyTypesInt MyType2 where
sumIt val = slen val

instance MyTypesInt MyType3 where
sumIt val = (dval1 val) * 2

data GenElem = forall s. (Show s, MyTypesInt s) => GE s
instance Show GenElem where
show (GE s) = show s

test1 :: [GenElem]
test1 = [GE (MyType2 { text = "Hello", slen=5 }), GE (MyType1 { x=1, y=2 }), GE (MyType3 { dval1=3, dval2="World" })]

foldTest xs = foldl (\acc (GE val) -> acc + sumIt val) (0::Int) xs
sumTest1 = foldTest test1

sumAll [] = 0
sumAll (GE v : xs) = (sumIt v) + (sumAll xs)

sumTest2 = sumAll test1

最佳答案

以下是如何使基于 hFoldl 的变体发挥作用:

data HSumAll = HSumAll
instance (MyTypesInt a, int ~ Int) => ApplyAB HSumAll (Int, a) int where
applyAB HSumAll (v1, v2) = v1 + sumIt v2

foldTest ls = hFoldl HSumAll (0 :: Int) ls
sumTest3 = foldTest test1

让直接版本工作更加棘手。首先,您必须使用模式匹配,因为 HList 是一个 GADT,如果您使用选择器函数,则类型细化不可能起作用。此外,GADT 上的函数匹配需要显式类型签名。所以你最终会得到这样的结果:

sumAll :: HList ls -> Int -- WRONG
sumAll HNil = 0
sumAll (HCons x xs) = sumIt x + sumAll xs

这会产生以下类型错误:

Could not deduce (MyTypesInt e) arising from a use of `sumIt' from the context (ls ~ (':) * e l1) ...

GHC 的提示当然是正确的。我们需要 ls 中的所有类型作为实例MyTypesInt。我浏览了 HList 包,看看该库是否提供了一种方法表达这一点,但在我看来,事实并非如此。幸运的是,这相对容易做到如今(需要 ConstraintKinds 并导入 GHC.Exts 才能访问 Constraint):

type family All (c :: * -> Constraint) (xs :: [*]) :: Constraint
type instance All c '[] = ()
type instance All c (x ': xs) = (c x, All c xs)

然后你可以说:

sumAll :: All MyTypesInt ls => HList ls -> Int
sumAll HNil = 0
sumAll (HCons x xs) = sumIt x + sumAll xs

此类型检查并按预期工作。

关于haskell hlist hnil 模式匹配 hfoldl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23003282/

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