Я думал, что GADT были великолепны, пока я не попытался применить примеры «выражения с GADT», разбросанные по всему Интернету.Получение тривиального класса Eq из GADT
Традиционные ADT любезно предоставляют определение равенства в виде Eq бесплатно. В GADTs для этого кода:
data Expr a where
(:+:) :: (Show a, Eq a) => Expr a -> Expr a -> Expr a
(:-:) :: (Show a, Eq a) => Expr a -> Expr a -> Expr a
(:*:) :: (Show a, Eq a) => Expr a -> Expr a -> Expr a
(:/:) :: (Show a, Eq a) => Expr a -> Expr a -> Expr a
(:=:) :: (Show a, Eq a) => Expr a -> Expr a -> Expr Bool
(:<:) :: (Show a, Eq a) => Expr a -> Expr a -> Expr Bool
(:>:) :: (Show a, Eq a) => Expr a -> Expr a -> Expr Bool
(:>=:) :: (Show a, Eq a) => Expr a -> Expr a -> Expr Bool
(:<=:) :: (Show a, Eq a) => Expr a -> Expr a -> Expr Bool
(:<>:) :: (Show a, Eq a) => Expr a -> Expr a -> Expr Bool
EOr :: Expr Bool -> Expr Bool -> Expr Bool
EAnd :: Expr Bool -> Expr Bool -> Expr Bool
ENot :: Expr Bool -> Expr Bool
ESymbol :: (Show a, Eq a) => String -> Expr a
ELiteral :: (Show a, Eq a) => a -> Expr a
EFunction :: (Show a, Eq a) => String -> [Expr a] -> Expr a
deriving (Eq)
я (очень понятно):
• Can't make a derived instance of ‘Eq (Expr a)’:
Constructor ‘:+:’ has existentials or constraints in its type
Constructor ‘:-:’ has existentials or constraints in its type
Constructor ‘:*:’ has existentials or constraints in its type
Constructor ‘:/:’ has existentials or constraints in its type
Constructor ‘:=:’ has existentials or constraints in its type
Constructor ‘:<:’ has existentials or constraints in its type
Constructor ‘:>:’ has existentials or constraints in its type
Constructor ‘:>=:’ has existentials or constraints in its type
Constructor ‘:<=:’ has existentials or constraints in its type
Constructor ‘:<>:’ has existentials or constraints in its type
Constructor ‘EOr’ has existentials or constraints in its type
Constructor ‘EAnd’ has existentials or constraints in its type
Constructor ‘ENot’ has existentials or constraints in its type
Constructor ‘ESymbol’ has existentials or constraints in its type
Constructor ‘ELiteral’ has existentials or constraints in its type
Constructor ‘EFunction’ has existentials or constraints in its type
Possible fix: use a standalone deriving declaration instead
• In the data declaration for ‘Expr’
Что бы понятно, если бы я не имел Eq
ограничение для каждого конструктора, но теперь я должен написать тривиальное равенство правила для всех этих конструкторов.
Я чувствую, что есть лучший способ пойти об этом, чем я
Почему у вас есть все эти ограничения для конструкторов в первую очередь? – Cubic
Я хочу, чтобы получить представление LaTeX с Show. Где я должен иметь ограничения? Действительно, теперь, когда я думаю об этом, некоторые из них не полезны, но я не могу придумать, как полностью покончить с экзистенциальными. – fakedrake