多态函数中的GADT
GADT in polymorphic function
我有一个函数有问题,我想使其具有多态性。
我想以分析或数字方式整合功能。在分析整合时,我提供结果。
在数字情况下,我想使用各种方法,目前,tanhsinh 中的 tanhsinh 例程。
我也想了解更多关于gadts的知识,所以我试图找到一个使用它们的解决方案。
到目前为止我有以下内容:
import qualified Data.VectorSpace as DV
import Numeric.Integration.TanhSinh
data IntegrationType a b where
MkAnalytic :: (DV.AdditiveGroup b) => (c -> b) -> c -> c -> IntegrationType Analytic b
MkNumeric :: NumericType -> IntegrationType Numeric [Result]
data Analytic = Analytic
data Numeric = Numeric
data Method = Trapez | Simpson
data IntBounds = Closed | NegInfPosInf | ZeroInf
data NumericType = MkSingleCoreTanhSinh IntBounds Method (Double -> Double) Double Double
| MkParallelTanhSinhExplicit IntBounds (Strategy [Double]) Method (Double -> Double) Double Double
| MkParallelTanhSinh IntBounds Method (Double -> Double) Double Double
integrate :: IntegrationType a b -> b
integrate (MkAnalytic f l h) = f h DV.^-^ f l
integrate (MkNumeric (MkSingleCoreTanhSinh Closed Trapez f l h )) = trap f l h
integrate (MkNumeric (MkSingleCoreTanhSinh Closed Simpson f l h )) = simpson f l h
此代码可以编译,因为我在构造函数 MkNumeric 中明确声明类型变量 b 是
[Result]
为什么我必须这样做?我可以不保留类型变量 b 如
data IntegrationType a b where
MkNumeric :: NumericType -> IntegrationType Numeric b
当我执行此操作时出现错误:
Could not deduce (b ~ [Result])
from the context (a ~ Numeric)
bound by a pattern with constructor
MkNumeric :: forall b. NumericType -> IntegrationType Numeric b,
in an equation for `integrate'
at test-classes-new-programm.hs:139:12-64
`b' is a rigid type variable bound by
the type signature for integrate :: IntegrationType a b -> b
at test-classes-new-programm.hs:137:14
Relevant bindings include
integrate :: IntegrationType a b -> b
(bound at test-classes-new-programm.hs:138:1)
In the expression: trap f l h
In an equation for `integrate':
integrate (MkNumeric (MkSingleCoreTanhSinh Closed Trapez f l h))
= trap f l h
类型
integrate :: IntegrationType a b -> b
表示,对于我选择的 any a
和 b
,如果我调用 integrate
类型为 IntegrationType a b
,我会得到一个 b
类型的值。当你定义
MkNumeric :: NumericType -> IntegrationType Numeric b
你让应用构造函数的人决定 b
是什么。所以我可以使用 MkNumeric
来制造一个值,比如说,类型 IntegrationType Numeric Int
。但是你的 integrate
知道如何生成 [Result]
,而不是 Int
。
在working代码中,只要integrate
"opens up the evidence box"通过匹配MkNumeric
,它就知道b ~ [Result]
和因此它可以 return 那种类型的东西。
我有一个函数有问题,我想使其具有多态性。 我想以分析或数字方式整合功能。在分析整合时,我提供结果。 在数字情况下,我想使用各种方法,目前,tanhsinh 中的 tanhsinh 例程。 我也想了解更多关于gadts的知识,所以我试图找到一个使用它们的解决方案。
到目前为止我有以下内容:
import qualified Data.VectorSpace as DV
import Numeric.Integration.TanhSinh
data IntegrationType a b where
MkAnalytic :: (DV.AdditiveGroup b) => (c -> b) -> c -> c -> IntegrationType Analytic b
MkNumeric :: NumericType -> IntegrationType Numeric [Result]
data Analytic = Analytic
data Numeric = Numeric
data Method = Trapez | Simpson
data IntBounds = Closed | NegInfPosInf | ZeroInf
data NumericType = MkSingleCoreTanhSinh IntBounds Method (Double -> Double) Double Double
| MkParallelTanhSinhExplicit IntBounds (Strategy [Double]) Method (Double -> Double) Double Double
| MkParallelTanhSinh IntBounds Method (Double -> Double) Double Double
integrate :: IntegrationType a b -> b
integrate (MkAnalytic f l h) = f h DV.^-^ f l
integrate (MkNumeric (MkSingleCoreTanhSinh Closed Trapez f l h )) = trap f l h
integrate (MkNumeric (MkSingleCoreTanhSinh Closed Simpson f l h )) = simpson f l h
此代码可以编译,因为我在构造函数 MkNumeric 中明确声明类型变量 b 是
[Result]
为什么我必须这样做?我可以不保留类型变量 b 如
data IntegrationType a b where
MkNumeric :: NumericType -> IntegrationType Numeric b
当我执行此操作时出现错误:
Could not deduce (b ~ [Result]) from the context (a ~ Numeric) bound by a pattern with constructor MkNumeric :: forall b. NumericType -> IntegrationType Numeric b, in an equation for `integrate' at test-classes-new-programm.hs:139:12-64 `b' is a rigid type variable bound by the type signature for integrate :: IntegrationType a b -> b at test-classes-new-programm.hs:137:14 Relevant bindings include integrate :: IntegrationType a b -> b (bound at test-classes-new-programm.hs:138:1) In the expression: trap f l h In an equation for `integrate': integrate (MkNumeric (MkSingleCoreTanhSinh Closed Trapez f l h)) = trap f l h
类型
integrate :: IntegrationType a b -> b
表示,对于我选择的 any a
和 b
,如果我调用 integrate
类型为 IntegrationType a b
,我会得到一个 b
类型的值。当你定义
MkNumeric :: NumericType -> IntegrationType Numeric b
你让应用构造函数的人决定 b
是什么。所以我可以使用 MkNumeric
来制造一个值,比如说,类型 IntegrationType Numeric Int
。但是你的 integrate
知道如何生成 [Result]
,而不是 Int
。
在working代码中,只要integrate
"opens up the evidence box"通过匹配MkNumeric
,它就知道b ~ [Result]
和因此它可以 return 那种类型的东西。