gpt4 book ai didi

haskell - Haskell 中的通用型变压器

转载 作者:行者123 更新时间:2023-12-02 15:10:50 25 4
gpt4 key购买 nike

从逻辑上讲,可以定义通用转换函数,可以从任何类型转换为任何类型。

可能的方法是:

{-#LANGUAGE MultiParamTypeClasses #-}
{-#LANGUAGE FlexibleInstances #-}

class FromTo a b where
fromTo:: a->b

instance FromTo a a where fromTo = id

instance FromTo Int Double where fromTo = fromIntegral
instance FromTo Int Float where fromTo = fromIntegral
instance FromTo Integer Double where fromTo = fromIntegral
instance FromTo Integer Float where fromTo = fromIntegral

instance FromTo Double Int where fromTo = round
instance FromTo Double Integer where fromTo = round
instance FromTo Float Int where fromTo = round
instance FromTo Float Integer where fromTo = round
-- e.t.c.

嗯,它有效,而且是可扩展的。但它非常庞大,因为我必须列出我想要使用的任何情况。

有什么好的解决方案吗?

<小时/>

如果正确的话,一个简洁的解决方案可以像这样完成(但事实并非如此):

{-#LANGUAGE MultiParamTypeClasses #-}
{-#LANGUAGE FlexibleInstances #-}
{-#LANGUAGE InstanceSigs #-}

class FromTo a b where
fromTo:: a->b

instance (Integral a, Num b) => FromTo a b where
fromTo::a->b
fromTo x = (fromIntegral x)

{---Commented, because addition breaks program.-------------------------------
instance (RealFrac a, Integral b) => FromTo a b where
fromTo::a->b
fromTo x = (round x)
-}

如果有一些类型集扩展(类似 Haskell 的伪代码),也许这是可能的:

{-#LANGUAGE MultiParamTypeClasses #-}
{-#LANGUAGE FlexibleInstances #-}
{-#LANGUAGE InstanceSigs #-}
{-#LANGUAGE TypeSets #-}

class FromTo a b where
fromTo:: a->b

instance setfrom (Integral a, Num b). (Integral a, Num b) => FromTo a b where
fromTo::a->b
fromTo x = (fromIntegral x)

instance setfrom (RealFrac a, Integral b). (RealFrac a, Integral b) => FromTo a b where
fromTo::a->b
fromTo x = (round x)

setfrom C1 a. 此处应使用 C1 类中的实例信息定义类型集。编译器应该检查实例是否相交。此扩展的另一种可能的构造是 set (T1,T2,...,TN) a.,它允许仅定义类型集。

<小时/>

更新1

<小时/>

第一个解决方案可以通过这种方式改进(但这是不正确的方式):

{-#LANGUAGE MultiParamTypeClasses #-}
{-#LANGUAGE FlexibleInstances #-}


class FromTo a b where
fromTo:: a->b

instance FromTo a a where fromTo = id

instance Num b => FromTo Int b where
fromTo x = fromIntegral x

instance Num b => FromTo Integer b where
fromTo x = fromIntegral x

instance Integral b => FromTo Float b where
fromTo x = round x

instance Integral b => FromTo Double b where
fromTo x = round x

但它仍然不好,此外,在交互模式下调用时会出现重叠:

*Main> fromTo (10::Double) ::Double

<interactive>:108:1:
Overlapping instances for FromTo Double Double
arising from a use of `fromTo'
Matching instances:
instance FromTo a a -- Defined at 4.hs:8:10
instance Integral b => FromTo Double b -- Defined at 4.hs:19:10
In the expression: fromTo (10 :: Double) :: Double
In an equation for `it': it = fromTo (10 :: Double) :: Double

最佳答案

据我了解,您希望通过类型的约束来参数化类实例。这可以通过现代 GHC 扩展实现:

{-#LANGUAGE MultiParamTypeClasses, FlexibleInstances, InstanceSigs, ConstraintKinds, 
KindSignatures, DataKinds, TypeOperators, UndecidableInstances, GADTs #-}

import GHC.Prim(Constraint)


class ConstrainedBy (cons :: [* -> Constraint]) (t :: *) where

instance ConstrainedBy '[] t
instance (x t, ConstrainedBy xs t) => ConstrainedBy (x ': xs) t

此类的目的是允许对 FromTo 类中的单个类型进行多个约束。例如,您可以决定 Num a, Real a => Floating a 具有与 Num a => Floating a 不同的实例(这是一个人为的示例 - 但取决于根据您的用例,您可能需要此功能)。

现在我们使用 GADT 将此类“提升”到数据级别:

data ConsBy cons t where 
ConsBy :: ConstrainedBy cons t => t -> ConsBy cons t

instance Show t => Show (ConsBy cons t) where
show (ConsBy t) = "ConsBy " ++ show t

然后,FromTo 类:

class FromTo (consa:: [* -> Constraint]) (a :: *) (consb :: [* -> Constraint]) (b :: *) where
fromTo :: ConsBy consa a -> ConsBy consb b

我不认为有办法获得您为函数 fromTo 指定的类型;如果类型只是 a -> b,则无法从函数参数中推断出约束。

以及您的实例:

instance (Integral a, Num b) => FromTo '[Integral] a '[Num] b where
fromTo (ConsBy x) = ConsBy (fromIntegral x)

instance (RealFrac a, Integral b) => FromTo '[RealFrac] a '[Integral] b where
fromTo (ConsBy x) = ConsBy (round x)

不幸的是,您必须将所有限制条件陈述两次。然后:

>let x = ConsBy 3 :: Integral a => ConsBy '[Integral] a
>x
ConsBy 3
>fromTo x :: ConsBy '[Num] Float
ConsBy 3.0

您可以拥有通常被视为“重叠”的实例:

instance (Integral a, Eq b, Num b) => FromTo '[Integral] a '[Num, Eq] b where
fromTo (ConsBy x) = ConsBy (fromIntegral x + 1) -- obviously stupid

>let x = ConsBy 3 :: Integral a => ConsBy '[Integral] a
>fromTo x :: Num a => ConsBy '[Num] a
ConsBy 3
>fromTo x :: (Num a, Eq a) => ConsBy '[Num, Eq] a
ConsBy 4

另一方面,如果您希望断言只有一个实例可以匹配类型和约束的组合(使上述不可能),您可以使用函数依赖来执行此操作:

{-# LANGUAGE FunctionalDependencies #-} 

class FromTo (consa:: [* -> Constraint]) (a :: *) (consb :: [* -> Constraint]) (b :: *)
| consa a -> consb b, consb b -> consa a
where
fromTo :: ConsBy consa a -> ConsBy consb b

现在我编写的第三个实例无效,但是,您可以使用 fromTo 而无需显式类型注释:

>let x = ConsBy 3 :: Integral a => ConsBy '[Integral] a
>fromTo x
ConsBy 3
>:t fromTo x
fromTo x
:: Num b =>
ConsBy ((':) (* -> Constraint) Num ('[] (* -> Constraint))) b

如您所见,输出类型 Num b => b 是根据输入类型推断出来的。这对于多态类型和具体类型是相同的:

>let x = ConsBy 3 :: ConsBy '[Integral] Int
>:t fromTo x
fromTo x
:: Num b =>
ConsBy ((':) (* -> Constraint) Num ('[] (* -> Constraint))) b

关于haskell - Haskell 中的通用型变压器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20270883/

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