How to compare two functions for equivalence, as in (λx.2*x) == (λx.x+x)?

后端 未结 6 1713
忘掉有多难
忘掉有多难 2020-11-30 21:11

Is there a way to compare two functions for equality? For example, (λx.2*x) == (λx.x+x) should return true, because those are obviously equivalent.

6条回答
  •  心在旅途
    2020-11-30 21:35

    This is undecidable in general, but for a suitable subset, you can indeed do it today effectively using SMT solvers:

    $ ghci
    GHCi, version 8.0.1: http://www.haskell.org/ghc/  :? for help
    Prelude> :m Data.SBV
    Prelude Data.SBV> (\x ->  2 * x) === (\x -> x + x :: SInteger)
    Q.E.D.
    Prelude Data.SBV> (\x ->  2 * x) === (\x -> 1 + x + x :: SInteger)
    Falsifiable. Counter-example:
      s0 = 0 :: Integer
    

    For details, see: https://hackage.haskell.org/package/sbv

提交回复
热议问题