gpt4 book ai didi

haskell - 使用 Template Haskell,如何将相同的类型拼接到多个位置?

转载 作者:行者123 更新时间:2023-12-04 02:21:41 26 4
gpt4 key购买 nike

我正在定义 vector-space 中的类的实例对于OpenGL类型,为了节省我的打字肌肉,我想使用 Template Haskell 为我编写一堆实例。

我通过定义函数来为 AdditiveGroup 派生实例从小处着手。 :

{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeFamilies #-}
module Data.VectorSpace.OpenGL.TH where

import Control.Applicative
import Control.Monad
import Data.AdditiveGroup
import Data.VectorSpace

import Language.Haskell.TH

deriveScalarAdditive ts = concat <$> forM (map conT ts) (\t -> [d|
instance AdditiveGroup $t where zeroV = 0; (^+^) = (+); negateV = negate
|])

这工作正常,但请注意,我只是拼接 $t一次进入牛津括号。现在,导出 VectorSpace 的函数实例:
deriveScalarVectorSpace ts = concat <$> forM (map conT ts) (\t -> [d|    
instance VectorSpace $t where type Scalar $t = $t; (*^) = (*)
|])

但是,这很糟糕:
Type indexes must match class instance head
Found `t_tt' but expected `t_ts'
In the associated type instance for `Scalar'
In the instance declaration for `VectorSpace $t'
In the Template Haskell quotation
[d| instance VectorSpace $t where
type instance Scalar $t = $t
{ *^ = (*) } |]
t_ts 之间的区别和 t_tt在错误中告诉我,每次我在 $t 中拼接时,TH 都会创建一个新的唯一名称,当然,只有当这些类型相同时,该定义才有效。

有没有办法使用牛津括号获得我想要的行为,或者我是否必须退回到良好的旧词法范围和 Language.Haskell.TH组合器?我知道使用 CPP 可能会更容易,但我想借此机会学习一些 TH。

最佳答案

我认为您必须使用 Language.Haskell.TH组合器。请看以下门票:

  • GHC 4230
  • GHC 4135

  • 这样做非常简单。我会从这个开始(稍微格式化)
    *Foo Language.Haskell.TH> runQ (deriveScalarAdditive [''Int] ) >>= print

    [InstanceD [] (AppT (ConT Data.AdditiveGroup.AdditiveGroup) (ConT GHC.Types.Int))
    [ValD (VarP zeroV_12) (NormalB (LitE (IntegerL 0))) [],
    ValD (VarP ^+^_13) (NormalB (VarE GHC.Num.+)) [],
    ValD (VarP negateV_14) (NormalB (VarE GHC.Num.negate)) []]
    ]

    从这里,很容易了解如何使用组合器构造实例。另请注意,您可以将引用与 TH 混合使用,引用表达式 [| some code |] :: ExpQ ,这对于创建函数体通常很有用。

    关于haskell - 使用 Template Haskell,如何将相同的类型拼接到多个位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8410761/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com