4

是否可以创建一个在编译时从模板haskell引号之外重写haskell代码的函数?

例如:

differentiate :: Floating a => (a -> a) -> a -> (a,a)
differentiate = -- what goes here?

f :: Num a => a -> a
f = sin

g :: Num a => a -> (a,a)
g = differentiate f

并在编译时将 g 转换为:

g x = (sin x, cos x)

我希望我的“区分”函数被传递给“f”的 AST,并让我在编译之前重写它。据我所知,您不能在模板 haskell 中执行此操作,除非将函数的完整语法传递给它,即“g =区分罪”。

谢谢

4

2 回答 2

8

您正在谈论方案中的宏。答案是不。Haskell 函数必须是“引用透明的”,这意味着如果给它两个指称相等的参数,则结果必须在指称上相等。即,每个 f人都必须拥有

f (1 + 1) = f 2

如果f是宏,则不一定如此。然而,这个属性对于语言的“纯粹性”是必不可少的——是什么让 Haskell 能够很好地推理和重构。

然而,在 Haskell 中有大量关于自动微分的工作,其中没有一个需要宏系统——抽象建模(以及使它看起来更漂亮的类型类)就足够了。

于 2012-05-17T21:56:34.160 回答
1

如果您愿意使用自己的一组数学函数和数字,理论上是可以做到的。您需要做的是创建一个类型系统来跟踪每个函数的计算方式。这将反映在表达式的类型中。使用模板 haskell 和 reify 函数,或者使用类型类代码,您可以在编译时生成正确的代码。

这是一个使用类型类的 hacky 示例实现。它适用于 sin、cos、常数和加法。实施全套操作将是很多工作。此外,代码中有相当多的重复,如果您打算使用这种方法,您应该尝试解决该问题:

{-# LANGUAGE ScopedTypeVariables, UndecidableInstances, FlexibleInstances, MultiParamTypeClasses, FunctionalDependencies #-}
module TrackedComputation where
import Prelude hiding (sin, cos, Num(..))
import Data.Function (on)
import qualified Prelude as P    

-- A tracked computation (TC for short).
-- It stores how a value is computed in the computation phantom variable
newtype TC newComp val = TC { getVal :: val }
    deriving (Eq)

instance (Show val) => Show (TC comp val) where
    show = show . getVal


data SinT comp = SinT
data CosT comp = CosT

data AddT comp1 comp2 = AddT

data ConstantT = ConstantT

data VariableT = VariableT

sin :: (P.Floating a) => TC comp1 a -> TC (SinT comp1) a
sin = TC . P.sin . getVal
cos :: (P.Floating a) => TC comp1 a -> TC (CosT comp1) a
cos = TC . P.cos . getVal

(+) :: (P.Num a) => TC comp1 a -> TC comp2 a -> TC (AddT comp1 comp2) a
(TC a) + (TC b) = TC $ (P.+) a b

toNum :: a -> TC ConstantT a
toNum = TC

class Differentiate comp compRIn compROut | comp compRIn -> compROut where
    differentiate :: P.Floating a => (TC VariableT a -> TC comp a) -> (TC compRIn a -> TC compROut a)


instance Differentiate ConstantT compIn ConstantT where
    differentiate _ = const $ toNum 0

instance Differentiate (SinT VariableT) compIn (CosT compIn) where
    differentiate _ = cos
instance Differentiate VariableT compIn (ConstantT) where
    differentiate _ = const $ toNum 1

instance (Differentiate add1 compIn add1Out, Differentiate add2 compIn add2Out) =>
    Differentiate (AddT add1 add2) compIn (AddT add1Out add2Out) where
    differentiate _ (val :: TC compROut a) = result where
        first = differentiate (undefined :: TC VariableT a -> TC add1 a) val :: TC add1Out a
        second = differentiate (undefined :: TC VariableT a -> TC add2 a) val :: TC add2Out a
        result = first + second

instance P.Num val => P.Num (TC ConstantT val) where
    (+) = (TC .) . ((P.+) `on` getVal)
    (*) = (TC .) . ((P.*) `on` getVal)
    abs = (TC) . ((P.abs) . getVal)
    signum = (TC) . ((P.signum) . getVal)
    fromInteger = TC . P.fromInteger

f x = sin x

g = differentiate f

h x = sin x + x + toNum 42 + x

test1 = f . toNum
test2 = g . toNum
test3 = differentiate h . toNum
于 2012-05-18T09:09:16.173 回答