我創建了一些函數來獲得2D幾何的一些舒適。 在這個例子中我使用Geom2D from CubicBezier package。Haskell:約束中的非類型變量參數
我的程序的完整代碼:https://gist.github.com/nskeip/3784d651ac646a67c5f246f048949af4
{-# LANGUAGE MultiParamTypeClasses, FlexibleInstances, FlexibleContexts #-}
import Geom2D
left :: (Num a) => Point a -> a -> Point a
left (Point x y) n = Point (x - n) y
right :: (Num a) => Point a -> a -> Point a
right (Point x y) n = Point (x + n) y
up :: (Num a) => Point a -> a -> Point a
up (Point x y) n = Point x (y - n)
down :: (Num a) => Point a -> a -> Point a
down (Point x y) n = Point x (y + n)
他們的工作是這樣的:
> (Point 0 0) `up` 10
Point 0.0 -10.0
凡Point
這樣定義:
data Point a = Point {
pointX :: !a,
pointY :: !a
} deriving (Eq, Ord, Functor, Foldable, Traversable)
一切都還順利,直到我心想:「嘿,這個功能會很好S(actualy,運營商)與像Line
thigs工作 - 不僅Point
「
所以我聲明一個類(不採取left
和right
保持thigs簡單):
class Num n => Moving p n where
up' :: n -> p -> p
down' :: n -> p -> p
up' n = down' (-n)
down' n = up' (-n)
併爲Moving
實例Point a
數據類型:
instance Num a => Moving (Point a) a where
up' n (Point x y) = Point x (y - n)
但是當我嘗試使用它,我得到了一個錯誤:
✗ ghci ./uno.hs
GHCi, version 7.10.3: http://www.haskell.org/ghc/ :? for help
[1 of 1] Compiling Main (uno.hs, interpreted)
Ok, modules loaded: Main.
*Main> let p = Point { pointX = 0, pointY = 0 }
*Main> up' 10 p
<interactive>:3:1:
Non type-variable argument in the constraint: Moving (Point a) n
(Use FlexibleContexts to permit this)
When checking that ‘it’ has the inferred type
it :: forall n a. (Num a, Moving (Point a) n) => Point a
而讓我困惑的事情是:我把FlexibleContexts
編譯指向了編譯列表中的頭部,但ghcu仍然建議我將它包含在內。
如何修復我的類/實例以獲取參數多態性的工作? :)
但它可能不在'ghci'中,你應該用'ghci -XFlexibleContexts file.hs'運行它。 –