情况
我有函数f,我想用函数g对其进行扩展,从而生成名为h的函数。
定义
在一般情况下,我的意思是:转换函数f的输入(一个或多个参数)或输出(返回值)。
在具体情况下,我的意思是:只转换函数f的输出(返回值),同时保持所有参数不变。
所谓“透明”,在“增强”的背景下(一般情况和具体情况),我的意思是:尽可能松散地将g的实现与f的实现相结合。
具体案例
在我目前的情况下,我需要这样做:
h a b c = g $ f a b c
我有兴趣把它改写成这样:
h = g . f -- Doesn't type-check.
因为从h和g的角度来看,f采用什么参数并不重要,他们只关心返回值,因此以任何方式提及参数都是紧密耦合的。例如,如果f的参数在将来发生变化,那么h也需要更改。
到目前为止
我在#haskell频道上问lambdabot:@pl h a b c = g $ f a b c,我得到了回复:
h = ((g .) .) . f
这仍然不够好,因为(.)的数量取决于f的参数数。
一般情况
我在这个方向上没有做太多的研究,但是#haskell上的erisco向我指出了http://matt.immute.net/content/pointless-fun,这向我暗示一般情况下的解决方案是可能的。
到目前为止
使用Luke在上面文章中定义的函数,这似乎相当于我们到目前为止所讨论的内容:
h = f $. id ~> id ~> id ~> g
但是,如果我们想要转换f的返回值--就像以前的方法一样--这个方法似乎也受到了依赖于f参数数量的影响。
工作实例
例如,在JavaScript中,可以实现这样的透明增强:
function h () { return g(f.apply(this, arguments)) }
问题
在Haskell中,一个函数如何“透明地增强”?
我主要对具体案件感兴趣,但也很高兴知道如何处理一般案件。
发布于 2014-04-17 04:37:13
从技术上讲,只要有足够的IncoherentInstances,您几乎可以做任何事情:
{-# LANGUAGE MultiParamTypeClasses, TypeFamilies,
FlexibleInstances, UndecidableInstances, IncoherentInstances #-}
class Augment a b f h where
augment :: (a -> b) -> f -> h
instance (a ~ c, h ~ b) => Augment a b c h where
augment = ($)
instance (Augment a b d h', h ~ (c -> h')) => Augment a b (c -> d) h where
augment g f = augment g . f
-- Usage
t1 = augment not not
r1 = t1 True
t2 = augment (+1) (+)
r2 = t2 2 3
t3 = augment (+1) foldr
r3 = t3 (+) 0 [2,3]发布于 2014-04-16 09:53:24
您可以对其进行排序,但由于无法为所有非函数的对象指定行为,因此对于您所关心的所有其他类型,您将需要大量琐碎的实例。
{-# LANGUAGE TypeFamilies, DefaultSignatures #-}
class Augment a where
type Result a
type Result a = a
type Augmented a r
type Augmented a r = r
augment :: (Result a -> r) -> a -> Augmented a r
default augment :: (a -> r) -> a -> r
augment g x = g x
instance Augment b => Augment (a -> b) where
type Result (a -> b) = Result b
type Augmented (a -> b) r = a -> Augmented b r
augment g f x = augment g (f x)
instance Augment Bool
instance Augment Char
instance Augment Integer
instance Augment [a]
-- and so on for every result type of every function you want to augment...示例:
> let g n x ys = replicate n x ++ ys
> g 2 'a' "bc"
"aabc"
> let g' = augment length g
> g' 2 'a' "bc"
4
> :t g
g :: Int -> a -> [a] -> [a]
> :t g'
g' :: Int -> a -> [a] -> Int发布于 2014-04-16 08:58:10
问题是,像a -> b -> c这样的东西的真正返回值不是c,而是b -> c。您想要的东西需要某种测试来告诉您某个类型是否是函数类型。您可以列举您感兴趣的类型,但这并不好。我认为HList以某种方式解决了这个问题,看看纸。我设法用重叠的实例理解了一些解决方案,但其他的恐怕有点超出了我的想法。
https://stackoverflow.com/questions/23103654
复制相似问题