Haskell 单子中的变分函数
我有一类可变函数族:Haskell 单子中的变分函数,haskell,type-families,Haskell,Type Families,我有一类可变函数族: type family (~~>) (argTypes :: [Type]) (result :: Type) :: Type where '[] ~~> r = r (t ': ts) ~~> r = t -> (ts ~~> r) infixr 0 ~~> 我想要一个变量函数,它将一些单变量动作(比如print)应用于它的所有参数: class Foo (ts :: [Type]) where foo :: ts ~~
type family (~~>) (argTypes :: [Type]) (result :: Type) :: Type where
'[] ~~> r = r
(t ': ts) ~~> r = t -> (ts ~~> r)
infixr 0 ~~>
我想要一个变量函数,它将一些单变量动作(比如print)应用于它的所有参数:
class Foo (ts :: [Type]) where
foo :: ts ~~> IO ()
instance Foo '[] where
foo = pure ()
instance (Show t, Foo ts) => Foo (t ': ts) where
foo t = print t >> foo @ts
通常的一元结构在这里不起作用。
(>>)
具有类型IO()->IO()->IO()
。我需要使用IO()->(ts~~~>IO())->ts~~~>IO()
类型的东西来编写print t t
和foo@ts
可以编写这样的函数吗?连续传递样式可以直接访问计算结果 另一种方法是构建一个类型类来迭代组合,但这很麻烦
{-# LANGUAGE FlexibleInstances #-}
class Foo t where
foo_ :: (IO () -> IO ()) -> t
instance (Show a, Foo t) => Foo (a -> t) where
foo_ k a = foo_ (\continue -> k (print a >> continue))
instance Foo (IO ()) where
foo_ k = k (return ())
foo :: Foo t => t
foo = foo_ id
main :: IO ()
main = foo () (Just "bar") [()]
我找到了一种编写函数
IO()->(ts~~~>IO())->ts~~~>IO()
因此,有关实例变成:
instance (Show t, Foo ts, BindV IO () () ts) => Foo (t ': ts) where
foo t = thenV @IO @() @() @ts (print t) (foo @ts)
我希望我能以操作员的身份编写thenV
和bindV
,但是TypeApplications
不能与操作员一起工作
instance (Show t, Foo ts, BindV IO () () ts) => Foo (t ': ts) where
foo t = thenV @IO @() @() @ts (print t) (foo @ts)