我试图实现由点一个类型索引“DrawEnv”类型的类:如何使用GHC MultiParamTypeClass
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TypeSynonymInstances #-}
class Monad m => DrawEnv p m where
box :: p -> p -> m()
clear :: m()
line :: p -> p -> m()
type Pos = (Float,Float)
instance DrawEnv Pos IO where
box p0 p1 = putStrLn $ "Box " ++ show p0 ++ " " ++ show p1
clear = putStrLn "Clear"
line p0 p1 = putStrLn $ "Line " ++ show p0 ++ " " ++ show p1
draw :: DrawEnv Pos m => m()
draw = do
clear
box (10.0,10.0) (100.0,100.0)
line (10.0,10.0) (100.0,50.0)
但是GHC是不开心:
Could not deduce (DrawEnv (t0, t1) m) arising from a use of `box'
from the context (DrawEnv Pos m)
bound by the type signature for draw :: DrawEnv Pos m => m()
at Code/Interfaces3.hs:63:9-29
The type variables `t0', `t1' are ambiguous
Relevant bindings include
draw :: m() (bound at Code/Interfaces3.hs:64:1)
Note: there is a potential instance available:
instance DrawEnv Pos IO -- Defined at Code/Interfaces3.hs:56:10
In a stmt of a 'do' block: box (10.0, 10.0) (100.0, 100.0)
In the expression:
do { clear;
box (10.0, 10.0) (100.0, 100.0);
line (10.0, 10.0) (100.0, 50.0) }
In an equation for `draw':
draw
= do { clear;
box (10.0, 10.0) (100.0, 100.0);
line (10.0, 10.0) (100.0, 50.0) }
我的问题是,为什么考虑到Pos限制,GHC不接受这个问题?
我没有GHC 7.8和7.10的任何错误。 –