Haskell function from (a -> [b]) -> [a -> b]

后端 未结 5 2021
有刺的猬
有刺的猬 2021-01-12 14:06

I have a function seperateFuncs such that

seperateFuncs :: [a -> b] -> (a -> [b])
seperateFuncs xs = \\x -> map ($ x) xs
         


        
5条回答
  •  梦谈多话
    2021-01-12 14:52

    "Is there some datatype f which has a function :: (a -> f b) -> f (a -> b)?"

    In fact, there is an even more general version of this function in the Traversable type class, which deals with commutable functors:

    class (Functor t, Foldable t) => Traversable t where
    
      ... 
    
      sequenceA :: Applicative f => t (f b) -> f (t b)
    

    How is this related to your function? Starting from your type, with one type substitution, we recover sequenceA:

    1. (a -> f b) -> f (a -> b) ==> let t = (->) a
    2. t (f b) -> f (t b)

    However, this type has the constraint that t must be a Traversable -- and there isn't a Traversable instance for (->) a, which means that this operation can't be done in general with functions. Although note that the "other direction" -- f (a -> b) -> (a -> f b) works fine for all functions and all Applicatives f.

提交回复
热议问题