Haskell 中存在类型的编译问题
Compilation issue with existential types in Haskell
我写了一个简单的类型class Shape
:
class Shape a where
draw :: a -> IO ()
move :: (Double,Double) -> a -> a
area :: a -> Double
circum :: a -> Double
我还有具体类型 Circle
、Rect
和 Triangle
可以实例化此类型 class,如下所示:
data Circle = Circle Point Double deriving (Show)
instance Shape Circle where
draw (Circle centre radius) = putStrLn $ "Circle [" ++ show centre ++ ", " ++ show radius ++ "]"
move (x,y) (Circle centre radius) = Circle (movePoint x y centre) radius
area (Circle _ r) = r ^ 2 * pi
circum (Circle _ r) = 2 * r * pi
movePoint :: Double -> Double -> Point -> Point
movePoint x y (Point x_a y_a) = Point (x_a + x) (y_a + y)
为了使用包含具体类型 Circle
、Rect
和 Triangle
实例的异构列表,我遵循 the haskell wiki tutorial on heterogenous collections 并实现了一个存在数据类型 ShapeType
像这样:
{-# LANGUAGE ExistentialQuantification #-}
data ShapeType = forall a . Shape a => MkShape a
我让 ShapeType
实例化 Shape
类型 class:
instance Shape ShapeType where
area (MkShape s) = area s
circum (MkShape s) = circum s
draw (MkShape s) = draw s
move (x,y) (MkShape s) = move (x,y) s -- this does not compile
现在我可以像下面这样在代码中使用它了:
rect = Rect (Point 0 0) (Point 5 4)
circle = Circle (Point 4 5) 4
triangle = Triangle (Point 0 0) (Point 4 0) (Point 4 3)
shapes :: [ShapeType]
shapes = [MkShape rect, MkShape circle, MkShape triangle]
main = do
print $ map area shapes
print $ map circum shapes
mapM_ draw shapes
不幸的是,这只有在我省略行时才能编译
move (x,y) (MkShape s) = move (x,y) s
否则会出现以下编译错误:
error:
* Couldn't match expected type `ShapeType' with actual type `a'
`a' is a rigid type variable bound by
a pattern with constructor:
MkShape :: forall a. Shape a => a -> ShapeType,
in an equation for `move'
at C:\workspace\FPvsOO\src\Lib.hs:102:15-23
* In the expression: move (x, y) s
In an equation for `move': move (x, y) (MkShape s) = move (x, y) s
In the instance declaration for `Shape ShapeType'
* Relevant bindings include
s :: a (bound at C:\workspace\FPvsOO\src\Lib.hs:102:23)
这对我来说没有意义,因为在其他三种情况下,通过模式匹配“提取”s
以供在委托调用中使用效果很好。
知道这里出了什么问题吗?
更新
通过这个简单的修复,代码现在可以按预期工作:
instance Shape ShapeType where
area (MkShape s) = area s
circum (MkShape s) = circum s
draw (MkShape s) = draw s
move vec (MkShape s) = MkShape (move vec s)
您错过了构造函数。你需要
move v (MkShape s) = MkShape $ move v s
我不太相信你这里的方法真的是最好的;在这种情况下,存在主义类型往往只会搞砸作品。您至少应该考虑一个普通的旧总和类型。存在主义对于某些目的来说是无价的,但在其他情况下它们是有害的。
我写了一个简单的类型class Shape
:
class Shape a where
draw :: a -> IO ()
move :: (Double,Double) -> a -> a
area :: a -> Double
circum :: a -> Double
我还有具体类型 Circle
、Rect
和 Triangle
可以实例化此类型 class,如下所示:
data Circle = Circle Point Double deriving (Show)
instance Shape Circle where
draw (Circle centre radius) = putStrLn $ "Circle [" ++ show centre ++ ", " ++ show radius ++ "]"
move (x,y) (Circle centre radius) = Circle (movePoint x y centre) radius
area (Circle _ r) = r ^ 2 * pi
circum (Circle _ r) = 2 * r * pi
movePoint :: Double -> Double -> Point -> Point
movePoint x y (Point x_a y_a) = Point (x_a + x) (y_a + y)
为了使用包含具体类型 Circle
、Rect
和 Triangle
实例的异构列表,我遵循 the haskell wiki tutorial on heterogenous collections 并实现了一个存在数据类型 ShapeType
像这样:
{-# LANGUAGE ExistentialQuantification #-}
data ShapeType = forall a . Shape a => MkShape a
我让 ShapeType
实例化 Shape
类型 class:
instance Shape ShapeType where
area (MkShape s) = area s
circum (MkShape s) = circum s
draw (MkShape s) = draw s
move (x,y) (MkShape s) = move (x,y) s -- this does not compile
现在我可以像下面这样在代码中使用它了:
rect = Rect (Point 0 0) (Point 5 4)
circle = Circle (Point 4 5) 4
triangle = Triangle (Point 0 0) (Point 4 0) (Point 4 3)
shapes :: [ShapeType]
shapes = [MkShape rect, MkShape circle, MkShape triangle]
main = do
print $ map area shapes
print $ map circum shapes
mapM_ draw shapes
不幸的是,这只有在我省略行时才能编译
move (x,y) (MkShape s) = move (x,y) s
否则会出现以下编译错误:
error:
* Couldn't match expected type `ShapeType' with actual type `a'
`a' is a rigid type variable bound by
a pattern with constructor:
MkShape :: forall a. Shape a => a -> ShapeType,
in an equation for `move'
at C:\workspace\FPvsOO\src\Lib.hs:102:15-23
* In the expression: move (x, y) s
In an equation for `move': move (x, y) (MkShape s) = move (x, y) s
In the instance declaration for `Shape ShapeType'
* Relevant bindings include
s :: a (bound at C:\workspace\FPvsOO\src\Lib.hs:102:23)
这对我来说没有意义,因为在其他三种情况下,通过模式匹配“提取”s
以供在委托调用中使用效果很好。
知道这里出了什么问题吗?
更新
通过这个简单的修复,代码现在可以按预期工作:
instance Shape ShapeType where
area (MkShape s) = area s
circum (MkShape s) = circum s
draw (MkShape s) = draw s
move vec (MkShape s) = MkShape (move vec s)
您错过了构造函数。你需要
move v (MkShape s) = MkShape $ move v s
我不太相信你这里的方法真的是最好的;在这种情况下,存在主义类型往往只会搞砸作品。您至少应该考虑一个普通的旧总和类型。存在主义对于某些目的来说是无价的,但在其他情况下它们是有害的。