Ниже приведен код отсюда Развлечение с функциями типов
{-# LANGUAGE MultiParamTypeClasses, FlexibleInstances, FlexibleContexts, TypeFamilies #-}
-- Start basic
class Add a b where
type SumTy a b
add :: a -> b -> SumTy a b
instance Add Integer Double where
type SumTy Integer Double = Double
add x y = fromIntegral x + y
instance Add Double Integer where
type SumTy Double Integer = Double
add x y = x + fromIntegral y
instance (Num a) => Add a a where
type SumTy a a = a
add x y = x + y
-- End basic
Это код, который я пытаюсь запустить:
main = print $ show (add 1 1)
Вот результат:
No instance for (Show (SumTy a0 b0))
arising from a use of `show'
Possible fix: add an instance declaration for (Show (SumTy a0 b0))
In the second argument of `($)', namely `show (add 1 1)'
In the expression: print $ show (add 1 1)
In an equation for `main': main = print $ show (add 1 1)
Я пробовал несколько вещей, таких как размещение «данных» везде:
Результат 1
Not a data constructor: `a'
Результат 2 (после удаления "instance (Num a)")
Multiple declarations of `Double'
Declared at: ...
как добавление некоторой функции:
class Add a b where
type SumTy a b
add :: a -> b -> SumTy a b
s :: SumTy a b -> String
instance Add Integer Double where
type SumTy Integer Double = Double
add x y = fromIntegral x + y
s (SumTy _ x) = show x
main = print $ show (s (add 1 2.0) )
с этим результатом:
Not in scope: data constructor `SumTy'
Как вы могли заметить, я застрял, поэтому любая помощь бесценна для меня. :)