Haskell排名n类型&类型语法

时间:2016-09-27 07:52:12

标签: haskell polymorphism

在学习Haskell时,我试图理解Haskell中clojure传感器的类型。

{-# LANGUAGE RankNTypes #-}

module Transducers where

-- r = reduced
type Reducer r a = r -> a -> r

type Transducer a b = forall r . Reducer r a -> Reducer r b

我无法理解如何输入以下功能:

-- type inference
transduce :: Foldable t => (t1 -> b -> a -> b) -> t1 -> b -> t a -> b
-- what I actually want
transduce :: forall t1 . Foldable t => Transducer a b -> t1 -> b -> t a -> b
transduce xform f init coll = foldl (xform f) init coll

这给我带来麻烦,它不会编译。我错过了语法方面的东西吗?或者这不可能吗?

1 个答案:

答案 0 :(得分:1)

在我看来,你可能意味着类似

transduce :: Foldable t => Transducer a b -> Reducer r a -> r -> t b -> r

作为user2407038 suggested,如果您想强制调用者提供Transducer,您只需要这样一种花哨的类型。否则,您可以将其简化为

transduce :: Foldable t => (x -> Reducer r b) -> x -> r -> t b -> r