我想定义一个新的抽象数据类型,它可以是通用数字或除法构造。我将如何在 Haskell 中做到这一点?
我的第一种方法是:
data MyMath = MyNum Num
| Div MyMath MyMath
问题是编译器抱怨“Num”不是数据类型而是类型类。所以我的第二个想法是解决这样的问题:
data MyMath = MyNum Int
| MyNum Float
| Div MyMath MyMath
但这也行不通,因为 MyNum 被使用了两次,这是不允许的,而且这种方法实际上并不是多态的。那么解决这个问题的方法是什么?
EDIT2:(再次)阅读答案后,我尝试使用 GADT 数据构造函数。这是一些人为的示例代码:
5 data MyMathExpr a where
6 MyNumExpr :: Num a => a -> MyMathExpr a
7 MyAddExpr :: MyMathExpr b -> MyMathExpr c -> MyMathExpr (b, c)
8 deriving instance Show(MyMathExpr a)
9 deriving instance Eq(MyMathExpr a)
10
11 data MyMathVal a where
12 MyMathVal :: Num a => a -> MyMathVal a
13 deriving instance Show(MyMathVal a)
14 deriving instance Eq(MyMathVal a)
15
16 foo :: MyMathExpr a -> MyMathVal a
17 foo (MyNumExpr num) = MyMathVal num
18 foo (MyAddExpr num1 num2) = MyMathVal (l + r)
19 where (MyMathVal l) = foo num1
20 (MyMathVal r) = foo num2
但是第 18 行有问题:
test.hs:18:40:
Couldn't match type `b' with `(b, c)'
`b' is a rigid type variable bound by
a pattern with constructor
MyAddExpr :: forall b c.
MyMathExpr b -> MyMathExpr c -> MyMathExpr (b, c),
in an equation for `foo'
at test.hs:18:6
In the first argument of `(+)', namely `l'
In the first argument of `MyMathVal', namely `(l + r)'
In the expression: MyMathVal (l + r)
'c' 也是如此。我想这是一个愚蠢的错误,我只是没有看到。你?