转换Haskell中的函数以指向无符号

时间:2018-11-02 09:46:57

标签: haskell pointfree

我在纸上有一个haskell函数,例如:

function2 a b c = (a * b) + c 

,我需要用无点表示法编写示例。我真的很擅长使用无点样式,因为我发现它在没有适当指导的情况下确实令人困惑,因此我尝试了一下:

function2 a b c = (a * b) + c
function2 a b c = ((*) a b) + c #operator sectioning
function2 a b c = (+) ((*) a b)c #operator sectioning once more
#I'm stuck here now

我不确定接下来会发生什么,因为这是我在此示例中可能想到的限制。希望对此有所帮助。

-第二个示例:

function3 a b = a `div` (g b)
function3 a b = `div` a (g b) --operator sectioning
function3 a b = (`div` a) (g b) --parentheses
function3 a b = ((`div` a g).)b --B combinator
function3 a   = ((`div` a g).) --eta conversion
function3 a   = ((.)(`div` a g)) --operator sectioning
function3 a   = ((.)flip(`div` g a))
function3 a   = ((.)flip(`div` g).a) --B combinator
function3     = ((.)flip(`div` g)) --eta conversion (complete)

2 个答案:

答案 0 :(得分:5)

您可以在此处应用 B 组合器(即(f . g) x = f (g x)):

function2 a b c = (a * b) + c
function2 a b c = ((*) a b) + c    -- operator sectioning
function2 a b c = (+) ((*) a b) c  -- operator sectioning once more
      = (+) (((*) a) b) c          -- explicit parentheses
      = ((+) . ((*) a)) b c        -- B combinator
      = ((.) (+) ((*) a)) b c      -- operator sectioning 
      = ((.) (+) . (*)) a b c      -- B combinator

实际上类型是相同的

> :t let function2 a b c = (a * b) + c in function2
let function2 a b c = (a * b) + c in function2
  :: Num a => a -> a -> a -> a

> :t ((.) (+) . (*))
((.) (+) . (*)) :: Num b => b -> b -> b -> b

我们以正确的顺序逐个抽取参数以得出

function2 a b c = (......) a b c

,因此可以应用 eta-contraction 摆脱显式参数,

function2       = (......) 

我们的工具

,我们可以在两个方向上应用
S a b c  =  (a c) (b c)  =  (a <*> b) c
K a b    =  a            =  const a b
I a      =  a            =  id a
B a b c  =  a (b c)      =  (a . b) c
C a b c  =  a c b        =  flip a b c
W a b    =  a b b        =  join a b
U a      =  a a          -- not in Haskell: `join id` has no type

还有(f =<< g) x = f (g x) x = join (f . g) x

当我们使用Pointfree一段时间后,会出现更多有用的模式:

((f .) .) g x y = f (g x y)
(((f .) .) .) g x y z = f (g x y z)
.....
((. g) . f) x y = f x (g y)
((. g) . f . h) x y = f (h x) (g y)

(更新。)在第二个示例的开头附近有一个错误,使后面的所有以下步骤无效:

function3 a b = a `div` (g b)
function3 a b = -- `div` a (g b)     -- wrong syntax, you meant
                div a (g b)
function3 a b = -- (`div` a) (g b)   -- wrong; it is
                (a `div`) (g b) --operator sectioning
function3 a b = ((a `div`) . g) b --B combinator
function3 a   = (div a . g) --eta conversion; back with plain syntax
function3 a   = (.) (div a) g --operator sectioning
function3 a   = flip (.) g (div a) --definition of flip
function3 a   = (flip (.) g . div) a --B combinator
function3     = (flip (.) g . div) --eta conversion
              = (.) (flip (.) g) div  --operator section

是的,有些步骤是朝正确的方向进行的。

答案 1 :(得分:3)

我们可以继续:

function2 a b = (+) ((*) a b)     [eta-reduction]
function2 a = (+) . (*) a         [using a dot, to eliminate the b]
function2 = ((.) . (.)) (+) (*)   ["blackbird" operator to pass two parameters]

此处"blackbird operator"是三个(.) :: (b -> c) -> (a -> b) -> a -> c运算符的组合。它在功能上等同于:

((.) . (.)) :: (c -> d) -> (a -> b -> c) -> a -> b -> d
((.) . (.)) f g x y = f (g x y)

在这里查看derivation