Thanks for the answers of my other quesion - How to write a Heterogeneous list on HList? , I could start using HList: Heterogeneous lists, mostly with API of https://hackage.haskell.org/package/HList-0.5.2.0/docs/Data-HList-HList.html
Now, I want - producing homogenous lists as HList l -> [e]
hMapOut :: forall f e l. HMapOut f l e => f -> HList l -> [e]
I've been trying create my own proof of concept code, but don't know how to do it properly.
import Data.HList (HList, hBuild, hEnd, hMap, hMapOut)
iA :: [Int]
iA = [1, 2, 3] :: [Int]
iB :: [[Char]]
iB = ["foo", "bar"] :: [[Char]]
iAiB :: HList '[[Int], [[Char]]]
iAiB = hEnd $ hBuild iA iB
flag :: Any -> Bool
flag = \iX -> length iX >= 3 --- ERROR HERE
flags :: HList l -> [e]
flags = \iList ->
iList & hMapOut flag
iBool :: [e]
iBool = flags iAiB
main :: IO ()
main = print iBool
This is the simplest example, and for my purpose, it needs to be wrapped into IO, in either way, it has the identical error.
• Couldn't match expected type ‘t0 a0’ with actual type ‘Any’
• In the first argument of ‘length’, namely ‘x’
In the first argument of ‘(>=)’, namely ‘length x’
In the expression: length x >= 3 typecheck(-Wdeferred-type-errors)
I suppose Any type is wrong, but I don't know how to type. Advice?
I also did
flag :: forall (t :: * -> *) a. Foldable t => t a -> Bool
flag = \iX -> length iX >= 3
This eliminates the error for this function, but now another error on
flags :: HList l -> [e]
flags = \iList ->
iList & hMapOut flag --- ERROR HERE
as
• Ambiguous type variables ‘t0’,
‘a0’ arising from a use of ‘hMapOut’
prevents the constraint ‘(Data.HList.HList.HFoldr
So, which seems to be the fundamental problem.
tried Fun'
import Data.HList (HList, hBuild, hEnd, hMap, hMapOut, Fun' (Fun'))
flags :: HList l -> [e]
flags = \iList ->
iList & hMapOut (Fun' flag :: Fun' Foldable Bool)
error:
• Could not deduce: b ~ Bool
from the context: Data.HList.FakePrelude.FunCxt Foldable b
bound by a type expected by the context:
forall b.
Data.HList.FakePrelude.FunCxt Foldable b =>
Data.HList.FakePrelude.FunApp Bool b -> b