Does haskell keep track of function composition?

梦想的初衷 提交于 2019-12-10 03:41:38

问题


I was wondering if Haskell keeps track of weather a function is a function composition, i.e would it be possible for me to define a function that does something similar to this?:

compositionSplit f.g = (f,g)

回答1:


No, it wouldn't be possible.

For example,

f1 = (+ 1) . (+ 1) :: Int -> Int

is the same function as

f2 = subtract 1 . (+ 3) :: Int -> Int

and referential transparency demands that equals can be substituted for equals, so if compositionSplit were possible, it would

  • need to produce the same result for f1 and f2, since that is the same function, yet
  • compositionSplit f1 = ((+ 1), (+1)) and compositionSplit f2 = (subtract 1, (+ 3)) would be required by the specification of compositionSplit.



回答2:


It could. In strictly interpretational non-compiled implementation, you could represent functions as

data Function = F Source | Compo Function Function

and then you'd just define

compositionSplit (Compo f g) = Just (f,g)
compositionSplit _  = Nothing

Such implementation would treat function equality (w.r.t. referential transparency) as intensional, not extensional equality. As the language itself doesn't say anything about equality of functions AFAIK, this shouldn't affect anything (except maybe performance).

In compiled implementations this could be achieved too, e.g. by maintaining provenance for every object in memory.


AndrewC gives a winning counter-argument: for the two values a=f.(g.h) and b=(f.g).h, if we want to consider them as equal values - which we normally do, in Haskell - fst.unJust.deCompo will produce two different results, breaking referential transparency. So it can't be part of pure FP paradigm. It'd have to return something which we could legitimately consider as being equal values too, in the two cases, and we wouldn't be able to take it apart, without breaking the purity. Maybe such a thing could exist in some impure monad, but that's not what OP asked for, sadly. :) So this answer is in error.



来源:https://stackoverflow.com/questions/16825559/does-haskell-keep-track-of-function-composition

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!